github.com/devdivbcp/moby@v17.12.0-ce-rc1.0.20200726071732-2d4bfdc789ad+incompatible/hack/ci/windows.ps1 (about)

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