package utils import ( "fmt" "testing" "github.com/stretchr/testify/assert" ) func TestToENVName(t *testing.T) { testCases := []struct { title string expected string labelKey string }{ { title: "test case: upper cases", expected: "test_1", labelKey: "test_1", }, { title: "test case: .", expected: "node.ncr.com", labelKey: "node.ncr.com", }, { title: "test case: empty string", expected: "", labelKey: "", }, { title: "test case: space string", expected: "", labelKey: " ", }, { title: "test case: space string", expected: "_", labelKey: "_", }, { title: "test case: . and space", expected: "testing.description_stuff", labelKey: "testing.description stuff", }, } for _, testCase := range testCases { t.Run(testCase.title, func(t *testing.T) { assert.Equal(t, testCase.expected, ToENVName(testCase.labelKey)) }) } } func FuzzToENVName(f *testing.F) { testcases := []string{"hello world", "HW.1_Hi", " Hello 1.2 /W", "12 H8)loo"} for _, tc := range testcases { f.Add(tc) } f.Fuzz(func(t *testing.T, a string) { envName := ToENVName(a) if !IsValidEnvironmentName(envName) { t.Errorf("%s is an invalid k8s environment name", envName) } }) } func TestToK8sName(t *testing.T) { testCases := []struct { title string expected string labelKey string }{ { title: "test case: _", expected: "test-1", labelKey: "test_1", }, { title: "test case: .", expected: "node-ncr-com", labelKey: "node.ncr.com", }, { title: "test case: empty string", expected: "", labelKey: "", }, { title: "test case: . and space", expected: "testing-description-stuff", labelKey: "testing.description stuff", }, { title: "test case: string starting/ending with special chars", expected: "testing", labelKey: " .testing.", }, { title: "test case: space", expected: "", labelKey: " ", }, { title: "test case: spaces", expected: "", labelKey: " ", }, { title: "test case: spaces and special chars", expected: "", labelKey: " &", }, } for _, testCase := range testCases { t.Run(testCase.title, func(t *testing.T) { assert.Equal(t, testCase.expected, ToK8sName(testCase.labelKey)) }) } } func TestIsValidValue(t *testing.T) { testCases := []struct { title string expected bool value string }{ { title: "test case: valid string", value: "hello", expected: true, }, { title: "test case: string starting with multiple special chars", value: "._hello.world", expected: false, }, { title: "test case: string with multiple special chars", value: "-hello.world", expected: false, }, { title: "test case: string with space", value: "hello world", expected: false, }, { title: "test case: string with special char", value: "hello&world", expected: false, }, { title: "test case: empty string", value: "", expected: true, }, { title: "test case: space", value: " ", expected: false, }, { title: "test case: special char", value: "_", expected: false, }, { title: "test case: string starting with space and special chars", value: " .-test", expected: false, }, } for _, testCase := range testCases { t.Run(testCase.title, func(t *testing.T) { assert.Equal(t, testCase.expected, IsValidLabelValue(testCase.value)) }) } } func TestValuesValidation(t *testing.T) { testCases := []struct { title string expected error value []string }{ { title: "test case: valid string list", value: []string{"hello", "world"}, expected: nil, }, { title: "test case: string with invalid chars", value: []string{"w/_.orld", "hello_", "wor_ld"}, expected: fmt.Errorf("invalid k8s label value: w/_.orld\ninvalid k8s label value: hello_"), }, { title: "test case: string that exceeds max length", value: []string{"w/_.orld", "hello_a_valid_label_key_and_value_should_be_less_than_63_characters_each"}, 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"), }, } for _, testCase := range testCases { t.Run(testCase.title, func(t *testing.T) { err := ValuesValidation(testCase.value) if testCase.expected != nil { assert.Error(t, err) assert.Equal(t, testCase.expected.Error(), err.Error()) } else { assert.NoError(t, err) } }) } }