Skip to content

Commit

Permalink
fix: add unitest (#2030)
Browse files Browse the repository at this point in the history
  • Loading branch information
lut777 authored Nov 15, 2022
1 parent 6861105 commit 5f40c22
Show file tree
Hide file tree
Showing 9 changed files with 2,565 additions and 35 deletions.
61 changes: 61 additions & 0 deletions pkg/util/address_family_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package util

import (
kubeovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1"
"golang.org/x/sys/unix"
"strings"
"testing"
)

func TestProtocolToFamily(t *testing.T) {
tests := []struct {
name string
prot string
want int
err string
}{
{
name: "correct",
prot: kubeovnv1.ProtocolIPv4,
want: unix.AF_INET,
err: "",
},
{
name: "v6",
prot: kubeovnv1.ProtocolIPv6,
want: unix.AF_INET6,
err: "",
},
{
name: "dual",
prot: kubeovnv1.ProtocolDual,
want: unix.AF_UNSPEC,
err: "",
},
{
name: "err",
prot: "damn",
want: -1,
err: "invalid protocol",
},
}
for _, c := range tests {
t.Run(c.name, func(t *testing.T) {
ans, err := ProtocolToFamily(c.prot)
if ans != c.want || !ErrorContains(err, c.err) {
t.Errorf("%v expected %v, %v, but %v, %v got",
c.prot, c.want, c.err, ans, err)
}
})
}
}

func ErrorContains(out error, want string) bool {
if out == nil {
return want == ""
}
if want == "" {
return false
}
return strings.Contains(out.Error(), want)
}
76 changes: 76 additions & 0 deletions pkg/util/k8s_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package util

import (
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"testing"
)

func TestGetNodeInternalIP(t *testing.T) {
tests := []struct {
name string
node v1.Node
exp4 string
exp6 string
}{
{
name: "correct",
node: v1.Node{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{},
Spec: v1.NodeSpec{},
Status: v1.NodeStatus{
Addresses: []v1.NodeAddress{
{
Type: "InternalIP",
Address: "192.168.0.2",
},
{
Type: "ExternalIP",
Address: "192.188.0.4",
},
{
Type: "InternalIP",
Address: "ffff:ffff:ffff:ffff:ffff::23",
},
},
},
},
exp4: "192.168.0.2",
exp6: "ffff:ffff:ffff:ffff:ffff::23",
},
{
name: "correctWithDiff",
node: v1.Node{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{},
Spec: v1.NodeSpec{},
Status: v1.NodeStatus{
Addresses: []v1.NodeAddress{
{
Type: "InternalIP",
Address: "ffff:ffff:ffff:ffff:ffff::23",
},
{
Type: "ExternalIP",
Address: "192.188.0.4",
},
{
Type: "InternalIP",
Address: "192.188.0.43",
},
},
},
},
exp4: "192.188.0.43",
exp6: "ffff:ffff:ffff:ffff:ffff::23",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if ret4, ret6 := GetNodeInternalIP(tt.node); ret4 != tt.exp4 || ret6 != tt.exp6 {
t.Errorf("got %v, %v, want %v, %v", ret4, ret6, tt.exp4, tt.exp6)
}
})
}
}
Loading

0 comments on commit 5f40c22

Please sign in to comment.