Skip to content

Commit 913178c

Browse files
authored
fix: Cleanup from #5342 (#5356)
* fix: Suppressing error message for `GOOGLE_CREDENTIALS` * fix: Cleanup * fix: Addressing removal of logger * fix: Replacing `--bootstrap-backend` with `--backend-bootstrap`
1 parent ffb1b19 commit 913178c

3 files changed

Lines changed: 87 additions & 36 deletions

File tree

internal/gcphelper/config.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func CreateGCSClient(
3535
config *GCPSessionConfig,
3636
opts *options.TerragruntOptions,
3737
) (*storage.Client, error) {
38-
clientOpts, err := CreateGCPConfig(ctx, l, config, opts)
38+
clientOpts, err := CreateGCPConfig(ctx, config, opts)
3939
if err != nil {
4040
return nil, err
4141
}
@@ -51,7 +51,6 @@ func CreateGCSClient(
5151
// CreateGCPConfig returns GCP client options for the given GCPSessionConfig and TerragruntOptions.
5252
func CreateGCPConfig(
5353
ctx context.Context,
54-
l log.Logger,
5554
gcpCfg *GCPSessionConfig,
5655
opts *options.TerragruntOptions,
5756
) ([]option.ClientOption, error) {
@@ -138,7 +137,7 @@ func createGCPCredentialsFromGoogleCredentialsEnv(ctx context.Context, opts *opt
138137
}
139138

140139
if err := json.Unmarshal([]byte(contents), &account); err != nil {
141-
return nil, errors.Errorf("Error parsing credentials '%s': %w", contents, err)
140+
return nil, errors.Errorf("Error parsing GCP credentials.")
142141
}
143142

144143
conf := jwt.Config{

internal/gcphelper/config_test.go

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@ import (
1010

1111
"github.com/gruntwork-io/terragrunt/internal/gcphelper"
1212
"github.com/gruntwork-io/terragrunt/pkg/options"
13-
"github.com/gruntwork-io/terragrunt/test/helpers/logger"
1413
"github.com/stretchr/testify/assert"
1514
"github.com/stretchr/testify/require"
1615
)
1716

1817
func TestCreateGcpConfigWithApplicationCredentialsEnv(t *testing.T) {
1918
t.Parallel()
2019

21-
l := logger.CreateLogger()
2220
ctx := context.Background()
2321

2422
// Create a temporary credentials file
@@ -33,15 +31,14 @@ func TestCreateGcpConfigWithApplicationCredentialsEnv(t *testing.T) {
3331
},
3432
}
3533

36-
clientOpts, err := gcphelper.CreateGCPConfig(ctx, l, nil, opts)
34+
clientOpts, err := gcphelper.CreateGCPConfig(ctx, nil, opts)
3735
require.NoError(t, err)
3836
assert.NotEmpty(t, clientOpts)
3937
}
4038

4139
func TestCreateGcpConfigWithOAuthAccessTokenEnv(t *testing.T) {
4240
t.Parallel()
4341

44-
l := logger.CreateLogger()
4542
ctx := context.Background()
4643

4744
opts := &options.TerragruntOptions{
@@ -50,15 +47,14 @@ func TestCreateGcpConfigWithOAuthAccessTokenEnv(t *testing.T) {
5047
},
5148
}
5249

53-
clientOpts, err := gcphelper.CreateGCPConfig(ctx, l, nil, opts)
50+
clientOpts, err := gcphelper.CreateGCPConfig(ctx, nil, opts)
5451
require.NoError(t, err)
5552
assert.NotEmpty(t, clientOpts)
5653
}
5754

5855
func TestCreateGcpConfigWithGoogleCredentialsEnv(t *testing.T) {
5956
t.Parallel()
6057

61-
l := logger.CreateLogger()
6258
ctx := context.Background()
6359

6460
// Test with JSON content directly (not a file path)
@@ -79,15 +75,14 @@ func TestCreateGcpConfigWithGoogleCredentialsEnv(t *testing.T) {
7975
},
8076
}
8177

82-
clientOpts, err := gcphelper.CreateGCPConfig(ctx, l, nil, opts)
78+
clientOpts, err := gcphelper.CreateGCPConfig(ctx, nil, opts)
8379
require.NoError(t, err)
8480
assert.NotEmpty(t, clientOpts)
8581
}
8682

8783
func TestCreateGcpConfigWithCredentialsFileFromConfig(t *testing.T) {
8884
t.Parallel()
8985

90-
l := logger.CreateLogger()
9186
ctx := context.Background()
9287

9388
// Create a temporary credentials file
@@ -104,15 +99,14 @@ func TestCreateGcpConfigWithCredentialsFileFromConfig(t *testing.T) {
10499
Credentials: credsFile,
105100
}
106101

107-
clientOpts, err := gcphelper.CreateGCPConfig(ctx, l, gcpCfg, opts)
102+
clientOpts, err := gcphelper.CreateGCPConfig(ctx, gcpCfg, opts)
108103
require.NoError(t, err)
109104
assert.NotEmpty(t, clientOpts)
110105
}
111106

112107
func TestCreateGcpConfigWithAccessTokenFromConfig(t *testing.T) {
113108
t.Parallel()
114109

115-
l := logger.CreateLogger()
116110
ctx := context.Background()
117111

118112
opts := &options.TerragruntOptions{
@@ -123,15 +117,14 @@ func TestCreateGcpConfigWithAccessTokenFromConfig(t *testing.T) {
123117
AccessToken: "test-access-token",
124118
}
125119

126-
clientOpts, err := gcphelper.CreateGCPConfig(ctx, l, gcpCfg, opts)
120+
clientOpts, err := gcphelper.CreateGCPConfig(ctx, gcpCfg, opts)
127121
require.NoError(t, err)
128122
assert.NotEmpty(t, clientOpts)
129123
}
130124

131125
func TestGcpConfigEnvVarsTakePrecedenceOverConfig(t *testing.T) {
132126
t.Parallel()
133127

134-
l := logger.CreateLogger()
135128
ctx := context.Background()
136129

137130
// Create temporary credentials files
@@ -157,7 +150,7 @@ func TestGcpConfigEnvVarsTakePrecedenceOverConfig(t *testing.T) {
157150
Credentials: configCredsFile, // This should be ignored in favor of env var
158151
}
159152

160-
clientOpts, err := gcphelper.CreateGCPConfig(ctx, l, gcpCfg, opts)
153+
clientOpts, err := gcphelper.CreateGCPConfig(ctx, gcpCfg, opts)
161154
require.NoError(t, err)
162155
assert.NotEmpty(t, clientOpts)
163156

@@ -168,7 +161,6 @@ func TestGcpConfigEnvVarsTakePrecedenceOverConfig(t *testing.T) {
168161
func TestCreateGcpConfigWithImpersonation(t *testing.T) {
169162
t.Parallel()
170163

171-
l := logger.CreateLogger()
172164
ctx := context.Background()
173165

174166
opts := &options.TerragruntOptions{
@@ -182,7 +174,7 @@ func TestCreateGcpConfigWithImpersonation(t *testing.T) {
182174

183175
// This will fail because we don't have real credentials, but we can verify
184176
// that the impersonation configuration is attempted
185-
_, err := gcphelper.CreateGCPConfig(ctx, l, gcpCfg, opts)
177+
_, err := gcphelper.CreateGCPConfig(ctx, gcpCfg, opts)
186178
// We expect an error because impersonation requires valid base credentials
187179
// The error should be about impersonation, not about missing credentials
188180
require.Error(t, err)
@@ -192,15 +184,14 @@ func TestCreateGcpConfigWithImpersonation(t *testing.T) {
192184
func TestCreateGcpConfigWithNoCredentials(t *testing.T) {
193185
t.Parallel()
194186

195-
l := logger.CreateLogger()
196187
ctx := context.Background()
197188

198189
opts := &options.TerragruntOptions{
199190
Env: map[string]string{},
200191
}
201192

202193
// No credentials provided - should return empty options (will use default credentials)
203-
clientOpts, err := gcphelper.CreateGCPConfig(ctx, l, nil, opts)
194+
clientOpts, err := gcphelper.CreateGCPConfig(ctx, nil, opts)
204195
require.NoError(t, err)
205196
// Should return empty options when no credentials are provided
206197
// (default credentials will be used by GCP client)
@@ -210,7 +201,6 @@ func TestCreateGcpConfigWithNoCredentials(t *testing.T) {
210201
func TestCreateGcpConfigWithGoogleCredentialsFile(t *testing.T) {
211202
t.Parallel()
212203

213-
l := logger.CreateLogger()
214204
ctx := context.Background()
215205

216206
// Create a temporary credentials file
@@ -234,7 +224,7 @@ func TestCreateGcpConfigWithGoogleCredentialsFile(t *testing.T) {
234224
},
235225
}
236226

237-
clientOpts, err := gcphelper.CreateGCPConfig(ctx, l, nil, opts)
227+
clientOpts, err := gcphelper.CreateGCPConfig(ctx, nil, opts)
238228
require.NoError(t, err)
239229
assert.NotEmpty(t, clientOpts)
240230
}

test/integration_gcp_test.go

Lines changed: 76 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package test_test
55
import (
66
"errors"
77
"fmt"
8+
"maps"
89
"os"
910
"path"
1011
"path/filepath"
@@ -263,8 +264,22 @@ func TestGcpWorksWithBackend(t *testing.T) {
263264

264265
defer deleteGCSBucket(t, gcsBucketName)
265266

266-
tmpTerragruntGCSConfigPath := createTmpTerragruntGCSConfig(t, testFixtureGcsPath, project, terraformRemoteStateGcpRegion, gcsBucketName, config.DefaultTerragruntConfigPath)
267-
helpers.RunTerragrunt(t, fmt.Sprintf("terragrunt apply -auto-approve --non-interactive --backend-bootstrap --config %s --working-dir %s", tmpTerragruntGCSConfigPath, testFixtureGcsPath))
267+
tmpTerragruntGCSConfigPath := createTmpTerragruntGCSConfig(
268+
t,
269+
testFixtureGcsPath,
270+
project,
271+
terraformRemoteStateGcpRegion,
272+
gcsBucketName,
273+
config.DefaultTerragruntConfigPath,
274+
)
275+
helpers.RunTerragrunt(
276+
t,
277+
fmt.Sprintf(
278+
"terragrunt apply -auto-approve --non-interactive --backend-bootstrap --config %s --working-dir %s",
279+
tmpTerragruntGCSConfigPath,
280+
testFixtureGcsPath,
281+
),
282+
)
268283

269284
var expectedGCSLabels = map[string]string{
270285
"owner": "terragrunt_test",
@@ -287,8 +302,22 @@ func TestGcpWorksWithExistingBucket(t *testing.T) {
287302
location := terraformRemoteStateGcpRegion
288303
createGCSBucket(t, project, location, gcsBucketName)
289304

290-
tmpTerragruntGCSConfigPath := createTmpTerragruntGCSConfig(t, testFixtureGcsByoBucketPath, project, terraformRemoteStateGcpRegion, gcsBucketName, config.DefaultTerragruntConfigPath)
291-
helpers.RunTerragrunt(t, fmt.Sprintf("terragrunt apply -auto-approve --non-interactive --config %s --working-dir %s", tmpTerragruntGCSConfigPath, testFixtureGcsByoBucketPath))
305+
tmpTerragruntGCSConfigPath := createTmpTerragruntGCSConfig(
306+
t,
307+
testFixtureGcsByoBucketPath,
308+
project,
309+
terraformRemoteStateGcpRegion,
310+
gcsBucketName,
311+
config.DefaultTerragruntConfigPath,
312+
)
313+
helpers.RunTerragrunt(
314+
t,
315+
fmt.Sprintf(
316+
"terragrunt apply -auto-approve --non-interactive --config %s --working-dir %s",
317+
tmpTerragruntGCSConfigPath,
318+
testFixtureGcsByoBucketPath,
319+
),
320+
)
292321

293322
validateGCSBucketExistsAndIsLabeled(t, location, gcsBucketName, nil)
294323
}
@@ -302,9 +331,24 @@ func TestGcpCheckMissingBucket(t *testing.T) {
302331
project := os.Getenv("GOOGLE_CLOUD_PROJECT")
303332
gcsBucketName := "terragrunt-test-bucket-" + strings.ToLower(helpers.UniqueID())
304333

305-
tmpTerragruntGCSConfigPath := createTmpTerragruntGCSConfig(t, testFixtureGcsNoBucket, project, terraformRemoteStateGcpRegion, gcsBucketName, config.DefaultTerragruntConfigPath)
306-
_, _, err := helpers.RunTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt apply -auto-approve --backend-bootstrap --non-interactive --config %s --working-dir %s", tmpTerragruntGCSConfigPath, testFixtureGcsNoBucket))
334+
tmpTerragruntGCSConfigPath := createTmpTerragruntGCSConfig(
335+
t,
336+
testFixtureGcsNoBucket,
337+
project,
338+
terraformRemoteStateGcpRegion,
339+
gcsBucketName,
340+
config.DefaultTerragruntConfigPath,
341+
)
342+
_, _, err := helpers.RunTerragruntCommandWithOutput(
343+
t,
344+
fmt.Sprintf(
345+
"terragrunt apply -auto-approve --backend-bootstrap --non-interactive --config %s --working-dir %s",
346+
tmpTerragruntGCSConfigPath,
347+
testFixtureGcsNoBucket,
348+
),
349+
)
307350
require.Error(t, err)
351+
308352
assert.Contains(t, err.Error(), "Missing required GCS remote state configuration bucket")
309353
}
310354

@@ -320,7 +364,14 @@ func TestGcpNoPrefixBucket(t *testing.T) {
320364
defer deleteGCSBucket(t, gcsBucketName)
321365

322366
tmpTerragruntGCSConfigPath := createTmpTerragruntGCSConfig(t, testFixtureGcsNoPrefix, project, terraformRemoteStateGcpRegion, gcsBucketName, config.DefaultTerragruntConfigPath)
323-
_, _, err := helpers.RunTerragruntCommandWithOutput(t, fmt.Sprintf("terragrunt apply -auto-approve --backend-bootstrap --non-interactive --config %s --working-dir %s", tmpTerragruntGCSConfigPath, testFixtureGcsNoPrefix))
367+
_, _, err := helpers.RunTerragruntCommandWithOutput(
368+
t,
369+
fmt.Sprintf(
370+
"terragrunt apply -auto-approve --backend-bootstrap --non-interactive --config %s --working-dir %s",
371+
tmpTerragruntGCSConfigPath,
372+
testFixtureGcsNoPrefix,
373+
),
374+
)
324375
require.NoError(t, err)
325376
}
326377

@@ -332,23 +383,36 @@ func TestGcpParallelStateInit(t *testing.T) {
332383
require.NoError(t, err)
333384
}
334385

335-
for i := 0; i < 20; i++ {
386+
for i := range 20 {
336387
err := util.CopyFolderContents(createLogger(), testFixtureGcsParallelStateInit, tmpEnvPath, ".terragrunt-test", nil, nil)
337388
require.NoError(t, err)
389+
338390
err = os.Rename(
339391
path.Join(tmpEnvPath, "template"),
340-
path.Join(tmpEnvPath, "app"+strconv.Itoa(i)))
392+
path.Join(tmpEnvPath, "app"+strconv.Itoa(i)),
393+
)
394+
341395
require.NoError(t, err)
342396
}
343397

344398
tmpTerragruntConfigFile := filepath.Join(tmpEnvPath, "root.hcl")
345399
project := os.Getenv("GOOGLE_CLOUD_PROJECT")
346400
gcsBucketName := "terragrunt-test-bucket-" + strings.ToLower(helpers.UniqueID())
347-
tmpTerragruntGCSConfigPath := createTmpTerragruntGCSConfig(t, testFixtureGcsParallelStateInit, project, terraformRemoteStateGcpRegion, gcsBucketName, "root.hcl")
401+
tmpTerragruntGCSConfigPath := createTmpTerragruntGCSConfig(
402+
t,
403+
testFixtureGcsParallelStateInit,
404+
project,
405+
terraformRemoteStateGcpRegion,
406+
gcsBucketName,
407+
"root.hcl",
408+
)
348409
err = util.CopyFile(tmpTerragruntGCSConfigPath, tmpTerragruntConfigFile)
349410
require.NoError(t, err)
350411

351-
helpers.RunTerragrunt(t, "terragrunt run --all --non-interactive --working-dir "+tmpEnvPath+" -- apply -auto-approve")
412+
helpers.RunTerragrunt(
413+
t,
414+
"terragrunt run --all --backend-bootstrap --non-interactive --working-dir "+tmpEnvPath+" -- apply",
415+
)
352416
}
353417

354418
func createTmpTerragruntGCSConfig(t *testing.T, templatesPath string, project string, location string, gcsBucketName string, configFileName string) string {
@@ -490,9 +554,7 @@ func assertGCSLabels(t *testing.T, expectedLabels map[string]string, bucketName
490554

491555
var actualLabels = make(map[string]string)
492556

493-
for key, value := range attrs.Labels {
494-
actualLabels[key] = value
495-
}
557+
maps.Copy(actualLabels, attrs.Labels)
496558

497559
assert.Equal(t, expectedLabels, actualLabels, "Did not find expected labels on GCS bucket.")
498560
}

0 commit comments

Comments
 (0)