Repository navigation
Expand file tree
/
Copy pathconsole_test.sh
More file actions
executable file
·120 lines (103 loc) · 4.95 KB
/
Copy pathconsole_test.sh
File metadata and controls
executable file
·120 lines (103 loc) · 4.95 KB
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
#!/bin/bash
# Console API test (SPEC-021 / cloud-storage#534, finding F1).
#
# The version this replaces asserted only that *a* URL string came back — which is
# exactly why it stayed green against a console that could never work: the old
# handlers returned a hardcoded http://localhost:6080 URL with a token nothing
# recorded and a port derived from a hash. This asserts the parts that make a URL
# real, and that everything we cannot serve is refused rather than faked.
#
# STUB MODE ONLY: the console must refuse with 501. Real mode is
# ./test/console_access_test.sh, which boots a real image and checks that its links
# resolve, stay with their instance and are revoked.
#
# Usage: ./test/console_test.sh
# Env: KEYSTONE_URL, NOVA_URL
set -euo pipefail
KEYSTONE_URL="${KEYSTONE_URL:-http://localhost:35357}"
NOVA_URL="${NOVA_URL:-http://localhost:8774}"
# The instance below has no image, which real mode boots to ERROR with no libvirt
# domain, so in real mode it could never get a console.
if [ "${LIGHTSTACK_MODE:-stub}" != "stub" ]; then
echo "console_test.sh is stub-only; for real mode run ./test/console_access_test.sh" >&2
exit 2
fi
pass=0
fail=0
ok() { echo " ✓ $1"; pass=$((pass + 1)); }
bad() { echo " ✗ $1"; fail=$((fail + 1)); }
echo "=== Console API test (stub mode) ==="
TOKEN=$(curl -i -s -X POST "$KEYSTONE_URL/v3/auth/tokens" \
-H "Content-Type: application/json" \
-d '{"auth":{"identity":{"methods":["password"],"password":{"user":{"name":"admin","password":"secret","domain":{"name":"Default"}}}},"scope":{"project":{"name":"default","domain":{"name":"Default"}}}}}' \
| grep -i "X-Subject-Token:" | awk '{print $2}' | tr -d '\r')
if [ -z "$TOKEN" ]; then
echo "✗ authentication failed"
exit 1
fi
echo "✓ authenticated"
# Per-run scratch dir, not fixed /tmp paths: two runs side by side would otherwise
# assert against each other's response body, and a run under sudo would leave
# root-owned files that break the next unprivileged run.
#
# Installed before the instance exists so an early failure still cleans up; both the
# rm and the DELETE tolerate a half-finished run.
TMP=$(mktemp -d)
trap 'rm -rf "$TMP"; [ -n "${INSTANCE_ID:-}" ] && curl -s -X DELETE "$NOVA_URL/v2.1/servers/$INSTANCE_ID" -H "X-Auth-Token: $TOKEN" >/dev/null 2>&1 || true' EXIT
INSTANCE_ID=$(curl -s -X POST "$NOVA_URL/v2.1/servers" \
-H "X-Auth-Token: $TOKEN" -H "Content-Type: application/json" \
-d '{"server":{"name":"spec021-console-vm","flavorRef":"00000000-0000-0000-0000-000000000010"}}' \
| jq -r '.server.id')
if [ -z "$INSTANCE_ID" ] || [ "$INSTANCE_ID" = "null" ]; then
echo "✗ failed to create the test instance"
exit 1
fi
echo "✓ created instance $INSTANCE_ID"
sleep 2
remote_console() { # $1 = type
curl -s -o "$TMP/body" -w '%{http_code}' \
-X POST "$NOVA_URL/v2.1/servers/$INSTANCE_ID/remote-consoles" \
-H "X-Auth-Token: $TOKEN" -H "Content-Type: application/json" \
-d "{\"remote_console\":{\"protocol\":\"vnc\",\"type\":\"$1\"}}"
}
echo ""
echo "1. novnc console"
CODE=$(remote_console novnc)
BODY=$(cat "$TMP/body")
# Stub mode has no hypervisor and therefore no guest. Saying so is the whole
# point — a URL here would reproduce F1, where the browser failed opaquely.
[ "$CODE" = "501" ] && ok "stub mode refuses with 501" || bad "expected 501, got $CODE: $BODY"
echo "$BODY" | grep -qi "stub" && ok "the refusal names stub mode" || bad "refusal should name stub mode: $BODY"
echo "$BODY" | grep -q "http://" && bad "stub mode must not return a URL: $BODY" || ok "no URL returned"
echo ""
echo "2. legacy os-getVNCConsole agrees with remote-consoles"
LEG_CODE=$(curl -s -o "$TMP/legacy" -w '%{http_code}' \
-X POST "$NOVA_URL/v2.1/servers/$INSTANCE_ID/action" \
-H "X-Auth-Token: $TOKEN" -H "Content-Type: application/json" \
-d '{"os-getVNCConsole":{"type":"novnc"}}')
[ "$LEG_CODE" = "$CODE" ] && ok "both surfaces answer $LEG_CODE" \
|| bad "remote-consoles said $CODE but the legacy action said $LEG_CODE"
echo ""
echo "3. unsupported console types are refused, not faked"
for t in xvpvnc spice-html5 rdp-html5; do
C=$(remote_console "$t")
B=$(cat "$TMP/body")
if [ "$C" = "400" ] && ! echo "$B" | grep -q "http://"; then
ok "$t refused with 400 and no URL"
else
bad "$t returned $C: $B"
fi
done
echo ""
echo "4. the serial console log still works (the documented fallback)"
OUT_CODE=$(curl -s -o "$TMP/out" -w '%{http_code}' \
-X POST "$NOVA_URL/v2.1/servers/$INSTANCE_ID/action" \
-H "X-Auth-Token: $TOKEN" -H "Content-Type: application/json" \
-d '{"os-getConsoleOutput":{"length":50}}')
[ "$OUT_CODE" = "200" ] && ok "os-getConsoleOutput returns 200" || bad "console log broke: $OUT_CODE"
jq -e '.output' < "$TMP/out" >/dev/null 2>&1 && ok "output field present" || bad "no output field"
echo ""
echo "========================================"
echo "passed: $pass failed: $fail"
echo "========================================"
[ "$fail" -eq 0 ]