summaryrefslogtreecommitdiff
path: root/test/utils/shippable/remote.sh
blob: 7f39ad10ff54ba1243500dcc57d2654ecbc704ae (plain)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/bin/bash -eux

set -o pipefail

source_root=$(python -c "from os import path; print(path.abspath(path.join(path.dirname('$0'), '../../..')))")

test_flags="${TEST_FLAGS:-}"
test_platform="${PLATFORM}"
test_version="${VERSION}"

test_target=(${TARGET})

instance_id="${INSTANCE_ID:-}"
start_instance=

if [ "${instance_id}" == "" ]; then
    instance_id=$(python -c 'import uuid; print(uuid.uuid4())')
    start_instance=1
fi

# Set this to a non-empty value to skip immediate termination of the remote instance after tests finish.
# The remote instance will still be auto-terminated when the remote time limit is reached.
keep_instance="${KEEP_INSTANCE:-}"

# Force ansible color output by default.
# To disable color force mode use FORCE_COLOR=0
force_color="${FORCE_COLOR:-1}"

if [ "${SHIPPABLE:-}" = "true" ]; then
    test_auth="shippable"
else
    test_auth="remote"
fi

case "${test_platform}" in
    "windows")
        ci_endpoint="https://14blg63h2i.execute-api.us-east-1.amazonaws.com"
        ;;
    "freebsd")
        ci_endpoint="https://14blg63h2i.execute-api.us-east-1.amazonaws.com"
        ;;
    "osx")
        ci_endpoint="https://osx.testing.ansible.com"
        ;;
    *)
        echo "unsupported platform: ${test_platform}"
        exit 1
        ;;
esac

env

case "${test_platform}" in
    "windows")
        args=""
        ;;
    *)
        ssh_key="${HOME}/.ssh/id_rsa"
        args="--public-key=${ssh_key}.pub"
        if [ ! -f "${ssh_key}.pub" ]; then
            ssh-keygen -q -t rsa -N '' -f "${ssh_key}"
        fi
        ;;
esac

pre_cleanup=

function cleanup
{
    if [ "${pre_cleanup}" != '' ]; then
        "${pre_cleanup}"
    fi

    if [ "${keep_instance}" = '' ]; then
        "${source_root}/test/utils/shippable/ansible-core-ci" --endpoint "${ci_endpoint}" -v stop "${instance_id}"
    fi

    echo "instance_id: ${instance_id}"
}

trap cleanup EXIT INT TERM

if [ ${start_instance} ]; then
    # shellcheck disable=SC2086
    "${source_root}/test/utils/shippable/ansible-core-ci" --endpoint "${ci_endpoint}" -v \
        start --id "${instance_id}" "${test_auth}" "${test_platform}" "${test_version}" ${args}
fi

pip install "${source_root}" --upgrade
pip install -r "${source_root}/test/utils/shippable/remote-requirements.txt" --upgrade
pip list

cd "${source_root}"
source hacking/env-setup
cd test/integration

case "${test_platform}" in
    "windows")
        inventory_template="${source_root}/test/integration/inventory.winrm.template"
        inventory_file="${source_root}/test/integration/inventory.winrm"
        ping_module="win_ping"
        ping_args=""
        ping_host="windows"
        test_function="test_windows"
        ;;
    *)
        inventory_template="${source_root}/test/integration/inventory.remote.template"
        inventory_file="${source_root}/test/integration/inventory.remote"
        ping_module="raw"
        ping_args="id"
        ping_host="remote"
        test_function="test_remote"
        ;;
esac

"${source_root}/test/utils/shippable/ansible-core-ci" --endpoint "${ci_endpoint}" -v \
    get "${instance_id}" \
    --template "${inventory_template}" \
    > "${inventory_file}" \

# hack to make sure instance is responding before beginning tests
n=60
for i in $(seq 1 ${n}); do
    echo "Verifying host is responding ($i of $n)"
    if \
        ANSIBLE_SSH_ARGS='' \
        ANSIBLE_HOST_KEY_CHECKING=False \
        ANSIBLE_FORCE_COLOR="${force_color}" \
        ansible -m "${ping_module}" -a "${ping_args}" -i "${inventory_file}" "${ping_host}"; then
        break
    fi
    sleep 5
done

test_windows() {
    JUNIT_OUTPUT_DIR="${source_root}/shippable/testresults" \
        ANSIBLE_FORCE_COLOR="${force_color}" \
        ANSIBLE_CALLBACK_WHITELIST=junit \
        TEST_FLAGS="${test_flags}" \
        LC_ALL=en_US.utf-8 \
        make "${test_target[@]}"
}

test_remote() {
    endpoint=$("${source_root}/test/utils/shippable/ansible-core-ci" --endpoint "${ci_endpoint}" get \
        "${instance_id}" \
        --template <(echo "@ansible_user@@ansible_host"))
    ssh_port=$("${source_root}/test/utils/shippable/ansible-core-ci" --endpoint "${ci_endpoint}" get \
        "${instance_id}" \
        --template <(echo "@ansible_port"))

(
cat <<EOF
env \
PLATFORM='${test_platform}' \
REPOSITORY_URL='${REPOSITORY_URL:-}' \
REPO_NAME='${REPO_NAME:-}' \
PULL_REQUEST='${PULL_REQUEST:-}' \
BRANCH='${BRANCH:-}' \
COMMIT='${COMMIT:-}' \
FORCE_COLOR='${force_color}' \
TARGET='${test_target[*]}' \
TEST_FLAGS='${test_flags}' \
/bin/sh -e /tmp/remote-integration.sh
EOF
) > /tmp/remote-script.sh

(
cat <<EOF
put "${source_root}/test/utils/shippable/remote-integration.sh" "/tmp/remote-integration.sh"
put "/tmp/remote-script.sh" "/tmp/remote-script.sh"
EOF
) | sftp -b - -o StrictHostKeyChecking=no -P "${ssh_port}" "${endpoint}"

    pre_cleanup=test_remote_cleanup

    case "${test_platform}" in
        "osx")
            become="sudo -i PATH=/usr/local/bin:\$PATH"
            ;;
        *)
            become="su -l root -c"
            ;;
    esac

    ssh -p "${ssh_port}" "${endpoint}" "${become}" "'chmod +x /tmp/remote-script.sh; /tmp/remote-script.sh'"
}

test_remote_cleanup() {
    scp -r -P "${ssh_port}" "${endpoint}:/tmp/shippable" "${source_root}"
}

"${test_function}"