Skip to content

Commit

Permalink
chapter 5 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanHohn committed Jan 13, 2024
1 parent b1c1af2 commit b97e8ff
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 0 deletions.
5 changes: 5 additions & 0 deletions chapter-05/Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ Vagrant.configure("2") do |config|
ansible.playbook = "playbook.yaml"
ansible.groups = groups
end
host.vm.provision "test", type: "ansible", run: "never" do |ansible|
ansible.limit = "all"
ansible.playbook = "test.yaml"
ansible.groups = groups
end
end
end
end
Expand Down
12 changes: 12 additions & 0 deletions chapter-05/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
- hosts: remote
become: yes
pre_tasks:
- name: install tests
ansible.builtin.copy:
src: tests
dest: /opt
owner: root
group: root
roles:
- test
13 changes: 13 additions & 0 deletions chapter-05/tests/basic.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# bats file_tags=host01

setup() {
BATS_LIB_PATH=/usr/local/lib/node_modules
bats_load_library bats-support
bats_load_library bats-assert
bats_require_minimum_version 1.5.0
}

@test 'available commands' {
run -0 which docker
run -0 which skopeo
}
25 changes: 25 additions & 0 deletions chapter-05/tests/build.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# bats file_tags=host01

setup() {
BATS_LIB_PATH=/usr/local/lib/node_modules
bats_load_library bats-support
bats_load_library bats-assert
bats_require_minimum_version 1.5.0
}

@test 'build docker image' {
run -0 /bin/bash -c '\
cd /opt/hello
docker build -t hello .'
run -0 docker images
assert_output --partial 'hello'
docker run --name hello -d -p 8080:80 hello
retry -d 1 -t 30 -- curl http://localhost:8080/
run -0 curl http://localhost:8080/
assert_output --partial 'Hello World!'
}

teardown() {
docker rm --force hello
docker rmi --force hello
}
29 changes: 29 additions & 0 deletions chapter-05/tests/nginx.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# bats file_tags=host01

setup() {
BATS_LIB_PATH=/usr/local/lib/node_modules
bats_load_library bats-support
bats_load_library bats-assert
bats_require_minimum_version 1.5.0
}

@test 'nginx' {
docker pull nginx
run -0 docker images
assert_output --partial 'nginx'
docker run --name nginx -d nginx
run -0 docker exec nginx /bin/sh -c 'ldd $(which nginx)'
assert_output --partial 'libc'
JQ_QUERY='.[0].SizeRw'
run -0 /bin/bash -c "docker inspect -s nginx | jq ${JQ_QUERY}"
assert_output --regexp '^[0-9]+$'
JQ_QUERY='.[0].GraphDriver.Data.MergedDir'
ROOT=$(docker inspect nginx | jq -r "${JQ_QUERY}")
ls ${ROOT}
run -0 mount
assert_output --partial 'merged'
}

teardown() {
docker rm --force nginx
}
29 changes: 29 additions & 0 deletions chapter-05/tests/overlay.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# bats file_tags=host01

setup() {
BATS_LIB_PATH=/usr/local/lib/node_modules
bats_load_library bats-support
bats_load_library bats-assert
bats_require_minimum_version 1.5.0
}

@test 'overlay filesystem' {
mkdir /tmp/{lower,upper,work,mount}
echo "hello1" > /tmp/lower/hello1
echo "hello2" > /tmp/upper/hello2
mount -t overlay -o rw,lowerdir=/tmp/lower,upperdir=/tmp/upper,workdir=/tmp/work overlay /tmp/mount
run -0 cat /tmp/mount/hello1
assert_output --partial 'hello1'
run -0 cat /tmp/mount/hello2
assert_output --partial 'hello2'
echo "hello3" > /tmp/mount/hello3
run -0 ls /tmp/lower
refute_output --partial 'hello3'
run -0 ls /tmp/upper
assert_output --partial 'hello3'
}

teardown() {
umount -f /tmp/mount
rm -fr /tmp/{lower,upper,work,mount}
}
20 changes: 20 additions & 0 deletions chapter-05/tests/registry.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# bats file_tags=host01

setup() {
BATS_LIB_PATH=/usr/local/lib/node_modules
bats_load_library bats-support
bats_load_library bats-assert
bats_require_minimum_version 1.5.0
}

@test 'local registry' {
docker pull busybox
docker tag busybox registry.local/busybox
docker push registry.local/busybox
docker pull registry.local/busybox
}

teardown() {
docker rmi --force busybox
docker rmi --force registry.local/busybox
}

0 comments on commit b97e8ff

Please sign in to comment.