fix: update error handling in Configuration.Init method, add tests for the method

Signed-off-by: Suleiman Dibirov <idsulik@gmail.com>
pull/13130/head
Suleiman Dibirov 6 months ago
parent a2a324e511
commit 800c33a5aa

@ -330,7 +330,7 @@ func GetVersionSet(client discovery.ServerResourcesInterface) (chartutil.Version
} }
versionMap := make(map[string]interface{}) versionMap := make(map[string]interface{})
versions := []string{} var versions []string
// Extract the groups // Extract the groups
for _, g := range groups { for _, g := range groups {
@ -411,12 +411,11 @@ func (cfg *Configuration) Init(getter genericclioptions.RESTClientGetter, namesp
namespace, namespace,
) )
if err != nil { if err != nil {
panic(fmt.Sprintf("Unable to instantiate SQL driver: %v", err)) return errors.Wrap(err, "unable to instantiate SQL driver")
} }
store = storage.Init(d) store = storage.Init(d)
default: default:
// Not sure what to do here. return errors.Errorf("unknown driver %q", helmDriver)
panic("Unknown driver in HELM_DRIVER: " + helmDriver)
} }
cfg.RESTClientGetter = getter cfg.RESTClientGetter = getter

@ -17,9 +17,11 @@ package action
import ( import (
"flag" "flag"
"fmt"
"io" "io"
"testing" "testing"
"github.com/stretchr/testify/assert"
fakeclientset "k8s.io/client-go/kubernetes/fake" fakeclientset "k8s.io/client-go/kubernetes/fake"
"helm.sh/helm/v3/pkg/chart" "helm.sh/helm/v3/pkg/chart"
@ -273,6 +275,74 @@ func namedReleaseStub(name string, status release.Status) *release.Release {
} }
} }
func TestConfiguration_Init(t *testing.T) {
tests := []struct {
name string
helmDriver string
expectedDriverType interface{}
expectErr bool
errMsg string
}{
{
name: "Test secret driver",
helmDriver: "secret",
expectedDriverType: &driver.Secrets{},
},
{
name: "Test secrets driver",
helmDriver: "secrets",
expectedDriverType: &driver.Secrets{},
},
{
name: "Test empty driver",
helmDriver: "",
expectedDriverType: &driver.Secrets{},
},
{
name: "Test configmap driver",
helmDriver: "configmap",
expectedDriverType: &driver.ConfigMaps{},
},
{
name: "Test configmaps driver",
helmDriver: "configmaps",
expectedDriverType: &driver.ConfigMaps{},
},
{
name: "Test memory driver",
helmDriver: "memory",
expectedDriverType: &driver.Memory{},
},
{
name: "Test sql driver",
helmDriver: "sql",
expectErr: true,
errMsg: "unable to instantiate SQL driver",
},
{
name: "Test unknown driver",
helmDriver: "someDriver",
expectErr: true,
errMsg: fmt.Sprintf("unknown driver %q", "someDriver"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := &Configuration{}
actualErr := cfg.Init(nil, "default", tt.helmDriver, nil)
if tt.expectErr {
assert.Error(t, actualErr)
assert.Contains(t, actualErr.Error(), tt.errMsg)
} else {
assert.NoError(t, actualErr)
assert.IsType(t, tt.expectedDriverType, cfg.Releases.Driver)
}
})
}
}
func TestGetVersionSet(t *testing.T) { func TestGetVersionSet(t *testing.T) {
client := fakeclientset.NewSimpleClientset() client := fakeclientset.NewSimpleClientset()

Loading…
Cancel
Save