-
-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathshellhub-agent-wrapper.bats
More file actions
183 lines (135 loc) · 5.13 KB
/
Copy pathshellhub-agent-wrapper.bats
File metadata and controls
183 lines (135 loc) · 5.13 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
#!/usr/bin/env bats
# Tests for the shellhub-agent wrapper that install.sh generates. The wrapper
# is a separate artifact with its own runtime behaviour — it locates the agent
# container and proxies commands into it — so install.sh appears here only in
# setup_file, to produce the fixture. Coverage of the generation itself lives
# in install.bats.
load install.helpers
setup_file() {
fixture_bin="$BATS_FILE_TMPDIR/fixture-bin"
mkdir -p "$fixture_bin"
printf '#!/bin/sh\necho 0\n' > "$fixture_bin/id"
chmod +x "$fixture_bin/id"
for runtime in docker podman; do
mkdir -p "$BATS_FILE_TMPDIR/$runtime"
env PATH="$fixture_bin:$PATH" INSTALL_SH_LIB=1 INSTALL_DIR="$BATS_FILE_TMPDIR/$runtime" \
sh -c '. "$1"; install_agent_wrapper "$2"' sh "$INSTALL_SH" "$runtime" > /dev/null
done
}
setup() {
setup_install_env
RUNTIME=docker
WRAPPER="$BATS_FILE_TMPDIR/docker/shellhub-agent"
}
use_podman() {
RUNTIME=podman
WRAPPER="$BATS_FILE_TMPDIR/podman/shellhub-agent"
}
# Stub the container runtime the wrapper drives.
# Usage: stub_runtime <ps output> [exec output] [exec exit status]
stub_runtime() {
export STUB_CONTAINERS="${1-}" STUB_EXEC_OUTPUT="${2-}" STUB_EXEC_STATUS="${3:-0}"
stub_bin "$RUNTIME" "echo \"$RUNTIME \$*\" >> \"\$CALLS\"
case \"\$1\" in
ps) [ -n \"\$STUB_CONTAINERS\" ] && printf '%s\n' \"\$STUB_CONTAINERS\" ;;
exec)
[ -n \"\$STUB_EXEC_OUTPUT\" ] && printf '%s\n' \"\$STUB_EXEC_OUTPUT\"
exit \"\$STUB_EXEC_STATUS\"
;;
esac
exit 0"
}
run_wrapper() {
run env PATH="$STUB_DIR:$REAL_BIN" "$WRAPPER" "$@"
}
@test "the wrapper looks the agent container up by its role label" {
stub_runtime shellhub
run_wrapper status
[ "$status" -eq 0 ]
assert_called "docker ps --filter label=shellhub.role=agent --format {{.Names}}"
}
@test "the wrapper proxies its arguments into the single agent container" {
stub_runtime shellhub
run_wrapper status --format json
[ "$status" -eq 0 ]
assert_called "docker exec shellhub agent status --format json"
}
@test "the wrapper reports when no agent container is running" {
stub_runtime ""
run_wrapper status
[ "$status" -eq 1 ]
assert_output_contains "no running agent container found"
assert_output_contains "install.sh uninstall"
refute_called "docker exec"
}
@test "the wrapper refuses to guess between several agent containers" {
stub_runtime "$(printf 'shellhub\nshellhub-spare')"
run_wrapper status
[ "$status" -eq 1 ]
assert_output_contains "multiple agent containers found"
assert_output_contains " shellhub"
assert_output_contains " shellhub-spare"
refute_called "docker exec"
}
@test "the wrapper propagates the agent's exit status" {
stub_runtime shellhub "" 3
run_wrapper status
[ "$status" -eq 3 ]
}
@test "login opens the accept-device URL in the host browser" {
stub_runtime shellhub "https://cloud.example.test/accept-device?code=ABC123"
stub_bin xdg-open
run_wrapper login
[ "$status" -eq 0 ]
assert_called "xdg-open https://cloud.example.test/accept-device?code=ABC123"
assert_output_contains "(opened in your browser)"
}
@test "login says nothing about a browser it could not open" {
stub_runtime shellhub "https://cloud.example.test/accept-device?code=ABC123"
stub_bin xdg-open 'echo "xdg-open $*" >> "$CALLS"; exit 1'
run_wrapper login
[ "$status" -eq 0 ]
assert_called "xdg-open https://cloud.example.test/accept-device?code=ABC123"
refute_output_contains "opened in your browser"
}
@test "login strips the whitespace around the URL before handing it to the browser" {
stub_runtime shellhub " https://cloud.example.test/accept-device?code=ABC123 "
stub_bin xdg-open
run_wrapper login
[ "$status" -eq 0 ]
assert_called "xdg-open https://cloud.example.test/accept-device?code=ABC123"
}
@test "login streams the agent output while watching for the URL" {
stub_runtime shellhub "$(printf 'waiting for acceptance\nhttps://cloud.example.test/accept-device?code=ABC123\ndone')"
stub_bin xdg-open
run_wrapper login
[ "$status" -eq 0 ]
assert_output_contains "waiting for acceptance"
assert_output_contains "done"
}
@test "login propagates the agent's exit status through the browser path" {
stub_runtime shellhub "https://cloud.example.test/accept-device?code=ABC123" 3
stub_bin xdg-open
run_wrapper login
[ "$status" -eq 3 ]
}
@test "login proxies straight through when no browser is available" {
stub_runtime shellhub "https://cloud.example.test/accept-device?code=ABC123"
run_wrapper login
[ "$status" -eq 0 ]
assert_called "docker exec shellhub agent login"
refute_output_contains "opened in your browser"
}
@test "login propagates the agent's exit status when no browser is available" {
stub_runtime shellhub "" 3
run_wrapper login
[ "$status" -eq 3 ]
}
@test "the podman wrapper drives podman" {
use_podman
stub_runtime shellhub
run_wrapper status
[ "$status" -eq 0 ]
assert_called "podman ps --filter label=shellhub.role=agent"
assert_called "podman exec shellhub agent status"
}