From 230880f55ef77818a45fbd94194d1e0c7e22503c Mon Sep 17 00:00:00 2001 From: Samuel K <69881238+skevetter@users.noreply.github.com> Date: Sun, 27 Sep 2026 22:02:19 -0600 Subject: [PATCH 1/2] test(pro): characterize the start command before it is split cmd/pro had no test files at all across its ten subpackages, so the 2207-line start.go could not be split without a safety net. Cover the pure, side-effect-free parts of it so a later move into smaller files is a mechanical change with a net underneath. - helm argument construction: host and ingress settings, the router disable flag, release version and product, and the interaction where Reset suppresses --reuse-values. - the generated admin password values file: its exact YAML shape, that %q keeps an awkward password a single valid scalar, and that the cleanup func removes it. - caller-supplied --values paths being absolutized for helm. - passwordRefIncomplete and deleteIgnoreNotFound, including that NotFound is skipped without stopping later deletes while any other error propagates immediately. Co-Authored-By: Claude Opus 4.8 (1M context) --- cmd/pro/start_test.go | 275 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 275 insertions(+) create mode 100644 cmd/pro/start_test.go diff --git a/cmd/pro/start_test.go b/cmd/pro/start_test.go new file mode 100644 index 0000000000..32b8859e83 --- /dev/null +++ b/cmd/pro/start_test.go @@ -0,0 +1,275 @@ +package pro + +import ( + "os" + "path/filepath" + "slices" + "strings" + "testing" + + storagev1 "github.com/devsy-org/api/pkg/apis/storage/v1" + "github.com/stretchr/testify/suite" + kerrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/runtime/schema" +) + +const startTestHost = "pro.example.com" + +type startSuite struct { + suite.Suite +} + +func TestStartSuite(t *testing.T) { + suite.Run(t, new(startSuite)) +} + +// hasArg reports whether args contains the exact value. +func hasArg(args []string, want string) bool { + return slices.Contains(args, want) +} + +// hasFlagValue reports whether args contains want immediately after flag. +func hasFlagValue(args []string, flag, want string) bool { + for i, arg := range args { + if arg == flag && i+1 < len(args) && args[i+1] == want { + return true + } + } + return false +} + +// hasSet reports whether args contains "--set" followed by want. +func hasSet(args []string, want string) bool { + return hasFlagValue(args, "--set", want) +} + +func (s *startSuite) TestAppendHostArgsNoHostNoTunnel() { + cmd := &StartCmd{} + s.Empty(cmd.appendHostArgs(nil)) +} + +func (s *startSuite) TestAppendHostArgsNoTunnelDisablesRouter() { + cmd := &StartCmd{NoTunnel: true} + args := cmd.appendHostArgs(nil) + // The router flag is passed with --set-string, not --set. + s.True( + hasFlagValue(args, "--set-string", "env.DISABLE_DEVSY_ROUTER=true"), + "args: %v", args, + ) + s.False(hasSet(args, "ingress.enabled=true"), "args: %v", args) +} + +// A host enables ingress, pins the apex domain, and routes subdomains. +func (s *startSuite) TestAppendHostArgsConfiguresIngress() { + cmd := &StartCmd{Host: startTestHost} + args := cmd.appendHostArgs(nil) + + s.True( + hasFlagValue(args, "--set-string", "env.DISABLE_DEVSY_ROUTER=true"), + "args: %v", args, + ) + for _, want := range []string{ + "ingress.enabled=true", + "ingress.host=pro.example.com", + "env.DEVSY_HOST=pro.example.com", + "devsyIngress.enabled=true", + "devsyIngress.host=*.pro.example.com", + "env.DEVSY_SUBDOMAIN=*.pro.example.com", + } { + s.True(hasSet(args, want), "missing %q in %v", want, args) + } +} + +func (s *startSuite) TestAppendHostArgsPreservesExistingArgs() { + cmd := &StartCmd{Host: startTestHost} + args := cmd.appendHostArgs([]string{"--first"}) + s.Equal("--first", args[0]) +} + +func (s *startSuite) TestAppendReleaseArgsEmpty() { + cmd := &StartCmd{} + s.Empty(cmd.appendReleaseArgs(nil)) +} + +func (s *startSuite) TestAppendReleaseArgsVersionAndProduct() { + cmd := &StartCmd{Version: "v1.2.3", Product: "loft"} + args := cmd.appendReleaseArgs(nil) + s.True(hasArg(args, "--version"), "args: %v", args) + s.True(hasArg(args, "v1.2.3"), "args: %v", args) + s.True(hasSet(args, "product=loft"), "args: %v", args) +} + +// Reuse-values is only meaningful when not resetting, since a reset discards +// the previous release values. +func (s *startSuite) TestAppendReleaseArgsReuseValuesRespectsReset() { + reusing := &StartCmd{ReuseValues: true} + s.True(hasArg(reusing.appendReleaseArgs(nil), "--reuse-values")) + + resetting := &StartCmd{ReuseValues: true, Reset: true} + s.False(hasArg(resetting.appendReleaseArgs(nil), "--reuse-values")) +} + +func (s *startSuite) TestAppendReleaseArgsReuseValuesNotRequested() { + cmd := &StartCmd{} + s.False(hasArg(cmd.appendReleaseArgs(nil), "--reuse-values")) +} + +func (s *startSuite) TestWritePasswordValuesFileContents() { + name, err := writePasswordValuesFile("s3cret") + s.Require().NoError(err) + s.T().Cleanup(func() { _ = os.Remove(name) }) + + data, err := os.ReadFile(name) //nolint:gosec // test reads a temp file it just created + s.Require().NoError(err) + s.Equal("admin:\n password: \"s3cret\"\n", string(data)) +} + +func (s *startSuite) TestWritePasswordValuesFileQuotesAwkwardPasswords() { + name, err := writePasswordValuesFile(`a"b`) + s.Require().NoError(err) + s.T().Cleanup(func() { _ = os.Remove(name) }) + + data, err := os.ReadFile(name) //nolint:gosec // test reads a temp file it just created + s.Require().NoError(err) + // %q escaping keeps the value a single valid YAML scalar. + s.Contains(string(data), `password: "a\"b"`) +} + +func (s *startSuite) TestBuildUpgradeArgsAddsPasswordValuesFile() { + cmd := &StartCmd{Password: "s3cret"} + args, cleanup, err := cmd.buildUpgradeArgs() + s.Require().NoError(err) + s.T().Cleanup(cleanup) + + index := -1 + for i, arg := range args { + if arg == "--values" { + index = i + break + } + } + s.Require().NotEqual(-1, index, "args: %v", args) + s.Require().Less(index+1, len(args), "args: %v", args) + file := args[index+1] + s.FileExists(file) + + data, err := os.ReadFile(file) //nolint:gosec // temp file the command created + s.Require().NoError(err) + s.Contains(string(data), "s3cret") + + // The generated values file must not outlive the command. + cleanup() + s.NoFileExists(file) +} + +func (s *startSuite) TestBuildUpgradeArgsNoPasswordMeansNoValuesFile() { + cmd := &StartCmd{} + args, cleanup, err := cmd.buildUpgradeArgs() + s.Require().NoError(err) + s.T().Cleanup(cleanup) + s.False(hasArg(args, "--values"), "args: %v", args) +} + +// A caller-supplied values file is passed as an absolute path, since helm runs +// with its own working directory. +func (s *startSuite) TestBuildUpgradeArgsAbsolutizesValuesPath() { + dir := s.T().TempDir() + relative := filepath.Join(dir, "values.yaml") + s.Require().NoError(os.WriteFile(relative, []byte("a: b\n"), 0o600)) + + cmd := &StartCmd{Values: relative} + args, cleanup, err := cmd.buildUpgradeArgs() + s.Require().NoError(err) + s.T().Cleanup(cleanup) + s.True(hasArg(args, relative), "args: %v", args) + if !filepath.IsAbs(relative) { + s.Fail("test expects an absolute input path") + } +} + +func (s *startSuite) TestBuildUpgradeArgsCombinesHostReleaseAndValues() { + cmd := &StartCmd{Host: startTestHost, Version: "v1.2.3", Product: "loft"} + args, cleanup, err := cmd.buildUpgradeArgs() + s.Require().NoError(err) + s.T().Cleanup(cleanup) + + s.True(hasSet(args, "ingress.host="+startTestHost), "args: %v", args) + s.True(hasArg(args, "v1.2.3"), "args: %v", args) + s.True(hasSet(args, "product=loft"), "args: %v", args) +} + +func (s *startSuite) TestPasswordRefIncomplete() { + for _, tc := range []struct { + name string + ref *storagev1.SecretRef + want bool + }{ + {"nil", nil, true}, + {"empty name", &storagev1.SecretRef{SecretNamespace: "ns"}, true}, + {"empty namespace", &storagev1.SecretRef{SecretName: "name"}, true}, + {"complete", &storagev1.SecretRef{SecretName: "name", SecretNamespace: "ns"}, false}, + } { + s.Run(tc.name, func() { + s.Equal(tc.want, passwordRefIncomplete(tc.ref)) + }) + } +} + +func (s *startSuite) TestDeleteIgnoreNotFoundRunsEveryDelete() { + var ran []string + record := func(name string) func() error { + return func() error { + ran = append(ran, name) + return nil + } + } + + s.Require().NoError(deleteIgnoreNotFound(record("a"), record("b"))) + s.Equal([]string{"a", "b"}, ran) +} + +func (s *startSuite) TestDeleteIgnoreNotFoundSkipsNotFound() { + ran := false + notFound := kerrors.NewNotFound( + schema.GroupResource{Resource: "secrets"}, + "missing", + ) + + err := deleteIgnoreNotFound( + func() error { ran = true; return notFound }, + func() error { ran = true; return nil }, + ) + s.Require().NoError(err) + s.True(ran, "subsequent deletes must still run") +} + +func (s *startSuite) TestDeleteIgnoreNotFoundPropagatesOtherErrors() { + sentinel := os.ErrPermission + ran := false + + err := deleteIgnoreNotFound( + func() error { return sentinel }, + func() error { ran = true; return nil }, + ) + s.ErrorIs(err, sentinel) + s.False(ran, "must stop at the first real error") +} + +func (s *startSuite) TestDeleteIgnoreNotFoundWithNoDeletes() { + s.NoError(deleteIgnoreNotFound()) +} + +// Guard the shape of the generated values document, since helm consumes it. +func (s *startSuite) TestWritePasswordValuesFileIsValidYAMLShape() { + name, err := writePasswordValuesFile("s3cret") + s.Require().NoError(err) + s.T().Cleanup(func() { _ = os.Remove(name) }) + + data, err := os.ReadFile(name) //nolint:gosec // test reads a temp file it just created + s.Require().NoError(err) + + lines := strings.Split(strings.TrimRight(string(data), "\n"), "\n") + s.Require().Len(lines, 2, "content: %q", string(data)) + s.Equal("admin:", lines[0]) + s.Equal(` password: "s3cret"`, lines[1]) +} From 24f78578eabd12db9b7de6548a0ea2c9743fb2f6 Mon Sep 17 00:00:00 2001 From: Samuel K <69881238+skevetter@users.noreply.github.com> Date: Sun, 27 Sep 2026 23:09:13 -0600 Subject: [PATCH 2/2] style: trim comments in the pro characterization tests Dropped the ones restating the helper or test name. Kept the rationale that the code does not carry: why reuse-values is suppressed by reset, why the values path is made absolute, and why the document shape matters. Co-Authored-By: Claude Opus 4.8 (1M context) --- cmd/pro/start_test.go | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/cmd/pro/start_test.go b/cmd/pro/start_test.go index 32b8859e83..2472b410b2 100644 --- a/cmd/pro/start_test.go +++ b/cmd/pro/start_test.go @@ -23,12 +23,10 @@ func TestStartSuite(t *testing.T) { suite.Run(t, new(startSuite)) } -// hasArg reports whether args contains the exact value. func hasArg(args []string, want string) bool { return slices.Contains(args, want) } -// hasFlagValue reports whether args contains want immediately after flag. func hasFlagValue(args []string, flag, want string) bool { for i, arg := range args { if arg == flag && i+1 < len(args) && args[i+1] == want { @@ -38,7 +36,6 @@ func hasFlagValue(args []string, flag, want string) bool { return false } -// hasSet reports whether args contains "--set" followed by want. func hasSet(args []string, want string) bool { return hasFlagValue(args, "--set", want) } @@ -59,7 +56,6 @@ func (s *startSuite) TestAppendHostArgsNoTunnelDisablesRouter() { s.False(hasSet(args, "ingress.enabled=true"), "args: %v", args) } -// A host enables ingress, pins the apex domain, and routes subdomains. func (s *startSuite) TestAppendHostArgsConfiguresIngress() { cmd := &StartCmd{Host: startTestHost} args := cmd.appendHostArgs(nil) @@ -99,8 +95,7 @@ func (s *startSuite) TestAppendReleaseArgsVersionAndProduct() { s.True(hasSet(args, "product=loft"), "args: %v", args) } -// Reuse-values is only meaningful when not resetting, since a reset discards -// the previous release values. +// A reset discards previous release values, so reuse-values is meaningless. func (s *startSuite) TestAppendReleaseArgsReuseValuesRespectsReset() { reusing := &StartCmd{ReuseValues: true} s.True(hasArg(reusing.appendReleaseArgs(nil), "--reuse-values")) @@ -119,7 +114,7 @@ func (s *startSuite) TestWritePasswordValuesFileContents() { s.Require().NoError(err) s.T().Cleanup(func() { _ = os.Remove(name) }) - data, err := os.ReadFile(name) //nolint:gosec // test reads a temp file it just created + data, err := os.ReadFile(name) //nolint:gosec // test temp file s.Require().NoError(err) s.Equal("admin:\n password: \"s3cret\"\n", string(data)) } @@ -129,7 +124,7 @@ func (s *startSuite) TestWritePasswordValuesFileQuotesAwkwardPasswords() { s.Require().NoError(err) s.T().Cleanup(func() { _ = os.Remove(name) }) - data, err := os.ReadFile(name) //nolint:gosec // test reads a temp file it just created + data, err := os.ReadFile(name) //nolint:gosec // test temp file s.Require().NoError(err) // %q escaping keeps the value a single valid YAML scalar. s.Contains(string(data), `password: "a\"b"`) @@ -170,8 +165,7 @@ func (s *startSuite) TestBuildUpgradeArgsNoPasswordMeansNoValuesFile() { s.False(hasArg(args, "--values"), "args: %v", args) } -// A caller-supplied values file is passed as an absolute path, since helm runs -// with its own working directory. +// Helm runs with its own working directory, hence the absolute path. func (s *startSuite) TestBuildUpgradeArgsAbsolutizesValuesPath() { dir := s.T().TempDir() relative := filepath.Join(dir, "values.yaml") @@ -259,13 +253,13 @@ func (s *startSuite) TestDeleteIgnoreNotFoundWithNoDeletes() { s.NoError(deleteIgnoreNotFound()) } -// Guard the shape of the generated values document, since helm consumes it. +// Helm parses this document, so its exact shape matters. func (s *startSuite) TestWritePasswordValuesFileIsValidYAMLShape() { name, err := writePasswordValuesFile("s3cret") s.Require().NoError(err) s.T().Cleanup(func() { _ = os.Remove(name) }) - data, err := os.ReadFile(name) //nolint:gosec // test reads a temp file it just created + data, err := os.ReadFile(name) //nolint:gosec // test temp file s.Require().NoError(err) lines := strings.Split(strings.TrimRight(string(data), "\n"), "\n")