Пример #1
0
        public void Initialize(string className)
        {
            if (initialized)
                return;

            if (mode == HttpRecorderMode.Record)
            {
                using (MockContext context = MockContext.Start(TestUtilities.GetCallingClass(), TestUtilities.GetCurrentMethodName(1)))
                {
                    var testFactory = new LegacyTest.CSMTestEnvironmentFactory();
                    var testEnv = testFactory.GetTestEnvironment();
                    var resourcesClient = LegacyTest.TestBase.GetServiceClient<ResourceManagementClient>(testFactory);
                    var mgmtClient = context.GetServiceClient<KeyVaultManagementClient>(TestEnvironmentFactory.GetTestEnvironment());
                    var tenantId = testEnv.AuthorizationContext.TenantId;

                    //Figure out which locations are available for Key Vault
                    location = GetKeyVaultLocation(resourcesClient);

                    //Create a resource group in that location
                    preCreatedVault = TestUtilities.GenerateName("pshtestvault");
                    resourceGroupName = TestUtilities.GenerateName("pshtestrg");

                    resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName, new ResourceGroup { Location = location });
                    var createResponse = CreateVault(mgmtClient, location, tenantId);
                }
            }

            initialized = true;
        }
        public void Initialize(string className)
        {
            if (initialized)
            {
                return;
            }

            if (mode == HttpRecorderMode.Record)
            {
                using (MockContext context = MockContext.Start(TestUtilities.GetCallingClass(), TestUtilities.GetCurrentMethodName(1)))
                {
                    var testFactory     = new LegacyTest.CSMTestEnvironmentFactory();
                    var testEnv         = testFactory.GetTestEnvironment();
                    var resourcesClient = LegacyTest.TestBase.GetServiceClient <ResourceManagementClient>(testFactory);
                    var mgmtClient      = context.GetServiceClient <KeyVaultManagementClient>(TestEnvironmentFactory.GetTestEnvironment());
                    var tenantId        = testEnv.AuthorizationContext.TenantId;

                    //Figure out which locations are available for Key Vault
                    location = GetKeyVaultLocation(resourcesClient);

                    //Create a resource group in that location
                    preCreatedVault   = TestUtilities.GenerateName("pshtestvault");
                    resourceGroupName = TestUtilities.GenerateName("pshtestrg");

                    resourcesClient.ResourceGroups.CreateOrUpdate(resourceGroupName, new ResourceGroup {
                        Location = location
                    });
                    var createResponse = CreateVault(mgmtClient, location, tenantId);
                }
            }

            initialized = true;
        }
        /// <summary>
        /// Runs the PowerShell test under mock undo context based on the test mode setting (Record|Playback)
        /// </summary>
        /// <param name="scriptBuilder">Script builder delegate</param>
        /// <param name="initialize">initialize action</param>
        /// <param name="cleanup">cleanup action</param>
        /// <param name="callingClassType">Calling class type</param>
        /// <param name="mockName">Mock Name</param>
        public void RunPsTestWorkflow(
            Func <string[]> scriptBuilder,
            Action <LegacyTest.CSMTestEnvironmentFactory> initialize,
            Action cleanup,
            string callingClassType,
            string mockName)
        {
            Dictionary <string, string> d = new Dictionary <string, string>();

            d.Add("Microsoft.Authorization", AuthorizationApiVersion);
            d.Add("Microsoft.Resources", null);
            d.Add("Microsoft.Features", null);
            var providersToIgnore = new Dictionary <string, string>();

            providersToIgnore.Add("Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01");
            HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(true, d, providersToIgnore);

            HttpMockServer.RecordsDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SessionRecords");
            using (MockContext context = MockContext.Start(callingClassType, mockName))
            {
                this.csmTestFactory = new LegacyTest.CSMTestEnvironmentFactory();
                if (initialize != null)
                {
                    initialize(this.csmTestFactory);
                }
                SetupManagementClients(context);
                helper.SetupEnvironment(AzureModule.AzureResourceManager);

                var callingClassName = callingClassType
                                       .Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries)
                                       .Last();
                helper.SetupModules(AzureModule.AzureResourceManager,
                                    "ScenarioTests\\Common.ps1",
                                    "ScenarioTests\\" + callingClassName + ".ps1",
                                    helper.RMProfileModule,
                                    helper.RMResourceModule,
                                    helper.GetRMModulePath(@"AzureRM.LogicApp.psd1"),
                                    "AzureRM.Resources.ps1");

                try
                {
                    if (scriptBuilder != null)
                    {
                        var psScripts = scriptBuilder();

                        if (psScripts != null)
                        {
                            helper.RunPowerShellTest(psScripts);
                        }
                    }
                }
                finally
                {
                    if (cleanup != null)
                    {
                        cleanup();
                    }
                }
            }
        }
