From b30b04fa4b4f05a4fdd4394d9d58ab58f1c45dd1 Mon Sep 17 00:00:00 2001 From: Anuj Agrawal Date: Sat, 12 Oct 2024 18:34:30 +0530 Subject: [PATCH] Added tests for pkg/util/lifted/validateclustertaints.go and validatingmcs.go Signed-off-by: Anuj Agrawal --- pkg/util/lifted/validateclustertaints_test.go | 157 ++++++++++++++++++ pkg/util/lifted/validatingmcs_test.go | 113 +++++++++++++ 2 files changed, 270 insertions(+) create mode 100644 pkg/util/lifted/validateclustertaints_test.go create mode 100644 pkg/util/lifted/validatingmcs_test.go diff --git a/pkg/util/lifted/validateclustertaints_test.go b/pkg/util/lifted/validateclustertaints_test.go new file mode 100644 index 000000000000..cc39f05ee481 --- /dev/null +++ b/pkg/util/lifted/validateclustertaints_test.go @@ -0,0 +1,157 @@ +/* +Copyright 2014 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package lifted + +import ( + "testing" + + "github.com/stretchr/testify/assert" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/util/validation/field" +) + +func TestValidateClusterTaintEffect(t *testing.T) { + tests := []struct { + name string + effect corev1.TaintEffect + allowEmpty bool + wantErrors int + errorMsg string + }{ + { + name: "valid NoSchedule effect", + effect: corev1.TaintEffectNoSchedule, + allowEmpty: false, + wantErrors: 0, + }, + { + name: "valid NoExecute effect", + effect: corev1.TaintEffectNoExecute, + allowEmpty: false, + wantErrors: 0, + }, + { + name: "invalid effect", + effect: corev1.TaintEffect("InvalidEffect"), + allowEmpty: false, + wantErrors: 1, + errorMsg: "test: Unsupported value: \"InvalidEffect\": supported values: \"NoSchedule\", \"NoExecute\"", + }, + { + name: "empty effect not allowed", + effect: corev1.TaintEffect(""), + allowEmpty: false, + wantErrors: 1, + errorMsg: "test: Required value", + }, + { + name: "empty effect with allowEmpty true", + effect: corev1.TaintEffect(""), + allowEmpty: true, + wantErrors: 1, + errorMsg: "test: Unsupported value: \"\": supported values: \"NoSchedule\", \"NoExecute\"", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + errors := validateClusterTaintEffect(&tt.effect, tt.allowEmpty, field.NewPath("test")) + assert.Len(t, errors, tt.wantErrors) + + if tt.wantErrors == 0 { + assert.Empty(t, errors) + } else { + assert.NotEmpty(t, errors) + assert.Equal(t, tt.errorMsg, errors[0].Error()) + } + }) + } +} +func TestValidateClusterTaints(t *testing.T) { + tests := []struct { + name string + taints []corev1.Taint + wantErrors int + }{ + { + name: "valid taints", + taints: []corev1.Taint{ + {Key: "key1", Value: "value1", Effect: corev1.TaintEffectNoSchedule}, + {Key: "key2", Value: "value2", Effect: corev1.TaintEffectNoExecute}, + }, + wantErrors: 0, + }, + { + name: "invalid taint key", + taints: []corev1.Taint{ + {Key: "invalid key", Value: "value1", Effect: corev1.TaintEffectNoSchedule}, + }, + wantErrors: 1, + }, + { + name: "invalid taint value", + taints: []corev1.Taint{ + {Key: "key1", Value: "invalid value!", Effect: corev1.TaintEffectNoSchedule}, + }, + wantErrors: 1, + }, + { + name: "invalid taint effect", + taints: []corev1.Taint{ + {Key: "key1", Value: "value1", Effect: corev1.TaintEffect("InvalidEffect")}, + }, + wantErrors: 1, + }, + { + name: "duplicate taints", + taints: []corev1.Taint{ + {Key: "key1", Value: "value1", Effect: corev1.TaintEffectNoSchedule}, + {Key: "key1", Value: "value2", Effect: corev1.TaintEffectNoSchedule}, + }, + wantErrors: 1, + }, + { + name: "multiple errors", + taints: []corev1.Taint{ + {Key: "invalid key", Value: "invalid value!", Effect: corev1.TaintEffect("InvalidEffect")}, + {Key: "key1", Value: "value1", Effect: corev1.TaintEffectNoSchedule}, + {Key: "key1", Value: "value2", Effect: corev1.TaintEffectNoSchedule}, + }, + wantErrors: 4, + }, + { + name: "empty effect", + taints: []corev1.Taint{ + {Key: "key1", Value: "value1", Effect: corev1.TaintEffect("")}, + }, + wantErrors: 1, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + errors := ValidateClusterTaints(tt.taints, field.NewPath("test")) + assert.Len(t, errors, tt.wantErrors) + + if tt.wantErrors == 0 { + assert.Empty(t, errors) + } else { + assert.NotEmpty(t, errors) + } + }) + } +} diff --git a/pkg/util/lifted/validatingmcs_test.go b/pkg/util/lifted/validatingmcs_test.go new file mode 100644 index 000000000000..385d1829a7f9 --- /dev/null +++ b/pkg/util/lifted/validatingmcs_test.go @@ -0,0 +1,113 @@ +/* +Copyright 2024 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package lifted + +import ( + "testing" + + "github.com/stretchr/testify/assert" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/util/validation/field" +) + +func TestValidateLoadBalancerStatus(t *testing.T) { + tests := []struct { + name string + status *corev1.LoadBalancerStatus + wantErrors int + }{ + { + name: "valid IP", + status: &corev1.LoadBalancerStatus{ + Ingress: []corev1.LoadBalancerIngress{ + {IP: "192.168.1.1"}, + }, + }, + wantErrors: 0, + }, + { + name: "valid hostname", + status: &corev1.LoadBalancerStatus{ + Ingress: []corev1.LoadBalancerIngress{ + {Hostname: "example.com"}, + }, + }, + wantErrors: 0, + }, + { + name: "invalid IP", + status: &corev1.LoadBalancerStatus{ + Ingress: []corev1.LoadBalancerIngress{ + {IP: "300.300.300.300"}, + }, + }, + wantErrors: 1, + }, + { + name: "invalid hostname", + status: &corev1.LoadBalancerStatus{ + Ingress: []corev1.LoadBalancerIngress{ + {Hostname: "invalid_hostname"}, + }, + }, + wantErrors: 1, + }, + { + name: "IP in hostname field", + status: &corev1.LoadBalancerStatus{ + Ingress: []corev1.LoadBalancerIngress{ + {Hostname: "192.168.1.1"}, + }, + }, + wantErrors: 1, + }, + { + name: "multiple valid entries", + status: &corev1.LoadBalancerStatus{ + Ingress: []corev1.LoadBalancerIngress{ + {IP: "192.168.1.1"}, + {Hostname: "example.com"}, + }, + }, + wantErrors: 0, + }, + { + name: "multiple entries with errors", + status: &corev1.LoadBalancerStatus{ + Ingress: []corev1.LoadBalancerIngress{ + {IP: "300.300.300.300"}, + {Hostname: "invalid_hostname"}, + {Hostname: "192.168.1.1"}, + }, + }, + wantErrors: 3, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + errors := ValidateLoadBalancerStatus(tt.status, field.NewPath("status")) + assert.Len(t, errors, tt.wantErrors) + + if tt.wantErrors == 0 { + assert.Empty(t, errors) + } else { + assert.NotEmpty(t, errors) + } + }) + } +}