Files
seaweedfs/seaweed-block/scripts/storageclass_secret_awk_test.go
T
pingqiu-cpu c273e07beb fix(scripts): the StorageClass secret step runs under gawk 5 (#991)
Merge note (pull_request_target spec-trace workflow has a startup_failure on every branch since 2026-12-02 22:45Z, so its checks run locally):
- Test run 47138633695 succeeded (go test (linux), process-level tests (real binaries)) on head 2eb06afa against main 5fb70a89, the current main head.
- spec-trace-check.sh and public-text-check.sh, run with the main branch's scripts against this head and body: both exit 0.
- Code review ACCEPT (light tier): the awk variable named namespace, a gawk 5 builtin, is renamed in the three Kubernetes helper scripts; shell variables and rendered manifests unchanged.
2026-12-03 07:51:57 -08:00

128 lines
4.6 KiB
Go

package scripts_test
import (
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"testing"
)
// The scripts that inject the node-stage secret into a StorageClass with awk.
var storageClassSecretScripts = []string{"run-basic-app-example.sh", "activate-k8s-alpha.sh", "run-alpha-app-demo.sh"}
// gawkReserved are names gawk 5 refuses as a -v variable (its builtins and
// keywords); gawk 5.2 (Ubuntu 24.04's default awk) stops with "cannot use
// gawk builtin 'namespace' as variable name" before reading any input.
var gawkReserved = map[string]bool{
"namespace": true, "function": true, "func": true, "BEGIN": true, "END": true, "BEGINFILE": true, "ENDFILE": true,
"getline": true, "next": true, "nextfile": true, "exit": true, "delete": true, "print": true, "printf": true,
"length": true, "index": true, "split": true, "sub": true, "gsub": true, "gensub": true, "substr": true, "sprintf": true,
"system": true, "close": true, "fflush": true, "tolower": true, "toupper": true, "match": true, "patsplit": true,
"asort": true, "asorti": true, "isarray": true, "typeof": true, "systime": true, "strftime": true, "mktime": true,
"and": true, "or": true, "xor": true, "lshift": true, "rshift": true, "compl": true, "strtonum": true,
"int": true, "sqrt": true, "exp": true, "log": true, "sin": true, "cos": true, "atan2": true, "rand": true, "srand": true,
"bindtextdomain": true, "dcgettext": true, "dcngettext": true,
}
var awkCall = regexp.MustCompile(`(?m)^\s*awk((?:\s+-v\s+[A-Za-z_][A-Za-z0-9_]*="[^"]*")+)\s+'\n((?s:.*?))\n\s*' "\$input"`)
var awkVar = regexp.MustCompile(`-v\s+([A-Za-z_][A-Za-z0-9_]*)="(\$[a-z_]+)"`)
type secretAwk struct {
vars map[string]string // awk variable -> the shell variable it takes
program string
}
func readSecretAwk(t *testing.T, script string) secretAwk {
t.Helper()
raw, err := os.ReadFile(script)
if err != nil {
t.Fatal(err)
}
var m []string
for _, c := range awkCall.FindAllStringSubmatch(strings.ReplaceAll(string(raw), "\r\n", "\n"), -1) {
if strings.Contains(c[2], "node-stage-secret-namespace") {
if m != nil {
t.Fatalf("%s: more than one awk call renders the node-stage secret", script)
}
m = c
}
}
if m == nil {
t.Fatalf("%s: no awk call that renders the StorageClass's node-stage secret", script)
}
out := secretAwk{vars: map[string]string{}, program: m[2]}
for _, v := range awkVar.FindAllStringSubmatch(m[1], -1) {
out.vars[v[1]] = v[2]
}
return out
}
// No awk variable of these scripts is a gawk builtin, so the program runs
// under gawk as under mawk.
func TestStorageClassSecretAwkUsesNoGawkBuiltinAsAVariable(t *testing.T) {
for _, s := range storageClassSecretScripts {
a := readSecretAwk(t, s)
for name := range a.vars {
if gawkReserved[name] {
t.Errorf("%s: awk -v %s=...: gawk refuses its builtin %q as a variable name", s, name, name)
}
}
}
}
// Each script's awk program, run under every awk this host has (gawk, mawk,
// busybox), adds the node-stage secret's name and namespace under
// parameters: and leaves the rest of the manifest as it was.
func TestStorageClassSecretAwkRendersUnderEveryAwk(t *testing.T) {
var awks [][]string
seen := map[string]bool{}
for _, c := range [][]string{{"gawk"}, {"mawk"}, {"original-awk"}, {"busybox", "awk"}, {"awk"}} {
p, err := exec.LookPath(c[0])
if err != nil {
continue
}
key := p
if r, err := filepath.EvalSymlinks(p); err == nil && len(c) == 1 {
key = r
}
if seen[key] {
continue
}
seen[key] = true
awks = append(awks, append([]string{p}, c[1:]...))
}
if len(awks) == 0 {
t.Skip("no awk on this host")
}
input := filepath.Join(t.TempDir(), "sc.yaml")
manifest := "kind: StorageClass\nparameters:\n fsType: ext4\n"
if err := os.WriteFile(input, []byte(manifest), 0o600); err != nil {
t.Fatal(err)
}
values := map[string]string{"$secret": "swb-chap", "$namespace": "swb-system"}
want := "kind: StorageClass\nparameters:\n" +
" csi.storage.k8s.io/node-stage-secret-name: \"swb-chap\"\n" +
" csi.storage.k8s.io/node-stage-secret-namespace: \"swb-system\"\n" +
" fsType: ext4\n"
for _, s := range storageClassSecretScripts {
a := readSecretAwk(t, s)
for _, awk := range awks {
args := append([]string{}, awk[1:]...)
for name, shell := range a.vars {
v, ok := values[shell]
if !ok {
t.Fatalf("%s: awk -v %s takes %s, which this test does not set", s, name, shell)
}
args = append(args, "-v", name+"="+v)
}
args = append(args, a.program, input)
out, err := exec.Command(awk[0], args...).CombinedOutput()
if err != nil || strings.ReplaceAll(string(out), "\r\n", "\n") != want {
t.Errorf("%s under %v: %v\n%s\nwant:\n%s", s, awk, err, out, want)
}
}
}
}