Skip to content

Commit 7588be6

Browse files
committed
Add a simple test for testing the dotnet imagestreams
This requires the user to have access to and be logged into an OpenShift instance already.
1 parent 77385d9 commit 7588be6

File tree

2 files changed

+275
-1
lines changed

2 files changed

+275
-1
lines changed

install-imagestreams.sh

+6-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ show_help() {
4646
echo ""
4747
echo "Options:"
4848
echo " --o,--os Installs image streams based on this distro ('rhel7', 'rhel8', 'centos7', or 'fedora')."
49+
echo " --i,--imagestreams Installs the imagestreams from the given path."
4950
echo " --n,--namespace Namespace to add imagestreams to. Defaults to current 'oc' project."
5051
echo " Set this to 'openshift' to install globally (requires admin priviledges)."
5152
echo " --rm Remove the image streams."
@@ -148,7 +149,7 @@ do
148149
;;
149150
-o|--os)
150151
shift
151-
os="$1"
152+
os=$(echo "$1" | tr '[:upper:]' '[:lower:]')
152153
case "$os" in
153154
"centos7")
154155
imagestreams_url="https://raw.githubusercontent.com/redhat-developer/s2i-dotnetcore/master/dotnet_imagestreams_centos.json"
@@ -173,6 +174,10 @@ do
173174
;;
174175
esac
175176
;;
177+
-i|--imagestreams)
178+
shift
179+
imagestreams_url="$1"
180+
;;
176181
-u|--user)
177182
shift
178183
user="$1"

test-imagestreams.sh

