|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +"""Tests for read_minion_id helper function.""" |
| 4 | + |
| 5 | +from pathlib import Path |
| 6 | +from unittest import mock |
| 7 | + |
| 8 | +from nisystemlink.clients.core.helpers._minion_id import read_minion_id |
| 9 | + |
| 10 | + |
| 11 | +class TestReadMinionId: |
| 12 | + """Test cases for read_minion_id function.""" |
| 13 | + |
| 14 | + def _setup_mock_path(self, mock_path_constants, exists=True): |
| 15 | + """Helper to set up the mock path structure.""" |
| 16 | + mock_minion_id_path = mock.Mock(spec=Path) |
| 17 | + mock_minion_id_path.exists.return_value = exists |
| 18 | + |
| 19 | + # Set up the path chain: salt_data_directory / "conf" / "minion_id" |
| 20 | + mock_conf_path = mock.Mock(spec=Path) |
| 21 | + mock_conf_path.__truediv__ = mock.Mock(return_value=mock_minion_id_path) |
| 22 | + |
| 23 | + mock_salt_dir = mock.Mock(spec=Path) |
| 24 | + mock_salt_dir.__truediv__ = mock.Mock(return_value=mock_conf_path) |
| 25 | + |
| 26 | + mock_path_constants.salt_data_directory = mock_salt_dir |
| 27 | + |
| 28 | + return mock_minion_id_path |
| 29 | + |
| 30 | + @mock.patch("nisystemlink.clients.core.helpers._minion_id.PathConstants") |
| 31 | + def test__minion_id_file_exists__returns_content(self, mock_path_constants): |
| 32 | + """Test that the minion ID is read correctly when the file exists.""" |
| 33 | + # Arrange |
| 34 | + expected_minion_id = "test-minion-123" |
| 35 | + mock_minion_id_path = self._setup_mock_path(mock_path_constants, exists=True) |
| 36 | + |
| 37 | + # Mock the file open and read |
| 38 | + mock_open = mock.mock_open(read_data=f"{expected_minion_id}\n") |
| 39 | + |
| 40 | + # Act |
| 41 | + with mock.patch("builtins.open", mock_open): |
| 42 | + result = read_minion_id() |
| 43 | + |
| 44 | + # Assert |
| 45 | + assert result == expected_minion_id |
| 46 | + mock_minion_id_path.exists.assert_called_once() |
| 47 | + mock_open.assert_called_once_with(mock_minion_id_path, "r", encoding="utf-8") |
| 48 | + |
| 49 | + @mock.patch("nisystemlink.clients.core.helpers._minion_id.PathConstants") |
| 50 | + def test__minion_id_file_exists_with_whitespace__returns_stripped_content( |
| 51 | + self, mock_path_constants |
| 52 | + ): |
| 53 | + """Test that the minion ID is stripped of leading/trailing whitespace.""" |
| 54 | + # Arrange |
| 55 | + expected_minion_id = "test-minion-456" |
| 56 | + mock_minion_id_path = self._setup_mock_path(mock_path_constants, exists=True) |
| 57 | + |
| 58 | + # Mock the file open with extra whitespace |
| 59 | + mock_open = mock.mock_open(read_data=f" {expected_minion_id} \n\t") |
| 60 | + |
| 61 | + # Act |
| 62 | + with mock.patch("builtins.open", mock_open): |
| 63 | + result = read_minion_id() |
| 64 | + |
| 65 | + # Assert |
| 66 | + assert result == expected_minion_id |
| 67 | + mock_minion_id_path.exists.assert_called_once() |
| 68 | + |
| 69 | + @mock.patch("nisystemlink.clients.core.helpers._minion_id.PathConstants") |
| 70 | + def test__minion_id_file_does_not_exist__returns_none(self, mock_path_constants): |
| 71 | + """Test that None is returned when the minion_id file does not exist.""" |
| 72 | + # Arrange |
| 73 | + mock_minion_id_path = self._setup_mock_path(mock_path_constants, exists=False) |
| 74 | + |
| 75 | + # Act |
| 76 | + result = read_minion_id() |
| 77 | + |
| 78 | + # Assert |
| 79 | + assert result is None |
| 80 | + mock_minion_id_path.exists.assert_called_once() |
| 81 | + |
| 82 | + @mock.patch("nisystemlink.clients.core.helpers._minion_id.PathConstants") |
| 83 | + def test__minion_id_file_has_oserror__returns_none(self, mock_path_constants): |
| 84 | + """Test that None is returned when an OSError occurs reading the file.""" |
| 85 | + # Arrange |
| 86 | + mock_minion_id_path = self._setup_mock_path(mock_path_constants, exists=True) |
| 87 | + |
| 88 | + # Mock the file open to raise OSError |
| 89 | + mock_open = mock.mock_open() |
| 90 | + mock_open.side_effect = OSError("File access error") |
| 91 | + |
| 92 | + # Act |
| 93 | + with mock.patch("builtins.open", mock_open): |
| 94 | + result = read_minion_id() |
| 95 | + |
| 96 | + # Assert |
| 97 | + assert result is None |
| 98 | + mock_minion_id_path.exists.assert_called_once() |
| 99 | + |
| 100 | + @mock.patch("nisystemlink.clients.core.helpers._minion_id.PathConstants") |
| 101 | + def test__minion_id_file_has_permission_error__returns_none( |
| 102 | + self, mock_path_constants |
| 103 | + ): |
| 104 | + """Test that None is returned when a PermissionError occurs reading the file.""" |
| 105 | + # Arrange |
| 106 | + mock_minion_id_path = self._setup_mock_path(mock_path_constants, exists=True) |
| 107 | + |
| 108 | + # Mock the file open to raise PermissionError |
| 109 | + mock_open = mock.mock_open() |
| 110 | + mock_open.side_effect = PermissionError("Permission denied") |
| 111 | + |
| 112 | + # Act |
| 113 | + with mock.patch("builtins.open", mock_open): |
| 114 | + result = read_minion_id() |
| 115 | + |
| 116 | + # Assert |
| 117 | + assert result is None |
| 118 | + mock_minion_id_path.exists.assert_called_once() |
| 119 | + |
| 120 | + @mock.patch("nisystemlink.clients.core.helpers._minion_id.PathConstants") |
| 121 | + def test__minion_id_file_is_empty__returns_empty_string(self, mock_path_constants): |
| 122 | + """Test that an empty string is returned when the file is empty.""" |
| 123 | + # Arrange |
| 124 | + mock_minion_id_path = self._setup_mock_path(mock_path_constants, exists=True) |
| 125 | + |
| 126 | + # Mock the file open with empty content |
| 127 | + mock_open = mock.mock_open(read_data="") |
| 128 | + |
| 129 | + # Act |
| 130 | + with mock.patch("builtins.open", mock_open): |
| 131 | + result = read_minion_id() |
| 132 | + |
| 133 | + # Assert |
| 134 | + assert result == "" |
| 135 | + mock_minion_id_path.exists.assert_called_once() |
| 136 | + |
| 137 | + @mock.patch("nisystemlink.clients.core.helpers._minion_id.PathConstants") |
| 138 | + def test__minion_id_file_only_whitespace__returns_empty_string( |
| 139 | + self, mock_path_constants |
| 140 | + ): |
| 141 | + """Test that an empty string is returned when the file contains only whitespace.""" |
| 142 | + # Arrange |
| 143 | + mock_minion_id_path = self._setup_mock_path(mock_path_constants, exists=True) |
| 144 | + |
| 145 | + # Mock the file open with only whitespace |
| 146 | + mock_open = mock.mock_open(read_data=" \n\t \n") |
| 147 | + |
| 148 | + # Act |
| 149 | + with mock.patch("builtins.open", mock_open): |
| 150 | + result = read_minion_id() |
| 151 | + |
| 152 | + # Assert |
| 153 | + assert result == "" |
| 154 | + mock_minion_id_path.exists.assert_called_once() |
0 commit comments