Pass through $HOME when invoking Terraform (#319)

## Changes

This is useful when developing the Databricks Terraform provider where
you keep a local-only build of the provider and refer to it using $HOME
from `~/.terraformrc`, for example like this:

```
plugin_cache_dir = "$HOME/.terraform.d/plugin-cache"
```

## Tests

That $HOME is passed through cannot be tested as is because the
`tfexec.Terraform` struct doesn't expose it through public fields or
methods. What can be tested is a successful run of the initialize
mutator and this is included in this commit.
This commit is contained in:
Pieter Noordhuis 2023-04-11 13:11:31 +02:00 committed by GitHub
parent 2a0f2f70b7
commit 42d29f92c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 0 deletions

View File

@ -96,6 +96,12 @@ func (m *initialize) Apply(ctx context.Context, b *bundle.Bundle) ([]bundle.Muta
return nil, err return nil, err
} }
// Include $HOME in set of environment variables to pass along.
home, ok := os.LookupEnv("HOME")
if ok {
env["HOME"] = home
}
// Configure environment variables for auth for Terraform to use. // Configure environment variables for auth for Terraform to use.
log.Debugf(ctx, "Environment variables for Terraform: %s", strings.Join(maps.Keys(env), ", ")) log.Debugf(ctx, "Environment variables for Terraform: %s", strings.Join(maps.Keys(env), ", "))
err = tf.SetEnv(env) err = tf.SetEnv(env)

View File

@ -0,0 +1,39 @@
package terraform
import (
"context"
"os/exec"
"testing"
"github.com/databricks/bricks/bundle"
"github.com/databricks/bricks/bundle/config"
"github.com/stretchr/testify/require"
)
func TestInitEnvironmentVariables(t *testing.T) {
_, err := exec.LookPath("terraform")
if err != nil {
t.Skipf("cannot find terraform binary: %s", err)
}
bundle := &bundle.Bundle{
Config: config.Root{
Path: t.TempDir(),
Bundle: config.Bundle{
Environment: "whatever",
Terraform: &config.Terraform{
ExecPath: "terraform",
},
},
},
}
// Trigger initialization of workspace client.
// TODO(pietern): create test fixture that initializes a mocked client.
t.Setenv("DATABRICKS_HOST", "https://x")
t.Setenv("DATABRICKS_TOKEN", "foobar")
bundle.WorkspaceClient()
_, err = Initialize().Apply(context.Background(), bundle)
require.NoError(t, err)
}