+269
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,269 @@
1+
#!/bin/bash
2+
3+
# Test an imagestreams file by installing it into an OpenShift instance.
4+
#
5+
6+
set -euo pipefail
7+
8+
if [[ "${DEBUG:-}" = "true" ]]; then
9+
set -x
10+
fi
11+
12+
usage () {
13+
echo " Usage:"
14+
echo " $0"
15+
echo " IMAGE_OS=FEDORA $0"
16+
echo " IMAGE_OS=RHEL7 REGISTRY_USERNAME=<user> REGISTRY_PASSWORD=<password> $0"
17+
echo ""
18+
echo "Test that the imagestreams file is valid by installing it into an OpenShift instance."
19+
echo ""
20+
echo "Requires: jq, oc"
21+
echo ""
22+
echo "Supported environment variables:"
23+
echo ""
24+
echo "-----------------------------------------------------"
25+
echo "VERSIONS The list of expected versions."
26+
echo " Defaults to all versions. i.e '2.1 3.1'."
27+
echo ""
28+
echo "IMAGE_OS The base os image to use when building"
29+
echo " the containers."
30+
echo " Options are CENTOS7, RHEL7, RHEL8, and"
31+
echo " FEDORA."
32+
echo " Defaults to match the OS."
33+
echo ""
34+
echo "REGISTRY_USERNAME Optional username for accessing the"
35+
echo " registry. An empty value means credentials"
36+
echo " are not required."
37+
echo " Defaults to empty."
38+
echo ""
39+
echo "REGISTRY_PASSWORD Optional password for accessing the"
40+
echo " registry. An empty value means credentials"
41+
echo " are not required."
42+
echo " Defaults to empty."
43+
echo ""
44+
echo "TIMEOUT The timeout, in seconds, for applications"
45+
echo " to build and start running."
46+
echo " Defaults to 180."
47+
echo "-----------------------------------------------------"
48+
echo ""
49+
}
50+
51+
if [[ $# -ge 1 ]]; then
52+
if [[ "$1" = '--help' ]]; then
53+
usage
54+
exit 0
55+
fi
56+
usage
57+
exit 1
58+
fi
59+
60+
get_os() {
61+
if [[ -f /etc/os-release ]]; then
62+
source /etc/os-release
63+
elif [[ -f /usr/lib/os-release ]]; then
64+
source /usr/lib/os-release
65+
fi
66+
67+
if [[ "$ID" = 'rhel' ]]; then
68+
version=${VERSION_ID%%.*}
69+
echo "RHEL${version}"
70+
elif [[ "$ID" = 'fedora' ]]; then
71+
echo "FEDORA"
72+
elif [[ "$ID" = 'centos' ]]; then
73+
version=${VERSION_ID%%.*}
74+
echo "CENTOS${version}"
75+
else
76+
echo 1>&2 "error: Set IMAGE_OS to specify the base image."
77+
exit 1
78+
fi
79+
}
80+
81+
if [[ -z "$IMAGE_OS" ]]; then
82+
IMAGE_OS=$(get_os)
83+
fi
84+
IMAGE_OS=$(echo "$IMAGE_OS" | tr '[:upper:]' '[:lower:]')
85+
86+
if [[ "$IMAGE_OS" = "centos7" ]]; then
87+
VERSIONS="${VERSIONS:-2.1 3.1}"
88+
imagestreams_file_name=dotnet_imagestreams_centos.json
89+
elif [[ "$IMAGE_OS" = "fedora" ]]; then
90+
VERSIONS="${VERSIONS:-3.1}"
91+
imagestreams_file_name=dotnet_imagestreams_fedora.json
92+
elif [[ "$IMAGE_OS" = "rhel7" ]]; then
93+
VERSIONS="${VERSIONS:-2.1 3.0 3.1}"
94+
imagestreams_file_name=dotnet_imagestreams.json
95+
elif [[ "$IMAGE_OS" = "rhel8" ]]; then
96+
VERSIONS="${VERSIONS:-2.1 3.0}"
97+
imagestreams_file_name=dotnet_imagestreams_rhel8.json
98+
else
99+
echo 1>&2 "error: Unknown or unsupported OS '$IMAGE_OS'. Set the env var IMAGE_OS to override this."
100+
exit 1
101+
fi
102+
103+
if [[ -z "${TIMEOUT:-}" ]]; then
104+
TIMEOUT=180
105+
fi
106+
107+
#
108+
# Create a new project and load the imagestream to test in it
109+
#
110+
111+
echo "Testing ${imagestreams_file_name}"
112+
113+
project_name=s2i-dotnetcore-imagestream-"$RANDOM"
114+
oc new-project "$project_name" >/dev/null
115+
install_arguments=(--os "$IMAGE_OS" -i "$imagestreams_file_name")
116+
if [[ -n ${REGISTRY_USERNAME:-} ]]; then
117+
install_arguments+=(--user "$REGISTRY_USERNAME")
118+
install_arguments+=(--password "$REGISTRY_PASSWORD")
119+
fi
120+
./install-imagestreams.sh "${install_arguments[@]}"
121+
oc_output="$(oc get -o json is | jq --raw-output '.items[].metadata.name')"
122+
mapfile -t imagestreams <<< "$oc_output"
123+
124+
125+
#
126+
# Test
127+
#
128+
echo "Test: Making sure expected versions are present"
129+
for imagestream in "${imagestreams[@]}"; do
130+
oc_output="$(oc get -o json is/"${imagestream}" | jq --raw-output '.spec.tags[] | .name')"
131+
mapfile -t known_versions <<< "$oc_output"
132+
for expected_version in ${VERSIONS}; do
133+
found=0
134+
for known_version in "${known_versions[@]}"; do
135+
if [[ "$known_version" = "$expected_version" ]]; then
136+
found=1
137+
break
138+
fi
139+
done
140+
if [[ $found = 0 ]]; then
141+
echo "FAIL: version '$expected_version' not found. Found versions were:" "${known_versions[@]}"
142+
exit 5
143+
fi
144+
done
145+
echo "PASS: ${imagestream} has expected all versions"
146+
echo " Expected: ${VERSIONS}"
147+
echo " Got: ${known_versions[*]}"
148+
done
149+
150+
151+
#
152+
# Test
153+
#
154+
echo "Test: Making sure latest points to the latest version"
155+
156+
for imagestream in "${imagestreams[@]}"; do
157+
oc_output="$(oc get -o json is/"${imagestream}" | jq --raw-output '.spec.tags[] | .name, .from.name')"
158+
mapfile -t names_and_sources <<< "$oc_output"
159+
latest=0
160+
for index in $(seq 0 2 $(("${#names_and_sources[@]}"-1)) ); do
161+
name=${names_and_sources[$index]}
162+
from=${names_and_sources[$((index+1))]}
163+
164+
if [[ $name = 'latest' ]]; then
165+
if [[ $from != "$latest" ]]; then
166+
echo "FAIL: 'latest' tag points to '$from', expected '$latest'."
167+
exit 5
168+
fi
169+
else
170+
if (( $(echo "$name > $latest" | bc -l ) )); then
171+
latest=$name
172+
fi
173+
fi
174+
done
175+
echo "PASS: ${imagestream} has 'latest' pointing to the highest version: $latest"
176+
done
177+
178+
179+
#
180+
# Test
181+
#
182+
echo "Test: Making sure images resolve"
183+
184+
for imagestream in "${imagestreams[@]}"; do
185+
186+
pass=false
187+
pause_time=1
188+
for _ in $(seq 1 "$pause_time" $TIMEOUT); do
189+
if { oc -o json import-image "${imagestream}" -o json || echo '{}'; } | jq '[.status.tags[] | select(.tag == "latest")] | length == 1' --exit-status >/dev/null 2>/dev/null ; then
190+
pass=true
191+
break
192+
fi
193+
sleep "$pause_time"
194+
done
195+
196+
if [[ $pass = false ]]; then
197+
echo
198+
echo "FAIL: Unable to import-image ${imagestream}"
199+
exit 5
200+
fi
201+
202+
oc_output="$(oc -o json import-image "${imagestream}" | jq --raw-output ' .status.tags[] | .tag, .items[0].image')"
203+
mapfile -t version_and_checksums <<< "$oc_output"
204+
for index in $(seq 0 2 $(("${#version_and_checksums[@]}"-1)) ); do
205+
version=${version_and_checksums[$index]}
206+
checksum=${version_and_checksums[$((index+1))]}
207+
208+
if [[ "$checksum" != sha256:* ]]; then
209+
echo "FAIL: no checksum for $name found."
210+
exit 5
211+
fi
212+
213+
echo "PASS: $imagestream $version has checksum $checksum"
214+
done
215+
done
216+
217+
218+
#
219+
# Test
220+
#
221+
echo "Test: Sample Applications"
222+
223+
for version in ${VERSIONS}; do
224+
if [[ $version == "2.1" ]]; then
225+
sample_apps=("s2i-dotnetcore-ex")
226+
else
227+
sample_apps=("s2i-dotnetcore-ex" "s2i-dotnetcore-persistent-ex")
228+
fi
229+
230+
for application in "${sample_apps[@]}"; do
231+
application_source="dotnet:${version}~https://github.com/redhat-developer/${application}#dotnetcore-${version}"
232+
233+
echo -n "Testing $application for .NET Core $version using $application_source "
234+
235+
# Add the .NET Core application
236+
# all resources for this application automatically get the label app=$application
237+
oc new-app "$application_source" --context-dir=app >/dev/null
238+
239+
# Make the .NET Core application accessible externally and show the url
240+
oc expose service "$application" >/dev/null
241+
url=$(oc get -o json route "$application"| jq --raw-output '["http://", .spec.host] | join("")')
242+
243+
pass=false
244+
pause_time=1
245+
for _ in $(seq 1 "$pause_time" $TIMEOUT); do
246+
if [[ "$(curl -s -o /dev/null -w "%{http_code}\n" "$url")" == 200 ]]; then
247+
pass=true
248+
break
249+
fi
250+
echo -n "."
251+
sleep "$pause_time"
252+
done
253+
254+
if [[ $pass = false ]]; then
255+
echo
256+
echo "FAIL: Application was not available at ${url} within $TIMEOUT seconds"
257+
oc get all -l "app=$application"
258+
exit 5
259+
else
260+
echo " OK"
261+
fi
262+
263+
oc delete all -l "app=$application" >/dev/null
264+
done
265+
done
266+
echo "PASS: All sample applications work."
267+
268+
269+
oc delete project $project_name

0 commit comments

Comments
 (0)