|
| 1 | +// ---------------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// Copyright Microsoft Corporation |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +// ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +using Microsoft.Azure.Commands.Common.Authentication; |
| 16 | +using Microsoft.Azure.Commands.Common.Authentication.Abstractions; |
| 17 | +using Microsoft.WindowsAzure.Commands.Common; |
| 18 | +using Microsoft.WindowsAzure.Commands.Common.Test.Mocks; |
| 19 | +using Microsoft.WindowsAzure.Commands.ScenarioTest; |
| 20 | +using Microsoft.WindowsAzure.Commands.Storage; |
| 21 | +using Microsoft.WindowsAzure.Commands.Storage.Common.Cmdlet; |
| 22 | +using Microsoft.WindowsAzure.Commands.Utilities.Common; |
| 23 | +using System.Linq; |
| 24 | +using Xunit; |
| 25 | +using System; |
| 26 | +using System.Collections.Generic; |
| 27 | + |
| 28 | +namespace Microsoft.WindowsAzure.Management.Storage.Test.Common.Cmdlet |
| 29 | +{ |
| 30 | + public class StorageContextDisconnectedTests |
| 31 | + { |
| 32 | + class TestProfileProvider : AzureRmProfileProvider |
| 33 | + { |
| 34 | + public override T GetProfile<T>() |
| 35 | + { |
| 36 | + return default(T); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + class TestSMProfileProvider : AzureSMProfileProvider |
| 41 | + { |
| 42 | + public override T GetProfile<T>() |
| 43 | + { |
| 44 | + return default(T); |
| 45 | + } |
| 46 | + |
| 47 | + public override void SetTokenCacheForProfile(IAzureContextContainer profile) |
| 48 | + { |
| 49 | + |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + class TestContextContainer : IAzureContextContainer |
| 54 | + { |
| 55 | + public IEnumerable<IAzureAccount> Accounts |
| 56 | + { |
| 57 | + get; |
| 58 | + } = new List<IAzureAccount>(); |
| 59 | + |
| 60 | + public IAzureContext DefaultContext |
| 61 | + { |
| 62 | + get; set; |
| 63 | + } |
| 64 | + |
| 65 | + public IEnumerable<IAzureEnvironment> Environments |
| 66 | + { |
| 67 | + get; |
| 68 | + } = new List<IAzureEnvironment>(); |
| 69 | + |
| 70 | + public IDictionary<string, string> ExtendedProperties |
| 71 | + { |
| 72 | + get; |
| 73 | + } = new Dictionary<string, string>(); |
| 74 | + |
| 75 | + public IEnumerable<IAzureSubscription> Subscriptions |
| 76 | + { |
| 77 | + get; |
| 78 | + } = new List<IAzureSubscription>(); |
| 79 | + |
| 80 | + public void Clear() |
| 81 | + { |
| 82 | + |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + [Fact] |
| 87 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 88 | + public void CanCreateStorageContext() |
| 89 | + { |
| 90 | + AzureSessionInitializer.InitializeAzureSession(); |
| 91 | + var smProvider = AzureSMProfileProvider.Instance; |
| 92 | + var rmProvider = AzureRmProfileProvider.Instance; |
| 93 | + AzureRmProfileProvider.SetInstance(() => new TestProfileProvider(), true); |
| 94 | + AzureSMProfileProvider.SetInstance(()=> new TestSMProfileProvider(), true); |
| 95 | + try |
| 96 | + { |
| 97 | + var mock = new MockCommandRuntime(); |
| 98 | + |
| 99 | + AzureSMProfileProvider.Instance.Profile = null; |
| 100 | + AzureRmProfileProvider.Instance.Profile = new TestContextContainer(); |
| 101 | + var cmdlet = new NewAzureStorageContext |
| 102 | + { |
| 103 | + CommandRuntime = mock, |
| 104 | + StorageAccountName = "contosostorage", |
| 105 | + StorageAccountKey = "AAAAAAAA", |
| 106 | + }; |
| 107 | + |
| 108 | + cmdlet.SetParameterSet("AccountNameAndKey"); |
| 109 | + cmdlet.ExecuteCmdlet(); |
| 110 | + var output = mock.OutputPipeline; |
| 111 | + Assert.NotNull(output); |
| 112 | + var storageContext = output.First() as AzureStorageContext; |
| 113 | + Assert.NotNull(storageContext); |
| 114 | + Assert.Equal(cmdlet.StorageAccountName, storageContext.StorageAccountName); |
| 115 | + } |
| 116 | + finally |
| 117 | + { |
| 118 | + AzureSMProfileProvider.SetInstance(() => smProvider, true); |
| 119 | + AzureRmProfileProvider.SetInstance(() => rmProvider, true); |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments