-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathtest_install.rb
More file actions
228 lines (196 loc) · 5.82 KB
/
test_install.rb
File metadata and controls
228 lines (196 loc) · 5.82 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
require "open3"
require "shellwords"
class Matcher
def initialize(string, expected)
@string = string
@expected = expected
end
def match?(out)
out.include?(@string) == @expected
end
def inspect
@expected ? "match #{@string.inspect}" : "not match #{@string.inspect}"
end
end
$EXPECTED_EXIT = 0
DISTROS = {
debian: "debian:bookworm-slim",
ubuntu: "ubuntu:latest",
fedora: "fedora:latest"
}
def run_cmd(*cmd, exit: 0)
formatted_cmd = cmd.map { Shellwords.escape(it) }.join(" ")
out, err, status = Open3.capture3(*cmd)
if status.exitstatus != exit
fail "Expected status to be #{exit}; " \
"got #{status.exitstatus}\n\n" \
"== command ==\n#{formatted_cmd}\n\n" \
"== stdout ==\n#{format_output(out)}\n\n" \
"== stderr ==\n#{format_output(err)}"
end
[out, err]
end
def run_docker(distro, cmd, exit: 0)
run_cmd(
"docker",
"run",
"--rm",
"-v",
"#{__dir__}:/source:ro",
DISTROS.fetch(distro),
"sh",
"-c",
cmd,
exit:
)
end
def match(matcher)
Matcher.new(matcher, true)
end
def doesnt_match(matcher)
Matcher.new(matcher, false)
end
def format_output(output)
output = "\\n" if output == "\n"
output = "<empty>" if output.empty?
output
end
def run_test(title, distro:, cmd:, exit: 0, out: nil, err: nil)
title = "\e[34m#{title}\e[0m"
print "\e[33m[ACTING]\e[0m #{title}"
stdout, stderr = run_docker(distro, cmd, exit:)
outputs = "\n\n== stdout ==\n#{format_output(stdout)}\n\n"
outputs = "#{outputs}== stderr ==\n#{format_output(stderr)}"
Array(out).each do |item|
if item
unless item.match?(stdout)
fail "Expected output to #{item.inspect}#{outputs}"
end
end
end
Array(err).each do |item|
if item
unless item.match?(stderr)
fail "Expected output to #{item.inspect}#{outputs}"
end
end
end
puts "\r\e[32m[PASSED] #{title}\e[0m"
rescue RuntimeError => error
puts "\r\e[31m[FAILED]\e[0m #{title}\nError: #{error.message}\n\n"
$EXPECTED_EXIT = 1
end
run_test "sanity: output capture works",
distro: :debian,
cmd: "echo hello_from_container",
out: match("hello_from_container")
run_test "debian: exits when curl is missing",
distro: :debian,
cmd: "sh /source/install.sh",
exit: 1,
out: match("Error: Missing required command(s): curl")
run_test "debian: no sudo in suggestions when running as root",
distro: :debian,
cmd: "apt-get update && apt-get -y install curl; sh /source/install.sh",
exit: 1,
out: [doesnt_match("sudo apt-get"), doesnt_match("sudo dnf")]
run_test "fedora: no sudo in suggestions when running as root",
distro: :fedora,
cmd: "dnf -y install curl; sh /source/install.sh",
exit: 1,
out: [doesnt_match("sudo apt-get"), doesnt_match("sudo dnf")]
run_test "debian: sudo in suggestions when running as user",
distro: :debian,
cmd: <<~SH,
apt-get update && apt-get -y install curl sudo
adduser --disabled-password --gecos "" test
su -c "sh /source/install.sh" test
SH
exit: 1,
out: match("sudo apt-get")
run_test "fedora: sudo in suggestions when running as user",
distro: :fedora,
cmd: <<~SH,
dnf -y install curl util-linux
adduser test
runuser -u test -- sh /source/install.sh
SH
exit: 1,
out: match("sudo dnf")
run_test "debian: warns about missing runtime libraries",
distro: :debian,
cmd: <<~SH,
apt-get update && apt-get -y install curl
apt-get -y --allow-remove-essential remove util-linux libudev1 libapt-pkg6.0 apt
sh /source/install.sh
SH
exit: 1,
out: [
match("runtime shared libraries are missing"),
match("libdbus-1"),
match("libudev")
]
run_test "fedora: warns about missing runtime libraries",
distro: :fedora,
cmd: "dnf -y install curl; rpm -e --nodeps systemd-libs; sh /source/install.sh",
exit: 1,
out: [
match("runtime shared libraries are missing"),
match("libdbus-1"),
match("libudev")
]
run_test "debian: successful install",
distro: :debian,
cmd: "apt-get update && apt-get -y install curl libdbus-1-3 libudev1; sh /source/install.sh",
out: [
match("Stellar CLI installed successfully"),
doesnt_match("runtime shared libraries are missing")
]
run_test "ubuntu: successful install",
distro: :ubuntu,
cmd: "apt-get update && apt-get -y install curl libdbus-1-3 libudev1; sh /source/install.sh",
out: [
match("Stellar CLI installed successfully"),
doesnt_match("runtime shared libraries are missing")
]
run_test "fedora: successful install",
distro: :fedora,
cmd: "dnf -y install curl dbus-libs systemd-libs; sh /source/install.sh",
out: [
match("Stellar CLI installed successfully"),
doesnt_match("runtime shared libraries are missing")
]
run_test "debian: install missing dependencies",
distro: :debian,
cmd: "apt-get update && apt-get -y install curl; sh /source/install.sh --install-deps",
out: [
match("Stellar CLI installed successfully"),
match("[OK] rustup"),
match("[OK] cargo"),
match("[OK] rustc"),
match("[OK] wasm32v1-none target"),
doesnt_match("runtime shared libraries are missing")
]
run_test "ubuntu: install missing dependencies",
distro: :ubuntu,
cmd: "apt-get update && apt-get -y install curl; sh /source/install.sh --install-deps",
out: [
match("Stellar CLI installed successfully"),
match("[OK] rustup"),
match("[OK] cargo"),
match("[OK] rustc"),
match("[OK] wasm32v1-none target"),
doesnt_match("runtime shared libraries are missing")
]
run_test "fedora: install missing dependencies",
distro: :fedora,
cmd: "dnf -y install curl; sh /source/install.sh --install-deps",
out: [
match("Stellar CLI installed successfully"),
match("[OK] rustup"),
match("[OK] cargo"),
match("[OK] rustc"),
match("[OK] wasm32v1-none target"),
doesnt_match("runtime shared libraries are missing")
]
exit $EXPECTED_EXIT