fix(test): fix tests and check nil for restclient

Signed-off-by: Manuel Alonso <m.alonso@Manuels-MacBook-Pro.local>
pull/31578/head
Manuel Alonso 8 months ago
parent 561410ae1d
commit 0f949a92c1
No known key found for this signature in database

@ -222,32 +222,34 @@ func (i *Install) installCRDs(crds []chart.CRD) error {
return err return err
} }
// If we have already gathered the capabilities, we need to invalidate if i.cfg.RESTClientGetter != nil {
// the cache so that the new CRDs are recognized. This should only be // If we have already gathered the capabilities, we need to invalidate
// the case when an action configuration is reused for multiple actions, // the cache so that the new CRDs are recognized. This should only be
// as otherwise it is later loaded by ourselves when getCapabilities // the case when an action configuration is reused for multiple actions,
// is called later on in the installation process. // as otherwise it is later loaded by ourselves when getCapabilities
if i.cfg.Capabilities != nil { // is called later on in the installation process.
discoveryClient, err := i.cfg.RESTClientGetter.ToDiscoveryClient() if i.cfg.Capabilities != nil {
if err != nil { discoveryClient, err := i.cfg.RESTClientGetter.ToDiscoveryClient()
return err if err != nil {
} return err
}
i.cfg.Logger().Debug("clearing discovery cache") i.cfg.Logger().Debug("clearing discovery cache")
discoveryClient.Invalidate() discoveryClient.Invalidate()
_, _ = discoveryClient.ServerGroups() _, _ = discoveryClient.ServerGroups()
} }
// Invalidate the REST mapper, since it will not have the new CRDs // Invalidate the REST mapper, since it will not have the new CRDs
// present. // present.
restMapper, err := i.cfg.RESTClientGetter.ToRESTMapper() restMapper, err := i.cfg.RESTClientGetter.ToRESTMapper()
if err != nil { if err != nil {
return err return err
} }
if resettable, ok := restMapper.(meta.ResettableRESTMapper); ok { if resettable, ok := restMapper.(meta.ResettableRESTMapper); ok {
i.cfg.Logger().Debug("clearing REST mapper cache") i.cfg.Logger().Debug("clearing REST mapper cache")
resettable.Reset() resettable.Reset()
}
} }
} }
return nil return nil

@ -1084,8 +1084,8 @@ func TestInstallSetRegistryClient(t *testing.T) {
assert.Equal(t, registryClient, instAction.GetRegistryClient()) assert.Equal(t, registryClient, instAction.GetRegistryClient())
} }
func TestInstalLCRDs(t *testing.T) { func TestInstallCRDs(t *testing.T) {
config := actionConfigFixture(t) config := actionConfigFixtureWithDummyResources(t, createDummyResourceList(false))
instAction := NewInstall(config) instAction := NewInstall(config)
mockFile := common.File{ mockFile := common.File{
@ -1100,7 +1100,7 @@ func TestInstalLCRDs(t *testing.T) {
require.NoError(t, instAction.installCRDs(crdsToInstall)) require.NoError(t, instAction.installCRDs(crdsToInstall))
} }
func TestInstalLCRDs_KubeClient_BuildError(t *testing.T) { func TestInstallCRDs_KubeClient_BuildError(t *testing.T) {
config := actionConfigFixture(t) config := actionConfigFixture(t)
failingKubeClient := kubefake.FailingKubeClient{PrintingKubeClient: kubefake.PrintingKubeClient{Out: io.Discard}, DummyResources: nil} failingKubeClient := kubefake.FailingKubeClient{PrintingKubeClient: kubefake.PrintingKubeClient{Out: io.Discard}, DummyResources: nil}
failingKubeClient.BuildError = errors.New("build error") failingKubeClient.BuildError = errors.New("build error")
@ -1117,7 +1117,7 @@ func TestInstalLCRDs_KubeClient_BuildError(t *testing.T) {
require.Error(t, instAction.installCRDs(crdsToInstall), "failed to install CRD") require.Error(t, instAction.installCRDs(crdsToInstall), "failed to install CRD")
} }
func TestInstalLCRDs_KubeClient_CreateError(t *testing.T) { func TestInstallCRDs_KubeClient_CreateError(t *testing.T) {
config := actionConfigFixture(t) config := actionConfigFixture(t)
failingKubeClient := kubefake.FailingKubeClient{PrintingKubeClient: kubefake.PrintingKubeClient{Out: io.Discard}, DummyResources: nil} failingKubeClient := kubefake.FailingKubeClient{PrintingKubeClient: kubefake.PrintingKubeClient{Out: io.Discard}, DummyResources: nil}
failingKubeClient.CreateError = errors.New("create error") failingKubeClient.CreateError = errors.New("create error")
@ -1134,7 +1134,7 @@ func TestInstalLCRDs_KubeClient_CreateError(t *testing.T) {
require.Error(t, instAction.installCRDs(crdsToInstall), "failed to install CRD") require.Error(t, instAction.installCRDs(crdsToInstall), "failed to install CRD")
} }
func TestInstalLCRDs_AlreadyExist(t *testing.T) { func TestInstallCRDs_AlreadyExist(t *testing.T) {
config := actionConfigFixture(t) config := actionConfigFixture(t)
failingKubeClient := kubefake.FailingKubeClient{PrintingKubeClient: kubefake.PrintingKubeClient{Out: io.Discard}, DummyResources: nil} failingKubeClient := kubefake.FailingKubeClient{PrintingKubeClient: kubefake.PrintingKubeClient{Out: io.Discard}, DummyResources: nil}
mockError := &apierrors.StatusError{ErrStatus: metav1.Status{ mockError := &apierrors.StatusError{ErrStatus: metav1.Status{
@ -1149,13 +1149,14 @@ func TestInstalLCRDs_AlreadyExist(t *testing.T) {
Name: "crds/foo.yaml", Name: "crds/foo.yaml",
Data: []byte("hello"), Data: []byte("hello"),
} }
mockChart := buildChart(withFile(mockFile)) mockChart := buildChart(withFile(mockFile))
crdsToInstall := mockChart.CRDObjects() crdsToInstall := mockChart.CRDObjects()
assert.Nil(t, instAction.installCRDs(crdsToInstall)) require.Error(t, instAction.installCRDs(crdsToInstall), "failed to install CRD")
} }
func TestInstalLCRDs_WaiterError(t *testing.T) { func TestInstallCRDs_WaiterError(t *testing.T) {
config := actionConfigFixture(t) config := actionConfigFixture(t)
failingKubeClient := kubefake.FailingKubeClient{PrintingKubeClient: kubefake.PrintingKubeClient{Out: io.Discard}, DummyResources: nil} failingKubeClient := kubefake.FailingKubeClient{PrintingKubeClient: kubefake.PrintingKubeClient{Out: io.Discard}, DummyResources: nil}
failingKubeClient.WaitError = errors.New("wait error") failingKubeClient.WaitError = errors.New("wait error")

Loading…
Cancel
Save