github.com/databricks/cli@v0.203.0/bundle/deploy/terraform/init_test.go (about) 1 package terraform 2 3 import ( 4 "context" 5 "os" 6 "os/exec" 7 "runtime" 8 "strings" 9 "testing" 10 11 "github.com/databricks/cli/bundle" 12 "github.com/databricks/cli/bundle/config" 13 "github.com/stretchr/testify/assert" 14 "github.com/stretchr/testify/require" 15 "golang.org/x/exp/maps" 16 ) 17 18 func unsetEnv(t *testing.T, name string) { 19 t.Setenv(name, "") 20 err := os.Unsetenv(name) 21 require.NoError(t, err) 22 } 23 24 func TestInitEnvironmentVariables(t *testing.T) { 25 _, err := exec.LookPath("terraform") 26 if err != nil { 27 t.Skipf("cannot find terraform binary: %s", err) 28 } 29 30 bundle := &bundle.Bundle{ 31 Config: config.Root{ 32 Path: t.TempDir(), 33 Bundle: config.Bundle{ 34 Environment: "whatever", 35 Terraform: &config.Terraform{ 36 ExecPath: "terraform", 37 }, 38 }, 39 }, 40 } 41 42 // Trigger initialization of workspace client. 43 // TODO(pietern): create test fixture that initializes a mocked client. 44 t.Setenv("DATABRICKS_HOST", "https://x") 45 t.Setenv("DATABRICKS_TOKEN", "foobar") 46 bundle.WorkspaceClient() 47 48 err = Initialize().Apply(context.Background(), bundle) 49 require.NoError(t, err) 50 } 51 52 func TestSetTempDirEnvVarsForUnixWithTmpDirSet(t *testing.T) { 53 if runtime.GOOS != "darwin" && runtime.GOOS != "linux" { 54 t.SkipNow() 55 } 56 57 b := &bundle.Bundle{ 58 Config: config.Root{ 59 Path: t.TempDir(), 60 Bundle: config.Bundle{ 61 Environment: "whatever", 62 }, 63 }, 64 } 65 66 // Set TMPDIR environment variable 67 t.Setenv("TMPDIR", "/foo/bar") 68 69 // compute env 70 env := make(map[string]string, 0) 71 err := setTempDirEnvVars(env, b) 72 require.NoError(t, err) 73 74 // Assert that we pass through TMPDIR. 75 assert.Equal(t, map[string]string{ 76 "TMPDIR": "/foo/bar", 77 }, env) 78 } 79 80 func TestSetTempDirEnvVarsForUnixWithTmpDirNotSet(t *testing.T) { 81 if runtime.GOOS != "darwin" && runtime.GOOS != "linux" { 82 t.SkipNow() 83 } 84 85 b := &bundle.Bundle{ 86 Config: config.Root{ 87 Path: t.TempDir(), 88 Bundle: config.Bundle{ 89 Environment: "whatever", 90 }, 91 }, 92 } 93 94 // Unset TMPDIR environment variable confirm it's not set 95 unsetEnv(t, "TMPDIR") 96 97 // compute env 98 env := make(map[string]string, 0) 99 err := setTempDirEnvVars(env, b) 100 require.NoError(t, err) 101 102 // Assert that we don't pass through TMPDIR. 103 assert.Equal(t, map[string]string{}, env) 104 } 105 106 func TestSetTempDirEnvVarsForWindowWithAllTmpDirEnvVarsSet(t *testing.T) { 107 if runtime.GOOS != "windows" { 108 t.SkipNow() 109 } 110 111 b := &bundle.Bundle{ 112 Config: config.Root{ 113 Path: t.TempDir(), 114 Bundle: config.Bundle{ 115 Environment: "whatever", 116 }, 117 }, 118 } 119 120 // Set environment variables 121 t.Setenv("TMP", "c:\\foo\\a") 122 t.Setenv("TEMP", "c:\\foo\\b") 123 t.Setenv("USERPROFILE", "c:\\foo\\c") 124 125 // compute env 126 env := make(map[string]string, 0) 127 err := setTempDirEnvVars(env, b) 128 require.NoError(t, err) 129 130 // assert that we pass through the highest priority env var value 131 assert.Equal(t, map[string]string{ 132 "TMP": "c:\\foo\\a", 133 }, env) 134 } 135 136 func TestSetTempDirEnvVarsForWindowWithUserProfileAndTempSet(t *testing.T) { 137 if runtime.GOOS != "windows" { 138 t.SkipNow() 139 } 140 141 b := &bundle.Bundle{ 142 Config: config.Root{ 143 Path: t.TempDir(), 144 Bundle: config.Bundle{ 145 Environment: "whatever", 146 }, 147 }, 148 } 149 150 // Set environment variables 151 unsetEnv(t, "TMP") 152 t.Setenv("TEMP", "c:\\foo\\b") 153 t.Setenv("USERPROFILE", "c:\\foo\\c") 154 155 // compute env 156 env := make(map[string]string, 0) 157 err := setTempDirEnvVars(env, b) 158 require.NoError(t, err) 159 160 // assert that we pass through the highest priority env var value 161 assert.Equal(t, map[string]string{ 162 "TEMP": "c:\\foo\\b", 163 }, env) 164 } 165 166 func TestSetTempDirEnvVarsForWindowWithUserProfileSet(t *testing.T) { 167 if runtime.GOOS != "windows" { 168 t.SkipNow() 169 } 170 171 b := &bundle.Bundle{ 172 Config: config.Root{ 173 Path: t.TempDir(), 174 Bundle: config.Bundle{ 175 Environment: "whatever", 176 }, 177 }, 178 } 179 180 // Set environment variables 181 unsetEnv(t, "TMP") 182 unsetEnv(t, "TEMP") 183 t.Setenv("USERPROFILE", "c:\\foo\\c") 184 185 // compute env 186 env := make(map[string]string, 0) 187 err := setTempDirEnvVars(env, b) 188 require.NoError(t, err) 189 190 // assert that we pass through the user profile 191 assert.Equal(t, map[string]string{ 192 "USERPROFILE": "c:\\foo\\c", 193 }, env) 194 } 195 196 func TestSetTempDirEnvVarsForWindowsWithoutAnyTempDirEnvVarsSet(t *testing.T) { 197 if runtime.GOOS != "windows" { 198 t.SkipNow() 199 } 200 201 b := &bundle.Bundle{ 202 Config: config.Root{ 203 Path: t.TempDir(), 204 Bundle: config.Bundle{ 205 Environment: "whatever", 206 }, 207 }, 208 } 209 210 // unset all env vars 211 unsetEnv(t, "TMP") 212 unsetEnv(t, "TEMP") 213 unsetEnv(t, "USERPROFILE") 214 215 // compute env 216 env := make(map[string]string, 0) 217 err := setTempDirEnvVars(env, b) 218 require.NoError(t, err) 219 220 // assert TMP is set to b.CacheDir("tmp") 221 tmpDir, err := b.CacheDir("tmp") 222 require.NoError(t, err) 223 assert.Equal(t, map[string]string{ 224 "TMP": tmpDir, 225 }, env) 226 } 227 228 func TestSetProxyEnvVars(t *testing.T) { 229 b := &bundle.Bundle{ 230 Config: config.Root{ 231 Path: t.TempDir(), 232 Bundle: config.Bundle{ 233 Environment: "whatever", 234 }, 235 }, 236 } 237 238 // Temporarily clear environment variables. 239 clearEnv := func() { 240 for _, v := range []string{"http_proxy", "https_proxy", "no_proxy"} { 241 for _, v := range []string{strings.ToUpper(v), strings.ToLower(v)} { 242 t.Setenv(v, "foo") 243 os.Unsetenv(v) 244 } 245 } 246 } 247 248 // No proxy env vars set. 249 clearEnv() 250 env := make(map[string]string, 0) 251 err := setProxyEnvVars(env, b) 252 require.NoError(t, err) 253 assert.Len(t, env, 0) 254 255 // Lower case set. 256 clearEnv() 257 t.Setenv("http_proxy", "foo") 258 t.Setenv("https_proxy", "foo") 259 t.Setenv("no_proxy", "foo") 260 env = make(map[string]string, 0) 261 err = setProxyEnvVars(env, b) 262 require.NoError(t, err) 263 assert.ElementsMatch(t, []string{"HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY"}, maps.Keys(env)) 264 265 // Upper case set. 266 clearEnv() 267 t.Setenv("HTTP_PROXY", "foo") 268 t.Setenv("HTTPS_PROXY", "foo") 269 t.Setenv("NO_PROXY", "foo") 270 env = make(map[string]string, 0) 271 err = setProxyEnvVars(env, b) 272 require.NoError(t, err) 273 assert.ElementsMatch(t, []string{"HTTP_PROXY", "HTTPS_PROXY", "NO_PROXY"}, maps.Keys(env)) 274 } 275 276 func TestInheritEnvVars(t *testing.T) { 277 env := map[string]string{} 278 279 t.Setenv("HOME", "/home/testuser") 280 t.Setenv("TF_CLI_CONFIG_FILE", "/tmp/config.tfrc") 281 282 err := inheritEnvVars(env) 283 284 require.NoError(t, err) 285 286 require.Equal(t, map[string]string{ 287 "HOME": "/home/testuser", 288 "TF_CLI_CONFIG_FILE": "/tmp/config.tfrc", 289 }, env) 290 }