From 22a9c8fe837a3f13b355c40f831923492a732fc3 Mon Sep 17 00:00:00 2001 From: Bala FA Date: Sun, 18 Sep 2022 22:32:41 +0530 Subject: [PATCH] fix unit tests (#641) Signed-off-by: Bala.FA --- pkg/client/dynamic_test.go | 56 +++++----- pkg/controller/controller_test.go | 2 +- pkg/drive/list_test.go | 10 +- pkg/installer/install_test.go | 29 +++-- pkg/installer/installer_test.go | 4 +- pkg/k8s/k8s_test.go | 12 +- pkg/listener/listener.go | 2 +- pkg/listener/listener_test.go | 169 ++++++++++++++--------------- pkg/metrics/collector_test.go | 124 +++++++++++---------- pkg/node/fake.go | 5 +- pkg/node/publish_unpublish_test.go | 26 +++-- pkg/node/stage_unstage_test.go | 67 ++++++------ pkg/utils/utils_test.go | 46 +------- pkg/volume/event_test.go | 67 +++++++----- pkg/volume/list_test.go | 23 +++- 15 files changed, 315 insertions(+), 327 deletions(-) diff --git a/pkg/client/dynamic_test.go b/pkg/client/dynamic_test.go index a715f298f..3ae36d87b 100644 --- a/pkg/client/dynamic_test.go +++ b/pkg/client/dynamic_test.go @@ -128,7 +128,7 @@ func createTestDrive(node, drive, backendVersion string, labels map[string]strin func getFakeLatestDriveClient(t *testing.T, i int, drive runtime.Object, version string) *latestDriveClient { unstructuredObject, err := runtime.DefaultUnstructuredConverter.ToUnstructured(drive) if err != nil { - t.Fatalf("case %v: error: expected: no error, got: %v", i+1, err) + t.Fatalf("case %v: unexpected error: %v", i+1, err) } return newFakeLatestDriveClient(version, consts.DriveResource, &unstructured.Unstructured{Object: unstructuredObject}) } @@ -152,7 +152,7 @@ func TestGetDrive(t *testing.T) { client := getFakeLatestDriveClient(t, i, testCase.object, testCase.apiVersion) drive, err := client.Get(ctx, testCase.name, metav1.GetOptions{}) if err != nil { - t.Fatalf("case %v: error: expected: no error, got: %v", i+1, err) + t.Fatalf("case %v: unexpected error: %v", i+1, err) } if expectedGV != drive.GetObjectKind().GroupVersionKind().GroupVersion() { t.Fatalf("case %v: groupVersion: expected: %v, got: %v", i+1, expectedGV, drive.GetObjectKind().GroupVersionKind().GroupVersion()) @@ -182,7 +182,7 @@ func TestListDrive(t *testing.T) { for j := range testCase.objects { obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(testCase.objects[j]) if err != nil { - t.Fatalf("case %v: object %v: error: expected: no error, got: %v", i+1, j, err) + t.Fatalf("case %v: object %v: unexpected error: %v", i+1, j, err) } unstructuredObjects = append(unstructuredObjects, &unstructured.Unstructured{Object: obj}) } @@ -190,7 +190,7 @@ func TestListDrive(t *testing.T) { client := newFakeLatestDriveClientForList(testCase.apiVersion, consts.DriveResource, "DirectPVDriveList", unstructuredObjects...) driveList, err := client.List(ctx, metav1.ListOptions{}) if err != nil { - t.Fatalf("case %v: error: expected: no error, got: %v", i+1, err) + t.Fatalf("case %v: unexpected error: %v", i+1, err) } if expectedGV != driveList.GetObjectKind().GroupVersionKind().GroupVersion() { t.Fatalf("case %v: groupVersion: expected: %v, got: %v", i+1, expectedGV, driveList.GetObjectKind().GroupVersionKind().GroupVersion()) @@ -224,7 +224,7 @@ func TestListDriveWithOption(t *testing.T) { for j := range testCase.objects { obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(testCase.objects[j]) if err != nil { - t.Fatalf("case %v: object %v: error: expected: no error, got: %v", i+1, j, err) + t.Fatalf("case %v: object %v: unexpected error: %v", i+1, j, err) } unstructuredObjects = append(unstructuredObjects, &unstructured.Unstructured{Object: obj}) } @@ -238,7 +238,7 @@ func TestListDriveWithOption(t *testing.T) { } driveList, err := client.List(ctx, metav1.ListOptions{LabelSelector: types.ToLabelSelector(labelMap)}) if err != nil { - t.Fatalf("case %v: error: expected: no error, got: %v", i+1, err) + t.Fatalf("case %v: unexpected error: %v", i+1, err) } var names []string for _, item := range driveList.Items { @@ -274,7 +274,7 @@ func TestCreateDrive(t *testing.T) { client := getFakeLatestDriveClient(t, i, testCase.inputDrive, testCase.apiVersion) createdDrive, err := client.Create(ctx, testCase.newDrive, metav1.CreateOptions{}) if err != nil { - t.Fatalf("case %v: error: expected: no error, got: %v", i+1, err) + t.Fatalf("case %v: unexpected error: %v", i+1, err) } if expectedGV != createdDrive.GetObjectKind().GroupVersionKind().GroupVersion() { t.Fatalf("case %v: groupVersion: expected: %v, got: %v", i+1, expectedGV, createdDrive.GetObjectKind().GroupVersionKind().GroupVersion()) @@ -299,10 +299,10 @@ func TestDeleteDrive(t *testing.T) { for i, testCase := range testCases { client := getFakeLatestDriveClient(t, i, testCase.inputDrive, testCase.apiVersion) if err := client.Delete(ctx, testCase.name, metav1.DeleteOptions{}); err != nil { - t.Fatalf("case %v: error: expected: no error, got: %v", i+1, err) + t.Fatalf("case %v: unexpected error: %v", i+1, err) } if _, err := client.Get(ctx, testCase.name, metav1.GetOptions{}); err != nil && !apierrors.IsNotFound(err) { - t.Fatalf("case %v: error: expected: no error, got: %v", i+1, err) + t.Fatalf("case %v: unexpected error: %v", i+1, err) } } } @@ -324,10 +324,10 @@ func TestDeleteCollectionDrive(t *testing.T) { for i, testCase := range testCases { client := getFakeLatestDriveClient(t, i, testCase.inputDrive, testCase.apiVersion) if err := client.DeleteCollection(ctx, metav1.DeleteOptions{}, metav1.ListOptions{}); err != nil { - t.Fatalf("case %v: error: expected: no error, got: %v", i+1, err) + t.Fatalf("case %v: unexpected error: %v", i+1, err) } if _, err := client.Get(ctx, testCase.name, metav1.GetOptions{}); err != nil && !apierrors.IsNotFound(err) { - t.Fatalf("case %v: error: expected: no error, got: %v", i+1, err) + t.Fatalf("case %v: unexpected error: %v", i+1, err) } } } @@ -353,7 +353,7 @@ func TestUpdateDrive(t *testing.T) { testCase.inputDrive.Status.AccessTier = testCase.accessTier updatedDrive, err := client.Update(ctx, testCase.inputDrive, metav1.UpdateOptions{}) if err != nil { - t.Fatalf("case %v: error: expected: no error, got: %v", i+1, err) + t.Fatalf("case %v: unexpected error: %v", i+1, err) } if expectedGV != updatedDrive.GetObjectKind().GroupVersionKind().GroupVersion() { t.Fatalf("case %v: groupVersion: expected: %v, got: %v", i+1, expectedGV, updatedDrive.GetObjectKind().GroupVersionKind().GroupVersion()) @@ -385,7 +385,7 @@ func TestUpdateStatusDrive(t *testing.T) { testCase.inputDrive.Status.AccessTier = testCase.accessTier updatedDrive, err := client.UpdateStatus(ctx, testCase.inputDrive, metav1.UpdateOptions{}) if err != nil { - t.Fatalf("case %v: error: expected: no error, got: %v", i+1, err) + t.Fatalf("case %v: unexpected error: %v", i+1, err) } if expectedGV != updatedDrive.GetObjectKind().GroupVersionKind().GroupVersion() { t.Fatalf("case %v: groupVersion: expected: %v, got: %v", i+1, expectedGV, updatedDrive.GetObjectKind().GroupVersionKind().GroupVersion()) @@ -406,7 +406,7 @@ func TestWatcher(t *testing.T) { } var drive types.Drive if err := runtime.DefaultUnstructuredConverter.FromUnstructured(event.Object.(*unstructured.Unstructured).Object, &drive); err != nil { - return fmt.Errorf("error: expected: no error, got: %v", err) + t.Fatalf("unexpected error: %v", err) } if !reflect.DeepEqual(&drive, expectedObject) { return fmt.Errorf("eventType %v: expected: %v, got: %v", expectedEventType, expectedObject, drive) @@ -418,7 +418,7 @@ func TestWatcher(t *testing.T) { fakeDriveClient := getFakeLatestDriveClient(t, 0, inputDrive, consts.LatestAPIVersion) fakeWatchInterface, err := fakeDriveClient.Watch(ctx, metav1.ListOptions{}) if err != nil { - t.Fatalf("error: expected: no error, got: %v", err) + t.Fatalf("unexpected error: %v", err) } resultCh := watchInterfaceWrapper{fakeWatchInterface}.ResultChan() @@ -430,7 +430,7 @@ func TestWatcher(t *testing.T) { }, } if _, err = fakeDriveClient.Create(ctx, testCreateObject, metav1.CreateOptions{}); err != nil { - t.Fatalf("error: expected: no error, got: %v", err) + t.Fatalf("unexpected error: %v", err) } event, ok := <-resultCh if !ok { @@ -449,7 +449,7 @@ func TestWatcher(t *testing.T) { Status: types.DriveStatus{Status: directpvtypes.DriveStatusError}, } if _, err = fakeDriveClient.Update(ctx, testUpdateObject, metav1.UpdateOptions{}); err != nil { - t.Fatalf("error: expected: no error, got: %v", err) + t.Fatalf("unexpected error: %v", err) } event, ok = <-resultCh if !ok { @@ -462,7 +462,7 @@ func TestWatcher(t *testing.T) { // Test Delete Event if err = fakeDriveClient.Delete(ctx, "drive1", metav1.DeleteOptions{}); err != nil { - t.Fatalf("error: expected: no error, got: %v", err) + t.Fatalf("unexpected error: %v", err) } event, ok = <-resultCh if !ok { @@ -519,7 +519,7 @@ func TestGetVolume(t *testing.T) { volumeClient := getFakeLatestVolumeClient(testCase.volume, i, testCase.apiVersion, t) volume, err := volumeClient.Get(ctx, testCase.name, metav1.GetOptions{}) if err != nil { - t.Fatalf("case %v: error: expected: no error, got: %v", i+1, err) + t.Fatalf("case %v: unexpected error: %v", i+1, err) } if expectedGV != volume.GetObjectKind().GroupVersionKind().GroupVersion() { t.Fatalf("case %v: groupVersion: expected: %v, got: %v", i+1, expectedGV, volume.GetObjectKind().GroupVersionKind().GroupVersion()) @@ -546,7 +546,7 @@ func TestListVolume(t *testing.T) { for j, value := range testCase.volumes { obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(value) if err != nil { - t.Fatalf("case %v: volume %v: error: expected: no error, got: %v", i+1, j, err) + t.Fatalf("case %v: volume %v: unexpected error: %v", i+1, j, err) } unstructuredObjects = append(unstructuredObjects, &unstructured.Unstructured{Object: obj}) } @@ -554,7 +554,7 @@ func TestListVolume(t *testing.T) { client := newFakeLatestVolumeClientForList(testCase.apiVersion, consts.DriveResource, "DirectPVVolumeList", unstructuredObjects...) volumeList, err := client.List(ctx, metav1.ListOptions{}) if err != nil { - t.Fatalf("case %v: error: expected: no error, got: %v", i+1, err) + t.Fatalf("case %v: unexpected error: %v", i+1, err) } if expectedGV != volumeList.GetObjectKind().GroupVersionKind().GroupVersion() { t.Fatalf("case %v: groupVersion: expected: %v, got: %v", i+1, expectedGV, volumeList.GetObjectKind().GroupVersionKind().GroupVersion()) @@ -582,7 +582,7 @@ func TestCreateVolume(t *testing.T) { volumeClient := getFakeLatestVolumeClient(testCase.volume, i, testCase.apiVersion, t) createdVolume, err := volumeClient.Create(ctx, testCase.newVolume, metav1.CreateOptions{}) if err != nil { - t.Fatalf("case %v: error: expected: no error, got: %v", i+1, err) + t.Fatalf("case %v: unexpected error: %v", i+1, err) } if expectedGV != createdVolume.GetObjectKind().GroupVersionKind().GroupVersion() { t.Fatalf("case %v: groupVersion: expected: %v, got: %v", i+1, expectedGV, createdVolume.GetObjectKind().GroupVersionKind().GroupVersion()) @@ -607,10 +607,10 @@ func TestDeleteVolume(t *testing.T) { for i, testCase := range testCases { volumeClient := getFakeLatestVolumeClient(testCase.volume, i, testCase.apiVersion, t) if err := volumeClient.Delete(ctx, testCase.name, metav1.DeleteOptions{}); err != nil { - t.Fatalf("case %v: error: expected: no error, got: %v", i+1, err) + t.Fatalf("case %v: unexpected error: %v", i+1, err) } if _, err := volumeClient.Get(ctx, testCase.name, metav1.GetOptions{}); err != nil && !apierrors.IsNotFound(err) { - t.Fatalf("case %v: error: expected: no error, got: %v", i+1, err) + t.Fatalf("case %v: unexpected error: %v", i+1, err) } } } @@ -632,10 +632,10 @@ func TestVolumeDeleteCollection(t *testing.T) { for i, testCase := range testCases { volumeClient := getFakeLatestVolumeClient(testCase.volume, i, testCase.apiVersion, t) if err := volumeClient.DeleteCollection(ctx, metav1.DeleteOptions{}, metav1.ListOptions{}); err != nil { - t.Fatalf("case %v: error: expected: no error, got: %v", i+1, err) + t.Fatalf("case %v: unexpected error: %v", i+1, err) } if _, err := volumeClient.Get(ctx, testCase.name, metav1.GetOptions{}); err != nil && !apierrors.IsNotFound(err) { - t.Fatalf("case %v: error: expected: no error, got: %v", i+1, err) + t.Fatalf("case %v: unexpected error: %v", i+1, err) } } } @@ -661,7 +661,7 @@ func TestUpdateVolume(t *testing.T) { testCase.volume.Status.AvailableCapacity = testCase.availableCapacity updatedVolume, err := volumeClient.Update(ctx, testCase.volume, metav1.UpdateOptions{}) if err != nil { - t.Fatalf("case %v: error: expected: no error, got: %v", i+1, err) + t.Fatalf("case %v: unexpected error: %v", i+1, err) } if expectedGV != updatedVolume.GetObjectKind().GroupVersionKind().GroupVersion() { t.Fatalf("case %v: groupVersion: expected: %v, got: %v", i+1, expectedGV, updatedVolume.GetObjectKind().GroupVersionKind().GroupVersion()) @@ -693,7 +693,7 @@ func TestUpdateStatusVolume(t *testing.T) { testCase.volume.Status.AvailableCapacity = testCase.availableCapacity updatedVolume, err := volumeClient.UpdateStatus(ctx, testCase.volume, metav1.UpdateOptions{}) if err != nil { - t.Fatalf("case %v: error: expected: no error, got: %v", i+1, err) + t.Fatalf("case %v: unexpected error: %v", i+1, err) } if expectedGV != updatedVolume.GetObjectKind().GroupVersionKind().GroupVersion() { t.Fatalf("case %v: groupVersion: expected: %v, got: %v", i+1, expectedGV, updatedVolume.GetObjectKind().GroupVersionKind().GroupVersion()) diff --git a/pkg/controller/controller_test.go b/pkg/controller/controller_test.go index cbe79d347..afa0ff1e8 100644 --- a/pkg/controller/controller_test.go +++ b/pkg/controller/controller_test.go @@ -396,7 +396,7 @@ func TestValidateVolumeCapabilities(t *testing.T) { for i, testCase := range testCases { result, err := controller.ValidateVolumeCapabilities(context.TODO(), testCase.request) if err != nil { - t.Fatalf("case %v: unexpected error %v", i+1, err) + t.Fatalf("case %v: unexpected error: %v", i+1, err) } if !reflect.DeepEqual(result, testCase.expectedResult) { diff --git a/pkg/drive/list_test.go b/pkg/drive/list_test.go index 74ab81c4f..6a22282d5 100644 --- a/pkg/drive/list_test.go +++ b/pkg/drive/list_test.go @@ -21,14 +21,16 @@ import ( "fmt" "testing" - directcsi "github.com/minio/directpv/pkg/apis/direct.csi.min.io/v1beta4" + "github.com/minio/directpv/pkg/client" clientsetfake "github.com/minio/directpv/pkg/clientset/fake" + "github.com/minio/directpv/pkg/types" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" ) func TestGetDriveList(t *testing.T) { - SetLatestDirectCSIDriveInterface(clientsetfake.NewSimpleClientset().DirectV1beta4().DirectCSIDrives()) + clientset := types.NewExtFakeClientset(clientsetfake.NewSimpleClientset()) + client.SetDriveInterface(clientset.DirectpvLatest().DirectPVDrives()) drives, err := GetDriveList(context.TODO(), nil, nil, nil) if err != nil { t.Fatalf("unexpected error: %v", err) @@ -43,7 +45,9 @@ func TestGetDriveList(t *testing.T) { objects, &types.Drive{ObjectMeta: metav1.ObjectMeta{Name: fmt.Sprintf("drive-%v", i)}}, ) } - SetLatestDirectCSIDriveInterface(clientsetfake.NewSimpleClientset(objects...).DirectV1beta4().DirectCSIDrives()) + + clientset = types.NewExtFakeClientset(clientsetfake.NewSimpleClientset(objects...)) + client.SetDriveInterface(clientset.DirectpvLatest().DirectPVDrives()) drives, err = GetDriveList(context.TODO(), nil, nil, nil) if err != nil { t.Fatalf("unexpected error: %v", err) diff --git a/pkg/installer/install_test.go b/pkg/installer/install_test.go index d76812e90..e3f9d91f4 100644 --- a/pkg/installer/install_test.go +++ b/pkg/installer/install_test.go @@ -31,19 +31,18 @@ func init() { client.FakeInit() } -func TestInstaller(t1 *testing.T) { +func TestInstaller(t *testing.T) { installConfig := &Config{ - Identity: consts.Identity, - ContainerImage: "test-image", - ContainerOrg: "test-org", - ContainerRegistry: "test-registry", - AdmissionControl: false, - NodeSelector: nil, - Tolerations: nil, - SeccompProfile: "", - ApparmorProfile: "", - DynamicDriveDiscovery: true, - DryRun: false, + Identity: consts.Identity, + ContainerImage: "test-image", + ContainerOrg: "test-org", + ContainerRegistry: "test-registry", + AdmissionControl: false, + NodeSelector: nil, + Tolerations: nil, + SeccompProfile: "", + ApparmorProfile: "", + DryRun: false, } getDiscoveryGroupsAndMethodsFn := func() ([]*metav1.APIGroup, []*metav1.APIResourceList, error) { @@ -138,15 +137,15 @@ func TestInstaller(t1 *testing.T) { k8s.SetDiscoveryInterface(getDiscoveryGroupsAndMethodsFn, &testVersion) ctx := context.TODO() if err := Install(ctx, installConfig); err != nil { - t1.Fatalf("install failed: %v", err) + t.Fatalf("unexpected error: %v", err) } installConfig.ForceRemove = true installConfig.UninstallCRD = true if err := Uninstall(ctx, installConfig); err != nil { - t1.Fatalf("uninstall failed: %v", err) + t.Fatalf("unexpected error: %v", err) } if _, err := k8s.KubeClient().CoreV1().Namespaces().Get(ctx, consts.Identity, metav1.GetOptions{}); err == nil { - t1.Errorf("namespace not removed upon uninstallation. version: %s.%s", testVersion.Major, testVersion.Minor) + t.Fatalf("namespace not removed upon uninstallation. version: %s.%s", testVersion.Major, testVersion.Minor) } } } diff --git a/pkg/installer/installer_test.go b/pkg/installer/installer_test.go index 54c3ea2ba..352b8cd7e 100644 --- a/pkg/installer/installer_test.go +++ b/pkg/installer/installer_test.go @@ -39,7 +39,7 @@ func TestTrimMinorVersion(t *testing.T) { t.Parallel() result, err := trimMinorVersion(testCase.minor) if err != nil { - t.Fatalf("unexpected error; %v", err) + t.Fatalf("unexpected error: %v", err) } if result != testCase.expectedResult { t.Fatalf("expected: %v, got: %v", testCase.expectedResult, result) @@ -62,7 +62,7 @@ func TestTrimMinorVersionError(t *testing.T) { t.Parallel() _, err := trimMinorVersion(testCase.minor) if err == nil { - t.Fatalf("expected error; but succeeded for %v", testCase.minor) + t.Fatalf("expected error but succeeded for %v", testCase.minor) } }) } diff --git a/pkg/k8s/k8s_test.go b/pkg/k8s/k8s_test.go index 5f95df08f..d0ad8068c 100644 --- a/pkg/k8s/k8s_test.go +++ b/pkg/k8s/k8s_test.go @@ -23,7 +23,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) -func TestVolumeStatusTransitions(t1 *testing.T) { +func TestVolumeStatusTransitions(t *testing.T) { statusList := []metav1.Condition{ { Type: "staged", @@ -64,11 +64,11 @@ func TestVolumeStatusTransitions(t1 *testing.T) { }, } - for _, tt := range testCases { - t1.Run(tt.name, func(t1 *testing.T) { - UpdateCondition(statusList, tt.condType, tt.condStatus, "", "") - if !IsCondition(statusList, tt.condType, tt.condStatus, "", "") { - t1.Errorf("Test case name %s: Status transition failed (Type, Status) = (%s, %v) condition list: %v", tt.name, tt.condType, tt.condStatus, statusList) + for _, testCase := range testCases { + t.Run(testCase.name, func(t *testing.T) { + UpdateCondition(statusList, testCase.condType, testCase.condStatus, "", "") + if !IsCondition(statusList, testCase.condType, testCase.condStatus, "", "") { + t.Fatalf("case %v: Status transition failed (Type, Status) = (%s, %v) condition list: %v", testCase.name, testCase.condType, testCase.condStatus, statusList) } }) } diff --git a/pkg/listener/listener.go b/pkg/listener/listener.go index 0e1c8bd8e..8264e0d62 100644 --- a/pkg/listener/listener.go +++ b/pkg/listener/listener.go @@ -376,7 +376,7 @@ func (listener *Listener) Run(ctx context.Context) error { listener.runController(ctx) }, OnStoppedLeading: func() { - klog.Errorf("Stopped %v controller after stopped leader election") + klog.Errorf("Stopped controller by stopped leader election") }, OnNewLeader: func(identity string) { klog.V(3).Infof("New leader detected; current leader %s", identity) diff --git a/pkg/listener/listener_test.go b/pkg/listener/listener_test.go index 6798f339a..38dd3e510 100644 --- a/pkg/listener/listener_test.go +++ b/pkg/listener/listener_test.go @@ -23,16 +23,15 @@ import ( "reflect" "testing" - directcsi "github.com/minio/directpv/pkg/apis/direct.csi.min.io/v1beta4" - "github.com/minio/directpv/pkg/clientset" + directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types" + "github.com/minio/directpv/pkg/client" clientsetfake "github.com/minio/directpv/pkg/clientset/fake" - "github.com/minio/directpv/pkg/utils" + "github.com/minio/directpv/pkg/consts" + pkgtypes "github.com/minio/directpv/pkg/types" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/watch" - "k8s.io/client-go/kubernetes" - kubernetesfake "k8s.io/client-go/kubernetes/fake" "k8s.io/client-go/tools/cache" ) @@ -41,34 +40,32 @@ const ( GiB = 1024 * 1024 * 1024 ) +func init() { + client.FakeInit() +} + type testEventHandler struct { - t *testing.T - kubeClient kubernetes.Interface - directCSIClient clientset.Interface - handleFunc func(args EventArgs) error + t *testing.T + handleFunc func(args EventArgs) error } func (handler *testEventHandler) ListerWatcher() cache.ListerWatcher { return &cache.ListWatch{ ListFunc: func(options metav1.ListOptions) (runtime.Object, error) { - return handler.directCSIClient.DirectV1beta4().DirectCSIVolumes().List(context.TODO(), options) + return client.VolumeClient().List(context.TODO(), options) }, WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) { - return handler.directCSIClient.DirectV1beta4().DirectCSIVolumes().Watch(context.TODO(), options) + return client.VolumeClient().Watch(context.TODO(), options) }, } } -func (handler *testEventHandler) KubeClient() kubernetes.Interface { - return handler.kubeClient -} - func (handler *testEventHandler) Name() string { return "volume" } func (handler *testEventHandler) ObjectType() runtime.Object { - return &types.Volume{} + return &pkgtypes.Volume{} } func (handler *testEventHandler) Handle(ctx context.Context, args EventArgs) error { @@ -90,9 +87,9 @@ func startTestController(ctx context.Context, t *testing.T, handler *testEventHa } type condition struct { - ctype types.VolumeCondition + ctype directpvtypes.VolumeConditionType status metav1.ConditionStatus - reason types.VolumeReason + reason directpvtypes.VolumeConditionReason } func (c condition) toCondition() metav1.Condition { @@ -104,14 +101,14 @@ func (c condition) toCondition() metav1.Condition { } } -func newDirectCSIVolume(name, uid string, capacity int64, conditions []condition) *types.Volume { +func newVolume(name, uid string, capacity int64, conditions []condition) *pkgtypes.Volume { var metaConditions []metav1.Condition for _, c := range conditions { metaConditions = append(metaConditions, c.toCondition()) } - return &types.Volume{ - TypeMeta: types.NewVolumeTypeMeta(), + return &pkgtypes.Volume{ + TypeMeta: pkgtypes.NewVolumeTypeMeta(), ObjectMeta: metav1.ObjectMeta{ Name: name, Finalizers: []string{ @@ -119,17 +116,17 @@ func newDirectCSIVolume(name, uid string, capacity int64, conditions []condition }, UID: types.UID(uid), }, - Status: types.VolumeStatus{ + Status: pkgtypes.VolumeStatus{ NodeName: nodeID, HostPath: "hostpath", - Drive: "test-drive", + DriveName: "test-drive", TotalCapacity: capacity, Conditions: metaConditions, }, } } -func getHandleFunc(t *testing.T, event EventType, volumes ...*types.Volume) (<-chan struct{}, func(EventArgs) error) { +func getHandleFunc(t *testing.T, event EventType, volumes ...*pkgtypes.Volume) (<-chan struct{}, func(EventArgs) error) { doneCh := make(chan struct{}) i := 0 errOccured := false @@ -146,7 +143,7 @@ func getHandleFunc(t *testing.T, event EventType, volumes ...*types.Volume) (<-c t.Fatalf("expected: %v, got: %v", event, args.Event) } - volume := args.Object.(*types.Volume) + volume := args.Object.(*pkgtypes.Volume) if !reflect.DeepEqual(volumes[i], volume) { errOccured = true t.Fatalf("received volume is not equal to volumes[%v]", i) @@ -155,7 +152,7 @@ func getHandleFunc(t *testing.T, event EventType, volumes ...*types.Volume) (<-c } } -func toRuntimeObjects(volumes ...*types.Volume) (objects []runtime.Object) { +func toRuntimeObjects(volumes ...*pkgtypes.Volume) (objects []runtime.Object) { for _, volume := range volumes { objects = append(objects, volume) } @@ -167,24 +164,25 @@ func TestListener(t *testing.T) { defer cancelFunc() testHandler := &testEventHandler{ - kubeClient: kubernetesfake.NewSimpleClientset(), - t: t, + t: t, } // Sync - volumes := []*types.Volume{ - newDirectCSIVolume("test-volume-1", "1", 2*GiB, []condition{ - {types.VolumeConditionStaged, metav1.ConditionTrue, types.VolumeReasonInUse}, - {types.VolumeConditionPublished, metav1.ConditionFalse, types.VolumeReasonNotInUse}, - {types.VolumeConditionReady, metav1.ConditionTrue, types.VolumeReasonReady}, + volumes := []*pkgtypes.Volume{ + newVolume("test-volume-1", "1", 2*GiB, []condition{ + {directpvtypes.VolumeConditionTypeStaged, metav1.ConditionTrue, directpvtypes.VolumeConditionReasonInUse}, + {directpvtypes.VolumeConditionTypePublished, metav1.ConditionFalse, directpvtypes.VolumeConditionReasonNotInUse}, + {directpvtypes.VolumeConditionTypeReady, metav1.ConditionTrue, directpvtypes.VolumeConditionReasonReady}, }), - newDirectCSIVolume("test-volume-2", "2", 3*GiB, []condition{ - {types.VolumeConditionStaged, metav1.ConditionFalse, types.VolumeReasonInUse}, - {types.VolumeConditionPublished, metav1.ConditionFalse, types.VolumeReasonNotInUse}, - {types.VolumeConditionReady, metav1.ConditionTrue, types.VolumeReasonReady}, + newVolume("test-volume-2", "2", 3*GiB, []condition{ + {directpvtypes.VolumeConditionTypeStaged, metav1.ConditionFalse, directpvtypes.VolumeConditionReasonInUse}, + {directpvtypes.VolumeConditionTypePublished, metav1.ConditionFalse, directpvtypes.VolumeConditionReasonNotInUse}, + {directpvtypes.VolumeConditionTypeReady, metav1.ConditionTrue, directpvtypes.VolumeConditionReasonReady}, }), } - testHandler.directCSIClient = clientsetfake.NewSimpleClientset(toRuntimeObjects(volumes...)...) + + clientset := pkgtypes.NewExtFakeClientset(clientsetfake.NewSimpleClientset(toRuntimeObjects(volumes...)...)) + client.SetVolumeInterface(clientset.DirectpvLatest().DirectPVVolumes()) doneCh, handleFunc := getHandleFunc(t, AddEvent, volumes...) testHandler.handleFunc = handleFunc @@ -192,26 +190,26 @@ func TestListener(t *testing.T) { <-doneCh // Update - volumes = []*types.Volume{ - newDirectCSIVolume("test-volume-1", "1", 4*GiB, []condition{ - {types.VolumeConditionStaged, metav1.ConditionTrue, types.VolumeReasonInUse}, - {types.VolumeConditionPublished, metav1.ConditionFalse, types.VolumeReasonNotInUse}, - {types.VolumeConditionReady, metav1.ConditionTrue, types.VolumeReasonReady}, + volumes = []*pkgtypes.Volume{ + newVolume("test-volume-1", "1", 4*GiB, []condition{ + {directpvtypes.VolumeConditionTypeStaged, metav1.ConditionTrue, directpvtypes.VolumeConditionReasonInUse}, + {directpvtypes.VolumeConditionTypePublished, metav1.ConditionFalse, directpvtypes.VolumeConditionReasonNotInUse}, + {directpvtypes.VolumeConditionTypeReady, metav1.ConditionTrue, directpvtypes.VolumeConditionReasonReady}, }), - newDirectCSIVolume("test-volume-1", "1", 6*GiB, []condition{ - {types.VolumeConditionStaged, metav1.ConditionTrue, types.VolumeReasonInUse}, - {types.VolumeConditionPublished, metav1.ConditionFalse, types.VolumeReasonNotInUse}, - {types.VolumeConditionReady, metav1.ConditionTrue, types.VolumeReasonReady}, + newVolume("test-volume-1", "1", 6*GiB, []condition{ + {directpvtypes.VolumeConditionTypeStaged, metav1.ConditionTrue, directpvtypes.VolumeConditionReasonInUse}, + {directpvtypes.VolumeConditionTypePublished, metav1.ConditionFalse, directpvtypes.VolumeConditionReasonNotInUse}, + {directpvtypes.VolumeConditionTypeReady, metav1.ConditionTrue, directpvtypes.VolumeConditionReasonReady}, }), } doneCh, handleFunc = getHandleFunc(t, UpdateEvent, volumes[1]) testHandler.handleFunc = handleFunc for _, volume := range volumes { - _, err := testHandler.directCSIClient.DirectV1beta4().DirectCSIVolumes().Update( + _, err := client.VolumeClient().Update( ctx, volume, metav1.UpdateOptions{ - TypeMeta: types.NewVolumeTypeMeta(), + TypeMeta: pkgtypes.NewVolumeTypeMeta(), }, ) if err != nil { @@ -221,11 +219,11 @@ func TestListener(t *testing.T) { <-doneCh // Delete - volumes = []*types.Volume{ - newDirectCSIVolume("test-volume-1", "1", 4*GiB, []condition{ - {types.VolumeConditionStaged, metav1.ConditionTrue, types.VolumeReasonInUse}, - {types.VolumeConditionPublished, metav1.ConditionFalse, types.VolumeReasonNotInUse}, - {types.VolumeConditionReady, metav1.ConditionTrue, types.VolumeReasonReady}, + volumes = []*pkgtypes.Volume{ + newVolume("test-volume-1", "1", 4*GiB, []condition{ + {directpvtypes.VolumeConditionTypeStaged, metav1.ConditionTrue, directpvtypes.VolumeConditionReasonInUse}, + {directpvtypes.VolumeConditionTypePublished, metav1.ConditionFalse, directpvtypes.VolumeConditionReasonNotInUse}, + {directpvtypes.VolumeConditionTypeReady, metav1.ConditionTrue, directpvtypes.VolumeConditionReasonReady}, }), } now := metav1.Now() @@ -233,11 +231,11 @@ func TestListener(t *testing.T) { doneCh, handleFunc = getHandleFunc(t, DeleteEvent, volumes...) testHandler.handleFunc = handleFunc for _, volume := range volumes { - _, err := testHandler.directCSIClient.DirectV1beta4().DirectCSIVolumes().Update( + _, err := client.VolumeClient().Update( ctx, volume, metav1.UpdateOptions{ - TypeMeta: types.NewVolumeTypeMeta(), + TypeMeta: pkgtypes.NewVolumeTypeMeta(), }, ) if err != nil { @@ -247,11 +245,11 @@ func TestListener(t *testing.T) { <-doneCh // Retry on error - volumes = []*types.Volume{ - newDirectCSIVolume("test-volume-1", "1", 512*GiB, []condition{ - {types.VolumeConditionStaged, metav1.ConditionTrue, types.VolumeReasonInUse}, - {types.VolumeConditionPublished, metav1.ConditionFalse, types.VolumeReasonNotInUse}, - {types.VolumeConditionReady, metav1.ConditionTrue, types.VolumeReasonReady}, + volumes = []*pkgtypes.Volume{ + newVolume("test-volume-1", "1", 512*GiB, []condition{ + {directpvtypes.VolumeConditionTypeStaged, metav1.ConditionTrue, directpvtypes.VolumeConditionReasonInUse}, + {directpvtypes.VolumeConditionTypePublished, metav1.ConditionFalse, directpvtypes.VolumeConditionReasonNotInUse}, + {directpvtypes.VolumeConditionTypeReady, metav1.ConditionTrue, directpvtypes.VolumeConditionReasonReady}, }), } @@ -268,7 +266,7 @@ func TestListener(t *testing.T) { t.Fatalf("expected: %v, got: %v", AddEvent, args.Event) } - volume := args.Object.(*types.Volume) + volume := args.Object.(*pkgtypes.Volume) if !reflect.DeepEqual(volumes[0], volume) { t.Fatalf("received volume is not equal to volumes[0]") } @@ -276,11 +274,11 @@ func TestListener(t *testing.T) { } for _, volume := range volumes { - _, err := testHandler.directCSIClient.DirectV1beta4().DirectCSIVolumes().Update( + _, err := client.VolumeClient().Update( ctx, volume, metav1.UpdateOptions{ - TypeMeta: types.NewVolumeTypeMeta(), + TypeMeta: pkgtypes.NewVolumeTypeMeta(), }, ) if err != nil { @@ -295,21 +293,22 @@ func TestListenerParallel(t *testing.T) { defer cancelFunc() testHandler := &testEventHandler{ - kubeClient: kubernetesfake.NewSimpleClientset(), - t: t, + t: t, } - testHandler.directCSIClient = clientsetfake.NewSimpleClientset( - toRuntimeObjects(newDirectCSIVolume("test-volume-1", "1", 2*GiB, []condition{ - {types.VolumeConditionStaged, metav1.ConditionTrue, types.VolumeReasonInUse}, - {types.VolumeConditionPublished, metav1.ConditionFalse, types.VolumeReasonNotInUse}, - {types.VolumeConditionReady, metav1.ConditionTrue, types.VolumeReasonReady}, + + clientset := pkgtypes.NewExtFakeClientset(clientsetfake.NewSimpleClientset( + toRuntimeObjects(newVolume("test-volume-1", "1", 2*GiB, []condition{ + {directpvtypes.VolumeConditionTypeStaged, metav1.ConditionTrue, directpvtypes.VolumeConditionReasonInUse}, + {directpvtypes.VolumeConditionTypePublished, metav1.ConditionFalse, directpvtypes.VolumeConditionReasonNotInUse}, + {directpvtypes.VolumeConditionTypeReady, metav1.ConditionTrue, directpvtypes.VolumeConditionReasonReady}, }))..., - ) + )) + client.SetVolumeInterface(clientset.DirectpvLatest().DirectPVVolumes()) doneCh := make(chan struct{}) stopCh := make(chan struct{}) testHandler.handleFunc = func(args EventArgs) (err error) { - volume := args.Object.(*types.Volume) + volume := args.Object.(*pkgtypes.Volume) if volume.Status.TotalCapacity == 512*GiB { close(stopCh) } @@ -330,15 +329,15 @@ func TestListenerParallel(t *testing.T) { } startTestController(ctx, t, testHandler, 40) - _, err := testHandler.directCSIClient.DirectV1beta4().DirectCSIVolumes().Update( + _, err := client.VolumeClient().Update( ctx, - newDirectCSIVolume("test-volume-1", "1", 512*GiB, []condition{ - {types.VolumeConditionStaged, metav1.ConditionTrue, types.VolumeReasonInUse}, - {types.VolumeConditionPublished, metav1.ConditionFalse, types.VolumeReasonNotInUse}, - {types.VolumeConditionReady, metav1.ConditionTrue, types.VolumeReasonReady}, + newVolume("test-volume-1", "1", 512*GiB, []condition{ + {directpvtypes.VolumeConditionTypeStaged, metav1.ConditionTrue, directpvtypes.VolumeConditionReasonInUse}, + {directpvtypes.VolumeConditionTypePublished, metav1.ConditionFalse, directpvtypes.VolumeConditionReasonNotInUse}, + {directpvtypes.VolumeConditionTypeReady, metav1.ConditionTrue, directpvtypes.VolumeConditionReasonReady}, }), metav1.UpdateOptions{ - TypeMeta: types.NewVolumeTypeMeta(), + TypeMeta: pkgtypes.NewVolumeTypeMeta(), }, ) if err != nil { @@ -346,15 +345,15 @@ func TestListenerParallel(t *testing.T) { } <-stopCh - _, err = testHandler.directCSIClient.DirectV1beta4().DirectCSIVolumes().Update( + _, err = client.VolumeClient().Update( ctx, - newDirectCSIVolume("test-volume-1", "1", 1*GiB, []condition{ - {types.VolumeConditionStaged, metav1.ConditionTrue, types.VolumeReasonInUse}, - {types.VolumeConditionPublished, metav1.ConditionFalse, types.VolumeReasonNotInUse}, - {types.VolumeConditionReady, metav1.ConditionTrue, types.VolumeReasonReady}, + newVolume("test-volume-1", "1", 1*GiB, []condition{ + {directpvtypes.VolumeConditionTypeStaged, metav1.ConditionTrue, directpvtypes.VolumeConditionReasonInUse}, + {directpvtypes.VolumeConditionTypePublished, metav1.ConditionFalse, directpvtypes.VolumeConditionReasonNotInUse}, + {directpvtypes.VolumeConditionTypeReady, metav1.ConditionTrue, directpvtypes.VolumeConditionReasonReady}, }), metav1.UpdateOptions{ - TypeMeta: types.NewVolumeTypeMeta(), + TypeMeta: pkgtypes.NewVolumeTypeMeta(), }, ) if err != nil { diff --git a/pkg/metrics/collector_test.go b/pkg/metrics/collector_test.go index c605f36c0..1c2f18246 100644 --- a/pkg/metrics/collector_test.go +++ b/pkg/metrics/collector_test.go @@ -23,30 +23,19 @@ import ( "testing" "time" - directcsi "github.com/minio/directpv/pkg/apis/direct.csi.min.io/v1beta4" "github.com/minio/directpv/pkg/client" - fakedirect "github.com/minio/directpv/pkg/clientset/fake" + clientsetfake "github.com/minio/directpv/pkg/clientset/fake" "github.com/minio/directpv/pkg/consts" - "github.com/minio/directpv/pkg/utils" + "github.com/minio/directpv/pkg/types" + "github.com/minio/directpv/pkg/xfs" "github.com/prometheus/client_golang/prometheus" - dto "github.com/prometheus/client_model/go" + clientmodelgo "github.com/prometheus/client_model/go" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - runtime "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime" "k8s.io/klog/v2" ) -const ( - testNodeName = "test-node-1" - testDriveName = "test-drive-1" - testTenantName = "tenant-1" - - KB = 1 << 10 - MB = KB << 10 - - mb20 = 20 * MB - mb30 = 30 * MB - mb10 = 10 * MB -) +const MiB = 1024 * 1024 type metricType string @@ -55,14 +44,58 @@ const ( metricStatsBytesTotal metricType = consts.AppName + "_stats_bytes_total" ) +var volumes = []types.Volume{ + { + TypeMeta: types.NewVolumeTypeMeta(), + ObjectMeta: metav1.ObjectMeta{ + Name: "test_volume_20MB", + Labels: map[string]string{tenantLabel: "tenant-1"}, + }, + Status: types.VolumeStatus{ + NodeName: "test-node-1", + DriveName: "test-drive-1", + TotalCapacity: 20 * MiB, + ContainerPath: "/path/containerpath", + UsedCapacity: 10 * MiB, + }, + }, + { + TypeMeta: types.NewVolumeTypeMeta(), + ObjectMeta: metav1.ObjectMeta{ + Name: "test_volume_30MB", + Labels: map[string]string{tenantLabel: "tenant-1"}, + }, + Status: types.VolumeStatus{ + NodeName: "test-node-1", + DriveName: "test-drive-1", + TotalCapacity: 30 * MiB, + ContainerPath: "/path/containerpath", + UsedCapacity: 20 * MiB, + }, + }, +} + func createFakeMetricsCollector() *metricsCollector { return &metricsCollector{ - desc: prometheus.NewDesc("directcsi_stats", "Statistics exposed by DirectCSI", nil, nil), - nodeID: testNodeName, + desc: prometheus.NewDesc(consts.AppName+"_stats", "Statistics exposed by "+consts.AppPrettyName, nil, nil), + nodeID: "test-node-1", + getDeviceByFSUUID: func(fsuuid string) (string, error) { return "", nil }, + getQuota: func(ctx context.Context, device, volumeID string) (quota *xfs.Quota, err error) { + for _, volume := range volumes { + if volume.Name == volumeID { + return &xfs.Quota{ + HardLimit: uint64(volume.Status.TotalCapacity), + SoftLimit: uint64(volume.Status.TotalCapacity), + CurrentSpace: uint64(volume.Status.UsedCapacity), + }, nil + } + } + return &xfs.Quota{}, nil + }, } } -func getVolumeNameFromLabelPair(labelPair []*dto.LabelPair) string { +func getVolumeNameFromLabelPair(labelPair []*clientmodelgo.LabelPair) string { for _, lp := range labelPair { if lp.GetName() == "volumeID" { return lp.GetValue() @@ -81,45 +114,16 @@ func getFQNameFromDesc(desc string) string { } func TestVolumeStatsEmitter(t *testing.T) { - testVolumeName20MB := "test_volume_20MB" - testVolumeName30MB := "test_volume_30MB" - - createTestVolume := func(volName string, totalCap, usedCap int64) *types.Volume { - return &types.Volume{ - TypeMeta: types.NewVolumeTypeMeta(), - ObjectMeta: metav1.ObjectMeta{ - Name: volName, - Labels: map[string]string{ - tenantLabel: testTenantName, - }, - }, - Status: types.VolumeStatus{ - NodeName: testNodeName, - Drive: testDriveName, - TotalCapacity: totalCap, - ContainerPath: "/path/containerpath", - UsedCapacity: usedCap, - }, - } - } - - testStatsGetter := func(_ context.Context, vol *types.Volume) (xfsVolumeStats, error) { - return xfsVolumeStats{ - TotalBytes: uint64(vol.Status.TotalCapacity), - UsedBytes: uint64(vol.Status.UsedCapacity), - AvailableBytes: uint64(vol.Status.TotalCapacity - vol.Status.UsedCapacity), - }, nil - } - - testObjects := []runtime.Object{ - createTestVolume(testVolumeName20MB, mb20, mb10), - createTestVolume(testVolumeName30MB, mb30, mb20), - } + testObjects := []runtime.Object{&volumes[0], &volumes[1]} var wg sync.WaitGroup ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second) + defer cancel() + fmc := createFakeMetricsCollector() - client.SetLatestDirectCSIVolumeInterface(fakedirect.NewSimpleClientset(testObjects...).DirectV1beta4().DirectCSIVolumes()) + + clientset := types.NewExtFakeClientset(clientsetfake.NewSimpleClientset(testObjects...)) + client.SetVolumeInterface(clientset.DirectpvLatest().DirectPVVolumes()) metricChan := make(chan prometheus.Metric) noOfMetricsExposedPerVolume := 2 @@ -137,7 +141,7 @@ func TestVolumeStatsEmitter(t *testing.T) { if !ok { return } - metricOut := dto.Metric{} + metricOut := clientmodelgo.Metric{} if err := metric.Write(&metricOut); err != nil { (*t).Fatal(err) } @@ -145,7 +149,7 @@ func TestVolumeStatsEmitter(t *testing.T) { mt := metricType(getFQNameFromDesc(metric.Desc().String())) switch mt { case metricStatsBytesUsed: - volObj, gErr := client.GetLatestDirectCSIVolumeInterface().Get(ctx, volumeName, metav1.GetOptions{ + volObj, gErr := client.VolumeClient().Get(ctx, volumeName, metav1.GetOptions{ TypeMeta: types.NewVolumeTypeMeta(), }) if gErr != nil { @@ -155,7 +159,7 @@ func TestVolumeStatsEmitter(t *testing.T) { t.Errorf("Expected Used capacity: %v But got %v", int64(volObj.Status.UsedCapacity), int64(*metricOut.Gauge.Value)) } case metricStatsBytesTotal: - volObj, gErr := client.GetLatestDirectCSIVolumeInterface().Get(ctx, volumeName, metav1.GetOptions{ + volObj, gErr := client.VolumeClient().Get(ctx, volumeName, metav1.GetOptions{ TypeMeta: types.NewVolumeTypeMeta(), }) if gErr != nil { @@ -175,8 +179,8 @@ func TestVolumeStatsEmitter(t *testing.T) { } }() - fmc.volumeStatsEmitter(ctx, metricChan, testStatsGetter) + fmc.publishVolumeStats(ctx, &volumes[0], metricChan) + fmc.publishVolumeStats(ctx, &volumes[1], metricChan) wg.Wait() - cancel() } diff --git a/pkg/node/fake.go b/pkg/node/fake.go index 26b74d147..eaa8c5ba5 100644 --- a/pkg/node/fake.go +++ b/pkg/node/fake.go @@ -37,8 +37,9 @@ func createFakeServer() *Server { getMounts: func() (map[string][]string, map[string][]string, error) { return map[string][]string{consts.MountRootDir: {}}, nil, nil }, - bindMount: func(source, target string, readOnly bool) error { return nil }, - unmount: func(target string) error { return nil }, + getDeviceByFSUUID: func(fsuuid string) (string, error) { return "", nil }, + bindMount: func(source, target string, readOnly bool) error { return nil }, + unmount: func(target string) error { return nil }, getQuota: func(ctx context.Context, device, volumeID string) (quota *xfs.Quota, err error) { return &xfs.Quota{}, nil }, diff --git a/pkg/node/publish_unpublish_test.go b/pkg/node/publish_unpublish_test.go index 255208706..c7f6986ef 100644 --- a/pkg/node/publish_unpublish_test.go +++ b/pkg/node/publish_unpublish_test.go @@ -23,10 +23,10 @@ import ( "github.com/container-storage-interface/spec/lib/go/csi" directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types" + "github.com/minio/directpv/pkg/client" clientsetfake "github.com/minio/directpv/pkg/clientset/fake" "github.com/minio/directpv/pkg/consts" "github.com/minio/directpv/pkg/k8s" - "github.com/minio/directpv/pkg/mount" "github.com/minio/directpv/pkg/types" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -48,10 +48,11 @@ func TestNodePublishVolume(t *testing.T) { Status: types.VolumeStatus{StagingPath: "volume-id-1-staging-target-path"}, } + clientset := types.NewExtFakeClientset(clientsetfake.NewSimpleClientset(volume)) + client.SetVolumeInterface(clientset.DirectpvLatest().DirectPVVolumes()) + nodeServer := createFakeServer() - nodeServer.directcsiClient = types.NewExtFakeClientset(clientsetfake.NewSimpleClientset(volume)) - _, err := nodeServer.NodePublishVolume(context.TODO(), req) - if err == nil { + if _, err := nodeServer.NodePublishVolume(context.TODO(), req); err == nil { t.Fatalf("expected error, but succeeded") } } @@ -139,22 +140,23 @@ func TestPublishUnpublishVolume(t *testing.T) { TargetPath: testContainerPath, } + clientset := types.NewExtFakeClientset(clientsetfake.NewSimpleClientset(testVol)) + client.SetVolumeInterface(clientset.DirectpvLatest().DirectPVVolumes()) + ctx := context.TODO() ns := createFakeServer() - ns.directcsiClient = types.NewExtFakeClientset(clientsetfake.NewSimpleClientset(testVol)) - directCSIClient := ns.directcsiClient.DirectpvLatest() // Publish volume test - ns.probeMounts = func() (map[string][]mount.MountInfo, error) { - return map[string][]mount.MountInfo{"0:0": {{MountPoint: testStagingPath}}}, nil + ns.getMounts = func() (map[string][]string, map[string][]string, error) { + return map[string][]string{testStagingPath: {}}, nil, nil } _, err := ns.NodePublishVolume(ctx, &publishVolumeRequest) if err != nil { t.Fatalf("[%s] PublishVolume failed. Error: %v", publishVolumeRequest.VolumeId, err) } - volObj, gErr := directCSIClient.DirectCSIVolumes().Get(ctx, publishVolumeRequest.GetVolumeId(), metav1.GetOptions{ - TypeMeta: directpvtypes.NewVolumeTypeMeta(), + volObj, gErr := client.VolumeClient().Get(ctx, publishVolumeRequest.GetVolumeId(), metav1.GetOptions{ + TypeMeta: types.NewVolumeTypeMeta(), }) if gErr != nil { t.Fatalf("Volume (%s) not found. Error: %v", publishVolumeRequest.GetVolumeId(), gErr) @@ -175,8 +177,8 @@ func TestPublishUnpublishVolume(t *testing.T) { t.Fatalf("[%s] PublishVolume failed. Error: %v", unpublishVolumeRequest.VolumeId, err) } - volObj, gErr = directCSIClient.DirectCSIVolumes().Get(ctx, unpublishVolumeRequest.GetVolumeId(), metav1.GetOptions{ - TypeMeta: directpvtypes.NewVolumeTypeMeta(), + volObj, gErr = client.VolumeClient().Get(ctx, unpublishVolumeRequest.GetVolumeId(), metav1.GetOptions{ + TypeMeta: types.NewVolumeTypeMeta(), }) if gErr != nil { t.Fatalf("Volume (%s) not found. Error: %v", unpublishVolumeRequest.GetVolumeId(), gErr) diff --git a/pkg/node/stage_unstage_test.go b/pkg/node/stage_unstage_test.go index fb3e4c9ae..63e1adf10 100644 --- a/pkg/node/stage_unstage_test.go +++ b/pkg/node/stage_unstage_test.go @@ -18,19 +18,17 @@ package node import ( "context" - "errors" + "fmt" "path" "testing" "github.com/container-storage-interface/spec/lib/go/csi" - directcsi "github.com/minio/directpv/pkg/apis/direct.csi.min.io/v1beta4" - "github.com/minio/directpv/pkg/apis/directpv.min.io/types" + directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types" "github.com/minio/directpv/pkg/client" - fakedirect "github.com/minio/directpv/pkg/clientset/fake" + clientsetfake "github.com/minio/directpv/pkg/clientset/fake" "github.com/minio/directpv/pkg/consts" "github.com/minio/directpv/pkg/k8s" - "github.com/minio/directpv/pkg/mount" - "github.com/minio/directpv/pkg/sys" + "github.com/minio/directpv/pkg/types" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" ) @@ -62,7 +60,7 @@ func TestNodeStageVolume(t *testing.T) { case2Drive := &types.Drive{ TypeMeta: types.NewDriveTypeMeta(), ObjectMeta: metav1.ObjectMeta{Name: "drive-1"}, - Status: types.DriveStatus{DriveStatus: directcsi.DriveStatusInUse}, + Status: types.DriveStatus{Status: directpvtypes.DriveStatusOK}, } case3Drive := &types.Drive{ @@ -71,18 +69,18 @@ func TestNodeStageVolume(t *testing.T) { Name: "drive-1", Finalizers: []string{consts.DriveFinalizerPrefix + "volume-id-1"}, }, - Status: types.DriveStatus{DriveStatus: directcsi.DriveStatusInUse}, + Status: types.DriveStatus{Status: directpvtypes.DriveStatusOK}, } testCases := []struct { req *csi.NodeStageVolumeRequest drive *types.Drive - mountInfo map[string][]mount.MountInfo + mountInfo map[string][]string }{ {case1Req, case1Drive, nil}, {case1Req, case2Drive, nil}, - {case1Req, case3Drive, map[string][]mount.MountInfo{"1:0": {}}}, - {case1Req, case3Drive, map[string][]mount.MountInfo{"0:0": {}}}, + {case1Req, case3Drive, map[string][]string{consts.MountRootDir: {}}}, + {case1Req, case3Drive, map[string][]string{consts.MountRootDir: {}}}, } for i, testCase := range testCases { @@ -91,24 +89,26 @@ func TestNodeStageVolume(t *testing.T) { ObjectMeta: metav1.ObjectMeta{Name: testCase.req.VolumeId}, Status: types.VolumeStatus{ NodeName: testNodeName, - Drive: testCase.drive.Name, + DriveName: testCase.drive.Name, TotalCapacity: 100 * MB, }, } + clientset := types.NewExtFakeClientset(clientsetfake.NewSimpleClientset(volume, testCase.drive)) + client.SetDriveInterface(clientset.DirectpvLatest().DirectPVDrives()) + client.SetVolumeInterface(clientset.DirectpvLatest().DirectPVVolumes()) + nodeServer := createFakeServer() - nodeServer.directcsiClient = fakedirect.NewSimpleClientset(volume, testCase.drive) - nodeServer.probeMounts = func() (map[string][]mount.MountInfo, error) { - return testCase.mountInfo, nil + nodeServer.getMounts = func() (map[string][]string, map[string][]string, error) { + return testCase.mountInfo, nil, nil } - nodeServer.verifyHostStateForDrive = func(drive *types.Drive) error { - if drive.Status.Path == "" { - return errors.New("empty path") + nodeServer.bindMount = func(source, stagingTargetPath string, readOnly bool) error { + if _, found := testCase.mountInfo[source]; !found { + return fmt.Errorf("source is not mounted") } return nil } - _, err := nodeServer.NodeStageVolume(context.TODO(), testCase.req) - if err == nil { + if _, err := nodeServer.NodeStageVolume(context.TODO(), testCase.req); err == nil { t.Fatalf("case %v: expected error, but succeeded", i+1) } } @@ -129,9 +129,8 @@ func TestStageUnstageVolume(t *testing.T) { }, }, Status: types.DriveStatus{ - Mountpoint: path.Join(sys.MountRoot, testDriveName), NodeName: testNodeName, - DriveStatus: directcsi.DriveStatusInUse, + Status: directpvtypes.DriveStatusOK, FreeCapacity: mb50, AllocatedCapacity: mb50, TotalCapacity: mb100, @@ -147,7 +146,8 @@ func TestStageUnstageVolume(t *testing.T) { }, Status: types.VolumeStatus{ NodeName: testNodeName, - Drive: testDriveName, + DriveName: testDriveName, + FSUUID: testDriveName, TotalCapacity: mb20, Conditions: []metav1.Condition{ { @@ -196,18 +196,15 @@ func TestStageUnstageVolume(t *testing.T) { StagingTargetPath: "/path/to/target", } + clientset := types.NewExtFakeClientset(clientsetfake.NewSimpleClientset(testObjects...)) + client.SetDriveInterface(clientset.DirectpvLatest().DirectPVDrives()) + client.SetVolumeInterface(clientset.DirectpvLatest().DirectPVVolumes()) + ctx := context.TODO() ns := createFakeServer() - ns.directcsiClient = fakedirect.NewSimpleClientset(testObjects...) - directCSIClient := ns.directcsiClient.DirectV1beta4() - hostPath := path.Join( - path.Join( - sys.MountRoot, - testDriveName, - ), - testVolumeName50MB) - ns.probeMounts = func() (map[string][]mount.MountInfo, error) { - return map[string][]mount.MountInfo{"0:0": {{MountPoint: "/var/lib/direct-csi/mnt", MajorMinor: "0:0"}}}, nil + hostPath := path.Join(consts.MountRootDir, testDriveName, ".FSUUID."+testDriveName, testVolumeName50MB) + ns.getMounts = func() (map[string][]string, map[string][]string, error) { + return map[string][]string{consts.MountRootDir: {}}, nil, nil } // Stage Volume test @@ -216,7 +213,7 @@ func TestStageUnstageVolume(t *testing.T) { t.Fatalf("[%s] StageVolume failed. Error: %v", stageVolumeRequest.VolumeId, err) } - volObj, gErr := directCSIClient.DirectCSIVolumes().Get(ctx, stageVolumeRequest.GetVolumeId(), metav1.GetOptions{ + volObj, gErr := client.VolumeClient().Get(ctx, stageVolumeRequest.GetVolumeId(), metav1.GetOptions{ TypeMeta: types.NewVolumeTypeMeta(), }) if gErr != nil { @@ -241,7 +238,7 @@ func TestStageUnstageVolume(t *testing.T) { t.Fatalf("[%s] UnstageVolume failed. Error: %v", unstageVolumeRequest.VolumeId, err) } - volObj, gErr = directCSIClient.DirectCSIVolumes().Get(ctx, unstageVolumeRequest.GetVolumeId(), metav1.GetOptions{ + volObj, gErr = client.VolumeClient().Get(ctx, unstageVolumeRequest.GetVolumeId(), metav1.GetOptions{ TypeMeta: types.NewVolumeTypeMeta(), }) if gErr != nil { diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index c6778850d..2366e60ca 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -18,16 +18,10 @@ package utils import ( "bytes" - "fmt" - "os" - "path" - "reflect" "testing" - "time" "github.com/minio/directpv/pkg/consts" - "github.com/minio/directpv/pkg/k8s" - "github.com/mitchellh/go-homedir" + "github.com/minio/directpv/pkg/types" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) @@ -35,10 +29,10 @@ import ( func TestWriteObject(t *testing.T) { var byteBuffer bytes.Buffer meta := metav1.ObjectMeta{ - Name: k8s.SanitizeResourceName(consts.GroupName), - Namespace: k8s.SanitizeResourceName(consts.GroupName), + Name: consts.GroupName, + Namespace: consts.GroupName, Annotations: map[string]string{ - string(config.CreatedByLabelKey): "kubectl/directpv", + string(types.CreatedByLabelKey): "kubectl/directpv", }, Labels: map[string]string{ "app": consts.GroupName, @@ -74,35 +68,3 @@ func TestWriteObject(t *testing.T) { } } } - -func TestNewSafeFile(t *testing.T) { - tempFile, _ := os.CreateTemp("", "safefile.") - dirname, _ := os.UserHomeDir() - timeinNs := time.Now().UnixNano() - homeDir, _ := homedir.Dir() - defaultDirname := path.Join(homeDir, ".directpv") - testCases := []struct { - input string - output *SafeFile - errReturned bool - }{ - { - input: fmt.Sprintf("%v/%v-%v", dirname+"/.directpv", "audit/"+"install", timeinNs), - output: &SafeFile{ - filename: fmt.Sprintf("%v/%v-%v", defaultDirname, "audit/"+"install", timeinNs), - tempFile: tempFile, - }, - errReturned: false, - }, - } - for i, test := range testCases { - out, err := NewSafeFile(testCases[i].input) - errReturned := err != nil - if errReturned != test.errReturned { - t.Fatalf("Test %d: expected %t got %t", i+1, test.errReturned, errReturned) - } - if !reflect.DeepEqual(out.filename, test.input) { - t.Fatalf("Test %d: expected %v got %v", i+1, test.input, out.filename) - } - } -} diff --git a/pkg/volume/event_test.go b/pkg/volume/event_test.go index 0a69d20ac..e44ee01fc 100644 --- a/pkg/volume/event_test.go +++ b/pkg/volume/event_test.go @@ -21,11 +21,12 @@ import ( "errors" "testing" - directcsi "github.com/minio/directpv/pkg/apis/direct.csi.min.io/v1beta4" + directpvtypes "github.com/minio/directpv/pkg/apis/directpv.min.io/types" "github.com/minio/directpv/pkg/client" clientsetfake "github.com/minio/directpv/pkg/clientset/fake" + "github.com/minio/directpv/pkg/consts" "github.com/minio/directpv/pkg/listener" - "github.com/minio/directpv/pkg/utils" + "github.com/minio/directpv/pkg/types" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" ) @@ -65,7 +66,7 @@ func TestVolumeEventHandlerHandle(t *testing.T) { }, Status: types.DriveStatus{ NodeName: "test-node", - DriveStatus: directcsi.DriveStatusInUse, + Status: directpvtypes.DriveStatusOK, FreeCapacity: mb50, AllocatedCapacity: mb50, TotalCapacity: mb100, @@ -83,25 +84,25 @@ func TestVolumeEventHandlerHandle(t *testing.T) { Status: types.VolumeStatus{ NodeName: "test-node", HostPath: "hostpath", - Drive: testDriveName, + DriveName: testDriveName, TotalCapacity: mb20, Conditions: []metav1.Condition{ { - Type: string(types.VolumeConditionStaged), + Type: string(directpvtypes.VolumeConditionTypeStaged), Status: metav1.ConditionTrue, Message: "", Reason: string((directpvtypes.VolumeConditionReasonInUse)), LastTransitionTime: metav1.Now(), }, { - Type: string(types.VolumeConditionPublished), + Type: string(directpvtypes.VolumeConditionTypePublished), Status: metav1.ConditionFalse, Message: "", - Reason: string(types.VolumeReasonNotInUse), + Reason: string(directpvtypes.VolumeConditionReasonNotInUse), LastTransitionTime: metav1.Now(), }, { - Type: string(types.VolumeConditionReady), + Type: string(directpvtypes.VolumeConditionTypeReady), Status: metav1.ConditionTrue, Message: "", Reason: string((directpvtypes.VolumeConditionReasonReady)), @@ -121,27 +122,27 @@ func TestVolumeEventHandlerHandle(t *testing.T) { Status: types.VolumeStatus{ NodeName: "test-node", HostPath: "hostpath", - Drive: testDriveName, + DriveName: testDriveName, TotalCapacity: mb30, StagingPath: "/path/staging", ContainerPath: "/path/container", Conditions: []metav1.Condition{ { - Type: string(types.VolumeConditionStaged), + Type: string(directpvtypes.VolumeConditionTypeStaged), Status: metav1.ConditionFalse, Message: "", - Reason: string(types.VolumeReasonNotInUse), + Reason: string(directpvtypes.VolumeConditionReasonNotInUse), LastTransitionTime: metav1.Now(), }, { - Type: string(types.VolumeConditionPublished), + Type: string(directpvtypes.VolumeConditionTypePublished), Status: metav1.ConditionFalse, Message: "", - Reason: string(types.VolumeReasonNotInUse), + Reason: string(directpvtypes.VolumeConditionReasonNotInUse), LastTransitionTime: metav1.Now(), }, { - Type: string(types.VolumeConditionReady), + Type: string(directpvtypes.VolumeConditionTypeReady), Status: metav1.ConditionTrue, Message: "", Reason: string((directpvtypes.VolumeConditionReasonReady)), @@ -154,8 +155,12 @@ func TestVolumeEventHandlerHandle(t *testing.T) { vl := createFakeVolumeEventListener("test-node") ctx := context.TODO() - client.SetLatestDirectCSIDriveInterface(clientsetfake.NewSimpleClientset(testDriveObject).DirectV1beta4().DirectCSIDrives()) - client.SetLatestDirectCSIVolumeInterface(clientsetfake.NewSimpleClientset(testVolumeObjects...).DirectV1beta4().DirectCSIVolumes()) + + clientset1 := types.NewExtFakeClientset(clientsetfake.NewSimpleClientset(testDriveObject)) + client.SetDriveInterface(clientset1.DirectpvLatest().DirectPVDrives()) + clientset2 := types.NewExtFakeClientset(clientsetfake.NewSimpleClientset(testVolumeObjects...)) + client.SetVolumeInterface(clientset2.DirectpvLatest().DirectPVVolumes()) + for _, testObj := range testVolumeObjects { var stagingUmountCalled, containerUmountCalled bool vl.safeUnmount = func(target string, force, detach, expire bool) error { @@ -174,7 +179,7 @@ func TestVolumeEventHandlerHandle(t *testing.T) { if !ok { continue } - newObj, vErr := client.GetLatestDirectCSIVolumeInterface().Get(ctx, vObj.Name, metav1.GetOptions{TypeMeta: types.NewVolumeTypeMeta()}) + newObj, vErr := client.VolumeClient().Get(ctx, vObj.Name, metav1.GetOptions{TypeMeta: types.NewVolumeTypeMeta()}) if vErr != nil { t.Fatalf("Error while getting the volume object: %+v", vErr) } @@ -182,7 +187,7 @@ func TestVolumeEventHandlerHandle(t *testing.T) { now := metav1.Now() newObj.DeletionTimestamp = &now - _, vErr = client.GetLatestDirectCSIVolumeInterface().Update( + _, vErr = client.VolumeClient().Update( ctx, newObj, metav1.UpdateOptions{TypeMeta: types.NewVolumeTypeMeta()}, ) if vErr != nil { @@ -197,7 +202,7 @@ func TestVolumeEventHandlerHandle(t *testing.T) { if newObj.Status.ContainerPath != "" && !containerUmountCalled { t.Error("container path is not umounted") } - updatedVolume, err := client.GetLatestDirectCSIVolumeInterface().Get( + updatedVolume, err := client.VolumeClient().Get( ctx, newObj.Name, metav1.GetOptions{TypeMeta: types.NewVolumeTypeMeta()}, ) if err != nil { @@ -208,7 +213,7 @@ func TestVolumeEventHandlerHandle(t *testing.T) { } } - driveObj, dErr := client.GetLatestDirectCSIDriveInterface().Get(ctx, testDriveName, metav1.GetOptions{TypeMeta: types.NewDriveTypeMeta()}) + driveObj, dErr := client.DriveClient().Get(ctx, testDriveName, metav1.GetOptions{TypeMeta: types.NewDriveTypeMeta()}) if dErr != nil { t.Fatalf("Error while getting the drive object: %+v", dErr) } @@ -217,8 +222,8 @@ func TestVolumeEventHandlerHandle(t *testing.T) { if len(driveFinalizers) != 1 || driveFinalizers[0] != consts.DriveFinalizerDataProtection { t.Fatalf("Unexpected drive finalizers set after clean-up: %+v", driveFinalizers) } - if driveObj.Status.DriveStatus != directpvtypes.DriveStatusOK { - t.Errorf("Unexpected drive status set. Expected: %s, Got: %s", string(directpvtypes.DriveStatusOK), string(driveObj.Status.DriveStatus)) + if driveObj.Status.Status != directpvtypes.DriveStatusOK { + t.Errorf("Unexpected drive status set. Expected: %s, Got: %s", string(directpvtypes.DriveStatusOK), string(driveObj.Status.Status)) } if driveObj.Status.FreeCapacity != mb100 { t.Errorf("Unexpected free capacity set. Expected: %d, Got: %d", mb100, driveObj.Status.FreeCapacity) @@ -241,25 +246,25 @@ func TestAbnormalDeleteEventHandle(t *testing.T) { Status: types.VolumeStatus{ NodeName: "test-node", HostPath: "hostpath", - Drive: "test-drive", + DriveName: "test-drive", TotalCapacity: int64(100), Conditions: []metav1.Condition{ { - Type: string(types.VolumeConditionStaged), + Type: string(directpvtypes.VolumeConditionTypeStaged), Status: metav1.ConditionTrue, Message: "", Reason: string((directpvtypes.VolumeConditionReasonInUse)), LastTransitionTime: metav1.Now(), }, { - Type: string(types.VolumeConditionPublished), + Type: string(directpvtypes.VolumeConditionTypePublished), Status: metav1.ConditionFalse, Message: "", - Reason: string(types.VolumeReasonNotInUse), + Reason: string(directpvtypes.VolumeConditionReasonNotInUse), LastTransitionTime: metav1.Now(), }, { - Type: string(types.VolumeConditionReady), + Type: string(directpvtypes.VolumeConditionTypeReady), Status: metav1.ConditionTrue, Message: "", Reason: string((directpvtypes.VolumeConditionReasonReady)), @@ -271,15 +276,17 @@ func TestAbnormalDeleteEventHandle(t *testing.T) { vl := createFakeVolumeEventListener("test-node") ctx := context.TODO() - client.SetLatestDirectCSIVolumeInterface(clientsetfake.NewSimpleClientset(testVolumeObject).DirectV1beta4().DirectCSIVolumes()) - newObj, vErr := client.GetLatestDirectCSIVolumeInterface().Get(ctx, testVolumeObject.Name, metav1.GetOptions{TypeMeta: types.NewVolumeTypeMeta()}) + clientset := types.NewExtFakeClientset(clientsetfake.NewSimpleClientset(testVolumeObject)) + client.SetVolumeInterface(clientset.DirectpvLatest().DirectPVVolumes()) + + newObj, vErr := client.VolumeClient().Get(ctx, testVolumeObject.Name, metav1.GetOptions{TypeMeta: types.NewVolumeTypeMeta()}) if vErr != nil { t.Fatalf("Error while getting the volume object: %+v", vErr) } now := metav1.Now() newObj.DeletionTimestamp = &now - _, vErr = client.GetLatestDirectCSIVolumeInterface().Update( + _, vErr = client.VolumeClient().Update( ctx, newObj, metav1.UpdateOptions{TypeMeta: types.NewVolumeTypeMeta()}, ) if vErr != nil { diff --git a/pkg/volume/list_test.go b/pkg/volume/list_test.go index 3f289419d..e25f9a862 100644 --- a/pkg/volume/list_test.go +++ b/pkg/volume/list_test.go @@ -21,14 +21,18 @@ import ( "fmt" "testing" - directcsi "github.com/minio/directpv/pkg/apis/direct.csi.min.io/v1beta4" + "github.com/minio/directpv/pkg/client" clientsetfake "github.com/minio/directpv/pkg/clientset/fake" + "github.com/minio/directpv/pkg/types" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" runtime "k8s.io/apimachinery/pkg/runtime" ) func TestGetVolumeList(t *testing.T) { - SetLatestDirectCSIVolumeInterface(clientsetfake.NewSimpleClientset().DirectV1beta4().DirectCSIVolumes()) + clientset := types.NewExtFakeClientset(clientsetfake.NewSimpleClientset()) + client.SetDriveInterface(clientset.DirectpvLatest().DirectPVDrives()) + client.SetVolumeInterface(clientset.DirectpvLatest().DirectPVVolumes()) + volumes, err := GetVolumeList(context.TODO(), nil, nil, nil, nil) if err != nil { t.Fatalf("unexpected error: %v", err) @@ -44,7 +48,10 @@ func TestGetVolumeList(t *testing.T) { ) } - SetLatestDirectCSIVolumeInterface(clientsetfake.NewSimpleClientset(objects...).DirectV1beta4().DirectCSIVolumes()) + clientset = types.NewExtFakeClientset(clientsetfake.NewSimpleClientset(objects...)) + client.SetDriveInterface(clientset.DirectpvLatest().DirectPVDrives()) + client.SetVolumeInterface(clientset.DirectpvLatest().DirectPVVolumes()) + volumes, err = GetVolumeList(context.TODO(), nil, nil, nil, nil) if err != nil { t.Fatalf("unexpected error: %v", err) @@ -55,7 +62,10 @@ func TestGetVolumeList(t *testing.T) { } func TestGetSortedVolumeList(t *testing.T) { - SetLatestDirectCSIVolumeInterface(clientsetfake.NewSimpleClientset().DirectV1beta4().DirectCSIVolumes()) + clientset := types.NewExtFakeClientset(clientsetfake.NewSimpleClientset()) + client.SetDriveInterface(clientset.DirectpvLatest().DirectPVDrives()) + client.SetVolumeInterface(clientset.DirectpvLatest().DirectPVVolumes()) + volumes, err := GetVolumeList(context.TODO(), nil, nil, nil, nil) if err != nil { t.Fatalf("unexpected error: %v", err) @@ -81,7 +91,10 @@ func TestGetSortedVolumeList(t *testing.T) { ) } - SetLatestDirectCSIVolumeInterface(clientsetfake.NewSimpleClientset(objects...).DirectV1beta4().DirectCSIVolumes()) + clientset = types.NewExtFakeClientset(clientsetfake.NewSimpleClientset(objects...)) + client.SetDriveInterface(clientset.DirectpvLatest().DirectPVDrives()) + client.SetVolumeInterface(clientset.DirectpvLatest().DirectPVVolumes()) + volumes, err = GetVolumeList(context.TODO(), nil, nil, nil, nil) if err != nil { t.Fatalf("unexpected error: %v", err)