Add unit tests for os-svc-daemon

Adds a set of unit tests for os-svc-daemon to verify the output
of running it with various options.  Includes a base test class
with some convenience functions for testing scripts.

Change-Id: Id54291686d39ab1b9f0ce4848a2fcf3c18a9ea18
This commit is contained in:
Ben Nemec 2014-03-28 19:52:58 -05:00
parent 05ac18a82b
commit 79f224d04a
5 changed files with 260 additions and 0 deletions

View File

View File

@ -0,0 +1,215 @@
# Copyright 2014 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from tests import base
class TestOsSvcDaemon(base.ScriptTestBase):
def setUp(self):
super(TestOsSvcDaemon, self).setUp()
self._stub_script('map-services', 'echo $1')
def test_standard_systemd(self):
self._stub_script('dib-init-system', 'echo systemd')
output = self._run_command(
['elements/os-svc-install/bin/os-svc-daemon',
'-p',
'-n', 'foo',
'-u', 'bar',
'-c', 'baz', 'arg',
])
expected = """[Unit]
Description=foo Service
After=os-refresh-config.service
Requires=foo-create-dir.service
[Service]
ExecStart=/opt/stack/venvs/bar/bin/baz arg
User=bar
[Install]
WantedBy=multi-user.target
Alias=foo.service
[Unit]
Description=Create /var/run/bar
[Service]
ExecStartPre=/bin/mkdir -p /var/run/bar
ExecStart=/bin/chown -R bar:bar /var/run/bar
[Install]
RequiredBy=foo.service
"""
self.assertEqual(expected, output)
def test_standard_upstart(self):
self._stub_script('dib-init-system', 'echo upstart')
output = self._run_command(
['elements/os-svc-install/bin/os-svc-daemon',
'-p',
'-n', 'foo',
'-u', 'bar',
'-c', 'baz', 'a',
])
expected = """start on runlevel [2345]
stop on runlevel [016]
env OS_SVC_ENABLE_CONTROL=1
export OS_SVC_ENABLE_CONTROL
pre-start script
mkdir -p /var/run/bar
chown -R bar:bar /var/run/bar
end script
respawn
# the default post-start of 1 second sleep delays respawning enough to
# not hit the default of 10 times in 5 seconds. Make it 2 times in 5s.
respawn limit 2 5
exec start-stop-daemon --start -c bar --exec /opt/stack/venvs/bar/bin/baz -- a
post-start exec sleep 1
"""
self.assertEqual(expected, output)
def test_dir_only_systemd(self):
self._stub_script('dib-init-system', 'echo systemd')
output = self._run_command(
['elements/os-svc-install/bin/os-svc-daemon',
'-p',
'-n', 'foo',
'-u', 'bar',
])
expected = """[Unit]
Description=Create /var/run/bar
[Service]
ExecStartPre=/bin/mkdir -p /var/run/bar
ExecStart=/bin/chown -R bar:bar /var/run/bar
[Install]
RequiredBy=foo.service
"""
self.assertEqual(expected, output)
def test_dir_only_upstart(self):
self._stub_script('dib-init-system', 'echo upstart')
output = self._run_command(
['elements/os-svc-install/bin/os-svc-daemon',
'-p',
'-n', 'foo',
'-u', 'bar',
])
expected = """start on runlevel [2345]
stop on runlevel [016]
env OS_SVC_ENABLE_CONTROL=1
export OS_SVC_ENABLE_CONTROL
pre-start script
mkdir -p /var/run/bar
chown -R bar:bar /var/run/bar
end script
"""
self.assertEqual(expected, output)
def test_install_dir_systemd(self):
self._stub_script('dib-init-system', 'echo systemd')
output = self._run_command(
['elements/os-svc-install/bin/os-svc-daemon',
'-p',
'-n', 'foo',
'-u', 'foo',
'-i', '/test/dir',
'-c', 'foo', 'arg',
])
self.assertIn('ExecStart=/test/dir/bin/foo arg', output)
def test_install_dir_upstart(self):
self._stub_script('dib-init-system', 'echo upstart')
output = self._run_command(
['elements/os-svc-install/bin/os-svc-daemon',
'-p',
'-n', 'foo',
'-u', 'foo',
'-i', '/test/dir',
'-c', 'foo', 'arg',
])
self.assertIn('--exec /test/dir/bin/foo -- arg', output)
def test_environment_systemd(self):
self._stub_script('dib-init-system', 'echo systemd')
output = self._run_command(
['elements/os-svc-install/bin/os-svc-daemon',
'-p',
'-n', 'foo',
'-u', 'foo',
'-e', '"foo=bar"',
'-c', 'foo', 'arg',
])
self.assertIn('Environment="foo=bar"', output)
def test_environment_upstart(self):
self._stub_script('dib-init-system', 'echo upstart')
output = self._run_command(
['elements/os-svc-install/bin/os-svc-daemon',
'-p',
'-n', 'foo',
'-u', 'foo',
'-e', 'foo=bar',
'-c', 'foo', 'arg',
])
self.assertIn('env foo=bar', output)
def test_post_start_upstart(self):
self._stub_script('dib-init-system', 'echo upstart')
output = self._run_command(
['elements/os-svc-install/bin/os-svc-daemon',
'-p',
'-n', 'foo',
'-u', 'foo',
'-s', 'bar',
'-c', 'foo', 'arg',
])
self.assertIn('post-start bar', output)
def test_runtime_dir_systemd(self):
self._stub_script('dib-init-system', 'echo systemd')
output = self._run_command(
['elements/os-svc-install/bin/os-svc-daemon',
'-p',
'-n', 'foo',
'-u', 'bar',
'-d', 'baz',
'-c', 'foo', 'arg',
])
self.assertIn('ExecStartPre=/bin/mkdir -p /var/run/baz', output)
self.assertIn('ExecStart=/bin/chown -R bar:bar /var/run/baz', output)
def test_runtime_dir_upstart(self):
self._stub_script('dib-init-system', 'echo upstart')
output = self._run_command(
['elements/os-svc-install/bin/os-svc-daemon',
'-p',
'-n', 'foo',
'-u', 'bar',
'-d', 'baz',
'-c', 'foo', 'arg',
])
self.assertIn('mkdir -p /var/run/baz', output)
self.assertIn('chown -R bar:bar /var/run/baz', output)

0
tests/__init__.py Normal file
View File

45
tests/base.py Normal file
View File

@ -0,0 +1,45 @@
# Copyright 2014 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
import subprocess
import tempfile
from oslotest import base
class ScriptTestBase(base.BaseTestCase):
def setUp(self):
super(ScriptTestBase, self).setUp()
self.tmpdir = tempfile.mkdtemp()
self.env = os.environ.copy()
self.env['PATH'] = self.env['PATH'] + ':' + self.tmpdir
def _stub_script(self, name, contents):
filename = os.path.join(self.tmpdir, name)
with open(filename, 'w') as f:
f.write('#!/bin/bash\n')
f.write(contents)
f.write('\n')
os.chmod(filename, 0o700)
def _run_command(self, cmd):
try:
return subprocess.check_output(cmd,
stderr=subprocess.STDOUT,
env=self.env)
# NOTE(bnemec): If we don't handle this exception, all we get is the
# exit code if the command fails.
except subprocess.CalledProcessError as e:
self.fail(e.output)