|
| 1 | +package backend |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "testing" |
| 7 | + |
| 8 | + . "github.com/onsi/gomega" |
| 9 | + corev1 "k8s.io/api/core/v1" |
| 10 | + networkingv1 "k8s.io/api/networking/v1" |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + "k8s.io/apimachinery/pkg/runtime" |
| 13 | + "k8s.io/client-go/informers" |
| 14 | + networkinginformers "k8s.io/client-go/informers/networking/v1" |
| 15 | + fakeclientset "k8s.io/client-go/kubernetes/fake" |
| 16 | + "k8s.io/client-go/kubernetes/typed/core/v1/fake" |
| 17 | + fake2 "k8s.io/client-go/kubernetes/typed/networking/v1/fake" |
| 18 | + corelisters "k8s.io/client-go/listers/core/v1" |
| 19 | + k8stesting "k8s.io/client-go/testing" |
| 20 | + |
| 21 | + "k8s.io/client-go/tools/cache" |
| 22 | +) |
| 23 | + |
| 24 | +func setUp(ctx context.Context, ingressClassList *networkingv1.IngressClassList, ingressList *networkingv1.IngressList, testService *corev1.Service, |
| 25 | + endpoints *corev1.Endpoints, pod *corev1.Pod) (networkinginformers.IngressClassInformer, networkinginformers.IngressInformer, |
| 26 | + corelisters.ServiceLister, corelisters.EndpointsLister, corelisters.PodLister, *fakeclientset.Clientset) { |
| 27 | + client := fakeclientset.NewSimpleClientset() |
| 28 | + client.NetworkingV1().(*fake2.FakeNetworkingV1). |
| 29 | + PrependReactor("list", "ingressclasses", func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) { |
| 30 | + return true, ingressClassList, nil |
| 31 | + }) |
| 32 | + |
| 33 | + client.NetworkingV1().(*fake2.FakeNetworkingV1). |
| 34 | + PrependReactor("list", "ingresses", func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) { |
| 35 | + return true, ingressList, nil |
| 36 | + }) |
| 37 | + |
| 38 | + client.CoreV1().Services("").(*fake.FakeServices).Fake. |
| 39 | + PrependReactor("get", "services", func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) { |
| 40 | + return true, testService, nil |
| 41 | + }) |
| 42 | + |
| 43 | + client.CoreV1().(*fake.FakeCoreV1). |
| 44 | + PrependReactor("get", "endpoints", func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) { |
| 45 | + return true, endpoints, nil |
| 46 | + }) |
| 47 | + |
| 48 | + client.CoreV1().(*fake.FakeCoreV1). |
| 49 | + PrependReactor("get", "pods", func(action k8stesting.Action) (handled bool, ret runtime.Object, err error) { |
| 50 | + return true, pod, nil |
| 51 | + }) |
| 52 | + |
| 53 | + informerFactory := informers.NewSharedInformerFactory(client, 0) |
| 54 | + ingressClassInformer := informerFactory.Networking().V1().IngressClasses() |
| 55 | + ingressClassInformer.Lister() |
| 56 | + |
| 57 | + ingressInformer := informerFactory.Networking().V1().Ingresses() |
| 58 | + ingressInformer.Lister() |
| 59 | + |
| 60 | + serviceInformer := informerFactory.Core().V1().Services() |
| 61 | + serviceLister := serviceInformer.Lister() |
| 62 | + |
| 63 | + endpointInformer := informerFactory.Core().V1().Endpoints() |
| 64 | + endpointLister := endpointInformer.Lister() |
| 65 | + |
| 66 | + podInformer := informerFactory.Core().V1().Pods() |
| 67 | + podLister := podInformer.Lister() |
| 68 | + |
| 69 | + informerFactory.Start(ctx.Done()) |
| 70 | + cache.WaitForCacheSync(ctx.Done(), ingressClassInformer.Informer().HasSynced) |
| 71 | + cache.WaitForCacheSync(ctx.Done(), ingressInformer.Informer().HasSynced) |
| 72 | + cache.WaitForCacheSync(ctx.Done(), serviceInformer.Informer().HasSynced) |
| 73 | + cache.WaitForCacheSync(ctx.Done(), endpointInformer.Informer().HasSynced) |
| 74 | + cache.WaitForCacheSync(ctx.Done(), podInformer.Informer().HasSynced) |
| 75 | + return ingressClassInformer, ingressInformer, serviceLister, endpointLister, podLister, client |
| 76 | +} |
| 77 | + |
| 78 | +func TestBuildPodConditionPatch(t *testing.T) { |
| 79 | + RegisterTestingT(t) |
| 80 | + pod := &corev1.Pod{ |
| 81 | + TypeMeta: metav1.TypeMeta{ |
| 82 | + APIVersion: "v1", |
| 83 | + Kind: "Pod", |
| 84 | + }, |
| 85 | + Spec: corev1.PodSpec{ |
| 86 | + Containers: []corev1.Container{ |
| 87 | + { |
| 88 | + Name: "test", |
| 89 | + Image: "echoserver", |
| 90 | + }, |
| 91 | + }, |
| 92 | + }, |
| 93 | + } |
| 94 | + newCondition := corev1.PodCondition{ |
| 95 | + Type: corev1.ContainersReady, |
| 96 | + Status: corev1.ConditionTrue, |
| 97 | + } |
| 98 | + patch, err := BuildPodConditionPatch(pod, newCondition) |
| 99 | + Expect(err == nil).Should(Equal(true)) |
| 100 | + Expect(bytes.Equal(patch, []byte("{\"status\":{\"conditions\":[{\"lastProbeTime\":null,\"lastTransitionTime\":null,\"status\":\"True\",\"type\":\"ContainersReady\"}]}}"))).Should(Equal(true)) |
| 101 | +} |
0 commit comments