Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pkg/hypershift/hypershift.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import (

configv1 "github.com/openshift/api/config/v1"
operv1 "github.com/openshift/api/operator/v1"
"gopkg.in/yaml.v2"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/yaml"
)

const HostedClusterLocalProxy = "socks5://127.0.0.1:8090"
Expand Down
114 changes: 113 additions & 1 deletion pkg/hypershift/hypershift_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package hypershift

import (
"testing"

. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/yaml"
"testing"
)

func TestParseHostedControlPlane(t *testing.T) {
Expand Down Expand Up @@ -103,3 +105,113 @@ spec:
g.Expect(actualOutput).To(Equal(tc.expectedOutput))
}
}

func TestTolerationsToStringSliceYaml(t *testing.T) {
g := NewGomegaWithT(t)

testCases := []struct {
name string
tolerations []corev1.Toleration
expected []string
}{
{
name: "operator Exists with no key or value should generate valid YAML",
tolerations: []corev1.Toleration{
{
Operator: corev1.TolerationOpExists,
},
},
expected: []string{
"- operator: Exists",
},
},
{
name: "operator Equal with key and value should include all fields",
tolerations: []corev1.Toleration{
{
Key: "node-role.kubernetes.io/master",
Operator: corev1.TolerationOpEqual,
Value: "true",
Effect: corev1.TaintEffectNoSchedule,
},
},
expected: []string{
"- effect: NoSchedule",
" key: node-role.kubernetes.io/master",
" operator: Equal",
" value: \"true\"",
},
},
{
name: "empty string values should be filtered out",
tolerations: []corev1.Toleration{
{
Key: "test-key",
Operator: corev1.TolerationOpEqual,
Value: "",
Effect: corev1.TaintEffectNoSchedule,
},
},
expected: []string{
"- effect: NoSchedule",
" key: test-key",
" operator: Equal",
},
},
{
name: "operator Exists with key should not include null value",
tolerations: []corev1.Toleration{
{
Key: "node.kubernetes.io/unreachable",
Operator: corev1.TolerationOpExists,
Effect: corev1.TaintEffectNoExecute,
},
},
expected: []string{
"- effect: NoExecute",
" key: node.kubernetes.io/unreachable",
" operator: Exists",
},
},
{
name: "multiple tolerations should generate proper YAML array",
tolerations: []corev1.Toleration{
{
Key: "node-role.kubernetes.io/master",
Operator: corev1.TolerationOpEqual,
Value: "true",
Effect: corev1.TaintEffectNoSchedule,
},
{
Operator: corev1.TolerationOpExists,
},
{
Key: "node.kubernetes.io/unreachable",
Operator: corev1.TolerationOpExists,
Effect: corev1.TaintEffectNoExecute,
},
},
expected: []string{
"- effect: NoSchedule",
" key: node-role.kubernetes.io/master",
" operator: Equal",
" value: \"true\"",
"- operator: Exists",
"- effect: NoExecute",
" key: node.kubernetes.io/unreachable",
" operator: Exists",
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result, err := tolerationsToStringSliceYaml(tc.tolerations)
g.Expect(err).NotTo(HaveOccurred())
if result[len(result)-1] == "" {
result = result[:len(result)-1]
}
g.Expect(result).To(Equal(tc.expected), "Expected exact YAML format match")
})
}
}