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 Commands . StorageSync . Interop . Clients ;
16
+ using Commands . StorageSync . Interop . DataObjects ;
17
+ using Commands . StorageSync . Interop . Interfaces ;
18
+ using Microsoft . Azure . Commands . StorageSync . Common ;
19
+ using Microsoft . Azure . Commands . StorageSync . Interfaces ;
20
+ using Microsoft . Azure . Test . HttpRecorder ;
21
+ using Microsoft . Rest . ClientRuntime . Azure . TestFramework ;
22
+ using System ;
23
+ using System . Text . RegularExpressions ;
24
+
25
+ namespace StorageSync . Test . Common
26
+ {
27
+ /// <summary>
28
+ /// Class StorageSyncResourceManager.
29
+ /// Implements the <see cref="Microsoft.Azure.Commands.StorageSync.Common.IStorageSyncResourceManager" />
30
+ /// Implements the <see cref="Microsoft.Azure.Commands.StorageSync.Interfaces.IStorageSyncResourceManager" />
31
+ /// </summary>
32
+ /// <seealso cref="Microsoft.Azure.Commands.StorageSync.Interfaces.IStorageSyncResourceManager" />
33
+ /// <seealso cref="Microsoft.Azure.Commands.StorageSync.Common.IStorageSyncResourceManager" />
34
+ public class MockStorageSyncResourceManager : IStorageSyncResourceManager
35
+ {
36
+
37
+ /// <summary>
38
+ /// Initializes a new instance of the <see cref="MockStorageSyncResourceManager"/> class.
39
+ /// </summary>
40
+ /// <param name="testName">Name of the test.</param>
41
+ public MockStorageSyncResourceManager ( string testName )
42
+ {
43
+ TestName = testName ;
44
+ }
45
+
46
+ /// <summary>
47
+ /// The is playback mode
48
+ /// </summary>
49
+ private bool ? isPlaybackMode ;
50
+
51
+ /// <summary>
52
+ /// Gets a value indicating whether this instance is playback mode.
53
+ /// </summary>
54
+ /// <value><c>true</c> if this instance is playback mode; otherwise, <c>false</c>.</value>
55
+ /// <exception cref="NotSupportedException"></exception>
56
+ protected bool IsPlaybackMode
57
+ {
58
+ get
59
+ {
60
+ if ( ! isPlaybackMode . HasValue )
61
+ {
62
+ if ( Microsoft . Azure . Test . HttpRecorder . HttpMockServer . Mode == Microsoft . Azure . Test . HttpRecorder . HttpRecorderMode . Playback )
63
+ {
64
+ isPlaybackMode = true ;
65
+ }
66
+ else if ( Microsoft . Azure . Test . HttpRecorder . HttpMockServer . Mode == Microsoft . Azure . Test . HttpRecorder . HttpRecorderMode . Record )
67
+ {
68
+ isPlaybackMode = false ;
69
+
70
+ }
71
+ else
72
+ {
73
+ throw new NotSupportedException ( $ "{ Microsoft . Azure . Test . HttpRecorder . HttpMockServer . Mode } Mode is not supported") ;
74
+ }
75
+ }
76
+ return isPlaybackMode . Value ;
77
+ }
78
+ }
79
+
80
+ /// <summary>
81
+ /// Gets the name of the test.
82
+ /// </summary>
83
+ /// <value>The name of the test.</value>
84
+ public string TestName
85
+ {
86
+ get ;
87
+ }
88
+
89
+ /// <summary>
90
+ /// Creates the ecs management.
91
+ /// </summary>
92
+ /// <returns>IEcsManagement.</returns>
93
+ public IEcsManagement CreateEcsManagement ( ) => IsPlaybackMode ? new MockEcsManagementInteropClient ( ) as IEcsManagement : new EcsManagementInteropClient ( ) ;
94
+
95
+ /// <summary>
96
+ /// Gets the unique identifier.
97
+ /// </summary>
98
+ /// <param name="testName">Name of the test.</param>
99
+ /// <returns>Guid.</returns>
100
+ public Guid GetGuid ( string testName ) => Microsoft . Azure . Test . HttpRecorder . HttpMockServer . GetAssetGuid ( testName ) ;
101
+
102
+ /// <summary>
103
+ /// Gets the afs agent installer path.
104
+ /// </summary>
105
+ /// <param name="afsAgentInstallerPath">The afs agent installer path.</param>
106
+ /// <returns>System.String.</returns>
107
+ public bool TryGetAfsAgentInstallerPath ( out string afsAgentInstallerPath )
108
+ {
109
+ afsAgentInstallerPath = @"C:\Program Files\Azure\StorageSyncAgent\" ;
110
+ return true ;
111
+ }
112
+
113
+ /// <summary>
114
+ /// Gets the afs agent version.
115
+ /// </summary>
116
+ /// <param name="afsAgentVersion">The afs agent version.</param>
117
+ /// <returns>System.String.</returns>
118
+ public bool TryGetAfsAgentVersion ( out string afsAgentVersion )
119
+ {
120
+ afsAgentVersion = @"5.0.2.0" ;
121
+ return true ;
122
+ }
123
+
124
+ /// <summary>
125
+ /// Updates the server registration data.
126
+ /// </summary>
127
+ /// <param name="pServerRegistrationData">The p server registration data.</param>
128
+ /// <returns>ServerRegistrationData.</returns>
129
+ public ServerRegistrationData UpdateServerRegistrationData ( ServerRegistrationData pServerRegistrationData )
130
+ {
131
+ pServerRegistrationData . Id = Microsoft . Azure . Test . HttpRecorder . HttpMockServer . GetVariable ( StorageSyncConstants . SyncServerId , pServerRegistrationData . Id ) ;
132
+ pServerRegistrationData . ServerId = Guid . Parse ( Regex . Match ( pServerRegistrationData . Id , @"([^/]+)$" ) . Value ) ;
133
+ return pServerRegistrationData ;
134
+ }
135
+
136
+ /// <summary>
137
+ /// Waits for access propogation.
138
+ /// </summary>
139
+ public void WaitForAccessPropogation ( )
140
+ {
141
+ if ( ! IsPlaybackMode )
142
+ {
143
+ System . Threading . Thread . Sleep ( 40 * 1000 ) ;
144
+ }
145
+ }
146
+
147
+ public string GetTenantId ( )
148
+ {
149
+ string tenantId = null ;
150
+
151
+ if ( IsPlaybackMode )
152
+ {
153
+ if ( HttpMockServer . Variables . ContainsKey ( StorageSyncConstants . TenantId ) )
154
+ {
155
+ tenantId = HttpMockServer . GetVariable ( StorageSyncConstants . TenantId , null ) ;
156
+ }
157
+ }
158
+ return tenantId ;
159
+ }
160
+ }
161
+ }
0 commit comments