AzSDK.Test/TestCases/ContinuousCompliance/ContinuousAssurance/ContinuousAssurance.ps1
Set-StrictMode -Version Latest class ContinuousAssurance:AzSDKTestBase{ [string]$AutomationAccountName = "AzSDKContinuousAssurance" #This is the standard name used by AzSDK for Alerts RG. [string]$AutomationAccountRG = "AzSDKRG" #This is the standard name used by AzSDK for Alerts RG. [string]$ResourceGroupName = "AzSDKTestRG" #This is the standard name used for testing resource in subscription. ContinuousAssurance([TestCase] $testcase, [TestSettings] $testsettings):Base($testcase, $testsettings){ } [void] Execute(){ switch ($this.testcase.TestMethod.Trim()){ "TestInstallAzSDKContinuousAssuranceCleanInstall"{ $this.TestInstallAzSDKContinuousAssuranceCleanInstall() break } "TestInstallAzSDKContinuousAssuranceExisting"{ $this.TestInstallAzSDKContinuousAssuranceExisting() break } "TestUpdateAzSDKContinuousAssuranceClean"{ $this.TestUpdateAzSDKContinuousAssuranceClean() break } "TestUpdateAzSDKContinuousAssuranceExistingAll"{ $this.TestUpdateAzSDKContinuousAssuranceExistingAll() break } "TestUpdateAzSDKContinuousAssuranceExistingDefault"{ $this.TestUpdateAzSDKContinuousAssuranceExistingDefault() break } "TestRemoveAzSDKContinuousAssuranceExisting"{ $this.TestRemoveAzSDKContinuousAssuranceExisting() break } "TestRemoveAzSDKContinuousAssuranceWithStorageStorage"{ $this.TestRemoveAzSDKContinuousAssuranceWithStorageStorage() break } "TestRemoveAzSDKContinuousAssuranceNotExisting"{ $this.TestRemoveAzSDKContinuousAssuranceNotExisting() break } Default { } } } [TestCaseResult] TestInstallAzSDKContinuousAssuranceCleanInstall(){ $existingaccount = Get-AzureRMAutomationAccount -ResourceGroupName $this.AutomationAccountRG -ErrorAction silentlycontinue | where-object{$_.AutomationAccountName -eq ($this.AutomationAccountName)} try{ if($existingaccount -ne $null){ #Remove-AzSDKContinuousAssurance -SubscriptionId $this.settings.SubscriptionId -DoNotOpenOutputFolder $this.testcaseResult = [TestCaseResult]::new($this.testCase,[TestStatus]::Manual,[string]::Empty) return $this.testcaseResult } else{ Install-AzSDKContinuousAssurance -SubscriptionId $this.settings.SubscriptionId ` -ResourceGroupNames $this.ResourceGroupName ` -OMSWorkspaceId $this.settings.OMSWorkspaceId ` -OMSSharedKey $this.settings.OMSSharedKey $existingaccount = Get-AzureRMAutomationAccount -ResourceGroupName $this.AutomationAccountRG -ErrorAction silentlycontinue | where-object{$_.AutomationAccountName -eq ($this.AutomationAccountName)} $existingStorage = Find-AzureRmResource -ResourceGroupNameEquals $this.AutomationAccountRG -ResourceNameContains "azsdk" -ResourceType "Microsoft.Storage/storageAccounts" if($existingaccount -ne $null -and $existingStorage -ne $null){ $this.testcaseResult = [TestCaseResult]::new($this.testCase,[TestStatus]::Passed,"Successfully installed AzSDK Continuous Assurance.") } else { $this.testcaseResult = [TestCaseResult]::new($this.testCase,[TestStatus]::Failed,"Something went wrong while installing CA components.") } } } catch{ $this.testcaseResult = [TestCaseResult]::new($this.testCase,[TestStatus]::Failed,"Error while installation of AzSDK Continuous Assurance.") } return $this.testcaseResult } [TestCaseResult] TestInstallAzSDKContinuousAssuranceExisting(){ #Test case needs CA components to already exist as prerequisites, checking the same. $existingaccount = Get-AzureRMAutomationAccount -ResourceGroupName $this.AutomationAccountRG -ErrorAction silentlycontinue | where-object{$_.AutomationAccountName -eq ($this.AutomationAccountName)} if($existingaccount -eq $null){ #Installing the pre-requisite CA components Install-AzSDKContinuousAssurance -SubscriptionId $this.settings.SubscriptionId ` -ResourceGroupNames $this.ResourceGroupName ` -OMSWorkspaceId $this.settings.OMSWorkspaceId ` -OMSSharedKey $this.settings.OMSSharedKey ` -DoNotOpenOutputFolder } try{ #Now trying to re-install the CA components Install-AzSDKContinuousAssurance -SubscriptionId $this.settings.SubscriptionId ` -ResourceGroupNames $this.ResourceGroupName ` -OMSWorkspaceId $this.settings.OMSWorkspaceId ` -OMSSharedKey $this.settings.OMSSharedKey ` -DoNotOpenOutputFolder $this.testcaseResult = [TestCaseResult]::new($this.testCase,[TestStatus]::Failed,"Command failed to throw an error even though CA components are already present in the subscription.") } catch{ $this.testcaseResult = [TestCaseResult]::new($this.testCase,[TestStatus]::Passed,"As CA components are already present in subscription, exception thrown.") } return $this.testcaseResult } [TestCaseResult] TestUpdateAzSDKContinuousAssuranceClean(){ $existingaccount = Get-AzureRMAutomationAccount -ResourceGroupName $this.AutomationAccountRG -ErrorAction silentlycontinue | where-object{$_.AutomationAccountName -eq ($this.AutomationAccountName)} if($existingaccount -ne $null){ try{ #Remove-AzSDKContinuousAssurance -SubscriptionId $this.settings.SubscriptionId -DoNotOpenOutputFolder $this.testcaseResult = [TestCaseResult]::new($this.testCase,[TestStatus]::Manual,"Pre-requisites are not met as CA components already exist, please verify this test case manually.") return $this.testcaseResult } catch { # Error while removing existing CA components } } else{ try{ Update-AzSDKContinuousAssurance -SubscriptionId $this.settings.SubscriptionId -DoNotOpenOutputFolder $this.testcaseResult = [TestCaseResult]::new($this.testCase,[TestStatus]::Failed,"Command failed to throw an error even though no CA components were present in the subscription to update.") } catch{ $this.testcaseResult = [TestCaseResult]::new($this.testCase,[TestStatus]::Passed,"As expected the command threw an error, since no CA components are present in subscription to update.") } } return $this.testcaseResult } [TestCaseResult] TestUpdateAzSDKContinuousAssuranceExistingAll(){ $existingaccount = Get-AzureRMAutomationAccount -ResourceGroupName $this.AutomationAccountRG -ErrorAction silentlycontinue | where-object{$_.AutomationAccountName -eq ($this.AutomationAccountName)} if($existingaccount -eq $null){ try{ Install-AzSDKContinuousAssurance -SubscriptionId $this.settings.SubscriptionId ` -ResourceGroupNames $this.ResourceGroupName ` -OMSWorkspaceId $this.settings.OMSWorkspaceId ` -OMSSharedKey $this.settings.OMSSharedKey ` -DoNotOpenOutputFolder } catch { # Error while installing CA. } } try{ Update-AzSDKContinuousAssurance -SubscriptionId $this.settings.SubscriptionId ` -ResourceGroupNames $this.ResourceGroupName ` -OMSWorkspaceId $this.settings.OMSWorkspaceId ` -OMSSharedKey $this.settings.OMSSharedKey ` -DoNotOpenOutputFolder $aaVariables = Get-AzureRmAutomationVariable -ResourceGroupName 'AzSDKRG' -AutomationAccountName 'AzSDKContinuousAssurance' if((($aaVariables| where-object{$_.Name -contains ('OMSWorkspaceId')}).Value -eq $this.settings.OMSWorkspaceId) -and ` (($aaVariables| where-object{$_.Name -contains ('AppResourceGroupNames')}).Value -eq $this.ResourceGroupName)){ $this.testcaseResult = [TestCaseResult]::new($this.testCase,[TestStatus]::Passed,"CA components are updated successfully.") } else { $this.testcaseResult = [TestCaseResult]::new($this.testCase,[TestStatus]::Failed,"CA components are not updated.") } } catch{ $this.testcaseResult = [TestCaseResult]::new($this.testCase,[TestStatus]::Failed,"Error while updating CA components.") } return $this.testcaseResult } [TestCaseResult] TestUpdateAzSDKContinuousAssuranceExistingDefault(){ $existingaccount = Get-AzureRMAutomationAccount -ResourceGroupName $this.AutomationAccountRG -ErrorAction silentlycontinue | where-object{$_.AutomationAccountName -eq ($this.AutomationAccountName)} if($existingaccount -eq $null){ try{ Install-AzSDKContinuousAssurance -SubscriptionId $this.settings.SubscriptionId ` -ResourceGroupNames $this.ResourceGroupName ` -OMSWorkspaceId $this.settings.OMSWorkspaceId ` -OMSSharedKey $this.settings.OMSSharedKey ` -DoNotOpenOutputFolder } catch { # Error while installing CA. } } try{ Update-AzSDKContinuousAssurance -SubscriptionId $this.settings.SubscriptionId -DoNotOpenOutputFolder $existingaccount = Get-AzureRMAutomationAccount -ResourceGroupName $this.AutomationAccountRG -ErrorAction silentlycontinue | where-object{$_.AutomationAccountName -eq ($this.AutomationAccountName)} if($existingaccount -ne $null -and $existingaccount.LastModifiedTime -ge (Get-Date).AddMinutes(-2)) { $this.testcaseResult = [TestCaseResult]::new($this.testCase,[TestStatus]::Passed,"CA components are updated successfully.") } else { $this.testcaseResult = [TestCaseResult]::new($this.testCase,[TestStatus]::Failed,"CA components are not updated.") } } catch{ $this.testcaseResult = [TestCaseResult]::new($this.testCase,[TestStatus]::Failed,"Error while updating CA components.") } return $this.testcaseResult } [TestCaseResult] TestRemoveAzSDKContinuousAssuranceExisting(){ $existingaccount = Get-AzureRMAutomationAccount -ResourceGroupName $this.AutomationAccountRG -ErrorAction silentlycontinue | where-object{$_.AutomationAccountName -eq ($this.AutomationAccountName)} if($existingaccount -eq $null){ try{ Install-AzSDKContinuousAssurance -SubscriptionId $this.settings.SubscriptionId ` -ResourceGroupNames $this.ResourceGroupName ` -OMSWorkspaceId $this.settings.OMSWorkspaceId ` -OMSSharedKey $this.settings.OMSSharedKey ` -DoNotOpenOutputFolder } catch { $this.testcaseResult = [TestCaseResult]::new($this.testCase,[TestStatus]::Failed,"Unable to test scenario. CA is not present and unable to install on subscription") return $this.testcaseResult } } try{ #Remove-AzSDKContinuousAssurance -SubscriptionId $this.settings.SubscriptionId -DoNotOpenOutputFolder $this.testcaseResult = [TestCaseResult]::new($this.testCase,[TestStatus]::Manual,"Pre-requisites are not met as CA components already exist, please verify this test case manually.") return $this.testcaseResult $account = Get-AzureRMAutomationAccount -ResourceGroupName $this.AutomationAccountRG -ErrorAction silentlycontinue | where-object{$_.AutomationAccountName -eq ($this.AutomationAccountName)} if($account){ $this.testcaseResult = [TestCaseResult]::new($this.testCase,[TestStatus]::Failed,"Error while removing AzSDK Continuous Assurance.") } else{ $this.testcaseResult = [TestCaseResult]::new($this.testCase,[TestStatus]::Passed,"Successfully removed AzSDK Continuous Assurance.") } } catch{ $this.testcaseResult = [TestCaseResult]::new($this.testCase,[TestStatus]::Failed,"Error while removing AzSDK Continuous Assurance.") } return $this.testcaseResult } [TestCaseResult] TestRemoveAzSDKContinuousAssuranceWithStorageStorage(){ $existingaccount = Get-AzureRMAutomationAccount -ResourceGroupName $this.AutomationAccountRG -ErrorAction silentlycontinue | where-object{$_.AutomationAccountName -eq ($this.AutomationAccountName)} if($existingaccount -eq $null){ try{ Install-AzSDKContinuousAssurance -SubscriptionId $this.settings.SubscriptionId ` -ResourceGroupNames $this.ResourceGroupName ` -OMSWorkspaceId $this.settings.OMSWorkspaceId ` -OMSSharedKey $this.settings.OMSSharedKey ` -DoNotOpenOutputFolder } catch { $this.testcaseResult = [TestCaseResult]::new($this.testCase,[TestStatus]::Failed,"Unable to test scenario. CA is not present and unable to install on subscription") return $this.testcaseResult } } try{ #Remove-AzSDKContinuousAssurance -SubscriptionId $this.settings.SubscriptionId -DeleteStorageReports -DoNotOpenOutputFolder $this.testcaseResult = [TestCaseResult]::new($this.testCase,[TestStatus]::Manual,"Pre-requisites are not met as CA components already exist, please verify this test case manually.") return $this.testcaseResult $account = Get-AzureRMAutomationAccount -ResourceGroupName $this.AutomationAccountRG -ErrorAction silentlycontinue | where-object{$_.AutomationAccountName -eq ($this.AutomationAccountName)} $existingStorage = Find-AzureRmResource -ResourceGroupNameEquals $this.AutomationAccountRG -ResourceNameContains "azsdk" -ResourceType "Microsoft.Storage/storageAccounts" if($existingStorage) { $containerName = "azsdkexecutionlogs" $keys = Get-AzureRmStorageAccountKey -ResourceGroupName $this.AutomationAccountRG -Name $existingStorage.Name $storageContext = New-AzureStorageContext -StorageAccountName $existingStorage.Name -StorageAccountKey $keys[0].Value -Protocol Https $existingContainer = Get-AzureStorageContainer -Name $containerName -Context $storageContext -ErrorAction SilentlyContinue if($account -ne $null -or $existingContainer -ne $null){ $this.testcaseResult = [TestCaseResult]::new($this.testCase,[TestStatus]::Failed,"Error while removing AzSDK Continuous Assurance.") } else{ $this.testcaseResult = [TestCaseResult]::new($this.testCase,[TestStatus]::Passed,"Successfully removed AzSDK Continuous Assurance.") } } } catch{ $this.testcaseResult = [TestCaseResult]::new($this.testCase,[TestStatus]::Failed,"Error while removing AzSDK Continuous Assurance.") } return $this.testcaseResult } [TestCaseResult] TestRemoveAzSDKContinuousAssuranceNotExisting(){ try{ $existingaccount = Get-AzureRMAutomationAccount -ResourceGroupName $this.AutomationAccountRG -ErrorAction silentlycontinue | where-object{$_.AutomationAccountName -eq ($this.AutomationAccountName)} if($existingaccount -ne $null){ Remove-AzureRmAutomationAccount -Name $this.AutomationAccountName -ResourceGroupName $this.AutomationAccountRG -Confirm } Remove-AzSDKContinuousAssurance -SubscriptionId $this.settings.SubscriptionId -DoNotOpenOutputFolder -Confirm $account = Get-AzureRMAutomationAccount -ResourceGroupName $this.AutomationAccountRG -ErrorAction silentlycontinue | where-object{$_.AutomationAccountName -eq ($this.AutomationAccountName)} if($account){ $this.testcaseResult = [TestCaseResult]::new($this.testCase,[TestStatus]::Failed,"Error while removing AzSDK Continuous Assurance.") } else{ $this.testcaseResult = [TestCaseResult]::new($this.testCase,[TestStatus]::Passed,"Successfully removed AzSDK Continuous Assurance.") } } catch{ $this.testcaseResult = [TestCaseResult]::new($this.testCase,[TestStatus]::Failed,"Error while removing AzSDK Continuous Assurance.") } return $this.testcaseResult } } |