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

Commit

Permalink
fix(DeploymentNode): children should inherit environment from parent
Browse files Browse the repository at this point in the history
  • Loading branch information
yt-ms authored and Midnighter committed Nov 29, 2020
1 parent 8d76282 commit 1e9d748
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/structurizr/model/deployment_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from .relationship import Relationship
from .software_system import SoftwareSystem
from .software_system_instance import SoftwareSystemInstance, SoftwareSystemInstanceIO
from .tags import Tags


if TYPE_CHECKING: # pragma: no cover
Expand Down Expand Up @@ -114,6 +115,7 @@ def __init__(
self._container_instances = set(container_instances)
self._software_system_instances = set(software_system_instances)
self._infrastructure_nodes = set(infrastructure_nodes)
self.tags.add(Tags.DEPLOYMENT_NODE)

@property
def children(self) -> Iterable["DeploymentNode"]:
Expand Down Expand Up @@ -158,7 +160,11 @@ def add_deployment_node(
**kwargs: additional keyword arguments for instantiating a `DeploymentNode`
"""
node = DeploymentNode(
name=name, description=description, technology=technology, **kwargs
name=name,
description=description,
technology=technology,
environment=self.environment,
**kwargs,
)
self._add_child_deployment_node(node)
return node
Expand Down Expand Up @@ -296,6 +302,13 @@ def _add_child_deployment_node(self, node: "DeploymentNode"):
f"DeploymentNode with name '{node.name}' already has parent "
f"{node.parent}. Cannot add to {self}."
)

if node.environment != self.environment:
raise ValueError(
f"DeploymentNode {node.name} cannot be in a different environment "
f"({node.environment}) from its parent ({self.environment})."
)

self._children.add(node)
if self.has_model:
model = self.model
Expand Down
23 changes: 22 additions & 1 deletion tests/unit/model/test_deployment_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from structurizr.model import Container, Relationship, SoftwareSystem
from structurizr.model.deployment_node import DeploymentNode, DeploymentNodeIO
from structurizr.model.infrastructure_node import InfrastructureNode
from structurizr.model.tags import Tags


class MockModel:
Expand Down Expand Up @@ -78,6 +79,7 @@ def test_deployment_node_init(attributes):
node = DeploymentNode(**attributes)
for attr, expected in attributes.items():
assert getattr(node, attr) == expected
assert Tags.DEPLOYMENT_NODE in node.tags


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


def test_deployment_node_child_picks_up_environment(model_with_node):
"""Ensure that the environment of the child matches the parent."""
top_node = model_with_node.empty_node
child = top_node.add_deployment_node(name="child")
assert child.environment == "Live"


def test_deployment_node_cant_add_child_in_different_environment(model_with_node):
"""Ensure that the environment of the child matches the parent."""
top_node = model_with_node.empty_node
child = DeploymentNode(name="child", environment="Dev")
with pytest.raises(
ValueError,
match=r"DeploymentNode .* cannot be in a different environment \(Dev\) from "
+ r"its parent \(Live\)\.",
):
top_node += child


def test_deployment_node_add_child_with_existing_parent(model_with_node: MockModel):
"""Check that adding a node with an existing parent fails."""
top_node = model_with_node.empty_node
Expand Down Expand Up @@ -156,7 +177,7 @@ def test_deployment_node_add_with_iadd(model_with_node: MockModel):
model_with_node += system
container = Container(name="container")
system += container
child_node = DeploymentNode(name="child")
child_node = DeploymentNode(name="child", environment="Live")
infra_node = InfrastructureNode(name="infra")

node += child_node
Expand Down

0 comments on commit 1e9d748

Please sign in to comment.