Skip to content

Commit 8ad0421

Browse files
committed
test(terraform): add regression test for same-source module edge resolution
Add test_module_output_edge_with_same_source_siblings to verify that variable resolution works when two modules share the same source and a third module references one of their outputs. The test fixture uses a minimal reproduction: a variable-to-output module (mod_a) instantiated twice, with the alphabetically-first instance receiving the other's output, and a separate module (mod_c) whose explicitly-passed variable must resolve correctly.
1 parent 6dbab6a commit 8ad0421

4 files changed

Lines changed: 64 additions & 0 deletions

File tree

tests/terraform/graph/graph_builder/test_local_graph.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,3 +434,30 @@ def test_variables_same_name_different_modules(self):
434434
# Check they point to 2 different modules
435435
self.assertEqual(2, len(module_variable_edges))
436436
self.assertNotEqual(local_graph.vertices[module_variable_edges[0].origin], local_graph.vertices[module_variable_edges[1].origin])
437+
438+
def test_module_output_edge_with_same_source_siblings(self):
439+
resources_dir = os.path.realpath(os.path.join(TEST_DIRNAME, '../resources/modules/same_source_module_edge'))
440+
hcl_config_parser = TFParser()
441+
module, _ = hcl_config_parser.parse_hcl_module(resources_dir, self.source)
442+
local_graph = TerraformLocalGraph(module)
443+
local_graph.build_graph(render_variables=True)
444+
445+
# Find the azurerm_storage_account.sa vertex (inside module "c")
446+
sa_vertices = [
447+
v for v in local_graph.vertices
448+
if v.block_type == BlockType.RESOURCE
449+
and v.name == "azurerm_storage_account.sa"
450+
]
451+
self.assertEqual(1, len(sa_vertices))
452+
sa_vertex = sa_vertices[0]
453+
454+
# The key assertion: var.default_action must have been resolved to "Deny"
455+
# Without the fix, the edge connects to module "a"'s output (wrong module),
456+
# corrupting variable resolution and leaving "var.default_action" unresolved.
457+
network_rules = sa_vertex.config.get("azurerm_storage_account", {}).get("sa", {}).get("network_rules", {})
458+
if isinstance(network_rules, list):
459+
network_rules = network_rules[0]
460+
default_action = network_rules.get("default_action", [None])
461+
if isinstance(default_action, list):
462+
default_action = default_action[0]
463+
self.assertEqual("Deny", default_action)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module "b" {
2+
source = "./mod_a"
3+
}
4+
5+
module "a" {
6+
source = "./mod_a"
7+
result = module.b.result
8+
}
9+
10+
module "c" {
11+
source = "./mod_c"
12+
13+
default_action = "Deny"
14+
15+
ref = module.b.result.some_attr
16+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
variable "result" {
2+
default = null
3+
}
4+
5+
output "result" {
6+
value = var.result
7+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
variable "default_action" {
2+
type = string
3+
default = "Deny"
4+
}
5+
6+
variable "ref" {
7+
default = null
8+
}
9+
10+
resource "azurerm_storage_account" "sa" {
11+
network_rules {
12+
default_action = var.default_action
13+
}
14+
}

0 commit comments

Comments
 (0)