From 59262baccec01564cd38aab023da649ae050c90f Mon Sep 17 00:00:00 2001 From: Parthvi Vala Date: Wed, 13 Jul 2022 18:05:49 +0530 Subject: [PATCH] Remove unused ComponentExist, PushedComponent function; Move ListFromCluster to List Signed-off-by: Parthvi Vala --- pkg/component/component.go | 10 - pkg/component/pushed_component.go | 64 +--- .../kubernetes/component/adapter_test.go | 134 -------- .../adapters/kubernetes/utils/utils_test.go | 89 +----- pkg/storage/kubernetes.go | 61 +--- pkg/storage/kubernetes_test.go | 290 +----------------- pkg/storage/mock_Client.go | 38 +-- pkg/storage/storage.go | 3 +- pkg/storage/storage_test.go | 3 +- 9 files changed, 29 insertions(+), 663 deletions(-) diff --git a/pkg/component/component.go b/pkg/component/component.go index abe144966d3..69d046e7d72 100644 --- a/pkg/component/component.go +++ b/pkg/component/component.go @@ -91,16 +91,6 @@ func GetOnePod(client kclient.ClientInterface, componentName string, appName str return client.GetOnePodFromSelector(odolabels.GetSelector(componentName, appName, odolabels.ComponentDevMode)) } -// ComponentExists checks whether a deployment by the given name exists in the given app -func ComponentExists(client kclient.ClientInterface, name string, app string) (bool, error) { - deployment, err := client.GetOneDeployment(name, app) - if _, ok := err.(*kclient.DeploymentNotFoundError); ok { - klog.V(2).Infof("Deployment %s not found for belonging to the %s app ", name, app) - return false, nil - } - return deployment != nil, err -} - // Log returns log from component func Log(client kclient.ClientInterface, componentName string, appName string, follow bool, command v1alpha2.Command) (io.ReadCloser, error) { diff --git a/pkg/component/pushed_component.go b/pkg/component/pushed_component.go index 5d9e2911efd..685f34979de 100644 --- a/pkg/component/pushed_component.go +++ b/pkg/component/pushed_component.go @@ -1,22 +1,20 @@ package component import ( - "errors" "fmt" - "github.com/redhat-developer/odo/pkg/kclient" + appsv1 "k8s.io/api/apps/v1" + "k8s.io/api/core/v1" + odolabels "github.com/redhat-developer/odo/pkg/labels" "github.com/redhat-developer/odo/pkg/storage" - v1 "k8s.io/api/apps/v1" - v12 "k8s.io/api/core/v1" - kerrors "k8s.io/apimachinery/pkg/api/errors" ) type provider interface { GetLabels() map[string]string GetAnnotations() map[string]string GetName() string - GetEnvVars() []v12.EnvVar + GetEnvVars() []v1.EnvVar GetLinkedSecrets() []SecretMount } @@ -40,7 +38,6 @@ type defaultPushedComponent struct { application string storage []storage.Storage provider provider - client kclient.ClientInterface storageClient storage.Client } @@ -63,7 +60,7 @@ func (d defaultPushedComponent) GetType() (string, error) { return getType(d.provider) } -func (d defaultPushedComponent) GetEnvVars() []v12.EnvVar { +func (d defaultPushedComponent) GetEnvVars() []v1.EnvVar { return d.provider.GetEnvVars() } @@ -75,7 +72,7 @@ func (d defaultPushedComponent) GetLinkedSecrets() []SecretMount { func (d defaultPushedComponent) GetStorage() ([]storage.Storage, error) { if d.storage == nil { if _, ok := d.provider.(*devfileComponent); ok { - storageList, err := d.storageClient.ListFromCluster() + storageList, err := d.storageClient.List() if err != nil { return nil, err } @@ -90,7 +87,7 @@ func (d defaultPushedComponent) GetApplication() string { } type devfileComponent struct { - d v1.Deployment + d appsv1.Deployment } var _ provider = (*devfileComponent)(nil) @@ -129,8 +126,8 @@ func (d devfileComponent) GetLinkedSecrets() (secretMounts []SecretMount) { return secretMounts } -func (d devfileComponent) GetEnvVars() []v12.EnvVar { - var envs []v12.EnvVar +func (d devfileComponent) GetEnvVars() []v1.EnvVar { + var envs []v1.EnvVar for _, container := range d.d.Spec.Template.Spec.Containers { envs = append(envs, container.Env...) } @@ -155,46 +152,3 @@ func getType(component provider) (string, error) { } return res, nil } - -func newPushedComponent(applicationName string, p provider, c kclient.ClientInterface, storageClient storage.Client) PushedComponent { - return &defaultPushedComponent{ - application: applicationName, - provider: p, - client: c, - storageClient: storageClient, - } -} - -// GetPushedComponent returns an abstraction over the cluster representation of the component -func GetPushedComponent(c kclient.ClientInterface, componentName, applicationName string) (PushedComponent, error) { - d, err := c.GetOneDeployment(componentName, applicationName) - if err != nil { - if isIgnorableError(err) { - return nil, nil - } - return nil, err - } - storageClient := storage.NewClient(storage.ClientOptions{ - Client: c, - Deployment: d, - }) - - return newPushedComponent(applicationName, &devfileComponent{d: *d}, c, storageClient), nil -} - -func isIgnorableError(err error) bool { - for { - e := errors.Unwrap(err) - if e != nil { - err = e - } else { - break - } - } - - if _, ok := err.(*kclient.DeploymentNotFoundError); ok { - return true - } - return kerrors.IsNotFound(err) || kerrors.IsForbidden(err) || kerrors.IsUnauthorized(err) - -} diff --git a/pkg/devfile/adapters/kubernetes/component/adapter_test.go b/pkg/devfile/adapters/kubernetes/component/adapter_test.go index 5b85b8a7f99..3991a62bd20 100644 --- a/pkg/devfile/adapters/kubernetes/component/adapter_test.go +++ b/pkg/devfile/adapters/kubernetes/component/adapter_test.go @@ -1,8 +1,6 @@ package component import ( - "encoding/json" - "errors" "reflect" "testing" "time" @@ -12,7 +10,6 @@ import ( "github.com/devfile/library/pkg/devfile/generator" - "github.com/redhat-developer/odo/pkg/component" "github.com/redhat-developer/odo/pkg/envinfo" "github.com/redhat-developer/odo/pkg/libdevfile" "github.com/redhat-developer/odo/pkg/preference" @@ -250,137 +247,6 @@ func TestGetFirstContainerWithSourceVolume(t *testing.T) { } } -func TestDoesComponentExist(t *testing.T) { - - tests := []struct { - name string - componentName string - appName string - getComponentName string - envInfo envinfo.EnvSpecificInfo - want bool - wantErr bool - }{ - { - name: "Case 1: Valid component name", - componentName: "test-name", - appName: "app", - getComponentName: "test-name", - envInfo: envinfo.EnvSpecificInfo{}, - want: true, - wantErr: false, - }, - { - name: "Case 2: Non-existent component name", - componentName: "test-name", - appName: "app", - getComponentName: "fake-component", - envInfo: envinfo.EnvSpecificInfo{}, - want: false, - wantErr: false, - }, - { - name: "Case 3: Error condition", - componentName: "test-name", - appName: "app", - getComponentName: "test-name", - envInfo: envinfo.EnvSpecificInfo{}, - want: false, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - devObj := devfileParser.DevfileObj{ - Data: func() data.DevfileData { - devfileData, err := data.NewDevfileData(string(data.APISchemaVersion200)) - if err != nil { - t.Error(err) - } - err = devfileData.AddComponents([]devfilev1.Component{testingutil.GetFakeContainerComponent("component")}) - if err != nil { - t.Error(err) - } - err = devfileData.AddCommands([]devfilev1.Command{getExecCommand("run", devfilev1.RunCommandGroupKind)}) - if err != nil { - t.Error(err) - } - return devfileData - }(), - } - - adapterCtx := AdapterContext{ - ComponentName: tt.componentName, - AppName: tt.appName, - Devfile: devObj, - } - - fkclient, fkclientset := kclient.FakeNew() - fkWatch := watch.NewFake() - - tt.envInfo.EnvInfo = *envinfo.GetFakeEnvInfo(envinfo.ComponentSettings{ - Name: tt.componentName, - AppName: tt.appName, - }) - - fkclientset.Kubernetes.PrependReactor("patch", "deployments", func(action ktesting.Action) (bool, runtime.Object, error) { - if patchAction, is := action.(ktesting.PatchAction); is { - patch := patchAction.GetPatch() - var deployment v1.Deployment - err := json.Unmarshal(patch, &deployment) - if err != nil { - t.Errorf("unable to parse deployment %q\n", err) - return false, nil, err - } - return true, &deployment, nil - } - return false, nil, nil - }) - - fkclientset.Kubernetes.PrependWatchReactor("pods", func(action ktesting.Action) (handled bool, ret watch.Interface, err error) { - return true, fkWatch, nil - }) - - tt.envInfo.EnvInfo = *envinfo.GetFakeEnvInfo(envinfo.ComponentSettings{ - Name: tt.componentName, - AppName: tt.appName, - }) - - // DoesComponentExist requires an already started component, so start it. - componentAdapter := NewKubernetesAdapter(fkclient, nil, nil, adapterCtx, "") - _, err := componentAdapter.createOrUpdateComponent(false, tt.envInfo, false, libdevfile.DevfileCommands{}, 0, nil) - - // Checks for unexpected error cases - if err != nil { - t.Errorf("component adapter start unexpected error %v", err) - } - - fkclientset.Kubernetes.PrependReactor("list", "deployments", func(action ktesting.Action) (bool, runtime.Object, error) { - emptyDeployment := odoTestingUtil.CreateFakeDeployment("") - deployment := odoTestingUtil.CreateFakeDeployment(tt.getComponentName) - - if tt.wantErr { - return true, &v1.DeploymentList{Items: []v1.Deployment{*emptyDeployment}}, errors.New("deployment get error") - } else if tt.getComponentName == tt.componentName { - return true, &v1.DeploymentList{Items: []v1.Deployment{*deployment}}, nil - } - - return true, &v1.DeploymentList{Items: []v1.Deployment{}}, nil - }) - - // Verify that a component with the specified name exists - componentExists, err := component.ComponentExists(fkclient, tt.getComponentName, tt.appName) - if !tt.wantErr && err != nil { - t.Errorf("unexpected error: %v", err) - } else if !tt.wantErr && componentExists != tt.want { - t.Errorf("expected %v, actual %v", tt.want, componentExists) - } - - }) - } - -} - func TestWaitAndGetComponentPod(t *testing.T) { testComponentName := "test" diff --git a/pkg/devfile/adapters/kubernetes/utils/utils_test.go b/pkg/devfile/adapters/kubernetes/utils/utils_test.go index fc3119db8ea..a8fcc9f0dac 100644 --- a/pkg/devfile/adapters/kubernetes/utils/utils_test.go +++ b/pkg/devfile/adapters/kubernetes/utils/utils_test.go @@ -1,105 +1,20 @@ package utils import ( - "errors" "reflect" "testing" devfilepkg "github.com/devfile/api/v2/pkg/devfile" "github.com/devfile/library/pkg/devfile/parser/data" - "github.com/redhat-developer/odo/pkg/component" "github.com/redhat-developer/odo/pkg/storage" "github.com/redhat-developer/odo/pkg/util" devfilev1 "github.com/devfile/api/v2/pkg/apis/workspaces/v1alpha2" devfileParser "github.com/devfile/library/pkg/devfile/parser" - appsv1 "k8s.io/api/apps/v1" - - "github.com/redhat-developer/odo/pkg/kclient" - odoTestingUtil "github.com/redhat-developer/odo/pkg/testingutil" - corev1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/runtime" - ktesting "k8s.io/client-go/testing" ) -func TestComponentExists(t *testing.T) { - - tests := []struct { - name string - componentType devfilev1.ComponentType - componentName string - appName string - getComponentName string - want bool - wantErr bool - }{ - { - name: "Case 1: Valid component name", - componentName: "test-name", - appName: "app", - getComponentName: "test-name", - want: true, - wantErr: false, - }, - { - name: "Case 2: Non-existent component name", - componentName: "test-name", - appName: "", - getComponentName: "fake-component", - want: false, - wantErr: false, - }, - { - name: "Case 3: Error condition", - componentName: "test-name", - appName: "app", - getComponentName: "test-name", - want: false, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - - fkclient, fkclientset := kclient.FakeNew() - fkclientset.Kubernetes.PrependReactor("list", "deployments", func(action ktesting.Action) (bool, runtime.Object, error) { - emptyDeployment := odoTestingUtil.CreateFakeDeployment("") - deployment := odoTestingUtil.CreateFakeDeployment(tt.getComponentName) - - if tt.wantErr { - return true, &appsv1.DeploymentList{ - Items: []appsv1.Deployment{ - *emptyDeployment, - }, - }, errors.New("deployment get error") - } else if tt.getComponentName == tt.componentName { - return true, &appsv1.DeploymentList{ - Items: []appsv1.Deployment{ - *deployment, - }, - }, nil - } - - return true, &appsv1.DeploymentList{ - Items: []appsv1.Deployment{}, - }, nil - }) - - // Verify that a component with the specified name exists - componentExists, err := component.ComponentExists(fkclient, tt.getComponentName, tt.appName) - if !tt.wantErr && err != nil { - t.Errorf("unexpected error: %v", err) - } else if !tt.wantErr && componentExists != tt.want { - t.Errorf("expected %v, actual %v", tt.want, componentExists) - } - - }) - } - -} - func TestAddOdoProjectVolume(t *testing.T) { tests := []struct { @@ -925,9 +840,9 @@ func TestUpdateContainersEntrypointsIfNeeded(t *testing.T) { runContainerArgs []string debugContainerArgs []string wantErr bool - //key is the container name + // key is the container name expectedContainerCommand map[string][]string - //key is the container name + // key is the container name expectedContainerArgs map[string][]string }{ { diff --git a/pkg/storage/kubernetes.go b/pkg/storage/kubernetes.go index a5144b11086..79b932768dd 100644 --- a/pkg/storage/kubernetes.go +++ b/pkg/storage/kubernetes.go @@ -2,7 +2,6 @@ package storage import ( "fmt" - "reflect" "strings" "github.com/devfile/library/pkg/devfile/generator" @@ -23,7 +22,7 @@ type kubernetesClient struct { client kclient.ClientInterface // if we don't have access to the local config - // we can use the deployment to call ListFromCluster() and + // we can use the deployment to call List() and // directly list storage from the cluster without the local config deployment *v1.Deployment } @@ -83,8 +82,8 @@ func (k kubernetesClient) Delete(name string) error { return nil } -// ListFromCluster lists pvc based Storage from the cluster -func (k kubernetesClient) ListFromCluster() (StorageList, error) { +// List lists pvc based Storage from the cluster +func (k kubernetesClient) List() (StorageList, error) { if k.deployment == nil { var err error k.deployment, err = k.client.GetOneDeployment(k.componentName, k.appName) @@ -179,57 +178,3 @@ func (k kubernetesClient) ListFromCluster() (StorageList, error) { return StorageList{Items: storage}, nil } - -// List lists pvc based Storage and local Storage with respective states -func (k kubernetesClient) List() (StorageList, error) { - if !k.localConfigProvider.Exists() { - return StorageList{}, fmt.Errorf("no local config was provided") - } - - localConfigStorage, err := k.localConfigProvider.ListStorage() - if err != nil { - return StorageList{}, err - } - - localStorage := ConvertListLocalToMachine(localConfigStorage) - var clusterStorage StorageList - if k.client != nil { - clusterStorage, err = k.ListFromCluster() - if err != nil { - return StorageList{}, err - } - } - - var storageList []Storage - - // find the local storage which are in a pushed and not pushed state - for _, localStore := range localStorage.Items { - found := false - for _, clusterStore := range clusterStorage.Items { - if reflect.DeepEqual(localStore, clusterStore) { - found = true - } - } - if found { - localStore.Status = StateTypePushed - } else { - localStore.Status = StateTypeNotPushed - } - storageList = append(storageList, localStore) - } - - // find the cluster storage which have been deleted locally - for _, clusterStore := range clusterStorage.Items { - found := false - for _, localStore := range localStorage.Items { - if reflect.DeepEqual(localStore, clusterStore) { - found = true - } - } - if !found { - clusterStore.Status = StateTypeLocallyDeleted - storageList = append(storageList, clusterStore) - } - } - return NewStorageList(storageList), nil -} diff --git a/pkg/storage/kubernetes_test.go b/pkg/storage/kubernetes_test.go index 2e1c1b0918c..cdac4d8b878 100644 --- a/pkg/storage/kubernetes_test.go +++ b/pkg/storage/kubernetes_test.go @@ -20,7 +20,7 @@ import ( "github.com/redhat-developer/odo/pkg/util" ) -func Test_kubernetesClient_ListFromCluster(t *testing.T) { +func Test_kubernetesClient_List(t *testing.T) { type fields struct { generic generic } @@ -335,294 +335,6 @@ func Test_kubernetesClient_ListFromCluster(t *testing.T) { tt.fields.generic.localConfigProvider = mockLocalConfig - k := kubernetesClient{ - generic: tt.fields.generic, - client: fakeClient, - } - got, err := k.ListFromCluster() - if (err != nil) != tt.wantErr { - t.Errorf("ListFromCluster() error = %v, wantErr %v", err, tt.wantErr) - return - } - if !reflect.DeepEqual(got, tt.want) { - t.Errorf("ListFromCluster() got = %v, want %v", got, tt.want) - } - }) - } -} - -func Test_kubernetesClient_List(t *testing.T) { - type fields struct { - generic generic - } - tests := []struct { - name string - fields fields - want StorageList - wantErr bool - returnedLocalStorage []localConfigProvider.LocalStorage - returnedDeployments *appsv1.DeploymentList - returnedPVCs *corev1.PersistentVolumeClaimList - }{ - { - name: "case 1: no volume on devfile and no pod on cluster", - fields: fields{ - generic: generic{ - componentName: "nodejs", - appName: "app", - }, - }, - returnedLocalStorage: []localConfigProvider.LocalStorage{}, - returnedDeployments: &appsv1.DeploymentList{ - Items: []appsv1.Deployment{}, - }, - returnedPVCs: &corev1.PersistentVolumeClaimList{ - Items: []corev1.PersistentVolumeClaim{}, - }, - want: NewStorageList(nil), - wantErr: false, - }, - { - name: "case 2: no volume on devfile and pod", - fields: fields{ - generic: generic{ - componentName: "nodejs", - appName: "app", - }, - }, - returnedLocalStorage: []localConfigProvider.LocalStorage{}, - returnedDeployments: &appsv1.DeploymentList{ - Items: []appsv1.Deployment{ - *testingutil.CreateFakeDeploymentsWithContainers("nodejs", []corev1.Container{testingutil.CreateFakeContainer("container-0")}, - []corev1.Container{}), - }, - }, - returnedPVCs: &corev1.PersistentVolumeClaimList{ - Items: []corev1.PersistentVolumeClaim{}, - }, - want: NewStorageList(nil), - wantErr: false, - }, - { - name: "case 3: same two volumes on cluster and devFile", - fields: fields{ - generic: generic{ - componentName: "nodejs", - appName: "app", - }, - }, - returnedLocalStorage: []localConfigProvider.LocalStorage{ - { - Name: "volume-0", - Size: "5Gi", - Path: "/data", - Container: "container-0", - }, - { - Name: "volume-1", - Size: "10Gi", - Path: "/path", - Container: "container-0", - }, - }, - returnedDeployments: &appsv1.DeploymentList{ - Items: []appsv1.Deployment{ - *testingutil.CreateFakeDeploymentsWithContainers("nodejs", []corev1.Container{ - testingutil.CreateFakeContainerWithVolumeMounts("container-0", []corev1.VolumeMount{ - {Name: "volume-0-vol", MountPath: "/data"}, - {Name: "volume-1-vol", MountPath: "/path"}, - }), - }, []corev1.Container{}), - }, - }, - returnedPVCs: &corev1.PersistentVolumeClaimList{ - Items: []corev1.PersistentVolumeClaim{ - *testingutil.FakePVC("volume-0", "5Gi", odolabels.Builder().WithComponent("nodejs").WithDevfileStorageName("volume-0").Labels()), - *testingutil.FakePVC("volume-1", "10Gi", odolabels.Builder().WithComponent("nodejs").WithDevfileStorageName("volume-1").Labels()), - }, - }, - want: NewStorageList([]Storage{ - generateStorage(NewStorage("volume-0", "5Gi", "/data", nil), StateTypePushed, "container-0"), - generateStorage(NewStorage("volume-1", "10Gi", "/path", nil), StateTypePushed, "container-0"), - }), - wantErr: false, - }, - { - name: "case 4: both volumes, present on the cluster and devFile, are different", - fields: fields{ - generic: generic{ - componentName: "nodejs", - appName: "app", - }, - }, - returnedLocalStorage: []localConfigProvider.LocalStorage{ - { - Name: "volume-0", - Size: "5Gi", - Path: "/data", - Container: "container-0", - }, - { - Name: "volume-1", - Size: "10Gi", - Path: "/path", - Container: "container-0", - }, - }, - returnedDeployments: &appsv1.DeploymentList{ - Items: []appsv1.Deployment{ - *testingutil.CreateFakeDeploymentsWithContainers("nodejs", []corev1.Container{ - testingutil.CreateFakeContainerWithVolumeMounts("container-0", []corev1.VolumeMount{ - {Name: "volume-00-vol", MountPath: "/data"}, - {Name: "volume-11-vol", MountPath: "/path"}, - }), - }, []corev1.Container{}), - }, - }, - returnedPVCs: &corev1.PersistentVolumeClaimList{ - Items: []corev1.PersistentVolumeClaim{ - *testingutil.FakePVC("volume-00", "5Gi", odolabels.Builder().WithComponent("nodejs").WithDevfileStorageName("volume-00").Labels()), - *testingutil.FakePVC("volume-11", "10Gi", odolabels.Builder().WithComponent("nodejs").WithDevfileStorageName("volume-11").Labels()), - }, - }, - want: NewStorageList([]Storage{ - generateStorage(NewStorage("volume-0", "5Gi", "/data", nil), StateTypeNotPushed, "container-0"), - generateStorage(NewStorage("volume-1", "10Gi", "/path", nil), StateTypeNotPushed, "container-0"), - generateStorage(NewStorage("volume-00", "5Gi", "/data", nil), StateTypeLocallyDeleted, "container-0"), - generateStorage(NewStorage("volume-11", "10Gi", "/path", nil), StateTypeLocallyDeleted, "container-0"), - }), - wantErr: false, - }, - { - name: "case 5: two containers with different volumes but one container is not pushed", - fields: fields{ - generic: generic{ - componentName: "nodejs", - appName: "app", - }, - }, - returnedLocalStorage: []localConfigProvider.LocalStorage{ - { - Name: "volume-0", - Size: "5Gi", - Path: "/data", - Container: "container-0", - }, - { - Name: "volume-1", - Size: "10Gi", - Path: "/data", - Container: "container-1", - }, - }, - returnedDeployments: &appsv1.DeploymentList{ - Items: []appsv1.Deployment{ - *testingutil.CreateFakeDeploymentsWithContainers("nodejs", []corev1.Container{ - testingutil.CreateFakeContainerWithVolumeMounts("container-0", []corev1.VolumeMount{ - {Name: "volume-0-vol", MountPath: "/data"}, - }), - }, []corev1.Container{}), - }, - }, - returnedPVCs: &corev1.PersistentVolumeClaimList{ - Items: []corev1.PersistentVolumeClaim{ - *testingutil.FakePVC("volume-0", "5Gi", odolabels.Builder().WithComponent("nodejs").WithDevfileStorageName("volume-0").Labels()), - }, - }, - want: NewStorageList([]Storage{ - generateStorage(NewStorage("volume-0", "5Gi", "/data", nil), StateTypePushed, "container-0"), - generateStorage(NewStorage("volume-1", "10Gi", "/data", nil), StateTypeNotPushed, "container-1"), - }), - wantErr: false, - }, - { - name: "case 6: two containers with different volumes on the cluster but one container is deleted locally", - fields: fields{ - generic: generic{ - componentName: "nodejs", - appName: "app", - }, - }, - returnedLocalStorage: []localConfigProvider.LocalStorage{ - { - Name: "volume-0", - Size: "5Gi", - Path: "/data", - Container: "container-0", - }, - }, - returnedDeployments: &appsv1.DeploymentList{ - Items: []appsv1.Deployment{ - *testingutil.CreateFakeDeploymentsWithContainers("nodejs", []corev1.Container{ - testingutil.CreateFakeContainerWithVolumeMounts("container-0", []corev1.VolumeMount{ - {Name: "volume-0-vol", MountPath: "/data"}, - }), - testingutil.CreateFakeContainerWithVolumeMounts("container-1", []corev1.VolumeMount{ - {Name: "volume-0-vol", MountPath: "/data"}}, - ), - }, []corev1.Container{}), - }, - }, - returnedPVCs: &corev1.PersistentVolumeClaimList{ - Items: []corev1.PersistentVolumeClaim{ - *testingutil.FakePVC("volume-0", "5Gi", odolabels.Builder().WithComponent("nodejs").WithDevfileStorageName("volume-0").Labels()), - }, - }, - want: NewStorageList([]Storage{ - generateStorage(NewStorage("volume-0", "5Gi", "/data", nil), StateTypePushed, "container-0"), - generateStorage(NewStorage("volume-0", "5Gi", "/data", nil), StateTypeLocallyDeleted, "container-1"), - }), - wantErr: false, - }, - { - name: "case 7: multiple pods are present on the cluster", - fields: fields{ - generic: generic{ - componentName: "nodejs", - appName: "app", - }, - }, - returnedLocalStorage: []localConfigProvider.LocalStorage{ - { - Name: "volume-0", - Size: "5Gi", - Path: "/data", - Container: "container-0", - }, - }, - returnedDeployments: &appsv1.DeploymentList{ - Items: []appsv1.Deployment{ - *testingutil.CreateFakeDeployment("nodejs"), - *testingutil.CreateFakeDeployment("nodejs"), - }, - }, - returnedPVCs: &corev1.PersistentVolumeClaimList{}, - want: StorageList{}, - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - fakeClient, fakeClientSet := kclient.FakeNew() - - fakeClientSet.Kubernetes.PrependReactor("list", "persistentvolumeclaims", func(action ktesting.Action) (bool, runtime.Object, error) { - return true, tt.returnedPVCs, nil - }) - - fakeClientSet.Kubernetes.PrependReactor("list", "deployments", func(action ktesting.Action) (bool, runtime.Object, error) { - return true, tt.returnedDeployments, nil - }) - - ctrl := gomock.NewController(t) - defer ctrl.Finish() - - mockLocalConfig := localConfigProvider.NewMockLocalConfigProvider(ctrl) - mockLocalConfig.EXPECT().GetName().Return(tt.fields.generic.componentName).AnyTimes() - mockLocalConfig.EXPECT().GetApplication().Return(tt.fields.generic.appName).AnyTimes() - mockLocalConfig.EXPECT().ListStorage().Return(tt.returnedLocalStorage, nil) - mockLocalConfig.EXPECT().Exists().Return(true) - tt.fields.generic.localConfigProvider = mockLocalConfig - k := kubernetesClient{ generic: tt.fields.generic, client: fakeClient, diff --git a/pkg/storage/mock_Client.go b/pkg/storage/mock_Client.go index 37932763da4..4c704f60d1c 100644 --- a/pkg/storage/mock_Client.go +++ b/pkg/storage/mock_Client.go @@ -5,35 +5,34 @@ package storage import ( - reflect "reflect" - gomock "github.com/golang/mock/gomock" + reflect "reflect" ) -// MockClient is a mock of Client interface. +// MockClient is a mock of Client interface type MockClient struct { ctrl *gomock.Controller recorder *MockClientMockRecorder } -// MockClientMockRecorder is the mock recorder for MockClient. +// MockClientMockRecorder is the mock recorder for MockClient type MockClientMockRecorder struct { mock *MockClient } -// NewMockClient creates a new mock instance. +// NewMockClient creates a new mock instance func NewMockClient(ctrl *gomock.Controller) *MockClient { mock := &MockClient{ctrl: ctrl} mock.recorder = &MockClientMockRecorder{mock} return mock } -// EXPECT returns an object that allows the caller to indicate expected use. +// EXPECT returns an object that allows the caller to indicate expected use func (m *MockClient) EXPECT() *MockClientMockRecorder { return m.recorder } -// Create mocks base method. +// Create mocks base method func (m *MockClient) Create(arg0 Storage) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Create", arg0) @@ -41,13 +40,13 @@ func (m *MockClient) Create(arg0 Storage) error { return ret0 } -// Create indicates an expected call of Create. +// Create indicates an expected call of Create func (mr *MockClientMockRecorder) Create(arg0 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Create", reflect.TypeOf((*MockClient)(nil).Create), arg0) } -// Delete mocks base method. +// Delete mocks base method func (m *MockClient) Delete(arg0 string) error { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "Delete", arg0) @@ -55,13 +54,13 @@ func (m *MockClient) Delete(arg0 string) error { return ret0 } -// Delete indicates an expected call of Delete. +// Delete indicates an expected call of Delete func (mr *MockClientMockRecorder) Delete(arg0 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Delete", reflect.TypeOf((*MockClient)(nil).Delete), arg0) } -// List mocks base method. +// List mocks base method func (m *MockClient) List() (StorageList, error) { m.ctrl.T.Helper() ret := m.ctrl.Call(m, "List") @@ -70,23 +69,8 @@ func (m *MockClient) List() (StorageList, error) { return ret0, ret1 } -// List indicates an expected call of List. +// List indicates an expected call of List func (mr *MockClientMockRecorder) List() *gomock.Call { mr.mock.ctrl.T.Helper() return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "List", reflect.TypeOf((*MockClient)(nil).List)) } - -// ListFromCluster mocks base method. -func (m *MockClient) ListFromCluster() (StorageList, error) { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "ListFromCluster") - ret0, _ := ret[0].(StorageList) - ret1, _ := ret[1].(error) - return ret0, ret1 -} - -// ListFromCluster indicates an expected call of ListFromCluster. -func (mr *MockClientMockRecorder) ListFromCluster() *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListFromCluster", reflect.TypeOf((*MockClient)(nil).ListFromCluster)) -} diff --git a/pkg/storage/storage.go b/pkg/storage/storage.go index 5c179d9e801..89cd9f3271f 100644 --- a/pkg/storage/storage.go +++ b/pkg/storage/storage.go @@ -41,7 +41,6 @@ type ClientOptions struct { type Client interface { Create(Storage) error Delete(string) error - ListFromCluster() (StorageList, error) List() (StorageList, error) } @@ -75,7 +74,7 @@ func Push(client Client, configProvider localConfigProvider.LocalConfigProvider) // list all the storage in the cluster storageClusterList := StorageList{} - storageClusterList, err := client.ListFromCluster() + storageClusterList, err := client.List() if err != nil { return nil, err } diff --git a/pkg/storage/storage_test.go b/pkg/storage/storage_test.go index 4a42ee7514c..5e9f8faf254 100644 --- a/pkg/storage/storage_test.go +++ b/pkg/storage/storage_test.go @@ -5,6 +5,7 @@ import ( "testing" "github.com/golang/mock/gomock" + odolabels "github.com/redhat-developer/odo/pkg/labels" "github.com/redhat-developer/odo/pkg/localConfigProvider" "github.com/redhat-developer/odo/pkg/util" @@ -244,7 +245,7 @@ func TestPush(t *testing.T) { fakeLocalConfig.EXPECT().GetName().Return(componentName).AnyTimes() - fakeStorageClient.EXPECT().ListFromCluster().Return(tt.returnedFromCluster, nil).AnyTimes() + fakeStorageClient.EXPECT().List().Return(tt.returnedFromCluster, nil).AnyTimes() fakeLocalConfig.EXPECT().ListStorage().Return(tt.returnedFromLocal, nil).AnyTimes() convert := ConvertListLocalToMachine(tt.createdItems)