@@ -45,26 +45,37 @@ def normalize_whitespace(s: str) -> str:
4545
4646
4747@pytest .mark .parametrize (
48- "ioc_name, command, before_state, before_enabled, after_state, after_enabled" ,
48+ "ioc_name, command, before_state, before_enabled, after_state, after_enabled, as_root " ,
4949 [
50- ("ioc1" , cmds .stop , "Running" , "Enabled" , "Stopped" , "Enabled" ),
51- ("ioc3" , cmds .stop , "Running" , "Disabled" , "Stopped" , "Disabled" ),
52- ("ioc4" , cmds .stop , "Stopped" , "Disabled" , "Stopped" , "Disabled" ),
53- ("ioc1" , cmds .start , "Running" , "Enabled" , "Running" , "Enabled" ),
54- ("ioc3" , cmds .start , "Running" , "Disabled" , "Running" , "Disabled" ),
55- ("ioc4" , cmds .start , "Stopped" , "Disabled" , "Running" , "Disabled" ),
56- ("ioc3" , cmds .enable , "Running" , "Disabled" , "Running" , "Enabled" ),
57- ("ioc4" , cmds .enable , "Stopped" , "Disabled" , "Stopped" , "Enabled" ),
58- ("ioc1" , cmds .disable , "Running" , "Enabled" , "Running" , "Disabled" ),
59- ("ioc3" , cmds .disable , "Running" , "Disabled" , "Running" , "Disabled" ),
60- ("ioc1" , cmds .restart , "Running" , "Enabled" , "Running" , "Enabled" ),
61- ("ioc4" , cmds .restart , "Stopped" , "Disabled" , "Running" , "Disabled" ),
62- ("ioc3" , cmds .restart , "Running" , "Disabled" , "Running" , "Disabled" ),
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 ),
6363 ],
6464)
6565def test_state_change_commands (
66- sample_iocs , ioc_name , command , before_state , before_enabled , after_state , after_enabled
66+ sample_iocs ,
67+ ioc_name ,
68+ command ,
69+ before_state ,
70+ before_enabled ,
71+ after_state ,
72+ after_enabled ,
73+ as_root ,
74+ monkeypatch ,
6775):
76+ if not as_root :
77+ monkeypatch .setattr (os , "geteuid" , lambda : 1000 ) # Mock as non-root user
78+
6879 _ , status = get_ioc_statuses (ioc_name )
6980 assert status == (before_state , before_enabled )
7081
@@ -76,8 +87,6 @@ def test_state_change_commands(
7687
7788
7889def test_install_new_ioc (sample_iocs , monkeypatch ):
79- monkeypatch .setattr (os , "geteuid" , lambda : 0 ) # Mock as root user
80-
8190 assert "ioc2" not in find_installed_iocs ()
8291
8392 rc = cmds .install ("ioc2" )
@@ -89,32 +98,19 @@ def test_install_new_ioc(sample_iocs, monkeypatch):
8998 assert "ioc2" in find_installed_iocs ()
9099
91100
92- def test_install_ioc_not_root (sample_iocs , monkeypatch ):
93- monkeypatch .setattr (os , "geteuid" , lambda : 1000 ) # Mock as non-root user
94-
95- with pytest .raises (RuntimeError , match = "You must be root to install an IOC!" ):
96- cmds .install ("ioc3" )
97-
98-
99101def test_install_ioc_wrong_host (sample_iocs , monkeypatch ):
100- monkeypatch .setattr (os , "geteuid" , lambda : 0 ) # Mock as root user
101-
102102 with pytest .raises (RuntimeError , match = "Cannot install IOC 'ioc1' on this host" ):
103103 cmds .install ("ioc1" )
104104
105105
106106def test_install_already_installed_ioc (sample_iocs , monkeypatch ):
107- monkeypatch .setattr (os , "geteuid" , lambda : 0 ) # Mock as root user
108-
109107 assert "ioc3" in find_installed_iocs ()
110108
111109 with pytest .raises (RuntimeError , match = "Failed to install IOC 'ioc3'!" ):
112110 cmds .install ("ioc3" )
113111
114112
115113def test_uninstall_ioc (sample_iocs , monkeypatch ):
116- monkeypatch .setattr (os , "geteuid" , lambda : 0 ) # Mock as root user
117-
118114 assert "ioc5" in find_installed_iocs ()
119115
120116 rc = cmds .uninstall ("ioc5" )
@@ -123,11 +119,15 @@ def test_uninstall_ioc(sample_iocs, monkeypatch):
123119 assert "ioc5" not in find_installed_iocs ()
124120
125121
126- def test_uninstall_ioc_not_root (sample_iocs , monkeypatch ):
122+ @pytest .mark .parametrize (
123+ "command" ,
124+ [cmds .enable , cmds .disable , cmds .enableall , cmds .disableall , cmds .install , cmds .uninstall ],
125+ )
126+ def test_requires_root (sample_iocs , monkeypatch , command ):
127127 monkeypatch .setattr (os , "geteuid" , lambda : 1000 ) # Mock as non-root user
128128
129- with pytest .raises (RuntimeError , match = "You must be root to uninstall an IOC! " ):
130- cmds . uninstall ("ioc1" )
129+ with pytest .raises (PermissionError , match = "requires root privileges. " ):
130+ command ("ioc1" )
131131
132132
133133@pytest .mark .parametrize (
@@ -155,15 +155,21 @@ def test_state_change_all(sample_iocs, cmd, expected_state, expected_enabled):
155155 assert get_ioc_statuses (ioc .name )[1 ][1 ] == expected_enabled
156156
157157
158- def test_attach (sample_iocs , monkeypatch , dummy_popen ):
159- monkeypatch .setattr (os , "geteuid" , lambda : 1000 ) # Mock as non-root user
158+ @pytest .mark .parametrize ("as_root" , [True , False ])
159+ def test_attach (sample_iocs , monkeypatch , dummy_popen , as_root ):
160+ if not as_root :
161+ monkeypatch .setattr (os , "geteuid" , lambda : 1000 ) # Mock as non-root user
160162
161163 ret = cmds .attach ("ioc3" )
162164
163165 assert ret == ["telnet" , "localhost" , "3456" ]
164166
165167
166- def test_status (sample_iocs , capsys ):
168+ @pytest .mark .parametrize ("as_root" , [True , False ])
169+ def test_status (sample_iocs , capsys , monkeypatch , as_root ):
170+ if not as_root :
171+ monkeypatch .setattr (os , "geteuid" , lambda : 1000 ) # Mock as non-root user
172+
167173 rc = cmds .status ()
168174 captured = capsys .readouterr ()
169175 expected_output = """IOC Status Auto-Start
@@ -194,8 +200,6 @@ def normalize_whitespace(s: str) -> str:
194200 ],
195201)
196202def test_command_failures (sample_iocs , monkeypatch , cmd , expected_message ):
197- monkeypatch .setattr (os , "geteuid" , lambda : 0 ) # Mock as root user
198-
199203 def failing_systemctl_passthrough (action : str , ioc : str ) -> tuple [str , str , int ]:
200204 return ("" , "Simulated failure" , 1 )
201205
@@ -214,8 +218,6 @@ def failing_systemctl_passthrough(action: str, ioc: str) -> tuple[str, str, int]
214218 ],
215219)
216220def test_uninstall_failures (sample_iocs , monkeypatch , failed_action , expected_message ):
217- monkeypatch .setattr (os , "geteuid" , lambda : 0 ) # Mock as root user
218-
219221 def failing_systemctl_passthrough (action : str , ioc : str ) -> tuple [str , str , int ]:
220222 if action == failed_action :
221223 return ("" , "Simulated failure" , 1 )
@@ -228,8 +230,6 @@ def failing_systemctl_passthrough(action: str, ioc: str) -> tuple[str, str, int]
228230
229231
230232def test_fail_to_install_ioc_to_run_as_root (sample_iocs , monkeypatch , sample_config_file_factory ):
231- monkeypatch .setattr (os , "geteuid" , lambda : 0 ) # Mock as root user
232-
233233 sample_config_file_factory (name = "ioc1" , user = "root" )
234234 with pytest .raises (RuntimeError , match = "Refusing to install IOC 'ioc1' to run as user 'root'!" ):
235235 cmds .install ("ioc1" )
0 commit comments