-
Notifications
You must be signed in to change notification settings - Fork 20
/
molecule_test.sh
executable file
·91 lines (78 loc) · 3.2 KB
/
molecule_test.sh
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
#!/bin/bash
# Execute molecule testing for each role that contains molecule scenario
set -eo pipefail
if [[ ! -n "${TEST_HOST}" && ! -n "${TEST_SSH_KEY}" ]]; then
echo "Some of the scenarious require the following env variables:"
echo -e "TEST_HOST\nTEST_SSH_KEY\n"
echo "Set the env variables and rerun the test."
echo "For explanation of the env variables, look for the openstack_task molecule role."
exit 1
fi
runs=0
failed_runs=0
declare -a tested_roles
declare -a failed_roles
logs_dir=$(pwd)/molecule_logs
rm -rf $logs_dir
mkdir $logs_dir
# Many of the ansible-nfv roles tests require access to the working Openstack environment.
# As the gate that is used is fully virtual and not accessible from outside, we are
# using tripleo_inventory role to provide that access.
# In order to minimize the time of tests execution and not to rerun the tripleo_inventory
# generation for each role, it will be done once when molecule_test.sh script executed.
# When a role tested separately, it will generate the inventory by itself.
export ANSIBLE_VERBOSITY=4
# Installing required collections
echo installing requirements
# Adding this to allow using external collection for newer ansible versions
# this variable set the path to the collection directory
export ANSIBLE_COLLECTIONS_PATH=/tmp/.collectinos
export ANSIBLE_COLLECTIONS_PATHS=/tmp/.collectinos
rm -rf ${ANSIBLE_COLLECTIONS_PATH}
rm -rf ${HOME}/.ansible/collections
ansible-galaxy collection install -r requirements.yaml -p ${ANSIBLE_COLLECTIONS_PATH}
# Install docker collection because it is required to execute molecule tests
ansible-galaxy collection install community.docker -p ${ANSIBLE_COLLECTIONS_PATH}
echo "Generating the inventory for the roles."
export MOLECULE_INVENTORY_PATH=$(pwd)/inventory
ANSIBLE_COLLECTIONS_PATHS=$ANSIBLE_COLLECTIONS_PATH ansible-playbook playbooks/tripleo/post_install/tripleo_inventory.yml \
-e host="${TEST_HOST}" -e ssh_key="${TEST_SSH_KEY}" -e setup_type=virt
export TEST_INV_GENERATED=true
# This is a workaround because molecule for some reason does not parse custom COLLECTION path
ln -s ${ANSIBLE_COLLECTIONS_PATH} ${HOME}/.ansible/collections
molecules="$(find roles/ -name molecule -type d)"
for molecule in $molecules; do
pushd $(dirname $molecule)
export ANSIBLE_LOG_PATH=$logs_dir/$(basename $(dirname $molecule)).log
if ! molecule --debug test --all; then
failed_runs=$((failed_runs + 1))
failed_roles+=($(dirname $molecule))
fi
runs=$((runs + 1))
tested_roles+=($(dirname $molecule))
popd
done
echo -e "\n################"
echo "Tests results:"
if [[ $failed_runs -ne 0 ]]; then
echo -e "\nFailed $failed_runs/$runs molecule tests"
echo -e "\nTested roles:"
for role in "${tested_roles[@]}"; do
echo "- $role"
done
echo -e "\nFailed molecule roles:"
for role in "${failed_roles[@]}"; do
echo "- $role"
done
exit 1
fi
echo -e "\nRan sucesfully $runs molecule tests"
echo -e "\nTested roles:"
for role in "${tested_roles[@]}"; do
echo "- $role"
done
# Cleaning worker from installed collections
echo removing requirements installed for test
rm -rf ${ANSIBLE_COLLECTIONS_PATH}
rm -rf ${HOME}/.ansible/collections
echo -e "################\n"