Skip to content

Commit

Permalink
Add UT for pkg/controllers/applicationfailover/common.go
Browse files Browse the repository at this point in the history
Signed-off-by: rayywu <rayywu@tencent.com>
  • Loading branch information
realnumber666 committed Jun 1, 2023
1 parent 8e0f186 commit e97d295
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 5 deletions.
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)
}

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
12 changes: 7 additions & 5 deletions pkg/controllers/status/rb_status_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package status

import (
"context"
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"
"testing"
"time"

"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
Expand All @@ -16,8 +16,10 @@ import (
"k8s.io/client-go/tools/record"
controllerruntime "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"testing"
"time"

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 generateRBStatusController() *RBStatusController {
Expand Down

0 comments on commit e97d295

Please sign in to comment.