forked from microsoft/navcontainerhelper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Get-TestsFromNavContainer.ps1
282 lines (255 loc) · 13.5 KB
/
Get-TestsFromNavContainer.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
<#
.Synopsis
Get test information from a NAV/BC Container
.Description
.Parameter containerName
Name of the container from which you want to get test information
.Parameter tenant
tenant to use if container is multitenant
.Parameter companyName
company to use
.Parameter profile
profile to use
.Parameter credential
Credentials of the SUPER user if using NavUserPassword authentication
.Parameter sqlcredential
SQL Credential if using an external sql server
.Parameter accesstoken
If your container is running AAD authentication, you need to specify an accesstoken for the user specified in credential
.Parameter testSuite
Name of test suite to get. Default is DEFAULT.
.Parameter testCodeunit
Name or ID of test codeunit to get. Wildcards (? and *) are supported. Default is *.
This parameter will not populate the test suite with the specified codeunit. This is used as a filter on the tests that are already present
(or otherwise loaded) in the suite.
This is not to be confused with -testCodeunitRange.
.Parameter testCodeunitRange
A BC-compatible filter string to use for loading test codeunits (similar to -extensionId). This is not to be confused with -testCodeunit.
If you set this parameter to '*', all test codeunits will be loaded.
This might not work on all versions of BC and only works when using the command-line-testtool.
.Parameter testPage
ID of the test page to use. Default for 15.x containers is 130455. Default for 14.x containers and earlier is 130409.
.Parameter culture
Set the culture when running the tests. Default is en-US. Microsoft tests are written for en-US.
.Parameter timezone
Set the timezone when running the tests. Default is current timezone.
.Parameter debugMode
Include this switch to output debug information if getting the tests fails.
.Parameter ignoreGroups
Test Groups are not supported in 15.x - include this switch to ignore test groups in 14.x and earlier and have compatible resultsets from this function
.Parameter usePublicWebBaseUrl
Connect to the public Url and not to localhost
.Example
Get-TestsFromBcContainer -containerName test -credential $credential
.Example
Get-TestsFromBcContainer -containerName $containername -credential $credential -TestSuite "MYTESTS" -TestCodeunit "134001"
#>
function Get-TestsFromBcContainer {
Param (
[string] $containerName = $bcContainerHelperConfig.defaultContainerName,
[Parameter(Mandatory=$false)]
[string] $tenant = "default",
[Parameter(Mandatory=$false)]
[string] $companyName = "",
[Parameter(Mandatory=$false)]
[string] $profile = "",
[Parameter(Mandatory=$false)]
[PSCredential] $credential = $null,
[Parameter(Mandatory=$false)]
[PSCredential] $sqlCredential = $credential,
[Parameter(Mandatory=$false)]
[string] $accessToken = "",
[Parameter(Mandatory=$false)]
[string] $testSuite = "DEFAULT",
[Parameter(Mandatory=$false)]
[string] $testCodeunit = "*",
[Parameter(Mandatory=$false)]
[string] $testCodeunitRange = "*",
[string] $extensionId = "",
[array] $disabledTests = @(),
[Parameter(Mandatory=$false)]
[int] $testPage,
[string] $culture = "en-US",
[string] $timezone = "",
[switch] $debugMode = $bcContainerHelperConfig.debugMode,
[switch] $ignoreGroups,
[switch] $usePublicWebBaseUrl,
[string] $useUrl,
[Hashtable] $bcAuthContext
)
$telemetryScope = InitTelemetryScope -name $MyInvocation.InvocationName -parameterValues $PSBoundParameters -includeParameters @()
try {
if ($bcAuthContext) {
$bcAuthContext = Renew-BcAuthContext $bcAuthContext
$accessToken = $bcAuthContext.accessToken
$credential = New-Object pscredential -ArgumentList $bcAuthContext.upn, (ConvertTo-SecureString -String $accessToken -AsPlainText -Force)
}
$navversion = Get-BcContainerNavversion -containerOrImageName $containerName
$version = [System.Version]($navversion.split('-')[0])
if (!($PSBoundParameters.ContainsKey('usePublicWebBaseUrl'))) {
$inspect = docker inspect $containerName | ConvertFrom-Json
if ($inspect.Config.Labels.psobject.Properties.Match('traefik.enable').Count -gt 0) {
if ($inspect.config.Labels.'traefik.enable' -eq "true") {
$usePublicWebBaseUrl = ($useUrl -eq "")
}
}
}
$PsTestToolFolder = Join-Path $bcContainerHelperConfig.hostHelperFolder "Extensions\$containerName\PsTestTool"
$PsTestFunctionsPath = Join-Path $PsTestToolFolder "PsTestFunctions.ps1"
$ClientContextPath = Join-Path $PsTestToolFolder "ClientContext.ps1"
$fobfile = Join-Path $PsTestToolFolder "PSTestToolPage.fob"
$serverConfiguration = Get-BcContainerServerConfiguration -ContainerName $containerName
$clientServicesCredentialType = $serverConfiguration.ClientServicesCredentialType
if ($usePublicWebBaseUrl -and $useUrl -ne "") {
throw "You cannot specify usePublicWebBaseUrl and useUrl at the same time"
}
if ($serverConfiguration.PublicWebBaseUrl -eq "") {
throw "Container $containerName needs to include the WebClient in order to get tests (PublicWebBaseUrl is blank)"
}
if (!$testPage) {
if ($version.Major -ge 15) {
$testPage = 130455
}
else {
$testPage = 130409
}
}
If (!(Test-Path -Path $PsTestToolFolder -PathType Container)) {
try {
New-Item -Path $PsTestToolFolder -ItemType Directory | Out-Null
Copy-Item -Path (Join-Path $PSScriptRoot "PsTestFunctions.ps1") -Destination $PsTestFunctionsPath -Force
Copy-Item -Path (Join-Path $PSScriptRoot "ClientContext.ps1") -Destination $ClientContextPath -Force
if ($version.Major -ge 15) {
if ($testPage -eq 130409) {
Publish-BcContainerApp -containerName $containerName -appFile (Join-Path $PSScriptRoot "Microsoft_PSTestToolPage_15.0.0.0.app") -skipVerification -sync -install
}
}
else {
if ($version.Major -lt 11) {
Copy-Item -Path (Join-Path $PSScriptRoot "PSTestToolPage$($version.Major).fob") -Destination $fobfile -Force
}
else {
Copy-Item -Path (Join-Path $PSScriptRoot "PSTestToolPage.fob") -Destination $fobfile -Force
}
if ($clientServicesCredentialType -eq "Windows") {
Import-ObjectsToNavContainer -containerName $containerName -objectsFile $fobfile
} else {
Import-ObjectsToNavContainer -containerName $containerName -objectsFile $fobfile -sqlCredential $sqlCredential
}
}
} catch {
Remove-Item -Path $PsTestToolFolder -Recurse -Force
throw
}
}
if ($clientServicesCredentialType -eq "Windows" -and "$CompanyName" -eq "") {
$myName = $myUserName.SubString($myUserName.IndexOf('\')+1)
Get-BcContainerBcUser -containerName $containerName | Where-Object { $_.UserName.EndsWith("\$MyName", [System.StringComparison]::InvariantCultureIgnoreCase) -or $_.UserName -eq $myName } | % {
$companyName = $_.Company
}
}
Invoke-ScriptInBCContainer -containerName $containerName -scriptBlock { Param($timeoutStr)
$webConfigFile = "C:\inetpub\wwwroot\$WebServerInstance\web.config"
try {
$webConfig = [xml](Get-Content $webConfigFile)
$node = $webConfig.configuration.'system.webServer'.aspNetCore.Attributes.GetNamedItem('requestTimeout')
if (!($node)) {
$node = $webConfig.configuration.'system.webServer'.aspNetCore.Attributes.Append($webConfig.CreateAttribute('requestTimeout'))
}
if ($node.Value -ne $timeoutStr) {
$node.Value = $timeoutStr
$webConfig.Save($webConfigFile)
}
}
catch {
Write-Host "WARNING: could not set requestTimeout in web.config"
}
} -argumentList "01:00:00"
$result = Invoke-ScriptInBcContainer -containerName $containerName { Param([string] $tenant, [string] $companyName, [string] $profile, [pscredential] $credential, [string] $accessToken, [string] $testSuite, [string] $testCodeunit, [string] $PsTestFunctionsPath, [string] $ClientContextPath, $testPage, $version, $culture, $timezone, $debugMode, $ignoreGroups, $usePublicWebBaseUrl, $useUrl, $extensionId, $disabledtests)
$newtonSoftDllPath = "C:\Program Files\Microsoft Dynamics NAV\*\Service\Management\NewtonSoft.json.dll"
if (!(Test-Path $newtonSoftDllPath)) {
$newtonSoftDllPath = "C:\Program Files\Microsoft Dynamics NAV\*\Service\NewtonSoft.json.dll"
}
$newtonSoftDllPath = (Get-Item $newtonSoftDllPath).FullName
$clientDllPath = "C:\Test Assemblies\Microsoft.Dynamics.Framework.UI.Client.dll"
$customConfigFile = Join-Path (Get-Item "C:\Program Files\Microsoft Dynamics NAV\*\Service").FullName "CustomSettings.config"
[xml]$customConfig = [System.IO.File]::ReadAllText($customConfigFile)
$publicWebBaseUrl = $customConfig.SelectSingleNode("//appSettings/add[@key='PublicWebBaseUrl']").Value.TrimEnd('/')
$clientServicesCredentialType = $customConfig.SelectSingleNode("//appSettings/add[@key='ClientServicesCredentialType']").Value
if ($useUrl) {
$serviceUrl = "$($useUrl.TrimEnd('/'))/cs?tenant=$tenant"
}
elseif ($usePublicWebBaseUrl) {
$serviceUrl = "$publicWebBaseUrl/cs?tenant=$tenant"
}
else {
$uri = [Uri]::new($publicWebBaseUrl)
$serviceUrl = "$($Uri.Scheme)://localhost:$($Uri.Port)/$($Uri.PathAndQuery)/cs?tenant=$tenant"
}
if ($clientServicesCredentialType -eq "Windows") {
$windowsUserName = whoami
if (!(Get-NAVServerUser -ServerInstance $ServerInstance -tenant $tenant -ErrorAction Ignore | Where-Object { $_.UserName -eq $windowsusername })) {
Write-Host "Creating $windowsusername as user"
New-NavServerUser -ServerInstance $ServerInstance -tenant $tenant -WindowsAccount $windowsusername
New-NavServerUserPermissionSet -ServerInstance $ServerInstance -tenant $tenant -WindowsAccount $windowsusername -PermissionSetId SUPER
}
}
elseif ($accessToken) {
$clientServicesCredentialType = "AAD"
$credential = New-Object pscredential $credential.UserName, (ConvertTo-SecureString -String $accessToken -AsPlainText -Force)
}
if ($companyName) {
$serviceUrl += "&company=$([Uri]::EscapeDataString($companyName))"
}
if ($profile) {
$serviceUrl += "&profile=$([Uri]::EscapeDataString($profile))"
}
. $PsTestFunctionsPath -newtonSoftDllPath $newtonSoftDllPath -clientDllPath $clientDllPath -clientContextScriptPath $ClientContextPath
$clientContext = $null
try {
Disable-SslVerification
$clientContext = New-ClientContext -serviceUrl $serviceUrl -auth $clientServicesCredentialType -credential $credential -culture $culture -timezone $timezone -debugMode:$debugMode
Get-Tests -clientContext $clientContext `
-TestSuite $testSuite `
-TestCodeunit $testCodeunit `
-ExtensionId $extensionId `
-DisabledTests $disabledtests `
-testPage $testPage `
-debugMode:$debugMode `
-ignoreGroups:$ignoreGroups `
-connectFromHost:$connectFromHost
}
catch {
if ($debugMode -and $clientContext) {
Dump-ClientContext -clientcontext $clientContext
}
throw
}
finally {
Enable-SslVerification
if ($clientContext) {
Remove-ClientContext -clientContext $clientContext
$clientContext = $null
}
}
} -argumentList $tenant, $companyName, $profile, $credential, $accessToken, $testSuite, $testCodeunit, (Get-BCContainerPath -containerName $containerName -path $PsTestFunctionsPath), (Get-BCContainerPath -containerName $containerName -path $ClientContextPath), $testPage, $version, $culture, $timezone, $debugMode, $ignoreGroups, $usePublicWebBaseUrl, $useUrl, $extensionId, $disabledtests
# When Invoke-ScriptInContainer is running as non-administrator - Write-Host (like license warnings) are send to the output
# If the output is an array - grab the last item.
if ($result -is [array]) {
0..($result.Count-2) | % { Write-Host $result[$_] }
$result[$result.Count-1] | ConvertFrom-Json
}
else {
$result | ConvertFrom-Json
}
}
catch {
TrackException -telemetryScope $telemetryScope -errorRecord $_
throw
}
finally {
TrackTrace -telemetryScope $telemetryScope
}
}
Set-Alias -Name Get-TestsFromNavContainer -Value Get-TestsFromBcContainer
Export-ModuleMember -Function Get-TestsFromBcContainer -Alias Get-TestsFromNavContainer