aetherscale

[unmaintained] code for a cloud provider tutorial
Log | Files | Refs | README | LICENSE

test_services.py (1462B)


      1 from pathlib import Path
      2 import tempfile
      3 from unittest import mock
      4 
      5 from aetherscale.services import SystemdServiceManager
      6 
      7 
      8 def test_systemd_creates_file(tmppath: Path):
      9     systemd = SystemdServiceManager(tmppath)
     10     with tempfile.NamedTemporaryFile('wt') as f:
     11         f.write('[Unit]')
     12         f.flush()
     13 
     14         systemd.install_service(Path(f.name), 'test.service')
     15         assert systemd.service_exists('test.service')
     16         assert (tmppath / 'test.service').is_file()
     17 
     18         systemd.uninstall_service('test.service')
     19         assert not (tmppath / 'test.service').is_file()
     20         assert not systemd.service_exists('test.service')
     21 
     22 
     23 @mock.patch('subprocess.run')
     24 def test_systemd_calls_system_binary(subprocess_run, tmppath):
     25     systemd = SystemdServiceManager(tmppath)
     26 
     27     keyword_pairs = [
     28         (systemd.enable_service, 'enable'),
     29         (systemd.disable_service, 'disable'),
     30         (systemd.start_service, 'start'),
     31         (systemd.stop_service, 'stop'),
     32         (systemd.restart_service, 'restart'),
     33         (systemd.service_is_running, 'is-active'),
     34     ]
     35 
     36     # we don't want to check the exact call as this might be valid in some
     37     # different forms; but we want to make sure that at least the right
     38     # keywords are inside the command
     39     for function, keyword in keyword_pairs:
     40         function('test.service')
     41         assert 'systemctl' in subprocess_run.call_args[0][0]
     42         assert keyword in subprocess_run.call_args[0][0]