Controllers/TestRunner.ps1

Set-StrictMode -Version Latest  
class TestRunner{
    [AzSDKTestBase] $testbase 
    [TestCase] $tcase
    [string]$subscriptionId = [string]::Empty

    TestRunner([string]$subId,[TestCase]$testcase,[TestSettings]$testsettings) {
        $this.subscriptionId = $subId
        
        $this.tcase = $testcase
        if($this.tcase.AzureLoginRequired){
            $this.SetAzureContext();
        }
        if($testcase.Feature.ToUpper().Trim() -eq "SVT"){
            switch ($testcase.ModuleName.ToUpper().Trim()) {
                "KEYVAULT" {
                    $this.testbase = [KeyVaultTest]::new($testcase, $testsettings) 
                    break
                }
                "VIRTUALMACHINE" {
                    $this.testbase = [VirtualMachineTest]::new($testcase, $testsettings)
                    break
                }
                "APPSERVICE"{
                    $this.testbase = [AppServiceTest]::new($testcase, $testsettings) 
                    break
                }
                "SEARCH"{
                    $this.testbase = [SearchTest]::new($testcase, $testsettings) 
                    break
                }
                "BATCH"{
                    $this.testbase = [BatchTest]::new($testcase, $testsettings) 
                    break
                }
                "ADLA"{
                    $this.testbase = [ADLATest]::new($testcase, $testsettings)
                    break
                }
                "ADLS"{
                    $this.testbase = [ADLSTest]::new($testcase, $testsettings)
                    break
                }
                "LogicApps"{
                    $this.testbase = [LogicAppsTest]::new($testcase, $testsettings)
                    break
                }
                "SQLDB"{
                    $this.testbase = [SQLDBTest]::new($testcase, $testsettings)
                    break
                }
                "DATAFACTORY"{
                    $this.testbase = [DataFactoryTest]::new($testcase, $testsettings)
                    break
                }
                "STORAGE"{
                    $this.testbase = [StorageTest]::new($testcase, $testsettings)
                    break
                }
                "ANALYSISSERVICES"{
                    $this.testbase = [AnalysisServicesTest]::new($testcase, $testsettings)
                    break
                }
                "VIRTUALNETWORK"{
                    $this.testbase = [VirtualNetworkTest]::new($testcase, $testsettings)
                    break
                }
                "NOTIFICATIONHUB"{
                    $this.testbase = [NotificationHubTest]::new($testcase, $testsettings)
                    break
                }
                "LOADBALANCER"{
                    $this.testbase = [LoadBalancerTest]::new($testcase, $testsettings)
                    break
                }
                "SERVICEBUS"{
                    $this.testbase = [ServiceBusTest]::new($testcase, $testsettings)
                    break
                }
                "CDN"{
                    $this.testbase = [CDNTest]::new($testcase, $testsettings)
                    break
                }
                "TRAFFICMANAGER"{
                    $this.testbase = [TrafficManagerTest]::new($testcase, $testsettings)
                    break
                }
                "FUNCTIONS"{
                    $this.testbase = [FunctionsTest]::new($testcase, $testsettings)
                    break
                }
                "EVENTHUB"{
                    $this.testbase = [EventHubTest]::new($testcase, $testsettings)
                    break
                }
                "SVTCOMMON"{
                    $this.testbase = [SVTCommonTest]::new($testcase, $testsettings)
                    break
                }
                Default {
                    $this.testbase = [SVTTestBase]::new($testcase, $testsettings)
                }
            }
        }

        if($testcase.Feature.ToUpper().Trim() -eq "SUBSCRIPTIONSECURITY"){
            switch ($testcase.ModuleName.Trim()) {
                "SSProvisioning"{
                    $this.testbase = [SSProvisioningTest]::new($testcase, $testsettings)
                    break
                }
                "SSHealth"{
                    $this.testbase = [SSHealthTest]::new($testcase, $testsettings)
                    break
                }
                Default {
                    $this.testbase = [AzSDKTestBase]::new($testcase, $testsettings)
                }
            }
        }

        if($testcase.Feature.ToUpper().Trim() -eq "ALERTMONITORING"){
            switch ($testcase.ModuleName.Trim()) {
                "OMS"{
                    $this.testbase = [OMSTest]::new($testcase, $testsettings)
                    break
                }
                Default {
                    $this.testbase = [AzSDKTestBase]::new($testcase, $testsettings)
                }
            }
        }
        if($testcase.Feature.ToUpper().Trim() -eq "SECURITYINTELLISENSE"){
            switch ($testcase.ModuleName.Trim()) {
                "SecIntel"{
                    $this.testbase = [SecIntelTest]::new($testcase, $testsettings)
                    break
                }
                Default {
                    $this.testbase = [AzSDKTestBase]::new($testcase, $testsettings)
                }
            }
        }
        if($testcase.Feature.ToUpper().Trim() -eq "CICDPIPELINE"){
            switch ($testcase.ModuleName.Trim()) {
                "CICD"{
                    $this.testbase = [CICDTest]::new($testcase, $testsettings)
                    break
                }
                Default {
                    $this.testbase = [AzSDKTestBase]::new($testcase, $testsettings)
                }
            }
        }
        if($testcase.Feature.ToUpper().Trim() -eq "CONTINUOUSCOMPLIANCE"){
            switch ($testcase.ModuleName.ToUpper().Trim()) {
                "CONTINUOUSASSURANCE"{
                    $this.testbase = [ContinuousAssurance]::new($testcase, $testsettings)
                    break
                }
                Default {
                    $this.testbase = [AzSDKTestBase]::new($testcase, $testsettings)
                }
            }

        }
    }
    
    [TestCaseResult] RunTestCase(){

        $this.testbase.Initialize()
        $testCaseResult = [TestCaseResult]::new($this.tcase)
        $this.testbase.Execute()
        $testCaseResult =  $this.testbase.testCaseResult
        $this.testbase.Cleanup()
        return $testCaseResult

    }

    [void] SetAzureContext(){
        $isContextNull = $false;
        $currentContext = $null;
        try 
        { 
            $currentContext = Get-AzureRmContext -ErrorAction Stop   
        } 
        catch  
        { 
            $isContextNull = $true 
        }

        if(($null -eq $currentContext) -or $isContextNull -or ($null -eq $currentContext.Subscription))
        {
            [CommonHelper]::Log("No active Azure login session found. Initiating login flow...",[MessageType]::Warning)

            $currentContext = Add-AzureRmAccount

            try 
            { 
                $currentContext = Get-AzureRmContext -ErrorAction Stop   
            } 
            catch  
            { 
                throw [System.ArgumentException] ("Subscription Id [" + $this.subscriptionId + "] is invalid or you may not have permissions.")
            }
        }

        if($currentContext.Subscription.Id -ne $this.subscriptionId)
        {
            $currentContext = Set-AzureRmContext -SubscriptionId $this.subscriptionId -ErrorAction Stop   
            
            # $currentContext will contain the desired subscription (or $null if id is wrong or no permission)
            if ($null -eq $currentContext)
            {
                throw [System.ArgumentException] ("Invalid Subscription Id: [" + $this.subscriptionId + "]")
            }
        }        
    }

}