fix minor regression in tests (#37570)

At one point, these tests were explicitly passing an empty cli config filepath so that terraform would ignore any existing cli config file (only relevant if you are running these tests locally), but the behavior changed over time such that it was no longer working for these tests (now if the env var file path is empty, we fall back to the default). The actual behavior is reasonably correct (I've depended on passing in blank config file paths before, so I don't love it, but I don't see a need to break this either) so I've added a small method that drops a blank file in place to fix the behavior and avoid file not found errors.
pull/37571/head
Kristin Laemmert 7 months ago committed by GitHub
parent f574c9cac9
commit cf9c491d9f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -112,6 +112,12 @@ func TestInitProvidersVendored(t *testing.T) {
t.Fatalf("unexpected error: %s", err)
}
// "zero out" any existing cli config file by passing in an empty override
// file.
configFile := emptyConfigFileForTests(t, wantMachineDir)
tf.AddEnv(fmt.Sprintf("TF_CLI_CONFIG_FILE=%s", configFile))
defer os.Remove(configFile)
stdout, stderr, err := tf.Run("init")
if err != nil {
t.Errorf("unexpected error: %s", err)
@ -127,7 +133,6 @@ func TestInitProvidersVendored(t *testing.T) {
if !strings.Contains(stdout, "- Installing hashicorp/null v1.0.0+local") {
t.Errorf("provider download message is missing from output:\n%s", stdout)
t.Logf("(this can happen if you have a copy of the plugin in one of the global plugin search dirs)")
}
}
@ -144,12 +149,6 @@ func TestInitProvidersLocalOnly(t *testing.T) {
fixturePath := filepath.Join("testdata", "local-only-provider")
tf := e2e.NewBinary(t, terraformBin, fixturePath)
// If you run this test on a workstation with a plugin-cache directory
// configured, it will leave a bad directory behind and terraform init will
// not work until you remove it.
//
// To avoid this, we will "zero out" any existing cli config file.
tf.AddEnv("TF_CLI_CONFIG_FILE=")
// Our fixture dir has a generic os_arch dir, which we need to customize
// to the actual OS/arch where this test is running in order to get the
@ -161,6 +160,16 @@ func TestInitProvidersLocalOnly(t *testing.T) {
t.Fatalf("unexpected error: %s", err)
}
// If you run this test on a workstation with a plugin-cache directory
// configured, it will leave a bad directory behind and terraform init will
// not work until you remove it.
//
// To avoid this, we will "zero out" any existing cli config file by
// passing in an empty override file.
configFile := emptyConfigFileForTests(t, wantMachineDir)
defer os.Remove(configFile)
tf.AddEnv(fmt.Sprintf("TF_CLI_CONFIG_FILE=%s", configFile))
stdout, stderr, err := tf.Run("init")
if err != nil {
t.Errorf("unexpected error: %s", err)
@ -409,3 +418,16 @@ func TestInitProviderWarnings(t *testing.T) {
}
}
// emptyConfigFileForTests creates a blank .terraformrc file in the requested
// path and returns the path to the new file. It is the caller's responsibility
// to cleanup the file after use.
func emptyConfigFileForTests(t testing.TB, path string) string {
// "zero out" any existing cli config file by passing in an empty override
// file
configFile, err := os.Create(filepath.Join(path, ".terraformrc"))
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
return configFile.Name()
}

Loading…
Cancel
Save