diff options
-rw-r--r-- | Makefile | 24 | ||||
-rwxr-xr-x | hooks/hooks.py | 29 | ||||
-rw-r--r-- | tox.ini | 21 | ||||
-rw-r--r-- | unit_tests/test_utils.py | 2 | ||||
-rw-r--r-- | unit_tests/test_write_log_rotate_config.py | 2 |
5 files changed, 46 insertions, 32 deletions
@@ -15,6 +15,8 @@ PYTHON := /usr/bin/env python3 ACTIONS := $(shell grep -slE '\#!(.*)python3' actions/*) +PROJECTPATH := $(realpath $(dir $(realpath $(firstword $(MAKEFILE_LIST))))) + clean: rm -f .coverage @@ -23,14 +25,9 @@ clean: rm -rf .tox (which dh_clean && dh_clean) || true -.venv: - sudo apt-get install -y gcc python3-dev python-virtualenv python3-apt - virtualenv .venv --python=python3 --system-site-packages - .venv/bin/pip3 install -I -r test_requirements.txt - -lint: .venv - .venv/bin/flake8 --exclude hooks/charmhelpers actions $(ACTIONS) hooks tests unit_tests - .venv/bin/charm-proof +lint: + @echo Starting flake8... + @tox -e lint unit: @echo Starting unit tests... @@ -38,17 +35,18 @@ unit: functional: @echo Starting functional tests... - rm -rf .venv # rm the python2 venv from unittests as it fails the juju deploy - @tox -e functional + @@CHARM_BUILD_DIR="$(PROJECTPATH)" tox -e functional + +test: lint unit functional sync: @mkdir -p bin @curl -o bin/charm_helpers_sync.py https://raw.githubusercontent.com/juju/charm-helpers/master/tools/charm_helpers_sync/charm_helpers_sync.py @$(PYTHON) bin/charm_helpers_sync.py -c charm-helpers-sync.yaml -publish: lint unit_test - bzr push lp:charms/mongodb - bzr push lp:charms/trusty/mongodb +# publish: lint unit +# bzr push lp:charms/mongodb +# bzr push lp:charms/trusty/mongodb # The targets below don't depend on a file .PHONY: lint test unittest functional publish sync diff --git a/hooks/hooks.py b/hooks/hooks.py index c5729ad..cc8fd85 100755 --- a/hooks/hooks.py +++ b/hooks/hooks.py @@ -269,7 +269,7 @@ class TimeoutException(Exception): ############################################################################### # Charm support functions ############################################################################### -def mongodb_conf(config_data=None): +def mongodb_conf(config_data=None): # noqa: C901 is too complex (28) if config_data is None: return(None) config = [] @@ -716,17 +716,16 @@ def configsvr_status(wait_for=default_wait_for, max_tries=default_max_tries): current_try = 0 while (process_check_pidfile('/var/run/mongodb/configsvr.pid') != ( - None, None)) and not port_check( - unit_get('private-address'), - config_data['config_server_port']) and current_try < max_tries: + None, None)) and not port_check( + unit_get('private-address'), + config_data['config_server_port']) and current_try < max_tries: juju_log("configsvr_status: Waiting for Config Server to be ready ...") time.sleep(wait_for) current_try += 1 - retVal = ( - process_check_pidfile('/var/run/mongodb/configsvr.pid') != (None, None) - ) == port_check(unit_get('private-address'), - config_data['config_server_port']) is True + retVal = (process_check_pidfile('/var/run/mongodb/configsvr.pid') != (None, None) + ) == port_check(unit_get('private-address'), + config_data['config_server_port']) is True if retVal: return(process_check_pidfile('/var/run/mongodb/configsvr.pid')) else: @@ -773,14 +772,14 @@ def enable_configsvr(config_data, wait_for=default_wait_for, '-p', '%s' % config_data['config_server_dbpath'] ] - ) + ) subprocess.call( [ 'mkdir', '-p', '%s' % os.path.dirname(config_data['config_server_logpath']) ] - ) + ) # Start the config server juju_log("enable_configsvr: Starting the config server") @@ -869,7 +868,7 @@ def enable_mongos(config_data=None, config_servers=None, '-p', '%s' % os.path.dirname(config_data['mongos_logpath']) ] - ) + ) cmd_line = "mongos" cmd_line += " --logpath %s" % config_data['mongos_logpath'] cmd_line += " --pidfilepath /var/run/mongodb/mongos.pid" @@ -1002,7 +1001,7 @@ def install_hook(): @hooks.hook('config-changed') -def config_changed(): +def config_changed(): # noqa: C901 is too complex (17) juju_log("Entering config_changed") status_set('maintenance', 'Configuring unit') config_data = config() @@ -1213,7 +1212,7 @@ def replica_set_relation_joined(): 'replset': my_replset, 'install-order': my_install_order, 'type': 'replset', - }) + }) update_status() juju_log("replica_set_relation_joined-finish") @@ -1569,7 +1568,7 @@ def update_nrpe_config(): shortname='mongodb', description='process check {%s}' % current_unit, check_cmd=check_mongo_script, - ) + ) nrpe.write() @@ -1778,7 +1777,7 @@ def volume_get_all_mounted(): # - if fresh new storage dir: rsync existing data # - manipulate /var/lib/mongodb/VERSION/CLUSTER symlink # -def config_changed_volume_apply(): +def config_changed_volume_apply(): # noqa: C901 is too complex (12) config_data = config() data_directory_path = config_data["dbpath"] assert(data_directory_path) @@ -4,15 +4,22 @@ envlist = unit, functional, lint skip_missing_interpreters = True [testenv] +basepython = python3 setenv = PYTHONPATH = . passenv = HOME - JUJU_REPOSITORY + CHARM_BUILD_DIR MODEL_SETTINGS +[testenv:lint] +deps = -r{toxinidir}/test_requirements.txt +commands = flake8 {posargs:hooks/ unit_tests/ tests/} + charm-proof + + [testenv:unit] -basepython = python2 +# basepython = python2 commands = nosetests -s --nologcapture --with-coverage unit_tests/ actions/ deps = -r{toxinidir}/test_requirements.txt @@ -28,3 +35,13 @@ basepython = python3 commands = functest-run-suite --keep-model --smoke deps = -r{toxinidir}/tests/test_requirements.txt + +[flake8] +ignore = E402,E226 +exclude = + hooks/charmhelpers, + .git, + __pycache__, + .tox, +max-line-length = 120 +max-complexity = 10 diff --git a/unit_tests/test_utils.py b/unit_tests/test_utils.py index 1f2a1aa..2d396cd 100644 --- a/unit_tests/test_utils.py +++ b/unit_tests/test_utils.py @@ -36,7 +36,7 @@ def load_config(): if not config: logging.error('Could not find config.yaml in any parent directory ' - 'of %s. ' % file) + 'of %s. ' % f) raise Exception return yaml.safe_load(open(config).read())['options'] diff --git a/unit_tests/test_write_log_rotate_config.py b/unit_tests/test_write_log_rotate_config.py index f94720f..ef1cd98 100644 --- a/unit_tests/test_write_log_rotate_config.py +++ b/unit_tests/test_write_log_rotate_config.py @@ -19,7 +19,7 @@ class TestWriteLogrotateConfigFile(unittest.TestCase): os.close(fd) with mock.patch('hooks.juju_log') as mock_juju_log: with mock.patch('hooks.open', create=True) as mock_open: - mock_open.return_value = mock.MagicMock(spec=file) + mock_open.return_value = mock.MagicMock() hooks.write_logrotate_config(config_data, temp_fn) os.unlink(temp_fn) mock_juju_log.assert_called_once_with('Writing {}.'.format(temp_fn)) |