AzSDK.Test/TestCases/AlertMonitoring/OMS/OMSTest.ps1

Set-StrictMode -Version Latest 
class OMSTest: AzSDKTestBase
{

    OMSTest([TestCase] $testcase, [TestSettings] $testsettings):Base($testcase, $testsettings)
    { }

    [string] $AzSdkSettingsPath = $Env:LOCALAPPDATA + "\Microsoft\AzSDK\AzSdkSettings.json"
    [string] $OMSRG = "mms-sea";
    [string] $OMSWorkspaceName = "azsdkomstestbed";

    [void] Execute()
    {
        switch ($this.testcase.TestMethod.Trim())
        {
            "TestSetAzSDKOMSSettings"{
                $this.TestSetAzSDKOMSSettings()
                break
            }
            "TestSetAzSDKOMSSettingsWithSource"{
                $this.TestSetAzSDKOMSSettingsWithSource()
                break
            }
            "TestSetAzSDKOMSSettingsDisable"{
                $this.TestSetAzSDKOMSSettingsDisable()
                break
            }
            "TestInstallAzSDKOMSSolutionAll"{
                $this.TestInstallAzSDKOMSSolution("All")
                break
            }
            "TestInstallAzSDKOMSSolutionQueries"{
                $this.TestInstallAzSDKOMSSolution("Queries")
                break
            }
            "TestInstallAzSDKOMSSolutionAlerts"{
                $this.TestInstallAzSDKOMSSolution("Alerts")
                break
            }
            "TestInstallAzSDKOMSSolutionSampleView"{
                $this.TestInstallAzSDKOMSSolution("SampleView")
                break
            }
            "TestUninstallAzSDKOMSetup"{
                $this.TestUninstallAzSDKOMSetup()
                break
            }
        }
    }

    [TestCaseResult] TestSetAzSDKOMSSettings()
    {
        try
        {
            Set-AzSDKOMSSettings -OMSWorkspaceId $this.settings.OMSWorkspaceId -OMSSharedKey $this.settings.OMSSharedKey
            $fileContent = (Get-Content -Raw -Path $this.AzSdkSettingsPath) | ConvertFrom-Json;

            $result = [TestStatus]::Passed;
            $message = "";
            if($fileContent.OMSWorkspaceId -ne $this.settings.OMSWorkspaceId)
            {
                $result = [TestStatus]::Failed;
                $message += "OMSWorkspaceId doesn't match. ";
            }

            if($fileContent.OMSSharedKey -ne $this.settings.OMSSharedKey)
            {
                $result = [TestStatus]::Failed;
                $message += "OMSSharedKey doesn't match. ";
            }

            if([string]::IsNullOrWhiteSpace($message))
            {
                $message = "Successfully updated the OMS data."
            }
            $this.testcaseResult = [TestCaseResult]::new($this.testCase, $result, $message)
        }
        catch
        {
            $this.testcaseResult = [TestCaseResult]::new($this.testCase, [TestStatus]::Failed, "Error while setting OMS data.")
        }

        return $this.testcaseResult
    }

    [TestCaseResult] TestSetAzSDKOMSSettingsWithSource()
    {
        try
        {
            $omsSource = "AzSDKTestSource-" + $(Get-Date -format "yyyyMMdd_HHmmss");
            Set-AzSDKOMSSettings -OMSWorkspaceId $this.settings.OMSWorkspaceId -OMSSharedKey $this.settings.OMSSharedKey -Source $omsSource
            $fileContent = (Get-Content -Raw -Path $this.AzSdkSettingsPath) | ConvertFrom-Json;

            $result = [TestStatus]::Passed;
            $message = "";
            if($fileContent.OMSWorkspaceId -ne $this.settings.OMSWorkspaceId)
            {
                $result = [TestStatus]::Failed;
                $message += "OMSWorkspaceId doesn't match. ";
            }

            if($fileContent.OMSSharedKey -ne $this.settings.OMSSharedKey)
            {
                $result = [TestStatus]::Failed;
                $message += "OMSSharedKey doesn't match. ";
            }

            if($fileContent.OMSSource -ne $omsSource)
            {
                $result = [TestStatus]::Failed;
                $message += "OMSSource doesn't match. ";
            }

            if([string]::IsNullOrWhiteSpace($message))
            {
                $message = "Successfully updated the OMS data with source. "
                Get-AzSDKSubscriptionSecurityStatus -SubscriptionId $this.settings.SubscriptionId -ControlIds "Azure_Subscription_AuthZ_Custom_RBAC_Roles" -DoNotOpenOutputFolder
                $omsLogs = $null;
                for($retries = 6; $retries -ge 0; $retries--)
                {
                    Write-Host "Fetching OMS logs in 5 seconds, Retries left: $retries"
                    Start-Sleep -Seconds 10
                    $omsLogs = Get-AzureRmOperationalInsightsSearchResults -ResourceGroupName $this.OMSRG -WorkspaceName $this.OMSWorkspaceName -Query "Type=AzSDK_CL Source_s=$omsSource" -Top 1
                    if($omsLogs -and ($omsLogs.Value | Measure-Object).Count -ne 0)
                    {
                        break;
                    }
                }

                if($omsLogs -and ($omsLogs.Value | Measure-Object).Count -ne 0)
                {
                    $message += "AzSDK is able to post data in OMS. ";
                }
                else
                {
                    $result = [TestStatus]::Failed;
                    $message += "AzSDK is UNABLE to post data in OMS. ";
                }
            }
            $this.testcaseResult = [TestCaseResult]::new($this.testCase, $result, $message)
        }
        catch
        {
            $this.testcaseResult = [TestCaseResult]::new($this.testCase, [TestStatus]::Failed, "Error while setting OMS data.")
        }

        return $this.testcaseResult
    }    

