sysv-generator-test.py revision f4f01ec146d91cb6943828851d98eee6a1ad4dd9
# systemd-sysv-generator integration test
#
# (C) 2015 Canonical Ltd.
# Author: Martin Pitt <martin.pitt@ubuntu.com>
#
# under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
# systemd is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with systemd; If not, see <http://www.gnu.org/licenses/>.
import unittest
import sys
import os
import subprocess
import tempfile
import shutil
try:
from configparser import RawConfigParser
except ImportError:
# python 2
from ConfigParser import RawConfigParser
#
# Helper methods
#
'''Run sysv-generator.
Fail if stderr contains any "Fail", unless expect_error is True.
Return (stderr, filename -> ConfigParser) pair with ouput to stderr and
parsed generated units.
'''
if not expect_error:
results = {}
continue
cp = RawConfigParser()
'''Create a SysV init script with the given keys in the LSB header
There are sensible default values for all fields.
If enable is True, links will be created in the rcN.d dirs. In that
case, the priority can be given with "prio" (default to 1).
Return path of generated script.
'''
f.write('#!/bin/init-d-interpreter\n### BEGIN INIT INFO\n')
if v is not None:
f.write('### END INIT INFO\ncode --goes here\n')
if enable:
return script
'''assert that a unit is enabled in precisely the given runlevels'''
# should be enabled
for runlevel in all_runlevels:
else:
'%s unexpectedly exists' % link)
#
# test cases
#
def test_nothing(self):
'''no input files'''
def test_simple_disabled(self):
'''simple service without dependencies, disabled'''
# no enablement links or other stuff
s = results['foo.service']
# $local_fs does not need translation, don't expect any dependency
# fields here
'%s start' % init_script)
'%s stop' % init_script)
def test_simple_enabled_all(self):
'''simple service without dependencies, enabled in all runlevels'''
def test_simple_enabled_some(self):
'''simple service without dependencies, enabled in some runlevels'''
'''single LSB macro dependency: $network'''
def test_lsb_macro_dep_multi(self):
'''multiple LSB macro dependencies'''
def test_lsb_deps(self):
'''LSB header dependencies to other services'''
# also give symlink priorities here; they should be ignored
'Should-Start': 'may1 ne_may2'},
# do not create ne_may2
['foo.service', 'may1.service', 'must1.service', 'must2.service'])
# foo should depend on all of them
['may1.service', 'must1.service', 'must2.service', 'ne_may2.service'])
# other services should not depend on each other
def test_symlink_prio_deps(self):
'''script without LSB headers use rcN.d priority'''
# create two init.d scripts without LSB header and enable them with
# startup priorities
f.write('#!/bin/init-d-interpreter\ncode --goes here\n')
'provider.service')
def test_multiple_provides(self):
'''multiple Provides: names'''
# should create symlinks for the alternative names
for f in ['bar.service', 'baz.service']:
'foo.service')
'''multiple init.d scripts provide the same name'''
# should create symlink for the alternative name for either unit
['foo.service', 'bar.service'])
'''init.d scripts provides the name of another init.d script'''
'''ignores non-executable init.d script'''
def test_sh_suffix(self):
'''init.d script with .sh suffix'''
s = results['foo.service']
# should not have a .sh
# calls correct script with .sh
'%s start' % init_script)
'%s stop' % init_script)
'''init.d script with .sh suffix and Provides:'''
# ensure we don't try to create a symlink to itself
'LSB: test foo service')
# should create symlink for the alternative name
'foo.service')
def test_hidden_files(self):
'''init.d script with hidden file suffix'''
# backup files (not enabled in rcN.d/)
def test_backup_file(self):
'''init.d script with backup file'''
# backup files (not enabled in rcN.d/)
print(err)
['foo.bak.service', 'foo.old.service', 'foo.service'])
# ensure we don't try to create a symlink to itself
'''existing native unit'''
f.write('[Unit]\n')
# no enablement or alias links, as native unit is disabled
if __name__ == '__main__':