Skip to content

Commit 2154013

Browse files
committed
unify the outputs and behaviors
- no direct exit() calls, these are handled by oslo_event processor - always save the svm_enabled output so it can be relied upon later
1 parent 26a3e88 commit 2154013

File tree

3 files changed

+69
-33
lines changed

3 files changed

+69
-33
lines changed

python/understack-workflows/pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ testpaths = [
7474
]
7575
filterwarnings = [
7676
# sushy
77-
"ignore:pkg_resources is deprecated as an API.:DeprecationWarning"
77+
"ignore:pkg_resources is deprecated as an API.:DeprecationWarning",
78+
# netapp
79+
"ignore:.*Number` field should not be instantiated.*"
7880
]
7981

8082
[tool.ruff]

python/understack-workflows/tests/test_keystone_project.py

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -123,22 +123,29 @@ def test_handle_project_created_wrong_event_type(self, mock_conn, mock_nautobot)
123123
assert result == 1
124124

125125
@patch("understack_workflows.oslo_event.keystone_project._keystone_project_tags")
126+
@patch("builtins.open")
126127
def test_handle_project_created_no_svm_tag(
127-
self, mock_tags, mock_conn, mock_nautobot, valid_event_data
128+
self, mock_open, mock_tags, mock_conn, mock_nautobot, valid_event_data
128129
):
129130
"""Test handling project creation without SVM tag."""
130131
mock_tags.return_value = ["tag1", "tag2"]
131132

132-
with pytest.raises(SystemExit) as exc_info:
133-
handle_project_created(mock_conn, mock_nautobot, valid_event_data)
134-
135-
assert exc_info.value.code == 0
133+
result = handle_project_created(mock_conn, mock_nautobot, valid_event_data)
134+
assert result == 0
136135
mock_tags.assert_called_once_with(mock_conn, "test-project-123")
136+
mock_open.assert_called_with("/var/run/argo/output.svm_enabled", "w")
137137

138138
@patch("understack_workflows.oslo_event.keystone_project.NetAppManager")
139139
@patch("understack_workflows.oslo_event.keystone_project._keystone_project_tags")
140+
@patch("builtins.open")
140141
def test_handle_project_created_with_svm_tag(
141-
self, mock_tags, mock_netapp_class, mock_conn, mock_nautobot, valid_event_data
142+
self,
143+
mock_open,
144+
mock_tags,
145+
mock_netapp_class,
146+
mock_conn,
147+
mock_nautobot,
148+
valid_event_data,
142149
):
143150
"""Test successful project creation handling with SVM tag."""
144151
mock_tags.return_value = ["tag1", SVM_PROJECT_TAG, "tag2"]
@@ -158,11 +165,19 @@ def test_handle_project_created_with_svm_tag(
158165
volume_size=VOLUME_SIZE,
159166
aggregate_name=AGGREGATE_NAME,
160167
)
168+
mock_open.assert_called()
161169

