|
12 | 12 | // limitations under the License.
|
13 | 13 | // ----------------------------------------------------------------------------------
|
14 | 14 |
|
15 |
| -using Microsoft.Azure.Management.KeyVault; |
16 |
| -using Microsoft.Azure.Management.KeyVault.Models; |
17 |
| -using Microsoft.Azure.Test.HttpRecorder; |
18 |
| -using Microsoft.Rest.ClientRuntime.Azure.TestFramework; |
19 | 15 | using Microsoft.WindowsAzure.Commands.Test.Utilities.Common;
|
20 | 16 | using System;
|
21 |
| -using System.Collections.Generic; |
22 |
| -using System.Diagnostics; |
23 |
| -using System.Linq; |
24 |
| -using Microsoft.Azure.Management.Internal.Resources; |
25 |
| -using Microsoft.Azure.Management.Internal.Resources.Models; |
26 |
| -using Sku = Microsoft.Azure.Management.KeyVault.Models.Sku; |
27 | 17 |
|
28 | 18 | namespace Microsoft.Azure.Commands.KeyVault.Test.ScenarioTests
|
29 | 19 | {
|
30 | 20 | public class KeyVaultTestFixture : RMTestBase, IDisposable
|
31 | 21 | {
|
32 |
| - private readonly HttpRecorderMode _mode; |
33 |
| - |
34 | 22 | public string TagName { get; set; } = "testtag";
|
35 | 23 | public string TagValue { get; set; } = "testvalue";
|
36 | 24 |
|
37 | 25 | public string ResourceGroupName { get; set; }
|
38 | 26 | public string Location { get; set; }
|
39 | 27 | public string PreCreatedVault { get; set; }
|
40 | 28 |
|
41 |
| - private bool _initialized; |
42 | 29 | public KeyVaultTestFixture()
|
43 | 30 | {
|
44 |
| - // Initialize has bug which causes null reference exception |
45 |
| - HttpMockServer.FileSystemUtilsObject = new FileSystemUtils(); |
46 |
| - _mode = HttpMockServer.GetCurrentMode(); |
47 | 31 | }
|
48 | 32 |
|
49 | 33 | public void Initialize(string className)
|
50 | 34 | {
|
51 |
| - if (_initialized) |
52 |
| - return; |
53 |
| - |
54 |
| - if (_mode == HttpRecorderMode.Record) |
55 |
| - { |
56 |
| - using (MockContext context = MockContext.Start(new StackTrace().GetFrame(1).GetMethod().ReflectedType?.ToString(), new StackTrace().GetFrame(1).GetMethod().Name)) |
57 |
| - { |
58 |
| - var resourcesClient = context.GetServiceClient<ResourceManagementClient>(TestEnvironmentFactory.GetTestEnvironment()); |
59 |
| - var mgmtClient = context.GetServiceClient<KeyVaultManagementClient>(TestEnvironmentFactory.GetTestEnvironment()); |
60 |
| - var tenantId = TestEnvironmentFactory.GetTestEnvironment().Tenant; |
61 |
| - |
62 |
| - //Figure out which locations are available for Key Vault |
63 |
| - Location = GetKeyVaultLocation(resourcesClient); |
64 |
| - |
65 |
| - //Create a resource group in that location |
66 |
| - PreCreatedVault = TestUtilities.GenerateName("pshtestvault"); |
67 |
| - ResourceGroupName = TestUtilities.GenerateName("pshtestrg"); |
68 |
| - |
69 |
| - resourcesClient.ResourceGroups.CreateOrUpdate(ResourceGroupName, new ResourceGroup { Location = Location }); |
70 |
| - CreateVault(mgmtClient, Location, tenantId); |
71 |
| - } |
72 |
| - } |
73 |
| - |
74 |
| - _initialized = true; |
75 |
| - } |
76 |
| - |
77 |
| - private static string GetKeyVaultLocation(ResourceManagementClient resourcesClient) |
78 |
| - { |
79 |
| - var provider = resourcesClient.Providers.Get("Microsoft.KeyVault"); |
80 |
| - var location = provider.ResourceTypes.First(resType => resType.ResourceType.Contains("vaults")).Locations.FirstOrDefault(); |
81 |
| - return location?.ToLowerInvariant().Replace(" ", ""); |
| 35 | + // no op |
82 | 36 | }
|
83 | 37 |
|
84 |
| - private void CreateVault(KeyVaultManagementClient mgmtClient, string location, string tenantId) |
85 |
| - { |
86 |
| - mgmtClient.Vaults.CreateOrUpdate( |
87 |
| - ResourceGroupName, |
88 |
| - PreCreatedVault, |
89 |
| - new VaultCreateOrUpdateParameters |
90 |
| - { |
91 |
| - Location = location, |
92 |
| - Tags = new Dictionary<string, string> { { TagName, TagValue } }, |
93 |
| - Properties = new VaultProperties |
94 |
| - { |
95 |
| - EnabledForDeployment = false, |
96 |
| - Sku = new Sku { Name = SkuName.Premium }, |
97 |
| - TenantId = Guid.Parse(tenantId), |
98 |
| - VaultUri = "", |
99 |
| - AccessPolicies = new AccessPolicyEntry[]{ } |
100 |
| - } |
101 |
| - }); |
102 |
| - } |
103 |
| - |
104 |
| - public void ResetPreCreatedVault() |
105 |
| - { |
106 |
| - if (_mode == HttpRecorderMode.Record) |
107 |
| - { |
108 |
| - using (MockContext context = MockContext.Start(new StackTrace().GetFrame(1).GetMethod().ReflectedType?.ToString(), new StackTrace().GetFrame(1).GetMethod().Name)) |
109 |
| - { |
110 |
| - var mgmtClient = context.GetServiceClient<KeyVaultManagementClient>(TestEnvironmentFactory.GetTestEnvironment()); |
111 |
| - var tenantId = Guid.Parse(TestEnvironmentFactory.GetTestEnvironment().Tenant); |
112 |
| - |
113 |
| - var policies = new AccessPolicyEntry[] { }; |
114 |
| - |
115 |
| - mgmtClient.Vaults.CreateOrUpdate( |
116 |
| - ResourceGroupName, |
117 |
| - PreCreatedVault, |
118 |
| - new VaultCreateOrUpdateParameters |
119 |
| - { |
120 |
| - Location = Location, |
121 |
| - Tags = new Dictionary<string, string> { { TagName, TagValue } }, |
122 |
| - Properties = new VaultProperties |
123 |
| - { |
124 |
| - EnabledForDeployment = false, |
125 |
| - Sku = new Sku { Name = SkuName.Premium }, |
126 |
| - TenantId = tenantId, |
127 |
| - VaultUri = "", |
128 |
| - AccessPolicies = policies |
129 |
| - } |
130 |
| - }); |
131 |
| - } |
132 |
| - } |
133 |
| - } |
134 | 38 | public void Dispose()
|
135 | 39 | {
|
136 |
| - Dispose(false); |
137 | 40 | GC.SuppressFinalize(this);
|
138 | 41 | }
|
139 |
| - |
140 |
| - protected virtual void Dispose(bool disposing) |
141 |
| - { |
142 |
| - if (disposing) |
143 |
| - { |
144 |
| - if (_mode == HttpRecorderMode.Record && _initialized) |
145 |
| - { |
146 |
| - using (MockContext context = MockContext.Start(new StackTrace().GetFrame(1).GetMethod().ReflectedType?.ToString(), new StackTrace().GetFrame(1).GetMethod().Name)) |
147 |
| - { |
148 |
| - var resourcesClient = context.GetServiceClient<ResourceManagementClient>(TestEnvironmentFactory.GetTestEnvironment()); |
149 |
| - resourcesClient.ResourceGroups.Delete(ResourceGroupName); |
150 |
| - } |
151 |
| - } |
152 |
| - } |
153 |
| - } |
154 | 42 | }
|
155 | 43 | }
|
0 commit comments