Пример #1
0
 public PowerShellTest(AzureModule commandMode, params string[] modules)
 {
     this.modules = new List <string>();
     this.modules.Add(FileUtilities.GetContentFilePath(@"ServiceManagement\Azure\Azure.psd1"));
     this.modules.Add("Assert.ps1");
     this.modules.Add("Common.ps1");
     this.modules.AddRange(modules);
     PSTestTracingInterceptor.AddToContext();
 }
 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);
     PSTestTracingInterceptor.AddToContext();
 }
Пример #3
0
        /// <summary>
        /// Setup certificates required for the unit tests
        /// </summary>
        public static void SetupCertificates()
        {
            PSTestTracingInterceptor.AddToContext();
            var newGuid = Guid.NewGuid();
            var profile = new AzureSMProfile(Path.Combine(AzureSession.Instance.ProfileDirectory, AzureSession.Instance.ProfileFile));

            AzureSMCmdlet.CurrentProfile                = profile;
            AzureSession.Instance.DataStore             = new MemoryDataStore();
            AzureSession.Instance.AuthenticationFactory = new MockTokenAuthenticationFactory();
            ProfileClient client = new ProfileClient(profile);
            var           acct2  = new AzureAccount
            {
                Id   = "test",
                Type = AzureAccount.AccountType.User
            };

            acct2.SetSubscriptions(newGuid.ToString());
            client.AddOrSetAccount(acct2);
            var acct3 = new AzureAccount
            {
                Id   = UnitTestHelper.GetUnitTestClientCertificate().Thumbprint,
                Type = AzureAccount.AccountType.Certificate
            };

            acct3.SetSubscriptions(newGuid.ToString());
            client.AddOrSetAccount(acct3);
            var acct = new AzureAccount
            {
                Id   = UnitTestHelper.GetUnitTestSSLCertificate().Thumbprint,
                Type = AzureAccount.AccountType.Certificate,
            };

            acct.SetSubscriptions(newGuid.ToString());
            client.AddOrSetAccount(acct);
            var sub1 = new AzureSubscription
            {
                Id   = newGuid.ToString(),
                Name = "test",
            };

            sub1.SetEnvironment(EnvironmentName.AzureCloud);
            sub1.SetAccount("test");
            client.AddOrSetSubscription(sub1);

            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()));
                }
            }
        }