1 package utils
2
3 import (
4 "fmt"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8 )
9
10 func TestToENVName(t *testing.T) {
11 testCases := []struct {
12 title string
13 expected string
14 labelKey string
15 }{
16 {
17 title: "test case: upper cases",
18 expected: "test_1",
19 labelKey: "test_1",
20 },
21 {
22 title: "test case: .",
23 expected: "node.ncr.com",
24 labelKey: "node.ncr.com",
25 },
26 {
27 title: "test case: empty string",
28 expected: "",
29 labelKey: "",
30 },
31 {
32 title: "test case: space string",
33 expected: "",
34 labelKey: " ",
35 },
36 {
37 title: "test case: space string",
38 expected: "_",
39 labelKey: "_",
40 },
41 {
42 title: "test case: . and space",
43 expected: "testing.description_stuff",
44 labelKey: "testing.description stuff",
45 },
46 }
47 for _, testCase := range testCases {
48 t.Run(testCase.title, func(t *testing.T) {
49 assert.Equal(t, testCase.expected, ToENVName(testCase.labelKey))
50 })
51 }
52 }
53
54 func FuzzToENVName(f *testing.F) {
55 testcases := []string{"hello world", "HW.1_Hi", " Hello 1.2 /W", "12 H8)loo"}
56 for _, tc := range testcases {
57 f.Add(tc)
58 }
59 f.Fuzz(func(t *testing.T, a string) {
60 envName := ToENVName(a)
61 if !IsValidEnvironmentName(envName) {
62 t.Errorf("%s is an invalid k8s environment name", envName)
63 }
64 })
65 }
66
67 func TestToK8sName(t *testing.T) {
68 testCases := []struct {
69 title string
70 expected string
71 labelKey string
72 }{
73 {
74 title: "test case: _",
75 expected: "test-1",
76 labelKey: "test_1",
77 },
78 {
79 title: "test case: .",
80 expected: "node-ncr-com",
81 labelKey: "node.ncr.com",
82 },
83 {
84 title: "test case: empty string",
85 expected: "",
86 labelKey: "",
87 },
88 {
89 title: "test case: . and space",
90 expected: "testing-description-stuff",
91 labelKey: "testing.description stuff",
92 },
93 {
94 title: "test case: string starting/ending with special chars",
95 expected: "testing",
96 labelKey: " .testing.",
97 },
98 {
99 title: "test case: space",
100 expected: "",
101 labelKey: " ",
102 },
103 {
104 title: "test case: spaces",
105 expected: "",
106 labelKey: " ",
107 },
108 {
109 title: "test case: spaces and special chars",
110 expected: "",
111 labelKey: " &",
112 },
113 }
114 for _, testCase := range testCases {
115 t.Run(testCase.title, func(t *testing.T) {
116 assert.Equal(t, testCase.expected, ToK8sName(testCase.labelKey))
117 })
118 }
119 }
120
121 func TestIsValidValue(t *testing.T) {
122 testCases := []struct {
123 title string
124 expected bool
125 value string
126 }{
127 {
128 title: "test case: valid string",
129 value: "hello",
130 expected: true,
131 },
132 {
133 title: "test case: string starting with multiple special chars",
134 value: "._hello.world",
135 expected: false,
136 },
137 {
138 title: "test case: string with multiple special chars",
139 value: "-hello.world",
140 expected: false,
141 },
142 {
143 title: "test case: string with space",
144 value: "hello world",
145 expected: false,
146 },
147 {
148 title: "test case: string with special char",
149 value: "hello&world",
150 expected: false,
151 },
152 {
153 title: "test case: empty string",
154 value: "",
155 expected: true,
156 },
157 {
158 title: "test case: space",
159 value: " ",
160 expected: false,
161 },
162 {
163 title: "test case: special char",
164 value: "_",
165 expected: false,
166 },
167 {
168 title: "test case: string starting with space and special chars",
169 value: " .-test",
170 expected: false,
171 },
172 }
173
174 for _, testCase := range testCases {
175 t.Run(testCase.title, func(t *testing.T) {
176 assert.Equal(t, testCase.expected, IsValidLabelValue(testCase.value))
177 })
178 }
179 }
180
181 func TestValuesValidation(t *testing.T) {
182 testCases := []struct {
183 title string
184 expected error
185 value []string
186 }{
187 {
188 title: "test case: valid string list",
189 value: []string{"hello", "world"},
190 expected: nil,
191 },
192 {
193 title: "test case: string with invalid chars",
194 value: []string{"w/_.orld", "hello_", "wor_ld"},
195 expected: fmt.Errorf("invalid k8s label value: w/_.orld\ninvalid k8s label value: hello_"),
196 },
197 {
198 title: "test case: string that exceeds max length",
199 value: []string{"w/_.orld", "hello_a_valid_label_key_and_value_should_be_less_than_63_characters_each"},
200 expected: fmt.Errorf("invalid k8s label value: w/_.orld\ninvalid k8s label value: hello_a_valid_label_key_and_value_should_be_less_than_63_characters_each"),
201 },
202 }
203
204 for _, testCase := range testCases {
205 t.Run(testCase.title, func(t *testing.T) {
206 err := ValuesValidation(testCase.value)
207 if testCase.expected != nil {
208 assert.Error(t, err)
209 assert.Equal(t, testCase.expected.Error(), err.Error())
210 } else {
211 assert.NoError(t, err)
212 }
213 })
214 }
215 }
216
View as plain text