55import manage_iocs
66import manage_iocs .commands as cmds
77import manage_iocs .utils
8- from manage_iocs .utils import find_installed_iocs , get_ioc_statuses
8+ from manage_iocs .utils import find_installed_iocs , get_ioc_status
9+
10+
11+ def strip_ansi_codes (s : str ) -> str :
12+ import re
13+
14+ ansi_escape = re .compile (r"\x1B\[[0-?]*[ -/]*[@-~]" )
15+ return ansi_escape .sub ("" , s )
916
1017
1118def test_version (capsys ):
@@ -44,22 +51,35 @@ def normalize_whitespace(s: str) -> str:
4451 assert normalize_whitespace (line ) in normalize_whitespace (captured .out )
4552
4653
54+ def test_report_no_iocs (monkeypatch , capsys ):
55+ monkeypatch .setattr (
56+ manage_iocs .utils ,
57+ "find_iocs" ,
58+ lambda : {},
59+ )
60+
61+ rc = cmds .report ()
62+ captured = capsys .readouterr ()
63+ assert "No IOCs found on configured to run on this host." in captured .out
64+ assert rc == 1
65+
66+
4767@pytest .mark .parametrize (
4868 "ioc_name, command, before_state, before_enabled, after_state, after_enabled, as_root" ,
4969 [
50- ("ioc1" , cmds .stop , "Running" , "Enabled" , "Stopped" , "Enabled" , False ),
51- ("ioc3" , cmds .stop , "Running" , "Disabled" , "Stopped" , "Disabled" , False ),
52- ("ioc4" , cmds .stop , "Stopped" , "Disabled" , "Stopped" , "Disabled" , False ),
53- ("ioc1" , cmds .start , "Running" , "Enabled" , "Running" , "Enabled" , False ),
54- ("ioc3" , cmds .start , "Running" , "Disabled" , "Running" , "Disabled" , False ),
55- ("ioc4" , cmds .start , "Stopped" , "Disabled" , "Running" , "Disabled" , False ),
56- ("ioc3" , cmds .enable , "Running" , "Disabled" , "Running" , "Enabled" , True ),
57- ("ioc4" , cmds .enable , "Stopped" , "Disabled" , "Stopped" , "Enabled" , True ),
58- ("ioc1" , cmds .disable , "Running" , "Enabled" , "Running" , "Disabled" , True ),
59- ("ioc3" , cmds .disable , "Running" , "Disabled" , "Running" , "Disabled" , True ),
60- ("ioc1" , cmds .restart , "Running" , "Enabled" , "Running" , "Enabled" , False ),
61- ("ioc4" , cmds .restart , "Stopped" , "Disabled" , "Running" , "Disabled" , False ),
62- ("ioc3" , cmds .restart , "Running" , "Disabled" , "Running" , "Disabled" , False ),
70+ ("ioc1" , cmds .stop , "Running" , True , "Stopped" , True , False ),
71+ ("ioc3" , cmds .stop , "Running" , False , "Stopped" , False , False ),
72+ ("ioc4" , cmds .stop , "Stopped" , False , "Stopped" , False , False ),
73+ ("ioc1" , cmds .start , "Running" , True , "Running" , True , False ),
74+ ("ioc3" , cmds .start , "Running" , False , "Running" , False , False ),
75+ ("ioc4" , cmds .start , "Stopped" , False , "Running" , False , False ),
76+ ("ioc3" , cmds .enable , "Running" , False , "Running" , True , True ),
77+ ("ioc4" , cmds .enable , "Stopped" , False , "Stopped" , True , True ),
78+ ("ioc1" , cmds .disable , "Running" , True , "Running" , False , True ),
79+ ("ioc3" , cmds .disable , "Running" , False , "Running" , False , True ),
80+ ("ioc1" , cmds .restart , "Running" , True , "Running" , True , False ),
81+ ("ioc4" , cmds .restart , "Stopped" , False , "Running" , False , False ),
82+ ("ioc3" , cmds .restart , "Running" , False , "Running" , False , False ),
6383 ],
6484)
6585def test_state_change_commands (
@@ -76,14 +96,12 @@ def test_state_change_commands(
7696 if not as_root :
7797 monkeypatch .setattr (os , "geteuid" , lambda : 1000 ) # Mock as non-root user
7898
79- _ , status = get_ioc_statuses (ioc_name )
80- assert status == (before_state , before_enabled )
99+ assert get_ioc_status (ioc_name ) == (before_state , before_enabled )
81100
82101 rc = command (ioc_name )
83102 assert rc == 0
84103
85- _ , status = get_ioc_statuses (ioc_name )
86- assert status == (after_state , after_enabled )
104+ assert get_ioc_status (ioc_name ) == (after_state , after_enabled )
87105
88106
89107def test_install_new_ioc (sample_iocs , monkeypatch ):
@@ -92,8 +110,7 @@ def test_install_new_ioc(sample_iocs, monkeypatch):
92110 rc = cmds .install ("ioc2" )
93111 assert rc == 0
94112
95- _ , status = get_ioc_statuses ("ioc2" )
96- assert status == ("Stopped" , "Disabled" )
113+ assert get_ioc_status ("ioc2" ) == ("Stopped" , False )
97114
98115 assert "ioc2" in find_installed_iocs ()
99116
@@ -135,8 +152,8 @@ def test_requires_root(sample_iocs, monkeypatch, command):
135152 [
136153 (cmds .startall , "Running" , None ),
137154 (cmds .stopall , "Stopped" , None ),
138- (cmds .enableall , None , "Enabled" ),
139- (cmds .disableall , None , "Disabled" ),
155+ (cmds .enableall , None , True ),
156+ (cmds .disableall , None , False ),
140157 ],
141158)
142159def test_state_change_all (sample_iocs , cmd , expected_state , expected_enabled ):
@@ -150,9 +167,9 @@ def test_state_change_all(sample_iocs, cmd, expected_state, expected_enabled):
150167 # Check all are running
151168 for ioc in installed_iocs .values ():
152169 if expected_state is not None :
153- assert get_ioc_statuses (ioc .name )[ 1 ] [0 ] == expected_state
170+ assert get_ioc_status (ioc .name )[0 ] == expected_state
154171 if expected_enabled is not None :
155- assert get_ioc_statuses (ioc .name )[1 ][ 1 ] == expected_enabled
172+ assert get_ioc_status (ioc .name )[1 ] is expected_enabled
156173
157174
158175@pytest .mark .parametrize ("as_root" , [True , False ])
@@ -173,22 +190,38 @@ def test_status(sample_iocs, capsys, monkeypatch, as_root):
173190 rc = cmds .status ()
174191 captured = capsys .readouterr ()
175192 expected_output = """IOC Status Auto-Start
176- ----------------------------
193+ --------------------------
177194ioc1 Running Enabled
178195ioc3 Running Disabled
179196ioc4 Stopped Disabled
180197ioc5 Stopped Enabled
181198"""
182199
183- def normalize_whitespace (s : str ) -> str :
184- return "\n " .join (" " .join (line .split ()) for line in s .strip ().splitlines ())
200+ def normalize_whitespace_and_ansi_codes (s : str ) -> str :
201+ whitespace_normalized = "\n " .join (" " .join (line .split ()) for line in s .strip ().splitlines ())
202+ return strip_ansi_codes (whitespace_normalized )
185203
186204 for line in expected_output .strip ().splitlines ():
187- assert normalize_whitespace (line ) in normalize_whitespace (captured .out )
205+ assert normalize_whitespace_and_ansi_codes (line ) in normalize_whitespace_and_ansi_codes (
206+ captured .out
207+ )
188208
189209 assert rc == 0
190210
191211
212+ def test_status_no_installed_iocs (sample_iocs , monkeypatch , capsys ):
213+ monkeypatch .setattr (
214+ manage_iocs .utils ,
215+ "find_installed_iocs" ,
216+ lambda : {},
217+ )
218+
219+ rc = cmds .status ()
220+ captured = capsys .readouterr ()
221+ assert "No Installed IOCs found on this host." in captured .out
222+ assert rc == 1
223+
224+
192225@pytest .mark .parametrize (
193226 "cmd, expected_message" ,
194227 [
0 commit comments