Skip to content

Commit

Permalink
add unittest
Browse files Browse the repository at this point in the history
Signed-off-by: zhangzujian <[email protected]>
  • Loading branch information
zhangzujian committed Dec 18, 2024
1 parent e9cc33f commit f4345a8
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions pkg/daemon/gateway_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package daemon

import (
"testing"

kubeovnv1 "github.com/kubeovn/kube-ovn/pkg/apis/kubeovn/v1"
"github.com/stretchr/testify/require"
)

func TestGetCidrByProtocol(t *testing.T) {
cases := []struct {
name string
cidr string
protocol string
wantErr bool
expetced string
}{{
name: "ipv4 only",
cidr: "1.1.1.0/24",
protocol: kubeovnv1.ProtocolIPv4,
expetced: "1.1.1.0/24",
}, {
name: "ipv6 only",
cidr: "2001:db8::/120",
protocol: kubeovnv1.ProtocolIPv6,
expetced: "2001:db8::/120",
}, {
name: "get ipv4 from ipv6",
cidr: "2001:db8::/120",
protocol: kubeovnv1.ProtocolIPv4,
}, {
name: "get ipv4 from dual stack",
cidr: "1.1.1.0/24,2001:db8::/120",
protocol: kubeovnv1.ProtocolIPv4,
expetced: "1.1.1.0/24",
}, {
name: "get ipv6 from ipv4",
cidr: "1.1.1.0/24",
protocol: kubeovnv1.ProtocolIPv6,
}, {
name: "get ipv6 from dual stack",
cidr: "1.1.1.0/24,2001:db8::/120",
protocol: kubeovnv1.ProtocolIPv6,
expetced: "2001:db8::/120",
}, {
name: "invalid cidr",
cidr: "foo bar",
protocol: kubeovnv1.ProtocolIPv4,
wantErr: true,
}}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
got, err := getCidrByProtocol(c.cidr, c.protocol)
if (err != nil) != c.wantErr {
t.Errorf("getCidrByProtocol(%q, %q) error = %v, wantErr = %v", c.cidr, c.protocol, err, c.wantErr)
}
require.Equal(t, c.expetced, got)
})
}
}

0 comments on commit f4345a8

Please sign in to comment.