Update to 100% coverage.
This commit is contained in:
parent
b99f2cf010
commit
a9fb83be8a
7
.coveragerc
Normal file
7
.coveragerc
Normal file
@ -0,0 +1,7 @@
|
||||
[report]
|
||||
# Regexes for lines to exclude from consideration
|
||||
exclude_lines =
|
||||
if __name__ == .__main__.:
|
||||
include=
|
||||
hooks/swift_storage_*
|
||||
|
14
Makefile
Normal file
14
Makefile
Normal file
@ -0,0 +1,14 @@
|
||||
#!/usr/bin/make
|
||||
PYTHON := /usr/bin/env python
|
||||
|
||||
lint:
|
||||
@flake8 --exclude hooks/charmhelpers hooks
|
||||
@flake8 --exclude hooks/charmhelpers unit_tests
|
||||
@charm proof
|
||||
|
||||
test:
|
||||
@echo Starting tests...
|
||||
@$(PYTHON) /usr/bin/nosetests --nologcapture --with-coverage unit_tests
|
||||
|
||||
sync:
|
||||
@charm-helper-sync -c charm-helpers-sync.yaml
|
@ -42,7 +42,8 @@ class RsyncContext(OSContextGenerator):
|
||||
interfaces = []
|
||||
|
||||
def enable_rsyncd(self):
|
||||
default = open('/etc/default/rsync').read()
|
||||
with open('/etc/default/rsync') as _in:
|
||||
default = _in.read()
|
||||
_m = re.compile('^RSYNC_ENABLE=(.*)$', re.MULTILINE)
|
||||
if not re.search(_m, default):
|
||||
with open('/etc/default/rsync', 'a+') as out:
|
||||
|
@ -16,7 +16,7 @@ from swift_storage_utils import (
|
||||
)
|
||||
|
||||
from charmhelpers.core.hookenv import (
|
||||
Hooks,
|
||||
Hooks, UnregisteredHookError,
|
||||
config,
|
||||
log,
|
||||
relation_get,
|
||||
@ -81,5 +81,13 @@ def swift_storage_relation_changed():
|
||||
CONFIGS.write('/etc/swift/swift.conf')
|
||||
fetch_swift_rings(rings_url)
|
||||
|
||||
if '/usr/bin/nosetests' not in sys.argv:
|
||||
hooks.execute(sys.argv)
|
||||
|
||||
def main():
|
||||
try:
|
||||
hooks.execute(sys.argv)
|
||||
except UnregisteredHookError as e:
|
||||
log('Unknown hook {} - skipping.'.format(e))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
@ -121,6 +121,7 @@ def find_block_devices():
|
||||
blacklist = ['sda', 'vda', 'cciss/c0d0']
|
||||
|
||||
with open('/proc/partitions') as proc:
|
||||
print proc
|
||||
partitions = [p.split() for p in proc.readlines()[2:]]
|
||||
for partition in [p[3] for p in partitions if p]:
|
||||
for inc in incl:
|
||||
|
6
setup.cfg
Normal file
6
setup.cfg
Normal file
@ -0,0 +1,6 @@
|
||||
[nosesetests]
|
||||
verbosity=2
|
||||
with-coverage=1
|
||||
cover-erase=1
|
||||
cover-package=hooks
|
||||
|
@ -0,0 +1,2 @@
|
||||
import sys
|
||||
sys.path.append('hooks')
|
73
unit_tests/test_swift_storage_context.py
Normal file
73
unit_tests/test_swift_storage_context.py
Normal file
@ -0,0 +1,73 @@
|
||||
from mock import MagicMock, patch
|
||||
|
||||
from unit_tests.test_utils import CharmTestCase, patch_open
|
||||
|
||||
|
||||
import hooks.swift_storage_context as swift_context
|
||||
|
||||
|
||||
TO_PATCH = [
|
||||
'config',
|
||||
'log',
|
||||
'related_units',
|
||||
'relation_get',
|
||||
'relation_ids',
|
||||
'get_host_ip',
|
||||
]
|
||||
|
||||
|
||||
class SwiftStorageContextTests(CharmTestCase):
|
||||
def setUp(self):
|
||||
super(SwiftStorageContextTests, self).setUp(swift_context, TO_PATCH)
|
||||
self.config.side_effect = self.test_config.get
|
||||
|
||||
def test_swift_storage_context_missing_data(self):
|
||||
self.relation_ids.return_value = []
|
||||
ctxt = swift_context.SwiftStorageContext()
|
||||
self.assertEquals(ctxt(), {})
|
||||
self.relation_ids.return_value = ['swift-proxy:0']
|
||||
self.related_units.return_value = ['swift-proxy/0']
|
||||
self.relation_get.return_value = ''
|
||||
self.assertEquals(ctxt(), {})
|
||||
|
||||
def test_swift_storage_context_with_data(self):
|
||||
self.relation_ids.return_value = []
|
||||
ctxt = swift_context.SwiftStorageContext()
|
||||
self.assertEquals(ctxt(), {})
|
||||
self.relation_ids.return_value = ['swift-proxy:0']
|
||||
self.related_units.return_value = ['swift-proxy/0']
|
||||
self.relation_get.return_value = 'fooooo'
|
||||
self.assertEquals(ctxt(), {'swift_hash': 'fooooo'})
|
||||
|
||||
def test_rsync_context(self):
|
||||
self.get_host_ip.return_value = '10.0.0.5'
|
||||
ctxt = swift_context.RsyncContext()
|
||||
ctxt.enable_rsyncd = MagicMock()
|
||||
ctxt.enable_rsyncd.return_value = True
|
||||
self.assertEquals({'local_ip': '10.0.0.5'}, ctxt())
|
||||
self.assertTrue(ctxt.enable_rsyncd.called)
|
||||
|
||||
def test_rsync_enale_rsync(self):
|
||||
with patch_open() as (_open, _file):
|
||||
ctxt = swift_context.RsyncContext()
|
||||
_file.read.return_value = 'RSYNC_ENABLE=false'
|
||||
ctxt.enable_rsyncd()
|
||||
_file.write.assert_called_with('RSYNC_ENABLE=true')
|
||||
_file.read.return_value = '#foo'
|
||||
ctxt.enable_rsyncd()
|
||||
_file.write.assert_called_with('RSYNC_ENABLE=true\n')
|
||||
|
||||
def test_swift_storage_server_context(self):
|
||||
self.get_host_ip.return_value = '10.0.0.5'
|
||||
self.test_config.set('account-server-port', '500')
|
||||
self.test_config.set('object-server-port', '501')
|
||||
self.test_config.set('container-server-port', '502')
|
||||
ctxt = swift_context.SwiftStorageServerContext()
|
||||
result = ctxt()
|
||||
ex = {
|
||||
'container_server_port': '502',
|
||||
'object_server_port': '501',
|
||||
'account_server_port': '500',
|
||||
'local_ip': '10.0.0.5'
|
||||
}
|
||||
self.assertEquals(ex, result)
|
@ -100,3 +100,16 @@ class SwiftStorageRelationsTests(CharmTestCase):
|
||||
self.fetch_swift_rings.assert_called_with(
|
||||
'http://swift-proxy.com/rings/'
|
||||
)
|
||||
|
||||
@patch('sys.argv')
|
||||
@patch.object(relations, 'install')
|
||||
def test_main_hook_exists(self, _install, _argv):
|
||||
_argv = ['hooks/install']
|
||||
relations.main()
|
||||
_install.assert_called()
|
||||
|
||||
@patch('sys.argv')
|
||||
def test_main_hook_missing(self, _argv):
|
||||
_argv = ['hooks/start']
|
||||
relations.main()
|
||||
self.log.assert_called()
|
||||
|
@ -1,6 +1,5 @@
|
||||
from mock import call, patch, MagicMock
|
||||
from contextlib import contextmanager
|
||||
from unit_tests.test_utils import CharmTestCase
|
||||
from unit_tests.test_utils import CharmTestCase, patch_open
|
||||
|
||||
|
||||
import hooks.swift_storage_utils as swift_utils
|
||||
@ -59,24 +58,6 @@ SCRIPT_RC_ENV = {
|
||||
}
|
||||
|
||||
|
||||
@contextmanager
|
||||
def patch_open():
|
||||
'''Patch open() to allow mocking both open() itself and the file that is
|
||||
yielded.
|
||||
|
||||
Yields the mock for "open" and "file", respectively.'''
|
||||
mock_open = MagicMock(spec=open)
|
||||
mock_file = MagicMock(spec=file)
|
||||
|
||||
@contextmanager
|
||||
def stub_open(*args, **kwargs):
|
||||
mock_open(*args, **kwargs)
|
||||
yield mock_file
|
||||
|
||||
with patch('__builtin__.open', stub_open):
|
||||
yield mock_open, mock_file
|
||||
|
||||
|
||||
class SwiftStorageUtilsTests(CharmTestCase):
|
||||
def setUp(self):
|
||||
super(SwiftStorageUtilsTests, self).setUp(swift_utils, TO_PATCH)
|
||||
@ -185,3 +166,35 @@ class SwiftStorageUtilsTests(CharmTestCase):
|
||||
self.get_host_ip.return_value = '10.0.0.1'
|
||||
swift_utils.save_script_rc()
|
||||
self._save_script_rc.assert_called_with(**SCRIPT_RC_ENV)
|
||||
|
||||
@patch('hooks.charmhelpers.contrib.openstack.templating.OSConfigRenderer')
|
||||
def test_register_configs_pre_install(self, renderer):
|
||||
self.get_os_codename_package.return_value = None
|
||||
swift_utils.register_configs()
|
||||
renderer.assert_called_with(templates_dir=swift_utils.TEMPLATES,
|
||||
openstack_release='essex')
|
||||
|
||||
@patch.object(swift_utils, 'SwiftStorageContext')
|
||||
@patch.object(swift_utils, 'RsyncContext')
|
||||
@patch.object(swift_utils, 'SwiftStorageServerContext')
|
||||
@patch('hooks.charmhelpers.contrib.openstack.templating.OSConfigRenderer')
|
||||
def test_register_configs_post_install(self, renderer,
|
||||
swift, rsync, server):
|
||||
swift.return_value = 'swift_context'
|
||||
rsync.return_value = 'rsync_context'
|
||||
server.return_value = 'swift_server_context'
|
||||
self.get_os_codename_package.return_value = 'grizzly'
|
||||
configs = MagicMock()
|
||||
configs.register = MagicMock()
|
||||
renderer.return_value = configs
|
||||
swift_utils.register_configs()
|
||||
renderer.assert_called_with(templates_dir=swift_utils.TEMPLATES,
|
||||
openstack_release='grizzly')
|
||||
ex = [
|
||||
call('/etc/swift/swift.conf', ['swift_server_context']),
|
||||
call('/etc/rsyncd.conf', ['rsync_context']),
|
||||
call('/etc/swift/account-server.conf', ['swift_context']),
|
||||
call('/etc/swift/object-server.conf', ['swift_context']),
|
||||
call('/etc/swift/container-server.conf', ['swift_context'])
|
||||
]
|
||||
self.assertEquals(ex, configs.register.call_args_list)
|
||||
|
@ -3,7 +3,8 @@ import unittest
|
||||
import os
|
||||
import yaml
|
||||
|
||||
from mock import patch
|
||||
from contextlib import contextmanager
|
||||
from mock import MagicMock, patch
|
||||
|
||||
|
||||
def load_config():
|
||||
@ -97,3 +98,21 @@ class TestRelation(object):
|
||||
elif attr in self.relation_data:
|
||||
return self.relation_data[attr]
|
||||
return None
|
||||
|
||||
|
||||
@contextmanager
|
||||
def patch_open():
|
||||
'''Patch open() to allow mocking both open() itself and the file that is
|
||||
yielded.
|
||||
|
||||
Yields the mock for "open" and "file", respectively.'''
|
||||
mock_open = MagicMock(spec=open)
|
||||
mock_file = MagicMock(spec=file)
|
||||
|
||||
@contextmanager
|
||||
def stub_open(*args, **kwargs):
|
||||
mock_open(*args, **kwargs)
|
||||
yield mock_file
|
||||
|
||||
with patch('__builtin__.open', stub_open):
|
||||
yield mock_open, mock_file
|
||||
|
Loading…
Reference in New Issue
Block a user