Skip to content

Commit

Permalink
fix unit tests (#641)
Browse files Browse the repository at this point in the history
Signed-off-by: Bala.FA <bala@minio.io>
  • Loading branch information
balamurugana committed Sep 18, 2022
1 parent ea06949 commit 22a9c8f
Show file tree
Hide file tree
Showing 15 changed files with 315 additions and 327 deletions.
56 changes: 28 additions & 28 deletions pkg/client/dynamic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
}
Expand All @@ -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())
Expand Down Expand Up @@ -182,15 +182,15 @@ 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})
}

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())
Expand Down Expand Up @@ -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})
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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())
Expand All @@ -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)
}
}
}
Expand All @@ -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)
}
}
}
Expand All @@ -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())
Expand Down Expand Up @@ -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())
Expand All @@ -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)
Expand All @@ -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()

Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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())
Expand All @@ -546,15 +546,15 @@ 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})
}

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())
Expand Down Expand Up @@ -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())
Expand All @@ -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)
}
}
}
Expand All @@ -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)
}
}
}
Expand All @@ -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())
Expand Down Expand Up @@ -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())
Expand Down
2 changes: 1 addition & 1 deletion pkg/controller/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
10 changes: 7 additions & 3 deletions pkg/drive/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
29 changes: 14 additions & 15 deletions pkg/installer/install_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
}
}
4 changes: 2 additions & 2 deletions pkg/installer/installer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
})
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/k8s/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/listener/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 22a9c8f

Please sign in to comment.