Files
seaweedfs/seaweed-block/internal/testops/blockvolume_template_unit_test.go
T
pingqiuandClaude Opus 5.5 400cf3edf7 fix(blockvolume): the template passes --volume-id=%i; guests are an observation, not complete detection
Review follow-up. The template unit takes the volume id from its
instance name, so a settings file cannot name another volume (a fourth
per-instance directive, kept by the drift test). /status/guests and its
schema say what it is: the counted frontends (NVMe/TCP, iSCSI) only; an
absent frontend is not served or not counted; a vanished host stays
counted until TCP closes; 0 is not proof that nothing uses the volume.
The endpoint test's callbacks use atomics.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 19:25:31 -08:00

61 lines
2.2 KiB
Go

package testops
import (
"strings"
"testing"
)
// unitDirectives are a unit file's directive lines, in order, without
// comments and blank lines; a continued line stays one line per physical
// line.
func unitDirectives(body string) []string {
var out []string
for _, line := range strings.Split(strings.ReplaceAll(body, "\r\n", "\n"), "\n") {
line = strings.TrimSpace(line)
if line == "" || strings.HasPrefix(line, "#") {
continue
}
out = append(out, line)
}
return out
}
// templatePerInstance are the only directives sw-blockvolume@.service may
// set differently from sw-blockvolume.service: each instance's name, its
// settings file, its state directory, and its volume id (the instance
// name, so a settings file cannot name another volume).
var templatePerInstance = map[string]string{
"Description=Seaweed Block volume (one replica)": "Description=Seaweed Block volume %i (one replica)",
"EnvironmentFile=/etc/sw-block/blockvolume.env": "EnvironmentFile=/etc/sw-block/volumes/%i.env",
"StateDirectory=sw-block-volume": "StateDirectory=sw-block-volume/%i",
`--volume-id=${SWB_VOLUME_ID} \`: `--volume-id=%i \`,
}
// The per-volume template is the shipped single-volume unit, directive for
// directive, except the per-instance lines; a change to one unit that is
// not made to the other fails here.
func TestBlockvolumeTemplateUnitIsTheShippedUnit(t *testing.T) {
single := unitDirectives(readRepoFile(t, "deploy", "systemd", "sw-blockvolume.service"))
tmpl := unitDirectives(readRepoFile(t, "deploy", "systemd", "sw-blockvolume@.service"))
want := make([]string, len(single))
changed := 0
for i, l := range single {
want[i] = l
if r, ok := templatePerInstance[l]; ok {
want[i] = r
changed++
}
}
if changed != len(templatePerInstance) {
t.Fatalf("the single unit has %d of the %d per-instance directives; update templatePerInstance with it", changed, len(templatePerInstance))
}
if len(tmpl) != len(want) {
t.Fatalf("template has %d directives, the single unit %d", len(tmpl), len(want))
}
for i := range want {
if tmpl[i] != want[i] {
t.Fatalf("directive %d: template %q, want %q (the single unit's, per instance where marked)", i, tmpl[i], want[i])
}
}
}