Skip to content

Commit 2fc89d2

Browse files
authored
DEVPL-461: add 'text file busy' as a retryable error (#19)
* style: run 'gofumpt -w .' and configure VSCode to maintain * fix: set up retryable errors, including 'text file busy' case
1 parent 6e87e14 commit 2fc89d2

5 files changed

Lines changed: 36 additions & 17 deletions

File tree

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"gopls": {
3+
"formatting.gofumpt": true
4+
}
5+
}

providers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func DownloadProviderVersionE(version string, sourceAddress string, providerName
166166
// Create ~/.terraform.d/plugin-cache directory if it doesn't exist
167167
// https://gist.github.com/ivanzoid/5040166bb3f0c82575b52c2ca5f5a60c
168168
if _, err := os.Stat(binaryPath); os.IsNotExist(err) {
169-
os.MkdirAll(binaryPath, os.ModeDir|0755)
169+
os.MkdirAll(binaryPath, os.ModeDir|0o755)
170170
}
171171
var binaryUrl string
172172
binaryUrl, err = GetBinaryUrl(version, providerName)
@@ -212,7 +212,7 @@ func DownloadProviderVersionE(version string, sourceAddress string, providerName
212212
}()
213213
defer os.Remove(out.Name())
214214
zipExtractPath := binaryDownloadDirectory + "/bin_" + version
215-
os.MkdirAll(zipExtractPath, 0755)
215+
os.MkdirAll(zipExtractPath, 0o755)
216216

217217
// Cleanup zip files
218218
defer os.RemoveAll(zipExtractPath)

terraform.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func UpdateModuleSourceAndVersionE(srcDir, module, src, ver string) error {
151151
}
152152

153153
if hasChanges {
154-
if err := os.WriteFile(filename, f.Bytes(), 0666); err != nil {
154+
if err := os.WriteFile(filename, f.Bytes(), 0o666); err != nil {
155155
return err
156156
}
157157
}
@@ -291,7 +291,7 @@ func UpdateProviderVersionE(dir, provider, version string, providerSource string
291291
}
292292

293293
if hasChanges {
294-
if err := ioutil.WriteFile(filename, f.Bytes(), 0666); err != nil {
294+
if err := ioutil.WriteFile(filename, f.Bytes(), 0o666); err != nil {
295295
return err
296296
}
297297

@@ -323,7 +323,6 @@ func UpdateProviderVersion(t *testing.T, dir, provider, version string, provider
323323
// Usage:
324324
// * version is the version of Terraform to download.
325325
func GetTerraformBinaryUrlE(version string) (string, error) {
326-
327326
var binaryUrl string
328327
operatingSystem := runtime.GOOS
329328
architecture := runtime.GOARCH
@@ -362,7 +361,6 @@ func GetTerraformBinaryUrlE(version string) (string, error) {
362361
} else {
363362
return "", errors.New("Unable to find an appropriate Terraform binary download URL for the underlying OS and architecture")
364363
}
365-
366364
}
367365

368366
// Closure to address file descriptors issue with all the deferred .Close() methods
@@ -412,7 +410,6 @@ func extractAndWriteFile(dst string, f *zip.File) error {
412410
// Usage:
413411
// * version is the version of Terraform to download.
414412
func DownloadTerraformVersionE(version string) (binaryPath string, err error) {
415-
416413
// Initialise all path variables
417414
homeDirectory, _ := os.UserHomeDir()
418415
binaryDownloadDirectory := filepath.Join(homeDirectory, ".terraform.versions")
@@ -424,7 +421,7 @@ func DownloadTerraformVersionE(version string) (binaryPath string, err error) {
424421
// Create ~/.terraform.versions directory if it doesn't exist
425422
// https://gist.github.com/ivanzoid/5040166bb3f0c82575b52c2ca5f5a60c
426423
if _, err := os.Stat(binaryDownloadDirectory); os.IsNotExist(err) {
427-
os.Mkdir(binaryDownloadDirectory, os.ModeDir|0755)
424+
os.Mkdir(binaryDownloadDirectory, os.ModeDir|0o755)
428425
}
429426

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

475472
zipExtractPath := binaryDownloadDirectory + "/bin_" + version
476-
os.MkdirAll(zipExtractPath, 0755)
473+
os.MkdirAll(zipExtractPath, 0o755)
477474

478475
// Cleanup zip files
479476
defer os.RemoveAll(zipExtractPath)

terraform_modules.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,13 @@ func UpdateModuleSourcesToLocalPaths(t *testing.T, dst string) {
4848
}
4949

5050
if hasChanges {
51-
if err := os.WriteFile(filename, f.Bytes(), 0666); err != nil {
51+
if err := os.WriteFile(filename, f.Bytes(), 0o666); err != nil {
5252
return err
5353
}
5454
}
5555

5656
return nil
5757
})
58-
5958
if err != nil {
6059
t.Fatalf("An error occurred when attempting to resolve all module sources to local paths: %s", err.Error())
6160
}

terraform_versions.go

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"regexp"
88
"strings"
99
"testing"
10+
"time"
1011

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

82+
func newTerraformOptions(t *testing.T) *terraform.Options {
83+
t.Helper()
84+
85+
// Start with default retryable errors as a baseline.
86+
opts := terraform.WithDefaultRetryableErrors(t, &terraform.Options{})
87+
88+
// Add a pattern to cover off this corner case.
89+
opts.RetryableTerraformErrors[".*text file busy.*"] = "os: StartProcess ETXTBSY race on Unix systems - " +
90+
"https://github.com/golang/go/issues/22315"
91+
92+
// Set some additional options to govern the retry behaviour.
93+
opts.MaxRetries = 3
94+
opts.TimeBetweenRetries = time.Second * 5
95+
96+
return opts
97+
}
98+
8199
func TerraformVersionsTest(t *testing.T, srcDir string, variables map[string]interface{}, environment_variables map[string]string) {
82100
constraint := GetTerraformVersionConstraint(t, srcDir)
83101
available := GetAvailableVersions(t, "terraform")
84102
versions := GetMatchingVersions(t, constraint, available)
85103

86104
for _, version := range versions {
87-
var tfOptions = &terraform.Options{}
105+
tfOptions := newTerraformOptions(t)
88106

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

114132
for _, version := range versions {
115-
var tfOptions = &terraform.Options{}
133+
tfOptions := newTerraformOptions(t)
116134

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

142160
for _, version := range testVers {
143-
var tfOptions = &terraform.Options{}
161+
tfOptions := newTerraformOptions(t)
144162

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

169187
for _, version := range testVers {
170-
var tfOptions = &terraform.Options{}
188+
tfOptions := newTerraformOptions(t)
171189

172190
if len(variables) > 0 {
173191
tfOptions.Vars = variables
@@ -193,7 +211,7 @@ func OpsgenieProviderVersionsTest(t *testing.T, srcDir string, variables map[str
193211
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
194212

195213
for _, version := range testVers {
196-
var tfOptions = &terraform.Options{}
214+
tfOptions := newTerraformOptions(t)
197215

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

222240
for _, version := range testVers {
223-
var tfOptions = &terraform.Options{}
241+
tfOptions := newTerraformOptions(t)
224242

225243
if len(variables) > 0 {
226244
tfOptions.Vars = variables

0 commit comments

Comments
 (0)