Finsh unit tests and fix lint

This commit is contained in:
Liam Young 2015-02-10 10:30:13 +00:00
parent bee432ba56
commit ecddb69e30
5 changed files with 208 additions and 60 deletions

View File

@ -5,4 +5,3 @@ exclude_lines =
include=
hooks/ceph.py
hooks/hooks.py
hooks/utils.py

View File

@ -2,7 +2,7 @@
PYTHON := /usr/bin/env python
lint:
@flake8 --exclude hooks/charmhelpers hooks tests
@flake8 --exclude hooks/charmhelpers hooks tests unit_tests
@charm proof
unit_test:

View File

@ -86,7 +86,7 @@ def enable_pocket(pocket):
def get_host_ip(hostname=None):
try:
if not hostname:
hostname=unit_get('private-address')
hostname = unit_get('private-address')
# Test to see if already an IPv4 address
socket.inet_aton(hostname)
return hostname

View File

@ -1,5 +1,4 @@
from mock import call, patch, MagicMock
from test_utils import CharmTestCase, patch_open
from test_utils import CharmTestCase
import ceph
@ -10,26 +9,34 @@ TO_PATCH = [
'time',
]
class CephRadosGWCephTests(CharmTestCase):
def setUp(self):
super(CephRadosGWCephTests, self).setUp(ceph, TO_PATCH)
def test_is_quorum_leader(self):
self.os.path.exists.return_value = True
self.get_unit_hostname.return_value = 'myhost'
self.subprocess.check_output.return_value = '{"state": "leader"}'
self.assertEqual(ceph.is_quorum(), True)
def test_is_quorum_notleader(self):
self.os.path.exists.return_value = True
self.get_unit_hostname.return_value = 'myhost'
self.subprocess.check_output.return_value = '{"state": "notleader"}'
self.assertEqual(ceph.is_quorum(), False)
def test_is_quorum_valerror(self):
self.os.path.exists.return_value = True
self.get_unit_hostname.return_value = 'myhost'
self.subprocess.check_output.return_value = "'state': 'bob'}"
self.assertEqual(ceph.is_quorum(), False)
def test_is_quorum_no_asok(self):
self.os.path.exists.return_value = False
self.assertEqual(ceph.is_quorum(), False)
def test_is_leader(self):
self.get_unit_hostname.return_value = 'myhost'
self.os.path.exists.return_value = True
@ -53,18 +60,31 @@ class CephRadosGWCephTests(CharmTestCase):
self.os.path.exists.return_value = False
self.assertEqual(ceph.is_leader(), False)
# def test_wait_for_quorum_yes(self):
# _is_quorum = self.patch('is_quorum')
# _is_quorum.return_value = False
# self.time.return_value = None
# ceph.wait_for_quorum()
# self.time.sleep.assert_called_with(3)
def test_wait_for_quorum_yes(self):
results = [True, False]
# def test_wait_for_quorum_no(self):
# _is_quorum = self.patch('is_quorum')
# _is_quorum.return_value = True
# ceph.wait_for_quorum()
# self.assertFalse(self.time.sleep.called)
def quorum():
return results.pop()
_is_quorum = self.patch('is_quorum')
_is_quorum.side_effect = quorum
ceph.wait_for_quorum()
self.time.sleep.assert_called_with(3)
def test_wait_for_quorum_no(self):
_is_quorum = self.patch('is_quorum')
_is_quorum.return_value = True
ceph.wait_for_quorum()
self.assertFalse(self.time.sleep.called)
def test_wait_for_bootstrap(self):
results = [True, False]
def bootstrapped():
return results.pop()
_is_bootstrapped = self.patch('is_bootstrapped')
_is_bootstrapped.side_effect = bootstrapped
ceph.wait_for_bootstrap()
self.time.sleep.assert_called_with(3)
def test_add_bootstrap_hint(self):
self.get_unit_hostname.return_value = 'myhost'
@ -77,7 +97,7 @@ class CephRadosGWCephTests(CharmTestCase):
]
self.os.path.exists.return_value = True
ceph.add_bootstrap_hint('mypeer')
self.subprocess.call.assert_called_with(cmd)
self.subprocess.call.assert_called_with(cmd)
def test_add_bootstrap_hint_noasok(self):
self.get_unit_hostname.return_value = 'myhost'
@ -87,14 +107,16 @@ class CephRadosGWCephTests(CharmTestCase):
def test_is_osd_disk(self):
# XXX Insert real sgdisk output
self.subprocess.check_output.return_value = 'Partition GUID code: 4FBD7E29-9D25-41B8-AFD0-062C0CEFF05D'
self.subprocess.check_output.return_value = \
'Partition GUID code: 4FBD7E29-9D25-41B8-AFD0-062C0CEFF05D'
self.assertEqual(ceph.is_osd_disk('/dev/fmd0'), True)
def test_is_osd_disk_no(self):
# XXX Insert real sgdisk output
self.subprocess.check_output.return_value = 'Partition GUID code: 5FBD7E29-9D25-41B8-AFD0-062C0CEFF05D'
self.subprocess.check_output.return_value = \
'Partition GUID code: 5FBD7E29-9D25-41B8-AFD0-062C0CEFF05D'
self.assertEqual(ceph.is_osd_disk('/dev/fmd0'), False)
def test_rescan_osd_devices(self):
cmd = [
'udevadm', 'trigger',
@ -102,7 +124,7 @@ class CephRadosGWCephTests(CharmTestCase):
]
ceph.rescan_osd_devices()
self.subprocess.call.assert_called_with(cmd)
def test_zap_disk(self):
cmd = [
'sgdisk', '--zap-all', '/dev/fmd0',
@ -121,9 +143,55 @@ class CephRadosGWCephTests(CharmTestCase):
]
ceph.import_osd_bootstrap_key('mykey')
self.subprocess.check_call.assert_called_with(cmd)
def test_is_bootstrapped(self):
self.os.path.exists.return_value = True
self.assertEqual(ceph.is_bootstrapped(), True)
self.assertEqual(ceph.is_bootstrapped(), True)
self.os.path.exists.return_value = False
self.assertEqual(ceph.is_bootstrapped(), False)
self.assertEqual(ceph.is_bootstrapped(), False)
def test_import_radosgw_key(self):
self.os.path.exists.return_value = False
ceph.import_radosgw_key('mykey')
cmd = [
'ceph-authtool',
'/etc/ceph/keyring.rados.gateway',
'--create-keyring',
'--name=client.radosgw.gateway',
'--add-key=mykey'
]
self.subprocess.check_call.assert_called_with(cmd)
def test_get_named_key_create(self):
self.get_unit_hostname.return_value = "myhost"
self.subprocess.check_output.return_value = """
[client.dummy]
key = AQAPiu1RCMb4CxAAmP7rrufwZPRqy8bpQa2OeQ==
"""
self.assertEqual(ceph.get_named_key('dummy'),
'AQAPiu1RCMb4CxAAmP7rrufwZPRqy8bpQa2OeQ==')
cmd = [
'ceph',
'--name', 'mon.',
'--keyring',
'/var/lib/ceph/mon/ceph-myhost/keyring',
'auth', 'get-or-create', 'client.dummy',
'mon', 'allow r', 'osd', 'allow rwx'
]
self.subprocess.check_output.assert_called_with(cmd)
def test_get_named_key_get(self):
self.get_unit_hostname.return_value = "myhost"
key = "AQAPiu1RCMb4CxAAmP7rrufwZPRqy8bpQa2OeQ=="
self.subprocess.check_output.return_value = key
self.assertEqual(ceph.get_named_key('dummy'), key)
cmd = [
'ceph',
'--name', 'mon.',
'--keyring',
'/var/lib/ceph/mon/ceph-myhost/keyring',
'auth', 'get-or-create', 'client.dummy',
'mon', 'allow r', 'osd', 'allow rwx'
]
self.subprocess.check_output.assert_called_with(cmd)

