Skip to content

Commit 3ec97a4

Browse files
authored
kubectl: add test coverage for cordon command (kubernetes#129202)
* kubectl: add test coverage for cordon command Signed-off-by: Omer Aplatony <[email protected]> * using cmp.Diff Signed-off-by: Omer Aplatony <[email protected]> --------- Signed-off-by: Omer Aplatony <[email protected]>
1 parent f073d64 commit 3ec97a4

File tree

1 file changed

+147
-0
lines changed

1 file changed

+147
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
/*
2+
Copyright 2024 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 drain
18+
19+
import (
20+
"testing"
21+
22+
"github.com/google/go-cmp/cmp"
23+
corev1 "k8s.io/api/core/v1"
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
"k8s.io/apimachinery/pkg/runtime"
26+
"k8s.io/apimachinery/pkg/runtime/schema"
27+
)
28+
29+
type newCordonHelperFromRuntimeObjectTestCase struct {
30+
name string
31+
nodeObject runtime.Object
32+
expectError bool
33+
expected *CordonHelper
34+
}
35+
36+
func TestNewCordonHelperFromRuntimeObject(t *testing.T) {
37+
tests := []newCordonHelperFromRuntimeObjectTestCase{
38+
{
39+
name: "valid node object",
40+
nodeObject: &corev1.Node{
41+
ObjectMeta: metav1.ObjectMeta{
42+
Name: "test-node",
43+
},
44+
},
45+
expectError: false,
46+
expected: &CordonHelper{
47+
node: &corev1.Node{
48+
TypeMeta: metav1.TypeMeta{
49+
Kind: "Node",
50+
APIVersion: "v1",
51+
},
52+
ObjectMeta: metav1.ObjectMeta{
53+
Name: "test-node",
54+
},
55+
},
56+
},
57+
},
58+
{
59+
name: "invalid object type",
60+
nodeObject: &corev1.Pod{
61+
ObjectMeta: metav1.ObjectMeta{
62+
Name: "test-pod",
63+
},
64+
},
65+
expectError: true,
66+
expected: nil,
67+
},
68+
}
69+
scheme := runtime.NewScheme()
70+
_ = corev1.AddToScheme(scheme)
71+
gvk := schema.GroupVersionKind{
72+
Group: "",
73+
Version: "v1",
74+
Kind: "Node",
75+
}
76+
77+
for _, tt := range tests {
78+
t.Run(tt.name, func(t *testing.T) {
79+
helper, err := NewCordonHelperFromRuntimeObject(tt.nodeObject, scheme, gvk)
80+
if tt.expectError && err == nil {
81+
t.Error("Expected error but got none")
82+
}
83+
if !tt.expectError && err != nil {
84+
t.Errorf("Unexpected error: %v", err)
85+
}
86+
if !tt.expectError && helper == nil {
87+
t.Error("Expected non-nil helper")
88+
}
89+
if tt.expected != nil && helper != nil {
90+
if diff := cmp.Diff(tt.expected.node, helper.node); diff != "" {
91+
t.Errorf("Node mismatch (-want +got):\n%s", diff)
92+
}
93+
}
94+
})
95+
}
96+
}
97+
98+
type updateIfRequiredTestCase struct {
99+
name string
100+
currentState bool
101+
desiredState bool
102+
expectUpdated bool
103+
}
104+
105+
func TestUpdateIfRequired(t *testing.T) {
106+
tests := []updateIfRequiredTestCase{
107+
{
108+
name: "no change required",
109+
currentState: true,
110+
desiredState: true,
111+
expectUpdated: false,
112+
},
113+
{
114+
name: "update required - cordon",
115+
currentState: false,
116+
desiredState: true,
117+
expectUpdated: true,
118+
},
119+
{
120+
name: "update required - uncordon",
121+
currentState: true,
122+
desiredState: false,
123+
expectUpdated: true,
124+
},
125+
}
126+
for _, tt := range tests {
127+
t.Run(tt.name, func(t *testing.T) {
128+
node := &corev1.Node{
129+
ObjectMeta: metav1.ObjectMeta{
130+
Name: "test-node",
131+
},
132+
Spec: corev1.NodeSpec{
133+
Unschedulable: tt.currentState,
134+
},
135+
}
136+
137+
helper := NewCordonHelper(node)
138+
updated := helper.UpdateIfRequired(tt.desiredState)
139+
if updated != tt.expectUpdated {
140+
t.Errorf("Expected UpdateIfRequired to return %v, got %v", tt.expectUpdated, updated)
141+
}
142+
if helper.desired != tt.desiredState {
143+
t.Errorf("Expected desired state to be %v, got %v", tt.desiredState, helper.desired)
144+
}
145+
})
146+
}
147+
}

0 commit comments

Comments
 (0)