Files
seaweedfs/seaweed-block/cmd/blockcsi/multi_volume_run_scope_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

56 lines
2.3 KiB
Go

package main
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
// The multi-volume example counts only this run's generated Deployments and
// pods: those whose PVC annotations name its namespace and PVC prefix. A
// generated pod is labelled app=<its Deployment>, not app=sw-blockvolume, so
// pods are selected by the volume label every generated object carries. A
// fake kubectl answers like the API server: a pod matches only the volume
// label selector.
func TestMultiVolumeExampleListsOnlyThisRunsLeftovers(t *testing.T) {
bash, err := exec.LookPath("bash")
if err != nil {
t.Skip("bash not available")
}
script := g15dReadFile(t, "scripts", "run-multi-volume-example.sh")
script = strings.ReplaceAll(script, "\r\n", "\n")
start := strings.Index(script, "list_run_blockvolume_objects() {")
end := strings.Index(script, "list_matching_pvs() {")
if start < 0 || end < start {
t.Fatal("run-scoped listing functions not found in run-multi-volume-example.sh")
}
funcs := script[start:end]
dir := t.TempDir()
fake := `#!/usr/bin/env bash
kind="$2"; selector=""
while [ $# -gt 0 ]; do [ "$1" = "-l" ] && selector="$2"; shift; done
if [ "$kind" = "pod" ] && [ "$selector" != "sw-block.seaweedfs.com/volume" ]; then exit 0; fi
if [ "$kind" = "deployment" ] && [ "$selector" != "app=sw-blockvolume" ]; then exit 0; fi
echo "sw-block-system/sw-blockvolume-mine-r1 default sw-block-multi-pvc-1"
echo "sw-block-system/sw-blockvolume-other-ns-r1 team-b sw-block-multi-pvc-1"
echo "sw-block-system/sw-blockvolume-other-pvc-r1 default someone-elses-pvc"
`
if err := os.WriteFile(filepath.Join(dir, "kubectl"), []byte(fake), 0o755); err != nil {
t.Fatal(err)
}
cmd := exec.Command(bash, "-c", "NAMESPACE=default\nPVC_PREFIX=sw-block-multi-pvc\n"+funcs+"\nlist_blockvolume_deployments\nlist_blockvolume_pods\n")
cmd.Env = append(os.Environ(), "PATH="+dir+string(os.PathListSeparator)+os.Getenv("PATH"))
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("listing: %v\n%s", err, out)
}
got := strings.Fields(strings.ReplaceAll(string(out), "\r", ""))
want := []string{"deployment/sw-block-system/sw-blockvolume-mine-r1", "pod/sw-block-system/sw-blockvolume-mine-r1"}
if strings.Join(got, " ") != strings.Join(want, " ") {
t.Fatalf("listed %v, want only this run's leftovers %v", got, want)
}
}