Skip to content

Commit a2fb3a5

Browse files
committed
feat: host IP aliases
1 parent 6da94dd commit a2fb3a5

File tree

5 files changed

+71
-23
lines changed

5 files changed

+71
-23
lines changed

Makefile

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ test-local-docker: ## test against local container
139139

140140
# test against local container
141141
make build CONTAINER_NAME="$(CONTAINER_NAME_TESTING)"
142+
142143
docker rm --force netassert-http-echo 2>/dev/null || true;
143144

144145
# start echo server
@@ -147,14 +148,14 @@ test-local-docker: ## test against local container
147148
hashicorp/http-echo \
148149
-listen=:59942 -text 'netassert test endpoint'
149150

150-
set -x; COUNT=0; \
151+
COUNT=0; \
151152
until [[ "$$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' netassert-http-echo)" != "" ]]; do \
152153
sleep 0.5; \
153154
if [[ $$((COUNT++)) -gt 10 ]]; then echo 'Container did not start'; exit 1; fi; \
154155
done;
155156

156157
# get IP to template into test file
157-
bash -euxo pipefail -c " \
158+
bash -euo pipefail -c " \
158159
TMP_TEST_FILE=$$(mktemp); \
159160
\
160161
cat test/test-localhost-docker-TEMPLATE.yaml \
@@ -166,6 +167,7 @@ test-local-docker: ## test against local container
166167
| tee -a \$${TMP_TEST_FILE}; \
167168
\
168169
./netassert \
170+
--verbose \
169171
--image $(CONTAINER_NAME_TESTING) \
170172
--no-pull \
171173
--ssh-user $${SSH_USER:-root} \

entrypoint.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ if [[ "${DEBUG:-}" == "1" ]]; then
7979
fi
8080

8181
mkdir -p ~/.ssh
82-
cp /home/netassert/.ssh ~/ -a || true
82+
if [[ -d /home/netassert/.ssh ]]; then
83+
cp /home/netassert/.ssh ~/ -a || true
84+
fi
8385
chown "$(whoami)" -R ${HOME}/.ssh
8486

8587
if [[ "${DEBUG:-}" == "1" ]]; then

