Skip to content

Commit

Permalink
DEVPL-461: add 'text file busy' as a retryable error (#19)
Browse files Browse the repository at this point in the history
* style: run 'gofumpt -w .' and configure VSCode to maintain
* fix: set up retryable errors, including 'text file busy' case
  • Loading branch information
jlucktay authored Jul 5, 2024
1 parent 6e87e14 commit 2fc89d2
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"gopls": {
"formatting.gofumpt": true
}
}
4 changes: 2 additions & 2 deletions providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ func DownloadProviderVersionE(version string, sourceAddress string, providerName
// Create ~/.terraform.d/plugin-cache directory if it doesn't exist
// https://gist.github.com/ivanzoid/5040166bb3f0c82575b52c2ca5f5a60c
if _, err := os.Stat(binaryPath); os.IsNotExist(err) {
os.MkdirAll(binaryPath, os.ModeDir|0755)
os.MkdirAll(binaryPath, os.ModeDir|0o755)
}
var binaryUrl string
binaryUrl, err = GetBinaryUrl(version, providerName)
Expand Down Expand Up @@ -212,7 +212,7 @@ func DownloadProviderVersionE(version string, sourceAddress string, providerName
}()
defer os.Remove(out.Name())
zipExtractPath := binaryDownloadDirectory + "/bin_" + version
os.MkdirAll(zipExtractPath, 0755)
os.MkdirAll(zipExtractPath, 0o755)

// Cleanup zip files
defer os.RemoveAll(zipExtractPath)
Expand Down
11 changes: 4 additions & 7 deletions terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func UpdateModuleSourceAndVersionE(srcDir, module, src, ver string) error {
}

if hasChanges {
if err := os.WriteFile(filename, f.Bytes(), 0666); err != nil {
if err := os.WriteFile(filename, f.Bytes(), 0o666); err != nil {
return err
}
}
Expand Down Expand Up @@ -291,7 +291,7 @@ func UpdateProviderVersionE(dir, provider, version string, providerSource string
}

if hasChanges {
if err := ioutil.WriteFile(filename, f.Bytes(), 0666); err != nil {
if err := ioutil.WriteFile(filename, f.Bytes(), 0o666); err != nil {
return err
}

Expand Down Expand Up @@ -323,7 +323,6 @@ func UpdateProviderVersion(t *testing.T, dir, provider, version string, provider
// Usage:
// * version is the version of Terraform to download.
func GetTerraformBinaryUrlE(version string) (string, error) {

var binaryUrl string
operatingSystem := runtime.GOOS
architecture := runtime.GOARCH
Expand Down Expand Up @@ -362,7 +361,6 @@ func GetTerraformBinaryUrlE(version string) (string, error) {
} else {
return "", errors.New("Unable to find an appropriate Terraform binary download URL for the underlying OS and architecture")
}

}

// Closure to address file descriptors issue with all the deferred .Close() methods
Expand Down Expand Up @@ -412,7 +410,6 @@ func extractAndWriteFile(dst string, f *zip.File) error {
// Usage:
// * version is the version of Terraform to download.
func DownloadTerraformVersionE(version string) (binaryPath string, err error) {

// Initialise all path variables
homeDirectory, _ := os.UserHomeDir()
binaryDownloadDirectory := filepath.Join(homeDirectory, ".terraform.versions")
Expand All @@ -424,7 +421,7 @@ func DownloadTerraformVersionE(version string) (binaryPath string, err error) {
// Create ~/.terraform.versions directory if it doesn't exist
// https://gist.github.com/ivanzoid/5040166bb3f0c82575b52c2ca5f5a60c
if _, err := os.Stat(binaryDownloadDirectory); os.IsNotExist(err) {
os.Mkdir(binaryDownloadDirectory, os.ModeDir|0755)
os.Mkdir(binaryDownloadDirectory, os.ModeDir|0o755)
}

var binaryUrl string
Expand Down Expand Up @@ -473,7 +470,7 @@ func DownloadTerraformVersionE(version string) (binaryPath string, err error) {
defer os.Remove(out.Name())

zipExtractPath := binaryDownloadDirectory + "/bin_" + version
os.MkdirAll(zipExtractPath, 0755)
os.MkdirAll(zipExtractPath, 0o755)

// Cleanup zip files
defer os.RemoveAll(zipExtractPath)
Expand Down
3 changes: 1 addition & 2 deletions terraform_modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,13 @@ func UpdateModuleSourcesToLocalPaths(t *testing.T, dst string) {
}

if hasChanges {
if err := os.WriteFile(filename, f.Bytes(), 0666); err != nil {
if err := os.WriteFile(filename, f.Bytes(), 0o666); err != nil {
return err
}
}

return nil
})

if err != nil {
t.Fatalf("An error occurred when attempting to resolve all module sources to local paths: %s", err.Error())
}
Expand Down
30 changes: 24 additions & 6 deletions terraform_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"regexp"
"strings"
"testing"
"time"

"github.com/gruntwork-io/terratest/modules/terraform"
teststructure "github.com/gruntwork-io/terratest/modules/test-structure"
Expand Down Expand Up @@ -78,13 +79,30 @@ func GetTerraformVersionConstraint(t *testing.T, srcDir string) string {
return constraint
}

func newTerraformOptions(t *testing.T) *terraform.Options {
t.Helper()

// Start with default retryable errors as a baseline.
opts := terraform.WithDefaultRetryableErrors(t, &terraform.Options{})

// Add a pattern to cover off this corner case.
opts.RetryableTerraformErrors[".*text file busy.*"] = "os: StartProcess ETXTBSY race on Unix systems - " +
"https://github.com/golang/go/issues/22315"

// Set some additional options to govern the retry behaviour.
opts.MaxRetries = 3
opts.TimeBetweenRetries = time.Second * 5

return opts
}

func TerraformVersionsTest(t *testing.T, srcDir string, variables map[string]interface{}, environment_variables map[string]string) {
constraint := GetTerraformVersionConstraint(t, srcDir)
available := GetAvailableVersions(t, "terraform")
versions := GetMatchingVersions(t, constraint, available)

for _, version := range versions {
var tfOptions = &terraform.Options{}
tfOptions := newTerraformOptions(t)

if len(variables) > 0 {
tfOptions.Vars = variables
Expand Down Expand Up @@ -112,7 +130,7 @@ func AwsProviderVersionsTest(t *testing.T, srcDir string, variables map[string]i
versions := GetMatchingVersions(t, constraint, available)

for _, version := range versions {
var tfOptions = &terraform.Options{}
tfOptions := newTerraformOptions(t)

if len(variables) > 0 {
tfOptions.Vars = variables
Expand Down Expand Up @@ -140,7 +158,7 @@ func CloudflareProviderVersionsTest(t *testing.T, srcDir string, variables map[s
testVers := GetMatchingVersions(t, constraint, available)

for _, version := range testVers {
var tfOptions = &terraform.Options{}
tfOptions := newTerraformOptions(t)

if len(variables) > 0 {
tfOptions.Vars = variables
Expand All @@ -167,7 +185,7 @@ func DatadogProviderVersionsTest(t *testing.T, srcDir string, variables map[stri
testVers := GetMatchingVersions(t, constraint, available)

for _, version := range testVers {
var tfOptions = &terraform.Options{}
tfOptions := newTerraformOptions(t)

if len(variables) > 0 {
tfOptions.Vars = variables
Expand All @@ -193,7 +211,7 @@ func OpsgenieProviderVersionsTest(t *testing.T, srcDir string, variables map[str
testVers := []string{"0.6.10", "0.6.11", "0.6.14", "0.6.15", "0.6.16", "0.6.17", "0.6.18", "0.6.19", "0.6.20"} // testing for specific versions as https://api.releases.hashicorp.com/v1/releases/terraform-provider-opsgenie is not showing anything newer than 0.6.11 currently

for _, version := range testVers {
var tfOptions = &terraform.Options{}
tfOptions := newTerraformOptions(t)

if len(variables) > 0 {
tfOptions.Vars = variables
Expand All @@ -220,7 +238,7 @@ func GcpProviderVersionsTest(t *testing.T, srcDir string, variables map[string]i
testVers := GetMatchingVersions(t, constraint, available)

for _, version := range testVers {
var tfOptions = &terraform.Options{}
tfOptions := newTerraformOptions(t)

if len(variables) > 0 {
tfOptions.Vars = variables
Expand Down

0 comments on commit 2fc89d2

Please sign in to comment.