-
Notifications
You must be signed in to change notification settings - Fork 889
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
Add ut for pkg/controllers/applicationfailover
and pkg/controllers/status
#3621
Merged
karmada-bot
merged 4 commits into
karmada-io:master
from
realnumber666:add-ut/crb_status_controller
Jun 6, 2023
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0ee9be3
Add UT for pkg/controllers/status/crb_status_controller.go
realnumber666 8e0f186
Add UT for pkg/controllers/status/rb_status_controller.go
realnumber666 e97d295
Add UT for pkg/controllers/applicationfailover/common.go
realnumber666 7864e11
Add UT for crb_application_failover_controller.go and rb_application_…
realnumber666 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 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 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,205 @@ | ||
package status | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/meta" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/types" | ||
dynamicfake "k8s.io/client-go/dynamic/fake" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
"k8s.io/client-go/tools/record" | ||
controllerruntime "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
|
||
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2" | ||
"github.com/karmada-io/karmada/pkg/util/fedinformer/genericmanager" | ||
"github.com/karmada-io/karmada/pkg/util/gclient" | ||
) | ||
|
||
func generateCRBStatusController() *CRBStatusController { | ||
stopCh := make(chan struct{}) | ||
defer close(stopCh) | ||
dynamicClient := dynamicfake.NewSimpleDynamicClient(scheme.Scheme, | ||
&corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "pod1", Namespace: "default"}}) | ||
m := genericmanager.NewSingleClusterInformerManager(dynamicClient, 0, stopCh) | ||
m.Lister(corev1.SchemeGroupVersion.WithResource("pods")) | ||
m.Start() | ||
m.WaitForCacheSync() | ||
|
||
c := &CRBStatusController{ | ||
Client: fake.NewClientBuilder().WithScheme(gclient.NewSchema()).Build(), | ||
DynamicClient: dynamicClient, | ||
InformerManager: m, | ||
RESTMapper: func() meta.RESTMapper { | ||
m := meta.NewDefaultRESTMapper([]schema.GroupVersion{corev1.SchemeGroupVersion}) | ||
m.Add(corev1.SchemeGroupVersion.WithKind("Pod"), meta.RESTScopeNamespace) | ||
return m | ||
}(), | ||
EventRecorder: &record.FakeRecorder{}, | ||
} | ||
return c | ||
} | ||
|
||
func TestCRBStatusController_Reconcile(t *testing.T) { | ||
preTime := metav1.Date(2023, 0, 0, 0, 0, 0, 0, time.UTC) | ||
tests := []struct { | ||
name string | ||
binding *workv1alpha2.ClusterResourceBinding | ||
expectRes controllerruntime.Result | ||
expectError bool | ||
}{ | ||
{ | ||
name: "failed in syncBindingStatus", | ||
binding: &workv1alpha2.ClusterResourceBinding{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "binding", | ||
Namespace: "default", | ||
}, | ||
Spec: workv1alpha2.ResourceBindingSpec{ | ||
Resource: workv1alpha2.ObjectReference{ | ||
APIVersion: "v1", | ||
Kind: "Pod", | ||
Namespace: "default", | ||
Name: "pod", | ||
}, | ||
}, | ||
}, | ||
expectRes: controllerruntime.Result{}, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "binding not found in client", | ||
expectRes: controllerruntime.Result{}, | ||
expectError: false, | ||
}, | ||
{ | ||
name: "failed in syncBindingStatus", | ||
binding: &workv1alpha2.ClusterResourceBinding{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "binding", | ||
Namespace: "default", | ||
DeletionTimestamp: &preTime, | ||
}, | ||
Spec: workv1alpha2.ResourceBindingSpec{ | ||
Resource: workv1alpha2.ObjectReference{ | ||
APIVersion: "v1", | ||
Kind: "Pod", | ||
Namespace: "default", | ||
Name: "pod", | ||
}, | ||
}, | ||
}, | ||
expectRes: controllerruntime.Result{}, | ||
expectError: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
c := generateCRBStatusController() | ||
|
||
// Prepare req | ||
req := controllerruntime.Request{ | ||
NamespacedName: types.NamespacedName{ | ||
Name: "binding", | ||
Namespace: "default", | ||
}, | ||
} | ||
|
||
// Prepare binding and create it in client | ||
if tt.binding != nil { | ||
if err := c.Client.Create(context.Background(), tt.binding); err != nil { | ||
t.Fatalf("Failed to create binding: %v", err) | ||
} | ||
} | ||
|
||
res, err := c.Reconcile(context.Background(), req) | ||
assert.Equal(t, tt.expectRes, res) | ||
if tt.expectError { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestCRBStatusController_syncBindingStatus(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
resource workv1alpha2.ObjectReference | ||
podNameInDynamicClient string | ||
resourceExistInClient bool | ||
expectedError bool | ||
}{ | ||
{ | ||
name: "failed in FetchResourceTemplate, err is NotFound", | ||
resource: workv1alpha2.ObjectReference{ | ||
APIVersion: "v1", | ||
Kind: "Pod", | ||
Namespace: "default", | ||
Name: "pod", | ||
}, | ||
podNameInDynamicClient: "pod1", | ||
resourceExistInClient: true, | ||
expectedError: false, | ||
}, | ||
{ | ||
name: "failed in FetchResourceTemplate, err is not NotFound", | ||
resource: workv1alpha2.ObjectReference{}, | ||
podNameInDynamicClient: "pod", | ||
resourceExistInClient: true, | ||
expectedError: true, | ||
}, | ||
{ | ||
name: "failed in AggregateClusterResourceBindingWorkStatus", | ||
resource: workv1alpha2.ObjectReference{ | ||
APIVersion: "v1", | ||
Kind: "Pod", | ||
Namespace: "default", | ||
Name: "pod", | ||
}, | ||
podNameInDynamicClient: "pod", | ||
resourceExistInClient: false, | ||
expectedError: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
c := generateCRBStatusController() | ||
c.DynamicClient = dynamicfake.NewSimpleDynamicClient(scheme.Scheme, | ||
&corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: tt.podNameInDynamicClient, Namespace: "default"}}) | ||
|
||
binding := &workv1alpha2.ClusterResourceBinding{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "binding", | ||
Namespace: "default", | ||
}, | ||
Spec: workv1alpha2.ResourceBindingSpec{ | ||
Resource: tt.resource, | ||
}, | ||
} | ||
|
||
if tt.resourceExistInClient { | ||
if err := c.Client.Create(context.Background(), binding); err != nil { | ||
t.Fatalf("Failed to create binding: %v", err) | ||
} | ||
} | ||
|
||
err := c.syncBindingStatus(binding) | ||
|
||
if tt.expectedError { | ||
assert.Error(t, err) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} |
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.
I see that this test will be overwritten by other tests. Can we consider removing it?