github.com/munnerz/test-infra@v0.0.0-20190108210205-ce3d181dc989/experiment/bootstrap/env_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 main 18 19 import ( 20 "os" 21 "testing" 22 "time" 23 ) 24 25 func TestSetDefaultEnv(t *testing.T) { 26 key := "KEY" 27 valOne := "1" 28 valTwo := "2" 29 // make sure the env is not set 30 err := os.Unsetenv(key) 31 if err != nil { 32 t.Errorf("Failed to call os.Unsetenv to prepare test: %v", err) 33 } 34 // assert that it is set by SetDefaultEnv 35 set, err := SetDefaultEnv(key, valOne) 36 if err != nil { 37 t.Errorf("encountered unexpected error calling SetDefaultEnv: %v", err) 38 } else if !set { 39 t.Errorf("SetDefaultEnv returned false, expected true") 40 } 41 // assert that val2 is not set once val1 has been set 42 set, err = SetDefaultEnv(key, valTwo) 43 if err != nil { 44 t.Errorf("encountered unexpected error calling SetDefaultEnv: %v", err) 45 } else if set { 46 t.Errorf("SetDefaultEnv returned true, expected false") 47 } 48 } 49 50 func TestEnvEqual(t *testing.T) { 51 keyEqualOne := "EQUAL1" 52 keyEqualTwo := "EQUAL2" 53 keyNotEqual := "NOT-EQUAL" 54 valEqual := "1" 55 valNotEqual := "3" 56 // initialize env so that keyEqualOne and keyEqualTwo are set to valEqual 57 // and keyNotEqual is set to valNotEqual 58 err := os.Setenv(keyEqualOne, valEqual) 59 if err != nil { 60 t.Errorf("Failed to call os.SetEnv to prepare test: %v", err) 61 } 62 err = os.Setenv(keyEqualTwo, valEqual) 63 if err != nil { 64 t.Errorf("Failed to call os.SetEnv to prepare test: %v", err) 65 } 66 err = os.Setenv(keyNotEqual, valNotEqual) 67 if err != nil { 68 t.Errorf("Failed to call os.SetEnv to prepare test: %v", err) 69 } 70 // assert that the keys are equal to each other 71 equal := EnvEqual(keyEqualOne, keyEqualTwo) 72 if !equal { 73 t.Errorf("EnvEqual returned false, expected true") 74 } 75 // assert that the keys are equal to themselves 76 equal = EnvEqual(keyEqualOne, keyEqualOne) 77 if !equal { 78 t.Errorf("EnvEqual returned false, expected true") 79 } 80 equal = EnvEqual(keyEqualTwo, keyEqualTwo) 81 if !equal { 82 t.Errorf("EnvEqual returned false, expected true") 83 } 84 // assert that neither of the equal keys matches the third key 85 equal = EnvEqual(keyEqualOne, keyNotEqual) 86 if equal { 87 t.Errorf("EnvEqual returned true, expected false") 88 } 89 // assert that neither of the equal keys maHomeEnvtches the third key 90 equal = EnvEqual(keyEqualTwo, keyNotEqual) 91 if equal { 92 t.Errorf("EnvEqual returned true, expected false") 93 } 94 } 95 96 func TestSetupMagicEnviroment(t *testing.T) { 97 job := "some-foo-job" 98 cwd, err := os.Getwd() 99 if err != nil { 100 t.Errorf("got an unexpected error calling os.Getwd to setup test: %v", err) 101 } 102 103 // setup a bogus job env to make sure we override it in SetupMagiEnvironment 104 err = os.Setenv(JobEnv, "a-ridiculous-value-nobody-would-use") 105 if err != nil { 106 t.Errorf("got an unexpected error calling os.Setenv to setup test: %v", err) 107 } 108 109 // pretend to be on jenkins 110 err = os.Setenv(JenkinsHomeEnv, os.Getenv(HomeEnv)) 111 if err != nil { 112 t.Errorf("got an unexpected error calling os.Setenv to setup test: %v", err) 113 } 114 115 // make sure other keys are not set 116 err = os.Unsetenv(GCEPrivKeyEnv) 117 if err != nil { 118 t.Errorf("got an unexpected error calling os.Unsetenv to setup test: %v", err) 119 } 120 err = os.Unsetenv(GCEPubKeyEnv) 121 if err != nil { 122 t.Errorf("got an unexpected error calling os.Unsetenv to setup test: %v", err) 123 } 124 err = os.Unsetenv(AWSPrivKeyEnv) 125 if err != nil { 126 t.Errorf("got an unexpected error calling os.Unsetenv to setup test: %v", err) 127 } 128 err = os.Unsetenv(AWSPubKeyEnv) 129 if err != nil { 130 t.Errorf("got an unexpected error calling os.Unsetenv to setup test: %v", err) 131 } 132 133 // make sure we don't get any errors and then assert the magic environment 134 err = SetupMagicEnviroment(job) 135 if err != nil { 136 t.Errorf("got an unexpected error calling SetupMagicEnviroment: %v", err) 137 } 138 139 // assert that we set env[JobEnv] = job 140 JobVal := os.Getenv(JobEnv) 141 if JobVal != job { 142 t.Errorf("expected os.GetEnv(%s) to be %s but got %s", JobEnv, job, JobVal) 143 } 144 // assert that JenkinsHomEnv != HomeEnv 145 if EnvEqual(JenkinsHomeEnv, HomeEnv) { 146 t.Errorf("EnvEqual(%s, %s) returned true but should be false", JenkinsHomeEnv, HomeEnv) 147 } 148 // assert that the ssh key envs are set 149 // TODO(bentheelder): should we also compute the exact values here and compare? 150 valGCEPrivKeyEnv := os.Getenv(GCEPrivKeyEnv) 151 if valGCEPrivKeyEnv == "" { 152 t.Errorf("Expected os.Getenv(`%s`) to not be ``", GCEPrivKeyEnv) 153 } 154 valGCEPubKeyEnv := os.Getenv(GCEPubKeyEnv) 155 if valGCEPubKeyEnv == "" { 156 t.Errorf("Expected os.Getenv(`%s`) to not be ``", GCEPubKeyEnv) 157 } 158 valAWSPrivKeyEnv := os.Getenv(AWSPrivKeyEnv) 159 if valAWSPrivKeyEnv == "" { 160 t.Errorf("Expected os.Getenv(`%s`) to not be ``", AWSPrivKeyEnv) 161 } 162 valAWSPubKeyEnv := os.Getenv(AWSPubKeyEnv) 163 if valAWSPubKeyEnv == "" { 164 t.Errorf("Expected os.Getenv(`%s`) to not be ``", AWSPubKeyEnv) 165 } 166 // assert that cloud sdk env is set 167 valCloudSDKEnv := os.Getenv(CloudSDKEnv) 168 if valCloudSDKEnv == "" { 169 t.Errorf("Expected os.Getenv(`%s`) to not be ``", CloudSDKEnv) 170 } 171 // assert that BootstrapEnv is "yes" 172 valBootstrapEnv := os.Getenv(BootstrapEnv) 173 if valBootstrapEnv != "yes" { 174 t.Errorf("Expected os.Getenv(`%s`) to be \"yes\" not %#v", BootstrapEnv, valBootstrapEnv) 175 } 176 // assert workspace env 177 valWorkspaceEnv := os.Getenv(WorkspaceEnv) 178 if valWorkspaceEnv != cwd { 179 t.Errorf("Expected os.Getenv(`%s`) == os.Getwd()", WorkspaceEnv) 180 } 181 } 182 183 func TestNodeName(t *testing.T) { 184 // TODO(bentheelder): improve this test? 185 name, err := NodeName() 186 if err != nil { 187 t.Errorf("Got an unexpected err running NodeName: %v", err) 188 } 189 valNodeNameEnv := os.Getenv(NodeNameEnv) 190 if valNodeNameEnv != name { 191 t.Errorf("Expected os.Getenv(`%s`) to equal %#v not %#v", NodeNameEnv, name, valNodeNameEnv) 192 } 193 } 194 195 func TestBuildName(t *testing.T) { 196 // TODO(bentheelder): improve this test? 197 name, err := BuildName(time.Unix(0, 0)) 198 if err != nil { 199 t.Errorf("Got an unexpected err running BuildName: %v", err) 200 } 201 if name == "" { 202 t.Errorf("Expected a non-empty build name") 203 } 204 valBuildNumberEnv := os.Getenv(BuildNumberEnv) 205 if valBuildNumberEnv != name { 206 t.Errorf("Expected os.Getenv(`%s`) to equal %#v not %#v", BuildNumberEnv, name, valBuildNumberEnv) 207 } 208 }