1
+ $taskScriptDir = [System.IO.Path ]::GetDirectoryName($PSCommandPath )
2
+ $env: repoRoot = [System.IO.Path ]::GetDirectoryName($taskScriptDir )
3
+ $userPsFileDir = [string ]::Empty
4
+
5
+ [string ]$envVariableName = " TEST_CSM_ORGID_AUTHENTICATION"
6
+
7
+ [CmdletBinding ]
8
+ Function Set-TestEnvironment
9
+ {
10
+ <#
11
+ . SYNOPSIS
12
+ This cmdlet helps you to setup Test Environment for running tests
13
+ In order to successfully run a test, you will need SubscriptionId, TenantId
14
+ This cmdlet will only prompt you for Subscription and Tenant information, rest all other parameters are optional
15
+
16
+ #>
17
+ [CmdletBinding (DefaultParameterSetName = ' UserIdParamSet' )]
18
+ param (
19
+ [Parameter (ParameterSetName = ' UserIdParamSet' , Mandatory = $true , HelpMessage = " UserId (OrgId) you would like to use" )]
20
+ [ValidateNotNullOrEmpty ()]
21
+ [string ]$UserId ,
22
+
23
+ [Parameter (ParameterSetName = ' UserIdParamSet' , Mandatory = $true , HelpMessage = " UserId (OrgId) you would like to use" )]
24
+ [ValidateNotNullOrEmpty ()]
25
+ [string ]$Password ,
26
+
27
+ [Parameter (ParameterSetName = ' SpnParamSet' , Mandatory = $true , HelpMessage = ' ServicePrincipal/ClientId you would like to use' )]
28
+ [ValidateNotNullOrEmpty ()]
29
+ [string ]$ServicePrincipal ,
30
+
31
+ [Parameter (ParameterSetName = ' SpnParamSet' , Mandatory = $true , HelpMessage = ' ServicePrincipal Secret/ClientId Secret you would like to use' )]
32
+ [ValidateNotNullOrEmpty ()]
33
+ [string ]$ServicePrincipalSecret ,
34
+
35
+ [Parameter (ParameterSetName = ' SpnParamSet' , Mandatory = $true )]
36
+ [Parameter (ParameterSetName = ' UserIdParamSet' , Mandatory = $true , HelpMessage = " SubscriptionId you would like to use" )]
37
+ [ValidateNotNullOrEmpty ()]
38
+ [string ]$SubscriptionId ,
39
+
40
+ [Parameter (ParameterSetName = ' SpnParamSet' , Mandatory = $true , HelpMessage = ' AADTenant/TenantId you would like to use' )]
41
+ [ValidateNotNullOrEmpty ()]
42
+ [string ]$TenantId ,
43
+
44
+ [ValidateSet (" Playback" , " Record" , " None" )]
45
+ [string ]$RecordMode = ' Playback' ,
46
+
47
+ [ValidateSet (" Prod" , " Dogfood" , " Current" , " Next" )]
48
+ [string ]$TargetEnvironment = ' Prod'
49
+ )
50
+
51
+ [string ]$uris = " https://management.azure.com/"
52
+
53
+ $formattedConnStr = [string ]::Format(" SubscriptionId={0};HttpRecorderMode={1};Environment={2}" , $SubscriptionId , $RecordMode , $TargetEnvironment )
54
+
55
+ if ([string ]::IsNullOrEmpty($UserId ) -eq $false )
56
+ {
57
+ $formattedConnStr = [string ]::Format([string ]::Concat($formattedConnStr , " ;UserId={0}" ), $UserId )
58
+ }
59
+
60
+ if ([string ]::IsNullOrEmpty($Password ) -eq $false )
61
+ {
62
+ $formattedConnStr = [string ]::Format([string ]::Concat($formattedConnStr , " ;Password={0}" ), $Password )
63
+ }
64
+
65
+ if ([string ]::IsNullOrEmpty($TenantId ) -eq $false )
66
+ {
67
+ $formattedConnStr = [string ]::Format([string ]::Concat($formattedConnStr , " ;AADTenant={0}" ), $TenantId )
68
+ }
69
+
70
+ if ([string ]::IsNullOrEmpty($ServicePrincipal ) -eq $false )
71
+ {
72
+ $formattedConnStr = [string ]::Format([string ]::Concat($formattedConnStr , " ;ServicePrincipal={0}" ), $ServicePrincipal )
73
+ }
74
+
75
+ if ([string ]::IsNullOrEmpty($ServicePrincipalSecret ) -eq $false )
76
+ {
77
+ $formattedConnStr = [string ]::Format([string ]::Concat($formattedConnStr , " ;ServicePrincipalSecret={0}" ), $ServicePrincipalSecret )
78
+ }
79
+
80
+ $formattedConnStr = [string ]::Format([string ]::Concat($formattedConnStr , " ;BaseUri={0}" ), $uris )
81
+
82
+ Write-Host " Below connection string is ready to be set"
83
+ Print- ConnectionString $UserId $Password $SubscriptionId $TenantId $ServicePrincipal $ServicePrincipalSecret $RecordMode $TargetEnvironment $uris
84
+
85
+ # Set connection string to Environment variable
86
+ $env: TEST_CSM_ORGID_AUTHENTICATION = $formattedConnStr
87
+ Write-Host " "
88
+
89
+ # Retrieve the environment variable
90
+ Write-Host " "
91
+ Write-Host " Below connection string was set. Start Visual Studio by typing devenv" - ForegroundColor Green
92
+ [Environment ]::GetEnvironmentVariable($envVariableName )
93
+ Write-Host " "
94
+
95
+ Write-Host " If your needs demand you to set connection string differently, for all the supported Key/Value pairs in connection string"
96
+ Write-Host " Please visit https://github.com/Azure/azure-powershell/blob/dev/documentation/Using-Azure-TestFramework.md" - ForegroundColor Yellow
97
+ }
98
+
99
+ Function Print-ConnectionString ([string ]$uid , [string ]$pwd , [string ]$subId , [string ]$aadTenant , [string ]$spn , [string ]$spnSecret , [string ]$recordMode , [string ]$targetEnvironment , [string ]$uris )
100
+ {
101
+
102
+ if ([string ]::IsNullOrEmpty($uid ) -eq $false )
103
+ {
104
+ Write-Host " UserId=" - ForegroundColor Green - NoNewline
105
+ Write-Host $uid " ;" - NoNewline
106
+ }
107
+
108
+ if ([string ]::IsNullOrEmpty($pwd ) -eq $false )
109
+ {
110
+ Write-Host " Password=" - ForegroundColor Green - NoNewline
111
+ Write-Host $pwd " ;" - NoNewline
112
+ }
113
+
114
+ if ([string ]::IsNullOrEmpty($subId ) -eq $false )
115
+ {
116
+ Write-Host " SubscriptionId=" - ForegroundColor Green - NoNewline
117
+ Write-Host $subId " ;" - NoNewline
118
+ }
119
+
120
+ if ([string ]::IsNullOrEmpty($aadTenant ) -eq $false )
121
+ {
122
+ Write-Host " AADTenant=" - ForegroundColor Green - NoNewline
123
+ Write-Host $aadTenant " ;" - NoNewline
124
+ }
125
+
126
+ if ([string ]::IsNullOrEmpty($spn ) -eq $false )
127
+ {
128
+ Write-Host " ServicePrincipal=" - ForegroundColor Green - NoNewline
129
+ Write-Host $spn " ;" - NoNewline
130
+ }
131
+
132
+ if ([string ]::IsNullOrEmpty($spnSecret ) -eq $false )
133
+ {
134
+ Write-Host " ServicePrincipalSecret=" - ForegroundColor Green - NoNewline
135
+ Write-Host $spnSecret " ;" - NoNewline
136
+ }
137
+
138
+ if ([string ]::IsNullOrEmpty($recordMode ) -eq $false )
139
+ {
140
+ Write-Host " HttpRecorderMode=" - ForegroundColor Green - NoNewline
141
+ Write-Host $recordMode " ;" - NoNewline
142
+ }
143
+
144
+ if ([string ]::IsNullOrEmpty($targetEnvironment ) -eq $false )
145
+ {
146
+ Write-Host " Environment=" - ForegroundColor Green - NoNewline
147
+ Write-Host $targetEnvironment " ;" - NoNewline
148
+ }
149
+
150
+ if ([string ]::IsNullOrEmpty($uris ) -eq $false )
151
+ {
152
+ Write-Host " BaseUri=" - ForegroundColor Green - NoNewline
153
+ Write-Host $uris - NoNewline
154
+ }
155
+
156
+ Write-Host " "
157
+ }
158
+
159
+ [CmdletBinding ]
160
+ Function Get-BuildScopes
161
+ {
162
+ <#
163
+ . SYNOPSIS
164
+ You can build a particular package rather than doing a full build by providing Build Scope.
165
+ This cmdlet will help to identify existing Scope available
166
+ This will enable to execute Start-RepoBuild <scope>
167
+
168
+ #>
169
+
170
+ Write-Host " Below are available scopes you can specify for building specific projects"
171
+ Write-Host " "
172
+ Get-ChildItem - path " $env: repoRoot \src\ResourceManager" - dir | Format-Wide - Column 5 | Format-Table - Property Name
173
+ Get-ChildItem - path " $env: repoRoot \src\ServiceManagement" - dir | Format-Wide - Column 5 | Format-Table - Property Name
174
+ }
175
+
176
+ [CmdletBinding ]
177
+ Function Start-Build
178
+ {
179
+ <#
180
+ . SYNOPSIS
181
+ This cmdlet will help to do either with full build or targeted build for specific scopes.
182
+
183
+ . PARAMETER BuildScope
184
+ Use Get-BuildScope cmdLet to get list of existing scopes that can be used to build
185
+ #>
186
+ param (
187
+ [parameter (Mandatory = $false , Position = 0 , HelpMessage = ' BuildScope that you would like to use. For list of build scopes, run List-BuildScopes' )]
188
+ [string ]$BuildScope
189
+ )
190
+
191
+ if ([string ]::IsNullOrEmpty($BuildScope ) -eq $true )
192
+ {
193
+ Write-Host " Starting Full build"
194
+ msbuild.exe " $env: repoRoot \build.proj" / t:Build
195
+ }
196
+ else
197
+ {
198
+ Write-Host " Building $BuildScope "
199
+ msbuild.exe " $env: repoRoot \build.proj" / t:Build / p:Scope= $BuildScope
200
+ }
201
+ }
202
+
203
+ [CmdletBinding ]
204
+ Function Invoke-CheckinTests
205
+ {
206
+ <#
207
+ . SYNOPSIS
208
+ Runs all the check in tests
209
+ #>
210
+ Write-Host " cmdline Args: msbuild.exe $env: repoRoot \build.proj /t:Test"
211
+ msbuild.exe " $env: repoRoot \build.proj" / t:Test
212
+ }
213
+
214
+ <#
215
+ We allow users to include any helper powershell scripts they would like to include in the current session
216
+ Currently we support two ways to include helper powershell scripts
217
+ 1) psuserspreferences environment variable
218
+ 2) $env:USERPROFILE\psFiles directory
219
+ We will include all *.ps1 files from any of the above mentioned locations
220
+ #>
221
+ if ([System.IO.Directory ]::Exists($env: psuserpreferences ))
222
+ {
223
+ $userPsFileDir = $env: psuserpreferences
224
+ }
225
+ elseif ([System.IO.Directory ]::Exists(" $env: USERPROFILE \psFiles" ))
226
+ {
227
+ $userPsFileDir = " $env: USERPROFILE \psFiles"
228
+ }
229
+
230
+ if ([string ]::IsNullOrEmpty($userPsFileDir ) -eq $false )
231
+ {
232
+ Get-ChildItem $userPsFileDir | WHERE {$_.Name -like " *.ps1" } | ForEach {
233
+ Write-Host " Including $_ " - ForegroundColor Green
234
+ . $userPsFileDir \$_
235
+ }
236
+ }
237
+ else
238
+ {
239
+ Write-Host " Loading skipped. 'psuserpreferences' environment variable was not set to load user preferences." - ForegroundColor DarkYellow
240
+ }
241
+
242
+ export-modulemember - Function Set-TestEnvironment
243
+ export-modulemember - Function Get-BuildScopes
244
+ export-modulemember - Function Start-Build
245
+ export-modulemember - Function Invoke-CheckinTests
0 commit comments