Skip to content

Commit 13e59ab

Browse files
authored
Merge pull request kubernetes#72433 from bart0sh/PR0050-kubeadm-app-util-use-T.Run
kubeadm: use T.Run API in app/util
2 parents 65f87b5 + 47b4d8f commit 13e59ab

File tree

10 files changed

+666
-527
lines changed

10 files changed

+666
-527
lines changed

cmd/kubeadm/app/util/apiclient/dryrunclient_test.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,33 @@ import (
2929

3030
func TestLogDryRunAction(t *testing.T) {
3131
var tests = []struct {
32+
name string
3233
action core.Action
3334
expectedBytes []byte
3435
buf *bytes.Buffer
3536
}{
3637
{
38+
name: "action GET on services",
3739
action: core.NewGetAction(schema.GroupVersionResource{Version: "v1", Resource: "services"}, "default", "kubernetes"),
3840
expectedBytes: []byte(`[dryrun] Would perform action GET on resource "services" in API group "core/v1"
3941
[dryrun] Resource name: "kubernetes"
4042
`),
4143
},
4244
{
45+
name: "action GET on clusterrolebindings",
4346
action: core.NewRootGetAction(schema.GroupVersionResource{Group: rbac.GroupName, Version: rbac.SchemeGroupVersion.Version, Resource: "clusterrolebindings"}, "system:node"),
4447
expectedBytes: []byte(`[dryrun] Would perform action GET on resource "clusterrolebindings" in API group "rbac.authorization.k8s.io/v1"
4548
[dryrun] Resource name: "system:node"
4649
`),
4750
},
4851
{
52+
name: "action LIST on services",
4953
action: core.NewListAction(schema.GroupVersionResource{Version: "v1", Resource: "services"}, schema.GroupVersionKind{Version: "v1", Kind: "Service"}, "default", metav1.ListOptions{}),
5054
expectedBytes: []byte(`[dryrun] Would perform action LIST on resource "services" in API group "core/v1"
5155
`),
5256
},
5357
{
58+
name: "action CREATE on services",
5459
action: core.NewCreateAction(schema.GroupVersionResource{Version: "v1", Resource: "services"}, "default", &v1.Service{
5560
ObjectMeta: metav1.ObjectMeta{
5661
Name: "foo",
@@ -72,6 +77,7 @@ func TestLogDryRunAction(t *testing.T) {
7277
`),
7378
},
7479
{
80+
name: "action PATCH on nodes",
7581
action: core.NewPatchAction(schema.GroupVersionResource{Version: "v1", Resource: "nodes"}, "default", "my-node", "application/strategic-merge-patch+json", []byte(`{"spec":{"taints":[{"key": "foo", "value": "bar"}]}}`)),
7682
expectedBytes: []byte(`[dryrun] Would perform action PATCH on resource "nodes" in API group "core/v1"
7783
[dryrun] Resource name: "my-node"
@@ -80,23 +86,26 @@ func TestLogDryRunAction(t *testing.T) {
8086
`),
8187
},
8288
{
89+
name: "action DELETE on pods",
8390
action: core.NewDeleteAction(schema.GroupVersionResource{Version: "v1", Resource: "pods"}, "default", "my-pod"),
8491
expectedBytes: []byte(`[dryrun] Would perform action DELETE on resource "pods" in API group "core/v1"
8592
[dryrun] Resource name: "my-pod"
8693
`),
8794
},
8895
}
8996
for _, rt := range tests {
90-
rt.buf = bytes.NewBufferString("")
91-
logDryRunAction(rt.action, rt.buf, DefaultMarshalFunc)
92-
actualBytes := rt.buf.Bytes()
97+
t.Run(rt.name, func(t *testing.T) {
98+
rt.buf = bytes.NewBufferString("")
99+
logDryRunAction(rt.action, rt.buf, DefaultMarshalFunc)
100+
actualBytes := rt.buf.Bytes()
93101

94-
if !bytes.Equal(actualBytes, rt.expectedBytes) {
95-
t.Errorf(
96-
"failed LogDryRunAction:\n\texpected bytes: %q\n\t actual: %q",
97-
rt.expectedBytes,
98-
actualBytes,
99-
)
100-
}
102+
if !bytes.Equal(actualBytes, rt.expectedBytes) {
103+
t.Errorf(
104+
"failed LogDryRunAction:\n\texpected bytes: %q\n\t actual: %q",
105+
rt.expectedBytes,
106+
actualBytes,
107+
)
108+
}
109+
})
101110
}
102111
}

cmd/kubeadm/app/util/apiclient/init_dryrun_test.go

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,95 +32,106 @@ func TestHandleGetAction(t *testing.T) {
3232
idr := NewInitDryRunGetter(masterName, serviceSubnet)
3333

3434
var tests = []struct {
35+
name string
3536
action core.GetActionImpl
3637
expectedHandled bool
3738
expectedObjectJSON []byte
3839
expectedErr bool
3940
}{
4041
{
42+
name: "get default services",
4143
action: core.NewGetAction(schema.GroupVersionResource{Version: "v1", Resource: "services"}, "default", "kubernetes"),
4244
expectedHandled: true,
4345
expectedObjectJSON: []byte(`{"metadata":{"name":"kubernetes","namespace":"default","creationTimestamp":null,"labels":{"component":"apiserver","provider":"kubernetes"}},"spec":{"ports":[{"name":"https","port":443,"targetPort":6443}],"clusterIP":"10.96.0.1"},"status":{"loadBalancer":{}}}`),
4446
expectedErr: false,
4547
},
4648
{
49+
name: "get nodes",
4750
action: core.NewRootGetAction(schema.GroupVersionResource{Version: "v1", Resource: "nodes"}, masterName),
4851
expectedHandled: true,
4952
expectedObjectJSON: []byte(`{"metadata":{"name":"master-foo","creationTimestamp":null,"labels":{"kubernetes.io/hostname":"master-foo"}},"spec":{},"status":{"daemonEndpoints":{"kubeletEndpoint":{"Port":0}},"nodeInfo":{"machineID":"","systemUUID":"","bootID":"","kernelVersion":"","osImage":"","containerRuntimeVersion":"","kubeletVersion":"","kubeProxyVersion":"","operatingSystem":"","architecture":""}}}`),
5053
expectedErr: false,
5154
},
5255
{
56+
name: "get clusterrolebinings",
5357
action: core.NewRootGetAction(schema.GroupVersionResource{Group: rbac.GroupName, Version: rbac.SchemeGroupVersion.Version, Resource: "clusterrolebindings"}, "system:node"),
5458
expectedHandled: true,
5559
expectedObjectJSON: []byte(``),
5660
expectedErr: true, // we expect a NotFound error here
5761
},
5862
{
63+
name: "get kube-system secret bootstrap-token-abcdef",
5964
action: core.NewGetAction(schema.GroupVersionResource{Version: "v1", Resource: "secrets"}, "kube-system", "bootstrap-token-abcdef"),
6065
expectedHandled: true,
6166
expectedObjectJSON: []byte(``),
6267
expectedErr: true, // we expect a NotFound error here
6368
},
6469
{ // an ask for a kubernetes service in the _kube-system_ ns should not be answered
70+
name: "get kube-system services",
6571
action: core.NewGetAction(schema.GroupVersionResource{Version: "v1", Resource: "services"}, "kube-system", "kubernetes"),
6672
expectedHandled: false,
6773
expectedObjectJSON: []byte(``),
6874
expectedErr: false,
6975
},
7076
{ // an ask for an other service than kubernetes should not be answered
77+
name: "get default my-other-service",
7178
action: core.NewGetAction(schema.GroupVersionResource{Version: "v1", Resource: "services"}, "default", "my-other-service"),
7279
expectedHandled: false,
7380
expectedObjectJSON: []byte(``),
7481
expectedErr: false,
7582
},
7683
{ // an ask for an other node than the master should not be answered
84+
name: "get other-node",
7785
action: core.NewRootGetAction(schema.GroupVersionResource{Version: "v1", Resource: "nodes"}, "other-node"),
7886
expectedHandled: false,
7987
expectedObjectJSON: []byte(``),
8088
expectedErr: false,
8189
},
8290
{ // an ask for a secret in any other ns than kube-system should not be answered
91+
name: "get default secret bootstrap-token-abcdef",
8392
action: core.NewGetAction(schema.GroupVersionResource{Version: "v1", Resource: "secrets"}, "default", "bootstrap-token-abcdef"),
8493
expectedHandled: false,
8594
expectedObjectJSON: []byte(``),
8695
expectedErr: false,
8796
},
8897
}
8998
for _, rt := range tests {
90-
handled, obj, actualErr := idr.HandleGetAction(rt.action)
91-
objBytes := []byte(``)
92-
if obj != nil {
93-
var err error
94-
objBytes, err = json.Marshal(obj)
95-
if err != nil {
96-
t.Fatalf("couldn't marshal returned object")
99+
t.Run(rt.name, func(t *testing.T) {
100+
handled, obj, actualErr := idr.HandleGetAction(rt.action)
101+
objBytes := []byte(``)
102+
if obj != nil {
103+
var err error
104+
objBytes, err = json.Marshal(obj)
105+
if err != nil {
106+
t.Fatalf("couldn't marshal returned object")
107+
}
97108
}
98-
}
99109

100-
if handled != rt.expectedHandled {
101-
t.Errorf(
102-
"failed HandleGetAction:\n\texpected handled: %t\n\t actual: %t %v",
103-
rt.expectedHandled,
104-
handled,
105-
rt.action,
106-
)
107-
}
110+
if handled != rt.expectedHandled {
111+
t.Errorf(
112+
"failed HandleGetAction:\n\texpected handled: %t\n\t actual: %t %v",
113+
rt.expectedHandled,
114+
handled,
115+
rt.action,
116+
)
117+
}
108118

109-
if !bytes.Equal(objBytes, rt.expectedObjectJSON) {
110-
t.Errorf(
111-
"failed HandleGetAction:\n\texpected object: %q\n\t actual: %q",
112-
rt.expectedObjectJSON,
113-
objBytes,
114-
)
115-
}
119+
if !bytes.Equal(objBytes, rt.expectedObjectJSON) {
120+
t.Errorf(
121+
"failed HandleGetAction:\n\texpected object: %q\n\t actual: %q",
122+
rt.expectedObjectJSON,
123+
objBytes,
124+
)
125+
}
116126

117-
if (actualErr != nil) != rt.expectedErr {
118-
t.Errorf(
119-
"failed HandleGetAction:\n\texpected error: %t\n\t actual: %t %v",
120-
rt.expectedErr,
121-
(actualErr != nil),
122-
rt.action,
123-
)
124-
}
127+
if (actualErr != nil) != rt.expectedErr {
128+
t.Errorf(
129+
"failed HandleGetAction:\n\texpected error: %t\n\t actual: %t %v",
130+
rt.expectedErr,
131+
(actualErr != nil),
132+
rt.action,
133+
)
134+
}
135+
})
125136
}
126137
}

0 commit comments

Comments
 (0)