diff --git a/Makefile b/Makefile index 3e52327..53ede91 100644 --- a/Makefile +++ b/Makefile @@ -1,14 +1,18 @@ #!/usr/bin/make PYTHON := /usr/bin/env python -lint: - @flake8 --exclude hooks/charmhelpers hooks - @flake8 --exclude hooks/charmhelpers unit_tests +virtualenv: + virtualenv .venv + .venv/bin/pip install flake8 nose coverage mock pyyaml netifaces \ + netaddr jinja2 + +lint: virtualenv + .venv/bin/flake8 --exclude hooks/charmhelpers hooks unit_tests tests @charm proof -unit_test: +unit_test: virtualenv @echo Starting tests... - @$(PYTHON) /usr/bin/nosetests --nologcapture unit_tests + @.venv/bin/nosetests --nologcapture --with-coverage unit_tests bin/charm_helpers_sync.py: @mkdir -p bin diff --git a/README.ex b/README.md similarity index 97% rename from README.ex rename to README.md index a0ebbe1..7be9fa1 100644 --- a/README.ex +++ b/README.md @@ -4,7 +4,7 @@ Once deployed this charm performs the configurations required for a PLUMgrid Dir # Usage -Step by step instructions on using the charm: +Instructions on using the charm: juju deploy neutron-api juju deploy neutron-api-plumgrid diff --git a/hooks/pg_dir_context.py b/hooks/pg_dir_context.py index 24e4d6d..11a1900 100644 --- a/hooks/pg_dir_context.py +++ b/hooks/pg_dir_context.py @@ -1,6 +1,7 @@ # Copyright (c) 2015, PLUMgrid Inc, http://plumgrid.com -# This file contains the class that generates context for PLUMgrid template files. +# This file contains the class that generates context +# for PLUMgrid template files. from charmhelpers.core.hookenv import ( config, @@ -21,7 +22,8 @@ from socket import gethostname as get_unit_hostname def _pg_dir_ips(): ''' - Inspects plumgrid-director peer relation and returns the ips of the peer directors + Inspects plumgrid-director peer relation and returns the + ips of the peer directors ''' pg_dir_ips = [] for rid in relation_ids('director'): @@ -58,7 +60,8 @@ class PGDirContext(context.NeutronContext): def pg_ctxt(self): ''' - Generated Config for all PLUMgrid templates inside the templates folder. + Generated Config for all PLUMgrid templates inside the templates + folder. ''' pg_ctxt = super(PGDirContext, self).pg_ctxt() if not pg_ctxt: @@ -66,7 +69,8 @@ class PGDirContext(context.NeutronContext): conf = config() pg_dir_ips = _pg_dir_ips() - pg_dir_ips.append(str(get_address_in_network(network=None, fallback=get_host_ip(unit_get('private-address'))))) + pg_dir_ips.append(str(get_address_in_network(network=None, + fallback=get_host_ip(unit_get('private-address'))))) pg_ctxt['director_ips'] = pg_dir_ips pg_dir_ips_string = '' single_ip = True diff --git a/hooks/pg_dir_hooks.py b/hooks/pg_dir_hooks.py index aaab4f5..a0c2fc4 100755 --- a/hooks/pg_dir_hooks.py +++ b/hooks/pg_dir_hooks.py @@ -6,11 +6,12 @@ # in this file. import sys - +import time from charmhelpers.core.hookenv import ( Hooks, UnregisteredHookError, log, + config, ) from charmhelpers.fetch import ( @@ -46,7 +47,6 @@ def install(): apt_install(pkg, options=['--force-yes'], fatal=True) load_iovisor() ensure_mtu() - post_pg_license() add_lcm_key() @@ -83,6 +83,20 @@ def config_changed(): restart_pg() +@hooks.hook('start') +def start(): + ''' + This hook is run when the charm is started. + ''' + if config('plumgrid-license-key') is not None: + count = 0 + while (count < 10): + if post_pg_license(): + break + count = count + 1 + time.sleep(15) + + @hooks.hook('stop') def stop(): ''' diff --git a/hooks/pg_dir_utils.py b/hooks/pg_dir_utils.py index 5d218fc..cda7dea 100644 --- a/hooks/pg_dir_utils.py +++ b/hooks/pg_dir_utils.py @@ -137,7 +137,8 @@ def remove_iovisor(): ''' Removes iovisor kernel module. ''' - _exec_cmd(cmd=['rmmod', 'iovisor'], error_msg='Error Loading IOVisor Kernel Module') + _exec_cmd(cmd=['rmmod', 'iovisor'], + error_msg='Error Loading IOVisor Kernel Module') def check_interface_type(): @@ -232,16 +233,24 @@ def post_pg_license(): LICENSE_GET_PATH = 'https://%s/0/tenant_manager/licenses' % PG_VIP PG_CURL = '%s/opt/pg/scripts/pg_curl.sh' % PG_LXC_PATH license = {"key1": {"license": key}} - licence_post_cmd = [PG_CURL, '-u', 'plumgrid:plumgrid', LICENSE_POST_PATH, '-d', json.dumps(license)] + licence_post_cmd = [ + PG_CURL, + '-u', + 'plumgrid:plumgrid', + LICENSE_POST_PATH, + '-d', + json.dumps(license) + ] licence_get_cmd = [PG_CURL, '-u', 'plumgrid:plumgrid', LICENSE_GET_PATH] try: old_license = subprocess.check_output(licence_get_cmd) except subprocess.CalledProcessError: - log('Virtual IP Changed') + log('No response from specified virtual IP') return 0 - _exec_cmd(cmd=licence_post_cmd, error_msg='Unable to post License', fatal=False) + _exec_cmd(cmd=licence_post_cmd, + error_msg='Unable to post License', fatal=False) new_license = subprocess.check_output(licence_get_cmd) if old_license == new_license: - log('PLUMgrid License already posted') + log('No change in PLUMgrid License') return 0 return 1 diff --git a/hooks/start b/hooks/start new file mode 120000 index 0000000..6530c5a --- /dev/null +++ b/hooks/start @@ -0,0 +1 @@ +pg_dir_hooks.py \ No newline at end of file diff --git a/tests/14-juno b/tests/14-kilo similarity index 96% rename from tests/14-juno rename to tests/14-kilo index c48d1c6..10a05e1 100755 --- a/tests/14-juno +++ b/tests/14-kilo @@ -10,7 +10,7 @@ class TestDeployment(unittest.TestCase): def setUpClass(cls): cls.deployment = amulet.Deployment(series='trusty') cls.deployment.load_bundle_file( - bundle_file='files/plumgrid-director.yaml', + bundle_file='tests/files/plumgrid-director.yaml', deployment_name='test') try: cls.deployment.setup(timeout=2000) diff --git a/unit_tests/test_pg_dir_context.py b/unit_tests/test_pg_dir_context.py index 8da7ba5..3a716eb 100644 --- a/unit_tests/test_pg_dir_context.py +++ b/unit_tests/test_pg_dir_context.py @@ -46,9 +46,11 @@ class PGDirContextTest(CharmTestCase): @patch.object(charmhelpers.contrib.openstack.context, 'unit_private_ip') @patch.object(context, '_pg_dir_ips') @patch.object(utils, 'check_interface_type') - def test_neutroncc_context_api_rel(self, _int_type, _pg_dir_ips, _unit_priv_ip, _npa, _ens_pkgs, - _save_ff, _https, _is_clus, _unit_get, - _config, _runits, _rids, _rget): + def test_neutroncc_context_api_rel(self, _int_type, _pg_dir_ips, + _unit_priv_ip, _npa, _ens_pkgs, + _save_ff, _https, _is_clus, + _unit_get, _config, _runits, _rids, + _rget): def mock_npa(plugin, section, manager): if section == "driver": return "neutron.randomdriver" @@ -87,7 +89,9 @@ class PGDirContextTest(CharmTestCase): 'label': 'node0', 'fabric_mode': 'host', 'virtual_router_id': '250', - 'director_ips': ['192.168.100.202', '192.168.100.203', '192.168.100.201'], - 'director_ips_string': '192.168.100.202,192.168.100.203,192.168.100.201', + 'director_ips': ['192.168.100.202', '192.168.100.203', + '192.168.100.201'], + 'director_ips_string': + '192.168.100.202,192.168.100.203,192.168.100.201', } self.assertEquals(expect, napi_ctxt()) diff --git a/unit_tests/test_pg_dir_hooks.py b/unit_tests/test_pg_dir_hooks.py index 2f700e3..2fa95a4 100644 --- a/unit_tests/test_pg_dir_hooks.py +++ b/unit_tests/test_pg_dir_hooks.py @@ -28,7 +28,8 @@ TO_PATCH = [ 'ensure_mtu', 'add_lcm_key', 'determine_packages', - 'post_pg_license' + 'post_pg_license', + 'config' ] NEUTRON_CONF_DIR = "/etc/neutron" @@ -39,6 +40,7 @@ class PGDirHooksTests(CharmTestCase): def setUp(self): super(PGDirHooksTests, self).setUp(hooks, TO_PATCH) + self.config.side_effect = self.test_config.get hooks.hooks._config_save = False def _call_hook(self, hookname): @@ -56,7 +58,6 @@ class PGDirHooksTests(CharmTestCase): ]) self.load_iovisor.assert_called_with() self.ensure_mtu.assert_called_with() - self.post_pg_license.assert_called_with() self.add_lcm_key.assert_called_with() def test_config_changed_hook(self): @@ -77,6 +78,10 @@ class PGDirHooksTests(CharmTestCase): self.CONFIGS.write_all.assert_called_with() self.restart_pg.assert_called_with() + def test_start(self): + self._call_hook('start') + self.test_config.set('plumgrid-license-key', None) + def test_stop(self): _pkgs = ['plumgrid-lxc', 'iovisor-dkms'] self._call_hook('stop') diff --git a/unit_tests/test_pg_dir_utils.py b/unit_tests/test_pg_dir_utils.py index 363f6a8..d4fe0b1 100644 --- a/unit_tests/test_pg_dir_utils.py +++ b/unit_tests/test_pg_dir_utils.py @@ -31,7 +31,7 @@ class TestPGDirUtils(CharmTestCase): def setUp(self): super(TestPGDirUtils, self).setUp(nutils, TO_PATCH) - #self.config.side_effect = self.test_config.get + # self.config.side_effect = self.test_config.get def tearDown(self): # Reset cached cache @@ -50,12 +50,12 @@ class TestPGDirUtils(CharmTestCase): self.os_release.return_value = 'trusty' templating.OSConfigRenderer.side_effect = _mock_OSConfigRenderer _regconfs = nutils.register_configs() - confs = ['/var/lib/libvirt/filesystems/plumgrid-data/conf/etc/keepalived.conf', - '/var/lib/libvirt/filesystems/plumgrid-data/conf/pg/plumgrid.conf', - '/var/lib/libvirt/filesystems/plumgrid-data/conf/pg/nginx.conf', - '/var/lib/libvirt/filesystems/plumgrid-data/conf/etc/hostname', - '/var/lib/libvirt/filesystems/plumgrid-data/conf/etc/hosts', - '/var/lib/libvirt/filesystems/plumgrid-data/conf/pg/ifcs.conf'] + confs = [nutils.PG_KA_CONF, + nutils.PG_CONF, + nutils.PG_DEF_CONF, + nutils.PG_HN_CONF, + nutils.PG_HS_CONF, + nutils.PG_IFCS_CONF] self.assertItemsEqual(_regconfs.configs, confs) def test_resource_map(self): @@ -67,10 +67,9 @@ class TestPGDirUtils(CharmTestCase): def test_restart_map(self): _restart_map = nutils.restart_map() - PG_KA_CONF = '/var/lib/libvirt/filesystems/plumgrid-data/conf/etc/keepalived.conf' expect = OrderedDict([ (nutils.PG_CONF, ['plumgrid']), - (PG_KA_CONF, ['plumgrid']), + (nutils.PG_KA_CONF, ['plumgrid']), (nutils.PG_DEF_CONF, ['plumgrid']), (nutils.PG_HN_CONF, ['plumgrid']), (nutils.PG_HS_CONF, ['plumgrid']),