|
| 1 | +$script:forcePull = $true |
| 2 | +# Get docker Engine OS |
| 3 | +function Get-DockerEngineOs |
| 4 | +{ |
| 5 | + docker info --format '{{ .OperatingSystem }}' |
| 6 | +} |
| 7 | + |
| 8 | +# Call Docker with appropriate result checksfunction Invoke-Docker |
| 9 | +function Invoke-Docker |
| 10 | +{ |
| 11 | + param( |
| 12 | + [Parameter(Mandatory=$true)] |
| 13 | + [string[]] |
| 14 | + $Command, |
| 15 | + [ValidateSet("error","warning",'ignore')] |
| 16 | + $FailureAction = 'error', |
| 17 | + |
| 18 | + [Parameter(Mandatory=$true)] |
| 19 | + [string[]] |
| 20 | + $Params, |
| 21 | + |
| 22 | + [switch] |
| 23 | + $PassThru, |
| 24 | + [switch] |
| 25 | + $SuppressHostOutput |
| 26 | + ) |
| 27 | + |
| 28 | + $ErrorActionPreference = 'Continue' |
| 29 | + |
| 30 | + # Log how we are running docker for troubleshooting issues |
| 31 | + Write-Verbose "Running docker $command $params" -Verbose |
| 32 | + if($SuppressHostOutput.IsPresent) |
| 33 | + { |
| 34 | + $result = docker $command $params 2>&1 |
| 35 | + } |
| 36 | + else |
| 37 | + { |
| 38 | + &'docker' $command $params 2>&1 | Tee-Object -Variable result -ErrorAction SilentlyContinue | Out-String -Stream -ErrorAction SilentlyContinue | Write-Host -ErrorAction SilentlyContinue |
| 39 | + } |
| 40 | + |
| 41 | + $dockerExitCode = $LASTEXITCODE |
| 42 | + if($PassThru.IsPresent) |
| 43 | + { |
| 44 | + Write-Verbose "passing through docker result$($result.length)..." -Verbose |
| 45 | + return $result |
| 46 | + } |
| 47 | + elseif($dockerExitCode -ne 0 -and $FailureAction -eq 'error') |
| 48 | + { |
| 49 | + Write-Error "docker $command failed with: $result" -ErrorAction Stop |
| 50 | + return $false |
| 51 | + } |
| 52 | + elseif($dockerExitCode -ne 0 -and $FailureAction -eq 'warning') |
| 53 | + { |
| 54 | + Write-Warning "docker $command failed with: $result" |
| 55 | + return $false |
| 56 | + } |
| 57 | + elseif($dockerExitCode -ne 0) |
| 58 | + { |
| 59 | + return $false |
| 60 | + } |
| 61 | + |
| 62 | + return $true |
| 63 | +} |
| 64 | + |
| 65 | +# Return a list of Linux Container Test Cases |
| 66 | +function Get-LinuxContainer |
| 67 | +{ |
| 68 | + foreach($os in 'centos7','opensuse42.1','ubuntu14.04','ubuntu16.04') |
| 69 | + { |
| 70 | + Write-Output @{ |
| 71 | + Name = $os |
| 72 | + Path = "$psscriptroot/../release/$os" |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | +} |
| 77 | + |
| 78 | +# Return a list of Windows Container Test Cases |
| 79 | +function Get-WindowsContainer |
| 80 | +{ |
| 81 | + foreach($os in 'windowsservercore','nanoserver') |
| 82 | + { |
| 83 | + Write-Output @{ |
| 84 | + Name = $os |
| 85 | + Path = "$psscriptroot/../release/$os" |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | + |
| 91 | +$script:repoName = 'microsoft/powershell' |
| 92 | +function Get-RepoName |
| 93 | +{ |
| 94 | + return $script:repoName |
| 95 | +} |
| 96 | + |
| 97 | +function Set-RepoName |
| 98 | +{ |
| 99 | + param([string]$RepoName) |
| 100 | + |
| 101 | + $script:repoName = $RepoName |
| 102 | + $script:forcePull = $false |
| 103 | +} |
| 104 | + |
| 105 | +function Test-SkipWindows |
| 106 | +{ |
| 107 | + [bool] $canRunWindows = (Get-DockerEngineOs) -like 'Windows*' |
| 108 | + return ($IsLinux -or $IsOSX -or !$canRunWindows) |
| 109 | +} |
| 110 | + |
| 111 | +function Test-SkipLinux |
| 112 | +{ |
| 113 | + return !((Get-DockerEngineOs) -like 'Alpine Linux*') |
| 114 | +} |
| 115 | + |
| 116 | +function Get-TestContext |
| 117 | +{ |
| 118 | + param( |
| 119 | + [ValidateSet('Linux','Windows','macOS')] |
| 120 | + [string]$Type |
| 121 | + ) |
| 122 | + |
| 123 | + $resultFileName = 'results.xml' |
| 124 | + $logFileName = 'results.log' |
| 125 | + $containerTestDrive = '/test' |
| 126 | + |
| 127 | + # Return a windows context if the Context in Windows *AND* |
| 128 | + # the current system is windows, otherwise Join-path will fail. |
| 129 | + if($Type -eq 'Windows' -and $IsWindows) |
| 130 | + { |
| 131 | + $ContainerTestDrive = 'C:\test' |
| 132 | + } |
| 133 | + $resolvedTestDrive = (Resolve-Path "Testdrive:\").providerPath |
| 134 | + |
| 135 | + return @{ |
| 136 | + ResolvedTestDrive = $resolvedTestDrive |
| 137 | + ResolvedXmlPath = Join-Path $resolvedTestDrive -ChildPath $resultFileName |
| 138 | + ResolvedLogPath = Join-Path $resolvedTestDrive -ChildPath $logFileName |
| 139 | + ContainerTestDrive = $ContainerTestDrive |
| 140 | + ContainerXmlPath = Join-Path $containerTestDrive -ChildPath $resultFileName |
| 141 | + ContainerLogPath = Join-Path $containerTestDrive -ChildPath $logFileName |
| 142 | + Type = $Type |
| 143 | + ForcePull = $script:forcePull |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +function Get-ContainerPowerShellVersion |
| 148 | +{ |
| 149 | + param( |
| 150 | + [HashTable] $TestContext, |
| 151 | + [string] $RepoName, |
| 152 | + [string] $Name |
| 153 | + ) |
| 154 | + |
| 155 | + $imageTag = "${script:repoName}:${Name}" |
| 156 | + |
| 157 | + if($TestContext.ForcePull) |
| 158 | + { |
| 159 | + $null=Invoke-Docker -Command 'image', 'pull' -Params $imageTag -SuppressHostOutput |
| 160 | + } |
| 161 | + |
| 162 | + $runParams = @() |
| 163 | + $localVolumeName = $testContext.resolvedTestDrive |
| 164 | + $runParams += '--rm' |
| 165 | + if($TestContext.Type -ne 'Windows' -and $isWindows) |
| 166 | + { |
| 167 | + # use a container volume on windows because host volumes are not automatic |
| 168 | + $volumeName = "test-volume-" + (Get-Random -Minimum 100 -Maximum 999) |
| 169 | + |
| 170 | + # using alpine because it's tiny |
| 171 | + $null=Invoke-Docker -Command create -Params '-v', '/test', '--name', $volumeName, 'alpine' -SuppressHostOutput |
| 172 | + $runParams += '--volumes-from' |
| 173 | + $runParams += $volumeName |
| 174 | + } |
| 175 | + else { |
| 176 | + $runParams += '-v' |
| 177 | + $runParams += "${localVolumeName}:$($testContext.containerTestDrive)" |
| 178 | + } |
| 179 | + |
| 180 | + $runParams += $imageTag |
| 181 | + $runParams += 'powershell' |
| 182 | + $runParams += '-c' |
| 183 | + $runParams += ('$PSVersionTable.PSVersion.ToString() | out-string | out-file -encoding ascii -FilePath '+$testContext.containerLogPath) |
| 184 | + |
| 185 | + $null = Invoke-Docker -Command run -Params $runParams -SuppressHostOutput |
| 186 | + if($TestContext.Type -ne 'Windows' -and $isWindows) |
| 187 | + { |
| 188 | + $null = Invoke-Docker -Command cp -Params "${volumeName}:$($testContext.containerLogPath)", $TestContext.ResolvedLogPath |
| 189 | + $null = Invoke-Docker -Command container, rm -Params $volumeName, '--force' -SuppressHostOutput |
| 190 | + } |
| 191 | + return (Get-Content -Encoding Ascii $testContext.resolvedLogPath)[0] |
| 192 | +} |
0 commit comments