Skip to content

Commit 00a6dee

Browse files
committed
add more tests
1 parent c4ddd55 commit 00a6dee

File tree

1 file changed

+147
-19
lines changed

1 file changed

+147
-19
lines changed

aks-node-controller/pkg/nodeconfigutils/utils_test.go

Lines changed: 147 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,14 @@ func TestUnmarshalConfigurationV1(t *testing.T) {
2121
name: "valid minimal config",
2222
data: []byte(`{
2323
"version": "v1",
24-
"authConfig": {
25-
"subscriptionId": "test-subscription"
24+
"auth_config": {
25+
"subscription_id": "test-subscription"
2626
},
27-
"clusterConfig": {
28-
"resourceGroup": "test-rg",
27+
"cluster_config": {
28+
"resource_group": "test-rg",
2929
"location": "eastus"
3030
},
31-
"apiServerConfig": {
31+
"api_server_config": {
3232
"apiServerName": "test-api-server"
3333
}
3434
}`),
@@ -50,22 +50,24 @@ func TestUnmarshalConfigurationV1(t *testing.T) {
5050
{
5151
name: "empty data",
5252
data: []byte(""),
53-
want: nil,
53+
want: &aksnodeconfigv1.Configuration{},
5454
wantErr: true,
5555
},
5656
{
57-
name: "invalid JSON",
58-
data: []byte(`{"version": "v1", invalid}`),
59-
want: nil,
57+
name: "invalid JSON",
58+
data: []byte(`{"version": "v1", invalid}`),
59+
want: &aksnodeconfigv1.Configuration{
60+
Version: "v1",
61+
},
6062
wantErr: true,
6163
},
6264
{
6365
name: "string is assigned with a boolean value",
6466
data: []byte(`{
6567
"version": "v1",
66-
"LinuxAdminUsername": true,
67-
"authConfig": {
68-
"subscriptionId": "test-subscription"
68+
"linux_admin_username": true,
69+
"auth_config": {
70+
"subscription_id": "test-subscription"
6971
}
7072
}`),
7173
want: &aksnodeconfigv1.Configuration{
@@ -74,15 +76,15 @@ func TestUnmarshalConfigurationV1(t *testing.T) {
7476
SubscriptionId: "test-subscription",
7577
},
7678
},
77-
wantErr: false,
79+
wantErr: true,
7880
},
7981
{
8082
name: "unknown field should be ignored",
8183
data: []byte(`{
8284
"version": "v1",
83-
"unknownField": "should be ignored",
84-
"authConfig": {
85-
"subscriptionId": "test-subscription"
85+
"unknown_feld": "should be ignored",
86+
"auth_config": {
87+
"subscription_id": "test-subscription"
8688
}
8789
}`),
8890
want: &aksnodeconfigv1.Configuration{
@@ -93,6 +95,119 @@ func TestUnmarshalConfigurationV1(t *testing.T) {
9395
},
9496
wantErr: false,
9597
},
98+
{
99+
name: "valid enum values as strings",
100+
data: []byte(`{
101+
"version": "v1",
102+
"auth_config": {
103+
"subscription_id": "test-subscription"
104+
},
105+
"workload_runtime": "WORKLOAD_RUNTIME_OCI_CONTAINER"
106+
}`),
107+
want: &aksnodeconfigv1.Configuration{
108+
Version: "v1",
109+
AuthConfig: &aksnodeconfigv1.AuthConfig{
110+
SubscriptionId: "test-subscription",
111+
},
112+
WorkloadRuntime: aksnodeconfigv1.WorkloadRuntime_WORKLOAD_RUNTIME_OCI_CONTAINER,
113+
},
114+
wantErr: false,
115+
},
116+
{
117+
name: "unknown enum values should default to UNSPECIFIED",
118+
data: []byte(`{
119+
"version": "v1",
120+
"auth_config": {
121+
"subscription_id": "test-subscription"
122+
},
123+
"workload_runtime": "WHAT IS THIS?"
124+
}`),
125+
want: &aksnodeconfigv1.Configuration{
126+
Version: "v1",
127+
AuthConfig: &aksnodeconfigv1.AuthConfig{
128+
SubscriptionId: "test-subscription",
129+
},
130+
WorkloadRuntime: aksnodeconfigv1.WorkloadRuntime_WORKLOAD_RUNTIME_UNSPECIFIED,
131+
},
132+
wantErr: false,
133+
},
134+
{
135+
name: "repeated string field with wrong type is ignored",
136+
data: []byte(`{
137+
"version": "v1",
138+
"auth_config": {
139+
"subscription_id": "test-subscription"
140+
},
141+
"custom_ca_certs": "not-an-array"
142+
}`),
143+
want: &aksnodeconfigv1.Configuration{
144+
Version: "v1",
145+
AuthConfig: &aksnodeconfigv1.AuthConfig{
146+
SubscriptionId: "test-subscription",
147+
},
148+
},
149+
wantErr: true,
150+
},
151+
{
152+
name: "repeated string field with mixed types parses valid elements",
153+
data: []byte(`{
154+
"version": "v1",
155+
"auth_config": {
156+
"subscription_id": "test-subscription"
157+
},
158+
"custom_ca_certs": ["valid-cert", 123, true]
159+
}`),
160+
want: &aksnodeconfigv1.Configuration{
161+
Version: "v1",
162+
AuthConfig: &aksnodeconfigv1.AuthConfig{
163+
SubscriptionId: "test-subscription",
164+
},
165+
CustomCaCerts: []string{"valid-cert"},
166+
},
167+
wantErr: true,
168+
},
169+
{
170+
name: "map field with wrong value type is ignored",
171+
data: []byte(`{
172+
"version": "v1",
173+
"auth_config": {
174+
"subscription_id": "test-subscription"
175+
},
176+
"kubelet_config": {
177+
"kubelet_flags": {
178+
"key1": "value1",
179+
"key2": 123
180+
}
181+
}
182+
}`),
183+
want: &aksnodeconfigv1.Configuration{
184+
Version: "v1",
185+
AuthConfig: &aksnodeconfigv1.AuthConfig{
186+
SubscriptionId: "test-subscription",
187+
},
188+
},
189+
wantErr: true,
190+
},
191+
{
192+
name: "optional int32 field with string value is ignored",
193+
data: []byte(`{
194+
"version": "v1",
195+
"auth_config": {
196+
"subscription_id": "test-subscription"
197+
},
198+
"kubelet_config": {
199+
"max_pods": "42"
200+
}
201+
}`),
202+
want: &aksnodeconfigv1.Configuration{
203+
Version: "v1",
204+
AuthConfig: &aksnodeconfigv1.AuthConfig{
205+
SubscriptionId: "test-subscription",
206+
},
207+
KubeletConfig: &aksnodeconfigv1.KubeletConfig{},
208+
},
209+
wantErr: false,
210+
},
96211
}
97212

98213
for _, tt := range tests {
@@ -104,9 +219,9 @@ func TestUnmarshalConfigurationV1(t *testing.T) {
104219
} else {
105220
require.NoError(t, err)
106221
// here we use proto.Equal for deep equality check
107-
if !proto.Equal(tt.want, got) {
108-
assert.Fail(t, "UnmarshalConfigurationV1() result mismatch", "want: %+v\n got: %+v", tt.want, got)
109-
}
222+
}
223+
if !proto.Equal(tt.want, got) {
224+
assert.Fail(t, "UnmarshalConfigurationV1() result mismatch", "want: %+v\n got: %+v", tt.want, got)
110225
}
111226
})
112227
}
@@ -149,3 +264,16 @@ func TestUnmarshalConfigurationV1FromAJsonFile(t *testing.T) {
149264
})
150265
}
151266
}
267+
268+
func TestMarsalConfiguratioV1(t *testing.T) {
269+
cfg := &aksnodeconfigv1.Configuration{
270+
Version: "v1",
271+
AuthConfig: &aksnodeconfigv1.AuthConfig{
272+
SubscriptionId: "test-subscription",
273+
},
274+
WorkloadRuntime: aksnodeconfigv1.WorkloadRuntime_WORKLOAD_RUNTIME_OCI_CONTAINER,
275+
}
276+
data, err := MarshalConfigurationV1(cfg)
277+
require.NoError(t, err)
278+
require.JSONEq(t, `{"version":"v1","auth_config":{"subscription_id":"test-subscription"}, "workload_runtime":"WORKLOAD_RUNTIME_OCI_CONTAINER"}`, string(data))
279+
}

0 commit comments

Comments
 (0)