Пример #4
0
        public void RunPowerShellTest(params string[] scripts)
        {
            var callingClassType = TestUtilities.GetCallingClass(2);
            var mockName         = TestUtilities.GetCurrentMethodName(2);

            HttpMockServer.RecordsDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SessionRecords");
            using (MockContext context = MockContext.Start(callingClassType, mockName))
            {
                this.csmTestFactory = new LegacyTest.CSMTestEnvironmentFactory();
                SetupManagementClients(context);

                var callingClassName = callingClassType
                                       .Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries)
                                       .Last();
                helper.SetupEnvironment(AzureModule.AzureResourceManager);
                helper.SetupModules(AzureModule.AzureResourceManager,
                                    "ScenarioTests\\" + callingClassName + ".ps1",
                                    helper.RMProfileModule,
                                    helper.RMResourceModule,
                                    helper.GetRMModulePath(@"AzureRM.RedisCache.psd1"));

                if (scripts != null)
                {
                    helper.RunPowerShellTest(scripts);
                }
            }
        }
        public void RunPowerShellTest(params string[] scripts)
        {
            var callingClassType = TestUtilities.GetCallingClass(2);
            var mockName = TestUtilities.GetCurrentMethodName(2);

            HttpMockServer.RecordsDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SessionRecords");
            using (MockContext context = MockContext.Start(callingClassType, mockName))
            {
                this.csmTestFactory = new LegacyTest.CSMTestEnvironmentFactory();
                SetupManagementClients(context);

                var callingClassName = callingClassType
                                        .Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries)
                                        .Last();
                helper.SetupEnvironment(AzureModule.AzureResourceManager);
                helper.SetupModules(AzureModule.AzureResourceManager,
                    "ScenarioTests\\" + callingClassName + ".ps1",
                    helper.RMProfileModule,
                    helper.RMResourceModule,
                    helper.GetRMModulePath(@"AzureRM.RedisCache.psd1"));

                if (scripts != null)
                {
                    helper.RunPowerShellTest(scripts);
                }
            }
        }
        public void RunPsTestWorkflow(
            Func <string[]> scriptBuilder,
            Action <LegacyTest.CSMTestEnvironmentFactory> initialize,
            Action cleanup,
            string callingClassType,
            string mockName)
        {
            Dictionary <string, string> d = new Dictionary <string, string>();

            d.Add("Microsoft.Authorization", AuthorizationApiVersion);
            HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(false, d);

            using (MockContext context = MockContext.Start(callingClassType, mockName))
            {
                this.csmTestFactory = new LegacyTest.CSMTestEnvironmentFactory();
                if (initialize != null)
                {
                    initialize(this.csmTestFactory);
                }

                SetupManagementClients(context);

                // register the namespace.
                this.TryRegisterSubscriptionForResource();
                helper.SetupEnvironment(AzureModule.AzureResourceManager);

                var callingClassName = callingClassType
                                       .Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries)
                                       .Last();
                helper.SetupModules(AzureModule.AzureResourceManager, "ScenarioTests\\Common.ps1", "ScenarioTests\\" + callingClassName + ".ps1",
                                    helper.RMProfileModule, helper.RMResourceModule, helper.GetRMModulePath(@"AzureRM.DataLakeStore.psd1"));

                try
                {
                    if (scriptBuilder != null)
                    {
                        var psScripts = scriptBuilder();

                        if (psScripts != null)
                        {
                            helper.RunPowerShellTest(psScripts);
                        }
                    }
                }
                finally
                {
                    if (cleanup != null)
                    {
                        cleanup();
                    }
                }
            }
        }
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (mode == HttpRecorderMode.Record && initialized)
                {
                    var testFactory     = new LegacyTest.CSMTestEnvironmentFactory();
                    var testEnv         = testFactory.GetTestEnvironment();
                    var resourcesClient = LegacyTest.TestBase.GetServiceClient <ResourceManagementClient>(testFactory);

                    resourcesClient.ResourceGroups.Delete(resourceGroupName);
                }
            }
        }
