-
Notifications
You must be signed in to change notification settings - Fork 303
Expand file tree
/
Copy pathtest-tempoup.sh
More file actions
executable file
·210 lines (182 loc) · 6.3 KB
/
Copy pathtest-tempoup.sh
File metadata and controls
executable file
·210 lines (182 loc) · 6.3 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
202
203
204
205
206
207
208
209
210
#!/usr/bin/env bash
# shellcheck disable=SC2016
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
TMP_ROOT="$(mktemp -d)"
trap 'rm -rf "$TMP_ROOT"' EXIT
TEMPOUP_SOURCE="$TMP_ROOT/tempoup-source.sh"
INSTALL_SOURCE="$TMP_ROOT/install-source.sh"
sed '$d' "$ROOT_DIR/tempoup/tempoup" > "$TEMPOUP_SOURCE"
sed '$d' "$ROOT_DIR/tempoup/install" > "$INSTALL_SOURCE"
test_index=0
q() {
printf '%q' "$1"
}
ok() {
printf 'ok - %s\n' "$1"
}
fail() {
printf 'not ok - %s\n' "$1" >&2
exit 1
}
contains() {
[[ "$1" == *"$2"* ]] || fail "expected output to contain $2"
}
log_contains() {
if [[ ! -f "$1" ]] || ! grep -qF "$2" "$1"; then
fail "expected $1 to contain $2"
fi
}
log_lacks() {
[[ ! -f "$1" ]] || ! grep -qF "$2" "$1" || fail "expected $1 not to contain $2"
}
run_source() {
local source_file="$1"
local snippet="$2"
local runner="$TMP_ROOT/case-$((test_index += 1)).sh"
{
printf 'set -e\n'
printf 'source %q\n' "$source_file"
printf '%s\n' "$snippet"
} > "$runner"
bash "$runner"
}
run_tempoup() {
run_source "$TEMPOUP_SOURCE" "$1"
}
run_install() {
run_source "$INSTALL_SOURCE" "$1"
}
fake_otool() {
mkdir -p "$1"
cat > "$1/otool" <<'FAKE_OTOOL'
#!/usr/bin/env bash
printf '%s:\n' "${@: -1}"
if [[ -n "${REQUIRED_DYLIB:-}" ]]; then
printf '\t%s (compatibility version 6.0.0, current version 6.0.0)\n' "$REQUIRED_DYLIB"
else
printf '\t/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1351.0.0)\n'
fi
FAKE_OTOOL
chmod +x "$1/otool"
}
fake_brew() {
mkdir -p "$1"
cat > "$1/brew" <<'FAKE_BREW'
#!/usr/bin/env bash
printf '%s\n' "$*" >> "$BREW_LOG"
case "$1 ${2:-}" in
"--prefix libusb") printf '%s\n' "$LIBUSB_PREFIX" ;;
"list --versions") [[ "${BREW_LIST_INSTALLED:-0}" == "1" ]] && printf 'libusb 1.0.30\n' || exit 1 ;;
"install libusb"|"reinstall libusb") mkdir -p "$LIBUSB_PREFIX/lib"; : > "$LIBUSB_PREFIX/lib/libusb-1.0.0.dylib" ;;
*) printf 'unexpected brew invocation: %s\n' "$*" >&2; exit 2 ;;
esac
FAKE_BREW
chmod +x "$1/brew"
}
setup_case() {
CASE_DIR="$TMP_ROOT/$1"
FAKE_BIN="$CASE_DIR/bin"
BREW_LOG="$CASE_DIR/brew.log"
LIBUSB_PREFIX="$CASE_DIR/prefix"
TEMPO_BINARY="$CASE_DIR/tempo"
REQUIRED_DYLIB="$LIBUSB_PREFIX/lib/libusb-1.0.0.dylib"
mkdir -p "$(dirname "$TEMPO_BINARY")"
: > "$TEMPO_BINARY"
fake_otool "$FAKE_BIN"
if [[ "${2:-brew}" == "brew" ]]; then
fake_brew "$FAKE_BIN"
fi
}
check_darwin_deps() {
REQUIRED_DYLIB="$1" BREW_LOG="$BREW_LOG" LIBUSB_PREFIX="$LIBUSB_PREFIX" \
PATH="$FAKE_BIN:$PATH" run_tempoup "ensure_runtime_dependencies darwin $(q "$TEMPO_BINARY")"
}
setup_case linux
BREW_LOG="$BREW_LOG" LIBUSB_PREFIX="$LIBUSB_PREFIX" \
PATH="$FAKE_BIN:$PATH" run_tempoup "ensure_runtime_dependencies linux $(q "$TEMPO_BINARY")"
[[ ! -f "$BREW_LOG" ]] || fail "linux runtime dependency check called brew"
ok "linux runtime dependency no-op"
setup_case darwin-no-link
check_darwin_deps ""
[[ ! -f "$BREW_LOG" ]] || fail "non-libusb darwin binary called brew"
ok "darwin binary without libusb link is no-op"
setup_case darwin-existing
mkdir -p "$LIBUSB_PREFIX/lib"
: > "$REQUIRED_DYLIB"
check_darwin_deps "$REQUIRED_DYLIB"
log_lacks "$BREW_LOG" "install libusb"
log_lacks "$BREW_LOG" "reinstall libusb"
ok "darwin existing libusb is no-op"
setup_case darwin-install
check_darwin_deps "$REQUIRED_DYLIB"
[[ -r "$REQUIRED_DYLIB" ]] || fail "libusb was not installed"
log_contains "$BREW_LOG" "install libusb"
ok "darwin missing libusb installs formula"
setup_case darwin-reinstall
mkdir -p "$LIBUSB_PREFIX/lib"
BREW_LIST_INSTALLED=1 check_darwin_deps "$REQUIRED_DYLIB"
[[ -r "$REQUIRED_DYLIB" ]] || fail "libusb was not reinstalled"
log_contains "$BREW_LOG" "reinstall libusb"
ok "darwin incomplete formula reinstalls"
setup_case darwin-no-brew none
if output="$(REQUIRED_DYLIB="$REQUIRED_DYLIB" PATH="$FAKE_BIN:/usr/bin:/bin" \
run_tempoup "ensure_runtime_dependencies darwin $(q "$TEMPO_BINARY")" 2>&1)"; then
printf '%s\n' "$output" >&2
fail "missing Homebrew case succeeded"
fi
contains "$output" "Homebrew was not found"
contains "$output" "brew install libusb"
ok "darwin missing Homebrew fails clearly"
GOOD_TEMPO="$TMP_ROOT/tempo-good"
BAD_TEMPO="$TMP_ROOT/tempo-bad"
cat > "$GOOD_TEMPO" <<'GOOD'
#!/usr/bin/env bash
printf 'Tempo Version: test\n'
GOOD
cat > "$BAD_TEMPO" <<'BAD'
#!/usr/bin/env bash
printf 'dyld: Library not loaded: libusb-1.0.0.dylib\n' >&2
exit 134
BAD
chmod +x "$GOOD_TEMPO" "$BAD_TEMPO"
TEST_BINARY="$GOOD_TEMPO" run_tempoup 'verify_tempo_binary "$TEST_BINARY"'
if output="$(TEST_BINARY="$BAD_TEMPO" run_tempoup 'verify_tempo_binary "$TEST_BINARY"' 2>&1)"; then
printf '%s\n' "$output" >&2
fail "broken tempo binary verified successfully"
fi
contains "$output" "could not launch because libusb is missing"
ok "tempo launch verification catches libusb failure"
run_install '
should_verify_tempo_after_tempoup
should_verify_tempo_after_tempoup --unsafe-skip-verify
should_verify_tempo_after_tempoup --unsafe-skip-verify -i v1.9.1
! should_verify_tempo_after_tempoup -i v1.9.1 --help
! should_verify_tempo_after_tempoup -i v1.9.1 --version
! should_verify_tempo_after_tempoup -i v1.9.1 --update
! should_verify_tempo_after_tempoup --help
! should_verify_tempo_after_tempoup -h
! should_verify_tempo_after_tempoup --version
! should_verify_tempo_after_tempoup -v
! should_verify_tempo_after_tempoup --update
! should_verify_tempo_after_tempoup -U
'
ok "bootstrap verification gating"
ROLLBACK_DIR="$TMP_ROOT/rollback-upgrade"
mkdir -p "$ROLLBACK_DIR"
printf 'old tempo\n' > "$ROLLBACK_DIR/tempo"
if output="$(ROLLBACK_DIR="$ROLLBACK_DIR" run_tempoup '
TEMPOUP_INSTALL_TARGET="$ROLLBACK_DIR/tempo"
TEMPOUP_OLD_BINARY="$TEMPOUP_INSTALL_TARGET.old"
TEMPOUP_HAD_OLD_BINARY=1
mv -f "$TEMPOUP_INSTALL_TARGET" "$TEMPOUP_OLD_BINARY"
trap rollback_tempo_install EXIT
printf "new tempo\n" > "$TEMPOUP_INSTALL_TARGET"
false
' 2>&1)"; then
printf '%s\n' "$output" >&2
fail "failed upgrade rollback succeeded"
fi
[[ "$(cat "$ROLLBACK_DIR/tempo")" == "old tempo" ]] || fail "failed upgrade did not restore old tempo"
[[ ! -e "$ROLLBACK_DIR/tempo.old" ]] || fail "failed upgrade left backup behind"
ok "failed upgrade restores old tempo"