    [TestCaseResult] TestSetAzSDKOMSSettingsDisable()
    {
        try
        {
            Set-AzSDKOMSSettings -Disable
            $fileContent = (Get-Content -Raw -Path $this.AzSdkSettingsPath) | ConvertFrom-Json;

            $result = [TestStatus]::Passed;
            $message = "";
            if(-not [string]::IsNullOrWhiteSpace($fileContent.OMSWorkspaceId))
            {
                $result = [TestStatus]::Failed;
                $message += "OMSWorkspaceId is not reset. ";
            }

            if(-not [string]::IsNullOrWhiteSpace($fileContent.OMSSharedKey))
            {
                $result = [TestStatus]::Failed;
                $message += "OMSSharedKey is not reset. ";
            }
            
            if([string]::IsNullOrWhiteSpace($message))
            {
                $message = "Successfully disabled the OMS settings."
            }
            $this.testcaseResult = [TestCaseResult]::new($this.testCase, $result, $message)
        }
        catch
        {
            $this.testcaseResult = [TestCaseResult]::new($this.testCase, [TestStatus]::Failed, "Error while setting OMS data.")
        }

        return $this.testcaseResult
    }    

    [TestCaseResult] TestInstallAzSDKOMSSolution($installationOption)
    {
        try
        {
            Install-AzSDKOMSSolution -ApplicationName AzSDKTestRG -ApplicationResourceGroups AzSDKTestRG -ApplicationSubscriptionId $this.settings.SubscriptionId -OMSResourceGroup $this.OMSRG -OMSSubscriptionId $this.settings.SubscriptionId -OMSWorkspaceName $this.OMSWorkspaceName -SecurityContactEmails $this.settings.SecurityPOCEmail -OMSInstallationOption $installationOption
            $deploymentNamePrefix = "AzSDK.AM.OMS.";
            $deploymentNames = @();

            switch($installationOption)
            {
                "All"
                {
                    $deploymentNames += ($deploymentNamePrefix + "Alerts");
                    $deploymentNames += ($deploymentNamePrefix + "Searches");
                    $deploymentNames += ($deploymentNamePrefix + "SampleView");
                }
                "Queries"
                {
                    $deploymentNames += ($deploymentNamePrefix + "Searches");
                }
                "Alerts"
                {
                    $deploymentNames += ($deploymentNamePrefix + "Alerts");
                }
                "SampleView"
                {
                    $deploymentNames += ($deploymentNamePrefix + "SampleView");
                }
                default
                {
                    $deploymentNames += ($deploymentNamePrefix + "Alerts");
                    $deploymentNames += ($deploymentNamePrefix + "Searches");
                    $deploymentNames += ($deploymentNamePrefix + "SampleView");
                }
            }

            #Fetch resource group deployments
            $timeRange = [Datetime]::UtcNow.AddMinutes(-2);
            Start-Sleep -Seconds 10
            $allDeployments = Get-AzureRmResourceGroupDeployment -ResourceGroupName $this.OMSRG

            $successDeployments = @();
            $selectedDeployments = @();
            $deploymentNames | ForEach-Object {
                $name = $_;
                $selectedDeployments += $allDeployments | Where-Object { $_.DeploymentName.StartsWith($name) -and $_.Timestamp -gt $timeRange } | Select-Object -First 1 
            }

            $successDeployments += $selectedDeployments | Where-Object { $_.ProvisioningState -eq "Succeeded" }

            if($successDeployments.Count -eq $deploymentNames.Count)
            {
                $this.testcaseResult = [TestCaseResult]::new($this.testCase, [TestStatus]::Passed, "Successfully installed the OMS views with installation option - $installationOption.")
            }
            else
            {
                $this.testcaseResult = [TestCaseResult]::new($this.testCase, [TestStatus]::Failed, "OMS installation failed - $installationOption.")
            }
        }
        catch
        {
            $this.testcaseResult = [TestCaseResult]::new($this.testCase, [TestStatus]::Failed, "Error while installing OMS views with installation option - $installationOption. `r`n" + $_.ToString())
        }

        return $this.testcaseResult
    }    

    [TestCaseResult] TestUninstallAzSDKOMSetup()
    {
        try
        {
            Uninstall-AzSDKOMSetup -OMSResourceGroup $this.OMSRG -OMSSubscriptionId $this.settings.SubscriptionId -OMSWorkspaceName $this.OMSWorkspaceName
            $savedSearches = Get-AzureRmOperationalInsightsSavedSearch -ResourceGroupName $this.OMSRG -WorkspaceName $this.OMSWorkspaceName
            $azSdkSearches = @()
            if($savedSearches -ne $null -and $savedSearches.Value -ne $null)
            {
                $savedSearches.Value | %{
                    Set-Variable -Name savedSearch -Value $_
                    if($savedSearch.Properties -ne $null -and $savedSearch.Properties.Category -like "*AzSDK*")
                    {
                        $azSdkSearches += $savedSearch
                    }
                }
            }        

            if($azSdkSearches.Count -eq 0)
            {
                $this.testcaseResult = [TestCaseResult]::new($this.testCase, [TestStatus]::Passed, "Successfully uninstalled the OMS views.")
            }
            else
            {
                $this.testcaseResult = [TestCaseResult]::new($this.testCase, [TestStatus]::Failed, "OMS queries are not uninstalled.")
            }
        }
        catch
        {
            $this.testcaseResult = [TestCaseResult]::new($this.testCase, [TestStatus]::Failed, "Error while uninstalling OMS views.`r`n" + $_.ToString())
        }

        return $this.testcaseResult
    }    
}