Best Python code snippet using avocado_python
unit-config
Source:unit-config
1#!/usr/bin/python32# autopkgtest check: enable/disable/configure units3# (C) 2015 Canonical Ltd.4# Author: Martin Pitt <martin.pitt@ubuntu.com>5import unittest6import subprocess7import os8import sys9import tempfile10from glob import glob11system_unit_dir = subprocess.check_output(12 ['pkg-config', '--variable=systemdsystemunitdir', 'systemd'],13 universal_newlines=True).strip()14systemd_sysv_install = os.path.join(os.path.dirname(system_unit_dir),15 'systemd-sysv-install')16class EnableTests(unittest.TestCase):17 def tearDown(self):18 # remove all traces from our test unit19 f = glob(system_unit_dir + '/test_enable*.service')20 f += glob(system_unit_dir + '/*/test_enable*.service')21 f += glob('/etc/systemd/system/test_enable*.service')22 f += glob('/etc/systemd/system/*/test_enable*.service')23 f += glob('/etc/init.d/test_enable*')24 f += glob('/etc/rc?.d/???test_enable*')25 [os.unlink(i) for i in f]26 subprocess.check_call(['systemctl', 'daemon-reload'])27 def create_unit(self, suffix='', enable=False):28 '''Create a test unit'''29 unit = os.path.join(system_unit_dir,30 'test_enable%s.service' % suffix)31 with open(unit, 'w') as f:32 f.write('''[Unit]33Description=Testsuite unit %s34[Service]35ExecStart=/bin/echo hello36[Install]37WantedBy=multi-user.target38''' % suffix)39 if enable:40 os.symlink(unit, '/etc/systemd/system/multi-user.target.wants/' +41 os.path.basename(unit))42 return unit43 def create_sysv(self, suffix='', enable=False):44 '''Create a test SysV script'''45 script = '/etc/init.d/test_enable%s' % suffix46 with open(script, 'w') as f:47 f.write('''/bin/sh48### BEGIN INIT INFO49# Provides: test_enable%s50# Required-Start: $remote_fs $syslog51# Required-Stop: $remote_fs $syslog52# Default-Start: 2 3 4 553# Default-Stop: 0 1 654# Short-Description: Testsuite script%s55### END INIT INFO56echo hello57''' % (suffix, suffix))58 os.chmod(script, 0o755)59 if enable:60 subprocess.check_call(61 [systemd_sysv_install, 'enable', os.path.basename(script)])62 def assertEnabled(self, enabled, unit='test_enable.service'):63 '''assert that given unit has expected state'''64 systemctl = subprocess.Popen(['systemctl', 'is-enabled', unit],65 stdout=subprocess.PIPE,66 universal_newlines=True)67 out = systemctl.communicate()[0].strip()68 if enabled:69 self.assertEqual(systemctl.returncode, 0)70 self.assertEqual(out, 'enabled')71 else:72 self.assertEqual(systemctl.returncode, 1)73 self.assertEqual(out, 'disabled')74 def test_unit_enable(self):75 '''no sysv: enable unit'''76 self.create_unit()77 self.assertEnabled(False)78 # also works without .service suffix79 self.assertEnabled(False, unit='test_enable')80 subprocess.check_call(['systemctl', 'enable', 'test_enable'])81 self.assertEnabled(True)82 # also works without .service suffix83 self.assertEnabled(True, unit='test_enable')84 l = '/etc/systemd/system/multi-user.target.wants/test_enable.service'85 self.assertTrue(os.path.islink(l))86 self.assertEqual(os.readlink(l),87 system_unit_dir + '/test_enable.service')88 # enable should be idempotent89 subprocess.check_call(['systemctl', 'enable', 'test_enable.service'])90 self.assertEnabled(True)91 def test_unit_disable(self):92 '''no sysv: disable unit'''93 self.create_unit(enable=True)94 self.assertEnabled(True)95 # also works without .service suffix96 self.assertEnabled(True, unit='test_enable')97 subprocess.check_call(['systemctl', 'disable', 'test_enable'])98 self.assertEnabled(False)99 # also works without .service suffix100 self.assertEnabled(False, unit='test_enable')101 l = '/etc/systemd/system/multi-user.target.wants/test_enable.service'102 self.assertFalse(os.path.islink(l))103 # disable should be idempotent104 subprocess.check_call(['systemctl', 'disable', 'test_enable.service'])105 self.assertEnabled(False)106 def test_unit_sysv_enable(self):107 '''with sysv: enable unit'''108 self.create_unit()109 self.create_sysv()110 self.assertEnabled(False)111 # also works without .service suffix112 self.assertEnabled(False, unit='test_enable')113 subprocess.check_call(['systemctl', 'enable', 'test_enable'])114 self.assertEnabled(True)115 # also works without .service suffix116 self.assertEnabled(True, unit='test_enable')117 l = '/etc/systemd/system/multi-user.target.wants/test_enable.service'118 self.assertTrue(os.path.islink(l))119 self.assertEqual(os.readlink(l),120 system_unit_dir + '/test_enable.service')121 # enabled the sysv script122 l = glob('/etc/rc2.d/S??test_enable')123 self.assertEqual(len(l), 1, 'expect one symlink in %s' % repr(l))124 self.assertEqual(os.readlink(l[0]), '../init.d/test_enable')125 # enable should be idempotent126 subprocess.check_call(['systemctl', 'enable', 'test_enable.service'])127 self.assertEnabled(True)128 def test_unit_sysv_disable(self):129 '''with sysv: disable unit'''130 self.create_unit(enable=True)131 self.create_sysv(enable=True)132 self.assertEnabled(True)133 # also works without .service suffix134 self.assertEnabled(True, unit='test_enable')135 subprocess.check_call(['systemctl', 'disable', 'test_enable'])136 self.assertEnabled(False)137 # also works without .service suffix138 self.assertEnabled(False, unit='test_enable')139 l = '/etc/systemd/system/multi-user.target.wants/test_enable.service'140 self.assertFalse(os.path.islink(l))141 # disabled the sysv script142 l = glob('/etc/rc2.d/S??test_enable')143 self.assertEqual(l, [])144 # disable should be idempotent145 subprocess.check_call(['systemctl', 'enable', 'test_enable.service'])146 self.assertEnabled(True)147 def test_unit_alias_enable(self):148 '''no sysv: enable unit with an alias'''149 u = self.create_unit()150 with open(u, 'a') as f:151 f.write('Alias=test_enablea.service\n')152 self.assertEnabled(False)153 subprocess.check_call(['systemctl', 'enable', 'test_enable'])154 self.assertEnabled(True)155 # enablement symlink156 l = '/etc/systemd/system/multi-user.target.wants/test_enable.service'157 self.assertTrue(os.path.islink(l))158 self.assertEqual(os.readlink(l),159 system_unit_dir + '/test_enable.service')160 # alias symlink161 l = '/etc/systemd/system/test_enablea.service'162 self.assertTrue(os.path.islink(l))163 self.assertEqual(os.readlink(l),164 system_unit_dir + '/test_enable.service')165 def test_unit_alias_disable(self):166 '''no sysv: disable unit with an alias'''167 u = self.create_unit()168 with open(u, 'a') as f:169 f.write('Alias=test_enablea.service\n')170 os.symlink(system_unit_dir + '/test_enable.service',171 '/etc/systemd/system/test_enablea.service')172 subprocess.check_call(['systemctl', 'disable', 'test_enable'])173 self.assertEnabled(False)174 # enablement symlink175 l = '/etc/systemd/system/multi-user.target.wants/test_enable.service'176 self.assertFalse(os.path.islink(l))177 # alias symlink178 l = '/etc/systemd/system/test_enablea.service'179 self.assertFalse(os.path.islink(l))180 def test_unit_sysv_alias_enable(self):181 '''with sysv: enable unit with an alias'''182 u = self.create_unit()183 with open(u, 'a') as f:184 f.write('Alias=test_enablea.service\n')185 self.create_sysv()186 self.assertEnabled(False)187 subprocess.check_call(['systemctl', 'enable', 'test_enable'])188 # enablement symlink189 l = '/etc/systemd/system/multi-user.target.wants/test_enable.service'190 self.assertTrue(os.path.islink(l))191 self.assertEqual(os.readlink(l),192 system_unit_dir + '/test_enable.service')193 # alias symlink194 l = '/etc/systemd/system/test_enablea.service'195 self.assertTrue(os.path.islink(l))196 self.assertEqual(os.readlink(l),197 system_unit_dir + '/test_enable.service')198 # enabled the sysv script199 l = glob('/etc/rc2.d/S??test_enable')200 self.assertEqual(len(l), 1, 'expect one symlink in %s' % repr(l))201 self.assertEqual(os.readlink(l[0]), '../init.d/test_enable')202 self.assertEnabled(True)203 def test_unit_sysv_alias_disable(self):204 '''with sysv: disable unit with an alias'''205 u = self.create_unit(enable=True)206 with open(u, 'a') as f:207 f.write('Alias=test_enablea.service\n')208 os.symlink(system_unit_dir + '/test_enable.service',209 '/etc/systemd/system/test_enablea.service')210 self.create_sysv(enable=True)211 subprocess.check_call(['systemctl', 'disable', 'test_enable'])212 # enablement symlink213 l = '/etc/systemd/system/multi-user.target.wants/test_enable.service'214 self.assertFalse(os.path.islink(l))215 # alias symlink216 l = '/etc/systemd/system/test_enablea.service'217 self.assertFalse(os.path.islink(l))218 # disabled the sysv script219 l = glob('/etc/rc2.d/S??test_enable')220 self.assertEqual(l, [])221 self.assertEnabled(False)222 def test_sysv_enable(self):223 '''only sysv: enable'''224 self.create_sysv()225 subprocess.check_call(['systemctl', 'enable', 'test_enable'])226 # enabled the sysv script227 l = glob('/etc/rc2.d/S??test_enable')228 self.assertEqual(len(l), 1, 'expect one symlink in %s' % repr(l))229 self.assertEqual(os.readlink(l[0]), '../init.d/test_enable')230 # enable should be idempotent231 subprocess.check_call(['systemctl', 'enable', 'test_enable'])232 self.assertEnabled(True)233 def test_sysv_disable(self):234 '''only sysv: disable'''235 self.create_sysv(enable=True)236 subprocess.check_call(['systemctl', 'disable', 'test_enable'])237 # disabled the sysv script238 l = glob('/etc/rc2.d/S??test_enable')239 self.assertEqual(l, [])240 # disable should be idempotent241 subprocess.check_call(['systemctl', 'disable', 'test_enable'])242 self.assertEnabled(False)243 def test_unit_link(self):244 '''systemctl link'''245 with tempfile.NamedTemporaryFile(suffix='.service') as f:246 f.write(b'[Unit]\n')247 f.flush()248 subprocess.check_call(['systemctl', 'link', f.name])249 unit = os.path.basename(f.name)250 l = os.path.join('/etc/systemd/system', unit)251 self.assertEqual(os.readlink(l), f.name)252 # disable it again253 subprocess.check_call(['systemctl', 'disable', unit])254 # this should also remove the unit symlink255 self.assertFalse(os.path.islink(l))256 def test_unit_enable_full_path(self):257 '''systemctl enable a unit in a non-default path'''258 with tempfile.NamedTemporaryFile(suffix='.service') as f:259 f.write(b'''[Unit]260Description=test261[Service]262ExecStart=/bin/true263[Install]264WantedBy=multi-user.target''')265 f.flush()266 unit = os.path.basename(f.name)267 # now enable it268 subprocess.check_call(['systemctl', 'enable', f.name])269 self.assertEnabled(True, unit=unit)270 l = os.path.join('/etc/systemd/system', unit)271 self.assertEqual(os.readlink(l), f.name)272 enable_l = '/etc/systemd/system/multi-user.target.wants/' + unit273 self.assertEqual(os.readlink(enable_l), f.name)274 # disable it again275 subprocess.check_call(['systemctl', 'disable', unit])276 # self.assertEnabled(False) does not work as now systemd does not277 # know about the unit at all any more278 self.assertFalse(os.path.islink(enable_l))279 # this should also remove the unit symlink280 self.assertFalse(os.path.islink(l))281if __name__ == '__main__':282 unittest.main(testRunner=unittest.TextTestRunner(stream=sys.stdout,...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!