Пример #8
0
        protected void RunPowerShellTest(params string[] scripts)
        {
            var callingClassType = TestUtilities.GetCallingClass(2);
            var mockName         = TestUtilities.GetCurrentMethodName(2);

            Dictionary <string, string> d = new Dictionary <string, string>();

            d.Add("Microsoft.Resources", null);
            d.Add("Microsoft.Features", null);
            d.Add("Microsoft.Authorization", null);
            var providersToIgnore = new Dictionary <string, string>();

            providersToIgnore.Add("Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01");
            HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(true, d, providersToIgnore);

            HttpMockServer.RecordsDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SessionRecords");
            using (MockContext context = MockContext.Start(callingClassType, mockName))
            {
                this.csmTestFactory = new LegacyTest.CSMTestEnvironmentFactory();

                SetupManagementClients(context);

                var callingClassName = callingClassType
                                       .Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries)
                                       .Last();

                List <string> modules = new List <string>();
                //modules.Add("Microsoft.Azure.Commands.NotificationHubs.dll");
                modules.Add("ScenarioTests\\" + this.GetType().Name + ".ps1");
                modules.Add(helper.RMProfileModule);
                modules.Add(helper.RMResourceModule);
                modules.Add(helper.GetRMModulePath(@"AzureRM.NotificationHubs.psd1"));
                modules.Add("AzureRM.Resources.ps1");

                helper.SetupEnvironment(AzureModule.AzureResourceManager);
                helper.SetupModules(AzureModule.AzureResourceManager, modules.ToArray());

                if (scripts != null)
                {
                    helper.RunPowerShellTest(scripts);
                }
            }
        }
Пример #9
0
        protected void RunPowerShellTest(params string[] scripts)
        {
            var callingClassType = TestUtilities.GetCallingClass(2);
            var mockName = TestUtilities.GetCurrentMethodName(2);

            Dictionary<string, string> d = new Dictionary<string, string>();
            d.Add("Microsoft.Resources", null);
            d.Add("Microsoft.Features", null);
            d.Add("Microsoft.Authorization", null);
            var providersToIgnore = new Dictionary<string, string>();
            providersToIgnore.Add("Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01");
            HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(true, d, providersToIgnore);

            HttpMockServer.RecordsDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SessionRecords");
            using (MockContext context = MockContext.Start(callingClassType, mockName))
            {
                this.csmTestFactory = new LegacyTest.CSMTestEnvironmentFactory();

                SetupManagementClients(context);

                var callingClassName = callingClassType
                        .Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries)
                        .Last();

                List<string> modules = new List<string>();
                //modules.Add("Microsoft.Azure.Commands.NotificationHubs.dll");
                modules.Add("ScenarioTests\\" + this.GetType().Name + ".ps1");
                modules.Add(helper.RMProfileModule);
                modules.Add(helper.RMResourceModule);
                modules.Add(helper.GetRMModulePath(@"AzureRM.NotificationHubs.psd1"));
                modules.Add("AzureRM.Resources.ps1");

                helper.SetupEnvironment(AzureModule.AzureResourceManager);
                helper.SetupModules(AzureModule.AzureResourceManager, modules.ToArray());

                if (scripts != null)
                {
                    helper.RunPowerShellTest(scripts);
                }
            }
        }
        public void RunPowerShellTest(XunitTracingInterceptor logger, params string[] scripts)
        {
            var callingClassType = TestUtilities.GetCallingClass(2);
            var mockName         = TestUtilities.GetCurrentMethodName(2);

            helper.TracingInterceptor = logger;

            Dictionary <string, string> d = new Dictionary <string, string>();

            d.Add("Microsoft.Resources", null);
            d.Add("Microsoft.Features", null);
            d.Add("Microsoft.Authorization", null);
            var providersToIgnore = new Dictionary <string, string>();

            HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(true, d, providersToIgnore);

            HttpMockServer.RecordsDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SessionRecords");
            using (MockContext context = MockContext.Start(callingClassType, mockName))
            {
                this.csmTestFactory = new LegacyTest.CSMTestEnvironmentFactory();
                SetupManagementClients(context);

                var callingClassName = callingClassType
                                       .Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries)
                                       .Last();
                helper.SetupEnvironment(AzureModule.AzureResourceManager);
                helper.SetupModules(AzureModule.AzureResourceManager,
                                    "ScenarioTests\\" + callingClassName + ".ps1",
                                    helper.RMProfileModule,
                                    "AzureRM.Storage.ps1",
                                    helper.GetRMModulePath(@"AzureRM.RedisCache.psd1"),
                                    "AzureRM.Resources.ps1");

                if (scripts != null)
                {
                    helper.RunPowerShellTest(scripts);
                }
            }
        }
        public void ResetPreCreatedVault()
        {
            if (mode == HttpRecorderMode.Record)
            {
                using (MockContext context = MockContext.Start(TestUtilities.GetCallingClass(), TestUtilities.GetCurrentMethodName(1)))
                {
                    var testFactory     = new LegacyTest.CSMTestEnvironmentFactory();
                    var testEnv         = testFactory.GetTestEnvironment();
                    var resourcesClient = LegacyTest.TestBase.GetServiceClient <ResourceManagementClient>(testFactory);
                    var mgmtClient      = context.GetServiceClient <KeyVaultManagementClient>(TestEnvironmentFactory.GetTestEnvironment());
                    var tenantId        = Guid.Parse(testEnv.AuthorizationContext.TenantId);

                    var policies = new AccessPolicyEntry[] { };

                    mgmtClient.Vaults.CreateOrUpdate(
                        resourceGroupName: resourceGroupName,
                        vaultName: preCreatedVault,
                        parameters: new VaultCreateOrUpdateParameters
                    {
                        Location = location,
                        Tags     = new Dictionary <string, string> {
                            { tagName, tagValue }
                        },
                        Properties = new VaultProperties
                        {
                            EnabledForDeployment = false,
                            Sku = new Sku {
                                Name = SkuName.Premium
                            },
                            TenantId       = tenantId,
                            VaultUri       = "",
                            AccessPolicies = policies
                        }
                    }
                        );
                }
            }
        }
