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