|
| 1 | +package controller |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "k8s.io/api/core/v1" |
| 7 | + extensions "k8s.io/api/extensions/v1beta1" |
| 8 | + meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 9 | + "k8s.io/apimachinery/pkg/fields" |
| 10 | + "k8s.io/client-go/kubernetes/fake" |
| 11 | + "k8s.io/client-go/tools/cache" |
| 12 | +) |
| 13 | + |
| 14 | +func TestStatusUpdate(t *testing.T) { |
| 15 | + ing := extensions.Ingress{ |
| 16 | + ObjectMeta: meta_v1.ObjectMeta{ |
| 17 | + Name: "ing-1", |
| 18 | + Namespace: "namespace", |
| 19 | + }, |
| 20 | + Status: extensions.IngressStatus{ |
| 21 | + LoadBalancer: v1.LoadBalancerStatus{ |
| 22 | + Ingress: []v1.LoadBalancerIngress{ |
| 23 | + { |
| 24 | + IP: "1.2.3.4", |
| 25 | + }, |
| 26 | + }, |
| 27 | + }, |
| 28 | + }, |
| 29 | + } |
| 30 | + fakeClient := fake.NewSimpleClientset( |
| 31 | + &extensions.IngressList{Items: []extensions.Ingress{ |
| 32 | + ing, |
| 33 | + }}, |
| 34 | + ) |
| 35 | + ingLister := StoreToIngressLister{} |
| 36 | + ingLister.Store, _ = cache.NewInformer( |
| 37 | + cache.NewListWatchFromClient(fakeClient.Extensions().RESTClient(), "ingresses", "nginx-ingress", fields.Everything()), |
| 38 | + &extensions.Ingress{}, 2, nil) |
| 39 | + |
| 40 | + ingLister.Store.Add(&ing) |
| 41 | + |
| 42 | + su := StatusUpdater{ |
| 43 | + client: fakeClient, |
| 44 | + namespace: "namespace", |
| 45 | + externalServiceName: "service-name", |
| 46 | + externalStatusAddress: "123.123.123.123", |
| 47 | + ingLister: &ingLister, |
| 48 | + keyFunc: cache.DeletionHandlingMetaNamespaceKeyFunc, |
| 49 | + } |
| 50 | + err := su.ClearIngressStatus(ing) |
| 51 | + if err != nil { |
| 52 | + t.Errorf("error clearing ing status: %v", err) |
| 53 | + } |
| 54 | + ings, _ := fakeClient.ExtensionsV1beta1().Ingresses("namespace").List(meta_v1.ListOptions{}) |
| 55 | + ingf := ings.Items[0] |
| 56 | + if !checkStatus("", ingf) { |
| 57 | + t.Errorf("expected: %v actual: %v", "", ingf.Status.LoadBalancer.Ingress[0]) |
| 58 | + } |
| 59 | + |
| 60 | + su.SaveStatusFromExternalStatus("1.1.1.1") |
| 61 | + err = su.UpdateIngressStatus(ing) |
| 62 | + if err != nil { |
| 63 | + t.Errorf("error updating ing status: %v", err) |
| 64 | + } |
| 65 | + ring, _ := fakeClient.ExtensionsV1beta1().Ingresses(ing.Namespace).Get(ing.Name, meta_v1.GetOptions{}) |
| 66 | + if !checkStatus("1.1.1.1", *ring) { |
| 67 | + t.Errorf("expected: %v actual: %v", "", ring.Status.LoadBalancer.Ingress) |
| 68 | + } |
| 69 | + |
| 70 | + svc := v1.Service{ |
| 71 | + ObjectMeta: meta_v1.ObjectMeta{ |
| 72 | + Namespace: "namespace", |
| 73 | + Name: "service-name", |
| 74 | + }, |
| 75 | + Status: v1.ServiceStatus{ |
| 76 | + LoadBalancer: v1.LoadBalancerStatus{ |
| 77 | + Ingress: []v1.LoadBalancerIngress{v1.LoadBalancerIngress{ |
| 78 | + IP: "2.2.2.2", |
| 79 | + }}, |
| 80 | + }, |
| 81 | + }, |
| 82 | + } |
| 83 | + su.SaveStatusFromExternalService(&svc) |
| 84 | + err = su.UpdateIngressStatus(ing) |
| 85 | + if err != nil { |
| 86 | + t.Errorf("error updating ing status: %v", err) |
| 87 | + } |
| 88 | + ring, _ = fakeClient.ExtensionsV1beta1().Ingresses(ing.Namespace).Get(ing.Name, meta_v1.GetOptions{}) |
| 89 | + if !checkStatus("1.1.1.1", *ring) { |
| 90 | + t.Errorf("expected: %v actual: %v", "1.1.1.1", ring.Status.LoadBalancer.Ingress) |
| 91 | + } |
| 92 | + |
| 93 | + su.SaveStatusFromExternalStatus("") |
| 94 | + err = su.UpdateIngressStatus(ing) |
| 95 | + if err != nil { |
| 96 | + t.Errorf("error updating ing status: %v", err) |
| 97 | + } |
| 98 | + ring, _ = fakeClient.ExtensionsV1beta1().Ingresses(ing.Namespace).Get(ing.Name, meta_v1.GetOptions{}) |
| 99 | + if !checkStatus("2.2.2.2", *ring) { |
| 100 | + t.Errorf("expected: %v actual: %v", "2.2.2.2", ring.Status.LoadBalancer.Ingress) |
| 101 | + } |
| 102 | + |
| 103 | + su.ClearStatusFromExternalService() |
| 104 | + err = su.UpdateIngressStatus(ing) |
| 105 | + if err != nil { |
| 106 | + t.Errorf("error updating ing status: %v", err) |
| 107 | + } |
| 108 | + ring, _ = fakeClient.ExtensionsV1beta1().Ingresses(ing.Namespace).Get(ing.Name, meta_v1.GetOptions{}) |
| 109 | + if !checkStatus("", *ring) { |
| 110 | + t.Errorf("expected: %v actual: %v", "", ring.Status.LoadBalancer.Ingress) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +func checkStatus(expected string, actual extensions.Ingress) bool { |
| 115 | + if len(actual.Status.LoadBalancer.Ingress) == 0 { |
| 116 | + if expected == "" { |
| 117 | + return true |
| 118 | + } |
| 119 | + return false |
| 120 | + } |
| 121 | + return expected == actual.Status.LoadBalancer.Ingress[0].IP |
| 122 | +} |
0 commit comments