Пример #12
0
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (mode == HttpRecorderMode.Record && initialized)
                {
                    var testFactory = new LegacyTest.CSMTestEnvironmentFactory();
                    var testEnv = testFactory.GetTestEnvironment();
                    var resourcesClient = LegacyTest.TestBase.GetServiceClient<ResourceManagementClient>(testFactory);

                    resourcesClient.ResourceGroups.Delete(resourceGroupName);
                }
            }
        }
        public void RunPsTestWorkflow(
            Func<string[]> scriptBuilder,
            Action<LegacyTest.CSMTestEnvironmentFactory> initialize,
            Action cleanup,
            string callingClassType,
            string mockName)
        {
            Dictionary<string, string> d = new Dictionary<string, string>();
            d.Add("Microsoft.Resources", null);
            d.Add("Microsoft.Features", null);
            d.Add("Microsoft.Authorization", null);
            var providersToIgnore = new Dictionary<string, string>();
            providersToIgnore.Add("Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01");
            HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(true, d, providersToIgnore);

            HttpMockServer.RecordsDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SessionRecords");
            using (MockContext context = MockContext.Start(callingClassType, mockName))
            {
                this.csmTestFactory = new LegacyTest.CSMTestEnvironmentFactory();
                if (initialize != null)
                {
                    initialize(this.csmTestFactory);
                }
                SetupManagementClients(context);
                helper.SetupEnvironment(AzureModule.AzureResourceManager);

                var callingClassName = callingClassType
                                        .Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries)
                                        .Last();
                helper.SetupModules(AzureModule.AzureResourceManager,
                    "ScenarioTests\\Common.ps1",
                    "ScenarioTests\\" + callingClassName + ".ps1",
                    helper.RMProfileModule,
                    helper.RMStorageDataPlaneModule,
                    helper.RMResourceModule,
                    helper.GetRMModulePath(@"AzureRM.DevTestLabs.psd1"));

                try
                {
                    if (scriptBuilder != null)
                    {
                        var psScripts = scriptBuilder();

                        if (psScripts != null)
                        {
                            helper.RunPowerShellTest(psScripts);
                        }
                    }
                }
                finally
                {
                    if (cleanup != null)
                    {
                        cleanup();
                    }
                }
            }
        }
