|
| 1 | +"""Tests for resync_ironic_to_nautobot module.""" |
| 2 | + |
| 3 | +from unittest.mock import MagicMock |
| 4 | +from unittest.mock import patch |
| 5 | + |
| 6 | +from understack_workflows.main.resync_ironic_to_nautobot import SyncResult |
| 7 | +from understack_workflows.main.resync_ironic_to_nautobot import argument_parser |
| 8 | +from understack_workflows.main.resync_ironic_to_nautobot import main |
| 9 | +from understack_workflows.main.resync_ironic_to_nautobot import sync_nodes |
| 10 | + |
| 11 | + |
| 12 | +class TestSyncResult: |
| 13 | + """Test cases for SyncResult dataclass.""" |
| 14 | + |
| 15 | + def test_defaults(self): |
| 16 | + result = SyncResult() |
| 17 | + assert result.total == 0 |
| 18 | + assert result.failed == 0 |
| 19 | + assert result.succeeded == 0 |
| 20 | + |
| 21 | + def test_succeeded_calculation(self): |
| 22 | + result = SyncResult(total=10, failed=3) |
| 23 | + assert result.succeeded == 7 |
| 24 | + |
| 25 | + def test_all_failed(self): |
| 26 | + result = SyncResult(total=5, failed=5) |
| 27 | + assert result.succeeded == 0 |
| 28 | + |
| 29 | + def test_none_failed(self): |
| 30 | + result = SyncResult(total=5, failed=0) |
| 31 | + assert result.succeeded == 5 |
| 32 | + |
| 33 | + |
| 34 | +class TestArgumentParser: |
| 35 | + """Test cases for argument_parser function.""" |
| 36 | + |
| 37 | + def test_default_args(self): |
| 38 | + parser = argument_parser() |
| 39 | + args = parser.parse_args([]) |
| 40 | + assert args.node is None |
| 41 | + assert args.dry_run is False |
| 42 | + |
| 43 | + def test_node_arg(self): |
| 44 | + parser = argument_parser() |
| 45 | + args = parser.parse_args(["--node", "test-uuid"]) |
| 46 | + assert args.node == "test-uuid" |
| 47 | + |
| 48 | + def test_dry_run_arg(self): |
| 49 | + parser = argument_parser() |
| 50 | + args = parser.parse_args(["--dry-run"]) |
| 51 | + assert args.dry_run is True |
| 52 | + |
| 53 | + |
| 54 | +class TestSyncNodes: |
| 55 | + """Test cases for sync_nodes function.""" |
| 56 | + |
| 57 | + @patch("understack_workflows.main.resync_ironic_to_nautobot.IronicClient") |
| 58 | + @patch( |
| 59 | + "understack_workflows.main.resync_ironic_to_nautobot.sync_device_to_nautobot" |
| 60 | + ) |
| 61 | + def test_sync_all_nodes_success(self, mock_sync, mock_ironic_class): |
| 62 | + mock_ironic = MagicMock() |
| 63 | + mock_ironic_class.return_value = mock_ironic |
| 64 | + mock_node1 = MagicMock(uuid="uuid-1", name="node-1") |
| 65 | + mock_node2 = MagicMock(uuid="uuid-2", name="node-2") |
| 66 | + mock_ironic.list_nodes.return_value = [mock_node1, mock_node2] |
| 67 | + mock_sync.return_value = 0 |
| 68 | + |
| 69 | + nautobot = MagicMock() |
| 70 | + result = sync_nodes(nautobot) |
| 71 | + |
| 72 | + assert result.total == 2 |
| 73 | + assert result.failed == 0 |
| 74 | + assert mock_sync.call_count == 2 |
| 75 | + |
| 76 | + @patch("understack_workflows.main.resync_ironic_to_nautobot.IronicClient") |
| 77 | + @patch( |
| 78 | + "understack_workflows.main.resync_ironic_to_nautobot.sync_device_to_nautobot" |
| 79 | + ) |
| 80 | + def test_sync_single_node(self, mock_sync, mock_ironic_class): |
| 81 | + mock_ironic = MagicMock() |
| 82 | + mock_ironic_class.return_value = mock_ironic |
| 83 | + mock_node = MagicMock(uuid="uuid-1", name="node-1") |
| 84 | + mock_ironic.get_node.return_value = mock_node |
| 85 | + mock_sync.return_value = 0 |
| 86 | + |
| 87 | + nautobot = MagicMock() |
| 88 | + result = sync_nodes(nautobot, node_uuid="uuid-1") |
| 89 | + |
| 90 | + assert result.total == 1 |
| 91 | + assert result.failed == 0 |
| 92 | + mock_ironic.get_node.assert_called_once_with("uuid-1") |
| 93 | + |
| 94 | + @patch("understack_workflows.main.resync_ironic_to_nautobot.IronicClient") |
| 95 | + @patch( |
| 96 | + "understack_workflows.main.resync_ironic_to_nautobot.sync_device_to_nautobot" |
| 97 | + ) |
| 98 | + def test_sync_with_failures(self, mock_sync, mock_ironic_class): |
| 99 | + mock_ironic = MagicMock() |
| 100 | + mock_ironic_class.return_value = mock_ironic |
| 101 | + mock_node1 = MagicMock(uuid="uuid-1", name="node-1") |
| 102 | + mock_node2 = MagicMock(uuid="uuid-2", name="node-2") |
| 103 | + mock_ironic.list_nodes.return_value = [mock_node1, mock_node2] |
| 104 | + mock_sync.side_effect = [0, 1] # First succeeds, second fails |
| 105 | + |
| 106 | + nautobot = MagicMock() |
| 107 | + result = sync_nodes(nautobot) |
| 108 | + |
| 109 | + assert result.total == 2 |
| 110 | + assert result.failed == 1 |
| 111 | + assert result.succeeded == 1 |
| 112 | + |
| 113 | + @patch("understack_workflows.main.resync_ironic_to_nautobot.IronicClient") |
| 114 | + def test_dry_run_skips_sync(self, mock_ironic_class): |
| 115 | + mock_ironic = MagicMock() |
| 116 | + mock_ironic_class.return_value = mock_ironic |
| 117 | + mock_node = MagicMock(uuid="uuid-1", name="node-1") |
| 118 | + mock_ironic.list_nodes.return_value = [mock_node] |
| 119 | + |
| 120 | + nautobot = MagicMock() |
| 121 | + result = sync_nodes(nautobot, dry_run=True) |
| 122 | + |
| 123 | + assert result.total == 1 |
| 124 | + assert result.failed == 0 |
| 125 | + |
| 126 | + |
| 127 | +class TestMain: |
| 128 | + """Test cases for main function.""" |
| 129 | + |
| 130 | + @patch("understack_workflows.main.resync_ironic_to_nautobot.sync_nodes") |
| 131 | + @patch("understack_workflows.main.resync_ironic_to_nautobot.pynautobot") |
| 132 | + @patch("understack_workflows.main.resync_ironic_to_nautobot.credential") |
| 133 | + @patch("understack_workflows.main.resync_ironic_to_nautobot.setup_logger") |
| 134 | + @patch("understack_workflows.main.resync_ironic_to_nautobot.argument_parser") |
| 135 | + def test_main_success( |
| 136 | + self, mock_parser, mock_logger, mock_cred, mock_pynb, mock_sync |
| 137 | + ): |
| 138 | + mock_args = MagicMock() |
| 139 | + mock_args.nautobot_token = "token" |
| 140 | + mock_args.nautobot_url = "http://nautobot" |
| 141 | + mock_args.node = None |
| 142 | + mock_args.dry_run = False |
| 143 | + mock_parser.return_value.parse_args.return_value = mock_args |
| 144 | + mock_sync.return_value = SyncResult(total=5, failed=0) |
| 145 | + |
| 146 | + result = main() |
| 147 | + |
| 148 | + assert result == 0 |
| 149 | + |
| 150 | + @patch("understack_workflows.main.resync_ironic_to_nautobot.sync_nodes") |
| 151 | + @patch("understack_workflows.main.resync_ironic_to_nautobot.pynautobot") |
| 152 | + @patch("understack_workflows.main.resync_ironic_to_nautobot.credential") |
| 153 | + @patch("understack_workflows.main.resync_ironic_to_nautobot.setup_logger") |
| 154 | + @patch("understack_workflows.main.resync_ironic_to_nautobot.argument_parser") |
| 155 | + def test_main_with_failures( |
| 156 | + self, mock_parser, mock_logger, mock_cred, mock_pynb, mock_sync |
| 157 | + ): |
| 158 | + mock_args = MagicMock() |
| 159 | + mock_args.nautobot_token = "token" |
| 160 | + mock_args.nautobot_url = "http://nautobot" |
| 161 | + mock_args.node = None |
| 162 | + mock_args.dry_run = False |
| 163 | + mock_parser.return_value.parse_args.return_value = mock_args |
| 164 | + mock_sync.return_value = SyncResult(total=5, failed=2) |
| 165 | + |
| 166 | + result = main() |
| 167 | + |
| 168 | + assert result == 1 |
0 commit comments