Repository navigation
Expand file tree
/
Copy pathscripts_linux_test.go
More file actions
403 lines (390 loc) · 16.8 KB
/
Copy pathscripts_linux_test.go
File metadata and controls
403 lines (390 loc) · 16.8 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package nodkey
import (
"bytes"
"crypto/sha256"
"encoding/base64"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
// The tests of scripts/build-release.sh and scripts/release-check/ (the
// Release check workflow, docs/releasing.md). The checks themselves run
// in that workflow, with real releases; here are the parts that decide:
// the arguments, N-1 and N-2, and the changelog rules.
// runScript runs sh SCRIPT ARGS in dir with stdin, and returns the
// output and the exit code.
func runScript(t *testing.T, dir, stdin, script string, args ...string) (string, int) {
t.Helper()
abs, err := filepath.Abs(script)
if err != nil {
t.Fatal(err)
}
cmd := exec.CommandContext(t.Context(), "/bin/sh", append([]string{abs}, args...)...) //nolint:gosec // G204: the test runs a script of the repository.
cmd.Dir = dir
cmd.Stdin = strings.NewReader(stdin)
var out bytes.Buffer
cmd.Stdout, cmd.Stderr = &out, &out
err = cmd.Run()
code := 0
if exit, ok := err.(*exec.ExitError); ok { //nolint:errorlint // the error of Run is an *exec.ExitError or another.
code = exit.ExitCode()
} else if err != nil {
t.Fatal(err)
}
return out.String(), code
}
func TestBuildReleaseRefuses(t *testing.T) {
out := filepath.Join(t.TempDir(), "dist")
for _, c := range []struct {
name string
args []string
want string
}{
{"no arguments", nil, "give VERSION and OUT_DIR"},
{"a v before the version", []string{"v1.2.3", out}, "is not X.Y.Z"},
{"two parts", []string{"1.2", out}, "is not X.Y.Z"},
{"a leading zero", []string{"01.2.3", out}, "is not X.Y.Z"},
{"a second line", []string{"1.2.3\nx", out}, "line break"},
{"an existing folder", []string{"1.2.3", t.TempDir()}, "exists already"},
} {
t.Run(c.name, func(t *testing.T) {
got, code := runScript(t, ".", "", "scripts/build-release.sh", c.args...)
if code != 1 || !strings.Contains(got, c.want) {
t.Errorf("exit %d, output %q; want exit 1 and %q", code, got, c.want)
}
})
}
if _, err := os.Stat(out); !os.IsNotExist(err) {
t.Errorf("a refused build made %s", out)
}
}
func TestPreviousReleases(t *testing.T) {
if _, err := exec.LookPath("jq"); err != nil {
skipOrFail(t, "jq is missing: %v", err)
}
const list = `[{"tag_name":"v0.9.0","draft":true,"prerelease":false,"published_at":null},
{"tag_name":"v0.8.1","draft":false,"prerelease":true,"published_at":"2026-01-02T00:00:00Z"},
{"tag_name":"v0.10.0","draft":false,"prerelease":false,"published_at":"2026-01-03T00:00:00Z"},
{"tag_name":"v0.2.0","draft":false,"prerelease":false,"published_at":"2026-01-01T00:00:00Z"}]
[{"tag_name":"v0.9.1","draft":false,"prerelease":false,"published_at":"2026-01-04T00:00:00Z"},
{"tag_name":"v0.12.0","draft":false,"prerelease":false,"published_at":"2026-01-05T00:00:00Z"},
{"tag_name":"v0.11.0-rc.1","draft":false,"prerelease":false,"published_at":"2026-01-05T00:00:00Z"},
{"tag_name":"0.10.5","draft":false,"prerelease":false,"published_at":"2026-01-05T00:00:00Z"}]`
for _, c := range []struct {
name, version, stdin, want string
code int
}{
// A draft, a prerelease, a newer release and odd tags do not count;
// 0.10.0 is above 0.9.1 (numbers, not text).
{"two below", "0.11.0", list, "0.10.0 0.9.1\n", 0},
{"one below", "0.9.0", list, "0.2.0\n", 0},
{"the candidate is published already", "0.10.0", list, "0.9.1 0.2.0\n", 0},
{"none below", "0.1.0", list, "no published release below 0.1.0", 1},
{"no releases", "1.0.0", "[]", "no published release below 1.0.0", 1},
{"a bad version", "v1.0.0", list, "is not X.Y.Z", 1},
} {
t.Run(c.name, func(t *testing.T) {
got, code := runScript(t, ".", c.stdin, "scripts/release-check/previous.sh", c.version)
if code != c.code || !strings.Contains(got, c.want) {
t.Errorf("exit %d, output %q; want exit %d and %q", code, got, c.code, c.want)
}
})
}
}
// changelogRepo makes a git repository with the tag v0.1.0, then the
// commits with the subjects, and CHANGELOG.md and docs/updating.md.
func changelogRepo(t *testing.T, notes, updating string, subjects ...string) string {
t.Helper()
dir := t.TempDir()
git := func(args ...string) {
t.Helper()
cmd := exec.CommandContext(t.Context(), "git", args...) //nolint:gosec // G204: git with the arguments of the test.
cmd.Dir = dir
cmd.Env = append(os.Environ(), "GIT_AUTHOR_NAME=test", "GIT_AUTHOR_EMAIL=test@example.com",
"GIT_COMMITTER_NAME=test", "GIT_COMMITTER_EMAIL=test@example.com", "GIT_CONFIG_GLOBAL=/dev/null")
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("git %q: %v\n%s", args, err, out)
}
}
git("init", "-q", "-b", "main")
git("commit", "-q", "--allow-empty", "-m", "feat: the first release (#1)")
git("tag", "v0.1.0")
for _, s := range subjects {
git("commit", "-q", "--allow-empty", "-m", s)
}
if err := os.MkdirAll(filepath.Join(dir, "docs"), 0o700); err != nil {
t.Fatal(err)
}
writeFile(t, filepath.Join(dir, "CHANGELOG.md"), []byte("# Changelog\n\n## [0.2.0](https://example.com) (2026-01-02)\n\n"+notes+
"\n## [0.1.0](https://example.com) (2026-01-01)\n\n* the first release ([#1](https://example.com/1))\n"), 0o600)
writeFile(t, filepath.Join(dir, "docs", "updating.md"), []byte("# Updating nodkey\n\n"+updating), 0o600)
git("add", "CHANGELOG.md", "docs/updating.md")
git("commit", "-q", "-m", "chore(main): release 0.2.0")
return dir
}
func TestChangelogCheck(t *testing.T) {
if _, err := exec.LookPath("git"); err != nil {
skipOrFail(t, "git is missing: %v", err)
}
script, err := filepath.Abs("scripts/release-check/changelog.sh")
if err != nil {
t.Fatal(err)
}
const notes = "### Features\n\n* a feature ([#2](https://example.com/2))\n* a change ([#4](https://example.com/4))\n"
const updating = "## Updating from 0.1.0\n\n**What changed (0.2.0, #4).** Something.\n"
for _, c := range []struct {
name string
notes string
updating string
subjects []string
want string
code int
}{
{"all good", notes, updating,
[]string{"feat: a feature (#2)", "ci: a hidden change (#3)", "feat(run)!: a change (#4)"}, "ok: #4 has its section", 0},
{"a feature is missing in the notes", notes, updating,
[]string{"feat: a feature (#2)", "fix: a fix (#5)"}, "FAILED: #5 (fix) is not in the notes of 0.2.0", 1},
{"no section for a ! change", notes, "",
[]string{"feat(run)!: a change (#4)"}, `has ! but docs/updating.md has no section "## Updating from 0.1.0"`, 1},
{"the section does not name the change", notes, "## Updating from 0.1.0\n\nOther.\n",
[]string{"feat(run)!: a change (#4)"}, "does not name #4", 1},
{"a hidden type with !", notes, updating,
[]string{"ci!: a hidden break (#6)"}, "FAILED: #6 has ! but is not in the notes", 1},
{"a commit without a pull request", notes, updating,
[]string{"feat: pushed to main"}, "has no (#N) at the end", 1},
{"a subject that is not a Conventional Commit", "* x (#7)\n", updating,
[]string{"Add a thing (#7)"}, "not a Conventional Commit title", 1},
{"#2 does not count for #20", notes, updating,
[]string{"feat: another feature (#20)"}, "FAILED: #20 (feat)", 1},
} {
t.Run(c.name, func(t *testing.T) {
dir := changelogRepo(t, c.notes, c.updating, c.subjects...)
got, code := runScript(t, dir, "", script, "0.1.0", "0.2.0")
if code != c.code || !strings.Contains(got, c.want) {
t.Errorf("exit %d, output:\n%s\nwant exit %d and %q", code, got, c.code, c.want)
}
})
}
t.Run("no tag of N-1", func(t *testing.T) {
dir := changelogRepo(t, notes, updating, "feat: a feature (#2)")
got, code := runScript(t, dir, "", script, "0.0.9", "0.2.0")
if code != 2 || !strings.Contains(got, "no tag v0.0.9") {
t.Errorf("exit %d, output %q", code, got)
}
})
t.Run("no section of the version", func(t *testing.T) {
dir := changelogRepo(t, notes, updating, "feat: a feature (#2)")
got, code := runScript(t, dir, "", script, "0.1.0", "0.3.0")
if code != 2 || !strings.Contains(got, "no section for 0.3.0") {
t.Errorf("exit %d, output %q", code, got)
}
})
}
func TestConfigsCheckRefusesAWrongCorpus(t *testing.T) {
// Fake nodkeys: "old" accepts every config, "new" refuses every one.
bin := t.TempDir()
writeFile(t, filepath.Join(bin, "old"), []byte("#!/bin/sh\nexit 0\n"), 0o700)
writeFile(t, filepath.Join(bin, "new"), []byte("#!/bin/sh\nexit 1\n"), 0o700)
corpus := t.TempDir()
writeFile(t, filepath.Join(corpus, "ok-a.toml"), []byte("x = 1\n"), 0o600)
writeFile(t, filepath.Join(corpus, "bad-b.toml"), []byte("x = 1\n"), 0o600)
writeFile(t, filepath.Join(corpus, "c.toml"), []byte("x = 1\n"), 0o600)
home := t.TempDir()
t.Setenv("HOME", home)
got, code := runScript(t, ".", "", "scripts/release-check/configs.sh",
filepath.Join(bin, "old"), filepath.Join(bin, "new"), corpus)
for _, want := range []string{
"FAILED: ok-a.toml: the old nodkey exits with 0, the new one with 1",
"FAILED: bad-b.toml: the old nodkey exits with 0, want 1; fix the corpus",
"FAILED: c.toml: the name must start with ok- or bad-",
} {
if !strings.Contains(got, want) {
t.Errorf("the output has no %q:\n%s", want, got)
}
}
if code != 1 {
t.Errorf("exit %d, want 1", code)
}
if left, _ := filepath.Glob(filepath.Join(home, "nodkey-configs.*")); len(left) > 0 {
t.Errorf("the check left %q", left)
}
}
func TestFragmentRefusesAnUnknownResult(t *testing.T) {
out := filepath.Join(t.TempDir(), "r.json")
got, code := runScript(t, ".", "", "scripts/release-check/fragment.sh", "1 build", "maybe", "/nonexistent", out)
if code != 2 || !strings.Contains(got, "RESULT must be pass, fail or report") {
t.Errorf("exit %d, output %q", code, got)
}
}
// verifyFixture makes the files of a release 1.2.3 in a new folder, and
// fake gh, go and docker programs in another, for verify-release.sh. The
// nodkey of each tarball is a script that prints "nodkey 1.2.3".
type verifyFixture struct {
dir, bin, commit string
head bool // run without NODKEY_VERIFY_COMMIT
}
func newVerifyFixture(t *testing.T) *verifyFixture {
t.Helper()
f := &verifyFixture{dir: t.TempDir(), bin: t.TempDir(), commit: strings.Repeat("ab", 20)}
nodkey := []byte("#!/bin/sh\necho \"nodkey ${FAKE_VERSION:-1.2.3}\"\n")
var sums, subjects strings.Builder
for _, kind := range []string{"ansible", "linux_amd64", "linux_arm64", "pullgate"} {
name := "nodkey_1.2.3_" + kind + ".tar.gz"
data := tarGz(t, tarFile{name: "nodkey", mode: 0o755, data: nodkey})
writeFile(t, filepath.Join(f.dir, name), data, 0o600)
sum := sha256.Sum256(data)
fmt.Fprintf(&sums, "%x %s\n", sum, name)
if subjects.Len() > 0 {
subjects.WriteString(",")
}
fmt.Fprintf(&subjects, `{"name":%q,"digest":{"sha256":"%x"}}`, name, sum)
}
writeFile(t, filepath.Join(f.dir, "SHA256SUMS"), []byte(sums.String()), 0o600)
payload := base64.StdEncoding.EncodeToString([]byte(`{"subject":[` + subjects.String() + `]}`))
writeFile(t, filepath.Join(f.dir, "nodkey_1.2.3.sigstore.json"), []byte(`{"dsseEnvelope":{"payload":"`+payload+`"}}`), 0o600)
writeFile(t, filepath.Join(f.bin, "gh"), []byte("#!/bin/sh\n[ ! -e \"$FAKE_FAIL/gh\" ]\n"), 0o700)
// go version -m: the arch is in the name of the folder of the binary.
writeFile(t, filepath.Join(f.bin, "go"), []byte(`#!/bin/sh
arch=$(basename "$(dirname "$3")")
printf '%s\n\tmod\tgithub.com/code6x/nodkey\tv1.2.3\t\n\tbuild\tGOARCH=%s\n\tbuild\tvcs.revision=%s\n\tbuild\tvcs.modified=false\n' "$3" "$arch" "$(cat "$FAKE_FAIL/../commit")"
`), 0o700)
writeFile(t, filepath.Join(f.bin, "docker"), []byte("#!/bin/sh\n[ ! -e \"$FAKE_FAIL/docker\" ] || exit 1\necho \"nodkey ${FAKE_DOCKER_VERSION:-1.2.3}\"\n"), 0o700)
return f
}
// run runs verify-release.sh with the fakes first in PATH.
func (f *verifyFixture) run(t *testing.T, fail ...string) (string, int) {
t.Helper()
state := t.TempDir()
failDir := filepath.Join(state, "fail")
mkdir(t, failDir, 0o700)
writeFile(t, filepath.Join(state, "commit"), []byte(f.commit), 0o600)
for _, name := range fail {
writeFile(t, filepath.Join(failDir, name), nil, 0o600)
}
gh := filepath.Join(t.TempDir(), "gh.tar.gz")
writeFile(t, gh, []byte("gh"), 0o600)
t.Setenv("PATH", f.bin+string(os.PathListSeparator)+os.Getenv("PATH"))
t.Setenv("FAKE_FAIL", failDir)
t.Setenv("NODKEY_VERIFY_COMMIT", strings.Repeat("ab", 20))
if f.head {
// The script then takes the commit of HEAD.
if err := os.Unsetenv("NODKEY_VERIFY_COMMIT"); err != nil {
t.Fatal(err)
}
}
return runScript(t, ".", "", "scripts/verify-release.sh", f.dir, "1.2.3", gh)
}
func TestVerifyRelease(t *testing.T) {
for _, p := range []string{"jq", "openssl", "base64"} {
if _, err := exec.LookPath(p); err != nil {
skipOrFail(t, "%s is missing: %v", p, err)
}
}
t.Run("the default commit is HEAD", func(t *testing.T) {
head, err := exec.CommandContext(t.Context(), "git", "rev-parse", "HEAD").Output()
if err != nil {
skipOrFail(t, "git rev-parse HEAD: %v", err)
}
f := newVerifyFixture(t)
f.commit, f.head = strings.TrimSpace(string(head)), true
got, code := f.run(t)
if code != 0 || !strings.Contains(got, "commit "+f.commit) {
t.Errorf("exit %d, output:\n%s", code, got)
}
})
t.Run("good files pass", func(t *testing.T) {
got, code := newVerifyFixture(t).run(t)
if code != 0 || strings.Contains(got, "FAILED") || strings.Count(got, "ok: ") != 7 {
t.Errorf("exit %d, output:\n%s", code, got)
}
})
for _, c := range []struct {
name string
change func(t *testing.T, f *verifyFixture) []string // returns the fakes that fail
want string
}{
{"an extra file", func(t *testing.T, f *verifyFixture) []string {
writeFile(t, filepath.Join(f.dir, "extra.txt"), nil, 0o600)
return nil
}, "FAILED: 1 the files of the draft are not the expected ones"},
{"a missing file", func(t *testing.T, f *verifyFixture) []string {
if err := os.Remove(filepath.Join(f.dir, "nodkey_1.2.3_pullgate.tar.gz")); err != nil {
t.Fatal(err)
}
return nil
}, "FAILED: 1 the files"},
{"a changed tarball", func(t *testing.T, f *verifyFixture) []string {
p := filepath.Join(f.dir, "nodkey_1.2.3_pullgate.tar.gz")
writeFile(t, p, append(readFile(t, p), 'x'), 0o600)
return nil
}, "FAILED: 2 a tarball does not match SHA256SUMS"},
{"SHA256SUMS without a tarball", func(t *testing.T, f *verifyFixture) []string {
p := filepath.Join(f.dir, "SHA256SUMS")
lines := strings.SplitAfter(string(readFile(t, p)), "\n")
writeFile(t, p, []byte(strings.Join(lines[1:], "")), 0o600)
return nil
}, "FAILED: 2 SHA256SUMS does not name each tarball exactly once"},
{"the attestation fails", func(*testing.T, *verifyFixture) []string { return []string{"gh"} },
"FAILED: 3 the attestation does not verify"},
{"the bundle names other files", func(t *testing.T, f *verifyFixture) []string {
payload := base64.StdEncoding.EncodeToString([]byte(`{"subject":[{"name":"other.tar.gz","digest":{"sha256":"00"}}]}`))
writeFile(t, filepath.Join(f.dir, "nodkey_1.2.3.sigstore.json"), []byte(`{"dsseEnvelope":{"payload":"`+payload+`"}}`), 0o600)
return nil
}, "FAILED: 3 the bundle does not name exactly the tarballs"},
{"a binary of another commit", func(_ *testing.T, f *verifyFixture) []string {
f.commit = strings.Repeat("cd", 20)
return nil
}, "FAILED: 4 nodkey_1.2.3_linux_amd64.tar.gz: the build info does not say"},
{"the install fails", func(*testing.T, *verifyFixture) []string { return []string{"docker"} },
"FAILED: 5 install.sh does not install"},
{"the install gives another version", func(t *testing.T, _ *verifyFixture) []string {
t.Setenv("FAKE_DOCKER_VERSION", "1.2.4")
return nil
}, "FAILED: 5 install.sh does not install"},
{"the amd64 binary says another version", func(t *testing.T, _ *verifyFixture) []string {
t.Setenv("FAKE_VERSION", "1.2.4")
return nil
}, "FAILED: 4 the amd64 binary says 'nodkey 1.2.4'"},
} {
t.Run(c.name, func(t *testing.T) {
f := newVerifyFixture(t)
got, code := f.run(t, c.change(t, f)...)
if code != 1 || !strings.Contains(got, c.want) {
t.Errorf("exit %d, output:\n%s\nwant exit 1 and %q", code, got, c.want)
}
})
}
for _, c := range []struct {
name string
env []string
args []string
want string
}{
{"a bad version", nil, []string{"v1.2.3"}, "is not X.Y.Z"},
{"a bad commit", []string{"NODKEY_VERIFY_COMMIT=abc"}, nil, "is not a full SHA-1"},
{"an unknown kind", []string{"NODKEY_VERIFY_KINDS=linux_amd64 windows"}, nil, "unknown kind 'windows'"},
{"no amd64", []string{"NODKEY_VERIFY_KINDS=pullgate"}, nil, "must have linux_amd64"},
} {
t.Run(c.name, func(t *testing.T) {
f := newVerifyFixture(t)
gh := filepath.Join(t.TempDir(), "gh.tar.gz")
writeFile(t, gh, []byte("gh"), 0o600)
args := []string{f.dir, "1.2.3", gh}
if c.args != nil {
args[1] = c.args[0]
}
t.Setenv("NODKEY_VERIFY_COMMIT", strings.Repeat("ab", 20))
for _, kv := range c.env {
k, v, _ := strings.Cut(kv, "=")
t.Setenv(k, v)
}
got, code := runScript(t, ".", "", "scripts/verify-release.sh", args...)
if code != 2 || !strings.Contains(got, c.want) {
t.Errorf("exit %d, output %q; want exit 2 and %q", code, got, c.want)
}
})
}
}