Пример #14
0
        public void ResetPreCreatedVault()
        {
            if (mode == HttpRecorderMode.Record)
            {
                using (MockContext context = MockContext.Start(TestUtilities.GetCallingClass(), TestUtilities.GetCurrentMethodName(1)))
                {
                    var testFactory = new LegacyTest.CSMTestEnvironmentFactory();
                    var testEnv = testFactory.GetTestEnvironment();
                    var resourcesClient = LegacyTest.TestBase.GetServiceClient<ResourceManagementClient>(testFactory);
                    var mgmtClient = context.GetServiceClient<KeyVaultManagementClient>(TestEnvironmentFactory.GetTestEnvironment());
                    var tenantId = Guid.Parse(testEnv.AuthorizationContext.TenantId);

                    var policies = new AccessPolicyEntry[] { };

                    mgmtClient.Vaults.CreateOrUpdate(
                    resourceGroupName: resourceGroupName,
                    vaultName: preCreatedVault,
                    parameters: new VaultCreateOrUpdateParameters
                    {
                        Location = location,
                        Tags = new Dictionary<string, string> { { tagName, tagValue } },
                        Properties = new VaultProperties
                        {
                            EnabledForDeployment = false,
                            Sku = new Sku { Name = SkuName.Premium },
                            TenantId = tenantId,
                            VaultUri = "",
                            AccessPolicies = policies
                        }
                    }
                    );
                }
            }
        }
        public void RunPsTestWorkflow(
            Func<string[]> scriptBuilder,
            Action<LegacyTest.CSMTestEnvironmentFactory> initialize,
            Action cleanup,
            string callingClassType,
            string mockName)
        {
            Dictionary<string, string> d = new Dictionary<string, string>();
            d.Add("Microsoft.Authorization", AuthorizationApiVersion);
            HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(false, d);

            using (MockContext context = MockContext.Start(callingClassType, mockName))
            {
                this.csmTestFactory = new LegacyTest.CSMTestEnvironmentFactory();
                if (initialize != null)
                {
                    initialize(this.csmTestFactory);
                }
                SetupManagementClients(context);
                helper.SetupEnvironment(AzureModule.AzureResourceManager);

                var callingClassName = callingClassType
                                        .Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries)
                                        .Last();
                helper.SetupModules(AzureModule.AzureResourceManager, 
                    "ScenarioTests\\Common.ps1", 
                    "ScenarioTests\\" + callingClassName + ".ps1", 
                    helper.RMProfileModule, 
                    helper.RMResourceModule, 
                    helper.GetRMModulePath(@"AzureRM.WebSites.psd1"));

                try
                {
                    if (scriptBuilder != null)
                    {
                        var psScripts = scriptBuilder();

                        if (psScripts != null)
                        {
                            helper.RunPowerShellTest(psScripts);
                        }
                    }
                }
                finally
                {
                    if (cleanup != null)
                    {
                        cleanup();
                    }
                }
            }
        }
