-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-tests
executable file
·98 lines (80 loc) · 2.95 KB
/
run-tests
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
95
96
97
98
#!/bin/bash
set -o nounset
set -o errexit
set -o pipefail
# The script expects a DEVICE_SECRETS environment variable that looks like config/device_secrets_sample.json.
unexpected_exit()
{
>&2 echo "Error: Unexpected exit (code $?)!"
trap - EXIT
revert_sudoers
exit 1
}
revert_sudoers()
{
echo "Reverting sudo password removal on ${DEVICE_IP}."
ssh -F "${SSH_CONFIG}" "${DEVICE_USER}@${DEVICE_IP}" 'sudo rm /etc/sudoers.d/pytest_no_passwd' > /dev/null 2>&1
}
if [ "$#" -ne 2 ]
then
>&2 echo "Error: Illegal number of arguments!"
>&2 echo "Usage: run-tests DEVICE_ID EDI_PROJECT_CONFIGURATION"
>&2 echo "Example: run-tests 3fb4632b-96b9-475d-ac89-02255bd15b6f edi-pi"
exit 1
fi
DEVICE_SECRETS="${DEVICE_SECRETS:=}"
DEVICE_ID=$1
EDI_PROJECT_CONFIGURATION=$2
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
SSH_CONFIG="${SCRIPT_DIR}/config/no_host_key_checking"
if [[ -z "${DEVICE_SECRETS}" ]]
then
>&2 echo "Error: Unable to retrieve device secrets (DEVICE_SECRETS) from environment!"
exit 1
fi
EDI_PROJECT_CONFIGURATION_JSON=$(echo "${EDI_PROJECT_CONFIGURATION}" | tr - _)
if ! DEVICE_USER=$(echo "${DEVICE_SECRETS}" | jq -r -e ."${EDI_PROJECT_CONFIGURATION_JSON}".user)
then
>&2 echo "Error: Unable to retrieve device user for configuration ${EDI_PROJECT_CONFIGURATION_JSON}!"
exit 1
fi
if ! DEVICE_PASSWORD=$(echo "${DEVICE_SECRETS}" | jq -r -e ."${EDI_PROJECT_CONFIGURATION_JSON}".password)
then
>&2 echo "Error: Unable to retrieve device password for configuration ${EDI_PROJECT_CONFIGURATION_JSON}!"
exit 1
fi
if ! DEVICE_INTERFACES=$(echo "${DEVICE_SECRETS}" | jq -r -e ."${EDI_PROJECT_CONFIGURATION_JSON}".interface)
then
>&2 echo "Error: Unable to retrieve device interface(s) for configuration ${EDI_PROJECT_CONFIGURATION_JSON}!"
exit 1
fi
echo "Retrieving IP address of ${DEVICE_ID}".
DEVICE_IP=""
IFS=' ' read -r -a INTERFACE_ARRAY <<< "${DEVICE_INTERFACES}"
for interface in "${INTERFACE_ARRAY[@]}"
do
if DEVICE_IP=$("${SCRIPT_DIR}/mender-api" query "${DEVICE_ID}" "${interface}")
then
DEVICE_IP="${DEVICE_IP%/*}"
break;
fi
done
if [[ -z "${DEVICE_IP}" ]]
then
>&2 echo "Error: Failed to retrieve IP address of ${DEVICE_ID}!"
exit 1
fi
echo "Testing connection to ${DEVICE_IP}."
ssh -F "${SSH_CONFIG}" "${DEVICE_USER}@${DEVICE_IP}" true
trap unexpected_exit EXIT
echo "Removing sudo password on ${DEVICE_IP}."
# shellcheck disable=SC2087
ssh -F "${SSH_CONFIG}" "${DEVICE_USER}@${DEVICE_IP}" > /dev/null 2>&1 << EOF
echo "${DEVICE_USER} ALL=(ALL) NOPASSWD: ALL" > pytest_no_passwd
echo "${DEVICE_PASSWORD}" | sudo -S chown root:root pytest_no_passwd
echo "${DEVICE_PASSWORD}" | sudo -S mv pytest_no_passwd /etc/sudoers.d/ > /dev/null 2>&1
EOF
echo "Running tests on ${DEVICE_IP}."
py.test-3 --ignore="${EDI_PROJECT_CONFIGURATION}/" --ignore="artifacts/" --ssh-config="${SSH_CONFIG}" --hosts="ssh://${DEVICE_USER}@${DEVICE_IP}"
revert_sudoers
trap - EXIT