Add some basic unit tests

This commit is contained in:
James Page 2016-11-21 12:29:29 +00:00
parent cd2ba26af6
commit 04fe473a3a
5 changed files with 131 additions and 7 deletions

View File

@ -16,7 +16,7 @@ setup:
- "{snap_common}/etc/nova"
- "{snap_common}/logs"
templates:
"nova-snap.conf.j2": "[snap_common}/etc/nova.conf.d/nova-snap.conf"
"nova-snap.conf.j2": "{snap_common}/etc/nova.conf.d/nova-snap.conf"
# Entry points are used to execute commands from with the snap
# with a sane set of defaults in terms of configuration files
# and directories
@ -38,7 +38,7 @@ entry_points:
binary: nova-manage
config-files:
- "{snap}/etc/nova/nova.conf"
- "[snap_common}/etc/nova/nova.conf"
- "{snap_common}/etc/nova/nova.conf"
config-dirs:
- "{snap_common}/etc/nova.conf.d"
log-file:

View File

@ -6,3 +6,4 @@ pbr>=1.6 # Apache-2.0
# Left unversioned as designed to align with OpenStack component being snapped
jinja2
jsonschema

View File

@ -0,0 +1,51 @@
# Sample snap-openstack configuration file
#
# snap-openstack will automatically substitute the following
# in both paths for files and directories and in templates:
#
# SNAP_COMMON -> snap_common
# SNAP -> snap
# SNAP -> snap_shared
#
# Setup is executed for all entry points prior to execution
# snap-openstack will assure that templated files are in place
# and that any directory structure in $SNAP_COMMON is created
setup:
dirs:
- "{snap_common}/etc/nova.conf.d"
- "{snap_common}/etc/nova"
- "{snap_common}/logs"
templates:
"nova-snap.conf.j2": "{snap_common}/etc/nova.conf.d/nova-snap.conf"
# Entry points are used to execute commands from with the snap
# with a sane set of defaults in terms of configuration files
# and directories
entry_points:
# Executes the following:
#
# nova-manage --config-file=$SNAP/etc/nova/nova,conf \
# --config-file=$SNAP_COMMON/etc/nova/nova.conf \
# --config-dir=$SNAP_COMMON/etc/nova.conf.d \
# --log-file=$SNAP_COMMON/logs/nova-manage.log
#
# this is designed to be executed from the snapcraft.yaml apps section
# using:
#
# command: snap-openstack nova-manage
#
# any additional arguments will be passed to the underlying binary
nova-manage:
binary: nova-manage
config-files:
- "{snap}/etc/nova/nova.conf"
- "{snap_common}/etc/nova/nova.conf"
config-dirs:
- "{snap_common}/etc/nova.conf.d"
nova-scheduler:
binary: nova-scheduler
config-files:
- "{snap}/etc/nova/nova.conf"
- "{snap_common}/etc/nova/nova.conf"
config-dirs:
- "{snap_common}/etc/nova.conf.d"
log-file: "{snap_common}/logs/nova-scheduler.log"

View File

@ -19,10 +19,82 @@ test_snap_openstack
Tests for `snap_openstack` module.
"""
from snap_openstack.tests import base
import os
from mock import patch
from snap_openstack import base
from snap_openstack.tests import base as test_base
TEST_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)),
'data')
MOCK_SNAP_ENV = {
'snap_common': '/var/snap/test/common',
'snap': '/snap/common',
}
class TestSnap_openstack(base.TestCase):
class TestSnapOpenStack(test_base.TestCase):
def test_something(self):
pass
@classmethod
def mock_exists(cls, path):
'''Test helper for os.path.exists'''
paths = {
'/snap/common/etc/nova/nova.conf': True,
'/var/snap/test/common/etc/nova.conf.d': True,
}
return paths.get(path, False)
@patch.object(base, 'snap_env')
@patch.object(base, 'os')
def test_base_snap_config(self, mock_os,
mock_snap_env):
'''Ensure wrapped binary called with full args list'''
mock_snap_env.return_value = MOCK_SNAP_ENV
snap = base.OpenStackSnap(os.path.join(TEST_DIR,
'snap-openstack.yaml'))
mock_os.path.exists.side_effect = self.mock_exists
snap.execute(['snap-openstack',
'nova-scheduler'])
mock_os.execvp.assert_called_with(
'nova-scheduler',
['nova-scheduler',
'--config-file=/snap/common/etc/nova/nova.conf',
'--config-dir=/var/snap/test/common/etc/nova.conf.d',
'--log-file=/var/snap/test/common/logs/nova-scheduler.log']
)
@patch.object(base, 'snap_env')
@patch.object(base, 'os')
def test_base_snap_config_no_logging(self, mock_os,
mock_snap_env):
'''Ensure wrapped binary called correctly with no logfile'''
mock_snap_env.return_value = MOCK_SNAP_ENV
snap = base.OpenStackSnap(os.path.join(TEST_DIR,
'snap-openstack.yaml'))
mock_os.path.exists.side_effect = self.mock_exists
snap.execute(['snap-openstack',
'nova-manage',
'db', 'sync'])
mock_os.execvp.assert_called_with(
'nova-manage',
['nova-manage',
'--config-file=/snap/common/etc/nova/nova.conf',
'--config-dir=/var/snap/test/common/etc/nova.conf.d',
'db', 'sync']
)
@patch.object(base, 'snap_env')
@patch.object(base, 'os')
def test_base_snap_config_missing_entry_point(self, mock_os,
mock_snap_env):
'''Ensure ValueError raised for missing entry_point'''
mock_snap_env.return_value = MOCK_SNAP_ENV
snap = base.OpenStackSnap(os.path.join(TEST_DIR,
'snap-openstack.yaml'))
mock_os.path.exists.side_effect = self.mock_exists
self.assertRaises(ValueError,
snap.execute,
['snap-openstack',
'nova-api'])

View File

@ -1,6 +1,6 @@
[tox]
minversion = 2.0
envlist = py34,py27,pypy,pep8
envlist = py35,py27,pep8
skipsdist = True
[testenv]