-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts_test.go
More file actions
298 lines (286 loc) · 9.06 KB
/
Copy pathscripts_test.go
File metadata and controls
298 lines (286 loc) · 9.06 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package execenv_test
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/sudosylabs/execenv"
"github.com/sudosylabs/execenv/isolated"
)
func TestFirstLanguageRecipesExist(t *testing.T) {
t.Parallel()
wantFROM := map[string]string{
"python": "FROM python:",
"node": "FROM node:",
"go": "FROM golang:",
"java": "FROM eclipse-temurin:",
}
for id, prefix := range wantFROM {
recipe := filepath.Join("catalog", id, "recipe.json")
if _, err := os.Stat(recipe); err != nil {
t.Fatalf("%s: %v", recipe, err)
}
df := filepath.Join("catalog", id, "Dockerfile")
body, err := os.ReadFile(df)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(body), prefix) {
t.Fatalf("%s does not FROM official slim image: %s", df, body)
}
if strings.Contains(string(body), "execenv") {
t.Fatalf("%s installs the agent; bake must inject it", df)
}
}
if _, err := os.Stat(filepath.Join("catalog", "default", "recipe.json")); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(filepath.Join("catalog", "default", "Dockerfile")); !os.IsNotExist(err) {
t.Fatal("default must not have a Dockerfile; it uses the universal-class source")
}
}
func TestAssembleReleaseWritesIndexAndChecksums(t *testing.T) {
t.Parallel()
dir := t.TempDir()
kernel := filepath.Join(dir, "vmlinux")
rootfs := filepath.Join(dir, "rootfs-python.ext4")
if err := os.WriteFile(kernel, []byte("kernel"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(rootfs, []byte("disk"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "execenv"), []byte("bin"), 0o755); err != nil {
t.Fatal(err)
}
cmd := exec.Command(filepath.Join("scripts", "assemble-release"), dir)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("assemble-release: %v\n%s", err, out)
}
idx, err := os.ReadFile(filepath.Join(dir, "index.json"))
if err != nil {
t.Fatal(err)
}
sum, err := isolated.Digest(kernel, rootfs)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(idx), `"id": "python"`) || !strings.Contains(string(idx), sum) {
t.Fatalf("index = %s", idx)
}
sums, err := os.ReadFile(filepath.Join(dir, "SHA256SUMS"))
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(sums), "execenv") || !strings.Contains(string(sums), "index.json") {
t.Fatalf("SHA256SUMS = %s", sums)
}
}
func TestPinImageAppendsWithoutRewritingOtherHashes(t *testing.T) {
t.Parallel()
dir := t.TempDir()
kernel := filepath.Join(dir, "vmlinux")
python := filepath.Join(dir, "rootfs-python.ext4")
if err := os.WriteFile(kernel, []byte("kernel"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(python, []byte("disk"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "execenv"), []byte("bin"), 0o755); err != nil {
t.Fatal(err)
}
cmd := exec.Command(filepath.Join("scripts", "assemble-release"), dir)
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("assemble-release: %v\n%s", err, out)
}
pythonHash, err := isolated.Digest(kernel, python)
if err != nil {
t.Fatal(err)
}
def := filepath.Join(dir, "rootfs-default.ext4")
if err := os.WriteFile(def, []byte("default-disk"), 0o644); err != nil {
t.Fatal(err)
}
pin := exec.Command(filepath.Join("scripts", "pin-image"),
"--dir", dir, "--id", "default", "--rootfs", def, "--kernel", kernel)
if out, err := pin.CombinedOutput(); err != nil {
t.Fatalf("pin-image: %v\n%s", err, out)
}
idx, err := os.ReadFile(filepath.Join(dir, "index.json"))
if err != nil {
t.Fatal(err)
}
text := string(idx)
if !strings.Contains(text, pythonHash) || !strings.Contains(text, `"id": "default"`) {
t.Fatalf("index = %s", text)
}
again := exec.Command(filepath.Join("scripts", "pin-image"),
"--dir", dir, "--id", "default", "--rootfs", def, "--kernel", kernel)
if out, err := again.CombinedOutput(); err != nil {
t.Fatalf("pin-image again: %v\n%s", err, out)
}
other := filepath.Join(dir, "other.ext4")
if err := os.WriteFile(other, []byte("other"), 0o644); err != nil {
t.Fatal(err)
}
bad := exec.Command(filepath.Join("scripts", "pin-image"),
"--dir", dir, "--id", "default", "--rootfs", other, "--kernel", kernel)
if err := bad.Run(); err == nil {
t.Fatal("pin-image accepted a hash change")
}
}
func TestExecenvDoesNotImportManager(t *testing.T) {
t.Parallel()
cmd := exec.Command("go", "list", "-f", "{{ join .Imports \"\\n\" }}", "./cmd/execenv")
out, err := cmd.Output()
if err != nil {
t.Fatal(err)
}
text := string(out)
if strings.Contains(text, "github.com/spf13/cobra") {
t.Fatal("execenv imports cobra")
}
if strings.Contains(text, "github.com/sudosylabs/execenv/internal/ctl") {
t.Fatal("execenv imports the manager package")
}
}
func TestCLIHasOnlyDaemonAndAgent(t *testing.T) {
t.Parallel()
body, err := os.ReadFile(filepath.Join("cmd", "execenv", "main.go"))
if err != nil {
t.Fatal(err)
}
text := string(body)
if strings.Contains(text, `"bake"`) || strings.Contains(text, "runBake") {
t.Fatal("execenv CLI still has a bake command")
}
if strings.Contains(text, `"bootstrap"`) || strings.Contains(text, "runBootstrap") {
t.Fatal("execenv CLI still has a bootstrap command")
}
}
func TestBakeScriptIsCIOnlyAndFailsClosed(t *testing.T) {
t.Parallel()
script := filepath.Join("scripts", "bake")
info, err := os.Stat(script)
if err != nil {
t.Fatal(err)
}
if info.Mode()&0o111 == 0 {
t.Fatal("scripts/bake is not executable")
}
body, err := os.ReadFile(script)
if err != nil {
t.Fatal(err)
}
text := string(body)
if !strings.Contains(text, "mcr.microsoft.com/devcontainers/universal:linux") {
t.Fatal("scripts/bake does not pin the universal-class source")
}
if strings.Contains(text, "execenv bake") {
t.Fatal("scripts/bake still invokes an execenv bake command")
}
if !strings.Contains(text, "--platform linux/amd64") {
t.Fatal("scripts/bake does not pin linux/amd64")
}
dir := t.TempDir()
kernel := filepath.Join(dir, "k")
agent := filepath.Join(dir, "a")
if err := os.WriteFile(kernel, []byte("k"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(agent, []byte("a"), 0o644); err != nil {
t.Fatal(err)
}
cmd := exec.Command(script, "--out", filepath.Join(dir, "out"), "--kernel", kernel, "--agent", agent)
cmd.Env = []string{"PATH=/nonexistent"}
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatal("bake succeeded without docker")
}
if !strings.Contains(string(out), "docker or podman") {
t.Fatalf("error = %s", out)
}
}
func TestGuestInitStartsAgentWithoutNetwork(t *testing.T) {
t.Parallel()
body, err := os.ReadFile(filepath.Join("scripts", "guest.sh"))
if err != nil {
t.Fatal(err)
}
text := string(body)
if !strings.Contains(text, execenv.GuestHome) || !strings.Contains(text, "guest-init") || !strings.Contains(text, "-contain-workload") {
t.Fatal("guest init does not start the contained agent with a child reaper")
}
if !strings.Contains(text, "mount -t cgroup2") || !strings.Contains(text, "|| exit 1") {
t.Fatal("guest init does not require the kernel workload hierarchy")
}
if !strings.Contains(text, execenv.GuestBin) {
t.Fatal("guest init does not exec the installed binary")
}
if strings.Contains(text, "apk ") || strings.Contains(text, "apt") || strings.Contains(text, "curl ") {
t.Fatal("guest init reaches the network")
}
}
func TestCatalogHashIsKernelThenRootfs(t *testing.T) {
t.Parallel()
dir := t.TempDir()
kernel := filepath.Join(dir, "vmlinux")
rootfs := filepath.Join(dir, "rootfs.ext4")
if err := os.WriteFile(kernel, []byte("kernel"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(rootfs, []byte("disk"), 0o644); err != nil {
t.Fatal(err)
}
sum, err := isolated.Digest(kernel, rootfs)
if err != nil {
t.Fatal(err)
}
cmd := exec.Command("sh", "-c", "cat -- \"$1\" \"$2\" | sha256sum | awk '{print $1}'", "hash", kernel, rootfs)
out, err := cmd.Output()
if err != nil {
t.Skip("sha256sum not available")
}
if strings.TrimSpace(string(out)) != sum {
t.Fatalf("script hash = %q, isolated.Digest = %q", strings.TrimSpace(string(out)), sum)
}
}
func TestDaemonDoesNotImportBake(t *testing.T) {
t.Parallel()
err := filepath.Walk("daemon", func(path string, info os.FileInfo, err error) error {
if err != nil || info.IsDir() || !strings.HasSuffix(path, ".go") {
return err
}
body, err := os.ReadFile(path)
if err != nil {
return err
}
text := string(body)
if strings.Contains(text, "/bake") || strings.Contains(text, "docker") || strings.Contains(text, "podman") {
t.Fatalf("%s mentions bake or a container client", path)
}
return nil
})
if err != nil {
t.Fatal(err)
}
}
func TestShellBootstrapIsRetired(t *testing.T) {
t.Parallel()
if _, err := os.Stat(filepath.Join("scripts", "bootstrap")); !os.IsNotExist(err) {
t.Fatal("scripts/bootstrap still exists; host install is execenvctl")
}
body, err := os.ReadFile("Makefile")
if err != nil {
t.Fatal(err)
}
if strings.Contains(string(body), "scripts/bootstrap") {
t.Fatal("Makefile still invokes the shell installer")
}
if !strings.Contains(string(body), "execenvctl bootstrap") {
t.Fatal("Makefile does not point operators at execenvctl")
}
}