Merge ~pjdc/ubuntu-mirror-charm/+git/ubuntu-mirror-charm:testability into ubuntu-mirror-charm:master

Proposed by Paul Collins
Status: Merged
Approved by: Haw Loeung
Approved revision: 3b3c8f072780da5e36d949e8678cda1f969c9a9b
Merged at revision: cc09bd3e25a29290590b254758b70a4c1fec33bd
Proposed branch: ~pjdc/ubuntu-mirror-charm/+git/ubuntu-mirror-charm:testability
Merge into: ubuntu-mirror-charm:master
Diff against target: 198 lines (+88/-11)
5 files modified
.gitignore (+5/-0)
Makefile (+14/-0)
hooks/Config.py (+3/-3)
hooks/hooks.py (+19/-8)
tox.ini (+47/-0)
Reviewer Review Type Date Requested Status
Haw Loeung +1 Approve
Canonical IS Reviewers Pending
Review via email: mp+388433@code.launchpad.net

Commit message

add lint and unittest, fix lint errors

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
Haw Loeung (hloeung) wrote :

LGTM

review: Approve (+1)
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

Change successfully merged at revision cc09bd3e25a29290590b254758b70a4c1fec33bd

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/.gitignore b/.gitignore
index b25c15b..490cc43 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,6 @@
1*~1*~
2*.charm
3.tox
4.coverage
5__pycache__
6build
diff --git a/Makefile b/Makefile
2new file mode 1006447new file mode 100644
index 0000000..4f10863
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,14 @@
1lint:
2 @echo "Running flake8"
3 @tox -e lint
4
5unittest:
6 @tox -e unit
7
8test: lint unittest
9
10clean:
11 @echo "Cleaning files"
12 @git clean -fXd
13
14.PHONY: lint test unittest clean
diff --git a/hooks/Config.py b/hooks/Config.py
index d6e8ba3..a729ba6 100755
--- a/hooks/Config.py
+++ b/hooks/Config.py
@@ -59,7 +59,7 @@ class Config:
59 def archive_logs_for(self, service):59 def archive_logs_for(self, service):
60 try:60 try:
61 ret = config("archive_logs_" + service)61 ret = config("archive_logs_" + service)
62 except:62 except Exception:
63 ret = False63 ret = False
64 return ret64 return ret
6565
@@ -104,11 +104,11 @@ class Config:
104 def logdir(self, service):104 def logdir(self, service):
105 try:105 try:
106 logdir = str(config(service + "_logdir"))106 logdir = str(config(service + "_logdir"))
107 except:107 except Exception:
108 logdir = None108 logdir = None
109 return logdir109 return logdir
110110
111 def mirror_map(self, role):111 def mirror_map(self, role): # noqa: C901
112 if role in self._mirror_map:112 if role in self._mirror_map:
113 return self._mirror_map[role]113 return self._mirror_map[role]
114114
diff --git a/hooks/hooks.py b/hooks/hooks.py
index 7dfa42a..fb28d13 100755
--- a/hooks/hooks.py
+++ b/hooks/hooks.py
@@ -28,7 +28,17 @@ from charmhelpers.core.host import (
28 user_exists,28 user_exists,
29 write_file,29 write_file,
30)30)
31from charmhelpers.core.hookenv import Hooks, log, charm_dir, relation_set, local_unit, is_relation_made, relation_id, relation_ids, status_set31from charmhelpers.core.hookenv import (
32 Hooks,
33 charm_dir,
34 is_relation_made,
35 local_unit,
36 log,
37 relation_id,
38 relation_ids,
39 relation_set,
40 status_set,
41)
32from charmhelpers.core import sysctl42from charmhelpers.core import sysctl
33from charmhelpers.fetch import apt_install43from charmhelpers.fetch import apt_install
34from charmhelpers.payload.execd import execd_run44from charmhelpers.payload.execd import execd_run
@@ -42,7 +52,7 @@ execd_dir = os.path.join(charm_dir(), 'exec.d')
42conf = Config()52conf = Config()
4353
44apache_tls_settings = {54apache_tls_settings = {
45 'ssl_cipher_suite': 'EECDH+AESGCM+AES128:EDH+AESGCM+AES128:EECDH+AES128:EDH+AES128:ECDH+AESGCM+AES128:aRSA+AESGCM+AES128:ECDH+AES128:DH+AES128:aRSA+AES128:EECDH+AESGCM:EDH+AESGCM:EECDH:EDH:ECDH+AESGCM:aRSA+AESGCM:ECDH:DH:aRSA:HIGH:!MEDIUM:!aNULL:!NULL:!LOW:!3DES:!DSS:!EXP:!PSK:!SRP',55 'ssl_cipher_suite': 'EECDH+AESGCM+AES128:EDH+AESGCM+AES128:EECDH+AES128:EDH+AES128:ECDH+AESGCM+AES128:aRSA+AESGCM+AES128:ECDH+AES128:DH+AES128:aRSA+AES128:EECDH+AESGCM:EDH+AESGCM:EECDH:EDH:ECDH+AESGCM:aRSA+AESGCM:ECDH:DH:aRSA:HIGH:!MEDIUM:!aNULL:!NULL:!LOW:!3DES:!DSS:!EXP:!PSK:!SRP', # noqa: E501
46 'ssl_honor_cipher_order': 'on',56 'ssl_honor_cipher_order': 'on',
47 # per IS policy, TLS 1.2 is the minimum acceptable version57 # per IS policy, TLS 1.2 is the minimum acceptable version
48 'ssl_protocol': 'ALL -SSLv2 -SSLv3 -TLSv1 -TLSv1.1',58 'ssl_protocol': 'ALL -SSLv2 -SSLv3 -TLSv1 -TLSv1.1',
@@ -185,7 +195,7 @@ def get_ssh_keyfile(username):
185# Creation of any necessary destination directories is the responsibility195# Creation of any necessary destination directories is the responsibility
186# of the sync script specified in ${mirror}_command196# of the sync script specified in ${mirror}_command
187#197#
188def configure_rsync_client():198def configure_rsync_client(): # noqa: C901
189 log("CHARM: Configuring rsync client")199 log("CHARM: Configuring rsync client")
190 mirror_userinfo = conf.mirror_userinfo()200 mirror_userinfo = conf.mirror_userinfo()
191 mirror_confdir = os.path.join(mirror_userinfo.pw_dir, "mirror-config")201 mirror_confdir = os.path.join(mirror_userinfo.pw_dir, "mirror-config")
@@ -315,7 +325,7 @@ def get_role_config(roles, hostname, role):
315#325#
316# Set up apache config files - adding or removing vhosts as needed326# Set up apache config files - adding or removing vhosts as needed
317#327#
318def configure_apache():328def configure_apache(): # noqa: C901
319 log("CHARM: Configuring apache")329 log("CHARM: Configuring apache")
320 ensure_package_status(service_affecting_packages, conf.package_status())330 ensure_package_status(service_affecting_packages, conf.package_status())
321 apache_dir = "/etc/apache2"331 apache_dir = "/etc/apache2"
@@ -452,7 +462,7 @@ def configure_apache():
452# Set up the mpm config462# Set up the mpm config
453# 2.4 defaults to event463# 2.4 defaults to event
454# Returns True is something changed, False otherwise464# Returns True is something changed, False otherwise
455def configure_apache_mpm():465def configure_apache_mpm(): # noqa: C901
456 log("CHARM: Configuring mpm")466 log("CHARM: Configuring mpm")
457 restart_required = False467 restart_required = False
458 apache_version = get_pkg_version("apache2")[0:3]468 apache_version = get_pkg_version("apache2")[0:3]
@@ -592,7 +602,7 @@ def configure_triggers():
592 write_file(keyfile, contents)602 write_file(keyfile, contents)
593603
594604
595def configure_nrpe():605def configure_nrpe(): # noqa: C901
596 if not conf.nagios_relation_ok():606 if not conf.nagios_relation_ok():
597 log("CHARM: Nagios relation not established - not configuring nrpe")607 log("CHARM: Nagios relation not established - not configuring nrpe")
598 return608 return
@@ -684,7 +694,8 @@ def configure_nrpe():
684694
685 # Clear out any old role checks695 # Clear out any old role checks
686 # Making (fairly) sure we only touch files we manage696 # Making (fairly) sure we only touch files we manage
687 valid_beginnings = role_checks.keys() + ["service__{}_".format(conf.nagios_hostname()) + s for s in role_checks.keys()]697 valid_beginnings = role_checks.keys() + ["service__{}_".format(conf.nagios_hostname()) + s
698 for s in role_checks.keys()]
688 for d in [nagios_etcdir, nagios_exportdir]:699 for d in [nagios_etcdir, nagios_exportdir]:
689 enabled_checks = []700 enabled_checks = []
690 for f in os.listdir(d):701 for f in os.listdir(d):
@@ -727,7 +738,7 @@ def configure_nrpe():
727738
728 # RT#79518: Add prompt-critical check for all disks; taken from puppet.739 # RT#79518: Add prompt-critical check for all disks; taken from puppet.
729 check_all_disks = juju_header()740 check_all_disks = juju_header()
730 check_all_disks += "command[check_all_disks_promptcrit]=/usr/lib/nagios/plugins/check_disk -L -w 2% -c 1% -K 4% -A -i '^/run/lxcfs' -i '^/sys' -i '^/snap'\n"741 check_all_disks += "command[check_all_disks_promptcrit]=/usr/lib/nagios/plugins/check_disk -L -w 2% -c 1% -K 4% -A -i '^/run/lxcfs' -i '^/sys' -i '^/snap'\n" # noqa: E501
731 write_file(os.path.join(nagios_etcdir, 'check_all_disks_promptcrit.cfg'),742 write_file(os.path.join(nagios_etcdir, 'check_all_disks_promptcrit.cfg'),
732 check_all_disks)743 check_all_disks)
733744
diff --git a/tox.ini b/tox.ini
734new file mode 100644745new file mode 100644
index 0000000..7898f03
--- /dev/null
+++ b/tox.ini
@@ -0,0 +1,47 @@
1[tox]
2skipsdist=True
3envlist = unit, functional
4skip_missing_interpreters = True
5
6[testenv]
7basepython = python2
8setenv =
9 PYTHONPATH = {toxinidir}/hooks
10
11[testenv:unit]
12commands =
13 pytest --ignore mod --ignore {toxinidir}/tests/functional \
14 {posargs:-v --cov=src --cov-report=term-missing --cov-branch}
15deps = -r{toxinidir}/tests/unit/requirements.txt
16 -r{toxinidir}/requirements.txt
17setenv =
18 PYTHONPATH={toxinidir}/src:{toxinidir}/build/lib:{toxinidir}/build/venv
19 TZ=UTC
20
21[testenv:functional]
22passenv =
23 HOME
24 JUJU_REPOSITORY
25 PATH
26commands =
27 pytest -v --ignore mod --ignore {toxinidir}/tests/unit {posargs}
28deps = -r{toxinidir}/tests/functional/requirements.txt
29 -r{toxinidir}/requirements.txt
30
31[testenv:black]
32commands = black --skip-string-normalization --line-length=120 src/ tests/
33deps = black
34
35[testenv:lint]
36commands = flake8 --exclude hooks/charmhelpers hooks/ tests/
37# Pin flake8 to 3.7.9 to match focal
38deps =
39 flake8==3.7.9
40
41[flake8]
42exclude =
43 .git,
44 __pycache__,
45 .tox,
46max-line-length = 120
47max-complexity = 10

Subscribers

People subscribed via source and target branches