|
1 | 1 | from unittest.mock import MagicMock
|
| 2 | +from unittest.mock import call |
2 | 3 | from unittest.mock import patch
|
3 | 4 |
|
4 | 5 | import pytest
|
|
9 | 10 | from understack_workflows.oslo_event.keystone_project import KeystoneProjectEvent
|
10 | 11 | from understack_workflows.oslo_event.keystone_project import _keystone_project_tags
|
11 | 12 | from understack_workflows.oslo_event.keystone_project import handle_project_created
|
| 13 | +from understack_workflows.oslo_event.keystone_project import handle_project_updated |
12 | 14 |
|
13 | 15 |
|
14 | 16 | class TestKeystoneProjectEvent:
|
@@ -288,3 +290,267 @@ def test_handle_project_created_constants_used(
|
288 | 290 | aggregate_name="aggr02_n02_NVME", # AGGREGATE_NAME constant
|
289 | 291 | )
|
290 | 292 | mock_open.assert_called()
|
| 293 | + |
| 294 | + |
| 295 | +class TestHandleProjectUpdated: |
| 296 | + """Test cases for handle_project_updated function.""" |
| 297 | + |
| 298 | + @pytest.fixture |
| 299 | + def mock_conn(self): |
| 300 | + """Create a mock OpenStack connection.""" |
| 301 | + return MagicMock() |
| 302 | + |
| 303 | + @pytest.fixture |
| 304 | + def mock_nautobot(self): |
| 305 | + """Create a mock Nautobot instance.""" |
| 306 | + return MagicMock() |
| 307 | + |
| 308 | + @pytest.fixture |
| 309 | + def valid_update_event_data(self): |
| 310 | + """Create valid update event data for testing.""" |
| 311 | + return { |
| 312 | + "event_type": "identity.project.updated", |
| 313 | + "payload": {"target": {"id": "test-project-123"}}, |
| 314 | + } |
| 315 | + |
| 316 | + def test_handle_project_updated_wrong_event_type(self, mock_conn, mock_nautobot): |
| 317 | + """Test handling event with wrong event type.""" |
| 318 | + event_data = { |
| 319 | + "event_type": "identity.project.created", |
| 320 | + "payload": {"target": {"id": "test-project-123"}}, |
| 321 | + } |
| 322 | + |
| 323 | + result = handle_project_updated(mock_conn, mock_nautobot, event_data) |
| 324 | + assert result == 1 |
| 325 | + |
| 326 | + @patch("understack_workflows.oslo_event.keystone_project.NetAppManager") |
| 327 | + @patch("understack_workflows.oslo_event.keystone_project._keystone_project_tags") |
| 328 | + @patch("builtins.open") |
| 329 | + def test_handle_project_updated_svm_tag_added( |
| 330 | + self, |
| 331 | + mock_open, |
| 332 | + mock_tags, |
| 333 | + mock_netapp_class, |
| 334 | + mock_conn, |
| 335 | + mock_nautobot, |
| 336 | + valid_update_event_data, |
| 337 | + ): |
| 338 | + """Test project update when SVM_UNDERSTACK tag is added.""" |
| 339 | + # Project now has SVM tag |
| 340 | + mock_tags.return_value = ["tag1", SVM_PROJECT_TAG, "tag2"] |
| 341 | + |
| 342 | + mock_netapp_manager = MagicMock() |
| 343 | + mock_netapp_manager.check_if_svm_exists.return_value = ( |
| 344 | + False # SVM doesn't exist yet |
| 345 | + ) |
| 346 | + mock_netapp_manager.create_svm.return_value = "os-test-project-123" |
| 347 | + mock_netapp_class.return_value = mock_netapp_manager |
| 348 | + |
| 349 | + result = handle_project_updated( |
| 350 | + mock_conn, mock_nautobot, valid_update_event_data |
| 351 | + ) |
| 352 | + |
| 353 | + assert result == 0 |
| 354 | + mock_tags.assert_called_once_with(mock_conn, "test-project-123") |
| 355 | + mock_netapp_manager.check_if_svm_exists.assert_called_once_with( |
| 356 | + project_id="test-project-123" |
| 357 | + ) |
| 358 | + mock_netapp_manager.create_svm.assert_called_once_with( |
| 359 | + project_id="test-project-123", aggregate_name=AGGREGATE_NAME |
| 360 | + ) |
| 361 | + mock_netapp_manager.create_volume.assert_called_once_with( |
| 362 | + project_id="test-project-123", |
| 363 | + volume_size=VOLUME_SIZE, |
| 364 | + aggregate_name=AGGREGATE_NAME, |
| 365 | + ) |
| 366 | + |
| 367 | + @patch("understack_workflows.oslo_event.keystone_project.NetAppManager") |
| 368 | + @patch("understack_workflows.oslo_event.keystone_project._keystone_project_tags") |
| 369 | + @patch("builtins.open") |
| 370 | + def test_handle_project_updated_svm_tag_removed( |
| 371 | + self, |
| 372 | + mock_open, |
| 373 | + mock_tags, |
| 374 | + mock_netapp_class, |
| 375 | + mock_conn, |
| 376 | + mock_nautobot, |
| 377 | + valid_update_event_data, |
| 378 | + ): |
| 379 | + """Test project update when SVM_UNDERSTACK tag is removed.""" |
| 380 | + # Project no longer has SVM tag |
| 381 | + mock_tags.return_value = ["tag1", "tag2"] |
| 382 | + |
| 383 | + mock_netapp_manager = MagicMock() |
| 384 | + mock_netapp_manager.check_if_svm_exists.return_value = ( |
| 385 | + True # SVM exists but tag removed |
| 386 | + ) |
| 387 | + mock_netapp_class.return_value = mock_netapp_manager |
| 388 | + |
| 389 | + result = handle_project_updated( |
| 390 | + mock_conn, mock_nautobot, valid_update_event_data |
| 391 | + ) |
| 392 | + |
| 393 | + assert result == 0 |
| 394 | + mock_tags.assert_called_once_with(mock_conn, "test-project-123") |
| 395 | + mock_netapp_manager.check_if_svm_exists.assert_called_once_with( |
| 396 | + project_id="test-project-123" |
| 397 | + ) |
| 398 | + # Should not create SVM or volume when tag is removed |
| 399 | + mock_netapp_manager.create_svm.assert_not_called() |
| 400 | + mock_netapp_manager.create_volume.assert_not_called() |
| 401 | + |
| 402 | + @patch("understack_workflows.oslo_event.keystone_project.NetAppManager") |
| 403 | + @patch("understack_workflows.oslo_event.keystone_project._keystone_project_tags") |
| 404 | + @patch("builtins.open") |
| 405 | + def test_handle_project_updated_random_tag_added( |
| 406 | + self, |
| 407 | + mock_open, |
| 408 | + mock_tags, |
| 409 | + mock_netapp_class, |
| 410 | + mock_conn, |
| 411 | + mock_nautobot, |
| 412 | + valid_update_event_data, |
| 413 | + ): |
| 414 | + """Test project update when random_text tag is added (no SVM tag).""" |
| 415 | + # Project has random tag but not SVM tag |
| 416 | + mock_tags.return_value = ["tag1", "random_text", "tag2"] |
| 417 | + |
| 418 | + mock_netapp_manager = MagicMock() |
| 419 | + mock_netapp_manager.check_if_svm_exists.return_value = False |
| 420 | + mock_netapp_class.return_value = mock_netapp_manager |
| 421 | + |
| 422 | + result = handle_project_updated( |
| 423 | + mock_conn, mock_nautobot, valid_update_event_data |
| 424 | + ) |
| 425 | + |
| 426 | + assert result == 0 |
| 427 | + mock_tags.assert_called_once_with(mock_conn, "test-project-123") |
| 428 | + mock_netapp_manager.check_if_svm_exists.assert_called_once_with( |
| 429 | + project_id="test-project-123" |
| 430 | + ) |
| 431 | + # Should not create SVM or volume when no SVM tag |
| 432 | + mock_netapp_manager.create_svm.assert_not_called() |
| 433 | + mock_netapp_manager.create_volume.assert_not_called() |
| 434 | + |
| 435 | + @patch("understack_workflows.oslo_event.keystone_project.NetAppManager") |
| 436 | + @patch("understack_workflows.oslo_event.keystone_project._keystone_project_tags") |
| 437 | + @patch("builtins.open") |
| 438 | + def test_handle_project_updated_svm_exists_and_tag_exists( |
| 439 | + self, |
| 440 | + mock_open, |
| 441 | + mock_tags, |
| 442 | + mock_netapp_class, |
| 443 | + mock_conn, |
| 444 | + mock_nautobot, |
| 445 | + valid_update_event_data, |
| 446 | + ): |
| 447 | + """Test project update when SVM tag exists and SVM already exists.""" |
| 448 | + # Project has SVM tag and SVM already exists |
| 449 | + mock_tags.return_value = [SVM_PROJECT_TAG, "tag1"] |
| 450 | + |
| 451 | + mock_netapp_manager = MagicMock() |
| 452 | + mock_netapp_manager.check_if_svm_exists.return_value = True |
| 453 | + mock_netapp_class.return_value = mock_netapp_manager |
| 454 | + |
| 455 | + result = handle_project_updated( |
| 456 | + mock_conn, mock_nautobot, valid_update_event_data |
| 457 | + ) |
| 458 | + |
| 459 | + assert result == 0 |
| 460 | + mock_tags.assert_called_once_with(mock_conn, "test-project-123") |
| 461 | + mock_netapp_manager.check_if_svm_exists.assert_called_once_with( |
| 462 | + project_id="test-project-123" |
| 463 | + ) |
| 464 | + # Should not create SVM or volume when both exist |
| 465 | + mock_netapp_manager.create_svm.assert_not_called() |
| 466 | + mock_netapp_manager.create_volume.assert_not_called() |
| 467 | + |
| 468 | + @patch("understack_workflows.oslo_event.keystone_project.NetAppManager") |
| 469 | + @patch("understack_workflows.oslo_event.keystone_project._keystone_project_tags") |
| 470 | + @patch("builtins.open") |
| 471 | + def test_handle_project_updated_netapp_manager_failure( |
| 472 | + self, |
| 473 | + mock_open, |
| 474 | + mock_tags, |
| 475 | + mock_netapp_class, |
| 476 | + mock_conn, |
| 477 | + mock_nautobot, |
| 478 | + valid_update_event_data, |
| 479 | + ): |
| 480 | + """Test handling when NetAppManager creation fails during update.""" |
| 481 | + mock_tags.return_value = [SVM_PROJECT_TAG] |
| 482 | + mock_netapp_class.side_effect = Exception("NetApp connection failed") |
| 483 | + |
| 484 | + with pytest.raises(Exception, match="NetApp connection failed"): |
| 485 | + handle_project_updated(mock_conn, mock_nautobot, valid_update_event_data) |
| 486 | + |
| 487 | + mock_tags.assert_called_once_with(mock_conn, "test-project-123") |
| 488 | + mock_netapp_class.assert_called_once() |
| 489 | + |
| 490 | + @patch("understack_workflows.oslo_event.keystone_project.NetAppManager") |
| 491 | + @patch("understack_workflows.oslo_event.keystone_project._keystone_project_tags") |
| 492 | + @patch("builtins.open") |
| 493 | + def test_handle_project_updated_svm_creation_failure( |
| 494 | + self, |
| 495 | + mock_open, |
| 496 | + mock_tags, |
| 497 | + mock_netapp_class, |
| 498 | + mock_conn, |
| 499 | + mock_nautobot, |
| 500 | + valid_update_event_data, |
| 501 | + ): |
| 502 | + """Test handling when SVM creation fails during update.""" |
| 503 | + mock_tags.return_value = [SVM_PROJECT_TAG] |
| 504 | + |
| 505 | + mock_netapp_manager = MagicMock() |
| 506 | + mock_netapp_manager.check_if_svm_exists.return_value = False |
| 507 | + mock_netapp_manager.create_svm.side_effect = Exception("SVM creation failed") |
| 508 | + mock_netapp_class.return_value = mock_netapp_manager |
| 509 | + |
| 510 | + with pytest.raises(Exception, match="SVM creation failed"): |
| 511 | + handle_project_updated(mock_conn, mock_nautobot, valid_update_event_data) |
| 512 | + |
| 513 | + mock_tags.assert_called_once_with(mock_conn, "test-project-123") |
| 514 | + mock_netapp_manager.check_if_svm_exists.assert_called_once_with( |
| 515 | + project_id="test-project-123" |
| 516 | + ) |
| 517 | + mock_netapp_manager.create_svm.assert_called_once_with( |
| 518 | + project_id="test-project-123", aggregate_name=AGGREGATE_NAME |
| 519 | + ) |
| 520 | + |
| 521 | + def test_handle_project_updated_invalid_event_data(self, mock_conn, mock_nautobot): |
| 522 | + """Test handling update with invalid event data.""" |
| 523 | + invalid_event_data = { |
| 524 | + "event_type": "identity.project.updated", |
| 525 | + "payload": {}, # Missing target |
| 526 | + } |
| 527 | + |
| 528 | + with pytest.raises(Exception, match="no target information in payload"): |
| 529 | + handle_project_updated(mock_conn, mock_nautobot, invalid_event_data) |
| 530 | + |
| 531 | + @patch("understack_workflows.oslo_event.keystone_project._keystone_project_tags") |
| 532 | + @patch("builtins.open") |
| 533 | + def test_handle_project_updated_output_files_written( |
| 534 | + self, mock_open, mock_tags, mock_conn, mock_nautobot, valid_update_event_data |
| 535 | + ): |
| 536 | + """Test that output files are written correctly during update.""" |
| 537 | + mock_tags.return_value = ["random_tag"] |
| 538 | + |
| 539 | + with patch( |
| 540 | + "understack_workflows.oslo_event.keystone_project.NetAppManager" |
| 541 | + ) as mock_netapp_class: |
| 542 | + mock_netapp_manager = MagicMock() |
| 543 | + mock_netapp_manager.check_if_svm_exists.return_value = False |
| 544 | + mock_netapp_class.return_value = mock_netapp_manager |
| 545 | + |
| 546 | + result = handle_project_updated( |
| 547 | + mock_conn, mock_nautobot, valid_update_event_data |
| 548 | + ) |
| 549 | + |
| 550 | + assert result == 0 |
| 551 | + # Verify output files are written |
| 552 | + expected_calls = [ |
| 553 | + call("/var/run/argo/output.svm_enabled", "w"), |
| 554 | + call("/var/run/argo/output.svm_name", "w"), |
| 555 | + ] |
| 556 | + mock_open.assert_has_calls(expected_calls, any_order=True) |
0 commit comments