/src/ServiceManagement/Services/Commands.Test/Profile/ProfileCmdltsTests.cs
C# | 1007 lines | 790 code | 125 blank | 92 comment | 3 complexity | d1f5423e22631f7c28a8e4bb3027f28a MD5 | raw file
- // ----------------------------------------------------------------------------------
- //
- // Copyright Microsoft Corporation
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- // http://www.apache.org/licenses/LICENSE-2.0
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- // ----------------------------------------------------------------------------------
-
- using System.Collections;
- using System.Security;
- using System.Text;
- using Hyak.Common;
- using Microsoft.Azure.Commands.Common.Authentication;
- using Microsoft.Azure.Commands.Common.Authentication.Models;
- using Microsoft.IdentityModel.Clients.ActiveDirectory;
- using Microsoft.ServiceBus.Management;
- using Microsoft.WindowsAzure.Commands.Common;
- using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
- using Microsoft.WindowsAzure.Commands.Profile;
- using Microsoft.WindowsAzure.Commands.Profile.Models;
- using Microsoft.WindowsAzure.Commands.Utilities.Common;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Management.Automation;
- using System.Reflection;
- using System.Security.Cryptography.X509Certificates;
- using Microsoft.Azure.Commands.Common.Authentication.Factories;
- using Microsoft.Azure.ServiceManagemenet.Common;
- using Microsoft.WindowsAzure.Commands.ScenarioTest;
- using Xunit;
-
- namespace Microsoft.WindowsAzure.Commands.Test.Profile
- {
- public class ProfileCmdltsTests
- {
- private AzureSubscription azureSubscription1;
- private AzureSubscription azureSubscription2;
- private AzureEnvironment azureEnvironment;
- private AzureAccount azureAccount;
- private MockCommandRuntime commandRuntimeMock;
- private X509Certificate2 SampleCertificate = new X509Certificate2(Convert.FromBase64String(@"MIIKJAIBAzCCCeQGCSqGSIb3DQEHAaCCC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PzVIR0hnf4AJ8/9BR0vTGmxiWwy8VMxrS3PyouLPZMXAgdT6ddRVwmewNjTe5g/tciGazIW+nROgg6fsgyObMp7keONMvtFMrJQLa2oKarGkwNzAfMAcGBSsOAwIaBBQXFDnqplMX7OuyknHK7B+HA/N8tAQUsL21+IY37DPL968vhVzqz09W/so="), string.Empty);
- private MemoryDataStore dataStore;
-
- public ProfileCmdltsTests()
- : base()
- {
- dataStore = new MemoryDataStore();
- AzureSession.DataStore = dataStore;
- commandRuntimeMock = new MockCommandRuntime();
- SetMockData();
- AzureSession.AuthenticationFactory = new MockTokenAuthenticationFactory();
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void ClearAzureProfileClearsDefaultProfile()
- {
- ClearAzureProfileCommand cmdlt = new ClearAzureProfileCommand();
- // Setup
- var profile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- AzureSMCmdlet.CurrentProfile = profile;
- ProfileClient client = new ProfileClient(profile);
- client.AddOrSetAccount(azureAccount);
- client.AddOrSetEnvironment(azureEnvironment);
- client.AddOrSetSubscription(azureSubscription1);
- client.Profile.Save();
-
- cmdlt.CommandRuntime = commandRuntimeMock;
- cmdlt.Force = new SwitchParameter(true);
-
- // Act
- cmdlt.InvokeBeginProcessing();
- cmdlt.ExecuteCmdlet();
- cmdlt.InvokeEndProcessing();
-
- // Verify
- client = new ProfileClient(new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile)));
- Assert.Equal(0, client.Profile.Subscriptions.Count);
- Assert.Equal(0, client.Profile.Accounts.Count);
- Assert.Equal(4, client.Profile.Environments.Count); //only default environments
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void ClearAzureProfileClearsCustomProfile()
- {
- string subscriptionDataFile = Path.GetTempFileName();
-
- ClearAzureProfileCommand cmdlt = new ClearAzureProfileCommand();
- // Setup
- ProfileClient client = new ProfileClient(new AzureSMProfile(subscriptionDataFile));
- client.AddOrSetAccount(azureAccount);
- client.AddOrSetEnvironment(azureEnvironment);
- client.AddOrSetSubscription(azureSubscription1);
- client.Profile.Save();
-
- cmdlt.CommandRuntime = commandRuntimeMock;
- cmdlt.Force = new SwitchParameter(true);
- cmdlt.Profile = new AzureSMProfile(subscriptionDataFile);
-
- // Act
- cmdlt.InvokeBeginProcessing();
- cmdlt.ExecuteCmdlet();
- cmdlt.InvokeEndProcessing();
-
- // Verify
- client = new ProfileClient(new AzureSMProfile(subscriptionDataFile));
- Assert.Equal(0, client.Profile.Subscriptions.Count);
- Assert.Equal(0, client.Profile.Accounts.Count);
- Assert.Equal(4, client.Profile.Environments.Count); //only default environments
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void ClearAzureProfileClearsTokenCache()
- {
- var profile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- AzureSMCmdlet.CurrentProfile = profile;
- ClearAzureProfileCommand cmdlt = new ClearAzureProfileCommand();
- AzureSession.DataStore = new MemoryDataStore();
- AzureSession.TokenCache = new ProtectedFileTokenCache(Path.Combine(AzureSession.ProfileDirectory, AzureSession.TokenCacheFile));
-
- cmdlt.CommandRuntime = commandRuntimeMock;
- cmdlt.Force = new SwitchParameter(true);
-
- // Act
- cmdlt.InvokeBeginProcessing();
- var tokenCache = AzureSession.TokenCache as ProtectedFileTokenCache;
- tokenCache.HasStateChanged = true;
-
- // HACK: Do not look at this code
- TokenCacheNotificationArgs args = new TokenCacheNotificationArgs();
- typeof(TokenCacheNotificationArgs).GetProperty("ClientId").SetValue(args, "123");
- typeof(TokenCacheNotificationArgs).GetProperty("Resource").SetValue(args, "123");
- typeof(TokenCacheNotificationArgs).GetProperty("TokenCache").SetValue(args, tokenCache);
- typeof(TokenCacheNotificationArgs).GetProperty("UniqueId").SetValue(args, "test@live.com");
- typeof(TokenCacheNotificationArgs).GetProperty("DisplayableId").SetValue(args, "test@live.com");
- AuthenticationResult authenticationResult =
- typeof(AuthenticationResult).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance,
- null, new Type[] { typeof(string), typeof(string), typeof(string), typeof(DateTimeOffset) }, null).Invoke(new object[]
- {
- "foo", "123", "123",
- new DateTimeOffset(DateTime.Now.AddDays(1))
- }) as AuthenticationResult;
- var storeToCache = typeof(TokenCache).GetMethod("StoreToCache", BindingFlags.Instance | BindingFlags.NonPublic);
- storeToCache.Invoke(tokenCache,
- new object[] { authenticationResult, "Common", "123", "123", 0, null});
-
- tokenCache.AfterAccess.Invoke(args);
-
- Assert.Equal(1, tokenCache.ReadItems().Count());
- cmdlt.ExecuteCmdlet();
- cmdlt.InvokeEndProcessing();
- // Verify
- Assert.Equal(0, tokenCache.ReadItems().Count());
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void DeleteCorruptedTokenCache()
- {
- //setup
- string testFileName = @"c:\foobar\TokenCache.dat";
- AzureSession.DataStore.WriteFile(testFileName, new byte[] { 0, 1 });
-
- //Act
- ProtectedFileTokenCache tokenCache = new ProtectedFileTokenCache(testFileName);
-
- //Assert
- Assert.False(AzureSession.DataStore.FileExists(testFileName));
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void SetAzureSubscriptionAddsSubscriptionWithCertificate()
- {
- RunMockedCmdletTest(() =>
- {
-
- var profile =
- new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- ProfileClient client = new ProfileClient(profile);
- AzureSMProfileProvider.Instance.Profile = profile;
- SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
- // Setup
- cmdlt.CommandRuntime = commandRuntimeMock;
- cmdlt.SubscriptionId = Guid.NewGuid().ToString();
- cmdlt.SubscriptionName = "NewSubscriptionName";
- cmdlt.CurrentStorageAccountName = "NewCloudStorage";
- cmdlt.Certificate = SampleCertificate;
-
- // Act
- cmdlt.InvokeBeginProcessing();
- cmdlt.ExecuteCmdlet();
- cmdlt.InvokeEndProcessing();
-
- // Verify
- var newSubscription = client.Profile.Subscriptions[new Guid(cmdlt.SubscriptionId)];
- var newAccount = client.Profile.Accounts[SampleCertificate.Thumbprint];
- Assert.Equal(cmdlt.SubscriptionName, newSubscription.Name);
- Assert.Equal(EnvironmentName.AzureCloud, newSubscription.Environment);
- Assert.True(
- newSubscription.GetProperty(AzureSubscription.Property.StorageAccount)
- .Contains(string.Format("AccountName={0}", cmdlt.CurrentStorageAccountName)));
-
- Assert.Equal(newAccount.Id, newSubscription.Account);
- Assert.Equal(AzureAccount.AccountType.Certificate, newAccount.Type);
- Assert.Equal(SampleCertificate.Thumbprint, newAccount.Id);
- Assert.Equal(cmdlt.SubscriptionId,
- newAccount.GetProperty(AzureAccount.Property.Subscriptions));
- });
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void SetAzureSubscriptionDerivesEnvironmentFromEnvironmentParameterOnAdd()
- {
- RunMockedCmdletTest(() =>
- {
-
- // Setup
- var profile =
- new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- AzureSMCmdlet.CurrentProfile = profile;
- ProfileClient client = new ProfileClient(profile);
- client.AddOrSetEnvironment(azureEnvironment);
- client.Profile.Save();
- SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
-
- cmdlt.CommandRuntime = commandRuntimeMock;
- cmdlt.SubscriptionId = Guid.NewGuid().ToString();
- cmdlt.SubscriptionName = "NewSubscriptionName";
- cmdlt.CurrentStorageAccountName = "NewCloudStorage";
- cmdlt.Environment = azureEnvironment.Name;
- cmdlt.Certificate = SampleCertificate;
-
- // Act
- cmdlt.InvokeBeginProcessing();
- cmdlt.ExecuteCmdlet();
- cmdlt.InvokeEndProcessing();
-
- // Verify
- client =
- new ProfileClient(
- new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory,
- AzureSession.ProfileFile)));
- var newSubscription = client.Profile.Subscriptions[new Guid(cmdlt.SubscriptionId)];
- Assert.Equal(cmdlt.SubscriptionName, newSubscription.Name);
- Assert.Equal(cmdlt.Environment, newSubscription.Environment);
- Assert.True(StorageAccountMatchesConnectionString(cmdlt.CurrentStorageAccountName,
- newSubscription.GetProperty(AzureSubscription.Property.StorageAccount)));
- });
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void SetAzureSubscriptionThrowsExceptionWithoutCertificateOnAdd()
- {
- SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
- // Setup
- var profile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- AzureSMCmdlet.CurrentProfile = profile;
- ProfileClient client = new ProfileClient(profile);
- client.AddOrSetEnvironment(azureEnvironment);
- client.Profile.Save();
-
- cmdlt.CommandRuntime = commandRuntimeMock;
- cmdlt.SubscriptionId = Guid.NewGuid().ToString();
- cmdlt.SubscriptionName = "NewSubscriptionName";
- cmdlt.CurrentStorageAccountName = "NewCloudStorage";
- cmdlt.Environment = azureEnvironment.Name;
-
- // Verify
- cmdlt.InvokeBeginProcessing();
- Assert.Throws<ArgumentException>(() => cmdlt.ExecuteCmdlet());
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void SetAzureSubscriptionDerivesEnvironmentFromEnvironmentParameterOnSet()
- {
- RunMockedCmdletTest(() =>
- {
-
- // Setup
- var profile =
- new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- AzureSMCmdlet.CurrentProfile = profile;
- ProfileClient client = new ProfileClient(profile);
- client.AddOrSetAccount(azureAccount);
- client.AddOrSetEnvironment(azureEnvironment);
- client.AddOrSetSubscription(azureSubscription1);
- client.Profile.Save();
- SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
-
- cmdlt.CommandRuntime = commandRuntimeMock;
- cmdlt.SubscriptionId = azureSubscription1.Id.ToString();
- cmdlt.CurrentStorageAccountName = "NewCloudStorage";
- cmdlt.Environment = azureEnvironment.Name;
-
- // Act
- cmdlt.InvokeBeginProcessing();
- cmdlt.ExecuteCmdlet();
- cmdlt.InvokeEndProcessing();
-
- // Verify
- client =
- new ProfileClient(
- new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory,
- AzureSession.ProfileFile)));
- var newSubscription = client.Profile.Subscriptions[new Guid(cmdlt.SubscriptionId)];
- Assert.Equal(cmdlt.Environment, newSubscription.Environment);
- Assert.True(StorageAccountMatchesConnectionString(cmdlt.CurrentStorageAccountName,
- newSubscription.GetProperty(AzureSubscription.Property.StorageAccount)));
- });
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void SetAzureSubscriptionDerivesEnvironmentFromServiceEndpointParameterOnSet()
- {
- RunMockedCmdletTest(() =>
- {
-
- // Setup
- var profile =
- new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- AzureSMCmdlet.CurrentProfile = profile;
- ProfileClient client = new ProfileClient(profile);
- client.AddOrSetAccount(azureAccount);
- client.AddOrSetEnvironment(azureEnvironment);
- client.AddOrSetSubscription(azureSubscription1);
- client.Profile.Save();
- SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
-
- cmdlt.CommandRuntime = commandRuntimeMock;
- cmdlt.SubscriptionId = azureSubscription1.Id.ToString();
- cmdlt.CurrentStorageAccountName = "NewCloudStorage";
- cmdlt.ServiceEndpoint =
- azureEnvironment.GetEndpoint(AzureEnvironment.Endpoint.ServiceManagement);
-
- // Act
- cmdlt.InvokeBeginProcessing();
- cmdlt.ExecuteCmdlet();
- cmdlt.InvokeEndProcessing();
-
- // Verify
- client =
- new ProfileClient(
- new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory,
- AzureSession.ProfileFile)));
- var newSubscription = client.Profile.Subscriptions[new Guid(cmdlt.SubscriptionId)];
- Assert.Equal(cmdlt.Environment, newSubscription.Environment);
- Assert.True(StorageAccountMatchesConnectionString(cmdlt.CurrentStorageAccountName,
- newSubscription.GetProperty(AzureSubscription.Property.StorageAccount)));
- });
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void SetAzureSubscriptionDerivesEnvironmentFromResourcesEndpointParameterOnSet()
- {
- RunMockedCmdletTest(() =>
- {
- // Setup
- var profile =
- new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- ProfileClient client = new ProfileClient(profile);
- AzureSMCmdlet.CurrentProfile = profile;
- client.AddOrSetAccount(azureAccount);
- client.AddOrSetEnvironment(azureEnvironment);
- client.AddOrSetSubscription(azureSubscription1);
- client.Profile.Save();
- SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
-
- cmdlt.CommandRuntime = commandRuntimeMock;
- cmdlt.SubscriptionId = azureSubscription1.Id.ToString();
- cmdlt.CurrentStorageAccountName = "NewCloudStorage";
- cmdlt.ResourceManagerEndpoint =
- azureEnvironment.GetEndpoint(AzureEnvironment.Endpoint.ResourceManager);
-
- // Act
- cmdlt.InvokeBeginProcessing();
- cmdlt.ExecuteCmdlet();
- cmdlt.InvokeEndProcessing();
-
- // Verify
- client = new ProfileClient(profile);
- var newSubscription = client.Profile.Subscriptions[new Guid(cmdlt.SubscriptionId)];
- Assert.Equal(cmdlt.Environment, newSubscription.Environment);
- Assert.True(StorageAccountMatchesConnectionString(cmdlt.CurrentStorageAccountName,
- newSubscription.GetProperty(AzureSubscription.Property.StorageAccount)));
- });
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void SetAzureSubscriptionDerivesEnvironmentFromBothEndpointParameters()
- {
- RunMockedCmdletTest(() =>
- {
- // Setup
- var profile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- AzureSMCmdlet.CurrentProfile = profile;
- ProfileClient client = new ProfileClient(profile);
- client.AddOrSetAccount(azureAccount);
- client.AddOrSetEnvironment(azureEnvironment);
- client.AddOrSetSubscription(azureSubscription1);
- client.Profile.Save();
-
- SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
-
- cmdlt.CommandRuntime = commandRuntimeMock;
- cmdlt.SubscriptionId = azureSubscription1.Id.ToString();
- cmdlt.CurrentStorageAccountName = "NewCloudStorage";
- cmdlt.ServiceEndpoint = azureEnvironment.GetEndpoint(AzureEnvironment.Endpoint.ServiceManagement);
- cmdlt.ResourceManagerEndpoint = azureEnvironment.GetEndpoint(AzureEnvironment.Endpoint.ResourceManager);
-
- // Act
- cmdlt.InvokeBeginProcessing();
- cmdlt.ExecuteCmdlet();
- cmdlt.InvokeEndProcessing();
-
- // Verify
- client =
- new ProfileClient(
- new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile)));
- var newSubscription = client.Profile.Subscriptions[new Guid(cmdlt.SubscriptionId)];
- Assert.Equal(cmdlt.Environment, newSubscription.Environment);
- Assert.True(StorageAccountMatchesConnectionString(cmdlt.CurrentStorageAccountName,
- newSubscription.GetProperty(AzureSubscription.Property.StorageAccount)));
- });
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void SetAzureSubscriptionUpdatesSubscriptionWithCertificate()
- {
- RunMockedCmdletTest(() =>
- {
-
- // Setup
- var profile =
- new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- AzureSMCmdlet.CurrentProfile = profile;
- ProfileClient client = new ProfileClient(profile);
- client.AddOrSetAccount(azureAccount);
- client.AddOrSetEnvironment(azureEnvironment);
- client.AddOrSetSubscription(azureSubscription1);
- client.Profile.Save();
-
- SetAzureSubscriptionCommand cmdlt = new SetAzureSubscriptionCommand();
-
- cmdlt.CommandRuntime = commandRuntimeMock;
- cmdlt.SubscriptionId = azureSubscription1.Id.ToString();
- cmdlt.CurrentStorageAccountName = "NewCloudStorage";
- cmdlt.Certificate = SampleCertificate;
-
- // Act
- cmdlt.InvokeBeginProcessing();
- cmdlt.ExecuteCmdlet();
- cmdlt.InvokeEndProcessing();
-
- // Verify
- client =
- new ProfileClient(
- new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory,
- AzureSession.ProfileFile)));
- var newSubscription = client.Profile.Subscriptions[new Guid(cmdlt.SubscriptionId)];
- var newAccount = client.Profile.Accounts[SampleCertificate.Thumbprint];
- var existingAccount = client.Profile.Accounts[azureAccount.Id];
- Assert.Equal(azureEnvironment.Name, newSubscription.Environment);
- Assert.True(StorageAccountMatchesConnectionString(cmdlt.CurrentStorageAccountName,
- newSubscription.GetProperty(AzureSubscription.Property.StorageAccount)));
-
- Assert.Equal(newAccount.Id, newSubscription.Account);
- Assert.Equal(AzureAccount.AccountType.Certificate, newAccount.Type);
- Assert.Equal(SampleCertificate.Thumbprint, newAccount.Id);
- Assert.Equal(cmdlt.SubscriptionId,
- newAccount.GetProperty(AzureAccount.Property.Subscriptions));
-
- Assert.Equal(azureAccount.Id, existingAccount.Id);
- Assert.Equal(AzureAccount.AccountType.User, existingAccount.Type);
- Assert.True(
- existingAccount.GetPropertyAsArray(AzureAccount.Property.Subscriptions)
- .Contains(cmdlt.SubscriptionId));
- });
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void ImportPublishSettingsFileSelectsCorrectEnvironment()
- {
- ImportAzurePublishSettingsCommand cmdlt = new ImportAzurePublishSettingsCommand();
-
- // Setup
- AzureSession.DataStore.WriteFile("ImportPublishSettingsFileSelectsCorrectEnvironment.publishsettings",
- Commands.Common.Test.Properties.Resources.ValidProfileChina);
- var profile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- AzureSMCmdlet.CurrentProfile = profile;
- ProfileClient client = new ProfileClient(profile);
- var oldDataStore = FileUtilities.DataStore;
- FileUtilities.DataStore = AzureSession.DataStore;
- var expectedEnv = "AzureChinaCloud";
- var expected = client.ImportPublishSettings("ImportPublishSettingsFileSelectsCorrectEnvironment.publishsettings", null);
-
- cmdlt.CommandRuntime = commandRuntimeMock;
- cmdlt.ProfileClient = new ProfileClient(profile);
- cmdlt.PublishSettingsFile = "ImportPublishSettingsFileSelectsCorrectEnvironment.publishsettings";
-
- try
- {
- // Act
- cmdlt.InvokeBeginProcessing();
- AzureSession.DataStore = FileUtilities.DataStore;
- cmdlt.ExecuteCmdlet();
- cmdlt.InvokeEndProcessing();
-
- // Verify
- foreach (var subscription in expected)
- {
- Assert.Equal(cmdlt.ProfileClient.GetSubscription(subscription.Id).Environment, expectedEnv);
- }
- Assert.Equal(1, commandRuntimeMock.OutputPipeline.Count);
- }
- finally
- {
- // Cleanup
- FileUtilities.DataStore = oldDataStore;
- }
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void ImportPublishSettingsFileOverwritesEnvironment()
- {
- ImportAzurePublishSettingsCommand cmdlt = new ImportAzurePublishSettingsCommand();
- var oldAzureDataStore = AzureSession.DataStore;
- AzureSession.DataStore = new MemoryDataStore();
- // Setup
- AzureSession.DataStore.WriteFile("ImportPublishSettingsFileSelectsCorrectEnvironment.publishsettings",
- Commands.Common.Test.Properties.Resources.ValidProfileChina);
- var profile = new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- AzureSMCmdlet.CurrentProfile = profile;
- ProfileClient client = new ProfileClient(profile);
- var oldDataStore = FileUtilities.DataStore;
- FileUtilities.DataStore = AzureSession.DataStore;
- var expectedEnv = "AzureCloud";
- var expected = client.ImportPublishSettings("ImportPublishSettingsFileSelectsCorrectEnvironment.publishsettings", expectedEnv);
-
- cmdlt.CommandRuntime = commandRuntimeMock;
- cmdlt.ProfileClient = client;
- cmdlt.PublishSettingsFile = "ImportPublishSettingsFileSelectsCorrectEnvironment.publishsettings";
- cmdlt.Environment = expectedEnv;
-
- try
- {
- // Act
- cmdlt.InvokeBeginProcessing();
- cmdlt.ExecuteCmdlet();
- cmdlt.InvokeEndProcessing();
-
- // Verify
- foreach (var subscription in expected)
- {
- Assert.Equal(cmdlt.ProfileClient.GetSubscription(subscription.Id).Environment, expectedEnv);
- }
- Assert.Equal(1, commandRuntimeMock.OutputPipeline.Count);
- }
- finally
- {
- // Cleanup
- FileUtilities.DataStore = oldDataStore;
- AzureSession.DataStore = oldAzureDataStore;
- }
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void ImportPublishSettingsWorksForCustomProfile()
- {
- ImportAzurePublishSettingsCommand cmdlt = new ImportAzurePublishSettingsCommand();
- var oldAzureDataStore = AzureSession.DataStore;
- AzureSession.DataStore = new MemoryDataStore();
- // Setup
- AzureSession.DataStore.WriteFile("ImportPublishSettingsFileSelectsCorrectEnvironment.publishsettings",
- Commands.Common.Test.Properties.Resources.ValidProfileChina);
- var oldProfile = new AzureSMProfile();
- AzureSMCmdlet.CurrentProfile = oldProfile;
- var profile = new AzureSMProfile();
- ProfileClient client = new ProfileClient(profile);
- var oldDataStore = FileUtilities.DataStore;
- FileUtilities.DataStore = AzureSession.DataStore;
- var expectedEnv = "AzureCloud";
- var expected = client.ImportPublishSettings("ImportPublishSettingsFileSelectsCorrectEnvironment.publishsettings", expectedEnv);
- AzureSMCmdlet.CurrentProfile = new AzureSMProfile();
- cmdlt.Profile = profile;
- cmdlt.CommandRuntime = commandRuntimeMock;
- cmdlt.ProfileClient = new ProfileClient(new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile)));
- cmdlt.PublishSettingsFile = "ImportPublishSettingsFileSelectsCorrectEnvironment.publishsettings";
- cmdlt.Environment = expectedEnv;
-
- try
- {
- // Act
- cmdlt.InvokeBeginProcessing();
- cmdlt.ExecuteCmdlet();
- cmdlt.InvokeEndProcessing();
-
- // Verify
- foreach (var subscription in expected)
- {
- Assert.Equal(client.GetSubscription(subscription.Id).Environment, expectedEnv);
- }
- Assert.Equal(1, commandRuntimeMock.OutputPipeline.Count);
- Assert.Equal(oldProfile.Subscriptions.Count, 0);
- Assert.Equal(oldProfile.Accounts.Count, 0);
- }
- finally
- {
- // Cleanup
- FileUtilities.DataStore = oldDataStore;
- AzureSession.DataStore = oldAzureDataStore;
- }
- }
-
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void SelectDefaultAzureSubscriptionByNameUpdatesProfile()
- {
- var client = SetupDefaultProfile();
- SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
-
- // Setup
- cmdlt.CommandRuntime = commandRuntimeMock;
- cmdlt.SetParameterSet("SelectDefaultSubscriptionByNameParameterSet");
- cmdlt.SubscriptionName = azureSubscription2.Name;
- cmdlt.Default = new SwitchParameter(true);
- Assert.NotEqual(azureSubscription2.Id, client.Profile.DefaultSubscription.Id);
-
- // Act
- cmdlt.InvokeBeginProcessing();
- cmdlt.ExecuteCmdlet();
- cmdlt.InvokeEndProcessing();
-
- // Verify
- client = new ProfileClient(new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile)));
- Assert.NotNull(client.Profile.DefaultSubscription);
- Assert.Equal(azureSubscription2.Id, client.Profile.DefaultSubscription.Id);
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void SelectAzureSubscriptionByNameUpdatesProfile()
- {
- SetupDefaultProfile();
- SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
-
- // Setup
- cmdlt.CommandRuntime = commandRuntimeMock;
- cmdlt.SetParameterSet("SelectSubscriptionByNameParameterSet");
- cmdlt.SubscriptionName = azureSubscription2.Name;
-
- // Act
- cmdlt.InvokeBeginProcessing();
- cmdlt.ExecuteCmdlet();
- cmdlt.InvokeEndProcessing();
-
- // Verify
- Assert.NotNull(cmdlt.Profile.Context.Subscription);
- Assert.Equal(azureSubscription2.Id, cmdlt.Profile.Context.Subscription.Id);
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void SelectAzureSubscriptionByNameUpdatesCustomProfile()
- {
- var client = SetupDefaultProfile();
- var profile = SetupCustomProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile));
- SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
-
- // Setup
- cmdlt.CommandRuntime = commandRuntimeMock;
- cmdlt.SetParameterSet("SelectSubscriptionByNameParameterSet");
- cmdlt.SubscriptionName = azureSubscription2.Name;
- cmdlt.Profile = profile;
-
- // Act
- cmdlt.InvokeBeginProcessing();
- cmdlt.ExecuteCmdlet();
- cmdlt.InvokeEndProcessing();
-
- // Verify
- Assert.NotNull(cmdlt.Profile.Context.Subscription);
- Assert.Equal(azureSubscription2.Id, cmdlt.Profile.Context.Subscription.Id);
- // current profile unchanged
- Assert.Equal(azureSubscription1.Id, client.Profile.Context.Subscription.Id);
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void SelectAzureSubscriptionByNameWithoutAccountPreservesTheAccount()
- {
- SetupDefaultProfile();
- SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
-
- // Setup
- cmdlt.CommandRuntime = commandRuntimeMock;
- cmdlt.SetParameterSet("SelectSubscriptionByNameParameterSet");
- cmdlt.SubscriptionName = azureSubscription2.Name;
-
- // Act
- cmdlt.InvokeBeginProcessing();
- cmdlt.ExecuteCmdlet();
- cmdlt.InvokeEndProcessing();
-
- // Verify
- Assert.NotNull(cmdlt.Profile.Context.Subscription);
- Assert.Equal(azureSubscription2.Account, cmdlt.Profile.Context.Subscription.Account);
- Assert.Equal(azureSubscription2.Id, cmdlt.Profile.Context.Subscription.Id);
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void SelectAzureSubscriptionByIdWithoutAccountPreservesTheAccount()
- {
- SetupDefaultProfile();
- SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
-
- // Setup
- cmdlt.CommandRuntime = commandRuntimeMock;
- cmdlt.SetParameterSet("SelectSubscriptionByIdParameterSet");
- cmdlt.SubscriptionId = azureSubscription2.Id.ToString();
-
- // Act
- cmdlt.InvokeBeginProcessing();
- cmdlt.ExecuteCmdlet();
- cmdlt.InvokeEndProcessing();
-
- // Verify
- Assert.NotNull(cmdlt.Profile.Context.Subscription);
- Assert.Equal(azureSubscription2.Account, cmdlt.Profile.Context.Subscription.Account);
- Assert.Equal(azureSubscription2.Id, cmdlt.Profile.Context.Subscription.Id);
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void SelectAzureSubscriptionWithoutPassthroughDoesNotPrint()
- {
- SetupDefaultProfile();
- SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
-
- // Setup
- cmdlt.CommandRuntime = commandRuntimeMock;
- cmdlt.SetParameterSet("SelectSubscriptionByNameParameterSet");
- cmdlt.SubscriptionName = azureSubscription2.Name;
-
- // Act
- cmdlt.InvokeBeginProcessing();
- cmdlt.ExecuteCmdlet();
- cmdlt.InvokeEndProcessing();
-
- // Verify
- Assert.Equal(0, commandRuntimeMock.OutputPipeline.Count);
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void SelectAzureSubscriptionWithPassthroughPrintsSubscription()
- {
- SetupDefaultProfile();
- SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
-
- // Setup
- cmdlt.CommandRuntime = commandRuntimeMock;
- cmdlt.SetParameterSet("SelectSubscriptionByNameParameterSet");
- cmdlt.SubscriptionName = azureSubscription2.Name;
- cmdlt.PassThru = new SwitchParameter(true);
-
- // Act
- cmdlt.InvokeBeginProcessing();
- cmdlt.ExecuteCmdlet();
- cmdlt.InvokeEndProcessing();
-
- // Verify
- Assert.Equal(1, commandRuntimeMock.OutputPipeline.Count);
- Assert.True(commandRuntimeMock.OutputPipeline[0] is PSAzureSubscription);
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void SelectDefaultAzureSubscriptionByIdAndNoDefaultUpdatesProfile()
- {
- var client = SetupDefaultProfile();
- SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
-
- // Setup
- cmdlt.CommandRuntime = commandRuntimeMock;
- cmdlt.SetParameterSet("SelectDefaultSubscriptionByIdParameterSet");
- cmdlt.SubscriptionId = azureSubscription2.Id.ToString();
- cmdlt.Default = new SwitchParameter(true);
- Assert.NotEqual(azureSubscription2.Id, client.Profile.DefaultSubscription.Id);
-
- // Act
- cmdlt.InvokeBeginProcessing();
- cmdlt.ExecuteCmdlet();
- cmdlt.InvokeEndProcessing();
-
- // Verify
- client = new ProfileClient(new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile)));
- Assert.NotNull(client.Profile.DefaultSubscription);
- Assert.Equal(azureSubscription2.Id, client.Profile.DefaultSubscription.Id);
-
- cmdlt = new SelectAzureSubscriptionCommand();
-
- // Setup
- cmdlt.CommandRuntime = commandRuntimeMock;
- cmdlt.SetParameterSet("NoDefaultSubscriptionParameterSet");
- cmdlt.NoDefault = new SwitchParameter(true);
-
- // Act
- cmdlt.InvokeBeginProcessing();
- cmdlt.ExecuteCmdlet();
- cmdlt.InvokeEndProcessing();
-
- // Verify
- client = new ProfileClient(new AzureSMProfile(Path.Combine(AzureSession.ProfileDirectory, AzureSession.ProfileFile)));
- Assert.Null(client.Profile.DefaultSubscription);
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void SelectAzureSubscriptionByInvalidIdThrowsException()
- {
- SetupDefaultProfile();
- SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
-
- // Setup
- cmdlt.CommandRuntime = commandRuntimeMock;
- cmdlt.SetParameterSet("SelectSubscriptionByIdParameterSet");
- string invalidGuid = Guid.NewGuid().ToString();
- cmdlt.SubscriptionId = invalidGuid;
-
- // Act
- cmdlt.InvokeBeginProcessing();
- try
- {
- cmdlt.ExecuteCmdlet();
- Assert.True(false);
- }
- catch (ArgumentException ex)
- {
- Assert.Contains(string.Format("The subscription id {0} doesn't exist.\r\nParameter name: id", invalidGuid), ex.Message);
- }
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void SelectAzureSubscriptionByInvalidGuidThrowsException()
- {
- SelectAzureSubscriptionCommand cmdlt = new SelectAzureSubscriptionCommand();
- SetupDefaultProfile();
-
- // Setup
- cmdlt.CommandRuntime = commandRuntimeMock;
- cmdlt.SetParameterSet("SelectSubscriptionByIdParameterSet");
- string invalidGuid = "foo";
- cmdlt.SubscriptionId = invalidGuid;
-
- // Act
- cmdlt.InvokeBeginProcessing();
- try
- {
- cmdlt.ExecuteCmdlet();
- Assert.True(false);
- }
- catch (ArgumentException ex)
- {
- Assert.Contains(string.Format(Microsoft.WindowsAzure.Commands.Common.Properties.Resources.InvalidGuid, invalidGuid), ex.Message);
- }
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void CanCreateProfileCertificateAuth()
- {
- RunCreateProfileTestForParams(
- (cmdlet) =>
- {
- cmdlet.Certificate = SampleCertificate;
- },
- NewAzureProfileCommand.CertificateParameterSet, ValidateCertificate);
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void CanCreateProfileFromHashCertificateAuth()
- {
- RunCreateProfileTestForHashTable(
- (cmdlet) =>
- {
- cmdlet.Properties.Add(NewAzureProfileCommand.CertificateKey, SampleCertificate);
- }, ValidateCertificate);
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void CanCreateProfileWithADAuth()
- {
- var credential = GenerateCredential();
- RunCreateProfileTestForParams(
- (cmdlet) =>
- {
- cmdlet.Credential = credential;
- }, NewAzureProfileCommand.CredentialsParameterSet,
- (profile) => ValidateCredential(credential, profile, AzureAccount.AccountType.User));
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void CanCreateProfileFromHashWithADAuth()
- {
- var password = GeneratePassword();
- var credential = GenerateCredential(password);
- RunCreateProfileTestForHashTable(
- (cmdlet) =>
- {
- cmdlet.Properties[NewAzureProfileCommand.UsernameKey] = credential.UserName;
- cmdlet.Properties[NewAzureProfileCommand.PasswordKey] = password;
- }, (profile) => ValidateCredential(credential, profile, AzureAccount.AccountType.User));
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void CanAddAccountToCustomProfile()
- {
- var cmdlet = new AddAzureAccount();
- var csmSubscription = Guid.NewGuid();
- var rdfeSubscription = Guid.NewGuid();
- var credential = GenerateCredential("mySillyPassword");
- var profile = new AzureSMProfile();
- var client = new ProfileClient(profile);
- cmdlet.Credential = credential;
- cmdlet.Profile = profile;
- cmdlet.SetParameterSet("User");
-
- AzureSession.ClientFactory =
- new MockClientFactory(
- new List<object>
- {
- ProfileClientHelper.CreateRdfeSubscriptionClient(rdfeSubscription, rdfeSubscription.ToString()),
- ProfileClientHelper.CreateCsmSubscriptionClient(new List<string>{csmSubscription.ToString()},
- new List<string>{csmSubscription.ToString(), rdfeSubscription.ToString()})
- }, true);
- AzureSession.AuthenticationFactory = new MockTokenAuthenticationFactory(credential.UserName,
- Guid.NewGuid().ToString());
- cmdlet.CommandRuntime = commandRuntimeMock;
- cmdlet.InvokeBeginProcessing();
- cmdlet.ExecuteCmdlet();
- cmdlet.InvokeEndProcessing();
- Assert.NotNull(profile.Subscriptions);
- Assert.NotNull(profile.Accounts);
- Assert.NotNull(profile.Environments);
- Assert.NotNull(profile.Context);
- Assert.Equal(profile.Subscriptions.Values.Count((s) => s.Id == csmSubscription), 0);
- Assert.Equal(profile.Subscriptions.Values.Count((s) => s.Id == rdfeSubscription), 1);
- Assert.Equal(profile.Accounts.Values.Count((s) => s.Id == credential.UserName), 1);
- Assert.Contains(rdfeSubscription.ToString(), profile.Accounts.First().Value.GetProperty(AzureAccount.Property.Subscriptions));
- Assert.Equal(profile.Context.Account.Id, credential.UserName);
- Assert.Equal(profile.Context.Subscription.Id, rdfeSubscription);
- }
-
- [Fact]
- [Trait(Category.AcceptanceType, Category.CheckIn)]
- public void AddAzureAccountThrowsForEmptySubscriptions()
- {
- var cmdlet = new AddAzureAccount();
- var csmSubscription = Guid.NewGuid();
- var rdfeSubscription = Guid.NewGuid();
- var credential = GenerateCredential("mySillyPassword");
- var profile = new AzureSMProfile();
- var client = new ProfileClient(profile);
- cmdlet.Credential = credential;
- cmdlet.Profile = profile;
- cmdlet.SetParameterSet("User");
-
- AzureSession.ClientFactory =
- new MockClientFactory(
- new List<object>
- {
- ProfileClientHelper.CreateRdfeSubscriptionClient(rdfeSubscription),
- ProfileClientHelper.CreateCsmSubscriptionClient(new List<string>(),
- new List<string>{csmSubscription.ToString(), rdfeSubscription.ToString()})
- }, true);
- AzureSession.AuthenticationFactory = new MockTokenAuthenticationFactory(credential.UserName,
- Guid.NewGuid().ToString());
- cmdlet.CommandRuntime = commandRuntimeMock;
-