Пример #16
0
        public void RunPsTestWorkflow(bool createWasbAccount,
            Func<string[]> scriptBuilder,
            Action<LegacyTest.CSMTestEnvironmentFactory> initialize,
            Action cleanup,
            string callingClassType,
            string mockName)
        {
            Dictionary<string, string> d = new Dictionary<string, string>();
            d.Add("Microsoft.Resources", null);
            d.Add("Microsoft.Features", null);
            d.Add("Microsoft.Authorization", null);
            var providersToIgnore = new Dictionary<string, string>();
            providersToIgnore.Add("Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01");
            HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(true, d, providersToIgnore);
            HttpMockServer.RecordsDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SessionRecords");
            using (MockContext context = MockContext.Start(callingClassType, mockName))
            {
                this.csmTestFactory = new LegacyTest.CSMTestEnvironmentFactory();
                if (initialize != null)
                {
                    initialize(this.csmTestFactory);
                }

                SetupManagementClients(context);

                // register the namespace.
                this.TryRegisterSubscriptionForResource();
                helper.SetupEnvironment(AzureModule.AzureResourceManager);
                var callingClassName = callingClassType
                                        .Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries)
                                        .Last();
                helper.SetupModules(AzureModule.AzureResourceManager, "ScenarioTests\\Common.ps1", "ScenarioTests\\" + callingClassName + ".ps1",
                helper.RMProfileModule, helper.RMResourceModule, helper.GetRMModulePath(@"AzureRM.DataLakeAnalytics.psd1"), helper.GetRMModulePath(@"AzureRM.DataLakeStore.psd1"), "AzureRM.Resources.ps1");

                if (createWasbAccount)
                {
                    string storageSuffix;
                    this.resourceGroupName = TestUtilities.GenerateName("abarg1");
                    TryCreateResourceGroup(this.resourceGroupName, resourceGroupLocation);
                    this.azureBlobStoreName = TestUtilities.GenerateName("azureblob01");
                    this.azureBlobStoreAccessKey = this.TryCreateStorageAccount(this.resourceGroupName,
                        this.azureBlobStoreName,
                        "DataLakeAnalyticsTestStorage", "DataLakeAnalyticsTestStorageDescription", resourceGroupLocation,
                        out storageSuffix);
                }

                try
                {
                    if (scriptBuilder != null)
                    {
                        var psScripts = scriptBuilder();

                        if (psScripts != null)
                        {
                            // inject the access key into the script if necessary.
                            for (int i = 0; i < psScripts.Length; i++)
                            {
                                if (psScripts[i].Contains("-blobAccountKey") && createWasbAccount)
                                {
                                    psScripts[i] = psScripts[i].Replace("-blobAccountKey",
                                        string.Format("-blobAccountName {0} -blobAccountKey '{1}'", this.azureBlobStoreName, this.azureBlobStoreAccessKey));
                                }
                            }

                            helper.RunPowerShellTest(psScripts);
                        }
                    }
                }
                finally
                {
                    if (createWasbAccount)
                    {
                        try
                        {
                            ResourceManagementClient.ResourceGroups.Delete(this.resourceGroupName);
                        }
                        catch
                        {
                            // best effort cleanup.
                        }
                    }

                    if (cleanup != null)
                    {
                        cleanup();
                    }
                }
            }
        }
Пример #17
0
        public Collection <PSObject> RunPsTestWorkflow(
            PsBackupProviderTypes providerType,
            Func <string[]> scriptBuilder,
            Action <LegacyTest.CSMTestEnvironmentFactory> initialize,
            Action cleanup,
            string callingClassType,
            string mockName)
        {
            Dictionary <string, string> providers = new Dictionary <string, string>();

            providers.Add("Microsoft.Resources", null);
            providers.Add("Microsoft.Features", null);
            providers.Add("Microsoft.Authorization", null);
            providers.Add("Microsoft.Compute", null);
            providers.Add("Microsoft.Network", null);
            providers.Add("Microsoft.Storage", null);
            var providersToIgnore = new Dictionary <string, string>();

            providersToIgnore.Add("Microsoft.Azure.Management.Resources.ResourceManagementClient", "2016-02-01");
            providersToIgnore.Add("Microsoft.Azure.Management.Internal.Resources.ResourceManagementClient", "2016-02-01");
            HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(true, providers, providersToIgnore);

            HttpMockServer.RecordsDirectory =
                Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "SessionRecords");

            using (MockContext context = MockContext.Start(callingClassType, mockName))
            {
                csmTestFactory = new LegacyTest.CSMTestEnvironmentFactory();

                if (initialize != null)
                {
                    initialize.Invoke(csmTestFactory);
                }

                SetupManagementClients(context);

                helper.SetupEnvironment(AzureModule.AzureResourceManager);

                var testFolderName   = providerType.ToString();
                var callingClassName =
                    callingClassType
                    .Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries).Last();
                string psFile =
                    "ScenarioTests\\" + testFolderName + "\\" + callingClassName + ".ps1";
                string commonPsFile               = "ScenarioTests\\" + testFolderName + "\\Common.ps1";
                string rmProfileModule            = helper.RMProfileModule;
                string rmModulePath               = helper.GetRMModulePath("AzureRM.RecoveryServices.Backup.psd1");
                string recoveryServicesModulePath =
                    helper.GetRMModulePath("AzureRM.RecoveryServices.psd1");

                List <string> modules = new List <string>();

                if (File.Exists(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, commonPsFile)))
                {
                    modules.Add(commonPsFile);
                }

                modules.Add(psFile);
                modules.Add(rmProfileModule);
                modules.Add(rmModulePath);
                modules.Add(recoveryServicesModulePath);
                modules.Add(helper.RMResourceModule);
                modules.Add(helper.RMStorageDataPlaneModule);
                modules.Add(helper.RMStorageModule);
                modules.Add(helper.GetRMModulePath("AzureRM.Compute.psd1"));
                modules.Add(helper.GetRMModulePath("AzureRM.Network.psd1"));
                modules.Add("AzureRM.Storage.ps1");
                modules.Add("AzureRM.Resources.ps1");

                helper.SetupModules(AzureModule.AzureResourceManager, modules.ToArray());

                try
                {
                    if (scriptBuilder != null)
                    {
                        var psScripts = scriptBuilder();

                        if (psScripts != null)
                        {
                            return(helper.RunPowerShellTest(psScripts));
                        }
                    }
                }
                finally
                {
                    if (cleanup != null)
                    {
                        cleanup.Invoke();
                    }
                }
            }

            return(null);
        }
