-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstacks-test.sh
More file actions
executable file
·79 lines (66 loc) · 3.15 KB
/
stacks-test.sh
File metadata and controls
executable file
·79 lines (66 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env bashunit
set_up() {
ENV=dev-eu
NSTACKS=0
source stacks-gen-deps.sh $ENV $NSTACKS
}
test_branch_pattern() {
re=$(along_branch_re path/to/dir)
assert_string_starts_with '^(path/to/dir/|path/to/|path/|)[^/]+' $re
assert_matches $re$ path/to/dir/leaf.tf
assert_matches $re$ root.tf
assert_matches $re$ path/branch.tfvars
assert_not_matches $re$ ./root.tfvars
assert_not_matches $re$ path/to/dir2/leaf.tf
assert_not_matches $re$ path/to/dir/sub/below.tf
}
test_env_match() {
ENV='dev-eu-fr'
assert_same 3 $(env_match 'dev' $ENV)
assert_same 2 $(env_match 'eu' $ENV)
assert_same 1 $(env_match 'fr' $ENV)
assert_same 3 $(env_match 'dev-eu' $ENV)
assert_same 2 $(env_match 'eu-fr' $ENV)
set +e
for name in 'dev-fr' 'dev-' 'dev-e' 'eu-' 'eu-f' 'fr-'; do
env_match "$name" "$ENV"
assert_unsuccessful_code
done
}
test_directory_flattening() {
# Run the script directly to test the directory flattening logic
cd example/
output=$(bash ../stacks-gen-deps.sh dev-eu 1 network/vpc network/vpc/main.tf network/netplan.tf org/main.tf providers.tf network/vpc/subdir/below.tf)
# Check that nested files in the branch are flattened correctly
assert_contains '/network_vpc_main.tf: network/vpc/main.tf' "$output"
assert_contains '/network_netplan.tf: network/netplan.tf' "$output"
assert_contains '/providers.tf: providers.tf' "$output"
assert_not_contains 'org/main.tf' "$output"
assert_not_contains 'below' "$output"
}
test_tfvars_precedence() {
# Run the script to test tfvars parsing and precedence mapping
cd example/
output=$(bash ../stacks-gen-deps.sh dev-eu 1 network/vpc network/vpc/main.tf all.tfvars dev-eu.tfvars dev.tfvars network/all.tfvars network/dev-eu.tfvars network/eu.tfvars)
# https://opentofu.org/docs/language/values/variables/#variable-definition-precedence
assert_contains '/0-all-.auto.tfvars: all.tfvars' "$output"
assert_contains '/2-dev-.auto.tfvars: dev.tfvars' "$output"
assert_contains '/2-dev-eu-.auto.tfvars: dev-eu.tfvars' "$output"
assert_contains '/network_0-all-.auto.tfvars: network/all.tfvars' "$output"
assert_contains '/network_1-eu-.auto.tfvars: network/eu.tfvars' "$output"
assert_contains '/network_2-dev-eu-.auto.tfvars: network/dev-eu.tfvars' "$output"
}
test_cross_stack_dependencies() {
# Run the script to test cross-stack dependency extraction
cd example/
output=$(bash ../stacks-gen-deps.sh dev-eu 2 network/vpc instances network/vpc/main.tf instances/main.tf)
# Check for downstream/upstream mapping
assert_contains 'DOWNSTREAMS_network/vpc += instances' "$output"
assert_contains 'UPSTREAMS_instances += network/vpc' "$output"
# Check for plan and outputs dependencies
assert_matches 'instances/[^/]+/tfplan.json: .*network/vpc/[^/]+/tfplan.json' "$output"
assert_matches 'instances/[^/]+/outputs.json: .*network/vpc/[^/]+/outputs.json' "$output"
# Check for destroy and refresh dependencies
assert_matches 'network/vpc/[^/]+/.destroy: destroy-instances' "$output"
assert_matches 'instances/[^/]+/.refresh: refresh-network/vpc' "$output"
}