Skip to content
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
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions pkg/controllers/applicationfailover/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,87 @@ import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"

workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
)

func TestNewWorkloadUnhealthyMap(t *testing.T) {
m := newWorkloadUnhealthyMap()
expected := &workloadUnhealthyMap{
workloadUnhealthy: make(map[types.NamespacedName]map[string]metav1.Time),
}
assert.Equal(t, expected, m)
}
Copy link
Member

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?


func TestTimeStampProcess(t *testing.T) {
key := types.NamespacedName{
Namespace: "default",
Name: "test",
}
cluster := "cluster-1"

m := newWorkloadUnhealthyMap()
m.setTimeStamp(key, cluster)
res := m.hasWorkloadBeenUnhealthy(key, cluster)
assert.Equal(t, true, res)

time := m.getTimeStamp(key, cluster)
assert.NotEmpty(t, time)

m.delete(key)
res = m.hasWorkloadBeenUnhealthy(key, cluster)
assert.Equal(t, false, res)
}

func TestWorkloadUnhealthyMap_deleteIrrelevantClusters(t *testing.T) {
cluster1 := "cluster-1"
cluster2 := "cluster-2"
cluster3 := "cluster-3"
t.Run("normal case", func(t *testing.T) {
key := types.NamespacedName{
Namespace: "default",
Name: "test",
}

m := newWorkloadUnhealthyMap()

m.setTimeStamp(key, cluster1)
m.setTimeStamp(key, cluster2)
m.setTimeStamp(key, cluster3)

allClusters := sets.New[string](cluster2, cluster3)
healthyClusters := []string{cluster3}

m.deleteIrrelevantClusters(key, allClusters, healthyClusters)
res1 := m.hasWorkloadBeenUnhealthy(key, cluster1)
assert.Equal(t, false, res1)
res2 := m.hasWorkloadBeenUnhealthy(key, cluster2)
assert.Equal(t, true, res2)
res3 := m.hasWorkloadBeenUnhealthy(key, cluster3)
assert.Equal(t, false, res3)
})

t.Run("unhealthyClusters is nil", func(t *testing.T) {
key := types.NamespacedName{
Namespace: "default",
Name: "test",
}

m := newWorkloadUnhealthyMap()

allClusters := sets.New[string](cluster2, cluster3)
healthyClusters := []string{cluster3}

m.deleteIrrelevantClusters(key, allClusters, healthyClusters)
res := m.hasWorkloadBeenUnhealthy(key, cluster2)
assert.Equal(t, false, res)
})
}

func TestDistinguishUnhealthyClustersWithOthers(t *testing.T) {
tests := []struct {
name string
Expand Down
205 changes: 205 additions & 0 deletions pkg/controllers/status/crb_status_controller_test.go
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)
}
})
}
}
Loading