Пример #18
0
        public void RunPsTestWorkflow(bool createWasbAccount,
                                      Func <string[]> scriptBuilder,
                                      Action <LegacyTest.CSMTestEnvironmentFactory> initialize,
                                      Action cleanup,
                                      string callingClassType,
                                      string mockName)
        {
            Dictionary <string, string> d = new Dictionary <string, string>();

            d.Add("Microsoft.Authorization", AuthorizationApiVersion);
            HttpMockServer.Matcher = new PermissiveRecordMatcherWithApiExclusion(false, d);

            using (MockContext context = MockContext.Start(callingClassType, mockName))
            {
                this.csmTestFactory = new LegacyTest.CSMTestEnvironmentFactory();
                if (initialize != null)
                {
                    initialize(this.csmTestFactory);
                }

                SetupManagementClients(context);

                // register the namespace.
                this.TryRegisterSubscriptionForResource();
                helper.SetupEnvironment(AzureModule.AzureResourceManager);
                var callingClassName = callingClassType
                                       .Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries)
                                       .Last();
                helper.SetupModules(AzureModule.AzureResourceManager, "ScenarioTests\\Common.ps1", "ScenarioTests\\" + callingClassName + ".ps1",
                                    helper.RMProfileModule, helper.RMResourceModule, helper.GetRMModulePath(@"AzureRM.DataLakeAnalytics.psd1"), helper.GetRMModulePath(@"AzureRM.DataLakeStore.psd1"));

                if (createWasbAccount)
                {
                    string storageSuffix;
                    this.resourceGroupName = TestUtilities.GenerateName("abarg1");
                    TryCreateResourceGroup(this.resourceGroupName, resourceGroupLocation);
                    this.azureBlobStoreName      = TestUtilities.GenerateName("azureblob01");
                    this.azureBlobStoreAccessKey = this.TryCreateStorageAccount(this.resourceGroupName,
                                                                                this.azureBlobStoreName,
                                                                                "DataLakeAnalyticsTestStorage", "DataLakeAnalyticsTestStorageDescription", resourceGroupLocation,
                                                                                out storageSuffix);
                }

                try
                {
                    if (scriptBuilder != null)
                    {
                        var psScripts = scriptBuilder();

                        if (psScripts != null)
                        {
                            // inject the access key into the script if necessary.
                            for (int i = 0; i < psScripts.Length; i++)
                            {
                                if (psScripts[i].Contains("-blobAccountKey") && createWasbAccount)
                                {
                                    psScripts[i] = psScripts[i].Replace("-blobAccountKey",
                                                                        string.Format("-blobAccountName {0} -blobAccountKey '{1}'", this.azureBlobStoreName, this.azureBlobStoreAccessKey));
                                }
                            }

                            helper.RunPowerShellTest(psScripts);
                        }
                    }
                }
                finally
                {
                    if (createWasbAccount)
                    {
                        try
                        {
                            ResourceManagementClient.ResourceGroups.Delete(this.resourceGroupName);
                        }
                        catch
                        {
                            // best effort cleanup.
                        }
                    }

                    if (cleanup != null)
                    {
                        cleanup();
                    }
                }
            }
        }