From 337de950189bc1cfcedc4f6269520b5ab551a09f Mon Sep 17 00:00:00 2001 From: zhangzujian Date: Wed, 4 Dec 2024 10:08:49 +0000 Subject: [PATCH] add unittest for generated deepcopy functions Signed-off-by: zhangzujian --- go.mod | 1 + go.sum | 2 + pkg/apis/kubeovn/v1/condition_test.go | 172 ++++++++++++++++++ .../kubeovn/v1/zz_generated.deepcopy_test.go | 74 ++++++++ 4 files changed, 249 insertions(+) create mode 100644 pkg/apis/kubeovn/v1/condition_test.go create mode 100644 pkg/apis/kubeovn/v1/zz_generated.deepcopy_test.go diff --git a/go.mod b/go.mod index 85fe214a648..83d28a2ac77 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/Microsoft/go-winio v0.6.2 github.com/Microsoft/hcsshim v0.12.7 github.com/bhendo/go-powershell v0.0.0-20190719160123-219e7fb4e41e + github.com/brianvoe/gofakeit/v7 v7.0.0 github.com/cenkalti/backoff/v4 v4.3.0 github.com/cnf/structhash v0.0.0-20201127153200-e1b16c1ebc08 github.com/containerd/containerd v1.7.22 diff --git a/go.sum b/go.sum index 98d7a60843a..1bb93218eeb 100644 --- a/go.sum +++ b/go.sum @@ -37,6 +37,8 @@ github.com/bhendo/go-powershell v0.0.0-20190719160123-219e7fb4e41e h1:KCjb01YiNo github.com/bhendo/go-powershell v0.0.0-20190719160123-219e7fb4e41e/go.mod h1:f7vw6ObmmNcyFQLhZX9eUGBJGpnwTJFDvVjqZxIxHWY= github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM= github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ= +github.com/brianvoe/gofakeit/v7 v7.0.0 h1:y2MKKQ5qnErs2DaGg/O9MfKN0nEOaLf69lSF6ztfnCI= +github.com/brianvoe/gofakeit/v7 v7.0.0/go.mod h1:QXuPeBw164PJCzCUZVmgpgHJ3Llj49jSLVkKPMtxtxA= github.com/bufbuild/protocompile v0.4.0 h1:LbFKd2XowZvQ/kajzguUp2DC9UEIQhIq77fZZlaQsNA= github.com/bufbuild/protocompile v0.4.0/go.mod h1:3v93+mbWn/v3xzN+31nwkJfrEpAUwp+BagBSZWx+TP8= github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs= diff --git a/pkg/apis/kubeovn/v1/condition_test.go b/pkg/apis/kubeovn/v1/condition_test.go new file mode 100644 index 00000000000..488705001e2 --- /dev/null +++ b/pkg/apis/kubeovn/v1/condition_test.go @@ -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) + }) + } +} diff --git a/pkg/apis/kubeovn/v1/zz_generated.deepcopy_test.go b/pkg/apis/kubeovn/v1/zz_generated.deepcopy_test.go new file mode 100644 index 00000000000..2623adeb126 --- /dev/null +++ b/pkg/apis/kubeovn/v1/zz_generated.deepcopy_test.go @@ -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{}) +}