public PowerShellTest(AzureModule commandMode, params string[] modules)
 {
     this.modules = new List <string>();
     if (commandMode == AzureModule.AzureServiceManagement)
     {
         this.modules.Add(FileUtilities.GetContentFilePath(@"ServiceManagement\Azure\Azure.psd1"));
     }
     else if (commandMode == AzureModule.AzureResourceManager)
     {
         this.modules.Add(FileUtilities.GetContentFilePath(@"ResourceManager\AzureResourceManager\AzureResourceManager.psd1"));
     }
     else
     {
         throw new ArgumentException("Unknown command type for testing");
     }
     this.modules.Add("Assert.ps1");
     this.modules.Add("Common.ps1");
     this.modules.AddRange(modules);
     TestingTracingInterceptor.AddToContext();
 }
Beispiel #2
0
 public TestBase()
 {
     TestingTracingInterceptor.AddToContext();
 }
        /// <summary>
        /// Setup certificates required for the unit tests
        /// </summary>
        public static void SetupCertificates()
        {
            TestingTracingInterceptor.AddToContext();
            var newGuid = Guid.NewGuid();
            var profile = new AzureProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));

            AzurePSCmdlet.CurrentProfile       = profile;
            AzureSession.DataStore             = new MockDataStore();
            AzureSession.AuthenticationFactory = new MockTokenAuthenticationFactory();
            ProfileClient client = new ProfileClient(profile);

            client.Profile.Subscriptions[newGuid] = new AzureSubscription
            {
                Id          = newGuid,
                Name        = "test",
                Environment = EnvironmentName.AzureCloud,
                Account     = "test"
            };
            client.Profile.Accounts["test"] = new AzureAccount
            {
                Id         = "test",
                Type       = AzureAccount.AccountType.User,
                Properties = new Dictionary <AzureAccount.Property, string>
                {
                    { AzureAccount.Property.Subscriptions, newGuid.ToString() }
                }
            };
            client.Profile.Accounts[UnitTestHelper.GetUnitTestClientCertificate().Thumbprint] = new AzureAccount
            {
                Id         = UnitTestHelper.GetUnitTestClientCertificate().Thumbprint,
                Type       = AzureAccount.AccountType.Certificate,
                Properties = new Dictionary <AzureAccount.Property, string>
                {
                    { AzureAccount.Property.Subscriptions, newGuid.ToString() }
                }
            };
            client.Profile.Accounts[UnitTestHelper.GetUnitTestSSLCertificate().Thumbprint] = new AzureAccount
            {
                Id         = UnitTestHelper.GetUnitTestSSLCertificate().Thumbprint,
                Type       = AzureAccount.AccountType.Certificate,
                Properties = new Dictionary <AzureAccount.Property, string>
                {
                    { AzureAccount.Property.Subscriptions, newGuid.ToString() }
                }
            };
            client.SetSubscriptionAsDefault(newGuid, "test");
            client.Profile.Save();


            // Check if the cert has been installed
            Process proc = ExecuteProcess(
                "netsh",
                string.Format(
                    CultureInfo.InvariantCulture,
                    "http show sslcert ipport=0.0.0.0:{0}",
                    DefaultHttpsServerPrefixUri.Port));

            if (proc.ExitCode != 0)
            {
                // Install the SSL and client certificates to the LocalMachine store
                X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
                store.Open(OpenFlags.ReadWrite);
                store.Add(UnitTestHelper.GetUnitTestSSLCertificate());
                store.Add(UnitTestHelper.GetUnitTestClientCertificate());
                store.Close();
                store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
                store.Open(OpenFlags.ReadWrite);
                store.Add(UnitTestHelper.GetUnitTestSSLCertificate());
                store.Add(UnitTestHelper.GetUnitTestClientCertificate());
                store.Close();

                // Remove any existing certs on the default port
                proc = ExecuteProcess(
                    "netsh",
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "http delete sslcert ipport=0.0.0.0:{0}",
                        DefaultHttpsServerPrefixUri.Port));

                // Install the ssl cert on the default port
                proc = ExecuteProcess(
                    "netsh",
                    string.Format(
                        CultureInfo.InvariantCulture,
                        "http add sslcert ipport=0.0.0.0:{0} certhash={1} appid={2:B}",
                        DefaultHttpsServerPrefixUri.Port,
                        UnitTestHelper.GetUnitTestSSLCertificate().Thumbprint,
                        MockHttpServer.HttpsAppId));

                if (proc.ExitCode != 0)
                {
                    throw new InvalidOperationException(string.Format(
                                                            CultureInfo.InvariantCulture,
                                                            "Unable to add ssl certificate: {0}",
                                                            proc.StandardOutput.ReadToEnd()));
                }
            }
        }