netassert

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ main() {
7070

7171
read_config
7272

73+
parse_host_map || true
74+
7375
count_expected_tests
7476

7577
verbose "${CONFIG}"
@@ -83,6 +85,34 @@ main() {
8385
iterate_host
8486
}
8587

88+
get_host_filename() {
89+
local HOST="${1:-}"
90+
get_host "${HOST}" | base64
91+
}
92+
93+
get_host() {
94+
local HOST="${1:-}"
95+
96+
if [[ ! "${HOST}" =~ - ]]; then
97+
local HOST_CACHE_KEY="HOST_MAP_${HOST//./_}"
98+
# attempt to detect invalid cache keys :/
99+
if [[ "${!HOST_CACHE_KEY:-}" != "" ]]; then
100+
echo "${!HOST_CACHE_KEY} (${HOST})"
101+
return 0
102+
fi
103+
fi
104+
105+
echo "${HOST}"
106+
}
107+
108+
# todo(ajm) unsafe
109+
parse_host_map() {
110+
local HOST_ALIAS_MAP=$(jqc "${CONFIG}" -r \
111+
".alias? | to_entries? | map(\"export HOST_MAP_\(.key)='\(.value|tostring)'\") | .[]? | sub(\"\\\.\"; \"_\"; \"g\") ")
112+
verbose "${HOST_ALIAS_MAP}"
113+
source /dev/stdin < <(echo "${HOST_ALIAS_MAP}") || true
114+
}
115+
86116
count_expected_tests() {
87117
EXPECTED_TESTS=$(jqc "${CONFIG}" '[(.host, .k8s.deployment) | keys? | .[]? ] | length')
88118
info "${EXPECTED_TESTS} tests expected"
@@ -180,7 +210,7 @@ _iterate_k8s_worker() {
180210
--env TEST_YAML=$(echo "${TEST_DATA}" | to_yaml | base64 -w0) \
181211
--env DEBUG="${DEBUG}" \
182212
\
183-
| tee "${TEMP_DIR}/${HOST}" >/dev/null; then
213+
| tee "${TEMP_DIR}/$(get_host_filename ${HOST})" >/dev/null; then
184214

185215
warning "${HOST} fail"
186216
else
@@ -202,10 +232,12 @@ configure_parallel() {
202232

203233
parallel --no-notice --record-env
204234
export -f _iterate_k8s_worker _iterate_host_worker jqc success \
205-
info debug verbose error warning ssh_to_node log_message_prefix is_gke to_yaml wait_safe
235+
info debug verbose error warning ssh_to_node log_message_prefix \
236+
is_gke to_yaml wait_safe get_host get_host_filename
206237

207238
export CONFIG TEMP_DIR THIS_SCRIPT DIR NETASSERT_IMAGE_NAME \
208-
DEBUG IS_OFFLINE CONTAINER_BASE_NAME SSH_DEFAULT_OPTIONS SSH_USER SSH_USER_OPTIONS YJ
239+
DEBUG IS_OFFLINE CONTAINER_BASE_NAME SSH_DEFAULT_OPTIONS \
240+
SSH_USER SSH_USER_OPTIONS YJ
209241
}
210242

211243
iterate_host() {
@@ -250,24 +282,24 @@ _iterate_host_worker() {
250282
if [[ -f /opt/ssh_config ]]; then
251283
CONFIG_FILE_FLAG="-F /opt/ssh_config"
252284
fi
253-
info "Iterating instance targets: $(echo ${HOSTS} | tr ' ' ',')"
285+
verbose "Iterating instance targets: $(echo ${HOSTS} | tr ' ' ',')"
254286

255287

256288
local CONNECTION="ssh -tt ${SSH_DEFAULT_OPTIONS} ${SSH_USER_OPTIONS} ${CONFIG_FILE_FLAG} "
257289

258290
if [[ "${IS_OFFLINE:-}" != 1 ]]; then
259291
for HOST in ${HOSTS}; do
260292
if [[ "${HOST}" == 'localhost' ]]; then
261-
(info "$(hostname): pulling image locally" && docker pull "${NETASSERT_IMAGE_NAME}") &
293+
(info "$(get_host "$(hostname)"): pulling image locally" && docker pull "${NETASSERT_IMAGE_NAME}") &
262294
PIDS+="$! "
263295

264296
else
265-
local LOG_MESSAGE=$(info "${HOST}: pulling image remotely")
297+
local LOG_MESSAGE=$(info "$(get_host "${HOST}"): pulling image remotely")
266298

267299
${CONNECTION} ${HOST} \
268300
-- \
269301
bash -c "echo ${LOG_MESSAGE} && docker pull ${NETASSERT_IMAGE_NAME}" 2>&1 \
270-
| tee -a "${TEMP_DIR}/${HOST}" &
302+
| tee -a "${TEMP_DIR}/$(get_host_filename "${HOST}")" &
271303
PIDS+="$! "
272304
fi
273305
done
@@ -285,7 +317,7 @@ _iterate_host_worker() {
285317

286318
if [[ "${HOST}" == 'localhost' ]]; then
287319

288-
info "${HOST}: local test start"
320+
info "$(get_host "${HOST}"): local test start"
289321

290322
if ! \
291323
docker run -t \
@@ -295,15 +327,15 @@ _iterate_host_worker() {
295327
--env DEBUG="${DEBUG}" \
296328
"${NETASSERT_IMAGE_NAME}" \
297329
npm test -s \
298-
| tee -a "${TEMP_DIR}/${HOST}" >/dev/null; then
330+
| tee -a "${TEMP_DIR}/$(get_host_filename "${HOST}")" >/dev/null; then
299331

300-
warning "${HOST}: test fail"
332+
warning "$(get_host "${HOST}"): test fail"
301333
else
302-
success "${HOST}: test pass"
334+
success "$(get_host "${HOST}"): test pass"
303335
fi
304336
else
305337

306-
info "${HOST}: remote test start"
338+
info "$(get_host "${HOST}"): remote test start"
307339

308340
if ! \
309341
${CONNECTION} ${HOST} \
@@ -319,11 +351,11 @@ _iterate_host_worker() {
319351
"${NETASSERT_IMAGE_NAME}" \
320352
npm test -s \
321353
" \
322-
| tee -a "${TEMP_DIR}/${HOST}" >/dev/null; then
354+
| tee -a "${TEMP_DIR}/$(get_host_filename "${HOST}")" >/dev/null; then
323355

324-
warning "${HOST}: test fail"
356+
warning "$(get_host "${HOST}"): test fail"
325357
else
326-
success "${HOST}: test pass"
358+
success "$(get_host "${HOST}"): test pass"
327359
fi
328360
fi
329361
done <<<"${HOSTS}"
@@ -367,7 +399,7 @@ ssh_to_node() {
367399
CONNECTION="ssh -tt ${SSH_DEFAULT_OPTIONS} ${SSH_USER_OPTIONS} ${HOST} -- "
368400
fi
369401

370-
info "Connecting to ${HOST}..."
402+
info "Connecting to $(get_host "${HOST}")..."
371403

372404
local PULL_COMMAND=""
373405
if [[ "${IS_OFFLINE:-}" != 1 ]]; then
@@ -405,8 +437,15 @@ present_results() {
405437
local OUTPUT_FAIL_HR=$(printf "%s\n" "${COLOUR_RED}$(hr)${COLOUR_RESET}")
406438
local OUTPUT_PASS_HR=$(printf "%s\n" "${COLOUR_GREEN}$(hr)${COLOUR_RESET}")
407439

408-
for RESULT in $(find ${TEMP_DIR}/ -type f); do
440+
# todo(ajm) write aliases into test results at runtime
441+
while read ALIAS; do
442+
sed "s=${ALIAS}=g" -i "${TEMP_DIR}/"*
443+
done < <(jqc "${CONFIG}" -r \
444+
".alias? | to_entries? | map(\"\(.key)=\(.value|tostring) (\(.key))\") | .[]? ")
445+
446+
while read RESULT; do
409447
local TEST_NAME="${RESULT/${TEMP_DIR}\//}"
448+
TEST_NAME=$(base64 -d <<<"${TEST_NAME}")
410449
RESULT_COUNT=$((RESULT_COUNT + 1))
411450
local TAP_RESULT=$(
412451
if ! cat "${RESULT}" | grep -E '^TAP version [0-9]+' -A 9999; then
@@ -432,7 +471,7 @@ present_results() {
432471
echo "${OUTPUT_PASS_HR}"
433472
echo)
434473
fi
435-
done
474+
done < <(find ${TEMP_DIR}/ -type f)
436475

437476
local OUTPUT_RESULT=$(
438477
if [[ "${TEST_FAILURES}" -gt 0 ]]; then
@@ -510,8 +549,8 @@ check_dependencies() {
510549
}
511550

512551
handle_arguments() {
513-
parse_arguments "$@"
514-
validate_arguments "$@"
552+
parse_arguments "${@}"
553+
validate_arguments "${@}"
515554
}
516555

517556
parse_arguments() {

test/netassert-test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ process.setMaxListeners(200)
1212

1313
function log () {
1414
if (debug) {
15+
// process.stdout.write('# ')
1516
console.log.apply(null, arguments)
1617
}
1718
}

test/test-localhost-docker-TEMPLATE.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
alias:
2+
${DOCKER_HOST_IP}: echo-server
3+
localhost: my nice local host
4+
15
host:
26
localhost:
37
${DOCKER_HOST_IP}:

0 commit comments

Comments
 (0)