From 02abe93d2232e15d3fa92d9a95e51e1ab07cc013 Mon Sep 17 00:00:00 2001 From: quality <280983584+hivecommons-hive[bot]@users.noreply.github.com> Date: Mon, 2 Nov 2026 16:08:38 -0500 Subject: [PATCH 1/2] test(homelab): cover the join CLI's passphrase sources and manual control plane nodePassphrase's credential fallback and attempt's HOMELAB_CONTROL_PLANE path are not reached by the homelab-cluster e2e scenario, which uses homelab.conf and mDNS. Test them against a loopback join server. Signed-off-by: quality <280983584+hivecommons-hive[bot]@users.noreply.github.com> --- .../cluster/cmd/bluefin-cluster/main_test.go | 163 ++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 files/homelab/cluster/cmd/bluefin-cluster/main_test.go diff --git a/files/homelab/cluster/cmd/bluefin-cluster/main_test.go b/files/homelab/cluster/cmd/bluefin-cluster/main_test.go new file mode 100644 index 00000000..6d389823 --- /dev/null +++ b/files/homelab/cluster/cmd/bluefin-cluster/main_test.go @@ -0,0 +1,163 @@ +package main + +import ( + "context" + "errors" + "io" + "log/slog" + "net" + "os" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/projectbluefin/server/files/homelab/cluster/internal/conf" + "github.com/projectbluefin/server/files/homelab/cluster/internal/join" + "github.com/projectbluefin/server/files/homelab/cluster/internal/ops" +) + +const ( + goodPass = "correct-horse-battery-staple" + badPass = "correct-horse-battery-stapler" +) + +func writeCredential(t *testing.T, value string) string { + t.Helper() + dir := t.TempDir() + if err := os.WriteFile(filepath.Join(dir, credentialKey), []byte(value), 0o600); err != nil { + t.Fatal(err) + } + return dir +} + +func TestNodePassphraseFromConfIsNormalised(t *testing.T) { + t.Setenv("CREDENTIALS_DIRECTORY", writeCredential(t, "credential-should-be-ignored")) + got, err := nodePassphrase(conf.Config{passKey: " Correct Horse_battery STAPLE\n"}) + if err != nil { + t.Fatal(err) + } + if got != goodPass { + t.Errorf("got %q, want %q (homelab.conf wins over the credential)", got, goodPass) + } +} + +func TestNodePassphraseFallsBackToTheCredential(t *testing.T) { + t.Setenv("CREDENTIALS_DIRECTORY", writeCredential(t, "Correct-Horse-Battery-Staple\n")) + got, err := nodePassphrase(conf.Config{}) + if err != nil { + t.Fatal(err) + } + if got != goodPass { + t.Errorf("got %q, want %q", got, goodPass) + } +} + +func TestNodePassphraseMissing(t *testing.T) { + for name, dir := range map[string]string{ + "no credentials directory": "", + "credential file not present": t.TempDir(), + } { + t.Run(name, func(t *testing.T) { + t.Setenv("CREDENTIALS_DIRECTORY", dir) + _, err := nodePassphrase(conf.Config{"HOMELAB_ROLE": conf.RoleNode}) + if err == nil { + t.Fatal("want an error when no passphrase is configured") + } + for _, want := range []string{passKey, conf.DefaultPath, credentialKey} { + if !strings.Contains(err.Error(), want) { + t.Errorf("error %q does not name %s", err, want) + } + } + }) + } +} + +func TestNodePassphraseRejectsNonASCII(t *testing.T) { + t.Setenv("CREDENTIALS_DIRECTORY", "") + if _, err := nodePassphrase(conf.Config{passKey: "correct-horse-battery-stäple"}); err == nil { + t.Fatal("want an error for a non-ASCII passphrase") + } +} + +type fakeMinter struct{} + +func (fakeMinter) Mint(_ context.Context, _ string) (ops.Grant, error) { + return ops.Grant{ + Runtime: ops.RuntimeKubeadm, + Endpoint: "cp.local:6443", + Token: "abcdef.0123456789abcdef", + CACertHashes: []string{"sha256:" + strings.Repeat("ab", 32)}, + TTLSeconds: int(ops.TokenTTL / time.Second), + }, nil +} + +// startJoinServer serves joins on loopback and returns its host:port, the +// form HOMELAB_CONTROL_PLANE takes. +func startJoinServer(t *testing.T, pass string) string { + t.Helper() + dir := t.TempDir() + cert, err := join.LoadOrCreateCert(dir) + if err != nil { + t.Fatal(err) + } + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + t.Fatal(err) + } + srv := &join.Server{ + Passphrase: pass, + Cluster: "lab", + Cert: cert, + Runtime: func() string { return ops.RuntimeKubeadm }, + Minter: fakeMinter{}, + Limiter: join.NewLimiter(join.DefaultServerLimits, filepath.Join(dir, "failures.json"), nil), + Log: slog.New(slog.NewTextHandler(io.Discard, nil)), + } + ctx, cancel := context.WithCancel(context.Background()) + done := make(chan struct{}) + go func() { _ = srv.Serve(ctx, ln); close(done) }() + t.Cleanup(func() { cancel(); <-done }) + return ln.Addr().String() +} + +func joinClient(pass string) *join.Client { + return &join.Client{ + Passphrase: pass, + Node: "node-1", + Runtime: ops.RuntimeKubeadm, + Budget: join.NewClientBudget(100, time.Hour, nil), + Timeout: 10 * time.Second, + } +} + +func TestAttemptManualControlPlaneJoins(t *testing.T) { + addr := startJoinServer(t, goodPass) + p, err := attempt(context.Background(), joinClient(goodPass), addr) + if err != nil { + t.Fatal(err) + } + if p.Cluster != "lab" || p.Grant.Runtime != ops.RuntimeKubeadm || p.Grant.Endpoint != "cp.local:6443" { + t.Errorf("payload %+v", p) + } +} + +func TestAttemptManualControlPlaneWrongPassphrase(t *testing.T) { + addr := startJoinServer(t, goodPass) + _, err := attempt(context.Background(), joinClient(badPass), addr) + if !errors.Is(err, join.ErrNotProven) { + t.Fatalf("want ErrNotProven, got %v", err) + } + if !strings.Contains(err.Error(), "127.0.0.1") { + t.Errorf("error %q does not name the control plane it tried", err) + } +} + +func TestAttemptManualControlPlaneBadPort(t *testing.T) { + for _, manual := range []string{"127.0.0.1:0", "127.0.0.1:65536", "127.0.0.1:http"} { + _, err := attempt(context.Background(), joinClient(goodPass), manual) + if err == nil || !strings.Contains(err.Error(), "bad port") { + t.Errorf("%s: want a bad port error, got %v", manual, err) + } + } +} From 17123e4970c4c10fbb182f135646970c71ddb969 Mon Sep 17 00:00:00 2001 From: Jeffrey Sica Date: Mon, 2 Nov 2026 20:55:45 -0600 Subject: [PATCH 2/2] test(homelab): keep only the join tests #591 does not cover, in join_test.go #591 adds cmd/bluefin-cluster/main_test.go with its own nodePassphrase cases, so this file moves to join_test.go and drops the three cases that duplicate them (conf beats credential, credential fallback, non-ASCII). Kept: the missing-passphrase error naming its sources and the manual-control-plane joins against a loopback join.Server. Assisted-by: Claude Opus 5.5 via GitHub Copilot Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../{main_test.go => join_test.go} | 39 ------------------- 1 file changed, 39 deletions(-) rename files/homelab/cluster/cmd/bluefin-cluster/{main_test.go => join_test.go} (75%) diff --git a/files/homelab/cluster/cmd/bluefin-cluster/main_test.go b/files/homelab/cluster/cmd/bluefin-cluster/join_test.go similarity index 75% rename from files/homelab/cluster/cmd/bluefin-cluster/main_test.go rename to files/homelab/cluster/cmd/bluefin-cluster/join_test.go index 6d389823..1ed5c81e 100644 --- a/files/homelab/cluster/cmd/bluefin-cluster/main_test.go +++ b/files/homelab/cluster/cmd/bluefin-cluster/join_test.go @@ -6,7 +6,6 @@ import ( "io" "log/slog" "net" - "os" "path/filepath" "strings" "testing" @@ -22,37 +21,6 @@ const ( badPass = "correct-horse-battery-stapler" ) -func writeCredential(t *testing.T, value string) string { - t.Helper() - dir := t.TempDir() - if err := os.WriteFile(filepath.Join(dir, credentialKey), []byte(value), 0o600); err != nil { - t.Fatal(err) - } - return dir -} - -func TestNodePassphraseFromConfIsNormalised(t *testing.T) { - t.Setenv("CREDENTIALS_DIRECTORY", writeCredential(t, "credential-should-be-ignored")) - got, err := nodePassphrase(conf.Config{passKey: " Correct Horse_battery STAPLE\n"}) - if err != nil { - t.Fatal(err) - } - if got != goodPass { - t.Errorf("got %q, want %q (homelab.conf wins over the credential)", got, goodPass) - } -} - -func TestNodePassphraseFallsBackToTheCredential(t *testing.T) { - t.Setenv("CREDENTIALS_DIRECTORY", writeCredential(t, "Correct-Horse-Battery-Staple\n")) - got, err := nodePassphrase(conf.Config{}) - if err != nil { - t.Fatal(err) - } - if got != goodPass { - t.Errorf("got %q, want %q", got, goodPass) - } -} - func TestNodePassphraseMissing(t *testing.T) { for name, dir := range map[string]string{ "no credentials directory": "", @@ -73,13 +41,6 @@ func TestNodePassphraseMissing(t *testing.T) { } } -func TestNodePassphraseRejectsNonASCII(t *testing.T) { - t.Setenv("CREDENTIALS_DIRECTORY", "") - if _, err := nodePassphrase(conf.Config{passKey: "correct-horse-battery-stäple"}); err == nil { - t.Fatal("want an error for a non-ASCII passphrase") - } -} - type fakeMinter struct{} func (fakeMinter) Mint(_ context.Context, _ string) (ops.Grant, error) {