Skip to content

Commit

Permalink
add unittest for generated deepcopy functions
Browse files Browse the repository at this point in the history
Signed-off-by: zhangzujian <[email protected]>
  • Loading branch information
zhangzujian committed Dec 4, 2024
1 parent c6b1d10 commit 337de95
Show file tree
Hide file tree
Showing 4 changed files with 249 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
172 changes: 172 additions & 0 deletions pkg/apis/kubeovn/v1/condition_test.go
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)
})
}
}
74 changes: 74 additions & 0 deletions pkg/apis/kubeovn/v1/zz_generated.deepcopy_test.go
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)

Check failure on line 26 in pkg/apis/kubeovn/v1/zz_generated.deepcopy_test.go

View workflow job for this annotation

GitHub Actions / Build kube-ovn

Error return value of `gofakeit.Struct` is not checked (errcheck)
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{})
}

0 comments on commit 337de95

Please sign in to comment.