PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/ResourceManager/Resources/Commands.Resources.Test/Features/GetAzureProviderFeatureCmdletTests.cs

https://gitlab.com/jslee1/azure-powershell
C# | 365 lines | 244 code | 57 blank | 64 comment | 0 complexity | a4b14711540a5411daf5138f3aac5e72 MD5 | raw file
  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. namespace Microsoft.Azure.Commands.Resources.Test
  15. {
  16. using Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation;
  17. using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkClient;
  18. using Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels;
  19. using Microsoft.Azure.Management.ResourceManager;
  20. using Microsoft.Azure.Management.ResourceManager.Models;
  21. using Microsoft.Rest.Azure;
  22. using Microsoft.WindowsAzure.Commands.Common.Test.Mocks;
  23. using Microsoft.WindowsAzure.Commands.ScenarioTest;
  24. using Moq;
  25. using System;
  26. using System.Collections.Generic;
  27. using System.Linq;
  28. using System.Management.Automation;
  29. using System.Threading;
  30. using System.Threading.Tasks;
  31. using WindowsAzure.Commands.Test.Utilities.Common;
  32. using Xunit;
  33. using Xunit.Abstractions;
  34. /// <summary>
  35. /// Tests the Azure Provider Feature cmdlets
  36. /// </summary>
  37. public class GetAzureProviderFeatureCmdletTests : RMTestBase
  38. {
  39. /// <summary>
  40. /// An instance of the cmdlet
  41. /// </summary>
  42. private readonly GetAzureProviderFeatureCmdletTest cmdlet;
  43. /// <summary>
  44. /// A mock of the command runtime
  45. /// </summary>
  46. private readonly Mock<ICommandRuntime> commandRuntimeMock;
  47. private MockCommandRuntime mockRuntime;
  48. /// <summary>
  49. /// A mock of the client
  50. /// </summary>
  51. private readonly Mock<IFeaturesOperations> featureOperationsMock;
  52. /// <summary>
  53. /// Initializes a new instance of the <see cref="GetAzureProviderFeatureCmdletTests"/> class.
  54. /// </summary>
  55. public GetAzureProviderFeatureCmdletTests(ITestOutputHelper output)
  56. {
  57. this.featureOperationsMock = new Mock<IFeaturesOperations>();
  58. var featureClient = new Mock<IFeatureClient>();
  59. featureClient
  60. .SetupGet(client => client.Features)
  61. .Returns(() => this.featureOperationsMock.Object);
  62. this.commandRuntimeMock = new Mock<ICommandRuntime>();
  63. this.cmdlet = new GetAzureProviderFeatureCmdletTest
  64. {
  65. //CommandRuntime = commandRuntimeMock.Object,
  66. ProviderFeatureClient = new ProviderFeatureClient
  67. {
  68. FeaturesManagementClient = featureClient.Object
  69. }
  70. };
  71. PSCmdletExtensions.SetCommandRuntimeMock(cmdlet, commandRuntimeMock.Object);
  72. mockRuntime = new MockCommandRuntime();
  73. commandRuntimeMock.Setup(f => f.Host).Returns(mockRuntime.Host);
  74. }
  75. /// <summary>
  76. /// Validates all Get-AzureRmResourceProvider parameter combinations
  77. /// </summary>
  78. [Fact]
  79. [Trait(Category.AcceptanceType, Category.CheckIn)]
  80. public void GetProviderFeatureTests()
  81. {
  82. // setup return values
  83. const string Provider1Namespace = "Providers.Test1";
  84. const string Feature1Name = "feature1";
  85. const string Provider2Namespace = "Providers.Test2";
  86. const string Feature2Name = "feature2";
  87. var provider1RegisteredFeature = new FeatureResult
  88. {
  89. Id = "featureId1",
  90. Name = Provider1Namespace + "/" + Feature1Name,
  91. Properties = new FeatureProperties
  92. {
  93. State = ProviderFeatureClient.RegisteredStateName,
  94. },
  95. Type = "Microsoft.Features/feature"
  96. };
  97. var provider1UnregisteredFeature = new FeatureResult
  98. {
  99. Id = "featureId1",
  100. Name = Provider1Namespace + "/" + Feature2Name,
  101. Properties = new FeatureProperties
  102. {
  103. State = "Unregistered",
  104. },
  105. Type = "Microsoft.Features/feature"
  106. };
  107. var provider2UnregisteredFeature = new FeatureResult
  108. {
  109. Id = "featureId2",
  110. Name = Provider2Namespace + "/" + Feature1Name,
  111. Properties = new FeatureProperties
  112. {
  113. State = "Unregistered",
  114. },
  115. Type = "Microsoft.Features/feature"
  116. };
  117. var pagableResult = new Page<FeatureResult>();
  118. //var listResult = new[] { provider1RegisteredFeature, provider1UnregisteredFeature, provider2UnregisteredFeature };
  119. var listResult = new List<FeatureResult>() { provider1RegisteredFeature, provider1UnregisteredFeature, provider2UnregisteredFeature };
  120. pagableResult.SetItemValue<FeatureResult>(listResult);
  121. var result = new AzureOperationResponse<IPage<FeatureResult>>()
  122. {
  123. Body = pagableResult
  124. };
  125. this.featureOperationsMock
  126. .Setup(f => f.ListAllWithHttpMessagesAsync(null, It.IsAny<CancellationToken>()))
  127. .Returns(() => Task.FromResult(result));
  128. // 1. List only registered features of providers
  129. this.commandRuntimeMock
  130. .Setup(m => m.WriteObject(It.IsAny<object>()))
  131. .Callback((object obj) =>
  132. {
  133. Assert.IsType<PSProviderFeature[]>(obj);
  134. var features = (PSProviderFeature[])obj;
  135. Assert.Equal(1, features.Length);
  136. var provider = features.Single();
  137. Assert.Equal(Provider1Namespace, provider.ProviderName, StringComparer.OrdinalIgnoreCase);
  138. Assert.Equal(Feature1Name, provider.FeatureName, StringComparer.OrdinalIgnoreCase);
  139. Assert.Equal(ProviderFeatureClient.RegisteredStateName, provider.RegistrationState, StringComparer.OrdinalIgnoreCase);
  140. });
  141. this.cmdlet.ParameterSetOverride = GetAzureProviderFeatureCmdlet.ListAvailableParameterSet;
  142. this.cmdlet.ExecuteCmdlet();
  143. this.VerifyListAllCallPatternAndReset();
  144. // 2. List all features of all providers
  145. this.cmdlet.ListAvailable = true;
  146. this.commandRuntimeMock
  147. .Setup(m => m.WriteObject(It.IsAny<object>()))
  148. .Callback((object obj) =>
  149. {
  150. Assert.IsType<PSProviderFeature[]>(obj);
  151. var features = (PSProviderFeature[])obj;
  152. Assert.Equal(listResult.Count(), features.Length);
  153. });
  154. this.cmdlet.ExecuteCmdlet();
  155. this.VerifyListAllCallPatternAndReset();
  156. // 3.a. List only registered features of a particular provider - and they exist
  157. string providerOfChoice = Provider1Namespace;
  158. this.cmdlet.ListAvailable = false;
  159. this.cmdlet.ProviderNamespace = providerOfChoice;
  160. //pagableResult.SetItemValue<FeatureResult>(new List<FeatureResult>() { provider1RegisteredFeature, provider1UnregisteredFeature });
  161. this.featureOperationsMock
  162. .Setup(f => f.ListWithHttpMessagesAsync(It.IsAny<string>(), null, It.IsAny<CancellationToken>()))
  163. .Callback((string providerName, Dictionary<string, List<string>> customHeaders, CancellationToken ignored) => Assert.Equal(providerOfChoice, providerName, StringComparer.OrdinalIgnoreCase))
  164. .Returns(() => Task.FromResult(
  165. new AzureOperationResponse<IPage<FeatureResult>>()
  166. {
  167. Body = pagableResult
  168. }));
  169. this.commandRuntimeMock
  170. .Setup(m => m.WriteObject(It.IsAny<object>()))
  171. .Callback((object obj) =>
  172. {
  173. Assert.IsType<PSProviderFeature[]>(obj);
  174. var features = (PSProviderFeature[])obj;
  175. Assert.Equal(1, features.Length);
  176. var provider = features.Single();
  177. Assert.Equal(Provider1Namespace, provider.ProviderName, StringComparer.OrdinalIgnoreCase);
  178. Assert.Equal(Feature1Name, provider.FeatureName, StringComparer.OrdinalIgnoreCase);
  179. Assert.Equal(ProviderFeatureClient.RegisteredStateName, provider.RegistrationState, StringComparer.OrdinalIgnoreCase);
  180. });
  181. this.cmdlet.ParameterSetOverride = GetAzureProviderFeatureCmdlet.GetFeatureParameterSet;
  182. this.cmdlet.ExecuteCmdlet();
  183. this.VerifyListProviderFeaturesCallPatternAndReset();
  184. // 3.b. List only registered features of a particular provider - and they do not exist
  185. providerOfChoice = Provider2Namespace;
  186. this.cmdlet.ListAvailable = false;
  187. this.cmdlet.ProviderNamespace = providerOfChoice;
  188. //pagableResult.SetItemValue<FeatureResult>(new List<FeatureResult>() { provider2UnregisteredFeature });
  189. this.commandRuntimeMock
  190. .Setup(m => m.WriteObject(It.IsAny<object>()))
  191. .Callback((object obj) =>
  192. {
  193. Assert.IsType<PSProviderFeature[]>(obj);
  194. var features = (PSProviderFeature[])obj;
  195. Assert.Equal(0, features.Length);
  196. });
  197. this.cmdlet.ExecuteCmdlet();
  198. this.VerifyListProviderFeaturesCallPatternAndReset();
  199. // 4. List all features of a particular provider
  200. providerOfChoice = Provider1Namespace;
  201. this.cmdlet.ProviderNamespace = providerOfChoice;
  202. this.cmdlet.ListAvailable = true;
  203. //pagableResult.SetItemValue<FeatureResult>(new List<FeatureResult>() { provider1RegisteredFeature, provider1UnregisteredFeature });
  204. this.commandRuntimeMock
  205. .Setup(m => m.WriteObject(It.IsAny<object>()))
  206. .Callback((object obj) =>
  207. {
  208. Assert.IsType<PSProviderFeature[]>(obj);
  209. var features = (PSProviderFeature[])obj;
  210. Assert.Equal(2, features.Length);
  211. Assert.True(features.Any(feature => string.Equals(feature.FeatureName, Feature1Name, StringComparison.OrdinalIgnoreCase)));
  212. Assert.True(features.Any(feature => string.Equals(feature.FeatureName, Feature2Name, StringComparison.OrdinalIgnoreCase)));
  213. Assert.True(features.All(feature => string.Equals(feature.ProviderName, Provider1Namespace, StringComparison.OrdinalIgnoreCase)));
  214. });
  215. this.cmdlet.ParameterSetOverride = GetAzureProviderFeatureCmdlet.ListAvailableParameterSet;
  216. this.cmdlet.ExecuteCmdlet();
  217. this.VerifyListProviderFeaturesCallPatternAndReset();
  218. // 5. get a single provider feature by name
  219. this.cmdlet.ProviderNamespace = Provider2Namespace;
  220. this.cmdlet.FeatureName = Feature1Name;
  221. this.cmdlet.ListAvailable = false;
  222. this.featureOperationsMock
  223. .Setup(f => f.GetWithHttpMessagesAsync(It.IsAny<string>(), It.IsAny<string>(), null, It.IsAny<CancellationToken>()))
  224. .Callback((string providerName, string featureName, Dictionary<string, List<string>> customHeaders, CancellationToken ignored) =>
  225. {
  226. Assert.Equal(Provider2Namespace, providerName, StringComparer.OrdinalIgnoreCase);
  227. Assert.Equal(Feature1Name, featureName, StringComparer.OrdinalIgnoreCase);
  228. })
  229. .Returns(() => Task.FromResult(new AzureOperationResponse<FeatureResult>()
  230. {
  231. Body = provider2UnregisteredFeature
  232. }));
  233. this.commandRuntimeMock
  234. .Setup(m => m.WriteObject(It.IsAny<object>()))
  235. .Callback((object obj) =>
  236. {
  237. Assert.IsType<PSProviderFeature[]>(obj);
  238. var features = (PSProviderFeature[])obj;
  239. Assert.Equal(1, features.Length);
  240. var feature = features.Single();
  241. Assert.Equal(Provider2Namespace, feature.ProviderName, StringComparer.OrdinalIgnoreCase);
  242. Assert.Equal(Feature1Name, feature.FeatureName, StringComparer.OrdinalIgnoreCase);
  243. Assert.Equal("Unregistered", feature.RegistrationState, StringComparer.OrdinalIgnoreCase);
  244. });
  245. this.cmdlet.ParameterSetOverride = GetAzureProviderFeatureCmdlet.GetFeatureParameterSet;
  246. this.cmdlet.ExecuteCmdlet();
  247. this.VerifyGetCallPatternAndReset();
  248. }
  249. /// <summary>
  250. /// Resets the calls on the mocks
  251. /// </summary>
  252. private void ResetCalls()
  253. {
  254. this.featureOperationsMock.ResetCalls();
  255. this.commandRuntimeMock.ResetCalls();
  256. }
  257. /// <summary>
  258. /// Verifies the right call patterns are made
  259. /// </summary>
  260. private void VerifyGetCallPatternAndReset()
  261. {
  262. this.featureOperationsMock.Verify(f => f.GetWithHttpMessagesAsync(It.IsAny<string>(), It.IsAny<string>(), null, It.IsAny<CancellationToken>()), Times.Once());
  263. this.featureOperationsMock.Verify(f => f.ListWithHttpMessagesAsync(It.IsAny<string>(), null, It.IsAny<CancellationToken>()), Times.Never);
  264. this.featureOperationsMock.Verify(f => f.ListAllWithHttpMessagesAsync(null, It.IsAny<CancellationToken>()), Times.Never);
  265. this.featureOperationsMock.Verify(f => f.ListNextWithHttpMessagesAsync(It.IsAny<string>(), null, It.IsAny<CancellationToken>()), Times.Never);
  266. this.featureOperationsMock.Verify(f => f.ListAllNextWithHttpMessagesAsync(It.IsAny<string>(), null, It.IsAny<CancellationToken>()), Times.Never);
  267. this.commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny<object>(), It.IsAny<bool>()), Times.Once());
  268. this.ResetCalls();
  269. }
  270. /// <summary>
  271. /// Verifies the right call patterns are made
  272. /// </summary>
  273. private void VerifyListAllCallPatternAndReset()
  274. {
  275. this.featureOperationsMock.Verify(f => f.ListAllWithHttpMessagesAsync(null, It.IsAny<CancellationToken>()), Times.Once());
  276. this.featureOperationsMock.Verify(f => f.ListAllNextWithHttpMessagesAsync(It.IsAny<string>(), null, It.IsAny<CancellationToken>()), Times.Never);
  277. this.commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny<object>(), It.IsAny<bool>()), Times.Once());
  278. this.ResetCalls();
  279. }
  280. /// <summary>
  281. /// Verifies the right call patterns are made
  282. /// </summary>
  283. private void VerifyListProviderFeaturesCallPatternAndReset()
  284. {
  285. this.featureOperationsMock.Verify(f => f.ListWithHttpMessagesAsync(It.IsAny<string>(), null, It.IsAny<CancellationToken>()), Times.Once());
  286. this.featureOperationsMock.Verify(f => f.ListNextWithHttpMessagesAsync(It.IsAny<string>(), null, It.IsAny<CancellationToken>()), Times.Never);
  287. this.commandRuntimeMock.Verify(f => f.WriteObject(It.IsAny<object>(), It.IsAny<bool>()), Times.Once());
  288. this.ResetCalls();
  289. }
  290. /// <summary>
  291. /// Helper class that enables setting the parameter set name
  292. /// </summary>
  293. private class GetAzureProviderFeatureCmdletTest : GetAzureProviderFeatureCmdlet
  294. {
  295. /// <summary>
  296. /// Sets the parameter set name to return
  297. /// </summary>
  298. public string ParameterSetOverride { private get; set; }
  299. /// <summary>
  300. /// Determines the parameter set name based on the <see cref="ParameterSetOverride"/> property
  301. /// </summary>
  302. public override string DetermineParameterSetName()
  303. {
  304. return this.ParameterSetOverride;
  305. }
  306. }
  307. }
  308. }