From 1890a97952e1c44ddf4f2257d1f343068b1db0b8 Mon Sep 17 00:00:00 2001 From: Alex Kavanagh Date: Tue, 30 Jul 2019 15:17:58 +0100 Subject: [PATCH] Modify paths to ensure that charms' charmhelpers is first The pre-patch version of unit-testing tested the git-repo version of charm-helpers instead of the version that was synced to the charm. The git-repo version of charmhelpers is pip installed to obtain the testing part of charm-helpers, for the unit tests. Whilst this is a bug (really), it was compounded in that - due to how the paths worked - the unit tests' git version of charm-helpers was in the path first, and thus all unit tests were performed using that version of charm-helpers, and not the version actually synced into the charm. This has led to all sorts of 'odd' double mocking of config in the tests as there are potentially two copies of charm-helpers (as was the case for py27). A further commit will undo that madness. This patchset moves the charm synced version of charm-helpers earlier into the path so it gets picked first when the modules are loaded. Change-Id: Iff0566c52a283ff0cd2fc7707d981d8c47f3e6b2 --- actions/security_checklist.py | 14 +++++++++++++- unit_tests/__init__.py | 19 ++++++++++++++++--- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/actions/security_checklist.py b/actions/security_checklist.py index f8b4dad4..41f6cb3b 100755 --- a/actions/security_checklist.py +++ b/actions/security_checklist.py @@ -15,9 +15,20 @@ # limitations under the License. import configparser +import os import sys -sys.path.append('hooks') +_path = os.path.dirname(os.path.realpath(__file__)) +_hooks = os.path.abspath(os.path.join(_path, '../hooks')) + + +def _add_path(path): + if path not in sys.path: + sys.path.insert(1, path) + + +_add_path(_hooks) + import charmhelpers.contrib.openstack.audits as audits from charmhelpers.contrib.openstack.audits import ( @@ -47,5 +58,6 @@ def main(): config['neutron_config'] = dict(conf) return audits.action_parse_results(audits.run(config)) + if __name__ == "__main__": sys.exit(main()) diff --git a/unit_tests/__init__.py b/unit_tests/__init__.py index c9c8fd1a..973e4e6d 100644 --- a/unit_tests/__init__.py +++ b/unit_tests/__init__.py @@ -1,5 +1,18 @@ +import os import sys -sys.path.append('unit_tests') -sys.path.append('actions') -sys.path.append('hooks') + +_path = os.path.dirname(os.path.realpath(__file__)) +_hooks = os.path.abspath(os.path.join(_path, '../hooks')) +_actions = os.path.abspath(os.path.join(_path, '../actions')) +_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(_hooks) +_add_path(_actions) +_add_path(_unit_tests)