-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
2,565 additions
and
35 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
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) | ||
} |
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,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) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.