|
| 1 | +from unittest.mock import MagicMock |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from understack_workflows.oslo_event.keystone_project import AGGREGATE_NAME |
| 7 | +from understack_workflows.oslo_event.keystone_project import SVM_PROJECT_TAG |
| 8 | +from understack_workflows.oslo_event.keystone_project import VOLUME_SIZE |
| 9 | +from understack_workflows.oslo_event.keystone_project import KeystoneProjectEvent |
| 10 | +from understack_workflows.oslo_event.keystone_project import _keystone_project_tags |
| 11 | +from understack_workflows.oslo_event.keystone_project import handle_project_created |
| 12 | + |
| 13 | + |
| 14 | +class TestKeystoneProjectEvent: |
| 15 | + """Test cases for KeystoneProjectEvent class.""" |
| 16 | + |
| 17 | + def test_from_event_dict_success(self): |
| 18 | + """Test successful event parsing.""" |
| 19 | + event_data = {"payload": {"target": {"id": "test-project-123"}}} |
| 20 | + |
| 21 | + event = KeystoneProjectEvent.from_event_dict(event_data) |
| 22 | + assert event.project_id == "test-project-123" |
| 23 | + |
| 24 | + def test_from_event_dict_no_payload(self): |
| 25 | + """Test event parsing with missing payload.""" |
| 26 | + event_data = {} |
| 27 | + |
| 28 | + with pytest.raises(Exception, match="Invalid event. No 'payload'"): |
| 29 | + KeystoneProjectEvent.from_event_dict(event_data) |
| 30 | + |
| 31 | + def test_from_event_dict_no_target(self): |
| 32 | + """Test event parsing with missing target.""" |
| 33 | + event_data = {"payload": {}} |
| 34 | + |
| 35 | + with pytest.raises(Exception, match="no target information in payload"): |
| 36 | + KeystoneProjectEvent.from_event_dict(event_data) |
| 37 | + |
| 38 | + def test_from_event_dict_no_project_id(self): |
| 39 | + """Test event parsing with missing project ID.""" |
| 40 | + event_data = {"payload": {"target": {}}} |
| 41 | + |
| 42 | + with pytest.raises(Exception, match="no project_id found in payload"): |
| 43 | + KeystoneProjectEvent.from_event_dict(event_data) |
| 44 | + |
| 45 | + def test_dataclass_initialization(self): |
| 46 | + """Test direct dataclass initialization.""" |
| 47 | + event = KeystoneProjectEvent("test-project-456") |
| 48 | + assert event.project_id == "test-project-456" |
| 49 | + |
| 50 | + |
| 51 | +class TestKeystoneProjectTags: |
| 52 | + """Test cases for _keystone_project_tags function.""" |
| 53 | + |
| 54 | + def test_project_tags_with_tags(self): |
| 55 | + """Test getting project tags when project has tags.""" |
| 56 | + mock_project = MagicMock() |
| 57 | + mock_project.tags = ["tag1", "tag2", SVM_PROJECT_TAG] |
| 58 | + |
| 59 | + mock_conn = MagicMock() |
| 60 | + mock_conn.identity.get_project.return_value = mock_project |
| 61 | + |
| 62 | + tags = _keystone_project_tags(mock_conn, "test-project-id") |
| 63 | + |
| 64 | + assert tags == ["tag1", "tag2", SVM_PROJECT_TAG] |
| 65 | + mock_conn.identity.get_project.assert_called_once_with("test-project-id") |
| 66 | + |
| 67 | + def test_project_tags_no_tags_attribute(self): |
| 68 | + """Test getting project tags when project has no tags attribute.""" |
| 69 | + mock_project = MagicMock() |
| 70 | + del mock_project.tags # Remove tags attribute |
| 71 | + |
| 72 | + mock_conn = MagicMock() |
| 73 | + mock_conn.identity.get_project.return_value = mock_project |
| 74 | + |
| 75 | + tags = _keystone_project_tags(mock_conn, "test-project-id") |
| 76 | + |
| 77 | + assert tags == [] |
| 78 | + mock_conn.identity.get_project.assert_called_once_with("test-project-id") |
| 79 | + |
| 80 | + def test_project_tags_empty_tags(self): |
| 81 | + """Test getting project tags when project has empty tags.""" |
| 82 | + mock_project = MagicMock() |
| 83 | + mock_project.tags = [] |
| 84 | + |
| 85 | + mock_conn = MagicMock() |
| 86 | + mock_conn.identity.get_project.return_value = mock_project |
| 87 | + |
| 88 | + tags = _keystone_project_tags(mock_conn, "test-project-id") |
| 89 | + |
| 90 | + assert tags == [] |
| 91 | + mock_conn.identity.get_project.assert_called_once_with("test-project-id") |
| 92 | + |
| 93 | + |
| 94 | +class TestHandleProjectCreated: |
| 95 | + """Test cases for handle_project_created function.""" |
| 96 | + |
| 97 | + @pytest.fixture |
| 98 | + def mock_conn(self): |
| 99 | + """Create a mock OpenStack connection.""" |
| 100 | + return MagicMock() |
| 101 | + |
| 102 | + @pytest.fixture |
| 103 | + def mock_nautobot(self): |
| 104 | + """Create a mock Nautobot instance.""" |
| 105 | + return MagicMock() |
| 106 | + |
| 107 | + @pytest.fixture |
| 108 | + def valid_event_data(self): |
| 109 | + """Create valid event data for testing.""" |
| 110 | + return { |
| 111 | + "event_type": "identity.project.created", |
| 112 | + "payload": {"target": {"id": "test-project-123"}}, |
| 113 | + } |
| 114 | + |
| 115 | + def test_handle_project_created_wrong_event_type(self, mock_conn, mock_nautobot): |
| 116 | + """Test handling event with wrong event type.""" |
| 117 | + event_data = { |
| 118 | + "event_type": "identity.project.updated", |
| 119 | + "payload": {"target": {"id": "test-project-123"}}, |
| 120 | + } |
| 121 | + |
| 122 | + result = handle_project_created(mock_conn, mock_nautobot, event_data) |
| 123 | + assert result == 1 |
| 124 | + |
| 125 | + @patch("understack_workflows.oslo_event.keystone_project._keystone_project_tags") |
| 126 | + def test_handle_project_created_no_svm_tag( |
| 127 | + self, mock_tags, mock_conn, mock_nautobot, valid_event_data |
| 128 | + ): |
| 129 | + """Test handling project creation without SVM tag.""" |
| 130 | + mock_tags.return_value = ["tag1", "tag2"] |
| 131 | + |
| 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 |
| 136 | + mock_tags.assert_called_once_with(mock_conn, "test-project-123") |
| 137 | + |
| 138 | + @patch("understack_workflows.oslo_event.keystone_project.NetAppManager") |
| 139 | + @patch("understack_workflows.oslo_event.keystone_project._keystone_project_tags") |
| 140 | + def test_handle_project_created_with_svm_tag( |
| 141 | + self, mock_tags, mock_netapp_class, mock_conn, mock_nautobot, valid_event_data |
| 142 | + ): |
| 143 | + """Test successful project creation handling with SVM tag.""" |
| 144 | + mock_tags.return_value = ["tag1", SVM_PROJECT_TAG, "tag2"] |
| 145 | + mock_netapp_manager = MagicMock() |
| 146 | + mock_netapp_class.return_value = mock_netapp_manager |
| 147 | + |
| 148 | + result = handle_project_created(mock_conn, mock_nautobot, valid_event_data) |
| 149 | + |
| 150 | + assert result == 0 |
| 151 | + mock_tags.assert_called_once_with(mock_conn, "test-project-123") |
| 152 | + mock_netapp_class.assert_called_once() |
| 153 | + mock_netapp_manager.create_svm.assert_called_once_with( |
| 154 | + project_id="test-project-123", aggregate_name=AGGREGATE_NAME |
| 155 | + ) |
| 156 | + mock_netapp_manager.create_volume.assert_called_once_with( |
| 157 | + project_id="test-project-123", |
| 158 | + volume_size=VOLUME_SIZE, |
| 159 | + aggregate_name=AGGREGATE_NAME, |
| 160 | + ) |
| 161 | + |
| 162 | + @patch("understack_workflows.oslo_event.keystone_project.NetAppManager") |
| 163 | + @patch("understack_workflows.oslo_event.keystone_project._keystone_project_tags") |
| 164 | + def test_handle_project_created_netapp_manager_failure( |
| 165 | + self, mock_tags, mock_netapp_class, mock_conn, mock_nautobot, valid_event_data |
| 166 | + ): |
| 167 | + """Test handling when NetAppManager creation fails.""" |
| 168 | + mock_tags.return_value = [SVM_PROJECT_TAG] |
| 169 | + mock_netapp_class.side_effect = Exception("NetApp connection failed") |
| 170 | + |
| 171 | + with pytest.raises(Exception, match="NetApp connection failed"): |
| 172 | + handle_project_created(mock_conn, mock_nautobot, valid_event_data) |
| 173 | + |
| 174 | + mock_tags.assert_called_once_with(mock_conn, "test-project-123") |
| 175 | + mock_netapp_class.assert_called_once() |
| 176 | + |
| 177 | + @patch("understack_workflows.oslo_event.keystone_project.NetAppManager") |
| 178 | + @patch("understack_workflows.oslo_event.keystone_project._keystone_project_tags") |
| 179 | + def test_handle_project_created_svm_creation_failure( |
| 180 | + self, mock_tags, mock_netapp_class, mock_conn, mock_nautobot, valid_event_data |
| 181 | + ): |
| 182 | + """Test handling when SVM creation fails.""" |
| 183 | + mock_tags.return_value = [SVM_PROJECT_TAG] |
| 184 | + mock_netapp_manager = MagicMock() |
| 185 | + mock_netapp_manager.create_svm.side_effect = Exception("SVM creation failed") |
| 186 | + mock_netapp_class.return_value = mock_netapp_manager |
| 187 | + |
| 188 | + with pytest.raises(Exception, match="SVM creation failed"): |
| 189 | + handle_project_created(mock_conn, mock_nautobot, valid_event_data) |
| 190 | + |
| 191 | + mock_tags.assert_called_once_with(mock_conn, "test-project-123") |
| 192 | + mock_netapp_class.assert_called_once() |
| 193 | + mock_netapp_manager.create_svm.assert_called_once_with( |
| 194 | + project_id="test-project-123", aggregate_name=AGGREGATE_NAME |
| 195 | + ) |
| 196 | + |
| 197 | + @patch("understack_workflows.oslo_event.keystone_project.NetAppManager") |
| 198 | + @patch("understack_workflows.oslo_event.keystone_project._keystone_project_tags") |
| 199 | + def test_handle_project_created_volume_creation_failure( |
| 200 | + self, mock_tags, mock_netapp_class, mock_conn, mock_nautobot, valid_event_data |
| 201 | + ): |
| 202 | + """Test handling when volume creation fails.""" |
| 203 | + mock_tags.return_value = [SVM_PROJECT_TAG] |
| 204 | + mock_netapp_manager = MagicMock() |
| 205 | + mock_netapp_manager.create_volume.side_effect = Exception( |
| 206 | + "Volume creation failed" |
| 207 | + ) |
| 208 | + mock_netapp_class.return_value = mock_netapp_manager |
| 209 | + |
| 210 | + with pytest.raises(Exception, match="Volume creation failed"): |
| 211 | + handle_project_created(mock_conn, mock_nautobot, valid_event_data) |
| 212 | + |
| 213 | + mock_tags.assert_called_once_with(mock_conn, "test-project-123") |
| 214 | + mock_netapp_class.assert_called_once() |
| 215 | + mock_netapp_manager.create_svm.assert_called_once_with( |
| 216 | + project_id="test-project-123", aggregate_name=AGGREGATE_NAME |
| 217 | + ) |
| 218 | + mock_netapp_manager.create_volume.assert_called_once_with( |
| 219 | + project_id="test-project-123", |
| 220 | + volume_size=VOLUME_SIZE, |
| 221 | + aggregate_name=AGGREGATE_NAME, |
| 222 | + ) |
| 223 | + |
| 224 | + def test_handle_project_created_invalid_event_data(self, mock_conn, mock_nautobot): |
| 225 | + """Test handling with invalid event data.""" |
| 226 | + invalid_event_data = { |
| 227 | + "event_type": "identity.project.created", |
| 228 | + "payload": {}, # Missing target |
| 229 | + } |
| 230 | + |
| 231 | + with pytest.raises(Exception, match="no target information in payload"): |
| 232 | + handle_project_created(mock_conn, mock_nautobot, invalid_event_data) |
| 233 | + |
| 234 | + @patch("understack_workflows.oslo_event.keystone_project._keystone_project_tags") |
| 235 | + def test_handle_project_created_constants_used( |
| 236 | + self, mock_tags, mock_conn, mock_nautobot, valid_event_data |
| 237 | + ): |
| 238 | + """Test constants used for aggregate name and volume size.""" |
| 239 | + mock_tags.return_value = [SVM_PROJECT_TAG] |
| 240 | + |
| 241 | + with patch( |
| 242 | + "understack_workflows.oslo_event.keystone_project.NetAppManager" |
| 243 | + ) as mock_netapp_class: |
| 244 | + mock_netapp_manager = MagicMock() |
| 245 | + mock_netapp_class.return_value = mock_netapp_manager |
| 246 | + |
| 247 | + handle_project_created(mock_conn, mock_nautobot, valid_event_data) |
| 248 | + |
| 249 | + # Verify the constants are used correctly |
| 250 | + mock_netapp_manager.create_svm.assert_called_once_with( |
| 251 | + project_id="test-project-123", |
| 252 | + aggregate_name="aggr02_n02_NVME", # AGGREGATE_NAME constant |
| 253 | + ) |
| 254 | + mock_netapp_manager.create_volume.assert_called_once_with( |
| 255 | + project_id="test-project-123", |
| 256 | + volume_size="1GB", # VOLUME_SIZE constant |
| 257 | + aggregate_name="aggr02_n02_NVME", # AGGREGATE_NAME constant |
| 258 | + ) |
0 commit comments