162170
@patch("understack_workflows.oslo_event.keystone_project.NetAppManager")
163171
@patch("understack_workflows.oslo_event.keystone_project._keystone_project_tags")
172+
@patch("builtins.open")
164173
def test_handle_project_created_netapp_manager_failure(
165-
self, mock_tags, mock_netapp_class, mock_conn, mock_nautobot, valid_event_data
174+
self,
175+
mock_open,
176+
mock_tags,
177+
mock_netapp_class,
178+
mock_conn,
179+
mock_nautobot,
180+
valid_event_data,
166181
):
167182
"""Test handling when NetAppManager creation fails."""
168183
mock_tags.return_value = [SVM_PROJECT_TAG]
@@ -173,11 +188,19 @@ def test_handle_project_created_netapp_manager_failure(
173188

174189
mock_tags.assert_called_once_with(mock_conn, "test-project-123")
175190
mock_netapp_class.assert_called_once()
191+
mock_open.assert_called()
176192

177193
@patch("understack_workflows.oslo_event.keystone_project.NetAppManager")
178194
@patch("understack_workflows.oslo_event.keystone_project._keystone_project_tags")
195+
@patch("builtins.open")
179196
def test_handle_project_created_svm_creation_failure(
180-
self, mock_tags, mock_netapp_class, mock_conn, mock_nautobot, valid_event_data
197+
self,
198+
mock_open,
199+
mock_tags,
200+
mock_netapp_class,
201+
mock_conn,
202+
mock_nautobot,
203+
valid_event_data,
181204
):
182205
"""Test handling when SVM creation fails."""
183206
mock_tags.return_value = [SVM_PROJECT_TAG]
@@ -196,8 +219,15 @@ def test_handle_project_created_svm_creation_failure(
196219

197220
@patch("understack_workflows.oslo_event.keystone_project.NetAppManager")
198221
@patch("understack_workflows.oslo_event.keystone_project._keystone_project_tags")
222+
@patch("builtins.open")
199223
def test_handle_project_created_volume_creation_failure(
200-
self, mock_tags, mock_netapp_class, mock_conn, mock_nautobot, valid_event_data
224+
self,
225+
mock_open,
226+
mock_tags,
227+
mock_netapp_class,
228+
mock_conn,
229+
mock_nautobot,
230+
valid_event_data,
201231
):
202232
"""Test handling when volume creation fails."""
203233
mock_tags.return_value = [SVM_PROJECT_TAG]
@@ -232,8 +262,9 @@ def test_handle_project_created_invalid_event_data(self, mock_conn, mock_nautobo
232262
handle_project_created(mock_conn, mock_nautobot, invalid_event_data)
233263

234264
@patch("understack_workflows.oslo_event.keystone_project._keystone_project_tags")
265+
@patch("builtins.open")
235266
def test_handle_project_created_constants_used(
236-
self, mock_tags, mock_conn, mock_nautobot, valid_event_data
267+
self, mock_open, mock_tags, mock_conn, mock_nautobot, valid_event_data
237268
):
238269
"""Test constants used for aggregate name and volume size."""
239270
mock_tags.return_value = [SVM_PROJECT_TAG]
@@ -253,6 +284,7 @@ def test_handle_project_created_constants_used(
253284
)
254285
mock_netapp_manager.create_volume.assert_called_once_with(
255286
project_id="test-project-123",
256-
volume_size="1GB", # VOLUME_SIZE constant
287+
volume_size="514GB", # VOLUME_SIZE constant
257288
aggregate_name="aggr02_n02_NVME", # AGGREGATE_NAME constant
258289
)
290+
mock_open.assert_called()

python/understack-workflows/understack_workflows/oslo_event/keystone_project.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
SVM_PROJECT_TAG = "UNDERSTACK_SVM"
1313
AGGREGATE_NAME = "aggr02_n02_NVME"
1414
VOLUME_SIZE = "514GB"
15+
OUTPUT_BASE_PATH = "/var/run/argo"
1516

1617

1718
@dataclass
@@ -47,38 +48,39 @@ def handle_project_created(
4748
conn: Connection, _nautobot: Nautobot, event_data: dict
4849
) -> int:
4950
if event_data.get("event_type") != "identity.project.created":
51+
logger.error("Received event that is not identity.project.created")
5052
return 1
5153

5254
event = KeystoneProjectEvent.from_event_dict(event_data)
5355
logger.info("Starting ONTAP SVM and Volume creation workflow.")
5456
tags = _keystone_project_tags(conn, event.project_id)
5557
logger.debug("Project %s has tags: %s", event.project_id, tags)
56-
if SVM_PROJECT_TAG not in tags:
58+
59+
project_is_svm_enabled = SVM_PROJECT_TAG in tags
60+
_save_output("svm_enabled", str(project_is_svm_enabled))
61+
62+
if not project_is_svm_enabled:
5763
logger.info("The %s is missing, not creating SVM.", SVM_PROJECT_TAG)
5864
return 0
5965

60-
netapp_manager = NetAppManager()
61-
netapp_manager.create_svm(
62-
project_id=event.project_id, aggregate_name=AGGREGATE_NAME
63-
)
64-
svm_name = netapp_manager.create_volume(
65-
project_id=event.project_id,
66-
volume_size=VOLUME_SIZE,
67-
aggregate_name=AGGREGATE_NAME,
68-
)
69-
with open("/var/run/argo/output.svm_name", "w") as f: # noqa: S108
66+
svm_name = None
67+
try:
68+
netapp_manager = NetAppManager()
69+
netapp_manager.create_svm(
70+
project_id=event.project_id, aggregate_name=AGGREGATE_NAME
71+
)
72+
svm_name = netapp_manager.create_volume(
73+
project_id=event.project_id,
74+
volume_size=VOLUME_SIZE,
75+
aggregate_name=AGGREGATE_NAME,
76+
)
77+
finally:
7078
if not svm_name:
7179
svm_name = "not_returned"
72-
73-
f.write(svm_name)
74-
75-
_save_result("success", 0)
80+
_save_output("svm_name", svm_name)
7681
return 0
7782

7883

79-
def _save_result(msg, exit_code):
80-
with open("/var/run/argo/output.msg", "w") as f: # noqa: S108
81-
f.write(msg)
82-
with open("/var/run/argo/output.exit_code", "w") as f: # noqa: S108
83-
f.write(str(exit_code))
84-
exit(exit_code)
84+
def _save_output(name, value):
85+
with open(f"{OUTPUT_BASE_PATH}/output.{name}", "w") as f:
86+
return f.write(value)

0 commit comments

Comments
 (0)