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