1 package k8s
2
3 import (
4 "context"
5 "errors"
6 "fmt"
7 "testing"
8
9 corev1 "k8s.io/api/core/v1"
10
11 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
12 )
13
14 func TestNewContainerMetricsForward(t *testing.T) {
15
16 tests := []struct {
17 ns string
18 name string
19 k8sConfigs []string
20 err error
21 }{
22 {
23 "pod-ns",
24 "pod-name",
25 []string{`apiVersion: v1
26 kind: Pod
27 metadata:
28 name: pod-name
29 namespace: pod-ns
30 status:
31 phase: Running
32 spec:
33 containers:
34 - name: linkerd-proxy
35 ports:
36 - name: bad-port
37 port: 123`,
38 },
39 errors.New("no linkerd-admin port found for container pod-name/linkerd-proxy"),
40 },
41 }
42
43 for i, test := range tests {
44 test := test
45 t.Run(fmt.Sprintf("%d: NewContainerMetricsForward returns expected result", i), func(t *testing.T) {
46 k8sClient, err := NewFakeAPI(test.k8sConfigs...)
47 if err != nil {
48 t.Fatalf("Unexpected error %s", err)
49 }
50 pod, err := k8sClient.CoreV1().Pods(test.ns).Get(context.Background(), test.name, metav1.GetOptions{})
51 if err != nil {
52 t.Fatalf("Unexpected error %s", err)
53 }
54 var container corev1.Container
55 for _, c := range pod.Spec.Containers {
56 container = c
57 break
58 }
59 _, err = NewContainerMetricsForward(&KubernetesAPI{Interface: k8sClient}, *pod, container, false, ProxyAdminPortName)
60 if err != nil || test.err != nil {
61 if (err == nil && test.err != nil) ||
62 (err != nil && test.err == nil) ||
63 (err.Error() != test.err.Error()) {
64 t.Fatalf("Unexpected error (Expected: %s, Got: %s)", test.err, err)
65 }
66 }
67 })
68 }
69 }
70
71 func TestNewPortForward(t *testing.T) {
72 tests := []struct {
73 description string
74 ns string
75 deployName string
76 k8sConfigs []string
77 err error
78 }{
79 {
80 "Pod is owned by the specified deployment",
81 "ns",
82 "deploy",
83 []string{`apiVersion: v1
84 kind: Pod
85 metadata:
86 name: pod
87 namespace: ns
88 uid: pod
89 labels:
90 app: foo
91 ownerReferences:
92 - apiVersion: apps/v1
93 controller: true
94 kind: Deployment
95 name: rs
96 uid: rs
97 status:
98 phase: Running`,
99 `apiVersion: apps/v1
100 kind: ReplicaSet
101 metadata:
102 name: rs
103 namespace: ns
104 uid: rs
105 labels:
106 app: foo
107 ownerReferences:
108 - apiVersion: apps/v1
109 controller: true
110 kind: Deployment
111 name: deploy
112 uid: deploy
113 spec:
114 selector:
115 matchLabels:
116 app: foo
117 `,
118 `apiVersion: apps/v1
119 kind: Deployment
120 metadata:
121 name: deploy
122 namespace: ns
123 uid: deploy
124 spec:
125 selector:
126 matchLabels:
127 app: foo
128 `},
129 nil,
130 },
131
132
133
134 {
135 "Pod's labels match, but is not owned by the deployment",
136 "ns",
137 "deploy",
138 []string{`apiVersion: v1
139 kind: Pod
140 metadata:
141 name: pod
142 namespace: ns
143 uid: pod
144 labels:
145 app: foo
146 ownerReferences:
147 - apiVersion: apps/v1
148 controller: true
149 kind: ReplicaSet
150 name: rs
151 uid: SOME-OTHER-UID
152 status:
153 phase: Running`,
154 `apiVersion: apps/v1
155 kind: ReplicaSet
156 metadata:
157 name: rs
158 namespace: ns
159 uid: rs
160 labels:
161 app: foo
162 ownerReferences:
163 - apiVersion: apps/v1
164 controller: true
165 kind: Deployment
166 name: deploy
167 uid: deploy
168 spec:
169 selector:
170 matchLabels:
171 app: foo
172 `,
173 `apiVersion: apps/v1
174 kind: Deployment
175 metadata:
176 name: deploy
177 namespace: ns
178 uid: deploy
179 spec:
180 selector:
181 matchLabels:
182 app: foo
183 `},
184 errors.New("no running pods found for deploy"),
185 },
186 {
187 "Pod is owned by the specified deployment but is not running",
188 "ns",
189 "deploy",
190 []string{`apiVersion: v1
191 kind: Pod
192 metadata:
193 name: pod
194 namespace: ns
195 uid: pod
196 labels:
197 app: foo
198 ownerReferences:
199 - apiVersion: apps/v1
200 controller: true
201 kind: ReplicaSet
202 name: rs
203 uid: rs
204 status:
205 phase: Stopped`,
206 `apiVersion: apps/v1
207 kind: ReplicaSet
208 metadata:
209 name: rs
210 namespace: ns
211 uid: rs
212 labels:
213 app: foo
214 ownerReferences:
215 - apiVersion: apps/v1
216 controller: true
217 kind: Deployment
218 name: deploy
219 uid: deploy
220 spec:
221 selector:
222 matchLabels:
223 app: foo
224 `,
225 `apiVersion: apps/v1
226 kind: Deployment
227 metadata:
228 name: deploy
229 namespace: ns
230 uid: deploy
231 spec:
232 selector:
233 matchLabels:
234 app: foo
235 `},
236 errors.New("no running pods found for deploy"),
237 },
238 }
239
240 for _, test := range tests {
241 test := test
242 t.Run(test.description, func(t *testing.T) {
243 k8sClient, err := NewFakeAPI(test.k8sConfigs...)
244 if err != nil {
245 t.Fatalf("Unexpected error %s", err)
246 }
247 _, err = NewPortForward(context.Background(), &KubernetesAPI{Interface: k8sClient}, test.ns, test.deployName, "localhost", 0, 0, false)
248 if err != nil || test.err != nil {
249 if (err == nil && test.err != nil) ||
250 (err != nil && test.err == nil) ||
251 (err.Error() != test.err.Error()) {
252 t.Fatalf("Unexpected error (Expected: %s, Got: %s)", test.err, err)
253 }
254 }
255 })
256 }
257 }
258
View as plain text