-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathbaudbot.test.sh
More file actions
201 lines (168 loc) · 4.4 KB
/
baudbot.test.sh
File metadata and controls
201 lines (168 loc) · 4.4 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
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
194
195
196
197
198
199
200
201
#!/bin/bash
# Focused tests for bin/baudbot CLI dispatcher behavior.
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
CLI="$REPO_ROOT/bin/baudbot"
TOTAL=0
PASSED=0
FAILED=0
run_test() {
local name="$1"
shift
local out
TOTAL=$((TOTAL + 1))
printf " %-45s " "$name"
out="$(mktemp /tmp/baudbot-cli-test-output.XXXXXX)"
if "$@" >"$out" 2>&1; then
echo "✓"
PASSED=$((PASSED + 1))
else
echo "✗ FAILED"
tail -40 "$out" | sed 's/^/ /'
FAILED=$((FAILED + 1))
fi
rm -f "$out"
}
test_version_uses_package_json() {
(
set -euo pipefail
local tmp out
tmp="$(mktemp -d /tmp/baudbot-cli-test.XXXXXX)"
trap 'rm -rf "$tmp"' EXIT
mkdir -p "$tmp/bin"
printf '{"version":"9.9.9"}\n' > "$tmp/package.json"
out="$(BAUDBOT_ROOT="$tmp" bash "$CLI" version)"
printf '%s\n' "$out" | grep -q '^baudbot 9\.9\.9'
)
}
test_status_dispatches_via_runtime_module() {
(
set -euo pipefail
local tmp out
tmp="$(mktemp -d /tmp/baudbot-cli-test.XXXXXX)"
trap 'rm -rf "$tmp"' EXIT
mkdir -p "$tmp/bin/lib"
printf '{"version":"1.2.3"}\n' > "$tmp/package.json"
cat > "$tmp/bin/lib/baudbot-runtime.sh" <<'EOF'
#!/bin/bash
cmd_status() { echo "status-dispatch-ok"; }
cmd_logs() { echo "logs-dispatch-ok"; }
cmd_sessions() { echo "sessions-dispatch-ok"; }
cmd_attach() { echo "attach-dispatch-ok"; }
has_systemd() { return 1; }
EOF
out="$(BAUDBOT_ROOT="$tmp" bash "$CLI" status)"
[ "$out" = "status-dispatch-ok" ]
)
}
test_attach_requires_root() {
(
set -euo pipefail
local tmp fakebin out
tmp="$(mktemp -d /tmp/baudbot-cli-test.XXXXXX)"
trap 'rm -rf "$tmp"' EXIT
mkdir -p "$tmp/fakebin"
fakebin="$tmp/fakebin"
cat > "$fakebin/id" <<'EOF'
#!/bin/bash
if [ "${1:-}" = "-u" ]; then
echo 1000
elif [ "${1:-}" = "-un" ]; then
echo tester
else
/usr/bin/id "$@"
fi
EOF
chmod +x "$fakebin/id"
if PATH="$fakebin:$PATH" BAUDBOT_ROOT="$REPO_ROOT" bash "$CLI" attach >/tmp/baudbot-attach.out 2>&1; then
return 1
fi
out="$(cat /tmp/baudbot-attach.out)"
rm -f /tmp/baudbot-attach.out
printf '%s\n' "$out" | grep -q "requires root"
)
}
test_broker_register_requires_root() {
(
set -euo pipefail
local tmp fakebin out
tmp="$(mktemp -d /tmp/baudbot-cli-test.XXXXXX)"
trap 'rm -rf "$tmp"' EXIT
mkdir -p "$tmp/fakebin"
fakebin="$tmp/fakebin"
cat > "$fakebin/id" <<'EOF'
#!/bin/bash
if [ "${1:-}" = "-u" ]; then
echo 1000
elif [ "${1:-}" = "-un" ]; then
echo tester
else
/usr/bin/id "$@"
fi
EOF
chmod +x "$fakebin/id"
if PATH="$fakebin:$PATH" BAUDBOT_ROOT="$REPO_ROOT" bash "$CLI" broker register >/tmp/baudbot-broker.out 2>&1; then
return 1
fi
out="$(cat /tmp/baudbot-broker.out)"
rm -f /tmp/baudbot-broker.out
printf '%s\n' "$out" | grep -q "requires root"
)
}
test_restart_restarts_systemd() {
(
set -euo pipefail
local tmp fakebin log_file
tmp="$(mktemp -d /tmp/baudbot-cli-test.XXXXXX)"
trap 'rm -rf "$tmp"' EXIT
mkdir -p "$tmp/fakebin" "$tmp/bin/lib"
fakebin="$tmp/fakebin"
log_file="$tmp/calls.log"
printf '{"version":"1.2.3"}\n' > "$tmp/package.json"
cat > "$tmp/bin/lib/baudbot-runtime.sh" <<'EOF'
#!/bin/bash
has_systemd() { return 0; }
cmd_status() { :; }
cmd_logs() { :; }
cmd_sessions() { :; }
cmd_attach() { :; }
EOF
cat > "$fakebin/id" <<'EOF'
#!/bin/bash
if [ "${1:-}" = "-u" ]; then
echo 0
else
/usr/bin/id "$@"
fi
EOF
cat > "$fakebin/sudo" <<'EOF'
#!/bin/bash
if [ "${1:-}" = "-u" ]; then
user="$2"
shift 2
fi
echo "sudo $*" >> "${BAUDBOT_TEST_LOG}"
exec "$@"
EOF
cat > "$fakebin/systemctl" <<'EOF'
#!/bin/bash
echo "systemctl $*" >> "${BAUDBOT_TEST_LOG}"
exit 0
EOF
chmod +x "$fakebin/id" "$fakebin/sudo" "$fakebin/systemctl"
PATH="$fakebin:$PATH" BAUDBOT_TEST_LOG="$log_file" BAUDBOT_ROOT="$tmp" bash "$CLI" restart
grep -q '^systemctl restart baudbot$' "$log_file"
)
}
echo "=== baudbot cli tests ==="
echo ""
run_test "version reads package.json" test_version_uses_package_json
run_test "status dispatches via runtime module" test_status_dispatches_via_runtime_module
run_test "attach requires root" test_attach_requires_root
run_test "broker register requires root" test_broker_register_requires_root
run_test "restart restarts systemd" test_restart_restarts_systemd
echo ""
echo "=== $PASSED/$TOTAL passed, $FAILED failed ==="
if [ "$FAILED" -gt 0 ]; then
exit 1
fi