Skip to content

Commit 6ce7bf2

Browse files
friismMichaelSimons
authored andcommitted
This is a set of changes that remove the host dependency from the build and test script. This is great because the tests can now be run against a remote daemon
1. use volume to move publish output between sdk and runtime containers 2. use Dockerfile to build the app instead of script
1 parent 520e7df commit 6ce7bf2

10 files changed

+169
-122
lines changed

build-and-test.ps1

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ param(
77
)
88

99
Set-StrictMode -Version Latest
10-
$ErrorActionPreference="Stop"
10+
$ErrorActionPreference = 'Stop'
1111

1212
$dockerRepo="microsoft/dotnet"
1313
$dirSeparator = [IO.Path]::DirectorySeparatorChar
@@ -19,7 +19,9 @@ else {
1919
$optionalDockerBuildArgs = "--no-cache"
2020
}
2121

22-
if ($Platform -eq "win") {
22+
$actualPlatform = docker version -f "{{ .Server.Os }}"
23+
24+
if ($actualPlatform -eq "windows") {
2325
$imageOs = "nanoserver"
2426
$tagSuffix = "-nanoserver"
2527
}
@@ -52,6 +54,6 @@ Get-ChildItem -Recurse -Filter Dockerfile |
5254

5355
popd
5456

55-
./test/run-test.ps1 -Platform $Platform
57+
./test/run-test.ps1 -UseImageCache:$UseImageCache
5658

5759
Write-Host "Tags built and tested:`n$($tags | Out-String)"

build-and-test.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
2-
set -e # Exit immediately upon failure
3-
set -o pipefail # Carry failures over pipes
2+
set -e # Exit immediately upon failure
3+
set -o pipefail # Carry failures over pipes
44

55
repo_root="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
66
docker_repo="microsoft/dotnet"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
FROM {image}
2+
3+
RUN sed -i '/<PropertyGroup>/a \ <RuntimeIdentifiers>debian.8-x64<\/RuntimeIdentifiers>' *.csproj
4+
RUN dotnet restore
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM {image}
2+
3+
RUN runtimes_section=" },\n \"runtimes\": {\n \"debian.8-x64\": {}\n }" \
4+
&& sed -i '/"type": "platform"/d' project.json \
5+
&& sed -i "s/^ }$/${runtimes_section}/" project.json
6+
RUN dotnet restore

test/Dockerfile.linux.testrunner

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Use this Dockerfile to create a Linux test-runner image
2+
# docker build -t testrunner -f .\test\Dockerfile.runner .\test\.
3+
# docker run -v /var/run/docker.sock:/var/run/docker.sock -v ${PWD}:/run --workdir /run testrunner powershell -File build-and-test.ps1 -Platform linux -UseImageCache
4+
5+
FROM ubuntu
6+
RUN apt-get update \
7+
&& apt-get install -y --no-install-recommends \
8+
ca-certificates \
9+
curl \
10+
docker.io \
11+
libicu55 \
12+
libcurl3 \
13+
libunwind8 \
14+
&& rm -rf /var/lib/apt/lists/*
15+
RUN curl -o powershell.deb -ssL https://github.com/PowerShell/PowerShell/releases/download/v6.0.0-alpha.15/powershell_6.0.0-alpha.15-1ubuntu1.16.04.1_amd64.deb \
16+
&& dpkg -i powershell.deb \
17+
&& rm -f powershell.deb

test/Dockerfile.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
FROM {image}
2+
3+
WORKDIR test
4+
RUN dotnet new {dotnetNewParam}
5+
RUN dotnet restore
6+
RUN dotnet build

test/create-run-publish-app.ps1

Lines changed: 0 additions & 35 deletions
This file was deleted.

test/create-run-publish-app.sh

Lines changed: 0 additions & 35 deletions
This file was deleted.

test/run-test.ps1

Lines changed: 92 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,43 @@
11
[cmdletbinding()]
22
param(
3-
[Parameter(Mandatory=$true)]
4-
[ValidateSet("win", "linux")]
5-
[string]$Platform
3+
[switch]$UseImageCache
64
)
75

6+
function Exec([scriptblock]$cmd, [string]$errorMessage = "Error executing command: " + $cmd) {
7+
& $cmd
8+
if ($LastExitCode -ne 0) {
9+
throw $errorMessage
10+
}
11+
}
12+
813
Set-StrictMode -Version Latest
9-
$ErrorActionPreference="Stop"
14+
$ErrorActionPreference = 'Stop'
15+
16+
if ($UseImageCache) {
17+
$optionalDockerBuildArgs=""
18+
}
19+
else {
20+
$optionalDockerBuildArgs = "--no-cache"
21+
}
1022

1123
$dockerRepo="microsoft/dotnet"
1224
$dirSeparator = [IO.Path]::DirectorySeparatorChar
1325
$repoRoot = Split-Path -Parent $PSScriptRoot
26+
$platform = docker version -f "{{ .Server.Os }}"
1427

15-
if ($Platform -eq "win") {
28+
if ($Platform -eq "windows") {
1629
$imageOs = "nanoserver"
1730
$tagSuffix = "-nanoserver"
1831
$testScriptSuffix = "ps1"
1932
$containerRoot = "C:\"
20-
$sdkRunArg = "powershell"
33+
$platformDirSeparator = '\'
2134
}
2235
else {
2336
$imageOs = "debian"
2437
$tagSuffix = ""
2538
$testScriptSuffix = "sh"
2639
$containerRoot = "/"
27-
$sdkRunArg = ""
40+
$platformDirSeparator = '/'
2841
}
2942

3043
pushd $repoRoot
@@ -47,41 +60,82 @@ Get-ChildItem -Recurse -Filter Dockerfile |
4760
TrimEnd("-sdk")
4861

4962
$timeStamp = Get-Date -Format FileDateTime
50-
$appName="app$timeStamp"
51-
$appDir="$repoRoot$dirSeparator.test-assets$dirSeparator$appName"
52-
53-
New-Item $appDir -type directory | Out-Null
54-
55-
Write-Host "----- Testing $fullSdkTag -----"
56-
docker run -t --rm `
57-
-v "${appDir}:${containerRoot}${appName}" `
58-
-v "${repoRoot}${dirSeparator}test:${containerRoot}test" `
59-
--name "sdk-test-$appName" `
60-
$fullSdkTag $sdkRunArg "${containerRoot}test${dirSeparator}create-run-publish-app.${testScriptSuffix}" "${containerRoot}${appName}" $sdkTag
61-
if (-NOT $?) {
62-
throw "Testing $fullSdkTag failed"
63+
$appName="app$timeStamp".ToLower()
64+
65+
$buildImage = "sdk-build-$appName"
66+
$dotnetNewParam = ""
67+
if ($sdkTag -like "*1.1-sdk-msbuild*") {
68+
$dotnetNewParam = "-t Console1.1"
6369
}
70+
$dockerFilesPath = "${repoRoot}${dirSeparator}test${dirSeparator}"
6471

65-
Write-Host "----- Testing $baseTag-runtime$tagSuffix with $sdkTag app -----"
66-
docker run -t --rm `
67-
-v "${appDir}:${containerRoot}${appName}" `
68-
--name "runtime-test-$appName" `
69-
--entrypoint dotnet `
70-
"$baseTag-runtime$tagSuffix" `
71-
"${containerRoot}${appName}${dirSeparator}publish${dirSeparator}framework-dependent${dirSeparator}${appName}.dll"
72-
if (-NOT $?) {
73-
throw "Testing $baseTag-runtime$tagSuffix failed"
72+
Write-Host "----- Testing create, restore and build with $fullSdkTag with image $buildImage -----"
73+
exec { (Get-Content ${dockerFilesPath}Dockerfile.test).Replace("{image}", $fullSdkTag).Replace("{dotnetNewParam}", $dotnetNewParam) `
74+
| docker build $optionalDockerBuildArgs -t $buildImage -
7475
}
7576

76-
if ($Platform -eq "linux") {
77-
Write-Host "----- Testing $baseTag-runtime-deps$tagSuffix with $sdkTag app -----"
78-
docker run -t --rm `
79-
-v "${appDir}:${containerRoot}${appName}" `
80-
--name "runtime-deps-test-$appName" `
81-
--entrypoint "${containerRoot}${appName}${dirSeparator}publish${dirSeparator}self-contained${dirSeparator}${appName}" `
82-
"$baseTag-runtime-deps$tagSuffix"
83-
if (-NOT $?) {
84-
throw "Testing $baseTag-runtime-deps$tagSuffix failed"
77+
Write-Host "----- Running app built on $fullSdkTag -----"
78+
exec { docker run --rm $buildImage dotnet run }
79+
80+
Try {
81+
$framworkDepVol = "framework-dep-publish-$appName"
82+
Write-Host "----- Publishing framework-dependant app built on $fullSdkTag to volume $framworkDepVol -----"
83+
exec { docker run --rm -v ${framworkDepVol}:"${containerRoot}volume" $buildImage dotnet publish -o ${containerRoot}volume }
84+
85+
Write-Host "----- Testing on $baseTag-runtime$tagSuffix with $sdkTag framework-dependent app -----"
86+
exec { docker run --rm `
87+
-v ${framworkDepVol}":${containerRoot}volume" `
88+
--entrypoint dotnet `
89+
"$baseTag-runtime$tagSuffix" `
90+
"${containerRoot}volume${platformDirSeparator}test.dll"
91+
}
92+
}
93+
Finally {
94+
docker volume rm $framworkDepVol
95+
}
96+
97+
if ($platform -eq "linux") {
98+
if ($sdkTag -like "*projectjson*") {
99+
$projectType = "projectjson"
100+
}
101+
else {
102+
$projectType = "msbuild"
103+
$publishArg = ''
104+
}
105+
106+
$selfContainedImage = "self-contained-build-${buildImage}"
107+
Write-Host "----- Creating publish-image for self-contained app built on $fullSdkTag -----"
108+
exec {
109+
(Get-Content ${dockerFilesPath}Dockerfile.linux.${projectType}.publish).Replace("{image}", $buildImage) `
110+
| docker build $optionalDockerBuildArgs -t $selfContainedImage -
111+
}
112+
113+
Try {
114+
$selfContainedVol = "self-contained-publish-$appName"
115+
Write-Host "----- Publishing self-contained published app built on $fullSdkTag to volume $selfContainedVol using image $selfContainedImage -----"
116+
# REMARK: This structure seems to be required because of some PowerShell parameter passing weirdness
117+
if ($projectType -eq "projectjson") {
118+
exec { docker run --rm `
119+
-v ${selfContainedVol}":${containerRoot}volume" `
120+
--entrypoint dotnet `
121+
$selfContainedImage `
122+
publish -o ${containerRoot}volume
123+
}
124+
}
125+
else {
126+
exec { docker run --rm `
127+
-v ${selfContainedVol}":${containerRoot}volume" `
128+
--entrypoint dotnet `
129+
$selfContainedImage `
130+
publish -r debian.8-x64 -o ${containerRoot}volume
131+
}
132+
}
133+
134+
Write-Host "----- Testing $baseTag-runtime-deps$tagSuffix with $sdkTag self-contained app -----"
135+
exec { docker run -t --rm -v ${selfContainedVol}":${containerRoot}volume" ${baseTag}-runtime-deps$tagSuffix ${containerRoot}volume${platformDirSeparator}test }
136+
}
137+
Finally {
138+
docker volume rm $selfContainedVol
85139
}
86140
}
87141
}

test/run-test.sh

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
2-
set -e # Exit immediately upon failure
3-
set -o pipefail # Carry failures over pipes
2+
set -e # Exit immediately upon failure
3+
set -o pipefail # Carry failures over pipes
44

55
docker_repo="microsoft/dotnet"
66
repo_root="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )/.."
@@ -16,19 +16,47 @@ for sdk_tag in $( find . -path './.*' -prune -o -path '*/debian/sdk/*/Dockerfile
1616
full_sdk_tag="${docker_repo}:${sdk_tag}"
1717

1818
base_tag="$( echo "${full_sdk_tag}" | sed -e 's/-sdk//' -e 's/-msbuild//' -e 's/-projectjson//' )"
19-
2019
app_name="app$(date +%s)"
21-
app_dir="${repo_root}/.test-assets/${app_name}"
22-
mkdir -p "${app_dir}"
2320

24-
echo "----- Testing ${full_sdk_tag} -----"
25-
docker run -t ${optional_docker_run_args} -v "${app_dir}:/${app_name}" -v "${repo_root}/test:/test" --name "sdk-test-${app_name}" --entrypoint /test/create-run-publish-app.sh "${full_sdk_tag}" "${app_name}" "${sdk_tag}"
21+
dotnetNewParam=""
22+
echo "DEBUG sdk_tag: $sdk_tag"
23+
if [[ $sdk_tag == "1.1-sdk-msbuild" ]]; then
24+
echo "DEBUG setting custom console stuff"
25+
dotnetNewParam="-t Console1.1"
26+
fi
27+
28+
buildImage="build-image-${sdk_tag}"
29+
echo "----- Testing, create, restore and build for ${full_sdk_tag} -----"
30+
cat test/Dockerfile.test | sed -e "s#{image}#$full_sdk_tag#g" -e "s/{dotnetNewParam}/$dotnetNewParam/g" | docker build --rm -t ${buildImage} -
31+
32+
echo "----- Testing running ${full_sdk_tag} -----"
33+
docker run $optional_docker_run_args -t ${optional_docker_run_args} ${buildImage} dotnet run
34+
35+
echo "----- Testing publishing framework dependent app built on ${full_sdk_tag} -----"
36+
frameworkDepVol="framework-dep-publish-$app_name"
37+
docker run $optional_docker_run_args -v ${frameworkDepVol}:/volume $buildImage dotnet publish -o /volume
38+
echo "----- Testing ${base_tag}-runtime with ${sdk_tag} app -----"
39+
docker run $optional_docker_run_args -v ${frameworkDepVol}:/volume --entrypoint dotnet "${base_tag}-runtime" /volume/test.dll
40+
41+
if [[ $sdk_tag == *"projectjson"* ]]; then
42+
projectType="projectjson"
43+
publishArg=""
44+
else
45+
projectType="msbuild"
46+
publishArg="-r debian.8-x64"
47+
fi
48+
49+
selfContainedImage="self-contained-build-${buildImage}"
50+
selfContainedVol="self-contained-publish-$app_name"
51+
echo "----- Testing publishing self-contained app with ${full_sdk_tag} built on $buildImage -----"
52+
cat test/Dockerfile.linux.${projectType}.publish | sed -e "s#{image}#$buildImage#g" | docker build -t $selfContainedImage -
53+
docker run $optional_docker_run_args -v ${selfContainedVol}:/volume $selfContainedImage dotnet publish $publishArg -o /volume
2654

2755
echo "----- Testing ${base_tag}-runtime with ${sdk_tag} app -----"
28-
docker run -t ${optional_docker_run_args} -v "${app_dir}:/${app_name}" --name "runtime-test-${app_name}" --entrypoint dotnet "${base_tag}-runtime" "/${app_name}/publish/framework-dependent/${app_name}.dll"
56+
docker run ${optional_docker_run_args} -v ${selfContainedVol}:/volume --entrypoint dotnet "${base_tag}-runtime" /volume/test.dll
2957

3058
echo "----- Testing ${base_tag}-runtime-deps with ${sdk_tag} app -----"
31-
docker run -t ${optional_docker_run_args} -v "${app_dir}:/${app_name}" --name "runtime-deps-test-${app_name}" --entrypoint "/${app_name}/publish/self-contained/${app_name}" "${base_tag}-runtime-deps"
59+
docker run ${optional_docker_run_args} -v ${selfContainedVol}:/volume --entrypoint "/volume/test" "${base_tag}-runtime-deps"
3260
done
3361

3462
popd > /dev/null

0 commit comments

Comments
 (0)