github.com/m-lab/locate@v0.17.6/cmd/heartbeat/health/checker_test.go (about) 1 package health 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/m-lab/locate/cmd/heartbeat/health/healthtest" 8 v1 "k8s.io/api/core/v1" 9 "k8s.io/client-go/kubernetes/fake" 10 ) 11 12 func TestChecker_getHealth(t *testing.T) { 13 tests := []struct { 14 name string 15 checker *Checker 16 endpointStatus int 17 want float64 18 }{ 19 { 20 name: "health-1", 21 checker: NewCheckerK8S( 22 &PortProbe{}, 23 &KubernetesClient{ 24 clientset: healthyClientset, 25 }, 26 &EndpointClient{}, 27 ), 28 endpointStatus: 200, 29 want: 1, 30 }, 31 { 32 name: "health-1-k8s-nil", 33 checker: NewChecker( 34 &PortProbe{}, 35 &EndpointClient{}, 36 ), 37 endpointStatus: 200, 38 want: 1, 39 }, 40 { 41 name: "ports-unhealthy", 42 checker: NewCheckerK8S( 43 &PortProbe{ 44 ports: map[string]bool{"65536": true}, 45 }, 46 &KubernetesClient{ 47 clientset: healthyClientset, 48 }, 49 &EndpointClient{}, 50 ), 51 endpointStatus: 200, 52 want: 0, 53 }, 54 { 55 name: "kubernetes-call-fail", 56 checker: NewCheckerK8S( 57 &PortProbe{}, 58 &KubernetesClient{ 59 clientset: fake.NewSimpleClientset(), 60 }, 61 &EndpointClient{}, 62 ), 63 endpointStatus: 200, 64 want: 1, 65 }, 66 { 67 name: "kubernetes-unhealthy", 68 checker: NewCheckerK8S( 69 &PortProbe{}, 70 &KubernetesClient{ 71 clientset: fake.NewSimpleClientset( 72 &v1.Pod{ 73 Status: v1.PodStatus{ 74 Phase: "Pending", 75 }, 76 }, 77 &v1.Node{ 78 Status: v1.NodeStatus{ 79 Conditions: []v1.NodeCondition{ 80 {Type: "Ready", Status: "False"}, 81 }, 82 }, 83 }, 84 ), 85 }, 86 &EndpointClient{}, 87 ), 88 endpointStatus: 200, 89 want: 0, 90 }, 91 { 92 name: "endpoint-unhealthy", 93 checker: NewCheckerK8S( 94 &PortProbe{}, 95 &KubernetesClient{ 96 clientset: healthyClientset, 97 }, 98 &EndpointClient{}, 99 ), 100 endpointStatus: 500, 101 want: 0, 102 }, 103 { 104 name: "all-unhealthy", 105 checker: NewCheckerK8S( 106 &PortProbe{ 107 ports: map[string]bool{"65536": true}, 108 }, 109 &KubernetesClient{ 110 clientset: fake.NewSimpleClientset( 111 &v1.Pod{ 112 Status: v1.PodStatus{ 113 Phase: "Pending", 114 }, 115 }, 116 &v1.Node{ 117 Status: v1.NodeStatus{ 118 Conditions: []v1.NodeCondition{ 119 {Type: "Ready", Status: "False"}, 120 }, 121 }, 122 }, 123 ), 124 }, 125 &EndpointClient{}, 126 ), 127 want: 0, 128 }, 129 { 130 name: "all-unhealthy-k8s-nil", 131 checker: NewChecker( 132 &PortProbe{ 133 ports: map[string]bool{"65536": true}, 134 }, 135 &EndpointClient{}, 136 ), 137 want: 0, 138 }, 139 } 140 141 for _, tt := range tests { 142 t.Run(tt.name, func(t *testing.T) { 143 if tt.endpointStatus != 0 { 144 srv := healthtest.TestHealthServer(tt.endpointStatus) 145 healthAddress = srv.URL + "/health" 146 defer srv.Close() 147 } 148 149 got := tt.checker.GetHealth(context.Background()) 150 if got != tt.want { 151 t.Errorf("Checker.GetHealth() = %v, want %v", got, tt.want) 152 } 153 }) 154 } 155 }