Add UT for pkg/controllers/applicationfailover/common.go

Signed-off-by: rayywu <rayywu@tencent.com>
This commit is contained in:
rayywu 2023-06-01 21:07:47 +08:00
parent 8e0f186302
commit e97d29500b
2 changed files with 85 additions and 5 deletions

View File

@ -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

View File

@ -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"
@ -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 {