forked from JeetShetty/ansible-role-greenpithumb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build
executable file
·94 lines (75 loc) · 2.48 KB
/
build
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/bin/sh
# Exit immediately on error
set -e
cd tests/
cp "Dockerfile-${distro}" Dockerfile
# Build container
container_tag=${distro}
docker build \
--rm=true \
--tag=${container_tag} .
# Run container in detached state
id_file=$(mktemp)
docker run \
--detach \
--volume="${PWD}":/etc/ansible/roles/role_under_test:ro \
${run_opts} \
${container_tag} ${init} > "${id_file}"
container_id="$(cat $id_file)"
echo "Container ID:" "${container_id}"
printf "${container_id} ansible_connection=docker\n" > hosts
# Print ansible version.
ansible --version
ansible-galaxy install -r requirements.yml
# Create a symlink to the role under test (removing previous if it existed)
mkdir -p roles
rm roles/role_under_test || true
ln -s ../../ roles/role_under_test
# Ansible syntax check.
ansible-playbook test.yml --syntax-check
# Check that role installs successfully.
ansible-playbook test.yml
# Test role idempotence.
idempotence_file=$(mktemp)
ansible-playbook test.yml \
| tee "${idempotence_file}"
grep -q "changed=0.*failed=0" "${idempotence_file}" && \
(echo "Idempotence test: pass" && exit 0) || \
(echo "Idempotence test: fail" && exit 1)
# Get container's IP address.
print_ip_python=$(echo "import sys, json;
print json.load(sys.stdin)[0]['NetworkSettings']['IPAddress']")
container_ip=$(docker inspect "${container_id}" | \
python -c "${print_ip_python}")
echo "Container IP Address:" "${container_ip}"
# Check that GreenPiThumb Frontend is serving.
curl -s "http://${container_ip}/" \
| grep "GreenPiThumb" && \
(echo "Landing page test: pass" && exit 0) || \
(echo "Landing page test: fail" && exit 1)
# Make sure application files are accessible
curl -s "http://${container_ip}/app/app.js" -o /dev/null && \
(echo "Application file test: pass") || \
(echo "Application file test: fail" && exit 1)
# Make sure vendor files are accessible
curl -s "http://${container_ip}/vendor/bootstrap/bootstrap.min.css" \
-o /dev/null && \
(echo "Vendor file test: pass") || \
(echo "Vendor file test: fail" && exit 1)
# Check that GreenPiThumb Frontend returns 404 on non-existent HTML files.
response_code=$( \
curl -s -o /dev/null -w "%{http_code}" \
"http://${container_ip}/non-existent-file.html")
if [ "${response_code}" -eq 404 ]; then
echo "404 test: pass"
else
echo "404 test: fail" && exit 1
fi
# Check role for linter errors.
ansible-lint test.yml -t role_under_test
# Clean up
docker stop "${container_id}"
docker rm "${container_id}"
rm -rf roles
rm Dockerfile
rm hosts