Files
seaweedfs/seaweed-block/cmd/sw-block/volume_operator_text_test.go
T
Ping QiuandClaude Opus 5.5 a5686e7fa7 Sync seaweed-block v1.1.1 into seaweed-block/ (#13411)
Brings seaweed-block main (v1.1.1 plus the docs and a test fix after it)
into seaweed-block/ as one commit. Only files under seaweed-block/ change,
and the subtree equals seaweed-block main exactly. The module's full
history stays in the seaweed-block repository.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-04 19:04:53 -08:00

147 lines
6.5 KiB
Go

package main
import (
"bytes"
"strings"
"testing"
"time"
)
// --help prints the usage and the verb's flags to stdout and exits 0, for
// every verb, before (and without) the root check.
func TestVolumeHelpPrintsTheUsageWithoutRoot(t *testing.T) {
for _, verb := range []string{"start", "stop", "backup", "restore"} {
for _, help := range []string{"--help", "-h"} {
var out, errb bytes.Buffer
code := run([]string{"volume", verb, help}, &out, &errb)
if code != 0 || !strings.Contains(out.String(), "usage: sudo /usr/local/bin/sw-block volume start") || strings.Contains(errb.String(), "needs root") {
t.Fatalf("volume %s %s: %d stdout %q stderr %q; want 0 and the usage", verb, help, code, out.String(), errb.String())
}
}
}
var out, errb bytes.Buffer
if code := run([]string{"volume", "start", "vol-a", "--help"}, &out, &errb); code != 0 || !strings.Contains(out.String(), "-server-id") {
t.Fatalf("volume start vol-a --help: %d %q %q; want 0 and start's flags", code, out.String(), errb.String())
}
}
// A start refused over another volume's settings prints no "+ write" step:
// only a start that writes the file says it writes it, and an identical
// re-run says it keeps it.
func TestARefusedStartPrintsNoSettingsWrite(t *testing.T) {
h := newFakeHost(t)
env := h.env(t)
v := h.spec(false)
if code, out, errs := runStart(env, v, startOpts{Wait: time.Second}); code != 0 || !strings.Contains(out, "+ write ") {
t.Fatalf("first start: %d %s %s; want a write step", code, out, errs)
}
other := v
other.NVMeListen = "10.0.0.7:4421"
code, out, errs := runStart(env, other, startOpts{Wait: time.Second})
if code == 0 || !strings.Contains(errs, "--replace") {
t.Fatalf("other settings: %d %s; want a refusal", code, errs)
}
if strings.Contains(out, "+ write") || strings.Contains(out, "+ keep") {
t.Fatalf("a refused start printed a settings step:\n%s", out)
}
if code, out, errs := runStart(env, v, startOpts{Wait: time.Second}); code != 0 || strings.Contains(out, "+ write") || !strings.Contains(out, "+ keep ") {
t.Fatalf("identical re-run: %d %s %s; want a keep step and no write", code, out, errs)
}
}
// With --iscsi-listen, start prints the iSCSI discovery and login, and
// "Before stopping" names the iSCSI logout next to the NVMe disconnect.
func TestAStartOverISCSIPrintsTheLoginAndTheLogout(t *testing.T) {
h := newFakeHost(t)
env := h.env(t)
stubResolve(t, "00112233445566778899aabbccddeeff", nil)
v := h.spec(true)
w := testSpecs()["with-iscsi"]
v.ISCSIListen, v.IQN = w.ISCSIListen, w.IQN
code, out, errs := runStart(env, v, startOpts{Wait: time.Second})
if code != 0 {
t.Fatalf("start: %d %s", code, errs)
}
for _, want := range []string{
"sudo iscsiadm -m discovery -t sendtargets -p 10.0.0.7:3260",
"sudo iscsiadm -m node -T iqn.2026-05.io.seaweedfs:vol-a -p 10.0.0.7:3260 --login",
"(over iSCSI: sudo iscsiadm -m node -T iqn.2026-05.io.seaweedfs:vol-a -p 10.0.0.7:3260 --logout)",
"sudo nvme disconnect -n nqn.2026-05.io.seaweedfs:vol-a",
} {
if !strings.Contains(out, want) {
t.Fatalf("output lacks %q:\n%s", want, out)
}
}
before := out[strings.Index(out, "Before stopping"):]
if !strings.Contains(before, "--logout") {
t.Fatalf("Before stopping does not name the iSCSI logout:\n%s", before)
}
// Control: without iSCSI, no iscsiadm line.
n := newFakeHost(t)
if code, out, errs := runStart(n.env(t), n.spec(false), startOpts{Wait: time.Second}); code != 0 || strings.Contains(out, "iscsiadm") {
t.Fatalf("NVMe-only start: %d %s %s; want no iscsiadm line", code, out, errs)
}
}
// Every command line a verb prints for the operator names the absolute
// /usr/local/bin/sw-block: EL9's sudo resets PATH to its secure_path, which
// has no /usr/local/bin.
func TestVolumeHintsNameTheAbsolutePath(t *testing.T) {
h, env := startedHost(t, true)
_, startOut, _ := runStart(env, h.spec(true), startOpts{Wait: time.Second})
_, stopOut, stopErr := runStop(env, stopOpts{Volume: "vol-a", Timeout: time.Second})
all := startOut + stopOut + stopErr + volumeHelp()
if strings.Contains(all, "sudo sw-block") {
t.Fatalf("a hint names a bare sudo sw-block:\n%s", all)
}
if !strings.Contains(startOut, "sudo /usr/local/bin/sw-block volume stop vol-a") || !strings.Contains(stopOut, "sudo /usr/local/bin/sw-block volume start") {
t.Fatalf("the start or stop hint lacks the absolute path:\nstart:\n%s\nstop:\n%s", startOut, stopOut)
}
g := newFakeHost(t)
gEnv := g.env(t)
if code, _, errs := runStart(gEnv, g.spec(false), startOpts{Wait: time.Second}); code != 0 {
t.Fatalf("setup: %s", errs)
}
g.guests.Store(1)
if _, _, errs := runStop(gEnv, stopOpts{Volume: "vol-a", Timeout: time.Second}); strings.Contains(errs, "sudo sw-block") {
t.Fatalf("a refusal names a bare sudo sw-block: %s", errs)
}
}
// The stop's closing line, printed after the unit stopped, speaks of the
// writes between the cut and the stop: with no guest counted, only one that
// connected after the check; with --force over a connected guest, that
// guest's.
func TestTheStopsClosingLineSpeaksOfTheStoppedUnit(t *testing.T) {
h, env := startedHost(t, true)
_, out, _ := runStop(env, stopOpts{Volume: "vol-a", Timeout: time.Second})
last := lastLine(out)
if last != "recovery point in the object store: the cut at 2026-11-28T23:00:00Z; the unit is stopped, and a write acknowledged after the cut (only from a guest that connected after the check) is on this host's disk only" {
t.Fatalf("closing line %q", last)
}
if h.called("stop sw-blockvolume@vol-a.service") != 1 {
t.Fatal("the unit was not stopped")
}
f, fenv := startedHost(t, true)
f.guests.Store(2)
_, out, _ = runStop(fenv, stopOpts{Volume: "vol-a", Force: true, Timeout: time.Second})
if last := lastLine(out); !strings.Contains(last, "writes a guest made after the cut (one was connected or could not be counted: --force) are on this host's disk only") {
t.Fatalf("--force closing line %q", last)
}
n, nenv := startedHost(t, true)
_, out, _ = runStop(nenv, stopOpts{Volume: "vol-a", NoCut: true, Timeout: time.Second})
if last := lastLine(out); !strings.HasPrefix(last, "no cut taken (--no-cut): ") || !strings.HasSuffix(last, "; the unit is stopped, and writes acknowledged after it are on this host's disk only") || n.cuts.Load() != 0 {
t.Fatalf("--no-cut closing line %q (cuts %d)", last, n.cuts.Load())
}
for _, o := range []string{out, volumeHelp()} {
if strings.Contains(o, "writes after it are on this host only") {
t.Fatalf("the old sentence is still printed:\n%s", o)
}
}
}
func lastLine(out string) string {
lines := strings.Split(strings.TrimSpace(out), "\n")
return lines[len(lines)-1]
}