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"
)
// 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) {
cfg := actionConfigFixture(t)
client := NewGetMetadata(cfg)
@ -424,9 +415,9 @@ func TestGetMetadata_Run_DifferentStatuses(t *testing.T) {
func TestGetMetadata_Run_UnreachableKubeClient(t *testing.T) {
cfg := actionConfigFixture(t)
cfg.KubeClient = &unreachableKubeClient{
PrintingKubeClient: kubefake.PrintingKubeClient{Out: io.Discard},
}
failingKubeClient := kubefake.FailingKubeClient{PrintingKubeClient: kubefake.PrintingKubeClient{Out: io.Discard}, DummyResources: nil}
failingKubeClient.ConnectionError = errors.New("connection refused")
cfg.KubeClient = &failingKubeClient
client := NewGetMetadata(cfg)

@ -17,6 +17,7 @@ limitations under the License.
package action
import (
"errors"
"io"
"testing"
@ -168,9 +169,9 @@ func TestGetValues_Run_EmptyValues(t *testing.T) {
func TestGetValues_Run_UnreachableKubeClient(t *testing.T) {
cfg := actionConfigFixture(t)
cfg.KubeClient = &unreachableKubeClient{
PrintingKubeClient: kubefake.PrintingKubeClient{Out: io.Discard},
}
failingKubeClient := kubefake.FailingKubeClient{PrintingKubeClient: kubefake.PrintingKubeClient{Out: io.Discard}, DummyResources: nil}
failingKubeClient.ConnectionError = errors.New("connection refused")
cfg.KubeClient = &failingKubeClient
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
import (
"errors"
"io"
"testing"
"github.com/stretchr/testify/assert"
kubefake "helm.sh/helm/v4/pkg/kube/fake"
release "helm.sh/helm/v4/pkg/release/v1"
"helm.sh/helm/v4/pkg/storage"
)
@ -367,3 +370,16 @@ func TestSelectorList(t *testing.T) {
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
import (
"errors"
"fmt"
"io"
"testing"
"github.com/stretchr/testify/assert"
@ -151,3 +153,17 @@ func TestUninstallRelease_Cascade(t *testing.T) {
require.Error(t, err)
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 (
"context"
"errors"
"fmt"
"io"
"reflect"
"testing"
"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
BuildError error
BuildTableError error
ConnectionError error
BuildDummy bool
DummyResources kube.ResourceList
BuildUnstructuredError error
@ -166,6 +167,13 @@ func (f *FailingKubeClient) GetWaiter(ws kube.WaitStrategy) (kube.Waiter, error)
}, nil
}
func (f *FailingKubeClient) IsReachable() error {
if f.ConnectionError != nil {
return f.ConnectionError
}
return f.PrintingKubeClient.IsReachable()
}
func createDummyResourceList() kube.ResourceList {
var resInfo resource.Info
resInfo.Name = "dummyName"
Loading…
Cancel
Save