View File

@ -2,17 +2,27 @@
from mock import call, patch, MagicMock
from test_utils import CharmTestCase, patch_open
import utils
_reg = utils.register_configs
utils.register_configs = MagicMock()
import hooks as ceph_hooks
utils.register_configs = _reg
TO_PATCH = [
'add_source',
'apt_update',
'apt_install',
'apt_purge',
'config',
'cmp_pkgrevno',
'execd_preinstall',
'enable_pocket',
'get_host_ip',
'get_iface_for_address',
'get_netmask_for_address',
'get_unit_hostname',
'glob',
'is_apache_24',
@ -25,12 +35,14 @@ TO_PATCH = [
'relation_set',
'relation_get',
'render_template',
'resolve_address',
'shutil',
'subprocess',
'sys',
'unit_get',
]
class CephRadosGWTests(CharmTestCase):
def setUp(self):
@ -47,10 +59,11 @@ class CephRadosGWTests(CharmTestCase):
def test_install_ceph_optimised_packages(self):
self.lsb_release.return_value = {'DISTRIB_CODENAME': 'vivid'}
git_url = 'http://gitbuilder.ceph.com'
fastcgi_source = ('http://gitbuilder.ceph.com/'
fastcgi_source = (
'http://gitbuilder.ceph.com/'
'libapache-mod-fastcgi-deb-vivid-x86_64-basic/ref/master')
apache_source = ('http://gitbuilder.ceph.com/'
apache_source = (
'http://gitbuilder.ceph.com/'
'apache2-deb-vivid-x86_64-basic/ref/master')
calls = [
call(fastcgi_source, key='6EAEAE2203C3951A'),
@ -64,22 +77,33 @@ class CephRadosGWTests(CharmTestCase):
ceph_hooks.install_packages()
self.add_source.assert_called_with('distro', 'secretkey')
self.apt_update.assert_called()
self.apt_install.assert_called_with(['radosgw',
'libapache2-mod-fastcgi',
'apache2',
'ntp'], fatal=True)
self.apt_install.assert_called_with(['libapache2-mod-fastcgi',
'apache2'], fatal=True)
def test_install_optimised_packages(self):
def test_install_optimised_packages_no_embedded(self):
self.test_config.set('use-ceph-optimised-packages', True)
self.test_config.set('use-embedded-webserver', False)
_install_packages = self.patch('install_ceph_optimised_packages')
ceph_hooks.install_packages()
self.add_source.assert_called_with('distro', 'secretkey')
self.apt_update.assert_called()
_install_packages.assert_called()
self.apt_install.assert_called_with(['libapache2-mod-fastcgi',
'apache2'], fatal=True)
def test_install_optimised_packages_embedded(self):
self.test_config.set('use-ceph-optimised-packages', True)
self.test_config.set('use-embedded-webserver', True)
_install_packages = self.patch('install_ceph_optimised_packages')
ceph_hooks.install_packages()
self.add_source.assert_called_with('distro', 'secretkey')
self.apt_update.assert_called()
_install_packages.assert_called()
self.apt_install.assert_called_with(['radosgw',
'libapache2-mod-fastcgi',
'apache2',
'ntp'], fatal=True)
'ntp',
'haproxy'], fatal=True)
self.apt_purge.assert_called_with(['libapache2-mod-fastcgi',
'apache2'])
def test_install(self):
_install_packages = self.patch('install_packages')
@ -105,9 +129,10 @@ class CephRadosGWTests(CharmTestCase):
'old_auth': False,
'use_syslog': 'false',
'keystone_key': 'keystone_value',
'embedded_webserver': False,
}
self.cmp_pkgrevno.return_value = 1
with patch_open() as (_open, _file):
with patch_open() as (_open, _file):
ceph_hooks.emit_cephconf()
self.os.makedirs.assert_called_with('/etc/ceph')
_open.assert_called_with('/etc/ceph/ceph.conf', 'w')
@ -119,9 +144,10 @@ class CephRadosGWTests(CharmTestCase):
apachecontext = {
"hostname": '10.0.0.1',
}
with patch_open() as (_open, _file):
vhost_file = '/etc/apache2/sites-available/rgw.conf'
with patch_open() as (_open, _file):
ceph_hooks.emit_apacheconf()
_open.assert_called_with('/etc/apache2/sites-available/rgw.conf', 'w')
_open.assert_called_with(vhost_file, 'w')
self.render_template.assert_called_with('rgw', apachecontext)
def test_apache_sites24(self):
@ -205,6 +231,7 @@ class CephRadosGWTests(CharmTestCase):
self.test_config.set('revocation-check-interval', '21')
self.relation_ids.return_value = ['idrelid']
self.related_units.return_value = ['idunit']
def _relation_get(key, unit, relid):
ks_dict = {
'auth_protocol': 'https',
@ -214,15 +241,15 @@ class CephRadosGWTests(CharmTestCase):
}
return ks_dict[key]
self.relation_get.side_effect = _relation_get
self.assertEquals(ceph_hooks.get_keystone_conf(),
{'auth_type': 'keystone',
'auth_protocol': 'https',
'admin_token': 'sectocken',
'user_roles': 'admin',
'auth_host': '10.0.0.2',
'cache_size': '42',
'auth_port': '8090',
'revocation_check_interval': '21'})
self.assertEquals(ceph_hooks.get_keystone_conf(), {
'auth_type': 'keystone',
'auth_protocol': 'https',
'admin_token': 'sectocken',
'user_roles': 'admin',
'auth_host': '10.0.0.2',
'cache_size': '42',
'auth_port': '8090',
'revocation_check_interval': '21'})
def test_get_keystone_conf_missinginfo(self):
self.test_config.set('operator-roles', 'admin')
@ -230,6 +257,7 @@ class CephRadosGWTests(CharmTestCase):
self.test_config.set('revocation-check-interval', '21')
self.relation_ids.return_value = ['idrelid']
self.related_units.return_value = ['idunit']
def _relation_get(key, unit, relid):
ks_dict = {
'auth_protocol': 'https',
@ -248,6 +276,7 @@ class CephRadosGWTests(CharmTestCase):
ceph_hooks.mon_relation()
_restart.assert_called()
_ceph.import_radosgw_key.assert_called_with('seckey')
_emit_cephconf.assert_called()
def test_mon_relation_nokey(self):
_emit_cephconf = self.patch('emit_cephconf')
@ -257,6 +286,7 @@ class CephRadosGWTests(CharmTestCase):
ceph_hooks.mon_relation()
self.assertFalse(_ceph.import_radosgw_key.called)
self.assertFalse(_restart.called)
_emit_cephconf.assert_called()
def test_gateway_relation(self):
self.unit_get.return_value = 'myserver'
@ -265,15 +295,18 @@ class CephRadosGWTests(CharmTestCase):
def test_start(self):
ceph_hooks.start()
self.subprocess.call.assert_called_with(['service', 'radosgw', 'start'])
cmd = ['service', 'radosgw', 'start']
self.subprocess.call.assert_called_with(cmd)
def test_stop(self):
ceph_hooks.stop()
self.subprocess.call.assert_called_with(['service', 'radosgw', 'stop'])
cmd = ['service', 'radosgw', 'stop']
self.subprocess.call.assert_called_with(cmd)
def test_start(self):
def test_restart(self):
ceph_hooks.restart()
self.subprocess.call.assert_called_with(['service', 'radosgw', 'restart'])
cmd = ['service', 'radosgw', 'restart']
self.subprocess.call.assert_called_with(cmd)
def test_identity_joined_early_version(self):
self.cmp_pkgrevno.return_value = -1
@ -282,18 +315,20 @@ class CephRadosGWTests(CharmTestCase):
def test_identity_joined(self):
self.cmp_pkgrevno.return_value = 1
self.resolve_address.return_value = 'myserv'
self.test_config.set('region', 'region1')
self.test_config.set('operator-roles', 'admin')
self.unit_get.return_value = 'myserv'
ceph_hooks.identity_joined(relid='rid')
self.relation_set.assert_called_with(service='swift',
region='region1',
public_url='http://myserv:80/swift/v1',
internal_url='http://myserv:80/swift/v1',
requested_roles='admin',
rid='rid',
admin_url='http://myserv:80/swift')
self.relation_set.assert_called_with(
service='swift',
region='region1',
public_url='http://myserv:80/swift/v1',
internal_url='http://myserv:80/swift/v1',
requested_roles='admin',
relation_id='rid',
admin_url='http://myserv:80/swift')
def test_identity_changed(self):
_emit_cephconf = self.patch('emit_cephconf')
_restart = self.patch('restart')
@ -301,3 +336,49 @@ class CephRadosGWTests(CharmTestCase):
_emit_cephconf.assert_called()
_restart.assert_called()
def test_canonical_url_ipv6(self):
ipv6_addr = '2001:db8:85a3:8d3:1319:8a2e:370:7348'
self.resolve_address.return_value = ipv6_addr
self.assertEquals(ceph_hooks.canonical_url({}),
'http://[%s]' % ipv6_addr)
@patch.object(ceph_hooks, 'CONFIGS')
def test_cluster_changed(self, configs):
_id_joined = self.patch('identity_joined')
self.relation_ids.return_value = ['rid']
ceph_hooks.cluster_changed()
configs.write_all.assert_called()
_id_joined.assert_called_with(relid='rid')
def test_ha_relation_joined_no_vip(self):
self.test_config.set('vip', '')
ceph_hooks.ha_relation_joined()
self.sys.exit.assert_called_with(1)
def test_ha_relation_joined_vip(self):
self.test_config.set('ha-bindiface', 'eth8')
self.test_config.set('ha-mcastport', '5000')
self.test_config.set('vip', '10.0.0.10')
self.get_iface_for_address.return_value = 'eth7'
self.get_netmask_for_address.return_value = '255.255.0.0'
ceph_hooks.ha_relation_joined()
eth_params = ('params ip="10.0.0.10" cidr_netmask="255.255.0.0" '
'nic="eth7"')
resources = {'res_cephrg_haproxy': 'lsb:haproxy',
'res_cephrg_eth7_vip': 'ocf:heartbeat:IPaddr2'}
resource_params = {'res_cephrg_haproxy': 'op monitor interval="5s"',
'res_cephrg_eth7_vip': eth_params}
self.relation_set.assert_called_with(
init_services={'res_cephrg_haproxy': 'haproxy'},
corosync_bindiface='eth8',
corosync_mcastport='5000',
resource_params=resource_params,
resources=resources,
clones={'cl_cephrg_haproxy': 'res_cephrg_haproxy'})
def test_ha_relation_changed(self):
_id_joined = self.patch('identity_joined')
self.relation_get.return_value = True
self.relation_ids.return_value = ['rid']
ceph_hooks.ha_relation_changed()
_id_joined.assert_called_with(relid='rid')