Skip to content

Commit c8ecfc3

Browse files
authored
Merge pull request kubernetes#91158 from prasadkatti/master
[kubeadm] add tests for package `patchnode`
2 parents 0d4464d + 21a1d1d commit c8ecfc3

File tree

2 files changed

+138
-1
lines changed

2 files changed

+138
-1
lines changed

cmd/kubeadm/app/phases/patchnode/BUILD

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@io_bazel_rules_go//go:def.bzl", "go_library")
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
22

33
go_library(
44
name = "go_default_library",
@@ -27,3 +27,16 @@ filegroup(
2727
tags = ["automanaged"],
2828
visibility = ["//visibility:public"],
2929
)
30+
31+
go_test(
32+
name = "go_default_test",
33+
srcs = ["patchnode_test.go"],
34+
embed = [":go_default_library"],
35+
deps = [
36+
"//cmd/kubeadm/app/constants:go_default_library",
37+
"//staging/src/k8s.io/api/core/v1:go_default_library",
38+
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
39+
"//staging/src/k8s.io/client-go/kubernetes:go_default_library",
40+
"//staging/src/k8s.io/client-go/rest:go_default_library",
41+
],
42+
)
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package patchnode
18+
19+
import (
20+
"bytes"
21+
"encoding/json"
22+
"net/http"
23+
"net/http/httptest"
24+
"testing"
25+
26+
v1 "k8s.io/api/core/v1"
27+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
clientset "k8s.io/client-go/kubernetes"
29+
restclient "k8s.io/client-go/rest"
30+
kubeadmconstants "k8s.io/kubernetes/cmd/kubeadm/app/constants"
31+
)
32+
33+
func TestAnnotateCRISocket(t *testing.T) {
34+
tests := []struct {
35+
name string
36+
currentCRISocketAnnotation string
37+
newCRISocketAnnotation string
38+
expectedPatch string
39+
}{
40+
{
41+
name: "CRI-socket annotation missing",
42+
currentCRISocketAnnotation: "",
43+
newCRISocketAnnotation: "/run/containerd/containerd.sock",
44+
expectedPatch: `{"metadata":{"annotations":{"kubeadm.alpha.kubernetes.io/cri-socket":"/run/containerd/containerd.sock"}}}`,
45+
},
46+
{
47+
name: "CRI-socket annotation already exists",
48+
currentCRISocketAnnotation: "/run/containerd/containerd.sock",
49+
newCRISocketAnnotation: "/run/containerd/containerd.sock",
50+
expectedPatch: `{}`,
51+
},
52+
{
53+
name: "CRI-socket annotation needs to be updated",
54+
currentCRISocketAnnotation: "/var/run/dockershim.sock",
55+
newCRISocketAnnotation: "/run/containerd/containerd.sock",
56+
expectedPatch: `{"metadata":{"annotations":{"kubeadm.alpha.kubernetes.io/cri-socket":"/run/containerd/containerd.sock"}}}`,
57+
},
58+
}
59+
60+
for _, tc := range tests {
61+
t.Run(tc.name, func(t *testing.T) {
62+
63+
nodename := "node01"
64+
node := &v1.Node{
65+
ObjectMeta: metav1.ObjectMeta{
66+
Name: nodename,
67+
Labels: map[string]string{
68+
v1.LabelHostname: nodename,
69+
},
70+
Annotations: map[string]string{},
71+
},
72+
}
73+
74+
if tc.currentCRISocketAnnotation != "" {
75+
node.ObjectMeta.Annotations[kubeadmconstants.AnnotationKubeadmCRISocket] = tc.currentCRISocketAnnotation
76+
}
77+
78+
jsonNode, err := json.Marshal(node)
79+
if err != nil {
80+
t.Fatalf("unexpected encoding error: %v", err)
81+
}
82+
83+
var patchRequest string
84+
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
85+
w.Header().Set("Content-Type", "application/json")
86+
87+
if req.URL.Path != "/api/v1/nodes/"+nodename {
88+
t.Errorf("request for unexpected HTTP resource: %v", req.URL.Path)
89+
http.Error(w, "", http.StatusNotFound)
90+
return
91+
}
92+
93+
switch req.Method {
94+
case "GET":
95+
case "PATCH":
96+
buf := new(bytes.Buffer)
97+
buf.ReadFrom(req.Body)
98+
patchRequest = buf.String()
99+
default:
100+
t.Errorf("request for unexpected HTTP verb: %v", req.Method)
101+
http.Error(w, "", http.StatusNotFound)
102+
return
103+
}
104+
105+
w.WriteHeader(http.StatusOK)
106+
w.Write(jsonNode)
107+
}))
108+
defer s.Close()
109+
110+
cs, err := clientset.NewForConfig(&restclient.Config{Host: s.URL})
111+
if err != nil {
112+
t.Fatalf("unexpected error building clientset: %v", err)
113+
}
114+
115+
if err := AnnotateCRISocket(cs, nodename, tc.newCRISocketAnnotation); err != nil {
116+
t.Errorf("unexpected error: %v", err)
117+
}
118+
119+
if tc.expectedPatch != patchRequest {
120+
t.Errorf("expected patch %v, got %v", tc.expectedPatch, patchRequest)
121+
}
122+
})
123+
}
124+
}

0 commit comments

Comments
 (0)