Skip to content

Commit 73a2e41

Browse files
authored
Merge pull request #566 from anti-social/refactor-image-tests
Refactor image tests
2 parents a00314a + 1468196 commit 73a2e41

File tree

3 files changed

+96
-94
lines changed

3 files changed

+96
-94
lines changed

tests/image.bats

+41-80
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,71 @@
11
setup() {
22
cd /work/tests/image
3-
mkdir -p images
4-
mkdir -p nginx/logs
53
}
64

75
@test "image: pack" {
8-
rm -rf images/image.*
6+
mkdir -p .vagga/images
97

10-
vagga _pack_image alpine -f images/image.tar
11-
vagga _pack_image alpine -f images/image.tar.gz -z
12-
vagga _pack_image alpine -f images/image.tar.bz2 -j
13-
vagga _pack_image alpine -f images/image.tar.xz -J
14-
run file images/image.tar
8+
vagga _pack_image image -f .vagga/images/image.tar
9+
vagga _pack_image image -f .vagga/images/image.tar.gz -z
10+
vagga _pack_image image -f .vagga/images/image.tar.bz2 -j
11+
vagga _pack_image image -f .vagga/images/image.tar.xz -J
12+
run file .vagga/images/image.tar
1513
printf "%s\n" "${lines[@]}"
14+
image_tar_hashsum=$(sha256sum .vagga/images/image.tar | cut -d " " -f 1)
1615
[[ $output = *"image.tar: POSIX tar archive (GNU)"* ]]
17-
run file images/image.tar.gz
16+
run tar -tf .vagga/images/image.tar
17+
[[ $output = *"/var/lib/question.txt"* ]]
18+
run file .vagga/images/image.tar.gz
1819
printf "%s\n" "${lines[@]}"
20+
image_targz_hashsum=$(sha256sum .vagga/images/image.tar.gz | cut -d " " -f 1)
1921
[[ $output = *"image.tar.gz: gzip compressed data"* ]]
20-
run file images/image.tar.bz2
22+
run file .vagga/images/image.tar.bz2
2123
printf "%s\n" "${lines[@]}"
2224
[[ $output = *"image.tar.bz2: bzip2 compressed data"* ]]
23-
run file images/image.tar.xz
25+
run file .vagga/images/image.tar.xz
2426
printf "%s\n" "${lines[@]}"
2527
[[ $output = *"image.tar.xz: XZ compressed data"* ]]
2628

27-
vagga _pack_image alpine > images/image.tar
28-
vagga _pack_image alpine -z > images/image.tgz
29-
run file images/image.tar
29+
vagga _pack_image image > .vagga/images/image-stdout.tar
30+
vagga _pack_image image -z > .vagga/images/image-stdout.tgz
31+
run file .vagga/images/image-stdout.tar
3032
printf "%s\n" "${lines[@]}"
31-
[[ $output = *"image.tar: POSIX tar archive (GNU)"* ]]
32-
run file images/image.tgz
33+
[[ $output = *"image-stdout.tar: POSIX tar archive (GNU)"* ]]
34+
[[ $(sha256sum .vagga/images/image-stdout.tar | cut -d " " -f 1) = $image_tar_hashsum ]]
35+
run file .vagga/images/image-stdout.tgz
3336
printf "%s\n" "${lines[@]}"
34-
[[ $output = *"image.tgz: gzip compressed data"* ]]
35-
}
36-
37-
@test "image: missing" {
38-
rm -rf .vagga/.roots/alpine.375a2970
39-
40-
run vagga _build alpine
41-
printf "%s\n" "${lines[@]}"
42-
[[ $status = 0 ]]
43-
link=$(readlink .vagga/alpine)
44-
[[ $link = ".roots/alpine.375a2970/root" ]]
45-
[[ $output = *"Will clean and build it locally"* ]]
37+
[[ $output = *"image-stdout.tgz: gzip compressed data"* ]]
38+
[[ $(sha256sum .vagga/images/image-stdout.tgz | cut -d " " -f 1) = $image_targz_hashsum ]]
4639
}
4740

