|
| 1 | +"""Tests for resync_keystone_to_nautobot module.""" |
| 2 | + |
| 3 | +from unittest.mock import MagicMock |
| 4 | +from unittest.mock import patch |
| 5 | + |
| 6 | +from understack_workflows.main.resync_keystone_to_nautobot import argument_parser |
| 7 | +from understack_workflows.main.resync_keystone_to_nautobot import main |
| 8 | +from understack_workflows.main.resync_keystone_to_nautobot import sync_projects |
| 9 | +from understack_workflows.main.sync_keystone import is_domain |
| 10 | +from understack_workflows.resync import SyncResult |
| 11 | + |
| 12 | + |
| 13 | +class TestIsDomain: |
| 14 | + """Test cases for is_domain helper function.""" |
| 15 | + |
| 16 | + def test_is_domain_true(self): |
| 17 | + project = MagicMock() |
| 18 | + project.is_domain = True |
| 19 | + assert is_domain(project) is True |
| 20 | + |
| 21 | + def test_is_domain_false(self): |
| 22 | + project = MagicMock() |
| 23 | + project.is_domain = False |
| 24 | + assert is_domain(project) is False |
| 25 | + |
| 26 | + def test_is_domain_missing_attr(self): |
| 27 | + project = MagicMock(spec=[]) |
| 28 | + assert is_domain(project) is False |
| 29 | + |
| 30 | + |
| 31 | +class TestArgumentParser: |
| 32 | + """Test cases for argument_parser function.""" |
| 33 | + |
| 34 | + def test_default_args(self): |
| 35 | + parser = argument_parser() |
| 36 | + args = parser.parse_args([]) |
| 37 | + assert args.project is None |
| 38 | + assert args.dry_run is False |
| 39 | + |
| 40 | + def test_project_arg(self): |
| 41 | + parser = argument_parser() |
| 42 | + args = parser.parse_args(["--project", "test-uuid"]) |
| 43 | + assert args.project == "test-uuid" |
| 44 | + |
| 45 | + def test_dry_run_arg(self): |
| 46 | + parser = argument_parser() |
| 47 | + args = parser.parse_args(["--dry-run"]) |
| 48 | + assert args.dry_run is True |
| 49 | + |
| 50 | + |
| 51 | +class TestSyncProjects: |
| 52 | + """Test cases for sync_projects function.""" |
| 53 | + |
| 54 | + def test_sync_all_projects_success(self): |
| 55 | + conn = MagicMock() |
| 56 | + project1 = MagicMock( |
| 57 | + id="12345678-1234-5678-1234-567812345678", |
| 58 | + name="project-1", |
| 59 | + is_domain=False, |
| 60 | + ) |
| 61 | + project2 = MagicMock( |
| 62 | + id="87654321-4321-8765-4321-876543218765", |
| 63 | + name="project-2", |
| 64 | + is_domain=False, |
| 65 | + ) |
| 66 | + conn.identity.projects.return_value = [project1, project2] |
| 67 | + |
| 68 | + nautobot = MagicMock() |
| 69 | + |
| 70 | + with patch( |
| 71 | + "understack_workflows.main.resync_keystone_to_nautobot.handle_project_update" |
| 72 | + ) as mock_update: |
| 73 | + mock_update.return_value = 0 |
| 74 | + result = sync_projects(conn, nautobot) |
| 75 | + |
| 76 | + assert result.total == 2 |
| 77 | + assert result.failed == 0 |
| 78 | + assert result.skipped == 0 |
| 79 | + assert mock_update.call_count == 2 |
| 80 | + |
| 81 | + def test_sync_single_project(self): |
| 82 | + conn = MagicMock() |
| 83 | + project = MagicMock( |
| 84 | + id="12345678-1234-5678-1234-567812345678", |
| 85 | + name="project-1", |
| 86 | + is_domain=False, |
| 87 | + ) |
| 88 | + conn.identity.get_project.return_value = project |
| 89 | + |
| 90 | + nautobot = MagicMock() |
| 91 | + |
| 92 | + with patch( |
| 93 | + "understack_workflows.main.resync_keystone_to_nautobot.handle_project_update" |
| 94 | + ) as mock_update: |
| 95 | + mock_update.return_value = 0 |
| 96 | + result = sync_projects( |
| 97 | + conn, nautobot, project_uuid="12345678-1234-5678-1234-567812345678" |
| 98 | + ) |
| 99 | + |
| 100 | + assert result.total == 1 |
| 101 | + assert result.failed == 0 |
| 102 | + conn.identity.get_project.assert_called_once_with( |
| 103 | + "12345678-1234-5678-1234-567812345678" |
| 104 | + ) |
| 105 | + |
| 106 | + def test_sync_with_failures(self): |
| 107 | + conn = MagicMock() |
| 108 | + project1 = MagicMock( |
| 109 | + id="12345678-1234-5678-1234-567812345678", |
| 110 | + name="project-1", |
| 111 | + is_domain=False, |
| 112 | + ) |
| 113 | + project2 = MagicMock( |
| 114 | + id="87654321-4321-8765-4321-876543218765", |
| 115 | + name="project-2", |
| 116 | + is_domain=False, |
| 117 | + ) |
| 118 | + conn.identity.projects.return_value = [project1, project2] |
| 119 | + |
| 120 | + nautobot = MagicMock() |
| 121 | + |
| 122 | + with patch( |
| 123 | + "understack_workflows.main.resync_keystone_to_nautobot.handle_project_update" |
| 124 | + ) as mock_update: |
| 125 | + mock_update.side_effect = [0, 1] # First succeeds, second fails |
| 126 | + result = sync_projects(conn, nautobot) |
| 127 | + |
| 128 | + assert result.total == 2 |
| 129 | + assert result.failed == 1 |
| 130 | + assert result.succeeded == 1 |
| 131 | + |
| 132 | + def test_sync_skips_domains(self): |
| 133 | + conn = MagicMock() |
| 134 | + project = MagicMock( |
| 135 | + id="12345678-1234-5678-1234-567812345678", |
| 136 | + name="project-1", |
| 137 | + is_domain=False, |
| 138 | + ) |
| 139 | + domain = MagicMock( |
| 140 | + id="87654321-4321-8765-4321-876543218765", |
| 141 | + name="domain-1", |
| 142 | + is_domain=True, |
| 143 | + ) |
| 144 | + conn.identity.projects.return_value = [project, domain] |
| 145 | + |
| 146 | + nautobot = MagicMock() |
| 147 | + |
| 148 | + with patch( |
| 149 | + "understack_workflows.main.resync_keystone_to_nautobot.handle_project_update" |
| 150 | + ) as mock_update: |
| 151 | + mock_update.return_value = 0 |
| 152 | + result = sync_projects(conn, nautobot) |
| 153 | + |
| 154 | + assert result.total == 2 |
| 155 | + assert result.skipped == 1 |
| 156 | + assert result.succeeded == 1 |
| 157 | + mock_update.assert_called_once() |
| 158 | + |
| 159 | + def test_dry_run_skips_sync(self): |
| 160 | + conn = MagicMock() |
| 161 | + project = MagicMock(id="uuid-1", name="project-1", is_domain=False) |
| 162 | + conn.identity.projects.return_value = [project] |
| 163 | + |
| 164 | + nautobot = MagicMock() |
| 165 | + |
| 166 | + with patch( |
| 167 | + "understack_workflows.main.resync_keystone_to_nautobot.handle_project_update" |
| 168 | + ) as mock_update: |
| 169 | + result = sync_projects(conn, nautobot, dry_run=True) |
| 170 | + |
| 171 | + assert result.total == 1 |
| 172 | + assert result.failed == 0 |
| 173 | + mock_update.assert_not_called() |
| 174 | + |
| 175 | + |
| 176 | +class TestMain: |
| 177 | + """Test cases for main function.""" |
| 178 | + |
| 179 | + @patch("understack_workflows.main.resync_keystone_to_nautobot.sync_projects") |
| 180 | + @patch("understack_workflows.main.resync_keystone_to_nautobot.get_nautobot_client") |
| 181 | + @patch("understack_workflows.main.resync_keystone_to_nautobot.get_openstack_client") |
| 182 | + @patch("understack_workflows.main.resync_keystone_to_nautobot.setup_logger") |
| 183 | + @patch("understack_workflows.main.resync_keystone_to_nautobot.argument_parser") |
| 184 | + def test_main_success( |
| 185 | + self, mock_parser, mock_logger, mock_get_os, mock_get_nb, mock_sync |
| 186 | + ): |
| 187 | + mock_args = MagicMock() |
| 188 | + mock_args.project = None |
| 189 | + mock_args.dry_run = False |
| 190 | + mock_parser.return_value.parse_args.return_value = mock_args |
| 191 | + mock_sync.return_value = SyncResult(total=5, failed=0) |
| 192 | + |
| 193 | + result = main() |
| 194 | + |
| 195 | + assert result == 0 |
| 196 | + mock_get_os.assert_called_once_with() |
| 197 | + |
| 198 | + @patch("understack_workflows.main.resync_keystone_to_nautobot.sync_projects") |
| 199 | + @patch("understack_workflows.main.resync_keystone_to_nautobot.get_nautobot_client") |
| 200 | + @patch("understack_workflows.main.resync_keystone_to_nautobot.get_openstack_client") |
| 201 | + @patch("understack_workflows.main.resync_keystone_to_nautobot.setup_logger") |
| 202 | + @patch("understack_workflows.main.resync_keystone_to_nautobot.argument_parser") |
| 203 | + def test_main_with_failures( |
| 204 | + self, mock_parser, mock_logger, mock_get_os, mock_get_nb, mock_sync |
| 205 | + ): |
| 206 | + mock_args = MagicMock() |
| 207 | + mock_args.project = None |
| 208 | + mock_args.dry_run = False |
| 209 | + mock_parser.return_value.parse_args.return_value = mock_args |
| 210 | + mock_sync.return_value = SyncResult(total=5, failed=2) |
| 211 | + |
| 212 | + result = main() |
| 213 | + |
| 214 | + assert result == 1 |
0 commit comments