github.com/maxgio92/test-infra@v0.1.0/kubetest/util/util_test.go (about) 1 /* 2 Copyright 2017 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 util 18 19 import ( 20 "os" 21 "strings" 22 "testing" 23 ) 24 25 func TestPushEnv(t *testing.T) { 26 env := "fake-env" 27 empty := "" 28 filled := "initial" 29 cases := []struct { 30 name string 31 initial *string 32 pushed string 33 }{ 34 { 35 name: "initial-missing-popped-missing", 36 pushed: "hello", 37 }, 38 { 39 name: "initial-empty-popped-empty", 40 initial: &empty, 41 pushed: "hello", 42 }, 43 { 44 name: "initial-set-popped-set", 45 initial: &filled, 46 pushed: "hello", 47 }, 48 } 49 for _, tc := range cases { 50 if tc.initial == nil { 51 if err := os.Unsetenv(env); err != nil { 52 t.Fatalf("%s: could not unset %s: %v", tc.name, env, err) 53 } 54 } else { 55 if err := os.Setenv(env, *tc.initial); err != nil { 56 t.Fatalf("%s: could not set %s: %v", tc.name, env, err) 57 } 58 } 59 f, err := PushEnv(env, tc.pushed) 60 if err != nil { 61 t.Errorf("%s: push error: %v", tc.name, err) 62 continue 63 } 64 actual, present := os.LookupEnv(env) 65 if !present { 66 t.Errorf("%s: failed to push %s", tc.name, tc.pushed) 67 continue 68 } 69 if actual != tc.pushed { 70 t.Errorf("%s: actual %s != expected %s", tc.name, actual, tc.pushed) 71 continue 72 } 73 if err = f(); err != nil { 74 t.Errorf("%s: pop error: %v", tc.name, err) 75 } 76 actual, present = os.LookupEnv(env) 77 if tc.initial == nil && present { 78 t.Errorf("%s: env present after popping", tc.name) 79 continue 80 } else if tc.initial != nil && *tc.initial != actual { 81 t.Errorf("%s: popped env is %s not initial %s", tc.name, actual, *tc.initial) 82 } 83 } 84 85 } 86 87 func TestMigrateOptions(t *testing.T) { 88 ov := "option-value" 89 ev := "env-value" 90 91 cases := []struct { 92 name string 93 setEnv bool 94 setOption bool 95 push bool 96 expectedEnv *string 97 expectedOption string 98 }{ 99 { 100 name: "no flag or env results in no change", 101 }, 102 { 103 name: "flag and env, no push results in no change", 104 setEnv: true, 105 setOption: true, 106 expectedEnv: &ev, 107 expectedOption: ov, 108 }, 109 { 110 name: "flag and env, push overwrites env", 111 setEnv: true, 112 setOption: true, 113 push: true, 114 expectedEnv: &ov, 115 expectedOption: ov, 116 }, 117 { 118 name: "flag and no env, no push results in no change", 119 setOption: true, 120 expectedOption: ov, 121 }, 122 { 123 name: "flag and no env, push overwrites env", 124 setOption: true, 125 push: true, 126 expectedEnv: &ov, 127 expectedOption: ov, 128 }, 129 { 130 name: "no flag and env overwrites option", 131 setEnv: true, 132 expectedEnv: &ev, 133 expectedOption: ev, 134 }, 135 } 136 137 env := "random-env" 138 139 for _, tc := range cases { 140 if tc.setEnv { 141 if err := os.Setenv(env, ev); err != nil { 142 t.Fatalf("%s: %v", tc.name, err) 143 } 144 } else if err := os.Unsetenv(env); err != nil { 145 t.Fatalf("%s: %v", tc.name, err) 146 } 147 148 opt := "" 149 if tc.setOption { 150 opt = ov 151 } 152 if err := MigrateOptions([]MigratedOption{ 153 { 154 Env: env, 155 Option: &opt, 156 Name: "--random-flag", 157 SkipPush: !tc.push, 158 }, 159 }); err != nil { 160 t.Fatalf("%s: %v", tc.name, err) 161 } 162 163 val, present := os.LookupEnv(env) 164 if present && tc.expectedEnv == nil { 165 t.Errorf("%s: env should not be set", tc.name) 166 } else if tc.expectedEnv != nil && !present { 167 t.Errorf("%s: env should be set", tc.name) 168 } else if tc.expectedEnv != nil && val != *tc.expectedEnv { 169 t.Errorf("%s: env actual %s != expected %s", tc.name, val, *tc.expectedEnv) 170 } 171 172 if tc.expectedOption != opt { 173 t.Errorf("%s: option actual %s != expected %s", tc.name, opt, tc.expectedOption) 174 } 175 } 176 } 177 178 func TestAppendField(t *testing.T) { 179 flag := "--target" 180 add := "hello" 181 cases := []struct { 182 name string 183 start string 184 expected string 185 }{ 186 { 187 name: "missing", 188 start: "--a=1 --b=2", 189 expected: "--a=1 --b=2 --target=hello", 190 }, 191 { 192 name: "empty", 193 start: "--target= --b=2", 194 expected: "--b=2 --target=hello", 195 }, 196 { 197 name: "set", 198 start: "--target=first --b=2", 199 expected: "--b=2 --target=first-hello", 200 }, 201 } 202 203 for _, tc := range cases { 204 actual := strings.Join(AppendField(strings.Fields(tc.start), flag, add), " ") 205 if actual != tc.expected { 206 t.Errorf("%s: actual %s != expected %s", tc.name, actual, tc.expected) 207 } 208 } 209 } 210 211 func TestSetFieldDefault(t *testing.T) { 212 flag := "--target" 213 def := "default-value" 214 cases := []struct { 215 name string 216 start string 217 expected string 218 }{ 219 { 220 name: "missing", 221 start: "--a 1 --b 2", 222 expected: "--a 1 --b 2 --target=default-value", 223 }, 224 { 225 name: "empty", 226 start: "--target= --b=2", 227 expected: "--b=2 --target=", 228 }, 229 { 230 name: "set", 231 start: "--target=1 --b=2", 232 expected: "--b=2 --target=1", 233 }, 234 } 235 236 for _, tc := range cases { 237 actual := strings.Join(SetFieldDefault(strings.Fields(tc.start), flag, def), " ") 238 if actual != tc.expected { 239 t.Errorf("%s: actual %s != expected %s", tc.name, actual, tc.expected) 240 } 241 } 242 } 243 244 func TestExtractField(t *testing.T) { 245 cases := []struct { 246 name string 247 start string 248 target string 249 out string 250 extracted string 251 found bool 252 }{ 253 { 254 name: "not present", 255 start: "--a=1 --b=2 --c=3", 256 target: "--missing", 257 out: "--a=1 --b=2 --c=3", 258 extracted: "", 259 found: false, 260 }, 261 { 262 name: "found filled", 263 start: "--a=1 --b=2 --c=3", 264 target: "--b", 265 out: "--a=1 --c=3", 266 extracted: "2", 267 found: true, 268 }, 269 { 270 name: "found empty", 271 start: "--a=1 --b= --c=3", 272 target: "--b", 273 out: "--a=1 --c=3", 274 extracted: "", 275 found: true, 276 }, 277 { 278 name: "found space instead of =", 279 start: "--a 1 --b 2 --c=3", 280 target: "--b", 281 out: "--a 1 --c=3", 282 extracted: "2", 283 found: true, 284 }, 285 } 286 for _, tc := range cases { 287 f, extracted, found := ExtractField(strings.Fields(tc.start), tc.target) 288 out := strings.Join(f, " ") 289 if out != tc.out { 290 t.Errorf("%s: actual fields %s != expected %s", tc.name, out, tc.out) 291 } 292 if extracted != tc.extracted { 293 t.Errorf("%s: actual extracted %s != expected %s", tc.name, extracted, tc.extracted) 294 } 295 if found != tc.found { 296 t.Errorf("%s: actual found %t != expected %t", tc.name, found, tc.found) 297 } 298 } 299 }