Skip to content

Commit 8ba47f2

Browse files
committed
chore: Adding TestReadTerragruntAuthProviderCmdRunAllCallCountWithRacing
1 parent edb722c commit 8ba47f2

8 files changed

Lines changed: 123 additions & 0 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.jsonl
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env bash
2+
3+
# Records each invocation as a JSON line in calls.jsonl in this script's
4+
# directory. Tests read calls.jsonl to assert how many times the
5+
# --auth-provider-cmd was invoked and the working directory of each call.
6+
7+
set -e
8+
9+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
10+
LOG_FILE="${SCRIPT_DIR}/calls.jsonl"
11+
12+
# JSON escape: replace backslash and double-quote.
13+
escaped_pwd="${PWD//\\/\\\\}"
14+
escaped_pwd="${escaped_pwd//\"/\\\"}"
15+
16+
ts="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
17+
18+
printf '{"ts":"%s","working_dir":"%s","pid":%d}\n' "$ts" "$escaped_pwd" "$$" >>"$LOG_FILE"
19+
20+
printf '{"envs":{"AUTH_PROVIDER_CALL_COUNT_VAR":"ok"}}\n'
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
variable "name" {
2+
type = string
3+
}
4+
5+
output "value" {
6+
value = "from-${var.name}"
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
include {
2+
path = find_in_parent_folders("root.hcl")
3+
}
4+
5+
inputs = {
6+
name = "dep"
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
variable "upstream" {
2+
type = string
3+
}
4+
5+
output "value" {
6+
value = var.upstream
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
include {
2+
path = find_in_parent_folders("root.hcl")
3+
}
4+
5+
dependency "dep" {
6+
config_path = "../dep"
7+
}
8+
9+
inputs = {
10+
upstream = dependency.dep.outputs.value
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
remote_state {
2+
backend = "local"
3+
generate = {
4+
path = "backend.tf"
5+
if_exists = "overwrite_terragrunt"
6+
}
7+
config = {
8+
path = "${path_relative_to_include()}/terraform.tfstate"
9+
}
10+
}

test/integration_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3307,6 +3307,66 @@ func TestReadTerragruntAuthProviderCmdEnvInLocalsRunAll(t *testing.T) {
33073307
assert.Equal(t, "from-auth-provider", outputs["secret"].Value)
33083308
}
33093309

3310+
func TestReadTerragruntAuthProviderCmdRunAllCallCountWithRacing(t *testing.T) {
3311+
t.Parallel()
3312+
3313+
helpers.CleanupTerraformFolder(t, testFixtureAuthProviderCmd)
3314+
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureAuthProviderCmd)
3315+
rootPath := filepath.Join(tmpEnvPath, testFixtureAuthProviderCmd, "run-all-call-count")
3316+
authProviderCmd := filepath.Join(rootPath, "auth-provider.sh")
3317+
logPath := filepath.Join(rootPath, "calls.jsonl")
3318+
3319+
helpers.ValidateAuthProviderScript(t, rootPath, authProviderCmd)
3320+
require.NoError(t, os.Remove(logPath), "auth-provider.sh should have created %s", logPath)
3321+
3322+
helpers.RunTerragrunt(
3323+
t,
3324+
fmt.Sprintf(
3325+
"terragrunt run --all apply --non-interactive "+
3326+
"--working-dir %s --auth-provider-cmd %s",
3327+
rootPath,
3328+
authProviderCmd,
3329+
),
3330+
)
3331+
3332+
logBytes, err := os.ReadFile(logPath)
3333+
require.NoError(t, err)
3334+
3335+
type authCall struct {
3336+
Timestamp string `json:"ts"`
3337+
WorkingDir string `json:"working_dir"`
3338+
PID int `json:"pid"`
3339+
}
3340+
3341+
lines := strings.Split(strings.TrimRight(string(logBytes), "\n"), "\n")
3342+
calls := make([]authCall, 0, len(lines))
3343+
3344+
for i, line := range lines {
3345+
var call authCall
3346+
3347+
require.NoErrorf(t, json.Unmarshal([]byte(line), &call), "line %d: %q", i+1, line)
3348+
3349+
calls = append(calls, call)
3350+
}
3351+
3352+
t.Logf("auth-provider-cmd invocations:\n%s", string(logBytes))
3353+
3354+
// Five invocations are expected, in this order:
3355+
//
3356+
// 1. Discovery relationship phase parses `dep`.
3357+
// 2. Discovery relationship phase parses `dependent`.
3358+
// 3. Runner pool task parses `dep`for apply.
3359+
// 4. Runner pool task parses `dependent` for apply.
3360+
// 5. While `dependent`'s HCL is being decoded, the `dependency.dep`
3361+
// block fetches `dep`'s outputs, which requires parsing `dep` again.
3362+
//
3363+
assert.Len(t, calls, 5)
3364+
3365+
for _, call := range calls {
3366+
assert.NotContains(t, call.WorkingDir, ".terragrunt-cache")
3367+
}
3368+
}
3369+
33103370
func TestIamRolesLoadingFromDifferentModules(t *testing.T) {
33113371
t.Parallel()
33123372

0 commit comments

Comments
 (0)