1// Copyright 2019 Istio Authors 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15// Package env makes it possible to track use of environment variables within procress 16// in order to generate documentation for these uses. 17package env 18 19import ( 20 "os" 21 "testing" 22 "time" 23) 24 25const testVar = "TESTXYZ" 26 27func reset() { 28 _ = os.Unsetenv(testVar) 29 mutex.Lock() 30 allVars = make(map[string]Var) 31 mutex.Unlock() 32} 33 34func TestString(t *testing.T) { 35 reset() 36 37 ev := RegisterStringVar(testVar, "123", "") 38 v, present := ev.Lookup() 39 if v != "123" { 40 t.Errorf("Expected 123, got %s", v) 41 } 42 if present { 43 t.Errorf("Expected not present") 44 } 45 46 v = ev.Get() 47 if v != "123" { 48 t.Errorf("Expected 123, got %s", v) 49 } 50 51 _ = os.Setenv(testVar, "ABC") 52 53 ev = RegisterStringVar(testVar, "123", "") 54 v, present = ev.Lookup() 55 if v != "ABC" { 56 t.Errorf("Expected ABC, got %s", v) 57 } 58 if !present { 59 t.Errorf("Expected present") 60 } 61 62 v = ev.Get() 63 if v != "ABC" { 64 t.Errorf("Expected ABC, got %s", v) 65 } 66} 67 68func TestInt(t *testing.T) { 69 reset() 70 71 ev := RegisterIntVar(testVar, 123, "") 72 v, present := ev.Lookup() 73 if v != 123 { 74 t.Errorf("Expected 123, got %v", v) 75 } 76 if present { 77 t.Errorf("Expected not present") 78 } 79 80 v = ev.Get() 81 if v != 123 { 82 t.Errorf("Expected 123, got %v", v) 83 } 84 85 _ = os.Setenv(testVar, "XXX") 86 87 ev = RegisterIntVar(testVar, 123, "") 88 v, present = ev.Lookup() 89 if v != 123 { 90 t.Errorf("Expected 123, got %v", v) 91 } 92 if !present { 93 t.Errorf("Expected present") 94 } 95 96 v = ev.Get() 97 if v != 123 { 98 t.Errorf("Expected 123, got %v", v) 99 } 100 101 _ = os.Setenv(testVar, "789") 102 103 ev = RegisterIntVar(testVar, 123, "") 104 v, present = ev.Lookup() 105 if v != 789 { 106 t.Errorf("Expected 789, got %v", v) 107 } 108 if !present { 109 t.Errorf("Expected present") 110 } 111 112 v = ev.Get() 113 if v != 789 { 114 t.Errorf("Expected 789, got %v", v) 115 } 116} 117 118func TestBool(t *testing.T) { 119 reset() 120 121 ev := RegisterBoolVar(testVar, true, "") 122 v, present := ev.Lookup() 123 if v != true { 124 t.Errorf("Expected true, got %v", v) 125 } 126 if present { 127 t.Errorf("Expected not present") 128 } 129 130 v = ev.Get() 131 if v != true { 132 t.Errorf("Expected true, got %v", v) 133 } 134 135 _ = os.Setenv(testVar, "XXX") 136 137 ev = RegisterBoolVar(testVar, true, "") 138 v, present = ev.Lookup() 139 if v != true { 140 t.Errorf("Expected true, got %v", v) 141 } 142 if !present { 143 t.Errorf("Expected present") 144 } 145 146 v = ev.Get() 147 if v != true { 148 t.Errorf("Expected true, got %v", v) 149 } 150 151 _ = os.Setenv(testVar, "true") 152 153 ev = RegisterBoolVar(testVar, false, "") 154 v, present = ev.Lookup() 155 if v != true { 156 t.Errorf("Expected true, got %v", v) 157 } 158 if !present { 159 t.Errorf("Expected present") 160 } 161 162 v = ev.Get() 163 if v != true { 164 t.Errorf("Expected true, got %v", v) 165 } 166} 167 168func TestFloat(t *testing.T) { 169 reset() 170 171 ev := RegisterFloatVar(testVar, 123.0, "") 172 v, present := ev.Lookup() 173 if v != 123.0 { 174 t.Errorf("Expected 123.0, got %v", v) 175 } 176 if present { 177 t.Errorf("Expected not present") 178 } 179 180 v = ev.Get() 181 if v != 123.0 { 182 t.Errorf("Expected 123.0, got %v", v) 183 } 184 185 _ = os.Setenv(testVar, "XXX") 186 187 ev = RegisterFloatVar(testVar, 123.0, "") 188 v, present = ev.Lookup() 189 if v != 123.0 { 190 t.Errorf("Expected 123.0, got %v", v) 191 } 192 if !present { 193 t.Errorf("Expected present") 194 } 195 196 v = ev.Get() 197 if v != 123.0 { 198 t.Errorf("Expected 123.0, got %v", v) 199 } 200 201 _ = os.Setenv(testVar, "789") 202 203 ev = RegisterFloatVar(testVar, 123.0, "") 204 v, present = ev.Lookup() 205 if v != 789 { 206 t.Errorf("Expected 789.0, got %v", v) 207 } 208 if !present { 209 t.Errorf("Expected present") 210 } 211 212 v = ev.Get() 213 if v != 789 { 214 t.Errorf("Expected 789.0, got %v", v) 215 } 216} 217 218func TestDuration(t *testing.T) { 219 reset() 220 221 ev := RegisterDurationVar(testVar, 123*time.Second, "") 222 v, present := ev.Lookup() 223 if v != 123*time.Second { 224 t.Errorf("Expected 123 seconds, got %v", v) 225 } 226 if present { 227 t.Errorf("Expected not present") 228 } 229 230 v = ev.Get() 231 if v != 123*time.Second { 232 t.Errorf("Expected 123 seconds, got %v", v) 233 } 234 235 _ = os.Setenv(testVar, "XXX") 236 237 ev = RegisterDurationVar(testVar, 123*time.Second, "") 238 v, present = ev.Lookup() 239 if v != 123*time.Second { 240 t.Errorf("Expected 123 seconds, got %v", v) 241 } 242 if !present { 243 t.Errorf("Expected present") 244 } 245 246 v = ev.Get() 247 if v != 123*time.Second { 248 t.Errorf("Expected 123 seconds, got %v", v) 249 } 250 251 _ = os.Setenv(testVar, "789s") 252 253 ev = RegisterDurationVar(testVar, 123*time.Second, "") 254 v, present = ev.Lookup() 255 if v != 789*time.Second { 256 t.Errorf("Expected 789 seconds, got %v", v) 257 } 258 if !present { 259 t.Errorf("Expected present") 260 } 261 262 v = ev.Get() 263 if v != 789*time.Second { 264 t.Errorf("Expected 789 seconds, got %v", v) 265 } 266} 267 268func TestDesc(t *testing.T) { 269 reset() 270 271 _ = RegisterDurationVar(testVar+"5", 123*time.Second, "A duration") 272 _ = RegisterStringVar(testVar+"1", "123", "A string") 273 _ = RegisterIntVar(testVar+"2", 456, "An int") 274 _ = RegisterBoolVar(testVar+"3", true, "A bool") 275 _ = RegisterFloatVar(testVar+"4", 789.0, "A float") 276 277 vars := VarDescriptions() 278 if vars[0].Name != "TESTXYZ1" { 279 t.Errorf("Expecting TESTXYZ1, got %s", vars[0].Name) 280 } 281 if vars[0].Description != "A string" { 282 t.Errorf("Expected 'A string', got '%s'", vars[0].Description) 283 } 284 285 if vars[1].Name != "TESTXYZ2" { 286 t.Errorf("Expecting TESTXYZ2, got %s", vars[0].Name) 287 } 288 if vars[1].Description != "An int" { 289 t.Errorf("Expected 'An int', got '%s'", vars[0].Description) 290 } 291 292 if vars[2].Name != "TESTXYZ3" { 293 t.Errorf("Expecting TESTXYZ3, got %s", vars[0].Name) 294 } 295 if vars[2].Description != "A bool" { 296 t.Errorf("Expected 'A bool', got '%s'", vars[0].Description) 297 } 298 299 if vars[3].Name != "TESTXYZ4" { 300 t.Errorf("Expecting TESTXYZ4, got %s", vars[0].Name) 301 } 302 if vars[3].Description != "A float" { 303 t.Errorf("Expected 'A float', got '%s'", vars[0].Description) 304 } 305 306 if vars[4].Name != "TESTXYZ5" { 307 t.Errorf("Expecting TESTXYZ5, got %s", vars[0].Name) 308 } 309 if vars[4].Description != "A duration" { 310 t.Errorf("Expected 'A duration', got '%s'", vars[0].Description) 311 } 312} 313 314func TestDupes(t *testing.T) { 315 316 // make sure var without a description doesn't overwrite one with 317 reset() 318 _ = RegisterStringVar(testVar, "123", "XYZ") 319 v := RegisterStringVar(testVar, "123", "") 320 if v.Description != "XYZ" { 321 t.Errorf("Expected 'XYZ', got '%s'", v.Description) 322 } 323 324 // make sure var without a description doesn't overwrite one with 325 reset() 326 _ = RegisterStringVar(testVar, "123", "") 327 v = RegisterStringVar(testVar, "123", "XYZ") 328 if v.Description != "XYZ" { 329 t.Errorf("Expected 'XYZ', got '%s'", v.Description) 330 } 331} 332