Files
helmfile/pkg/state/diff_error_propagation_test.go
T
a9594eb158 Fix error propagation in helmfile diff when Kubernetes is unreachable (#2149)
* Fix error propagation in helmfile diff when Kubernetes is unreachable

Co-authored-by: yxxhero <11087727+yxxhero@users.noreply.github.com>

* Fix golangci-lint issue: replace custom contains function with strings.Contains

Co-authored-by: zhaque44 <20215376+zhaque44@users.noreply.github.com>

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: yxxhero <11087727+yxxhero@users.noreply.github.com>
Co-authored-by: zhaque44 <20215376+zhaque44@users.noreply.github.com>
2025-09-02 12:51:17 -04:00

65 lines
1.8 KiB
Go

package state
import (
"errors"
"strings"
"sync"
"testing"
"go.uber.org/zap"
"github.com/helmfile/helmfile/pkg/exectest"
"github.com/helmfile/helmfile/pkg/helmexec"
)
func TestIsReleaseInstalled_HandlesConnectionError(t *testing.T) {
logger := zap.NewNop().Sugar()
state := &HelmState{
logger: logger,
}
// Create a custom helm mock that fails on List operations
helm := &CustomFailingHelm{
Helm: &exectest.Helm{
DiffMutex: &sync.Mutex{},
ChartsMutex: &sync.Mutex{},
ReleasesMutex: &sync.Mutex{},
Helm3: true,
},
}
release := ReleaseSpec{
Name: "test-release",
Chart: "test/chart",
Namespace: "default",
}
// This should return an error due to connection failure
_, err := state.isReleaseInstalled(helmexec.HelmContext{}, helm, release)
// Verify that error was propagated
if err == nil {
t.Fatalf("expected isReleaseInstalled to return error when Kubernetes is unreachable, but got no error")
}
if err.Error() == "" {
t.Fatalf("expected isReleaseInstalled to return meaningful error when Kubernetes is unreachable, but got empty error")
}
// Check if the error contains the expected message
expectedMsg := "Kubernetes cluster unreachable"
if err.Error() != expectedMsg && !strings.Contains(err.Error(), "Kubernetes cluster unreachable") {
t.Fatalf("expected error to contain 'Kubernetes cluster unreachable', but got: %v", err.Error())
}
}
// CustomFailingHelm wraps exectest.Helm and overrides List to simulate failures
type CustomFailingHelm struct {
*exectest.Helm
}
func (h *CustomFailingHelm) List(context helmexec.HelmContext, filter string, flags ...string) (string, error) {
return "", errors.New("Kubernetes cluster unreachable: Get \"http://localhost:8080/version\": dial tcp [::1]:8080: connect: connection refused")
}