- Notifications
You must be signed in to change notification settings - Fork925
chore(enterprise/coderd): use filesystem mirror for providers in TestWorkspaceTagsTerraform#16155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -6,7 +6,9 @@ import ( | ||
"database/sql" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
@@ -1399,13 +1401,10 @@ func TestTemplateDoesNotAllowUserAutostop(t *testing.T) { | ||
// real Terraform provisioner and validate that the workspace is created | ||
// successfully. The workspace itself does not specify any resources, and | ||
// this is fine. | ||
// To improve speed, we pre-download the providers and set a custom Terraform | ||
// config file so that we only reference those | ||
// nolint:paralleltest // t.Setenv | ||
func TestWorkspaceTagsTerraform(t *testing.T) { | ||
mainTfTemplate := ` | ||
terraform { | ||
required_providers { | ||
@@ -1424,6 +1423,8 @@ func TestWorkspaceTagsTerraform(t *testing.T) { | ||
} | ||
%s | ||
` | ||
tfCliConfigPath := downloadProviders(t, fmt.Sprintf(mainTfTemplate, "")) | ||
t.Setenv("TF_CLI_CONFIG_FILE", tfCliConfigPath) | ||
for _, tc := range []struct { | ||
name string | ||
@@ -1537,10 +1538,8 @@ func TestWorkspaceTagsTerraform(t *testing.T) { | ||
} { | ||
tc := tc | ||
t.Run(tc.name, func(t *testing.T) { | ||
// This can take a while, so set a relatively long timeout. | ||
ctx := testutil.Context(t, 2*testutil.WaitSuperLong) | ||
client, owner := coderdenttest.New(t, &coderdenttest.Options{ | ||
Options: &coderdtest.Options{ | ||
@@ -1587,6 +1586,55 @@ func TestWorkspaceTagsTerraform(t *testing.T) { | ||
} | ||
} | ||
// downloadProviders is a test helper that creates a temporary file and writes a | ||
// terraform CLI config file with a provider_installation stanza for coder/coder | ||
// using dev_overrides. It also fetches the latest provider release from GitHub | ||
// and extracts the binary to the temporary dir. It is the responsibility of the | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Love the line length alignment here 😍, too bad about the third line missing one char 🤣 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I did that specifically to annoy you ;-P | ||
// caller to set TF_CLI_CONFIG_FILE. | ||
func downloadProviders(t *testing.T, providersTf string) string { | ||
t.Helper() | ||
// We firstly write a Terraform CLI config file to a temporary directory: | ||
var ( | ||
ctx, cancel = context.WithTimeout(context.Background(), testutil.WaitLong) | ||
tempDir = t.TempDir() | ||
cacheDir = filepath.Join(tempDir, ".cache") | ||
providersTfPath = filepath.Join(tempDir, "providers.tf") | ||
cliConfigPath = filepath.Join(tempDir, "local.tfrc") | ||
) | ||
defer cancel() | ||
// Write files to disk | ||
require.NoError(t, os.MkdirAll(cacheDir, os.ModePerm|os.ModeDir)) | ||
require.NoError(t, os.WriteFile(providersTfPath, []byte(providersTf), os.ModePerm)) // nolint:gosec | ||
cliConfigTemplate := ` | ||
provider_installation { | ||
filesystem_mirror { | ||
path = %q | ||
include = ["*/*/*"] | ||
} | ||
direct { | ||
exclude = ["*/*/*"] | ||
} | ||
}` | ||
err := os.WriteFile(cliConfigPath, []byte(fmt.Sprintf(cliConfigTemplate, cacheDir)), os.ModePerm) // nolint:gosec | ||
require.NoError(t, err, "failed to write %s", cliConfigPath) | ||
// Run terraform providers mirror to mirror required providers to cacheDir | ||
cmd := exec.CommandContext(ctx, "terraform", "providers", "mirror", cacheDir) | ||
cmd.Env = os.Environ() // without this terraform may complain about path | ||
cmd.Env = append(cmd.Env, "TF_CLI_CONFIG_FILE="+cliConfigPath) | ||
cmd.Dir = tempDir | ||
out, err := cmd.CombinedOutput() | ||
if !assert.NoError(t, err) { | ||
t.Log("failed to download providers:") | ||
t.Log(string(out)) | ||
t.FailNow() | ||
} | ||
t.Logf("Set TF_CLI_CONFIG_FILE=%s", cliConfigPath) | ||
return cliConfigPath | ||
} | ||
// Blocked by autostart requirements | ||
func TestExecutorAutostartBlocked(t *testing.T) { | ||
t.Parallel() | ||
Uh oh!
There was an error while loading.Please reload this page.