Skip to content

Commit

Permalink
add utils ut
Browse files Browse the repository at this point in the history
Signed-off-by: bobz965 <[email protected]>
  • Loading branch information
bobz965 committed Sep 21, 2024
1 parent 73ce0ec commit 34331de
Show file tree
Hide file tree
Showing 2 changed files with 267 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkg/util/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func ServiceClusterIPs(svc v1.Service) []string {
func LabelSelectorNotEquals(key, value string) (labels.Selector, error) {
requirement, err := labels.NewRequirement(key, selection.NotEquals, []string{value})
if err != nil {
klog.Errorf("failed to create label requirement: %v", err)
return nil, err
}
return labels.Everything().Add(*requirement), nil
Expand All @@ -117,6 +118,9 @@ func LabelSelectorNotEmpty(key string) (labels.Selector, error) {
}

func GetTruncatedUID(uid string) string {
if len(uid) <= 12 {
return uid
}
return uid[len(uid)-12:]
}

Expand Down
263 changes: 263 additions & 0 deletions pkg/util/k8s_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package util

import (
"context"
"errors"
"net"
"net/http"
Expand All @@ -9,8 +10,11 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
clientv1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/klog/v2"
)

Expand Down Expand Up @@ -197,3 +201,262 @@ func TestGetNodeInternalIP(t *testing.T) {
})
}
}

func TestGetPodIPs(t *testing.T) {
tests := []struct {
name string
pod v1.Pod
exp []string
}{
{
name: "pod_with_one_pod_ip",
pod: v1.Pod{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{},
Spec: v1.PodSpec{},
Status: v1.PodStatus{
PodIPs: []v1.PodIP{{IP: "192.168.1.100"}},
PodIP: "192.168.1.100",
},
},
exp: []string{"192.168.1.100"},
},
{
name: "pod_with_one_pod_ip",
pod: v1.Pod{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{},
Spec: v1.PodSpec{},
Status: v1.PodStatus{
PodIPs: []v1.PodIP{{IP: "192.168.1.100"}, {IP: "fd00:10:16::8"}},
PodIP: "192.168.1.100",
},
},
exp: []string{"192.168.1.100", "fd00:10:16::8"},
},

{
name: "pod_with_no_pod_ip",
pod: v1.Pod{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{},
Spec: v1.PodSpec{},
Status: v1.PodStatus{
PodIPs: []v1.PodIP{},
PodIP: "",
},
},
exp: []string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if ret := PodIPs(tt.pod); len(ret) != len(tt.exp) {
t.Errorf("got %v, want %v", ret, tt.exp)
}
})
}

}

Check failure on line 260 in pkg/util/k8s_test.go

View workflow job for this annotation

GitHub Actions / Build kube-ovn

unnecessary trailing newline (whitespace)

func TestServiceClusterIPs(t *testing.T) {
tests := []struct {
name string
svc v1.Service
exp []string
}{
{
name: "service_with_one_cluster_ip",
svc: v1.Service{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{},
Spec: v1.ServiceSpec{
ClusterIP: "10.96.0.1",
ClusterIPs: []string{"10.96.0.1"},
},
},
exp: []string{"10.96.0.1"},
},
{
name: "service_with_two_cluster_ip",
svc: v1.Service{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{},
Spec: v1.ServiceSpec{
ClusterIP: "10.96.0.1",
ClusterIPs: []string{"10.96.0.1", "fd00:10:16::1"},
},
},
exp: []string{"10.96.0.1", "fd00:10:16::1"},
},
{
name: "service_with_no_cluster_ip",
svc: v1.Service{
TypeMeta: metav1.TypeMeta{},
ObjectMeta: metav1.ObjectMeta{},
Spec: v1.ServiceSpec{
ClusterIP: "",
ClusterIPs: []string{},
},
},
exp: []string{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if ret := ServiceClusterIPs(tt.svc); len(ret) != len(tt.exp) {
t.Errorf("got %v, want %v", ret, tt.exp)
}
})
}
}

func TestUpdateNodeLabels(t *testing.T) {
client := fake.NewSimpleClientset()
nodeClient := client.CoreV1().Nodes()
tests := []struct {
name string
cs clientv1.NodeInterface
node string
labels map[string]any
exp error
}{
{
name: "node_with_labels",
cs: nodeClient,
node: "node",
labels: map[string]any{
"key1": "value1",
},
exp: nil,
},
{
name: "node_with_nil_labels",
cs: nodeClient,
node: "node",
labels: map[string]any{},
exp: nil,
},
}
for _, tt := range tests {
// create a node
client.CoreV1().Nodes().Create(context.Background(), &v1.Node{

Check failure on line 343 in pkg/util/k8s_test.go

View workflow job for this annotation

GitHub Actions / Build kube-ovn

Error return value of `(k8s.io/client-go/kubernetes/typed/core/v1.NodeInterface).Create` is not checked (errcheck)
ObjectMeta: metav1.ObjectMeta{
Name: tt.node,
},
}, metav1.CreateOptions{})
t.Run(tt.name, func(t *testing.T) {
err := UpdateNodeLabels(tt.cs, tt.node, tt.labels)
if err != tt.exp {

Check failure on line 350 in pkg/util/k8s_test.go

View workflow job for this annotation

GitHub Actions / Build kube-ovn

comparing with != will fail on wrapped errors. Use errors.Is to check for a specific error (errorlint)
t.Errorf("got %v, want %v", err, tt.exp)
}
})
}
}

