From 722e7ef9e58b56fa5815af10c8794b0097ac8b9c Mon Sep 17 00:00:00 2001 From: Vivin Thomas Wilson Date: Mon, 22 Jul 2024 20:10:02 -0400 Subject: [PATCH] Update unit test to ignore ordering of labels generated by constructNodeSelector() (#1673) * Update unit test to ignore ordering of labels generated by constructNodeSelector() * Updates unit test in yaml_factory_test.go to fix failures due to output ordering of the generated selector labels. closes ut_label_ordering_fix * address PR comments --- cli/k8s_client/yaml_factory_test.go | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/cli/k8s_client/yaml_factory_test.go b/cli/k8s_client/yaml_factory_test.go index 34804f422..773ebcf0a 100644 --- a/cli/k8s_client/yaml_factory_test.go +++ b/cli/k8s_client/yaml_factory_test.go @@ -961,13 +961,30 @@ func TestGetCSIDaemonSetYAMLWindows_Tolerations(t *testing.T) { } func TestConstructNodeSelector(t *testing.T) { - nodeSelMap := map[string]string{"node-label-name": "master", "node-label-number": "20", "node-label-bool": "true", "node-label-alphanum": "alph20"} + nodeSelMap := map[string]string{ + "node-label-name": "master", + "node-label-number": "20", + "node-label-bool": "true", + "node-label-alphanum": "alph20", + } - expectedNodeSelString := []string{"- key: node-label-name\n operator: In\n values:\n - 'master'\n", "- key: node-label-number\n operator: In\n values:\n - '20'\n", "- key: node-label-bool\n operator: In\n values:\n - 'true'\n", "- key: node-label-alphanum\n operator: In\n values:\n - 'alph20'\n"} + expectedNodeSelStrings := []string{ + "- key: node-label-name\n operator: In\n values:\n - 'master'\n", + "- key: node-label-number\n operator: In\n values:\n - '20'\n", + "- key: node-label-bool\n operator: In\n values:\n - 'true'\n", + "- key: node-label-alphanum\n operator: In\n values:\n - 'alph20'\n", + } result := constructNodeSelector(nodeSelMap) - expectedString := strings.Join(expectedNodeSelString, "") - assert.Equal(t, expectedString, result) + // verify all the expected strings are present in the result + for _, expectedString := range expectedNodeSelStrings { + assert.Contains(t, result, expectedString) + before, after, _ := strings.Cut(result, expectedString) + result = before + after + } + + // verify that the result does not contain anything else other than what is expected. + assert.Empty(t, result) } func TestGetNamespaceYAML(t *testing.T) {