48-
# TODO: Move the test inside a nested container
4941
@test "image: push & pull" {
50-
container_dir="alpine.375a2970"
51-
image_name="${container_dir}.tar.xz"
42+
container_name="image"
43+
container_dir="$container_name.fb9b6868"
44+
image_file_name="${container_dir}.tar.xz"
5245

53-
rm -rf /work/tmp/cache/downloads/*-${image_name}
46+
vagga _build nginx
47+
vagga _build test-pull
5448

55-
run vagga _build alpine
56-
printf "%s\n" "${lines[@]}"
49+
rm -rf .vagga/$container_name .vagga/.roots/$container_name.*
50+
run vagga _build $container_name
5751
[[ $status = 0 ]]
58-
link=$(readlink .vagga/alpine)
59-
[[ $link = ".roots/${container_dir}/root" ]]
52+
[[ $output = *"Will clean and build it locally"* ]]
6053

61-
rm -rf nginx/nginx.pid
62-
rm -rf nginx/logs/*
63-
vagga _build nginx
64-
vagga nginx > /dev/null 2>&1 &
65-
nginx_pid=$!
66-
sleep 2
54+
# Pack image to cache capsule's dependencies so then
55+
# we will be able to run test inside an isolated network environment
56+
run vagga pack-image
57+
[[ $status = 0 ]]
6758

68-
run vagga _push_image alpine
69-
printf "%s\n" "${lines[@]}"
59+
run vagga --isolate-network test-push-and-pull $container_name
60+
[[ $status = 0 ]]
7061

71-
run tail -n 1 nginx/logs/access.log
72-
printf "%s\n" "${lines[@]}"
62+
run cat .vagga/.volumes/nginx-logs/access.log
7363
access_log_output=${output}
64+
[[ ${access_log_output} = *"PUT /upload/images/${image_file_name} HTTP/1.1\" 201"* ]]
65+
[[ ${access_log_output} = *"GET /images/${image_file_name} HTTP/1.1\" 200"* ]]
7466

75-
# test downloading from cache
76-
rm -rf .vagga/alpine
77-
rm -rf .vagga/.roots/${container_dir}
78-
run vagga _build alpine
79-
printf "%s\n" "${lines[@]}"
80-
build_status=${status}
81-
build_link=$(readlink .vagga/alpine)
82-
83-
run vagga _run alpine sh -c "echo '100*24' | bc"
84-
printf "%s\n" "${lines[@]}"
85-
run_status=${status}
86-
run_lines=${lines}
87-
88-
kill -TERM "${nginx_pid}"
89-
90-
[[ ${access_log_output} = *"PUT /upload/images/${image_name} HTTP/1.1\" 201"* ]]
67+
[[ $(readlink .vagga/$container_name) = ".roots/${container_dir}/root" ]]
9168

92-
[[ ${build_status} = 0 ]]
93-
[[ ${build_link} = ".roots/${container_dir}/root" ]]
94-
[[ ${run_status} -eq 0 ]]
95-
[[ ${run_lines[${#run_lines[@]}-1]} = "2400" ]]
96-
97-
run tail -n 1 nginx/logs/access.log
98-
printf "%s\n" "${lines[@]}"
99-
[[ ${output} = *"GET /images/${image_name} HTTP/1.1\" 200"* ]]
100-
101-
# check there is no image in local cache
102-
[[ $(ls -1 /work/tmp/cache/downloads/*-${image_name} | wc -l) = 0 ]]
103-
104-
run vagga _run alpine stat -c "%U" /var/lib/nobody
105-
printf "%s\n" "${lines[@]}"
106-
[[ $output = "nobody" ]]
107-
run vagga _run alpine stat -c "%U" /var/lib/someone
108-
printf "%s\n" "${lines[@]}"
109-
[[ $output = "nobody" ]]
69+
run cat .vagga/image/var/lib/question.txt
70+
[[ $output = "To be or not to be?" ]]
11071
}

tests/image/vagga.yaml

+54-12
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
11
containers:
2-
alpine:
3-
image-cache-url: http://localhost:10080/images/${container_name}.${short_hash}.tar.xz
2+
image:
3+
image-cache-url: http://localhost:8080/images/${container_name}.${short_hash}.tar.xz
44
setup:
5-
- !Alpine v3.4
6-
- !Install [bc]
7-
- !Sh mkdir -p /var/lib/nobody
5+
- !EnsureDir /var/lib
86
- !Text
9-
/var/lib/someone: Who am I?
10-
- !Sh chown -R nobody:nobody /var/lib/nobody /var/lib/someone
7+
/var/lib/question.txt: To be or not to be?
8+
9+
test-pull:
10+
volumes:
11+
/usr/lib/vagga: !VaggaBin
12+
environ:
13+
PATH: /usr/lib/vagga:/sbin:/bin:/usr/sbin:/usr/bin
14+
setup:
15+
- !Alpine v3.15
16+
- !Install [gawk, netcat-openbsd, curl]
17+
- !EnsureDir /usr/lib/vagga
1118

1219
nginx:
1320
volumes:
1421
/var/lib/nginx/client_temp: !Tmpfs {}
1522
/var/lib/nginx/proxy_temp: !Tmpfs {}
1623
/var/lib/nginx/tmp: !Tmpfs {size: 10M}
17-
/var/log/nginx: !BindRW /work/nginx/logs
24+
/var/log/nginx: !Persistent nginx-logs
25+
/var/www/images: !Persistent images
1826
/work/images: !Tmpfs {size: 100M}
1927
setup:
2028
- !Alpine v3.4
@@ -25,6 +33,7 @@ containers:
2533
- !EnsureDir /var/lib/nginx/proxy_temp
2634
- !EnsureDir /var/lib/nginx/tmp
2735
- !EnsureDir /var/log/nginx
36+
- !EnsureDir /var/www/images
2837
- !Text
2938
/etc/nginx/nginx.conf: |
3039
daemon off;
@@ -46,15 +55,15 @@ containers:
4655
proxy_temp_path /var/lib/nginx/proxy_temp 1 2;
4756
4857
server {
49-
listen 10080 default_server;
58+
listen 8080 default_server;
5059
5160
access_log /var/log/nginx/access.log;
5261
5362
location /upload/images/ {
54-
alias /work/images/;
63+
alias /var/www/images/;
5564
5665
client_max_body_size 1g;
57-
client_body_temp_path /work/images/;
66+
client_body_temp_path /var/www/images/;
5867
5968
dav_methods PUT DELETE MKCOL COPY MOVE;
6069
@@ -63,12 +72,45 @@ containers:
6372
}
6473
6574
location /images/ {
66-
alias /work/images/;
75+
alias /var/www/images/;
6776
}
6877
}
6978
}
7079
7180
commands:
81+
pack-image: !Command
82+
container: test-pull
83+
description: Cache xz capsule dependency
84+
run: |
85+
vagga _pack_image image -J > /dev/null
86+
87+
test-push-and-pull: !Supervise
88+
options: |
89+
Usage: vagga test-push-and-pull [options] <container>
90+
91+
Options:
92+
<container> Target container
93+
children:
94+
nginx: !Command
95+
container: nginx
96+
run: nginx
97+
test: !Command
98+
container: test-pull
99+
environ:
100+
VAGGA_SETTINGS: |
101+
push-image-script: 'curl -T ${image_path} http://localhost:8080/upload/images/${container_name}.${short_hash}.tar.xz'
102+
run: |
103+
for i in $(awk 'BEGIN { for ( i=0; i<60; i++ ) { print i; } }'); do
104+
sleep 0.5
105+
if nc -vz localhost 8080; then
106+
break
107+
fi
108+
done
109+
110+
vagga _push_image $VAGGAOPT_CONTAINER
111+
rm -rf .vagga/$VAGGAOPT_CONTAINER .vagga/.roots/$VAGGAOPT_CONTAINER.*
112+
vagga _build $VAGGAOPT_CONTAINER
113+
72114
nginx: !Command
73115
description: Run nginx
74116
container: nginx

vagga.yaml

+1-2
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,7 @@ commands:
205205
export VAGGA_SETTINGS="
206206
cache-dir: /work/tmp/cache
207207
ubuntu-mirror: $UBUNTU_MIRROR
208-
alpine-mirror: $ALPINE_MIRROR
209-
push-image-script: "'curl -T ${image_path} http://localhost:10080/upload/images/${container_name}.${short_hash}.tar.xz'
208+
alpine-mirror: $ALPINE_MIRROR"
210209
if [ -n "$*" ]; then
211210
bats "$@"
212211
else

0 commit comments

Comments
 (0)