1 # WARNING WARNING WARNING - DO NOT EDIT THIS FILE IN JENKINS DIRECTLY.
2 # SUBMIT A PR TO https://github.com/jhowardmsft/docker-w2wCIScripts/blob/master/runCI/executeCI.ps1,
3 # AND MAKE SURE https://github.com/jhowardmsft/docker-w2wCIScripts/blob/master/runCI/Invoke-DockerCI.ps1
4 # ISN'T BROKEN!!!!!!! VALIDATE USING A TEST CONTEXT IN JENKINS. THEN COPY/PASTE INTO JENKINS PRODUCTION.
5 #
6 # Jenkins CI scripts for Windows to Windows CI (Powershell Version)
7 # By John Howard (@jhowardmsft) January 2016 - bash version; July 2016 Ported to PowerShell
8 
9 $ErrorActionPreference = 'Stop'
10 $StartTime=Get-Date
11 
12 # Put up top to be blindingly obvious. The production jenkins.dockerproject.org Linux-container
13 # CI job is "Docker-PRs-LoW-RS3". Force into LCOW mode for this run, or not.
14 if ($env:BUILD_TAG -match "-LoW") { $env:LCOW_MODE=1 }
15 if ($env:BUILD_TAG -match "-WoW") { $env:LCOW_MODE="" }
16 
17 
18 # -------------------------------------------------------------------------------------------
19 # When executed, we rely on four variables being set in the environment:
20 #
21 # [The reason for being environment variables rather than parameters is historical. No reason
22 # why it couldn't be updated.]
23 #
24 #    SOURCES_DRIVE       is the drive on which the sources being tested are cloned from.
25 #                        This should be a straight drive letter, no platform semantics.
26 #                        For example 'c'
27 #
28 #    SOURCES_SUBDIR      is the top level directory under SOURCES_DRIVE where the
29 #                        sources are cloned to. There are no platform semantics in this
30 #                        as it does not include slashes.
31 #                        For example 'gopath'
32 #
33 #                        Based on the above examples, it would be expected that Jenkins
34 #                        would clone the sources being tested to
35 #                        SOURCES_DRIVE\SOURCES_SUBDIR\src\github.com\docker\docker, or
36 #                        c:\gopath\src\github.com\docker\docker
37 #
38 #    TESTRUN_DRIVE       is the drive where we build the binary on and redirect everything
39 #                        to for the daemon under test. On an Azure D2 type host which has
40 #                        an SSD temporary storage D: drive, this is ideal for performance.
41 #                        For example 'd'
42 #
43 #    TESTRUN_SUBDIR      is the top level directory under TESTRUN_DRIVE where we redirect
44 #                        everything to for the daemon under test. For example 'CI'.
45 #                        Hence, the daemon under test is run under
46 #                        TESTRUN_DRIVE\TESTRUN_SUBDIR\CI-<CommitID> or
47 #                        d:\CI\CI-<CommitID>
48 #
49 # In addition, the following variables can control the run configuration:
50 #
51 #    DOCKER_DUT_DEBUG         if defined starts the daemon under test in debug mode.
52 #
53 #    SKIP_VALIDATION_TESTS    if defined skips the validation tests
54 #
55 #    SKIP_UNIT_TESTS          if defined skips the unit tests
56 #
57 #    SKIP_INTEGRATION_TESTS   if defined skips the integration tests
58 #
59 #    SKIP_COPY_GO             if defined skips copy the go installer from the image
60 #
61 #    DOCKER_DUT_HYPERV        if default daemon under test default isolation is hyperv
62 #
63 #    INTEGRATION_TEST_NAME    to only run partial tests eg "TestInfo*" will only run
64 #                             any tests starting "TestInfo"
65 #
66 #    SKIP_BINARY_BUILD        if defined skips building the binary
67 #
68 #    SKIP_ZAP_DUT             if defined doesn't zap the daemon under test directory
69 #
70 #    SKIP_IMAGE_BUILD         if defined doesn't build the 'docker' image
71 #
72 #    INTEGRATION_IN_CONTAINER if defined, runs the integration tests from inside a container.
73 #                             As of July 2016, there are known issues with this.
74 #
75 #    SKIP_ALL_CLEANUP         if defined, skips any cleanup at the start or end of the run
76 #
77 #    WINDOWS_BASE_IMAGE       if defined, uses that as the base image. Note that the
78 #                             docker integration tests are also coded to use the same
79 #                             environment variable, and if no set, defaults to microsoft/windowsservercore
80 #
81 #    LCOW_BASIC_MODE          if defined, does very basic LCOW verification. Ultimately we
82 #                             want to run the entire CI suite from docker, but that's a way off.
83 #
84 #    LCOW_MODE                if defined, runs the entire CI suite
85 #
86 # -------------------------------------------------------------------------------------------
87 #
88 # Jenkins Integration. Add a Windows Powershell build step as follows:
89 #
90 #    Write-Host -ForegroundColor green "INFO: Jenkins build step starting"
91 #    $CISCRIPT_DEFAULT_LOCATION = "https://raw.githubusercontent.com/jhowardmsft/docker-w2wCIScripts/master/runCI/executeCI.ps1"
92 #    $CISCRIPT_LOCAL_LOCATION = "$env:TEMP\executeCI.ps1"
93 #    Write-Host -ForegroundColor green "INFO: Removing cached execution script"
94 #    Remove-Item $CISCRIPT_LOCAL_LOCATION -Force -ErrorAction SilentlyContinue 2>&1 | Out-Null
95 #    $wc = New-Object net.webclient
96 #    try {
97 #        Write-Host -ForegroundColor green "INFO: Downloading latest execution script..."
98 #        $wc.Downloadfile($CISCRIPT_DEFAULT_LOCATION, $CISCRIPT_LOCAL_LOCATION)
99 #    }
100 #    catch [System.Net.WebException]
101 #    {
102 #        Throw ("Failed to download: $_")
103 #    }
104 #    & $CISCRIPT_LOCAL_LOCATION
105 # -------------------------------------------------------------------------------------------
106 
107 
108 $SCRIPT_VER="05-Feb-2019 09:03 PDT"
109 $FinallyColour="Cyan"
110 
111 #$env:DOCKER_DUT_DEBUG="yes" # Comment out to not be in debug mode
112 #$env:SKIP_UNIT_TESTS="yes"
113 #$env:SKIP_VALIDATION_TESTS="yes"
114 #$env:SKIP_ZAP_DUT=""
115 #$env:SKIP_BINARY_BUILD="yes"
116 #$env:INTEGRATION_TEST_NAME=""
117 #$env:SKIP_IMAGE_BUILD="yes"
118 #$env:SKIP_ALL_CLEANUP="yes"
119 #$env:INTEGRATION_IN_CONTAINER="yes"
120 #$env:WINDOWS_BASE_IMAGE=""
121 #$env:SKIP_COPY_GO="yes"
122 
Nuke-Everythingnull123 Function Nuke-Everything {
124     $ErrorActionPreference = 'SilentlyContinue'
125 
126     try {
127 
128         if ($null -eq $env:SKIP_ALL_CLEANUP) {
129             Write-Host -ForegroundColor green "INFO: Nuke-Everything..."
130             $containerCount = ($(docker ps -aq | Measure-Object -line).Lines)
131             if (-not $LastExitCode -eq 0) {
132                 Throw "ERROR: Failed to get container count from control daemon while nuking"
133             }
134 
135             Write-Host -ForegroundColor green "INFO: Container count on control daemon to delete is $containerCount"
136             if ($(docker ps -aq | Measure-Object -line).Lines -gt 0) {
137                 docker rm -f $(docker ps -aq)
138             }
139 
140             $allImages  = $(docker images --format "{{.Repository}}#{{.ID}}")
141             $toRemove   = ($allImages | Select-String -NotMatch "windowsservercore","nanoserver","docker")
142             $imageCount = ($toRemove | Measure-Object -line).Lines
143 
144             if ($imageCount -gt 0) {
145                 Write-Host -Foregroundcolor green "INFO: Non-base image count on control daemon to delete is $imageCount"
146                 docker rmi -f ($toRemove | Foreach-Object { $_.ToString().Split("#")[1] })
147             }
148         } else {
149             Write-Host -ForegroundColor Magenta "WARN: Skipping cleanup of images and containers"
150         }
151 
152         # Kill any spurious daemons. The '-' is IMPORTANT otherwise will kill the control daemon!
153         $pids=$(get-process | where-object {$_.ProcessName -like 'dockerd-*'}).id
154         foreach ($p in $pids) {
155             Write-Host "INFO: Killing daemon with PID $p"
156             Stop-Process -Id $p -Force -ErrorAction SilentlyContinue
157         }
158 
159         if ($null -ne $pidFile) {
160             Write-Host "INFO: Tidying pidfile $pidfile"
161              if (Test-Path $pidFile) {
162                 $p=Get-Content $pidFile -raw
163                 if ($null -ne $p){
164                     Write-Host -ForegroundColor green "INFO: Stopping possible daemon pid $p"
165                     taskkill -f -t -pid $p
166                 }
167                 Remove-Item "$env:TEMP\docker.pid" -force -ErrorAction SilentlyContinue
168             }
169         }
170 
171         Stop-Process -name "cc1" -Force -ErrorAction SilentlyContinue 2>&1 | Out-Null
172         Stop-Process -name "link" -Force -ErrorAction SilentlyContinue 2>&1 | Out-Null
173         Stop-Process -name "compile" -Force -ErrorAction SilentlyContinue 2>&1 | Out-Null
174         Stop-Process -name "ld" -Force -ErrorAction SilentlyContinue 2>&1 | Out-Null
175         Stop-Process -name "go" -Force -ErrorAction SilentlyContinue 2>&1 | Out-Null
176         Stop-Process -name "git" -Force -ErrorAction SilentlyContinue 2>&1 | Out-Null
177         Stop-Process -name "git-remote-https" -Force -ErrorAction SilentlyContinue 2>&1 | Out-Null
178         Stop-Process -name "integration-cli.test" -Force -ErrorAction SilentlyContinue 2>&1 | Out-Null
179         Stop-Process -name "tail" -Force -ErrorAction SilentlyContinue 2>&1 | Out-Null
180 
181         # Detach any VHDs
182         gwmi msvm_mountedstorageimage -namespace root/virtualization/v2 -ErrorAction SilentlyContinue | foreach-object {$_.DetachVirtualHardDisk() }
183 
184         # Stop any compute processes
185         Get-ComputeProcess | Stop-ComputeProcess -Force
186 
187         # Delete the directory using our dangerous utility unless told not to
188         if (Test-Path "$env:TESTRUN_DRIVE`:\$env:TESTRUN_SUBDIR") {
189             if (($null -ne $env:SKIP_ZAP_DUT) -or ($null -eq $env:SKIP_ALL_CLEANUP)) {
190                 Write-Host -ForegroundColor Green "INFO: Nuking $env:TESTRUN_DRIVE`:\$env:TESTRUN_SUBDIR"
191                 docker-ci-zap "-folder=$env:TESTRUN_DRIVE`:\$env:TESTRUN_SUBDIR"
192             } else {
193                 Write-Host -ForegroundColor Magenta "WARN: Skip nuking $env:TESTRUN_DRIVE`:\$env:TESTRUN_SUBDIR"
194             }
195         }
196 
197         # TODO: This should be able to be removed in August 2017 update. Only needed for RS1  Production Server workaround - Psched
198         $reg = "HKLM:\System\CurrentControlSet\Services\Psched\Parameters\NdisAdapters"
199         $count=(Get-ChildItem $reg | Measure-Object).Count
200         if ($count -gt 0) {
201             Write-Warning "There are $count NdisAdapters leaked under Psched\Parameters"
202             if ($env:COMPUTERNAME -match "jenkins-rs1-") {
203                 Write-Warning "Cleaning Psched..."
204                 Get-ChildItem $reg | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue | Out-Null
205             } else {
206                 Write-Warning "Not cleaning as not a production RS1 server"
207             }
208         }
209 
210         # TODO: This should be able to be removed in August 2017 update. Only needed for RS1
211         $reg = "HKLM:\System\CurrentControlSet\Services\WFPLWFS\Parameters\NdisAdapters"
212         $count=(Get-ChildItem $reg | Measure-Object).Count
213         if ($count -gt 0) {
214             Write-Warning "There are $count NdisAdapters leaked under WFPLWFS\Parameters"
215             if ($env:COMPUTERNAME -match "jenkins-rs1-") {
216                 Write-Warning "Cleaning WFPLWFS..."
217                 Get-ChildItem $reg | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue | Out-Null
218             } else {
219                 Write-Warning "Not cleaning as not a production RS1 server"
220             }
221         }
222     } catch {
223         # Don't throw any errors onwards Throw $_
224     }
225 }
226 
227 Try {
228     Write-Host -ForegroundColor Cyan "`nINFO: executeCI.ps1 starting at $(date)`n"
229     Write-Host  -ForegroundColor Green "INFO: Script version $SCRIPT_VER"
230     Set-PSDebug -Trace 0  # 1 to turn on
231     $origPath="$env:PATH"            # so we can restore it at the end
232     $origDOCKER_HOST="$DOCKER_HOST"  # So we can restore it at the end
233     $origGOROOT="$env:GOROOT"        # So we can restore it at the end
234     $origGOPATH="$env:GOPATH"        # So we can restore it at the end
235 
236     # Turn off progress bars
237 	$origProgressPreference=$global:ProgressPreference
238 	$global:ProgressPreference='SilentlyContinue'
239 
240     # Git version
241     Write-Host  -ForegroundColor Green "INFO: Running $(git version)"
242 
243     # OS Version
244     $bl=(Get-ItemProperty -Path "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion"  -Name BuildLabEx).BuildLabEx
245     $a=$bl.ToString().Split(".")
246     $Branch=$a[3]
247     $WindowsBuild=$a[0]+"."+$a[1]+"."+$a[4]
248     Write-Host -ForegroundColor green "INFO: Branch:$Branch Build:$WindowsBuild"
249 
250     # List the environment variables
251     Write-Host -ForegroundColor green "INFO: Environment variables:"
252     Get-ChildItem Env: | Out-String
253 
254     # PR
255     if (-not ($null -eq $env:PR)) { Write-Output "INFO: PR#$env:PR (https://github.com/docker/docker/pull/$env:PR)" }
256 
257     # Make sure docker is installed
258     if ($null -eq (Get-Command "docker" -ErrorAction SilentlyContinue)) { Throw "ERROR: docker is not installed or not found on path" }
259 
260     # Make sure docker-ci-zap is installed
261     if ($null -eq (Get-Command "docker-ci-zap" -ErrorAction SilentlyContinue)) { Throw "ERROR: docker-ci-zap is not installed or not found on path" }
262 
263     # Make sure SOURCES_DRIVE is set
264     if ($null -eq $env:SOURCES_DRIVE) { Throw "ERROR: Environment variable SOURCES_DRIVE is not set" }
265 
266     # Make sure TESTRUN_DRIVE is set
267     if ($null -eq $env:TESTRUN_DRIVE) { Throw "ERROR: Environment variable TESTRUN_DRIVE is not set" }
268 
269     # Make sure SOURCES_SUBDIR is set
270     if ($null -eq $env:SOURCES_SUBDIR) { Throw "ERROR: Environment variable SOURCES_SUBDIR is not set" }
271 
272     # Make sure TESTRUN_SUBDIR is set
273     if ($null -eq $env:TESTRUN_SUBDIR) { Throw "ERROR: Environment variable TESTRUN_SUBDIR is not set" }
274 
275     # SOURCES_DRIVE\SOURCES_SUBDIR must be a directory and exist
276     if (-not (Test-Path -PathType Container "$env:SOURCES_DRIVE`:\$env:SOURCES_SUBDIR")) { Throw "ERROR: $env:SOURCES_DRIVE`:\$env:SOURCES_SUBDIR must be an existing directory" }
277 
278     # Create the TESTRUN_DRIVE\TESTRUN_SUBDIR if it does not already exist
279     New-Item -ItemType Directory -Force -Path "$env:TESTRUN_DRIVE`:\$env:TESTRUN_SUBDIR" -ErrorAction SilentlyContinue | Out-Null
280 
281     Write-Host  -ForegroundColor Green "INFO: Sources under $env:SOURCES_DRIVE`:\$env:SOURCES_SUBDIR\..."
282     Write-Host  -ForegroundColor Green "INFO: Test run under $env:TESTRUN_DRIVE`:\$env:TESTRUN_SUBDIR\..."
283 
284     # Check the intended source location is a directory
285     if (-not (Test-Path -PathType Container "$env:SOURCES_DRIVE`:\$env:SOURCES_SUBDIR\src\github.com\docker\docker" -ErrorAction SilentlyContinue)) {
286         Throw "ERROR: $env:SOURCES_DRIVE`:\$env:SOURCES_SUBDIR\src\github.com\docker\docker is not a directory!"
287     }
288 
289     # Make sure we start at the root of the sources
290     Set-Location "$env:SOURCES_DRIVE`:\$env:SOURCES_SUBDIR\src\github.com\docker\docker"
291     Write-Host  -ForegroundColor Green "INFO: Running in $(Get-Location)"
292 
293     # Make sure we are in repo
294     if (-not (Test-Path -PathType Leaf -Path ".\Dockerfile.windows")) {
295         Throw "$(Get-Location) does not contain Dockerfile.windows!"
296     }
297     Write-Host  -ForegroundColor Green "INFO: docker/docker repository was found"
298 
299     # Make sure microsoft/windowsservercore:latest image is installed in the control daemon. On public CI machines, windowsservercore.tar and nanoserver.tar
300     # are pre-baked and tagged appropriately in the c:\baseimages directory, and can be directly loaded.
301     # Note - this script will only work on 10B (Oct 2016) or later machines! Not 9D or previous due to image tagging assumptions.
302     #
303     # On machines not on Microsoft corpnet, or those which have not been pre-baked, we have to docker pull the image in which case it will
304     # will come in directly as microsoft/windowsservercore:latest. The ultimate goal of all this code is to ensure that whatever,
305     # we have microsoft/windowsservercore:latest
306     #
307     # Note we cannot use (as at Oct 2016) nanoserver as the control daemons base image, even if nanoserver is used in the tests themselves.
308 
309     $ErrorActionPreference = "SilentlyContinue"
310     $ControlDaemonBaseImage="windowsservercore"
311 
312     $readBaseFrom="c"
313     if ($((docker images --format "{{.Repository}}:{{.Tag}}" | Select-String $("microsoft/"+$ControlDaemonBaseImage+":latest") | Measure-Object -Line).Lines) -eq 0) {
314         # Try the internal azure CI image version or Microsoft internal corpnet where the base image is already pre-prepared on the disk,
315         # either through Invoke-DockerCI or, in the case of Azure CI servers, baked into the VHD at the same location.
316         if (Test-Path $("$env:SOURCES_DRIVE`:\baseimages\"+$ControlDaemonBaseImage+".tar")) {
317             # An optimization for CI servers to copy it to the D: drive which is an SSD.
318             if ($env:SOURCES_DRIVE -ne $env:TESTRUN_DRIVE) {
319                 $readBaseFrom=$env:TESTRUN_DRIVE
320                 if (!(Test-Path "$env:TESTRUN_DRIVE`:\baseimages")) {
321                     New-Item "$env:TESTRUN_DRIVE`:\baseimages" -type directory | Out-Null
322                 }
323                 if (!(Test-Path "$env:TESTRUN_DRIVE`:\baseimages\windowsservercore.tar")) {
324                     if (Test-Path "$env:SOURCES_DRIVE`:\baseimages\windowsservercore.tar") {
325                         Write-Host -ForegroundColor Green "INFO: Optimisation - copying $env:SOURCES_DRIVE`:\baseimages\windowsservercore.tar to $env:TESTRUN_DRIVE`:\baseimages"
326                         Copy-Item "$env:SOURCES_DRIVE`:\baseimages\windowsservercore.tar" "$env:TESTRUN_DRIVE`:\baseimages"
327                     }
328                 }
329                 if (!(Test-Path "$env:TESTRUN_DRIVE`:\baseimages\nanoserver.tar")) {
330                     if (Test-Path "$env:SOURCES_DRIVE`:\baseimages\nanoserver.tar") {
331                         Write-Host -ForegroundColor Green "INFO: Optimisation - copying $env:SOURCES_DRIVE`:\baseimages\nanoserver.tar to $env:TESTRUN_DRIVE`:\baseimages"
332                         Copy-Item "$env:SOURCES_DRIVE`:\baseimages\nanoserver.tar" "$env:TESTRUN_DRIVE`:\baseimages"
333                     }
334                 }
335                 $readBaseFrom=$env:TESTRUN_DRIVE
336             }
337             Write-Host  -ForegroundColor Green "INFO: Loading"$ControlDaemonBaseImage".tar from disk. This may take some time..."
338             $ErrorActionPreference = "SilentlyContinue"
339             docker load -i $("$readBaseFrom`:\baseimages\"+$ControlDaemonBaseImage+".tar")
340             $ErrorActionPreference = "Stop"
341             if (-not $LastExitCode -eq 0) {
342                 Throw $("ERROR: Failed to load $readBaseFrom`:\baseimages\"+$ControlDaemonBaseImage+".tar")
343             }
344             Write-Host -ForegroundColor Green "INFO: docker load of"$ControlDaemonBaseImage" completed successfully"
345         } else {
346             # We need to docker pull it instead. It will come in directly as microsoft/imagename:latest
347             Write-Host -ForegroundColor Green $("INFO: Pulling microsoft/"+$ControlDaemonBaseImage+":latest from docker hub. This may take some time...")
348             $ErrorActionPreference = "SilentlyContinue"
349             docker pull $("microsoft/"+$ControlDaemonBaseImage)
350             $ErrorActionPreference = "Stop"
351             if (-not $LastExitCode -eq 0) {
352                 Throw $("ERROR: Failed to docker pull microsoft/"+$ControlDaemonBaseImage+":latest.")
353             }
354             Write-Host -ForegroundColor Green $("INFO: docker pull of microsoft/"+$ControlDaemonBaseImage+":latest completed successfully")
355         }
356     } else {
357         Write-Host -ForegroundColor Green "INFO: Image"$("microsoft/"+$ControlDaemonBaseImage+":latest")"is already loaded in the control daemon"
358     }
359 
360     # Inspect the pulled image to get the version directly
361     $ErrorActionPreference = "SilentlyContinue"
362     $imgVersion = $(docker inspect  $("microsoft/"+$ControlDaemonBaseImage) --format "{{.OsVersion}}")
363     $ErrorActionPreference = "Stop"
364     Write-Host -ForegroundColor Green $("INFO: Version of microsoft/"+$ControlDaemonBaseImage+":latest is '"+$imgVersion+"'")
365 
366     # Provide the docker version for debugging purposes.
367     Write-Host  -ForegroundColor Green "INFO: Docker version of control daemon"
368     Write-Host
369     $ErrorActionPreference = "SilentlyContinue"
370     docker version
371     $ErrorActionPreference = "Stop"
372     if (-not($LastExitCode -eq 0)) {
373         Write-Host
374         Write-Host  -ForegroundColor Green "---------------------------------------------------------------------------"
375         Write-Host  -ForegroundColor Green " Failed to get a response from the control daemon. It may be down."
376         Write-Host  -ForegroundColor Green " Try re-running this CI job, or ask on #docker-maintainers on docker slack"
377         Write-Host  -ForegroundColor Green " to see if the daemon is running. Also check the service configuration."
378         Write-Host  -ForegroundColor Green " DOCKER_HOST is set to $DOCKER_HOST."
379         Write-Host  -ForegroundColor Green "---------------------------------------------------------------------------"
380         Write-Host
381         Throw "ERROR: The control daemon does not appear to be running."
382     }
383     Write-Host
384 
385     # Same as above, but docker info
386     Write-Host  -ForegroundColor Green "INFO: Docker info of control daemon"
387     Write-Host
388     $ErrorActionPreference = "SilentlyContinue"
389     docker info
390     $ErrorActionPreference = "Stop"
391     if (-not($LastExitCode -eq 0)) {
392         Throw "ERROR: The control daemon does not appear to be running."
393     }
394     Write-Host
395 
396     # Get the commit has and verify we have something
397     $ErrorActionPreference = "SilentlyContinue"
398     $COMMITHASH=$(git rev-parse --short HEAD)
399     $ErrorActionPreference = "Stop"
400     if (-not($LastExitCode -eq 0)) {
401         Throw "ERROR: Failed to get commit hash. Are you sure this is a docker repository?"
402     }
403     Write-Host  -ForegroundColor Green "INFO: Commit hash is $COMMITHASH"
404 
405     # Nuke everything and go back to our sources after
406     Nuke-Everything
407     cd "$env:SOURCES_DRIVE`:\$env:SOURCES_SUBDIR\src\github.com\docker\docker"
408 
409     # Redirect to a temporary location.
410     $TEMPORIG=$env:TEMP
411     $env:TEMP="$env:TESTRUN_DRIVE`:\$env:TESTRUN_SUBDIR\CI-$COMMITHASH"
412     $env:LOCALAPPDATA="$TEMP\localappdata"
413     $errorActionPreference='Stop'
414     New-Item -ItemType Directory "$env:TEMP" -ErrorAction SilentlyContinue | Out-Null
415     New-Item -ItemType Directory "$env:TEMP\userprofile" -ErrorAction SilentlyContinue  | Out-Null
416     New-Item -ItemType Directory "$env:TEMP\localappdata" -ErrorAction SilentlyContinue | Out-Null
417     New-Item -ItemType Directory "$env:TEMP\binary" -ErrorAction SilentlyContinue | Out-Null
418     New-Item -ItemType Directory "$env:TEMP\installer" -ErrorAction SilentlyContinue | Out-Null
419     if ($null -eq $env:SKIP_COPY_GO) {
420         # Wipe the previous version of GO - we're going to get it out of the image
421         if (Test-Path "$env:TEMP\go") { Remove-Item "$env:TEMP\go" -Recurse -Force -ErrorAction SilentlyContinue | Out-Null }
422         New-Item -ItemType Directory "$env:TEMP\go" -ErrorAction SilentlyContinue | Out-Null
423     }
424 
425     Write-Host -ForegroundColor Green "INFO: Location for testing is $env:TEMP"
426 
427     # CI Integrity check - ensure Dockerfile.windows and Dockerfile go versions match
428     $goVersionDockerfileWindows=$(Get-Content ".\Dockerfile.windows" | Select-String "^ENV GO_VERSION" | Select-object -First 1).ToString().Replace("ENV GO_VERSION=","").Replace("\","").Replace("``","").Trim()
429     $goVersionDockerfile=$(Get-Content ".\Dockerfile" | Select-String "^ENV GO_VERSION" | Select-object -First 1)
430 
431     # As of go 1.11, Dockerfile changed to be in the format like "FROM golang:1.11.0 AS base".
432     # If a version number ends with .0 (as in 1.11.0, a convention used in golang docker
433     # image versions), it needs to be removed (i.e. "1.11.0" becomes "1.11").
434     if ($null -eq $goVersionDockerfile) {
435         $goVersionDockerfile=$(Get-Content ".\Dockerfile" | Select-String "^FROM golang:" | Select-object -First 1)
436         if ($null -ne $goVersionDockerfile) {
437             $goVersionDockerfile = $goVersionDockerfile.ToString().Split(" ")[1].Split(":")[1] -replace '\.0$',''
438         }
439     } else {
440         $goVersionDockerfile = $goVersionDockerfile.ToString().Split(" ")[2]
441     }
442     if ($null -eq $goVersionDockerfile) {
443         Throw "ERROR: Failed to extract golang version from Dockerfile"
444     }
445     Write-Host  -ForegroundColor Green "INFO: Validating GOLang consistency in Dockerfile.windows..."
446     if (-not ($goVersionDockerfile -eq $goVersionDockerfileWindows)) {
447         Throw "ERROR: Mismatched GO versions between Dockerfile and Dockerfile.windows. Update your PR to ensure that both files are updated and in sync. $goVersionDockerfile $goVersionDockerfileWindows"
448     }
449 
450     # Build the image
451     if ($null -eq $env:SKIP_IMAGE_BUILD) {
452         Write-Host  -ForegroundColor Cyan "`n`nINFO: Building the image from Dockerfile.windows at $(Get-Date)..."
453         Write-Host
454         $ErrorActionPreference = "SilentlyContinue"
455         $Duration=$(Measure-Command { docker build -t docker -f Dockerfile.windows . | Out-Host })
456         $ErrorActionPreference = "Stop"
457         if (-not($LastExitCode -eq 0)) {
458            Throw "ERROR: Failed to build image from Dockerfile.windows"
459         }
460         Write-Host  -ForegroundColor Green "INFO: Image build ended at $(Get-Date). Duration`:$Duration"
461     } else {
462         Write-Host -ForegroundColor Magenta "WARN: Skipping building the docker image"
463     }
464 
465     # Following at the moment must be docker\docker as it's dictated by dockerfile.Windows
466     $contPath="$COMMITHASH`:c`:\go\src\github.com\docker\docker\bundles"
467 
468     # After https://github.com/docker/docker/pull/30290, .git was added to .dockerignore. Therefore
469     # we have to calculate unsupported outside of the container, and pass the commit ID in through
470     # an environment variable for the binary build
471     $CommitUnsupported=""
472     if ($(git status --porcelain --untracked-files=no).Length -ne 0) {
473         $CommitUnsupported="-unsupported"
474     }
475 
476     # Build the binary in a container unless asked to skip it.
477     if ($null -eq $env:SKIP_BINARY_BUILD) {
478         Write-Host  -ForegroundColor Cyan "`n`nINFO: Building the test binaries at $(Get-Date)..."
479         $ErrorActionPreference = "SilentlyContinue"
480         docker rm -f $COMMITHASH 2>&1 | Out-Null
481         if ($CommitUnsupported -ne "") {
482             Write-Host ""
483             Write-Warning "This version is unsupported because there are uncommitted file(s)."
484             Write-Warning "Either commit these changes, or add them to .gitignore."
485             git status --porcelain --untracked-files=no | Write-Warning
486              Write-Host ""
487         }
488         $Duration=$(Measure-Command {docker run --name $COMMITHASH -e DOCKER_GITCOMMIT=$COMMITHASH$CommitUnsupported docker hack\make.ps1 -Daemon -Client | Out-Host })
489         $ErrorActionPreference = "Stop"
490         if (-not($LastExitCode -eq 0)) {
491             Throw "ERROR: Failed to build binary"
492         }
493         Write-Host  -ForegroundColor Green "INFO: Binaries build ended at $(Get-Date). Duration`:$Duration"
494 
495         # Copy the binaries and the generated version_autogen.go out of the container
496         $ErrorActionPreference = "SilentlyContinue"
497         docker cp "$contPath\docker.exe" $env:TEMP\binary\
498         if (-not($LastExitCode -eq 0)) {
499             Throw "ERROR: Failed to docker cp the client binary (docker.exe) to $env:TEMP\binary"
500         }
501         docker cp "$contPath\dockerd.exe" $env:TEMP\binary\
502         if (-not($LastExitCode -eq 0)) {
503             Throw "ERROR: Failed to docker cp the daemon binary (dockerd.exe) to $env:TEMP\binary"
504         }
505         $ErrorActionPreference = "Stop"
506 
507         # Copy the built dockerd.exe to dockerd-$COMMITHASH.exe so that easily spotted in task manager.
508         Write-Host -ForegroundColor Green "INFO: Copying the built daemon binary to $env:TEMP\binary\dockerd-$COMMITHASH.exe..."
509         Copy-Item $env:TEMP\binary\dockerd.exe $env:TEMP\binary\dockerd-$COMMITHASH.exe -Force -ErrorAction SilentlyContinue
510 
511         # Copy the built docker.exe to docker-$COMMITHASH.exe
512         Write-Host -ForegroundColor Green "INFO: Copying the built client binary to $env:TEMP\binary\docker-$COMMITHASH.exe..."
513         Copy-Item $env:TEMP\binary\docker.exe $env:TEMP\binary\docker-$COMMITHASH.exe -Force -ErrorAction SilentlyContinue
514 
515     } else {
516         Write-Host -ForegroundColor Magenta "WARN: Skipping building the binaries"
517     }
518 
519     Write-Host -ForegroundColor Green "INFO: Copying dockerversion from the container..."
520     $ErrorActionPreference = "SilentlyContinue"
521     docker cp "$contPath\..\dockerversion\version_autogen.go" "$env:SOURCES_DRIVE`:\$env:SOURCES_SUBDIR\src\github.com\docker\docker\dockerversion"
522     if (-not($LastExitCode -eq 0)) {
523          Throw "ERROR: Failed to docker cp the generated version_autogen.go to $env:SOURCES_DRIVE`:\$env:SOURCES_SUBDIR\src\github.com\docker\docker\dockerversion"
524     }
525     $ErrorActionPreference = "Stop"
526 
527     # Grab the golang installer out of the built image. That way, we know we are consistent once extracted and paths set,
528     # so there's no need to re-deploy on account of an upgrade to the version of GO being used in docker.
529     if ($null -eq $env:SKIP_COPY_GO) {
530         Write-Host -ForegroundColor Green "INFO: Copying the golang package from the container to $env:TEMP\installer\go.zip..."
531         docker cp "$COMMITHASH`:c`:\go.zip" $env:TEMP\installer\
532         if (-not($LastExitCode -eq 0)) {
533             Throw "ERROR: Failed to docker cp the golang installer 'go.zip' from container:c:\go.zip to $env:TEMP\installer"
534         }
535         $ErrorActionPreference = "Stop"
536 
537         # Extract the golang installer
538         Write-Host -ForegroundColor Green "INFO: Extracting go.zip to $env:TEMP\go"
539         $Duration=$(Measure-Command { Expand-Archive $env:TEMP\installer\go.zip $env:TEMP -Force | Out-Null})
540         Write-Host  -ForegroundColor Green "INFO: Extraction ended at $(Get-Date). Duration`:$Duration"
541     } else {
542         Write-Host -ForegroundColor Magenta "WARN: Skipping copying and extracting golang from the image"
543     }
544 
545     # Set the GOPATH
546     Write-Host -ForegroundColor Green "INFO: Updating the golang and path environment variables"
547     $env:GOPATH="$env:SOURCES_DRIVE`:\$env:SOURCES_SUBDIR"
548     Write-Host -ForegroundColor Green "INFO: GOPATH=$env:GOPATH"
549 
550     # Set the path to have the version of go from the image at the front
551     $env:PATH="$env:TEMP\go\bin;$env:PATH"
552 
553     # Set the GOROOT to be our copy of go from the image
554     $env:GOROOT="$env:TEMP\go"
555     Write-Host -ForegroundColor Green "INFO: $(go version)"
556 
557     # Work out the -H parameter for the daemon under test (DASHH_DUT) and client under test (DASHH_CUT)
558     #$DASHH_DUT="npipe:////./pipe/$COMMITHASH" # Can't do remote named pipe
559     #$ip = (resolve-dnsname $env:COMPUTERNAME -type A -NoHostsFile -LlmnrNetbiosOnly).IPAddress # Useful to tie down
560     $DASHH_CUT="tcp://127.0.0.1`:2357"    # Not a typo for 2375!
561     $DASHH_DUT="tcp://0.0.0.0:2357"       # Not a typo for 2375!
562 
563     # Arguments for the daemon under test
564     $dutArgs=@()
565     $dutArgs += "-H $DASHH_DUT"
566     $dutArgs += "--data-root $env:TEMP\daemon"
567     $dutArgs += "--pidfile $env:TEMP\docker.pid"
568 
569     # Save the PID file so we can nuke it if set
570     $pidFile="$env:TEMP\docker.pid"
571 
572     # Arguments: Are we starting the daemon under test in debug mode?
573     if (-not ("$env:DOCKER_DUT_DEBUG" -eq "")) {
574         Write-Host -ForegroundColor Green "INFO: Running the daemon under test in debug mode"
575         $dutArgs += "-D"
576     }
577 
578     # Arguments: Are we starting the daemon under test with Hyper-V containers as the default isolation?
579     if (-not ("$env:DOCKER_DUT_HYPERV" -eq "")) {
580         Write-Host -ForegroundColor Green "INFO: Running the daemon under test with Hyper-V containers as the default"
581         $dutArgs += "--exec-opt isolation=hyperv"
582     }
583 
584     # Start the daemon under test, ensuring everything is redirected to folders under $TEMP.
585     # Important - we launch the -$COMMITHASH version so that we can kill it without
586     # killing the control daemon.
587     Write-Host -ForegroundColor Green "INFO: Starting a daemon under test..."
588     Write-Host -ForegroundColor Green "INFO: Args: $dutArgs"
589     New-Item -ItemType Directory $env:TEMP\daemon -ErrorAction SilentlyContinue  | Out-Null
590 
591     # In LCOW mode, for now we need to set an environment variable before starting the daemon under test
592     if (($null -ne $env:LCOW_MODE) -or ($null -ne $env:LCOW_BASIC_MODE)) {
593         $env:LCOW_SUPPORTED=1
594     }
595 
596     # Cannot fathom why, but always writes to stderr....
597     Start-Process "$env:TEMP\binary\dockerd-$COMMITHASH" `
598                   -ArgumentList $dutArgs `
599                   -RedirectStandardOutput "$env:TEMP\dut.out" `
600                   -RedirectStandardError "$env:TEMP\dut.err"
601     Write-Host -ForegroundColor Green "INFO: Process started successfully."
602     $daemonStarted=1
603 
604     # In LCOW mode, turn off that variable
605     if (($null -ne $env:LCOW_MODE) -or ($null -ne $env:LCOW_BASIC_MODE)) {
606         $env:LCOW_SUPPORTED=""
607     }
608 
609 
610     # Start tailing the daemon under test if the command is installed
611     if ($null -ne (Get-Command "tail" -ErrorAction SilentlyContinue)) {
612         $tail = start-process "tail" -ArgumentList "-f $env:TEMP\dut.out" -ErrorAction SilentlyContinue
613     }
614 
615     # Verify we can get the daemon under test to respond
616     $tries=20
617     Write-Host -ForegroundColor Green "INFO: Waiting for the daemon under test to start..."
618     while ($true) {
619         $ErrorActionPreference = "SilentlyContinue"
620         & "$env:TEMP\binary\docker-$COMMITHASH" "-H=$($DASHH_CUT)" version 2>&1 | Out-Null
621         $ErrorActionPreference = "Stop"
622         if ($LastExitCode -eq 0) {
623             break
624         }
625 
626         $tries--
627         if ($tries -le 0) {
628             Throw "ERROR: Failed to get a response from the daemon under test"
629         }
630         Write-Host -NoNewline "."
631         sleep 1
632     }
633     Write-Host -ForegroundColor Green "INFO: Daemon under test started and replied!"
634 
635     # Provide the docker version of the daemon under test for debugging purposes.
636     Write-Host -ForegroundColor Green "INFO: Docker version of the daemon under test"
637     Write-Host
638     $ErrorActionPreference = "SilentlyContinue"
639     & "$env:TEMP\binary\docker-$COMMITHASH" "-H=$($DASHH_CUT)" version
640     $ErrorActionPreference = "Stop"
641     if ($LastExitCode -ne 0) {
642         Throw "ERROR: The daemon under test does not appear to be running."
643     }
644     Write-Host
645 
646     # Same as above but docker info
647     Write-Host -ForegroundColor Green "INFO: Docker info of the daemon under test"
648     Write-Host
649     $ErrorActionPreference = "SilentlyContinue"
650     & "$env:TEMP\binary\docker-$COMMITHASH" "-H=$($DASHH_CUT)" info
651     $ErrorActionPreference = "Stop"
652     if ($LastExitCode -ne 0) {
653         Throw "ERROR: The daemon under test does not appear to be running."
654     }
655     Write-Host
656 
657     # Same as above but docker images
658     Write-Host -ForegroundColor Green "INFO: Docker images of the daemon under test"
659     Write-Host
660     $ErrorActionPreference = "SilentlyContinue"
661     & "$env:TEMP\binary\docker-$COMMITHASH" "-H=$($DASHH_CUT)" images
662     $ErrorActionPreference = "Stop"
663     if ($LastExitCode -ne 0) {
664         Throw "ERROR: The daemon under test does not appear to be running."
665     }
666     Write-Host
667 
668     # Don't need Windows images when in LCOW mode.
669     if (($null -eq $env:LCOW_MODE) -and ($null -eq $env:LCOW_BASIC_MODE)) {
670 
671         # Default to windowsservercore for the base image used for the tests. The "docker" image
672         # and the control daemon use microsoft/windowsservercore regardless. This is *JUST* for the tests.
673         if ($null -eq $env:WINDOWS_BASE_IMAGE) {
674             $env:WINDOWS_BASE_IMAGE="microsoft/windowsservercore"
675         }
676 
677         # Lowercase and make sure it has a microsoft/ prefix
678         $env:WINDOWS_BASE_IMAGE = $env:WINDOWS_BASE_IMAGE.ToLower()
679         if ($($env:WINDOWS_BASE_IMAGE -Split "/")[0] -ne "microsoft") {
680             Throw "ERROR: WINDOWS_BASE_IMAGE should start microsoft/"
681         }
682 
683         Write-Host -ForegroundColor Green "INFO: Base image for tests is $env:WINDOWS_BASE_IMAGE"
684 
685         $ErrorActionPreference = "SilentlyContinue"
686         if ($((& "$env:TEMP\binary\docker-$COMMITHASH" "-H=$($DASHH_CUT)" images --format "{{.Repository}}:{{.Tag}}" | Select-String $($env:WINDOWS_BASE_IMAGE+":latest") | Measure-Object -Line).Lines) -eq 0) {
687             # Try the internal azure CI image version or Microsoft internal corpnet where the base image is already pre-prepared on the disk,
688             # either through Invoke-DockerCI or, in the case of Azure CI servers, baked into the VHD at the same location.
689             if (Test-Path $("c:\baseimages\"+$($env:WINDOWS_BASE_IMAGE -Split "/")[1]+".tar")) {
690                 Write-Host  -ForegroundColor Green "INFO: Loading"$($env:WINDOWS_BASE_IMAGE -Split "/")[1]".tar from disk into the daemon under test. This may take some time..."
691                 $ErrorActionPreference = "SilentlyContinue"
692                 & "$env:TEMP\binary\docker-$COMMITHASH" "-H=$($DASHH_CUT)" load -i $("$readBaseFrom`:\baseimages\"+$($env:WINDOWS_BASE_IMAGE -Split "/")[1]+".tar")
693                 $ErrorActionPreference = "Stop"
694                 if (-not $LastExitCode -eq 0) {
695                     Throw $("ERROR: Failed to load $readBaseFrom`:\baseimages\"+$($env:WINDOWS_BASE_IMAGE -Split "/")[1]+".tar into daemon under test")
696                 }
697                 Write-Host -ForegroundColor Green "INFO: docker load of"$($env:WINDOWS_BASE_IMAGE -Split "/")[1]" into daemon under test completed successfully"
698             } else {
699                 # We need to docker pull it instead. It will come in directly as microsoft/imagename:latest
700                 Write-Host -ForegroundColor Green $("INFO: Pulling "+$env:WINDOWS_BASE_IMAGE+":latest from docker hub into daemon under test. This may take some time...")
701                 $ErrorActionPreference = "SilentlyContinue"
702                 & "$env:TEMP\binary\docker-$COMMITHASH" "-H=$($DASHH_CUT)" pull $($env:WINDOWS_BASE_IMAGE)
703                 $ErrorActionPreference = "Stop"
704                 if (-not $LastExitCode -eq 0) {
705                     Throw $("ERROR: Failed to docker pull "+$env:WINDOWS_BASE_IMAGE+":latest into daemon under test.")
706                 }
707                 Write-Host -ForegroundColor Green $("INFO: docker pull of "+$env:WINDOWS_BASE_IMAGE+":latest into daemon under test completed successfully")
708             }
709         } else {
710             Write-Host -ForegroundColor Green "INFO: Image"$($env:WINDOWS_BASE_IMAGE+":latest")"is already loaded in the daemon under test"
711         }
712 
713 
714         # Inspect the pulled or loaded image to get the version directly
715         $ErrorActionPreference = "SilentlyContinue"
716         $dutimgVersion = $(&"$env:TEMP\binary\docker-$COMMITHASH" "-H=$($DASHH_CUT)" inspect  $($env:WINDOWS_BASE_IMAGE) --format "{{.OsVersion}}")
717         $ErrorActionPreference = "Stop"
718         Write-Host -ForegroundColor Green $("INFO: Version of "+$env:WINDOWS_BASE_IMAGE+":latest is '"+$dutimgVersion+"'")
719     }
720 
721     # Run the validation tests unless SKIP_VALIDATION_TESTS is defined.
722     if ($null -eq $env:SKIP_VALIDATION_TESTS) {
723         Write-Host -ForegroundColor Cyan "INFO: Running validation tests at $(Get-Date)..."
724         $ErrorActionPreference = "SilentlyContinue"
725         $Duration=$(Measure-Command { hack\make.ps1 -DCO -GoFormat -PkgImports | Out-Host })
726         $ErrorActionPreference = "Stop"
727         if (-not($LastExitCode -eq 0)) {
728             Throw "ERROR: Validation tests failed"
729         }
730         Write-Host  -ForegroundColor Green "INFO: Validation tests ended at $(Get-Date). Duration`:$Duration"
731     } else {
732         Write-Host -ForegroundColor Magenta "WARN: Skipping validation tests"
733     }
734 
735     # Note the unit tests won't work in LCOW mode as I turned off loading the base images above.
736     # Run the unit tests inside a container unless SKIP_UNIT_TESTS is defined
737     if (($null -eq $env:LCOW_MODE) -and ($null -eq $env:LCOW_BASIC_MODE)) {
738         if ($null -eq $env:SKIP_UNIT_TESTS) {
739             Write-Host -ForegroundColor Cyan "INFO: Running unit tests at $(Get-Date)..."
740             $ErrorActionPreference = "SilentlyContinue"
741             $Duration=$(Measure-Command {docker run -e DOCKER_GITCOMMIT=$COMMITHASH$CommitUnsupported docker hack\make.ps1 -TestUnit | Out-Host })
742             $ErrorActionPreference = "Stop"
743             if (-not($LastExitCode -eq 0)) {
744                 Throw "ERROR: Unit tests failed"
745             }
746             Write-Host  -ForegroundColor Green "INFO: Unit tests ended at $(Get-Date). Duration`:$Duration"
747         } else {
748             Write-Host -ForegroundColor Magenta "WARN: Skipping unit tests"
749         }
750     }
751 
752     # Add the Windows busybox image. Needed for WCOW integration tests
753     if (($null -eq $env:LCOW_MODE) -and ($null -eq $env:LCOW_BASIC_MODE)) {
754         if ($null -eq $env:SKIP_INTEGRATION_TESTS) {
755             $ErrorActionPreference = "SilentlyContinue"
756             # Build it regardless while switching between nanoserver and windowsservercore
757             #$bbCount = $(& "$env:TEMP\binary\docker-$COMMITHASH" "-H=$($DASHH_CUT)" images | Select-String "busybox" | Measure-Object -line).Lines
758             #$ErrorActionPreference = "Stop"
759             #if (-not($LastExitCode -eq 0)) {
760             #    Throw "ERROR: Could not determine if busybox image is present"
761             #}
762             #if ($bbCount -eq 0) {
763                 Write-Host -ForegroundColor Green "INFO: Building busybox"
764                 $ErrorActionPreference = "SilentlyContinue"
765 
766                 # This is a temporary hack for nanoserver
767                 if ($env:WINDOWS_BASE_IMAGE -ne "microsoft/windowsservercore") {
768                     Write-Host -ForegroundColor Red "HACK HACK HACK - Building 64-bit nanoserver busybox image"
769                     $(& "$env:TEMP\binary\docker-$COMMITHASH" "-H=$($DASHH_CUT)" build -t busybox https://raw.githubusercontent.com/jhowardmsft/busybox64/v1.1/Dockerfile | Out-Host)
770                 } else {
771                     $(& "$env:TEMP\binary\docker-$COMMITHASH" "-H=$($DASHH_CUT)" build -t busybox https://raw.githubusercontent.com/jhowardmsft/busybox/v1.1/Dockerfile | Out-Host)
772                 }
773                 $ErrorActionPreference = "Stop"
774                 if (-not($LastExitCode -eq 0)) {
775                     Throw "ERROR: Failed to build busybox image"
776                 }
777             #}
778 
779 
780             Write-Host -ForegroundColor Green "INFO: Docker images of the daemon under test"
781             Write-Host
782             $ErrorActionPreference = "SilentlyContinue"
783             & "$env:TEMP\binary\docker-$COMMITHASH" "-H=$($DASHH_CUT)" images
784             $ErrorActionPreference = "Stop"
785             if ($LastExitCode -ne 0) {
786                 Throw "ERROR: The daemon under test does not appear to be running."
787             }
788             Write-Host
789         }
790     }
791 
792     # Run the WCOW integration tests unless SKIP_INTEGRATION_TESTS is defined
793     if (($null -eq $env:LCOW_MODE) -and ($null -eq $env:LCOW_BASIC_MODE)) {
794         if ($null -eq $env:SKIP_INTEGRATION_TESTS) {
795             Write-Host -ForegroundColor Cyan "INFO: Running integration tests at $(Get-Date)..."
796             $ErrorActionPreference = "SilentlyContinue"
797 
798             # Location of the daemon under test.
799             $env:OrigDOCKER_HOST="$env:DOCKER_HOST"
800 
801             #https://blogs.technet.microsoft.com/heyscriptingguy/2011/09/20/solve-problems-with-external-command-lines-in-powershell/ is useful to see tokenising
802             $c = "go test "
803             $c += "`"-check.v`" "
804             if ($null -ne $env:INTEGRATION_TEST_NAME) { # Makes is quicker for debugging to be able to run only a subset of the integration tests
805                 $c += "`"-check.f`" "
806                 $c += "`"$env:INTEGRATION_TEST_NAME`" "
807                 Write-Host -ForegroundColor Magenta "WARN: Only running integration tests matching $env:INTEGRATION_TEST_NAME"
808             }
809             $c += "`"-tags`" " + "`"autogen`" "
810             $c += "`"-check.timeout`" " + "`"10m`" "
811             $c += "`"-test.timeout`" " + "`"200m`" "
812 
813             if ($null -ne $env:INTEGRATION_IN_CONTAINER) {
814                 Write-Host -ForegroundColor Green "INFO: Integration tests being run inside a container"
815                 # Note we talk back through the containers gateway address
816                 # And the ridiculous lengths we have to go to get the default gateway address... (GetNetIPConfiguration doesn't work in nanoserver)
817                 # I just could not get the escaping to work in a single command, so output $c to a file and run that in the container instead...
818                 # Not the prettiest, but it works.
819                 $c | Out-File -Force "$env:TEMP\binary\runIntegrationCLI.ps1"
820                 $Duration= $(Measure-Command { & docker run `
821                                                         --rm `
822                                                         -e c=$c `
823                                                         --workdir "c`:\go\src\github.com\docker\docker\integration-cli" `
824                                                         -v "$env:TEMP\binary`:c:\target" `
825                                                         docker `
826                                                         "`$env`:PATH`='c`:\target;'+`$env:PATH`;  `$env:DOCKER_HOST`='tcp`://'+(ipconfig | select -last 1).Substring(39)+'`:2357'; c:\target\runIntegrationCLI.ps1" | Out-Host } )
827             } else  {
828                 Write-Host -ForegroundColor Green "INFO: Integration tests being run from the host:"
829                 Set-Location "$env:SOURCES_DRIVE`:\$env:SOURCES_SUBDIR\src\github.com\docker\docker\integration-cli"
830                 $env:DOCKER_HOST=$DASHH_CUT
831                 $env:PATH="$env:TEMP\binary;$env:PATH;"  # Force to use the test binaries, not the host ones.
832                 Write-Host -ForegroundColor Green "INFO: $c"
833                 Write-Host -ForegroundColor Green "INFO: DOCKER_HOST at $DASHH_CUT"
834                 # Explicit to not use measure-command otherwise don't get output as it goes
835                 $start=(Get-Date); Invoke-Expression $c; $Duration=New-Timespan -Start $start -End (Get-Date)
836             }
837             $ErrorActionPreference = "Stop"
838             if (-not($LastExitCode -eq 0)) {
839                 Throw "ERROR: Integration tests failed at $(Get-Date). Duration`:$Duration"
840             }
841             Write-Host  -ForegroundColor Green "INFO: Integration tests ended at $(Get-Date). Duration`:$Duration"
842         } else {
843             Write-Host -ForegroundColor Magenta "WARN: Skipping integration tests"
844         }
845     } else {
846         # The LCOW version of the tests here
847         if ($null -eq $env:SKIP_INTEGRATION_TESTS) {
848             Write-Host -ForegroundColor Cyan "INFO: Running LCOW tests at $(Get-Date)..."
849 
850             $ErrorActionPreference = "SilentlyContinue"
851 
852             # Location of the daemon under test.
853             $env:OrigDOCKER_HOST="$env:DOCKER_HOST"
854 
855             # Make sure we are pointing at the DUT
856             $env:DOCKER_HOST=$DASHH_CUT
857             Write-Host -ForegroundColor Green "INFO: DOCKER_HOST at $DASHH_CUT"
858 
859             # Force to use the test binaries, not the host ones.
860             $env:PATH="$env:TEMP\binary;$env:PATH;"
861 
862             if ($null -ne $env:LCOW_BASIC_MODE) {
863                 $wc = New-Object net.webclient
864                 try {
865                     Write-Host -ForegroundColor green "INFO: Downloading latest execution script..."
866                     $wc.Downloadfile("https://raw.githubusercontent.com/jhowardmsft/docker-w2wCIScripts/master/runCI/lcowbasicvalidation.ps1", "$env:TEMP\binary\lcowbasicvalidation.ps1")
867                 }
868                 catch [System.Net.WebException]
869                 {
870                     Throw ("Failed to download: $_")
871                 }
872 
873                 # Explicit to not use measure-command otherwise don't get output as it goes
874                 $ErrorActionPreference = "Stop"
875                 $start=(Get-Date); Invoke-Expression "powershell $env:TEMP\binary\lcowbasicvalidation.ps1"; $lec=$lastExitCode; $Duration=New-Timespan -Start $start -End (Get-Date)
876                 $Duration=New-Timespan -Start $start -End (Get-Date)
877                 Write-Host  -ForegroundColor Green "INFO: LCOW tests ended at $(Get-Date). Duration`:$Duration"
878                 if ($lec -ne 0) {
879                     Throw "LCOW validation tests failed"
880                 }
881             } else {
882                 #https://blogs.technet.microsoft.com/heyscriptingguy/2011/09/20/solve-problems-with-external-command-lines-in-powershell/ is useful to see tokenising
883                 $c = "go test "
884                 $c += "`"-check.v`" "
885                 if ($null -ne $env:INTEGRATION_TEST_NAME) { # Makes is quicker for debugging to be able to run only a subset of the integration tests
886                     $c += "`"-check.f`" "
887                     $c += "`"$env:INTEGRATION_TEST_NAME`" "
888                     Write-Host -ForegroundColor Magenta "WARN: Only running LCOW integration tests matching $env:INTEGRATION_TEST_NAME"
889                 }
890                 $c += "`"-tags`" " + "`"autogen`" "
891                 $c += "`"-check.timeout`" " + "`"10m`" "
892                 $c += "`"-test.timeout`" " + "`"200m`" "
893 
894                 Write-Host -ForegroundColor Green "INFO: LCOW Integration tests being run from the host:"
895                 Set-Location "$env:SOURCES_DRIVE`:\$env:SOURCES_SUBDIR\src\github.com\docker\docker\integration-cli"
896                 Write-Host -ForegroundColor Green "INFO: $c"
897                 Write-Host -ForegroundColor Green "INFO: DOCKER_HOST at $DASHH_CUT"
898                 # Explicit to not use measure-command otherwise don't get output as it goes
899                 $start=(Get-Date); Invoke-Expression $c; $Duration=New-Timespan -Start $start -End (Get-Date)
900 
901             }
902             $ErrorActionPreference = "Stop"
903             if (-not($LastExitCode -eq 0)) {
904                 Throw "ERROR: Integration tests failed at $(Get-Date). Duration`:$Duration"
905             }
906             Write-Host  -ForegroundColor Green "INFO: Integration tests ended at $(Get-Date). Duration`:$Duration"
907         } else {
908             Write-Host -ForegroundColor Magenta "WARN: Skipping LCOW tests"
909         }
910     }
911 
912     # Docker info now to get counts (after or if jjh/containercounts is merged)
913     if ($daemonStarted -eq 1) {
914         Write-Host -ForegroundColor Green "INFO: Docker info of the daemon under test at end of run"
915         Write-Host
916         $ErrorActionPreference = "SilentlyContinue"
917         & "$env:TEMP\binary\docker-$COMMITHASH" "-H=$($DASHH_CUT)" info
918         $ErrorActionPreference = "Stop"
919         if ($LastExitCode -ne 0) {
920             Throw "ERROR: The daemon under test does not appear to be running."
921         }
922         Write-Host
923     }
924 
925     # Stop the daemon under test
926     if (Test-Path "$env:TEMP\docker.pid") {
927         $p=Get-Content "$env:TEMP\docker.pid" -raw
928         if (($null -ne $p) -and ($daemonStarted -eq 1)) {
929             Write-Host -ForegroundColor green "INFO: Stopping daemon under test"
930             taskkill -f -t -pid $p
931             #sleep 5
932         }
933         Remove-Item "$env:TEMP\docker.pid" -force -ErrorAction SilentlyContinue
934     }
935 
936     Write-Host -ForegroundColor Green "INFO: executeCI.ps1 Completed successfully at $(Get-Date)."
937 }
938 Catch [Exception] {
939     $FinallyColour="Red"
940     Write-Host -ForegroundColor Red ("`r`n`r`nERROR: Failed '$_' at $(Get-Date)")
941     Write-Host -ForegroundColor Red ($_.InvocationInfo.PositionMessage)
942     Write-Host "`n`n"
943 
944     # Exit to ensure Jenkins captures it. Don't do this in the ISE or interactive Powershell - they will catch the Throw onwards.
945     if ( ([bool]([Environment]::GetCommandLineArgs() -Like '*-NonInteractive*')) -and `
946          ([bool]([Environment]::GetCommandLineArgs() -NotLike "*Powershell_ISE.exe*"))) {
947         exit 1
948     }
949     Throw $_
950 }
951 Finally {
952     $ErrorActionPreference="SilentlyContinue"
953     $global:ProgressPreference=$origProgressPreference
954     Write-Host  -ForegroundColor Green "INFO: Tidying up at end of run"
955 
956     # Restore the path
957     if ($null -ne $origPath) { $env:PATH=$origPath }
958 
959     # Restore the DOCKER_HOST
960     if ($null -ne $origDOCKER_HOST) { $env:DOCKER_HOST=$origDOCKER_HOST }
961 
962     # Restore the GOROOT and GOPATH variables
963     if ($null -ne $origGOROOT) { $env:GOROOT=$origGOROOT }
964     if ($null -ne $origGOPATH) { $env:GOPATH=$origGOPATH }
965 
966     # Dump the daemon log. This will include any possible panic stack in the .err.
967     if (($daemonStarted -eq 1) -and  ($(Get-Item "$env:TEMP\dut.err").Length -gt 0)) {
968         Write-Host -ForegroundColor Cyan "----------- DAEMON LOG ------------"
969         Get-Content "$env:TEMP\dut.err" -ErrorAction SilentlyContinue | Write-Host -ForegroundColor Cyan
970         Write-Host -ForegroundColor Cyan "----------- END DAEMON LOG --------"
971     }
972 
973     # Save the daemon under test log
974     if ($daemonStarted -eq 1) {
975         Write-Host -ForegroundColor Green "INFO: Saving daemon under test log ($env:TEMP\dut.out) to $TEMPORIG\CIDUT.out"
976         Copy-Item  "$env:TEMP\dut.out" "$TEMPORIG\CIDUT.out" -Force -ErrorAction SilentlyContinue
977         Write-Host -ForegroundColor Green "INFO: Saving daemon under test log ($env:TEMP\dut.err) to $TEMPORIG\CIDUT.err"
978         Copy-Item  "$env:TEMP\dut.err" "$TEMPORIG\CIDUT.err" -Force -ErrorAction SilentlyContinue
979     }
980 
981     Set-Location "$env:SOURCES_DRIVE\$env:SOURCES_SUBDIR" -ErrorAction SilentlyContinue
982     Nuke-Everything
983     $Dur=New-TimeSpan -Start $StartTime -End $(Get-Date)
984     Write-Host -ForegroundColor $FinallyColour "`nINFO: executeCI.ps1 exiting at $(date). Duration $dur`n"
985 }
986