Files
seaweedfs/seaweed-block/scripts/basic_app_tls_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

77 lines
2.8 KiB
Go

package scripts_test
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
// The first-volume helper's read-only status calls reach a blockmaster that
// accepts mutual TLS only. Without SW_BLOCK_TLS_* set it takes the viewer
// certificate the quick start's Step 2 issued for operator-status (under
// SW_BLOCK_TLS_DIR, by default ~/.sw-block-alpha-tls); a value already set is
// kept, and with no certificate nothing is set. The helper's own TLS_DIR line
// and function run in bash.
func TestBasicAppHelperUsesTheStep2ViewerCertificate(t *testing.T) {
bash, err := exec.LookPath("bash")
if err != nil {
t.Skip("bash not available")
}
script := filepath.ToSlash(filepath.Join(repoRoot(t), "scripts", "run-basic-app-example.sh"))
const probe = `eval "$(sed -n '/^TLS_DIR=/p;/^basic_app_tls_defaults()/,/^}/p' "$1")"; basic_app_tls_defaults; echo "cert=${SW_BLOCK_TLS_CERT:-} key=${SW_BLOCK_TLS_KEY:-} ca=${SW_BLOCK_TLS_CA:-}"`
run := func(home string, env ...string) string {
t.Helper()
cmd := exec.Command(bash, "-c", probe, "probe", script)
cmd.Env = append([]string{"HOME=" + home, "PATH=" + os.Getenv("PATH")}, env...)
out, err := cmd.CombinedOutput()
if err != nil {
t.Fatalf("probe: %v\n%s", err, out)
}
return strings.TrimSpace(string(out))
}
issued := func(dir string) {
t.Helper()
if err := os.MkdirAll(dir, 0o700); err != nil {
t.Fatal(err)
}
for _, f := range []string{"operator-status.crt", "operator-status.key", "ca.crt"} {
if err := os.WriteFile(filepath.Join(dir, f), []byte("x"), 0o600); err != nil {
t.Fatal(err)
}
}
}
// issuedIn checks the three paths by their last two elements (bash on
// Windows rewrites a drive path, D:/x to /d/x).
issuedIn := func(got, dir string) bool {
fields := strings.Fields(got)
if len(fields) != 3 {
return false
}
for i, f := range []string{"operator-status.crt", "operator-status.key", "ca.crt"} {
if !strings.HasSuffix(fields[i], "/"+filepath.Base(dir)+"/"+f) {
return false
}
}
return true
}
home := filepath.ToSlash(t.TempDir())
issued(filepath.Join(home, ".sw-block-alpha-tls"))
if got := run(home); !issuedIn(got, ".sw-block-alpha-tls") {
t.Errorf("Step 2 certificates present: %q, want the three files under ~/.sw-block-alpha-tls", got)
}
if got := run(home, "SW_BLOCK_TLS_CERT=/own/viewer.crt"); got != "cert=/own/viewer.crt key= ca=" {
t.Errorf("SW_BLOCK_TLS_CERT already set: %q, want it kept and nothing added", got)
}
if got := run(filepath.ToSlash(t.TempDir())); got != "cert= key= ca=" {
t.Errorf("no certificates: %q, want nothing set", got)
}
other := filepath.ToSlash(t.TempDir())
issued(other)
if got := run(filepath.ToSlash(t.TempDir()), "SW_BLOCK_TLS_DIR="+other); !issuedIn(got, other) {
t.Errorf("SW_BLOCK_TLS_DIR: %q, want the three files under %s", got, other)
}
}