From cb0b5ed62b24fb2d305d9c3a2ed4a1c9c18f0fb6 Mon Sep 17 00:00:00 2001 From: pingqiu Date: Sat, 5 Dec 2026 13:19:00 -0800 Subject: [PATCH] Complete mini release assets and bounded process checks --- .github/workflows/block-release.yml | 3 +- .github/workflows/block-test.yml | 4 + .../mini_nonroot_integration_test.go | 98 +++++----- .../blockvolume/mini_process_linux_test.go | 183 ++++++++++++++++++ seaweed-block/cmd/sw-block/mini.go | 17 ++ 5 files changed, 250 insertions(+), 55 deletions(-) create mode 100644 seaweed-block/cmd/blockvolume/mini_process_linux_test.go diff --git a/.github/workflows/block-release.yml b/.github/workflows/block-release.yml index c15d562b5d..deac728e3a 100644 --- a/.github/workflows/block-release.yml +++ b/.github/workflows/block-release.yml @@ -135,8 +135,9 @@ jobs: docker cp "$container:/usr/local/bin/$binary" "$out/bin/$binary" done bash seaweed-block/scripts/refuse-lab-binary.sh "$out/bin/"* + cp seaweed-block/deploy/systemd/sw-block.tmpfiles.conf "$out/bin/sw-block.tmpfiles.conf" asset="sw-block-${version}-linux-amd64.tar.gz" - tar -C "$out/bin" -czf "$out/$asset" blockmaster blockvolume sw-block + tar -C "$out/bin" -czf "$out/$asset" blockmaster blockvolume sw-block sw-block.tmpfiles.conf (cd "$out" && sha256sum "$asset" >SHA256SUMS) prerelease=false if [[ "$version" == *-* ]]; then prerelease=true; fi diff --git a/.github/workflows/block-test.yml b/.github/workflows/block-test.yml index 39c1d55651..27ddaf8ce4 100644 --- a/.github/workflows/block-test.yml +++ b/.github/workflows/block-test.yml @@ -98,6 +98,10 @@ jobs: python3 -B ../.github/block/scripts/doc-links-check-test.py python3 -I ../.github/block/scripts/doc-links-check.py . + - name: Release binary archive contract + if: matrix.suite == 'unit' + run: python3 -I .github/scripts/release-binary-assets-test.py + - name: Runner disk is NVMe # A self-hosted runner whose work, temp or Go cache directory sits on a # SATA system disk times the process-level tests out under disk load; diff --git a/seaweed-block/cmd/blockvolume/mini_nonroot_integration_test.go b/seaweed-block/cmd/blockvolume/mini_nonroot_integration_test.go index 5a59792d98..184f6f8857 100644 --- a/seaweed-block/cmd/blockvolume/mini_nonroot_integration_test.go +++ b/seaweed-block/cmd/blockvolume/mini_nonroot_integration_test.go @@ -1,14 +1,11 @@ -//go:build subprocess && chunkstore_integration +//go:build linux && subprocess && chunkstore_integration package main_test import ( - "bufio" - "bytes" "context" "fmt" "os" - "os/exec" "path/filepath" "strings" "syscall" @@ -22,6 +19,7 @@ import ( // scheduled snapshot runs. A start that fails after --dir was made stops what // it started and prints the --reuse command for the same --dir. func TestMiniStartsWithoutRootAndNamesItsBinaries(t *testing.T) { + assertMiniUnprivileged(t) weed := os.Getenv("CHUNKSTORE_WEED") if weed == "" { t.Fatal("CHUNKSTORE_WEED must name the pinned real SeaweedFS binary") @@ -33,58 +31,42 @@ func TestMiniStartsWithoutRootAndNamesItsBinaries(t *testing.T) { miniGoBuild(t, filepath.Join(bins, b), "github.com/seaweedfs/seaweed-block/cmd/"+b) } } - t.Logf("uid %d; /run/sw-block exists: %v", os.Getuid(), exists("/run/sw-block")) cli := filepath.Join(bins, "sw-block") shift := 3000 + (os.Getpid()*13)%6000 dir := filepath.Join(t.TempDir(), "mini") ctx, cancel := context.WithTimeout(context.Background(), 4*time.Minute) defer cancel() - cmd := exec.CommandContext(ctx, cli, "mini", "--dir", dir, "--bin", bins, "--weed", weed, "--port-shift", fmt.Sprint(shift)) - cmd.WaitDelay = 20 * time.Second - var out bytes.Buffer - pr, pw, err := os.Pipe() - if err != nil { - t.Fatal(err) - } - cmd.Stdout, cmd.Stderr = pw, pw - if err := cmd.Start(); err != nil { - t.Fatal(err) - } - pw.Close() - running := make(chan bool, 1) - done := make(chan struct{}) - go func() { - defer close(done) - sc := bufio.NewScanner(pr) - for sc.Scan() { - out.WriteString(sc.Text() + string(rune(10))) - if strings.HasPrefix(sc.Text(), "sw-block mini: running") { - select { - case running <- true: - default: - } - } + p := startOwnedMini(t, ctx, cli, "mini", "--dir", dir, "--bin", bins, "--weed", weed, "--port-shift", fmt.Sprint(shift)) + for !strings.Contains(p.output.String(), "sw-block mini: running") { + select { + case <-p.done: + t.Fatalf("mini did not reach running: %v\n%s", p.err, p.output.String()) + case <-ctx.Done(): + t.Fatalf("mini startup expired: %s", p.output.String()) + case <-time.After(20 * time.Millisecond): } - running <- false - }() - ok := <-running - if !ok { - <-done - _ = cmd.Wait() - t.Fatalf("mini did not reach running:%s%s", string(rune(10)), out.String()) } - time.Sleep(2 * time.Second) // let the hints print - _ = cmd.Process.Signal(syscall.SIGINT) - werr := cmd.Wait() - <-done - t.Logf("sw-block mini output:%s%s", string(rune(10)), out.String()) - if werr != nil { - t.Fatalf("mini after Ctrl-C: %v", werr) + // The running line precedes the hints; await the last hint without reading + // a buffer concurrently with its writer. + for !strings.Contains(p.output.String(), weed+" shell -master=") { + select { + case <-p.done: + t.Fatalf("mini exited before its hints: %s", p.output.String()) + case <-ctx.Done(): + t.Fatalf("mini hints expired: %s", p.output.String()) + case <-time.After(20 * time.Millisecond): + } } + _ = p.cmd.Process.Signal(syscall.SIGINT) + if err := p.join(t, 40*time.Second); err != nil { + t.Fatalf("mini after Ctrl-C: %v\n%s", err, p.output.String()) + } + p.assertStopped(t) // before the safety-net cleanup: product shutdown owns this + t.Logf("sw-block mini output:\n%s", p.output.String()) if !exists(filepath.Join(dir, "locks", "v1.lock")) { t.Fatalf("the holder's lock is not under --dir") } - text := out.String() + text := p.output.String() if strings.Contains(text, "+ sw-block ") || strings.Contains(text, " sw-block ") { t.Errorf("mini prints a sw-block command without its absolute path") } @@ -107,24 +89,32 @@ func TestMiniStartsWithoutRootAndNamesItsBinaries(t *testing.T) { dir2 := filepath.Join(t.TempDir(), "mini") ctx2, cancel2 := context.WithTimeout(context.Background(), 3*time.Minute) defer cancel2() - c2 := exec.CommandContext(ctx2, cli, "mini", "--dir", dir2, "--bin", broken, "--weed", weed, "--port-shift", fmt.Sprint(shift+1)) - c2.WaitDelay = 20 * time.Second - b2, err := c2.CombinedOutput() - t.Logf("failed start:%s%s", string(rune(10)), b2) - if err == nil || !strings.Contains(string(b2), "start again on it with") || !strings.Contains(string(b2), cli+" mini --dir "+dir2+" --reuse") { + p2 := startOwnedMini(t, ctx2, cli, "mini", "--dir", dir2, "--bin", broken, "--weed", weed, "--port-shift", fmt.Sprint(shift+1)) + err := p2.join(t, 3*time.Minute+10*time.Second) + b2 := p2.output.String() + t.Logf("failed start:\n%s", b2) + if err == nil || !strings.Contains(b2, "start again on it with") || !strings.Contains(b2, cli+" mini --dir "+dir2+" --reuse") { t.Fatalf("a failed start: %v", err) } + for _, binary := range []string{weed, filepath.Join(broken, "blockmaster"), filepath.Join(broken, "blockvolume")} { + if !strings.Contains(b2, "+ "+binary+" ") { + t.Fatalf("failed-start path did not reach %s: %s", binary, b2) + } + } + p2.assertStopped(t) // rejects a failed start which left weed/master running + } func miniGoBuild(t *testing.T, out, pkg string) { t.Helper() ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute) defer cancel() - cmd := exec.CommandContext(ctx, "go", "build", "-o", out, pkg) - cmd.WaitDelay = 5 * time.Second - if b, err := cmd.CombinedOutput(); err != nil { - t.Fatalf("build %s: %v%s%s", pkg, err, string(rune(10)), b) + p := startOwnedMini(t, ctx, "go", "build", "-o", out, pkg) + if err := p.join(t, 3*time.Minute+10*time.Second); err != nil { + t.Fatalf("build %s: %v\n%s", pkg, err, p.output.String()) } + p.assertStopped(t) + } func exists(p string) bool { diff --git a/seaweed-block/cmd/blockvolume/mini_process_linux_test.go b/seaweed-block/cmd/blockvolume/mini_process_linux_test.go new file mode 100644 index 0000000000..5522b36393 --- /dev/null +++ b/seaweed-block/cmd/blockvolume/mini_process_linux_test.go @@ -0,0 +1,183 @@ +//go:build linux && subprocess && chunkstore_integration + +package main_test + +import ( + "bytes" + "context" + "errors" + "os" + "os/exec" + "path/filepath" + "strconv" + "strings" + "sync" + "syscall" + "testing" + "time" +) + +// Preconditions are assertions: a privileged run is not non-root evidence. +func assertMiniUnprivileged(t *testing.T) { + t.Helper() + if os.Getuid() == 0 || os.Geteuid() == 0 { + t.Fatal("non-root qualification must run without uid 0") + } + p := "/run/sw-block/locks" + for { + _, err := os.Stat(p) + if err == nil { + break + } + if !errors.Is(err, os.ErrNotExist) { + t.Fatalf("host lock preflight: %v", err) + } + p = filepath.Dir(p) + } + if err := syscall.Access(p, 3); err == nil { + t.Fatalf("host lock path/ancestor %s is writable; non-root precondition is unproved", p) + } else if !errors.Is(err, syscall.EACCES) && !errors.Is(err, syscall.EPERM) { + t.Fatalf("host lock access preflight: %v", err) + } + t.Logf("non-root uid=%d euid=%d; host lock ancestor %s is not writable", os.Getuid(), os.Geteuid(), p) +} + +type miniOutput struct { + mu sync.Mutex + b bytes.Buffer +} + +func (b *miniOutput) Write(p []byte) (int, error) { + b.mu.Lock() + defer b.mu.Unlock() + return b.b.Write(p) +} +func (b *miniOutput) String() string { b.mu.Lock(); defer b.mu.Unlock(); return b.b.String() } + +type ownedMini struct { + cmd *exec.Cmd + done chan struct{} + err error + output miniOutput + stopOnce sync.Once +} + +// All descendants inherit this dedicated group. Cleanup is registered before +// any assertion can fail; TERM, bounded join, KILL, bounded join. No name kill. +func startOwnedMini(t *testing.T, ctx context.Context, bin string, args ...string) *ownedMini { + t.Helper() + p := &ownedMini{cmd: exec.CommandContext(ctx, bin, args...), done: make(chan struct{})} + p.cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} + p.cmd.Stdout, p.cmd.Stderr = &p.output, &p.output + p.cmd.WaitDelay = 3 * time.Second + p.cmd.Cancel = func() error { return syscall.Kill(-p.cmd.Process.Pid, syscall.SIGTERM) } + if err := p.cmd.Start(); err != nil { + t.Fatal(err) + } + go func() { p.err = p.cmd.Wait(); close(p.done) }() + t.Cleanup(func() { p.stop(t) }) + return p +} +func (p *ownedMini) join(t *testing.T, bound time.Duration) error { + t.Helper() + select { + case <-p.done: + return p.err + case <-time.After(bound): + t.Fatalf("owned process did not join in %s", bound) + return nil + } +} +func (p *ownedMini) stop(t *testing.T) { + p.stopOnce.Do(func() { p.stopGroup(t) }) +} +func (p *ownedMini) stopGroup(t *testing.T) { + t.Helper() + pgid := p.cmd.Process.Pid + if pgid <= 1 { + t.Error("invalid owned process group") + return + } + _ = syscall.Kill(-pgid, syscall.SIGTERM) + deadline := time.Now().Add(2 * time.Second) + for len(miniGroupLive(t, pgid)) != 0 && time.Now().Before(deadline) { + time.Sleep(20 * time.Millisecond) + } + _ = syscall.Kill(-pgid, syscall.SIGKILL) + _ = p.join(t, 5*time.Second) + p.assertStopped(t) +} +func (p *ownedMini) assertStopped(t *testing.T) { + t.Helper() + deadline := time.Now().Add(5 * time.Second) + for { + live := miniGroupLive(t, p.cmd.Process.Pid) + if len(live) == 0 { + return + } + if time.Now().After(deadline) { + t.Fatalf("owned group %d still has live descendants %v", p.cmd.Process.Pid, live) + } + time.Sleep(20 * time.Millisecond) + } +} +func miniGroupLive(t *testing.T, pgid int) []int { + t.Helper() + entries, err := os.ReadDir("/proc") + if err != nil { + t.Fatal(err) + } + var live []int + for _, entry := range entries { + pid, err := strconv.Atoi(entry.Name()) + if err != nil { + continue + } + b, err := os.ReadFile(filepath.Join("/proc", entry.Name(), "stat")) + if errors.Is(err, os.ErrNotExist) { + continue + } + if err != nil { + t.Fatal(err) + } + end := strings.LastIndexByte(string(b), ')') + if end < 0 { + t.Fatalf("invalid proc stat for %d", pid) + } + fields := strings.Fields(string(b[end+1:])) + if len(fields) < 3 { + t.Fatal("short proc stat") + } + group, err := strconv.Atoi(fields[2]) + if err != nil { + t.Fatal(err) + } + if group == pgid && fields[0] != "Z" && fields[0] != "X" { + live = append(live, pid) + } + } + return live +} + +// An intentionally TERM-ignoring descendant exercises the safety net itself. +func TestMiniOwnedGroupCleanupStopsDescendants(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) + defer cancel() + p := startOwnedMini(t, ctx, "/bin/sh", "-c", "trap '' TERM; sleep 60 & echo ready; wait") + deadline := time.Now().Add(5 * time.Second) + for !strings.Contains(p.output.String(), "ready") { + if time.Now().After(deadline) { + t.Fatal("fixture did not start") + } + time.Sleep(10 * time.Millisecond) + } + if live := miniGroupLive(t, p.cmd.Process.Pid); len(live) < 2 { + t.Fatalf("descendant fixture not reached: %v", live) + } + p.stop(t) + select { + case <-p.done: + default: + t.Fatal("leader not joined") + } +} diff --git a/seaweed-block/cmd/sw-block/mini.go b/seaweed-block/cmd/sw-block/mini.go index 8bf451120a..51f4f7861f 100644 --- a/seaweed-block/cmd/sw-block/mini.go +++ b/seaweed-block/cmd/sw-block/mini.go @@ -121,6 +121,13 @@ func reuseMiniRecord(c *miniConfig, weedFound string) (string, error) { if err := json.Unmarshal(b, &r); err != nil { return "", fmt.Errorf("--reuse: %s/%s: %w", c.Dir, miniRecordFile, err) } + if r.Weed != "" { + abs, err := filepath.Abs(r.Weed) + if err != nil { + return "", fmt.Errorf("--reuse: resolve weed path: %w", err) + } + r.Weed = abs + } differs := func(flag string, now, then any) error { if c.Explicit[flag] && fmt.Sprint(now) != fmt.Sprint(then) { return fmt.Errorf("--reuse: --%s %v differs from this dir's run (%v); drop it to reuse the run as recorded", flag, now, then) @@ -456,6 +463,16 @@ func mini(ctx context.Context, cfg miniConfig, stdout, stderr io.Writer) (int, e } else if st, err := os.Stat(weed); err != nil || st.IsDir() { return ops.VolumeStatusExitInvalid, fmt.Errorf("--weed %s not found", weed) } + if weed != "" { + abs, err := filepath.Abs(weed) + if err != nil { + return ops.VolumeStatusExitInvalid, fmt.Errorf("resolve weed path: %w", err) + } + weed = abs + if cfg.Weed != "" { + cfg.Weed = abs + } + } // A reuse takes the recorded run (its binaries included) before any // lookup; a fresh start finds the binaries next to sw-block by default. if cfg.Reuse {