Skip to content

Commit

Permalink
test: add unit test for kubernetesservice
Browse files Browse the repository at this point in the history
Signed-off-by: pengyu <[email protected]>
  • Loading branch information
lobshunter committed Feb 10, 2023
1 parent e7e98f7 commit eeff040
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/digitalocean/go-libvirt v0.0.0-20201209184759-e2a69bcd5bd1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/go-logr/logr v1.2.3 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.
github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1/go.mod h1:KJwIaB5Mv44NWtYuAOFCVOjcI94vtpEz2JU/D2v6IjE=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/evanphx/json-patch v4.2.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/evanphx/json-patch v4.12.0+incompatible h1:4onqiflcdA9EOZ4RxV643DvftH5pOlLGNtQ5lPWQu84=
github.com/evanphx/json-patch v4.12.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/fanliao/go-promise v0.0.0-20141029170127-1890db352a72/go.mod h1:PjfxuH4FZdUyfMdtBio2lsRr1AKEaVPwelzuHuh8Lqc=
github.com/fatih/camelcase v1.0.0/go.mod h1:yN2Sb0lFhZJUdVvtELVWefmrXpuZESvPmqwoZc+/fpc=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
Expand Down
122 changes: 122 additions & 0 deletions pkg/guestagent/kubernetesservice/kubernetesservice_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package kubernetesservice

import (
"context"
"net"
"reflect"
"testing"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/informers"
clientSet "k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/tools/cache"
)

func newFakeKubeClient() (clientSet.Interface, informers.SharedInformerFactory) {
kubeClient := fake.NewSimpleClientset()
informerFactory := informers.NewSharedInformerFactory(kubeClient, 0)
return kubeClient, informerFactory
}

func TestGetPorts(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
serviceCreatedCh := make(chan struct{}, 1)
kubeClient, informerFactory := newFakeKubeClient()
serviceInformer := informerFactory.Core().V1().Services().Informer()
serviceInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) { serviceCreatedCh <- struct{}{} },
})
informerFactory.Start(ctx.Done())
serviceWatcher := NewServiceWatcher()
serviceWatcher.setServiceInformer(serviceInformer)

type testCase struct {
name string
service corev1.Service
want []Entry
}
cases := []testCase{
{
name: "nodePort serivce",
service: corev1.Service{
ObjectMeta: metav1.ObjectMeta{Name: "nodeport"},
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeNodePort,
Ports: []corev1.ServicePort{
{
Name: "http",
Protocol: corev1.ProtocolTCP,
Port: 80,
TargetPort: intstr.FromInt(80),
NodePort: 8080,
},
},
},
},
want: []Entry{{
Protocol: TCP,
IP: net.ParseIP("0.0.0.0"),
Port: 8080,
}},
},
{
name: "loadBalancer service",
service: corev1.Service{
ObjectMeta: metav1.ObjectMeta{Name: "loadbalancer"},
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeLoadBalancer,
Ports: []corev1.ServicePort{
{
Name: "http",
Protocol: corev1.ProtocolTCP,
Port: 8081,
TargetPort: intstr.FromInt(80),
},
},
},
},
want: []Entry{{
Protocol: TCP,
IP: net.ParseIP("0.0.0.0"),
Port: 8081,
}},
},
{
name: "clusterIP service",
service: corev1.Service{
ObjectMeta: metav1.ObjectMeta{Name: "clusterip"},
Spec: corev1.ServiceSpec{
Type: corev1.ServiceTypeClusterIP,
Ports: []corev1.ServicePort{
{
Name: "http",
Protocol: corev1.ProtocolTCP,
Port: 80,
TargetPort: intstr.FromInt(80),
},
},
},
},
want: nil,
},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
_, err := kubeClient.CoreV1().Services("default").Create(ctx, &c.service, metav1.CreateOptions{})
if err != nil {
t.Fatalf("failed to create service: %v", err)
}
<-serviceCreatedCh

got := serviceWatcher.GetPorts()
if !reflect.DeepEqual(got, c.want) {
t.Errorf("got %v, want %v", got, c.want)
}
kubeClient.CoreV1().Services("default").Delete(ctx, c.service.Name, metav1.DeleteOptions{})
})
}
}

0 comments on commit eeff040

Please sign in to comment.