func TestUpdateNodeAnnotations(t *testing.T) {
client := fake.NewSimpleClientset()
nodeClient := client.CoreV1().Nodes()
tests := []struct {
name string
cs clientv1.NodeInterface
node string
annotations map[string]any
exp error
}{
{
name: "node_with_annotations",
cs: nodeClient,
node: "node",
annotations: map[string]any{
"key1": "value1",
},
exp: nil,
},
{
name: "node_with_nil_annotations",
cs: nodeClient,
node: "node",
annotations: map[string]any{},
exp: nil,
},
}
for _, tt := range tests {
// create a node
client.CoreV1().Nodes().Create(context.Background(), &v1.Node{

Check failure on line 386 in pkg/util/k8s_test.go

View workflow job for this annotation

GitHub Actions / Build kube-ovn

Error return value of `(k8s.io/client-go/kubernetes/typed/core/v1.NodeInterface).Create` is not checked (errcheck)
ObjectMeta: metav1.ObjectMeta{
Name: tt.node,
},
}, metav1.CreateOptions{})
t.Run(tt.name, func(t *testing.T) {
err := UpdateNodeAnnotations(tt.cs, tt.node, tt.annotations)
if err != tt.exp {

Check failure on line 393 in pkg/util/k8s_test.go

View workflow job for this annotation

GitHub Actions / Build kube-ovn

comparing with != will fail on wrapped errors. Use errors.Is to check for a specific error (errorlint)
t.Errorf("got %v, want %v", err, tt.exp)
}
})
}
}

func TestNodeMergePatch(t *testing.T) {
client := fake.NewSimpleClientset()
nodeClient := client.CoreV1().Nodes()
tests := []struct {
name string
cs clientv1.NodeInterface
node string
patch string
exp error
}{
{
name: "node_with_patch",
cs: nodeClient,
node: "node",
patch: `{"metadata":{"labels":{"key1":"value1"}}}`,
exp: nil,
},
}
for _, tt := range tests {
// create a node
client.CoreV1().Nodes().Create(context.Background(), &v1.Node{

Check failure on line 420 in pkg/util/k8s_test.go

View workflow job for this annotation

GitHub Actions / Build kube-ovn

Error return value of `(k8s.io/client-go/kubernetes/typed/core/v1.NodeInterface).Create` is not checked (errcheck)
ObjectMeta: metav1.ObjectMeta{
Name: tt.node,
},
}, metav1.CreateOptions{})
t.Run(tt.name, func(t *testing.T) {
err := nodeMergePatch(tt.cs, tt.node, tt.patch)
if err != tt.exp {

Check failure on line 427 in pkg/util/k8s_test.go

View workflow job for this annotation

GitHub Actions / Build kube-ovn

comparing with != will fail on wrapped errors. Use errors.Is to check for a specific error (errorlint)
t.Errorf("got %v, want %v", err, tt.exp)
}
})
}
}

func TestLabelSelectorNotEquals(t *testing.T) {
selector, err := LabelSelectorNotEquals("key", "value")
require.NoError(t, err)
require.Equal(t, "key!=value", selector.String())
// Test error case
selector, err = LabelSelectorNotEquals("", "")
require.Error(t, err)
require.Nil(t, selector)
}

func TestLabelSelectorNotEmpty(t *testing.T) {
selector, err := LabelSelectorNotEmpty("key")
require.NoError(t, err)
require.Equal(t, "key!=", selector.String())
// Test error case
selector, err = LabelSelectorNotEmpty("")
require.Error(t, err)
require.Nil(t, selector)
}

func TestGetTruncatedUID(t *testing.T) {
uid := "12345678-1234-1234-1234-123456789012"
require.Equal(t, "123456789012", GetTruncatedUID(uid))
// Test id length less than 12
require.Equal(t, "", GetTruncatedUID(""))
require.Equal(t, "0123456789ab", GetTruncatedUID("0123456789ab"))
require.Equal(t, "0123456789a", GetTruncatedUID("0123456789a"))
require.Equal(t, "0", GetTruncatedUID("0"))
}

0 comments on commit 34331de

Please sign in to comment.