Refactor unreachableKubeClient for testing into failingKubeClient

Signed-off-by: Stephanie Hohenberg <stephanie.hohenberg@gmail.com>
pull/31258/head
Stephanie Hohenberg 2 weeks ago
parent fc22b6df31
commit e19d9fb6ee

@ -31,15 +31,6 @@ import (
helmtime "helm.sh/helm/v4/pkg/time" helmtime "helm.sh/helm/v4/pkg/time"
) )
// unreachableKubeClient is a test client that always returns an error for IsReachable
type unreachableKubeClient struct {
kubefake.PrintingKubeClient
}
func (u *unreachableKubeClient) IsReachable() error {
return errors.New("connection refused")
}
func TestNewGetMetadata(t *testing.T) { func TestNewGetMetadata(t *testing.T) {
cfg := actionConfigFixture(t) cfg := actionConfigFixture(t)
client := NewGetMetadata(cfg) client := NewGetMetadata(cfg)
@ -424,9 +415,9 @@ func TestGetMetadata_Run_DifferentStatuses(t *testing.T) {
func TestGetMetadata_Run_UnreachableKubeClient(t *testing.T) { func TestGetMetadata_Run_UnreachableKubeClient(t *testing.T) {
cfg := actionConfigFixture(t) cfg := actionConfigFixture(t)
cfg.KubeClient = &unreachableKubeClient{ failingKubeClient := kubefake.FailingKubeClient{PrintingKubeClient: kubefake.PrintingKubeClient{Out: io.Discard}, DummyResources: nil}
PrintingKubeClient: kubefake.PrintingKubeClient{Out: io.Discard}, failingKubeClient.ConnectionError = errors.New("connection refused")
} cfg.KubeClient = &failingKubeClient
client := NewGetMetadata(cfg) client := NewGetMetadata(cfg)

@ -17,6 +17,7 @@ limitations under the License.
package action package action
import ( import (
"errors"
"io" "io"
"testing" "testing"
@ -168,9 +169,9 @@ func TestGetValues_Run_EmptyValues(t *testing.T) {
func TestGetValues_Run_UnreachableKubeClient(t *testing.T) { func TestGetValues_Run_UnreachableKubeClient(t *testing.T) {
cfg := actionConfigFixture(t) cfg := actionConfigFixture(t)
cfg.KubeClient = &unreachableKubeClient{ failingKubeClient := kubefake.FailingKubeClient{PrintingKubeClient: kubefake.PrintingKubeClient{Out: io.Discard}, DummyResources: nil}
PrintingKubeClient: kubefake.PrintingKubeClient{Out: io.Discard}, failingKubeClient.ConnectionError = errors.New("connection refused")
} cfg.KubeClient = &failingKubeClient
client := NewGetValues(cfg) client := NewGetValues(cfg)

@ -997,3 +997,19 @@ func TestUrlEqual(t *testing.T) {
}) })
} }
} }
func TestInstallRun_UnreachableKubeClient(t *testing.T) {
config := actionConfigFixture(t)
failingKubeClient := kubefake.FailingKubeClient{PrintingKubeClient: kubefake.PrintingKubeClient{Out: io.Discard}, DummyResources: nil}
failingKubeClient.ConnectionError = errors.New("connection refused")
config.KubeClient = &failingKubeClient
instAction := NewInstall(config)
instAction.ClientOnly = false
ctx, done := context.WithCancel(t.Context())
res, err := instAction.RunWithContext(ctx, nil, nil)
done()
assert.Nil(t, res)
assert.ErrorContains(t, err, "connection refused")
}

@ -17,10 +17,13 @@ limitations under the License.
package action package action
import ( import (
"errors"
"io"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
kubefake "helm.sh/helm/v4/pkg/kube/fake"
release "helm.sh/helm/v4/pkg/release/v1" release "helm.sh/helm/v4/pkg/release/v1"
"helm.sh/helm/v4/pkg/storage" "helm.sh/helm/v4/pkg/storage"
) )
@ -367,3 +370,16 @@ func TestSelectorList(t *testing.T) {
assert.ElementsMatch(t, expectedFilteredList, res) assert.ElementsMatch(t, expectedFilteredList, res)
}) })
} }
func TestListRun_UnreachableKubeClient(t *testing.T) {
config := actionConfigFixture(t)
failingKubeClient := kubefake.FailingKubeClient{PrintingKubeClient: kubefake.PrintingKubeClient{Out: io.Discard}, DummyResources: nil}
failingKubeClient.ConnectionError = errors.New("connection refused")
config.KubeClient = &failingKubeClient
lister := NewList(config)
result, err := lister.Run()
assert.Nil(t, result)
assert.ErrorContains(t, err, "connection refused")
}

@ -17,7 +17,9 @@ limitations under the License.
package action package action
import ( import (
"errors"
"fmt" "fmt"
"io"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
@ -151,3 +153,17 @@ func TestUninstallRelease_Cascade(t *testing.T) {
require.Error(t, err) require.Error(t, err)
is.Contains(err.Error(), "failed to delete release: come-fail-away") is.Contains(err.Error(), "failed to delete release: come-fail-away")
} }
func TestUninstallRun_UnreachableKubeClient(t *testing.T) {
t.Helper()
config := actionConfigFixture(t)
failingKubeClient := kubefake.FailingKubeClient{PrintingKubeClient: kubefake.PrintingKubeClient{Out: io.Discard}, DummyResources: nil}
failingKubeClient.ConnectionError = errors.New("connection refused")
config.KubeClient = &failingKubeClient
client := NewUninstall(config)
result, err := client.Run("")
assert.Nil(t, result)
assert.ErrorContains(t, err, "connection refused")
}

@ -18,7 +18,9 @@ package action
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"io"
"reflect" "reflect"
"testing" "testing"
"time" "time"
@ -690,3 +692,18 @@ func TestGetUpgradeServerSideValue(t *testing.T) {
} }
} }
func TestUpgradeRun_UnreachableKubeClient(t *testing.T) {
t.Helper()
config := actionConfigFixture(t)
failingKubeClient := kubefake.FailingKubeClient{PrintingKubeClient: kubefake.PrintingKubeClient{Out: io.Discard}, DummyResources: nil}
failingKubeClient.ConnectionError = errors.New("connection refused")
config.KubeClient = &failingKubeClient
client := NewUpgrade(config)
vals := map[string]interface{}{}
result, err := client.Run("", buildChart(), vals)
assert.Nil(t, result)
assert.ErrorContains(t, err, "connection refused")
}

@ -40,6 +40,7 @@ type FailingKubeClient struct {
UpdateError error UpdateError error
BuildError error BuildError error
BuildTableError error BuildTableError error
ConnectionError error
BuildDummy bool BuildDummy bool
DummyResources kube.ResourceList DummyResources kube.ResourceList
BuildUnstructuredError error BuildUnstructuredError error
@ -166,6 +167,13 @@ func (f *FailingKubeClient) GetWaiter(ws kube.WaitStrategy) (kube.Waiter, error)
}, nil }, nil
} }
func (f *FailingKubeClient) IsReachable() error {
if f.ConnectionError != nil {
return f.ConnectionError
}
return f.PrintingKubeClient.IsReachable()
}
func createDummyResourceList() kube.ResourceList { func createDummyResourceList() kube.ResourceList {
var resInfo resource.Info var resInfo resource.Info
resInfo.Name = "dummyName" resInfo.Name = "dummyName"
Loading…
Cancel
Save