-
Notifications
You must be signed in to change notification settings - Fork 93
add units for ValidateBackupStorageLocations #159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dymurray
merged 3 commits into
openshift:golang-operator
from
alaypatel07:unit-test-validate-bsl
Aug 3, 2021
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,204 @@ | ||
| package controllers | ||
|
|
||
| import ( | ||
| "context" | ||
| "reflect" | ||
| "testing" | ||
|
|
||
| "github.com/go-logr/logr" | ||
| oadpv1alpha1 "github.com/openshift/oadp-operator/api/v1alpha1" | ||
| velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/runtime" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "k8s.io/client-go/kubernetes/scheme" | ||
| "k8s.io/client-go/tools/record" | ||
| "k8s.io/utils/pointer" | ||
| "sigs.k8s.io/controller-runtime/pkg/client" | ||
| "sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
| ) | ||
|
|
||
| func getSchemeForFakeClient() (*runtime.Scheme, error) { | ||
| err := oadpv1alpha1.AddToScheme(scheme.Scheme) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| err = velerov1.AddToScheme(scheme.Scheme) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return scheme.Scheme, nil | ||
| } | ||
|
|
||
| func getFakeClientFromObjects(objs ...client.Object) (client.WithWatch, error) { | ||
| schemeForFakeClient, err := getSchemeForFakeClient() | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return fake.NewClientBuilder().WithScheme(schemeForFakeClient).WithObjects(objs...).Build(), nil | ||
| } | ||
|
|
||
| func TestVeleroReconciler_ValidateBackupStorageLocations(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| VeleroCR *oadpv1alpha1.Velero | ||
| want bool | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "test no BSLs, no noobaa", | ||
| VeleroCR: &oadpv1alpha1.Velero{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "foo", | ||
| Namespace: "bar", | ||
| }, | ||
| Spec: oadpv1alpha1.VeleroSpec{}, | ||
| }, | ||
| want: false, | ||
| wantErr: true, | ||
| }, | ||
| { | ||
| name: "test BSLs specified, no noobaa", | ||
| VeleroCR: &oadpv1alpha1.Velero{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "foo", | ||
| Namespace: "bar", | ||
| }, | ||
| Spec: oadpv1alpha1.VeleroSpec{ | ||
| BackupStorageLocations: []velerov1.BackupStorageLocationSpec{ | ||
| { | ||
| // TODO: foo is invalid provider, add test cases for it | ||
| Provider: "foo", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| want: true, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "test no BSL, noobaa configured", | ||
| VeleroCR: &oadpv1alpha1.Velero{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "foo", | ||
| Namespace: "bar", | ||
| }, | ||
| Spec: oadpv1alpha1.VeleroSpec{ | ||
| Noobaa: true, | ||
| }, | ||
| }, | ||
| want: true, | ||
| wantErr: false, | ||
| }, | ||
| { | ||
| name: "test get error", | ||
| VeleroCR: &oadpv1alpha1.Velero{}, | ||
| want: false, | ||
| wantErr: true, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| fakeClient, err := getFakeClientFromObjects(tt.VeleroCR) | ||
| if err != nil { | ||
| t.Errorf("error in creating fake client, likely programmer error") | ||
| } | ||
| r := &VeleroReconciler{ | ||
| Client: fakeClient, | ||
| Scheme: fakeClient.Scheme(), | ||
| Log: logr.Discard(), | ||
| Context: newContextForTest(tt.name), | ||
| NamespacedName: types.NamespacedName{ | ||
| Namespace: tt.VeleroCR.Namespace, | ||
| Name: tt.VeleroCR.Name, | ||
| }, | ||
| EventRecorder: record.NewFakeRecorder(10), | ||
| } | ||
| got, err := r.ValidateBackupStorageLocations(r.Log) | ||
| if (err != nil) != tt.wantErr { | ||
| t.Errorf("ValidateBackupStorageLocations() error = %v, wantErr %v", err, tt.wantErr) | ||
| return | ||
| } | ||
| if got != tt.want { | ||
| t.Errorf("ValidateBackupStorageLocations() got = %v, want %v", got, tt.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func newContextForTest(name string) context.Context { | ||
| return context.TODO() | ||
| } | ||
|
|
||
| func TestVeleroReconciler_updateBSLFromSpec(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| bsl *velerov1.BackupStorageLocation | ||
| velero *oadpv1alpha1.Velero | ||
| wantErr bool | ||
| }{ | ||
| { | ||
| name: "BSL without owner reference and labels", | ||
| bsl: &velerov1.BackupStorageLocation{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "foo-1", | ||
| Namespace: "bar", | ||
| }, | ||
| }, | ||
| velero: &oadpv1alpha1.Velero{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "foo", | ||
| Namespace: "bar", | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| scheme, err := getSchemeForFakeClient() | ||
| if err != nil { | ||
| t.Errorf("error getting scheme for the test: %#v", err) | ||
| } | ||
| r := &VeleroReconciler{ | ||
| Scheme: scheme, | ||
| } | ||
|
|
||
| wantBSl := &velerov1.BackupStorageLocation{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "foo-1", | ||
| Namespace: "bar", | ||
| Labels: map[string]string{ | ||
| "app.kubernetes.io/name": "oadp-operator-velero", | ||
| "app.kubernetes.io/instance": tt.velero.Name + "-1", | ||
| //"app.kubernetes.io/version": "x.y.z", | ||
| "app.kubernetes.io/managed-by": "oadp-operator", | ||
| "app.kubernetes.io/component": "bsl", | ||
| }, | ||
| OwnerReferences: []metav1.OwnerReference{{ | ||
| APIVersion: oadpv1alpha1.SchemeBuilder.GroupVersion.String(), | ||
| Kind: "Velero", | ||
| Name: tt.velero.Name, | ||
| UID: tt.velero.UID, | ||
| Controller: pointer.BoolPtr(true), | ||
| BlockOwnerDeletion: pointer.BoolPtr(true), | ||
| }}, | ||
| }, | ||
| } | ||
|
|
||
| err = r.updateBSLFromSpec(tt.bsl, tt.velero) | ||
| if (err != nil) != tt.wantErr { | ||
| t.Errorf("updateBSLFromSpec() error = %v, wantErr %v", err, tt.wantErr) | ||
| return | ||
| } | ||
| if !reflect.DeepEqual(tt.bsl.Labels, wantBSl.Labels) { | ||
| t.Errorf("expected bsl labels to be %#v, got %#v", wantBSl.Labels, tt.bsl.Labels) | ||
| } | ||
| if !reflect.DeepEqual(tt.bsl.OwnerReferences, wantBSl.OwnerReferences) { | ||
| t.Errorf("expected bsl owner references to be %#v, got %#v", wantBSl.OwnerReferences, tt.bsl.OwnerReferences) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a NIT, do you think we should make a fake client pkg separately? To make it easier for other tests to import the fake client usage?