From 6aa32f2d67496dd4337c90c94613617ebae3f54a Mon Sep 17 00:00:00 2001 From: Alex Kavanagh Date: Mon, 4 Feb 2019 19:18:02 +0000 Subject: [PATCH] Migrate charm to Python3 Change-Id: Ibb46dfa766b12aedea90f823d3964c569bdfcb01 --- .zuul.yaml | 3 +-- hooks/charmhelpers | 1 - hooks/hooks.py | 16 ++++++++++++++-- hooks/install | 2 +- tox.ini | 6 +++--- unit_tests/__init__.py | 18 ++++++++++++++++++ unit_tests/test_hooks.py | 22 ++++++++++------------ unit_tests/test_utils.py | 2 +- 8 files changed, 48 insertions(+), 22 deletions(-) delete mode 120000 hooks/charmhelpers diff --git a/.zuul.yaml b/.zuul.yaml index aa9c508..7051aee 100644 --- a/.zuul.yaml +++ b/.zuul.yaml @@ -1,4 +1,3 @@ - project: templates: - - python-charm-jobs - - openstack-python35-jobs-nonvoting + - python35-charm-jobs diff --git a/hooks/charmhelpers b/hooks/charmhelpers deleted file mode 120000 index 702de73..0000000 --- a/hooks/charmhelpers +++ /dev/null @@ -1 +0,0 @@ -../charmhelpers \ No newline at end of file diff --git a/hooks/hooks.py b/hooks/hooks.py index 13efe21..8d9455f 100755 --- a/hooks/hooks.py +++ b/hooks/hooks.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # # Copyright 2018 Canonical Ltd # @@ -16,8 +16,20 @@ import glob import os -import sys import shutil +import sys + + +_path = os.path.dirname(os.path.realpath(__file__)) +_root = os.path.abspath(os.path.join(_path, '..')) + + +def _add_path(path): + if path not in sys.path: + sys.path.insert(1, path) + + +_add_path(_root) from charmhelpers.fetch import add_source, apt_install, apt_update from charmhelpers.core import hookenv diff --git a/hooks/install b/hooks/install index 83a9d3c..23658c9 100755 --- a/hooks/install +++ b/hooks/install @@ -11,7 +11,7 @@ check_and_install() { fi } -PYTHON="python" +PYTHON="python3" for dep in ${DEPS[@]}; do check_and_install ${PYTHON} ${dep} diff --git a/tox.ini b/tox.ini index 3ba8b17..59d06dc 100644 --- a/tox.ini +++ b/tox.ini @@ -2,7 +2,7 @@ # This file is managed centrally by release-tools and should not be modified # within individual charm repos. [tox] -envlist = pep8,py27 +envlist = pep8,py3{5,6} skipsdist = True [testenv] @@ -20,13 +20,13 @@ passenv = HOME TERM AMULET_* CS_API_* basepython = python2.7 deps = -r{toxinidir}/requirements.txt -r{toxinidir}/test-requirements.txt +# charm is NOT PY27 compatible +commands = /bin/true [testenv:py35] basepython = python3.5 deps = -r{toxinidir}/requirements.txt -r{toxinidir}/test-requirements.txt -# charm is NOT Py3 compatible -commands = /bin/true [testenv:py36] basepython = python3.6 diff --git a/unit_tests/__init__.py b/unit_tests/__init__.py index e69de29..547d85f 100644 --- a/unit_tests/__init__.py +++ b/unit_tests/__init__.py @@ -0,0 +1,18 @@ +import os +import sys + +_path = os.path.dirname(os.path.realpath(__file__)) +_actions = os.path.abspath(os.path.join(_path, '../actions')) +_hooks = os.path.abspath(os.path.join(_path, '../hooks')) +_charmhelpers = os.path.abspath(os.path.join(_path, '../charmhelpers')) +_unit_tests = os.path.abspath(os.path.join(_path, '../unit_tests')) + + +def _add_path(path): + if path not in sys.path: + sys.path.insert(1, path) + +_add_path(_actions) +_add_path(_hooks) +_add_path(_charmhelpers) +_add_path(_unit_tests) diff --git a/unit_tests/test_hooks.py b/unit_tests/test_hooks.py index d0586ff..240f46c 100644 --- a/unit_tests/test_hooks.py +++ b/unit_tests/test_hooks.py @@ -43,12 +43,11 @@ class TestConfigChanged(CharmTestCase): @mock.patch.object(hooks, 'update_nrpe_config') @mock.patch('os.symlink') - @mock.patch('hooks.charmhelpers.core.hookenv.config') - @mock.patch('hooks.charmhelpers.core.hookenv.relations_of_type') - @mock.patch('hooks.charmhelpers.contrib.charmsupport.nrpe' - '.get_nagios_hostname') - @mock.patch('hooks.charmhelpers.contrib.charmsupport.nrpe.config') - @mock.patch('hooks.charmhelpers.contrib.charmsupport.nrpe.local_unit') + @mock.patch('charmhelpers.core.hookenv.config') + @mock.patch('charmhelpers.core.hookenv.relations_of_type') + @mock.patch('charmhelpers.contrib.charmsupport.nrpe.get_nagios_hostname') + @mock.patch('charmhelpers.contrib.charmsupport.nrpe.config') + @mock.patch('charmhelpers.contrib.charmsupport.nrpe.local_unit') def test_default_config(self, local_unit, nrpe_config, nag_host, relations_of_type, config, symlink, update_nrpe_config): @@ -82,12 +81,11 @@ class TestConfigChanged(CharmTestCase): @mock.patch('os.path.exists') @mock.patch('os.remove') @mock.patch('glob.glob') - @mock.patch('hooks.charmhelpers.core.hookenv.config') - @mock.patch('hooks.charmhelpers.core.hookenv.relations_of_type') - @mock.patch('hooks.charmhelpers.contrib.charmsupport.nrpe' - '.get_nagios_hostname') - @mock.patch('hooks.charmhelpers.contrib.charmsupport.nrpe.config') - @mock.patch('hooks.charmhelpers.contrib.charmsupport.nrpe.local_unit') + @mock.patch('charmhelpers.core.hookenv.config') + @mock.patch('charmhelpers.core.hookenv.relations_of_type') + @mock.patch('charmhelpers.contrib.charmsupport.nrpe.get_nagios_hostname') + @mock.patch('charmhelpers.contrib.charmsupport.nrpe.config') + @mock.patch('charmhelpers.contrib.charmsupport.nrpe.local_unit') def test_uninstall_cron(self, local_unit, nrpe_config, nag_host, relations_of_type, config, glob, remove, exists, update_nrpe_config): diff --git a/unit_tests/test_utils.py b/unit_tests/test_utils.py index eeefc60..9c21f04 100644 --- a/unit_tests/test_utils.py +++ b/unit_tests/test_utils.py @@ -36,7 +36,7 @@ def get_default_config(): ''' default_config = {} config = load_config() - for k, v in config.iteritems(): + for k, v in config.items(): if 'default' in v: default_config[k] = v['default'] else: