k8s.io/perf-tests/clusterloader2@v0.0.0-20240304094227-64bdb12da87e/pkg/modifier/modifier_test.go (about) 1 /* 2 Copyright 2020 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package modifier 18 19 import ( 20 "reflect" 21 "testing" 22 23 "k8s.io/perf-tests/clusterloader2/api" 24 ) 25 26 func defaultConfig() *api.Config { 27 return &api.Config{ 28 Name: "test-modifier", 29 Steps: []*api.Step{{ 30 Name: "eternal", 31 }}, 32 } 33 } 34 35 func TestModifySkipSteps(t *testing.T) { 36 m := &simpleModifier{skipSteps: []string{"skip-me", "skip me"}} 37 testCase := [][]string{{}, {"skip-me"}, {"skip-me", "skip me"}} 38 for _, d := range testCase { 39 c := defaultConfig() 40 for _, s := range d { 41 c.Steps = append(c.Steps, &api.Step{Name: s}) 42 } 43 m.modifySkipSteps(c) 44 if len(c.Steps) != 1 { 45 t.Errorf("For test case %v: Changed test config in unexpected way, expected to have 1 step, but was %d steps", d, len(c.Steps)) 46 } 47 if c.Steps[0].Name != "eternal" { 48 t.Errorf("For test case %v: Changed test in unexpected way, expected to have 1 step with name 'eternal', but was %s", d, c.Steps[0].Name) 49 } 50 } 51 } 52 53 func TestModifyOverwrite(t *testing.T) { 54 testCase := []struct { 55 overwrite []string 56 expected *api.Config 57 err string 58 }{ 59 {overwrite: []string{}, expected: defaultConfig()}, 60 { 61 overwrite: []string{"Namespace.Prefix=overwritten-prefix"}, 62 expected: &api.Config{ 63 Name: "test-modifier", 64 Steps: []*api.Step{{ 65 Name: "eternal", 66 }}, 67 Namespace: api.NamespaceConfig{ 68 Prefix: "overwritten-prefix", 69 }}, 70 }, 71 { 72 overwrite: []string{"Namespace.EnableExistingNamespaces=true"}, 73 expected: &api.Config{ 74 Name: "test-modifier", 75 Steps: []*api.Step{{ 76 Name: "eternal", 77 }}, 78 Namespace: api.NamespaceConfig{ 79 // Golang does not allow to take a pointer to '&true' 80 EnableExistingNamespaces: &[]bool{true}[0], 81 }}, 82 }, 83 { 84 overwrite: []string{"Namespace.Number=42"}, 85 expected: &api.Config{ 86 Name: "test-modifier", 87 Steps: []*api.Step{{ 88 Name: "eternal", 89 }}, 90 Namespace: api.NamespaceConfig{ 91 Number: 42, 92 }}, 93 }, 94 { 95 overwrite: []string{"Namespace.Number=214748364800"}, 96 err: "test config overwrite error: Cannot parse '214748364800' for key 'Namespace.Number' to int: strconv.ParseInt: parsing \"214748364800\": value out of range", 97 }, 98 { 99 overwrite: []string{"NotExistingParameter=123"}, 100 err: "cannot overwrite config for key 'NotExistingParameter'. Path does not exist", 101 }, 102 { 103 overwrite: []string{"NotAPair"}, 104 err: "not a key=value pair: 'NotAPair'", 105 }, 106 } 107 for _, d := range testCase { 108 m := &simpleModifier{overwriteTestConfig: d.overwrite} 109 c := defaultConfig() 110 err := m.modifyOverwrite(c) 111 if d.expected != nil { 112 if err != nil { 113 t.Errorf("For test case %v: Expected succes, however error %v happened", d.overwrite, err) 114 } 115 if !reflect.DeepEqual(c, d.expected) { 116 t.Errorf("For test case %v: Changed test in unexpected way, was '%v' but expected '%v'", d.overwrite, c, d.expected) 117 } 118 } else { 119 if err == nil { 120 t.Errorf("For test case %v: Expected error, however it did not happen", d.overwrite) 121 } 122 if err.Error() != d.err { 123 t.Errorf("For test case %v: Returned unexpected error '%s', expected error '%s'", d.overwrite, err, d.err) 124 } 125 } 126 } 127 }