# 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
import collections
try:
except ImportError:
# python 2
else:
#
# 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
try:
# for python3 we need here strict=False to parse multiple
# lines with the same key
except TypeError:
# RawConfigParser in python2 does not have the strict option
# but it allows multiple lines with the same key by default
'''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 targets'''
# should be enabled
for target in all_targets:
else:
'%s unexpectedly exists' % link)
#
# test cases
#
'''no input files'''
'''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)
'''simple service without dependencies, enabled in all runlevels'''
'''simple service without dependencies, that requires escaping the name'''
'''simple service without dependencies, enabled in some runlevels'''
'''single LSB macro dependency: $network'''
'''multiple LSB macro dependencies'''
'''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
'''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')
'''multiple Provides: names'''
# should create symlinks for the alternative names
for f in ['bar.service', 'baz.service']:
'foo.service')
'''a script that Provides: a name that requires escaping'''
'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'''
# we do expect an overwrite here, bar.service should overwrite the
# alias link from foo.service
'''ignores non-executable init.d script'''
'''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')
'''init.d script with hidden file suffix'''
# backup files (not enabled in rcN.d/)
'''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__':