Skip to content
This repository was archived by the owner on Jan 21, 2023. It is now read-only.

Commit 1e9d748

Browse files
yt-msMidnighter
authored andcommitted
fix(DeploymentNode): children should inherit environment from parent
1 parent 8d76282 commit 1e9d748

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/structurizr/model/deployment_node.py

+14-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from .relationship import Relationship
2828
from .software_system import SoftwareSystem
2929
from .software_system_instance import SoftwareSystemInstance, SoftwareSystemInstanceIO
30+
from .tags import Tags
3031

3132

3233
if TYPE_CHECKING: # pragma: no cover
@@ -114,6 +115,7 @@ def __init__(
114115
self._container_instances = set(container_instances)
115116
self._software_system_instances = set(software_system_instances)
116117
self._infrastructure_nodes = set(infrastructure_nodes)
118+
self.tags.add(Tags.DEPLOYMENT_NODE)
117119

118120
@property
119121
def children(self) -> Iterable["DeploymentNode"]:
@@ -158,7 +160,11 @@ def add_deployment_node(
158160
**kwargs: additional keyword arguments for instantiating a `DeploymentNode`
159161
"""
160162
node = DeploymentNode(
161-
name=name, description=description, technology=technology, **kwargs
163+
name=name,
164+
description=description,
165+
technology=technology,
166+
environment=self.environment,
167+
**kwargs,
162168
)
163169
self._add_child_deployment_node(node)
164170
return node
@@ -296,6 +302,13 @@ def _add_child_deployment_node(self, node: "DeploymentNode"):
296302
f"DeploymentNode with name '{node.name}' already has parent "
297303
f"{node.parent}. Cannot add to {self}."
298304
)
305+
306+
if node.environment != self.environment:
307+
raise ValueError(
308+
f"DeploymentNode {node.name} cannot be in a different environment "
309+
f"({node.environment}) from its parent ({self.environment})."
310+
)
311+
299312
self._children.add(node)
300313
if self.has_model:
301314
model = self.model

tests/unit/model/test_deployment_node.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from structurizr.model import Container, Relationship, SoftwareSystem
2020
from structurizr.model.deployment_node import DeploymentNode, DeploymentNodeIO
2121
from structurizr.model.infrastructure_node import InfrastructureNode
22+
from structurizr.model.tags import Tags
2223

2324

2425
class MockModel:
@@ -78,6 +79,7 @@ def test_deployment_node_init(attributes):
7879
node = DeploymentNode(**attributes)
7980
for attr, expected in attributes.items():
8081
assert getattr(node, attr) == expected
82+
assert Tags.DEPLOYMENT_NODE in node.tags
8183

8284

8385
def test_deployment_node_adds_to_children(model_with_node):
@@ -99,6 +101,25 @@ def test_deployment_node_adding_same_child_twice_is_ok(model_with_node):
99101
assert len(top_node.children) == 1
100102

101103

104+
def test_deployment_node_child_picks_up_environment(model_with_node):
105+
"""Ensure that the environment of the child matches the parent."""
106+
top_node = model_with_node.empty_node
107+
child = top_node.add_deployment_node(name="child")
108+
assert child.environment == "Live"
109+
110+
111+
def test_deployment_node_cant_add_child_in_different_environment(model_with_node):
112+
"""Ensure that the environment of the child matches the parent."""
113+
top_node = model_with_node.empty_node
114+
child = DeploymentNode(name="child", environment="Dev")
115+
with pytest.raises(
116+
ValueError,
117+
match=r"DeploymentNode .* cannot be in a different environment \(Dev\) from "
118+
+ r"its parent \(Live\)\.",
119+
):
120+
top_node += child
121+
122+
102123
def test_deployment_node_add_child_with_existing_parent(model_with_node: MockModel):
103124
"""Check that adding a node with an existing parent fails."""
104125
top_node = model_with_node.empty_node
@@ -156,7 +177,7 @@ def test_deployment_node_add_with_iadd(model_with_node: MockModel):
156177
model_with_node += system
157178
container = Container(name="container")
158179
system += container
159-
child_node = DeploymentNode(name="child")
180+
child_node = DeploymentNode(name="child", environment="Live")
160181
infra_node = InfrastructureNode(name="infra")
161182

162183
node += child_node

0 commit comments

Comments
 (0)