55from pathlib import Path
66from unittest import mock
77
8+ import pytest
89from nisystemlink .clients .core .helpers ._minion_id import read_minion_id
910
1011
11- class TestReadMinionId :
12- """Test cases for read_minion_id function."""
12+ @pytest .fixture
13+ def mock_path_constants ():
14+ """Fixture to mock PathConstants."""
15+ with mock .patch (
16+ "nisystemlink.clients.core.helpers._minion_id.PathConstants"
17+ ) as mock_constants :
18+ yield mock_constants
19+
20+
21+ @pytest .fixture
22+ def mock_minion_id_path (mock_path_constants ):
23+ """Fixture to set up the mock path structure for minion_id file.
24+
25+ Returns a callable that accepts an exists parameter to configure
26+ whether the path exists.
27+ """
1328
14- def _setup_mock_path (self , mock_path_constants , exists = True ):
15- """Helper to set up the mock path structure."""
29+ def _create_mock_path (exists = True ):
1630 mock_minion_id_path = mock .Mock (spec = Path )
1731 mock_minion_id_path .exists .return_value = exists
1832
@@ -27,12 +41,17 @@ def _setup_mock_path(self, mock_path_constants, exists=True):
2741
2842 return mock_minion_id_path
2943
30- @mock .patch ("nisystemlink.clients.core.helpers._minion_id.PathConstants" )
31- def test__minion_id_file_exists__returns_content (self , mock_path_constants ):
44+ return _create_mock_path
45+
46+
47+ class TestReadMinionId :
48+ """Test cases for read_minion_id function."""
49+
50+ def test__minion_id_file_exists__returns_content (self , mock_minion_id_path ):
3251 """Test that the minion ID is read correctly when the file exists."""
3352 # Arrange
3453 expected_minion_id = "test-minion-123"
35- mock_minion_id_path = self . _setup_mock_path ( mock_path_constants , exists = True )
54+ minion_path = mock_minion_id_path ( exists = True )
3655
3756 # Mock the file open and read
3857 mock_open = mock .mock_open (read_data = f"{ expected_minion_id } \n " )
@@ -43,17 +62,16 @@ def test__minion_id_file_exists__returns_content(self, mock_path_constants):
4362
4463 # Assert
4564 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" )
65+ minion_path .exists .assert_called_once ()
66+ mock_open .assert_called_once_with (minion_path , "r" , encoding = "utf-8" )
4867
49- @mock .patch ("nisystemlink.clients.core.helpers._minion_id.PathConstants" )
5068 def test__minion_id_file_exists_with_whitespace__returns_stripped_content (
51- self , mock_path_constants
69+ self , mock_minion_id_path
5270 ):
5371 """Test that the minion ID is stripped of leading/trailing whitespace."""
5472 # Arrange
5573 expected_minion_id = "test-minion-456"
56- mock_minion_id_path = self . _setup_mock_path ( mock_path_constants , exists = True )
74+ minion_path = mock_minion_id_path ( exists = True )
5775
5876 # Mock the file open with extra whitespace
5977 mock_open = mock .mock_open (read_data = f" { expected_minion_id } \n \t " )
@@ -64,26 +82,24 @@ def test__minion_id_file_exists_with_whitespace__returns_stripped_content(
6482
6583 # Assert
6684 assert result == expected_minion_id
67- mock_minion_id_path .exists .assert_called_once ()
85+ minion_path .exists .assert_called_once ()
6886
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 ):
87+ def test__minion_id_file_does_not_exist__returns_none (self , mock_minion_id_path ):
7188 """Test that None is returned when the minion_id file does not exist."""
7289 # Arrange
73- mock_minion_id_path = self . _setup_mock_path ( mock_path_constants , exists = False )
90+ minion_path = mock_minion_id_path ( exists = False )
7491
7592 # Act
7693 result = read_minion_id ()
7794
7895 # Assert
7996 assert result is None
80- mock_minion_id_path .exists .assert_called_once ()
97+ minion_path .exists .assert_called_once ()
8198
82- @mock .patch ("nisystemlink.clients.core.helpers._minion_id.PathConstants" )
83- def test__minion_id_file_has_oserror__returns_none (self , mock_path_constants ):
99+ def test__minion_id_file_has_oserror__returns_none (self , mock_minion_id_path ):
84100 """Test that None is returned when an OSError occurs reading the file."""
85101 # Arrange
86- mock_minion_id_path = self . _setup_mock_path ( mock_path_constants , exists = True )
102+ minion_path = mock_minion_id_path ( exists = True )
87103
88104 # Mock the file open to raise OSError
89105 mock_open = mock .mock_open ()
@@ -95,15 +111,14 @@ def test__minion_id_file_has_oserror__returns_none(self, mock_path_constants):
95111
96112 # Assert
97113 assert result is None
98- mock_minion_id_path .exists .assert_called_once ()
114+ minion_path .exists .assert_called_once ()
99115
100- @mock .patch ("nisystemlink.clients.core.helpers._minion_id.PathConstants" )
101116 def test__minion_id_file_has_permission_error__returns_none (
102- self , mock_path_constants
117+ self , mock_minion_id_path
103118 ):
104119 """Test that None is returned when a PermissionError occurs reading the file."""
105120 # Arrange
106- mock_minion_id_path = self . _setup_mock_path ( mock_path_constants , exists = True )
121+ minion_path = mock_minion_id_path ( exists = True )
107122
108123 # Mock the file open to raise PermissionError
109124 mock_open = mock .mock_open ()
@@ -115,13 +130,12 @@ def test__minion_id_file_has_permission_error__returns_none(
115130
116131 # Assert
117132 assert result is None
118- mock_minion_id_path .exists .assert_called_once ()
133+ minion_path .exists .assert_called_once ()
119134
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 ):
135+ def test__minion_id_file_is_empty__returns_empty_string (self , mock_minion_id_path ):
122136 """Test that an empty string is returned when the file is empty."""
123137 # Arrange
124- mock_minion_id_path = self . _setup_mock_path ( mock_path_constants , exists = True )
138+ minion_path = mock_minion_id_path ( exists = True )
125139
126140 # Mock the file open with empty content
127141 mock_open = mock .mock_open (read_data = "" )
@@ -132,15 +146,14 @@ def test__minion_id_file_is_empty__returns_empty_string(self, mock_path_constant
132146
133147 # Assert
134148 assert result == ""
135- mock_minion_id_path .exists .assert_called_once ()
149+ minion_path .exists .assert_called_once ()
136150
137- @mock .patch ("nisystemlink.clients.core.helpers._minion_id.PathConstants" )
138151 def test__minion_id_file_only_whitespace__returns_empty_string (
139- self , mock_path_constants
152+ self , mock_minion_id_path
140153 ):
141154 """Test that an empty string is returned when the file contains only whitespace."""
142155 # Arrange
143- mock_minion_id_path = self . _setup_mock_path ( mock_path_constants , exists = True )
156+ minion_path = mock_minion_id_path ( exists = True )
144157
145158 # Mock the file open with only whitespace
146159 mock_open = mock .mock_open (read_data = " \n \t \n " )
@@ -151,4 +164,4 @@ def test__minion_id_file_only_whitespace__returns_empty_string(
151164
152165 # Assert
153166 assert result == ""
154- mock_minion_id_path .exists .assert_called_once ()
167+ minion_path .exists .assert_called_once ()
0 commit comments