Add 100% test coverage for matcher, shadow, and names.go in pkg/util
Signed-off-by: Nishant Bansal <nishant.bansal.mec21@iitbhu.ac.in>
This commit is contained in:
parent
37488bd109
commit
95b421dd55
|
@ -313,3 +313,135 @@ func TestExactOrWildcard(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMatches(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
matcher *Matcher
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
name: "not equal operations, equal group version and kind",
|
||||
matcher: &Matcher{
|
||||
ObjGVK: schema.GroupVersionKind{
|
||||
Group: "apps",
|
||||
Version: "v1",
|
||||
Kind: "Deployment",
|
||||
},
|
||||
Operation: configv1alpha1.InterpreterOperationPrune,
|
||||
Rule: configv1alpha1.RuleWithOperations{
|
||||
Operations: []configv1alpha1.InterpreterOperation{
|
||||
configv1alpha1.InterpreterOperationRetain,
|
||||
configv1alpha1.InterpreterOperationInterpretReplica,
|
||||
},
|
||||
Rule: configv1alpha1.Rule{
|
||||
APIGroups: []string{"batch", "apps"},
|
||||
APIVersions: []string{"v1", "v1beta1"},
|
||||
Kinds: []string{"Pod", "Deployment"},
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "not equal group, equal operation version and kind",
|
||||
matcher: &Matcher{
|
||||
ObjGVK: schema.GroupVersionKind{
|
||||
Group: "apps",
|
||||
Version: "v1",
|
||||
Kind: "Deployment",
|
||||
},
|
||||
Operation: configv1alpha1.InterpreterOperationRetain,
|
||||
Rule: configv1alpha1.RuleWithOperations{
|
||||
Operations: []configv1alpha1.InterpreterOperation{
|
||||
configv1alpha1.InterpreterOperationRetain,
|
||||
configv1alpha1.InterpreterOperationInterpretReplica,
|
||||
},
|
||||
Rule: configv1alpha1.Rule{
|
||||
APIGroups: []string{"batch"},
|
||||
APIVersions: []string{"v1", "v1beta1"},
|
||||
Kinds: []string{"Pod", "Deployment"},
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "not equal version, equal operation group and kind",
|
||||
matcher: &Matcher{
|
||||
ObjGVK: schema.GroupVersionKind{
|
||||
Group: "apps",
|
||||
Version: "v1",
|
||||
Kind: "Deployment",
|
||||
},
|
||||
Operation: configv1alpha1.InterpreterOperationRetain,
|
||||
Rule: configv1alpha1.RuleWithOperations{
|
||||
Operations: []configv1alpha1.InterpreterOperation{
|
||||
configv1alpha1.InterpreterOperationRetain,
|
||||
configv1alpha1.InterpreterOperationInterpretReplica,
|
||||
},
|
||||
Rule: configv1alpha1.Rule{
|
||||
APIGroups: []string{"batch", "apps"},
|
||||
APIVersions: []string{"v1beta1"},
|
||||
Kinds: []string{"Pod", "Deployment"},
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "not equal kind, equal operation group and version",
|
||||
matcher: &Matcher{
|
||||
ObjGVK: schema.GroupVersionKind{
|
||||
Group: "apps",
|
||||
Version: "v1",
|
||||
Kind: "Deployment",
|
||||
},
|
||||
Operation: configv1alpha1.InterpreterOperationRetain,
|
||||
Rule: configv1alpha1.RuleWithOperations{
|
||||
Operations: []configv1alpha1.InterpreterOperation{
|
||||
configv1alpha1.InterpreterOperationRetain,
|
||||
configv1alpha1.InterpreterOperationInterpretReplica,
|
||||
},
|
||||
Rule: configv1alpha1.Rule{
|
||||
APIGroups: []string{"batch", "apps"},
|
||||
APIVersions: []string{"v1", "v1beta1"},
|
||||
Kinds: []string{"Pod"},
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
name: "operation, object matches the rule",
|
||||
matcher: &Matcher{
|
||||
ObjGVK: schema.GroupVersionKind{
|
||||
Group: "apps",
|
||||
Version: "v1",
|
||||
Kind: "Deployment",
|
||||
},
|
||||
Operation: configv1alpha1.InterpreterOperationRetain,
|
||||
Rule: configv1alpha1.RuleWithOperations{
|
||||
Operations: []configv1alpha1.InterpreterOperation{
|
||||
configv1alpha1.InterpreterOperationRetain,
|
||||
configv1alpha1.InterpreterOperationInterpretReplica,
|
||||
},
|
||||
Rule: configv1alpha1.Rule{
|
||||
APIGroups: []string{"batch", "apps"},
|
||||
APIVersions: []string{"v1", "v1beta1"},
|
||||
Kinds: []string{"Pod", "Deployment"},
|
||||
},
|
||||
},
|
||||
},
|
||||
expected: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
res := tt.matcher.Matches()
|
||||
if res != tt.expected {
|
||||
t.Errorf("Matches() = %v, want %v", res, tt.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,7 +37,8 @@ func TestGenerateExecutionSpaceName(t *testing.T) {
|
|||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
{name: "normal cluster name",
|
||||
{
|
||||
name: "normal cluster name",
|
||||
args: args{clusterName: "member-cluster-normal"},
|
||||
want: "karmada-es-member-cluster-normal",
|
||||
},
|
||||
|
@ -62,17 +63,20 @@ func TestGetClusterName(t *testing.T) {
|
|||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
{name: "normal execution space name",
|
||||
{
|
||||
name: "normal execution space name",
|
||||
args: args{executionSpaceName: "karmada-es-member-cluster-normal"},
|
||||
want: "member-cluster-normal",
|
||||
wantErr: false,
|
||||
},
|
||||
{name: "invalid member cluster",
|
||||
{
|
||||
name: "invalid member cluster",
|
||||
args: args{executionSpaceName: "invalid"},
|
||||
want: "",
|
||||
wantErr: true,
|
||||
},
|
||||
{name: "empty execution space name",
|
||||
{
|
||||
name: "empty execution space name",
|
||||
args: args{executionSpaceName: ""},
|
||||
want: "",
|
||||
wantErr: true,
|
||||
|
@ -210,6 +214,13 @@ func TestGenerateWorkName(t *testing.T) {
|
|||
namespace: "default",
|
||||
workname: "default-pod-pods",
|
||||
},
|
||||
{
|
||||
testCase: "non nil namespace, colon in name",
|
||||
kind: "Pods",
|
||||
name: "work:pod",
|
||||
namespace: "default",
|
||||
workname: "default-work.pod-pods",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
|
@ -217,6 +228,9 @@ func TestGenerateWorkName(t *testing.T) {
|
|||
|
||||
hash := fnv.New32a()
|
||||
hashutil.DeepHashObject(hash, test.workname)
|
||||
if strings.Contains(test.name, ":") {
|
||||
test.name = strings.ReplaceAll(test.name, ":", ".")
|
||||
}
|
||||
if result := fmt.Sprintf("%s-%s", strings.ToLower(test.name), rand.SafeEncodeString(fmt.Sprint(hash.Sum32()))); result != got {
|
||||
t.Errorf("Test %s failed: expected %v, but got %v", test.testCase, result, got)
|
||||
}
|
||||
|
@ -438,3 +452,30 @@ func TestGeneratePolicyName(t *testing.T) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNamespacedKey(t *testing.T) {
|
||||
tests := []struct {
|
||||
testCase string
|
||||
name string
|
||||
namespace string
|
||||
expectedNamespacedKey string
|
||||
}{
|
||||
{
|
||||
testCase: "empty namespace",
|
||||
name: "pod",
|
||||
namespace: "",
|
||||
expectedNamespacedKey: "pod",
|
||||
}, {
|
||||
testCase: "non nil namespace",
|
||||
name: "pod",
|
||||
namespace: "default",
|
||||
expectedNamespacedKey: "default/pod",
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
gotNamespacedKey := NamespacedKey(test.namespace, test.name)
|
||||
if gotNamespacedKey != test.expectedNamespacedKey {
|
||||
t.Errorf("Test %s failed: expected %v, but got %v", test.testCase, test.expectedNamespacedKey, gotNamespacedKey)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -22,6 +22,14 @@ import (
|
|||
|
||||
func TestAppliedOverrides_AscendOrder(t *testing.T) {
|
||||
applied := AppliedOverrides{}
|
||||
appliedEmptyBytes, er := applied.MarshalJSON()
|
||||
if er != nil {
|
||||
t.Fatalf("not expect error, but got: %v", er)
|
||||
}
|
||||
if appliedEmptyBytes != nil {
|
||||
t.Fatalf("expect nil, but got: %s", string(appliedEmptyBytes))
|
||||
}
|
||||
|
||||
item2 := OverridePolicyShadow{PolicyName: "bbb"}
|
||||
item1 := OverridePolicyShadow{PolicyName: "aaa"}
|
||||
item3 := OverridePolicyShadow{PolicyName: "ccc"}
|
||||
|
|
Loading…
Reference in New Issue