@@ -17,12 +17,18 @@ limitations under the License.
17
17
package proxy
18
18
19
19
import (
20
+ "bytes"
21
+ "context"
20
22
"os"
21
23
"strings"
22
24
"testing"
23
25
26
+ "github.com/lithammer/dedent"
27
+
24
28
apps "k8s.io/api/apps/v1"
29
+ v1 "k8s.io/api/core/v1"
25
30
apierrors "k8s.io/apimachinery/pkg/api/errors"
31
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26
32
"k8s.io/apimachinery/pkg/runtime"
27
33
clientsetfake "k8s.io/client-go/kubernetes/fake"
28
34
clientsetscheme "k8s.io/client-go/kubernetes/scheme"
@@ -192,3 +198,119 @@ func TestDaemonSetsHaveSystemNodeCriticalPriorityClassName(t *testing.T) {
192
198
})
193
199
}
194
200
}
201
+
202
+ func TestPrintOrCreateKubeProxyObjects (t * testing.T ) {
203
+ tests := []struct {
204
+ name string
205
+ printManifest bool
206
+ wantOut string
207
+ wantErr bool
208
+ }{
209
+ {
210
+ name : "do not print manifest" ,
211
+ printManifest : false ,
212
+ wantOut : "[addons] Applied essential addon: kube-proxy\n " ,
213
+ wantErr : false ,
214
+ },
215
+ {
216
+ name : "print manifest" ,
217
+ printManifest : true ,
218
+ wantOut : dedent .Dedent (`---
219
+ apiVersion: v1
220
+ kind: ServiceAccount
221
+ metadata:
222
+ creationTimestamp: null
223
+ name: kube-proxy
224
+ namespace: kube-system
225
+ ---
226
+ apiVersion: rbac.authorization.k8s.io/v1
227
+ kind: ClusterRoleBinding
228
+ metadata:
229
+ creationTimestamp: null
230
+ name: kubeadm:node-proxier
231
+ roleRef:
232
+ apiGroup: rbac.authorization.k8s.io
233
+ kind: ClusterRole
234
+ name: system:node-proxier
235
+ subjects:
236
+ - kind: ServiceAccount
237
+ name: kube-proxy
238
+ namespace: kube-system
239
+ ---
240
+ apiVersion: rbac.authorization.k8s.io/v1
241
+ kind: Role
242
+ metadata:
243
+ creationTimestamp: null
244
+ name: kube-proxy
245
+ namespace: kube-system
246
+ rules:
247
+ - apiGroups:
248
+ - ""
249
+ resourceNames:
250
+ - kube-proxy
251
+ resources:
252
+ - configmaps
253
+ verbs:
254
+ - get
255
+ ---
256
+ apiVersion: rbac.authorization.k8s.io/v1
257
+ kind: RoleBinding
258
+ metadata:
259
+ creationTimestamp: null
260
+ name: kube-proxy
261
+ namespace: kube-system
262
+ roleRef:
263
+ apiGroup: rbac.authorization.k8s.io
264
+ kind: Role
265
+ name: kube-proxy
266
+ subjects:
267
+ - kind: Group
268
+ name: system:bootstrappers:kubeadm:default-node-token
269
+ ---
270
+ foo
271
+ ---
272
+ bar
273
+ ` ),
274
+ wantErr : false ,
275
+ },
276
+ }
277
+ for _ , tt := range tests {
278
+ t .Run (tt .name , func (t * testing.T ) {
279
+ out := & bytes.Buffer {}
280
+ client := newMockClientForTest (t )
281
+ cmByte := []byte {'\n' , 'f' , 'o' , 'o' , '\n' }
282
+ dsByte := []byte {'\n' , 'b' , 'a' , 'r' , '\n' }
283
+ if err := printOrCreateKubeProxyObjects (cmByte , dsByte , client , out , tt .printManifest ); (err != nil ) != tt .wantErr {
284
+ t .Fatalf ("printOrCreateKubeProxyObjects() error = %v, wantErr %v" , err , tt .wantErr )
285
+ }
286
+ if gotOut := out .String (); gotOut != tt .wantOut {
287
+ t .Fatalf ("printOrCreateKubeProxyObjects() = %v, want %v" , gotOut , tt .wantOut )
288
+ }
289
+ })
290
+ }
291
+ }
292
+
293
+ func newMockClientForTest (t * testing.T ) * clientsetfake.Clientset {
294
+ client := clientsetfake .NewSimpleClientset ()
295
+ _ , err := client .AppsV1 ().DaemonSets (metav1 .NamespaceSystem ).Create (context .TODO (), & apps.DaemonSet {
296
+ TypeMeta : metav1.TypeMeta {
297
+ Kind : "DaemonSet" ,
298
+ APIVersion : "apps/v1" ,
299
+ },
300
+ ObjectMeta : metav1.ObjectMeta {
301
+ Name : "kube-proxy" ,
302
+ Namespace : metav1 .NamespaceSystem ,
303
+ Labels : map [string ]string {
304
+ "k8s-app" : "kube-proxy" ,
305
+ },
306
+ },
307
+ Spec : apps.DaemonSetSpec {
308
+ Template : v1.PodTemplateSpec {},
309
+ },
310
+ }, metav1.CreateOptions {})
311
+ if err != nil {
312
+ t .Fatalf ("error creating Daemonset: %v" , err )
313
+ }
314
+
315
+ return client
316
+ }
0 commit comments