-
Notifications
You must be signed in to change notification settings - Fork 451
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add unittest for generated deepcopy functions
Signed-off-by: zhangzujian <[email protected]>
- Loading branch information
1 parent
c6b1d10
commit 337de95
Showing
4 changed files
with
249 additions
and
0 deletions.
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
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,172 @@ | ||
package v1 | ||
|
||
import ( | ||
"testing" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSetCondition(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
conditions Conditions | ||
ctype ConditionType | ||
status corev1.ConditionStatus | ||
reason string | ||
message string | ||
generation int64 | ||
expctedLen int | ||
}{ | ||
{ | ||
name: "add to nil conditions", | ||
conditions: nil, | ||
ctype: "Foo", | ||
status: corev1.ConditionTrue, | ||
reason: "insert", | ||
message: "foo", | ||
generation: 1, | ||
expctedLen: 1, | ||
}, | ||
{ | ||
name: "insert a new condition", | ||
conditions: Conditions{{Type: "Foo", Status: corev1.ConditionTrue}}, | ||
ctype: "Bar", | ||
status: corev1.ConditionTrue, | ||
reason: "insert", | ||
message: "bar", | ||
generation: 2, | ||
expctedLen: 2, | ||
}, | ||
{ | ||
name: "update an existing condition", | ||
conditions: Conditions{{Type: "Foo", Status: corev1.ConditionTrue, ObservedGeneration: 1}}, | ||
ctype: "Foo", | ||
status: corev1.ConditionFalse, | ||
reason: "update", | ||
message: "bar", | ||
generation: 2, | ||
expctedLen: 1, | ||
}, | ||
{ | ||
name: "no op", | ||
conditions: Conditions{{Type: "Foo", Status: corev1.ConditionTrue, Reason: "noop", Message: "foo", ObservedGeneration: 1}}, | ||
ctype: "Foo", | ||
status: corev1.ConditionTrue, | ||
reason: "noop", | ||
message: "foo", | ||
generation: 1, | ||
expctedLen: 1, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tt.conditions.SetCondition(tt.ctype, tt.status, tt.reason, tt.message, 1) | ||
require.Len(t, tt.conditions, tt.expctedLen) | ||
}) | ||
} | ||
} | ||
|
||
func TestRemoveCondition(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
conditions Conditions | ||
ctype ConditionType | ||
expctedLen int | ||
}{ | ||
{ | ||
name: "remove from a nil conditions", | ||
conditions: nil, | ||
ctype: "Foo", | ||
expctedLen: 0, | ||
}, | ||
{ | ||
name: "remove from an empty conditions", | ||
conditions: Conditions{}, | ||
ctype: "Foo", | ||
expctedLen: 0, | ||
}, | ||
{ | ||
name: "remove an existing condition", | ||
conditions: Conditions{{ | ||
Type: "Foo", Status: corev1.ConditionTrue, ObservedGeneration: 1, | ||
}, { | ||
Type: "Bar", Status: corev1.ConditionFalse, ObservedGeneration: 2, | ||
}}, | ||
ctype: "Foo", | ||
expctedLen: 1, | ||
}, | ||
{ | ||
name: "remove the only condition", | ||
conditions: Conditions{{Type: "Foo", Status: corev1.ConditionTrue, ObservedGeneration: 1}}, | ||
ctype: "Foo", | ||
expctedLen: 0, | ||
}, | ||
{ | ||
name: "remove a non-existent condition", | ||
conditions: Conditions{{Type: "Foo", Status: corev1.ConditionTrue, ObservedGeneration: 1}}, | ||
ctype: "Bar", | ||
expctedLen: 1, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
tt.conditions.RemoveCondition(tt.ctype) | ||
require.Len(t, tt.conditions, tt.expctedLen) | ||
}) | ||
} | ||
} | ||
|
||
func TestGetCondition(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
conditions Conditions | ||
ctype ConditionType | ||
expcted *Condition | ||
}{ | ||
{ | ||
name: "get from a nil conditions", | ||
conditions: nil, | ||
ctype: "Foo", | ||
expcted: nil, | ||
}, | ||
{ | ||
name: "get from an empty conditions", | ||
conditions: Conditions{}, | ||
ctype: "Foo", | ||
expcted: nil, | ||
}, | ||
{ | ||
name: "get an existing condition", | ||
conditions: Conditions{{ | ||
Type: "Foo", Status: corev1.ConditionTrue, ObservedGeneration: 1, | ||
}, { | ||
Type: "Bar", Status: corev1.ConditionFalse, ObservedGeneration: 2, | ||
}}, | ||
ctype: "Foo", | ||
expcted: &Condition{Type: "Foo", Status: corev1.ConditionTrue, ObservedGeneration: 1}, | ||
}, | ||
{ | ||
name: "get the only condition", | ||
conditions: Conditions{{Type: "Foo", Status: corev1.ConditionTrue, ObservedGeneration: 1}}, | ||
ctype: "Foo", | ||
expcted: &Condition{Type: "Foo", Status: corev1.ConditionTrue, ObservedGeneration: 1}, | ||
}, | ||
{ | ||
name: "get a non-existent condition", | ||
conditions: Conditions{{Type: "Foo", Status: corev1.ConditionTrue, ObservedGeneration: 1}}, | ||
ctype: "Bar", | ||
expcted: nil, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
c := tt.conditions.GetCondition(tt.ctype) | ||
require.Equal(t, tt.expcted, c) | ||
}) | ||
} | ||
} |
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,74 @@ | ||
package v1 | ||
|
||
import ( | ||
"testing" | ||
|
||
runtime "k8s.io/apimachinery/pkg/runtime" | ||
|
||
"github.com/brianvoe/gofakeit/v7" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type DeepCopy[T any] interface { | ||
DeepCopy() T | ||
} | ||
|
||
type DeepCopyObject[T runtime.Object] interface { | ||
DeepCopy[T] | ||
DeepCopyObject() runtime.Object | ||
} | ||
|
||
func deepCopyTestHelper[T any](t *testing.T, in DeepCopy[T]) { | ||
require.Equal(t, in, in.DeepCopy()) | ||
} | ||
|
||
func deepCopyObjectTestHelper[T runtime.Object](t *testing.T, in DeepCopyObject[T]) { | ||
gofakeit.Struct(in) | ||
deepCopyTestHelper(t, in) | ||
require.Equal(t, in.(runtime.Object), in.DeepCopyObject()) | ||
} | ||
|
||
func TestDeepCopyObject(t *testing.T) { | ||
deepCopyObjectTestHelper(t, &IP{}) | ||
deepCopyObjectTestHelper(t, &IPList{}) | ||
deepCopyObjectTestHelper(t, &IPPool{}) | ||
deepCopyObjectTestHelper(t, &IPPoolList{}) | ||
deepCopyObjectTestHelper(t, &IptablesDnatRule{}) | ||
deepCopyObjectTestHelper(t, &IptablesDnatRuleList{}) | ||
deepCopyObjectTestHelper(t, &IptablesEIP{}) | ||
deepCopyObjectTestHelper(t, &IptablesEIPList{}) | ||
deepCopyObjectTestHelper(t, &IptablesFIPRule{}) | ||
deepCopyObjectTestHelper(t, &IptablesFIPRuleList{}) | ||
deepCopyObjectTestHelper(t, &IptablesSnatRule{}) | ||
deepCopyObjectTestHelper(t, &IptablesSnatRuleList{}) | ||
deepCopyObjectTestHelper(t, &OvnDnatRule{}) | ||
deepCopyObjectTestHelper(t, &OvnDnatRuleList{}) | ||
deepCopyObjectTestHelper(t, &OvnEip{}) | ||
deepCopyObjectTestHelper(t, &OvnEipList{}) | ||
deepCopyObjectTestHelper(t, &OvnFip{}) | ||
deepCopyObjectTestHelper(t, &OvnFipList{}) | ||
deepCopyObjectTestHelper(t, &OvnSnatRule{}) | ||
deepCopyObjectTestHelper(t, &OvnSnatRuleList{}) | ||
deepCopyObjectTestHelper(t, &ProviderNetwork{}) | ||
deepCopyObjectTestHelper(t, &ProviderNetworkList{}) | ||
deepCopyObjectTestHelper(t, &QoSPolicy{}) | ||
deepCopyObjectTestHelper(t, &QoSPolicyList{}) | ||
deepCopyObjectTestHelper(t, &SecurityGroup{}) | ||
deepCopyObjectTestHelper(t, &SecurityGroupList{}) | ||
deepCopyObjectTestHelper(t, &Subnet{}) | ||
deepCopyObjectTestHelper(t, &SubnetList{}) | ||
deepCopyObjectTestHelper(t, &SwitchLBRule{}) | ||
deepCopyObjectTestHelper(t, &SwitchLBRuleList{}) | ||
deepCopyObjectTestHelper(t, &Vip{}) | ||
deepCopyObjectTestHelper(t, &VipList{}) | ||
deepCopyObjectTestHelper(t, &Vlan{}) | ||
deepCopyObjectTestHelper(t, &VlanList{}) | ||
deepCopyObjectTestHelper(t, &Vpc{}) | ||
deepCopyObjectTestHelper(t, &VpcList{}) | ||
deepCopyObjectTestHelper(t, &VpcDns{}) | ||
deepCopyObjectTestHelper(t, &VpcDnsList{}) | ||
deepCopyObjectTestHelper(t, &VpcEgressGateway{}) | ||
deepCopyObjectTestHelper(t, &VpcEgressGatewayList{}) | ||
deepCopyObjectTestHelper(t, &VpcNatGateway{}) | ||
deepCopyObjectTestHelper(t, &VpcNatGatewayList{}) | ||
} |