Files
helmfile/cmd/root_test.go
T
pznamenskyandyxxhero c25d16a438 add trackLogsUntilReady flag (#2814)
* add trackLogsUntilReady flag

Signed-off-by: pznamensky <kompastver@gmail.com>

* refactor(kubedog): flatten nested conditionals in trackLogsUntilReady filter

- podLogFilter.update: replace nested if-found/if-uid block with a single
  combined guard plus a no-op delete
- podLogFilter.watch: extract cutoffTrackedKind predicate and
  watchNamespacePods helper; use RWTransactionErr (kubedog's own idiom)
  instead of capturing err in plain RWTransaction closures, removing the
  'if err == nil { inf.Run() }' nesting
- TrackResources: extract newStartupLogFilter guard-clause helper so the
  printer wiring stays flat
- add table tests for the new helpers

No behavior change; race tests and golangci-lint pass.

Signed-off-by: yxxhero <aiopsclub@163.com>

---------

Signed-off-by: pznamensky <kompastver@gmail.com>
Signed-off-by: yxxhero <aiopsclub@163.com>
Co-authored-by: yxxhero <aiopsclub@163.com>
2026-10-04 08:49:05 +08:00

104 lines
2.8 KiB
Go

package cmd
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/helmfile/helmfile/pkg/config"
"github.com/helmfile/helmfile/pkg/errors"
"github.com/helmfile/helmfile/pkg/helmexec"
)
func TestToCLIError(t *testing.T) {
g := config.NewGlobalImpl(&config.GlobalOptions{})
tests := []struct {
name string
err error
wantNil bool
wantExitCode int
wantMsgContains string
}{
{
name: "nil error returns nil",
err: nil,
wantNil: true,
},
{
name: "helmexec.ExitError returns correct exit code",
err: helmexec.ExitError{
Message: "helm command failed",
Code: 7,
},
wantExitCode: 7,
wantMsgContains: "helm command failed",
},
{
name: "wrapped helmexec.ExitError preserves exit code",
err: fmt.Errorf("helm version failed: %w", helmexec.ExitError{Message: "exit status 7", Code: 7}),
wantExitCode: 7,
wantMsgContains: "exit status 7",
},
{
name: "unknown error type returns exit code 1 without panic",
err: fmt.Errorf("some unexpected error"),
wantExitCode: 1,
wantMsgContains: "unexpected error",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Should never panic
var result error
assert.NotPanics(t, func() {
result = toCLIError(g, tt.err)
})
if tt.wantNil {
assert.NoError(t, result)
return
}
assert.Error(t, result)
exitErr, ok := result.(*errors.ExitError)
assert.True(t, ok, "expected *errors.ExitError, got %T", result)
assert.Equal(t, tt.wantExitCode, exitErr.ExitCode())
assert.Contains(t, exitErr.Error(), tt.wantMsgContains)
})
}
}
func TestRootCmdRegistersOtelTracingFlag(t *testing.T) {
rootCmd, err := NewRootCmd(&config.GlobalOptions{})
require.NoError(t, err)
flag := rootCmd.PersistentFlags().Lookup("otel-tracing")
require.NotNil(t, flag, "--otel-tracing flag should be registered")
assert.Equal(t, "bool", flag.Value.Type())
assert.Equal(t, "false", flag.DefValue)
assert.Contains(t, flag.Usage, "HELMFILE_OTEL_TRACING")
}
func TestTrackLogsUntilReadyFlag(t *testing.T) {
for _, command := range []string{"sync", "apply"} {
t.Run(command, func(t *testing.T) {
rootCmd, err := NewRootCmd(&config.GlobalOptions{})
require.NoError(t, err)
cmd, _, err := rootCmd.Find([]string{command})
require.NoError(t, err)
flag := cmd.Flags().Lookup("track-logs-until-ready")
require.NotNil(t, flag)
assert.Equal(t, "false", flag.DefValue)
require.NoError(t, cmd.ParseFlags([]string{"--track-logs", "--track-logs-until-ready"}))
enabled, err := cmd.Flags().GetBool("track-logs-until-ready")
require.NoError(t, err)
assert.True(t, enabled)
})
}
}