|
| 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.Test.Mocks; |
| 18 | +using Microsoft.WindowsAzure.Commands.ScenarioTest; |
| 19 | +using Microsoft.WindowsAzure.Commands.Storage; |
| 20 | +using Microsoft.WindowsAzure.Commands.Storage.Common.Cmdlet; |
| 21 | +using Microsoft.WindowsAzure.Commands.Utilities.Common; |
| 22 | +using System.Collections.Generic; |
| 23 | +using System.Linq; |
| 24 | +using Xunit; |
| 25 | + |
| 26 | +namespace Microsoft.WindowsAzure.Management.Storage.Test.Common.Cmdlet |
| 27 | +{ |
| 28 | + public class StorageContextDisconnectedTests |
| 29 | + { |
| 30 | + class TestProfileProvider : AzureRmProfileProvider |
| 31 | + { |
| 32 | + public override T GetProfile<T>() |
| 33 | + { |
| 34 | + return default(T); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + class TestSMProfileProvider : AzureSMProfileProvider |
| 39 | + { |
| 40 | + public override T GetProfile<T>() |
| 41 | + { |
| 42 | + return default(T); |
| 43 | + } |
| 44 | + |
| 45 | + public override void SetTokenCacheForProfile(IAzureContextContainer profile) |
| 46 | + { |
| 47 | + |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + class TestContextContainer : IAzureContextContainer |
| 52 | + { |
| 53 | + List<IAzureEnvironment> _environments = new List<IAzureEnvironment>(); |
| 54 | + public TestContextContainer() |
| 55 | + { |
| 56 | + foreach(var environment in AzureEnvironment.PublicEnvironments) |
| 57 | + { |
| 58 | + _environments.Add(environment.Value); |
| 59 | + } |
| 60 | + } |
| 61 | + public IEnumerable<IAzureAccount> Accounts |
| 62 | + { |
| 63 | + get; |
| 64 | + } = new List<IAzureAccount>(); |
| 65 | + |
| 66 | + public IAzureContext DefaultContext |
| 67 | + { |
| 68 | + get; set; |
| 69 | + } |
| 70 | + |
| 71 | + public IEnumerable<IAzureEnvironment> Environments |
| 72 | + { |
| 73 | + get { return _environments; } |
| 74 | + } |
| 75 | + |
| 76 | + public IDictionary<string, string> ExtendedProperties |
| 77 | + { |
| 78 | + get; |
| 79 | + } = new Dictionary<string, string>(); |
| 80 | + |
| 81 | + public IEnumerable<IAzureSubscription> Subscriptions |
| 82 | + { |
| 83 | + get; |
| 84 | + } = new List<IAzureSubscription>(); |
| 85 | + |
| 86 | + public void Clear() |
| 87 | + { |
| 88 | + |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + [Fact] |
| 93 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 94 | + public void CanCreateStorageContextNameAndKey() |
| 95 | + { |
| 96 | + AzureSessionInitializer.InitializeAzureSession(); |
| 97 | + var smProvider = AzureSMProfileProvider.Instance; |
| 98 | + var rmProvider = AzureRmProfileProvider.Instance; |
| 99 | + AzureRmProfileProvider.SetInstance(() => new TestProfileProvider(), true); |
| 100 | + AzureSMProfileProvider.SetInstance(()=> new TestSMProfileProvider(), true); |
| 101 | + try |
| 102 | + { |
| 103 | + var mock = new MockCommandRuntime(); |
| 104 | + |
| 105 | + AzureSMProfileProvider.Instance.Profile = null; |
| 106 | + AzureRmProfileProvider.Instance.Profile = new TestContextContainer(); |
| 107 | + var cmdlet = new NewAzureStorageContext |
| 108 | + { |
| 109 | + CommandRuntime = mock, |
| 110 | + StorageAccountName = "contosostorage", |
| 111 | + StorageAccountKey = "AAAAAAAA", |
| 112 | + }; |
| 113 | + |
| 114 | + cmdlet.SetParameterSet("AccountNameAndKey"); |
| 115 | + cmdlet.ExecuteCmdlet(); |
| 116 | + var output = mock.OutputPipeline; |
| 117 | + Assert.NotNull(output); |
| 118 | + var storageContext = output.First() as AzureStorageContext; |
| 119 | + Assert.NotNull(storageContext); |
| 120 | + Assert.Equal(cmdlet.StorageAccountName, storageContext.StorageAccountName); |
| 121 | + |
| 122 | + } |
| 123 | + finally |
| 124 | + { |
| 125 | + AzureSMProfileProvider.SetInstance(() => smProvider, true); |
| 126 | + AzureRmProfileProvider.SetInstance(() => rmProvider, true); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + [Fact] |
| 131 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 132 | + public void CanCreateStorageContextInChinaCloud() |
| 133 | + { |
| 134 | + AzureSessionInitializer.InitializeAzureSession(); |
| 135 | + var smProvider = AzureSMProfileProvider.Instance; |
| 136 | + var rmProvider = AzureRmProfileProvider.Instance; |
| 137 | + AzureRmProfileProvider.SetInstance(() => new TestProfileProvider(), true); |
| 138 | + AzureSMProfileProvider.SetInstance(() => new TestSMProfileProvider(), true); |
| 139 | + try |
| 140 | + { |
| 141 | + var mock = new MockCommandRuntime(); |
| 142 | + |
| 143 | + AzureSMProfileProvider.Instance.Profile = null; |
| 144 | + AzureRmProfileProvider.Instance.Profile = new TestContextContainer(); |
| 145 | + var cmdlet = new NewAzureStorageContext |
| 146 | + { |
| 147 | + CommandRuntime = mock, |
| 148 | + StorageAccountName = "contosostorage", |
| 149 | + StorageAccountKey = "AAAAAAAA", |
| 150 | + Environment = EnvironmentName.AzureChinaCloud |
| 151 | + }; |
| 152 | + |
| 153 | + cmdlet.SetParameterSet("AccountNameAndKeyEnvironment"); |
| 154 | + cmdlet.ExecuteCmdlet(); |
| 155 | + var output = mock.OutputPipeline; |
| 156 | + Assert.NotNull(output); |
| 157 | + var storageContext = output.First() as AzureStorageContext; |
| 158 | + Assert.NotNull(storageContext); |
| 159 | + Assert.Equal(cmdlet.StorageAccountName, storageContext.StorageAccountName); |
| 160 | + Assert.True(storageContext.BlobEndPoint.Contains(".cn")); |
| 161 | + |
| 162 | + } |
| 163 | + finally |
| 164 | + { |
| 165 | + AzureSMProfileProvider.SetInstance(() => smProvider, true); |
| 166 | + AzureRmProfileProvider.SetInstance(() => rmProvider, true); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + |
| 171 | + [Fact] |
| 172 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 173 | + public void CanCreateStorageContextSASToken() |
| 174 | + { |
| 175 | + AzureSessionInitializer.InitializeAzureSession(); |
| 176 | + var smProvider = AzureSMProfileProvider.Instance; |
| 177 | + var rmProvider = AzureRmProfileProvider.Instance; |
| 178 | + AzureRmProfileProvider.SetInstance(() => new TestProfileProvider(), true); |
| 179 | + AzureSMProfileProvider.SetInstance(() => new TestSMProfileProvider(), true); |
| 180 | + try |
| 181 | + { |
| 182 | + var mock = new MockCommandRuntime(); |
| 183 | + |
| 184 | + AzureSMProfileProvider.Instance.Profile = null; |
| 185 | + AzureRmProfileProvider.Instance.Profile = new TestContextContainer(); |
| 186 | + var cmdlet = new NewAzureStorageContext |
| 187 | + { |
| 188 | + CommandRuntime = mock, |
| 189 | + StorageAccountName = "contosostorage", |
| 190 | + SasToken = "AAAAAAAA", |
| 191 | + }; |
| 192 | + |
| 193 | + cmdlet.SetParameterSet("SasToken"); |
| 194 | + cmdlet.ExecuteCmdlet(); |
| 195 | + var output = mock.OutputPipeline; |
| 196 | + Assert.NotNull(output); |
| 197 | + var storageContext = output.First() as AzureStorageContext; |
| 198 | + Assert.NotNull(storageContext); |
| 199 | + Assert.Equal("[SasToken]", storageContext.StorageAccountName); |
| 200 | + } |
| 201 | + finally |
| 202 | + { |
| 203 | + AzureSMProfileProvider.SetInstance(() => smProvider, true); |
| 204 | + AzureRmProfileProvider.SetInstance(() => rmProvider, true); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + [Fact] |
| 209 | + [Trait(Category.AcceptanceType, Category.CheckIn)] |
| 210 | + public void CanCreateStorageContextAnonymous() |
| 211 | + { |
| 212 | + AzureSessionInitializer.InitializeAzureSession(); |
| 213 | + var smProvider = AzureSMProfileProvider.Instance; |
| 214 | + var rmProvider = AzureRmProfileProvider.Instance; |
| 215 | + AzureRmProfileProvider.SetInstance(() => new TestProfileProvider(), true); |
| 216 | + AzureSMProfileProvider.SetInstance(() => new TestSMProfileProvider(), true); |
| 217 | + try |
| 218 | + { |
| 219 | + var mock = new MockCommandRuntime(); |
| 220 | + |
| 221 | + AzureSMProfileProvider.Instance.Profile = null; |
| 222 | + AzureRmProfileProvider.Instance.Profile = new TestContextContainer(); |
| 223 | + var cmdlet = new NewAzureStorageContext |
| 224 | + { |
| 225 | + CommandRuntime = mock, |
| 226 | + StorageAccountName = "contosostorage", |
| 227 | + Anonymous = true, |
| 228 | + }; |
| 229 | + |
| 230 | + cmdlet.SetParameterSet("AnonymousAccount"); |
| 231 | + cmdlet.ExecuteCmdlet(); |
| 232 | + var output = mock.OutputPipeline; |
| 233 | + Assert.NotNull(output); |
| 234 | + var storageContext = output.First() as AzureStorageContext; |
| 235 | + Assert.NotNull(storageContext); |
| 236 | + Assert.Equal("[Anonymous]", storageContext.StorageAccountName); |
| 237 | + } |
| 238 | + finally |
| 239 | + { |
| 240 | + AzureSMProfileProvider.SetInstance(() => smProvider, true); |
| 241 | + AzureRmProfileProvider.SetInstance(() => rmProvider, true); |
| 242 | + } |
| 243 | + } |
| 244 | + |
| 245 | + } |
| 246 | +} |
0 commit comments