Merge ~barryprice/charm-k8s-wordpress/+git/charm-k8s-wordpress:master into charm-k8s-wordpress:master

Proposed by Barry Price
Status: Merged
Approved by: Barry Price
Approved revision: 410a1bbd10ce135917e4518ed6c86f9df763fefa
Merged at revision: 876bb9c7d963f549def98bf9631d797c6da0e69e
Proposed branch: ~barryprice/charm-k8s-wordpress/+git/charm-k8s-wordpress:master
Merge into: charm-k8s-wordpress:master
Diff against target: 192 lines (+138/-3)
6 files modified
Makefile (+19/-2)
reactive/wordpress.py (+1/-1)
requirements.txt (+2/-0)
tests/unit/requirements.txt (+7/-0)
tests/unit/test_wordpress.py (+64/-0)
tox.ini (+45/-0)
Reviewer Review Type Date Requested Status
Stuart Bishop (community) Approve
Canonical IS Reviewers Pending
Review via email: mp+376327@code.launchpad.net

Commit message

Set up our first basic unit test

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
Tom Haddon (mthaddon) wrote :

Let's switch to using tox targets for lint

Revision history for this message
Stuart Bishop (stub) wrote :

Looks good. Makefile lint target needs to call the tox rule you have created, per Tom's comment.

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

Change successfully merged at revision 876bb9c7d963f549def98bf9631d797c6da0e69e

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/Makefile b/Makefile
index 6dc2159..a46c2e2 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,23 @@
1lint:1lint:
2 black -l 120 -t py37 reactive/2 @echo "Normalising python layout with black."
3 flake8 reactive/3 @tox -e black
4 @echo "Running flake8"
5 @tox -e lint
6
7unittest:
8 @tox -e unit
9
10test: lint unittest
411
5build: lint12build: lint
6 charm build13 charm build
14
15clean:
16 @echo "Cleaning files"
17 @rm -rf ./.tox
18 @rm -rf ./.pytest_cache
19 @rm -rf ./tests/unit/__pycache__ ./reactive/__pycache__ ./lib/__pycache__
20 @rm -rf ./.coverage ./.unit-state.db
21
22
23.PHONY: lint test unittest build clean
diff --git a/reactive/wordpress.py b/reactive/wordpress.py
index ededd46..39ea6dc 100644
--- a/reactive/wordpress.py
+++ b/reactive/wordpress.py
@@ -14,6 +14,7 @@ from charms.reactive import hook, when, when_not
1414
15@hook("upgrade-charm")15@hook("upgrade-charm")
16def upgrade_charm():16def upgrade_charm():
17 status.maintenance("maintenance", "Upgrading charm")
17 reactive.clear_flag("wordpress.configured")18 reactive.clear_flag("wordpress.configured")
1819
1920
@@ -136,7 +137,6 @@ def first_install():
136 hookenv.log("No initial_setting provided or wordpress already configured. Skipping first install.")137 hookenv.log("No initial_setting provided or wordpress already configured. Skipping first install.")
137 return True138 return True
138 hookenv.log("Starting wordpress initial configuration")139 hookenv.log("Starting wordpress initial configuration")
139 # TODO: more of the below ought to be configurable
140 payload = {"admin_password": host.pwgen(24), "blog_public": "checked", "Submit": "submit"}140 payload = {"admin_password": host.pwgen(24), "blog_public": "checked", "Submit": "submit"}
141 payload.update(safe_load(config["initial_settings"]))141 payload.update(safe_load(config["initial_settings"]))
142 payload["admin_password2"] = payload["admin_password"]142 payload["admin_password2"] = payload["admin_password"]
diff --git a/requirements.txt b/requirements.txt
143new file mode 100644143new file mode 100644
index 0000000..888e37a
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1,2 @@
1# Include python requirements here
2requests
diff --git a/tests/unit/requirements.txt b/tests/unit/requirements.txt
0new file mode 1006443new file mode 100644
index 0000000..2d27572
--- /dev/null
+++ b/tests/unit/requirements.txt
@@ -0,0 +1,7 @@
1requests
2charmhelpers
3charms.reactive
4freezegun
5mock
6pytest
7pytest-cov
diff --git a/tests/unit/test_wordpress.py b/tests/unit/test_wordpress.py
0new file mode 1006448new file mode 100644
index 0000000..5ade069
--- /dev/null
+++ b/tests/unit/test_wordpress.py
@@ -0,0 +1,64 @@
1import os
2import shutil
3import sys
4import tempfile
5import unittest
6from unittest import mock
7
8# We also need to mock up charms.layer so we can run unit tests without having
9# to build the charm and pull in layers such as layer-status.
10sys.modules['charms.layer'] = mock.MagicMock()
11
12from charms.layer import status # NOQA: E402
13
14# Add path to where our reactive layer lives and import.
15sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__)))))
16from reactive import wordpress # NOQA: E402
17
18
19class TestCharm(unittest.TestCase):
20 def setUp(self):
21 self.maxDiff = None
22 self.tmpdir = tempfile.mkdtemp(prefix='charm-unittests-')
23 self.addCleanup(shutil.rmtree, self.tmpdir)
24
25 self.charm_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
26
27 patcher = mock.patch('charmhelpers.core.hookenv.log')
28 self.mock_log = patcher.start()
29 self.addCleanup(patcher.stop)
30 self.mock_log.return_value = ''
31
32 patcher = mock.patch('charmhelpers.core.hookenv.charm_dir')
33 self.mock_charm_dir = patcher.start()
34 self.addCleanup(patcher.stop)
35 self.mock_charm_dir.return_value = self.charm_dir
36
37 patcher = mock.patch('charmhelpers.core.hookenv.local_unit')
38 self.mock_local_unit = patcher.start()
39 self.addCleanup(patcher.stop)
40 self.mock_local_unit.return_value = 'mock-wordpress/0'
41
42 patcher = mock.patch('charmhelpers.core.hookenv.config')
43 self.mock_config = patcher.start()
44 self.addCleanup(patcher.stop)
45 self.mock_config.return_value = {'blog_hostname': 'myblog.example.com'}
46
47 patcher = mock.patch('charmhelpers.core.host.log')
48 self.mock_log = patcher.start()
49 self.addCleanup(patcher.stop)
50 self.mock_log.return_value = ''
51
52 status.active.reset_mock()
53 status.blocked.reset_mock()
54 status.maintenance.reset_mock()
55
56 @mock.patch('charms.reactive.clear_flag')
57 def test_hook_upgrade_charm_flags(self, clear_flag):
58 '''Test correct flags set via upgrade-charm hook'''
59 wordpress.upgrade_charm()
60 self.assertFalse(status.maintenance.assert_called())
61 want = [
62 mock.call('wordpress.configured'),
63 ]
64 self.assertFalse(clear_flag.assert_has_calls(want, any_order=True))
diff --git a/tox.ini b/tox.ini
0new file mode 10064465new file mode 100644
index 0000000..7b45934
--- /dev/null
+++ b/tox.ini
@@ -0,0 +1,45 @@
1[tox]
2skipsdist=True
3envlist = unit, functional
4skip_missing_interpreters = True
5
6[testenv]
7basepython = python3
8setenv =
9 PYTHONPATH = .
10
11[testenv:unit]
12commands =
13 pytest --ignore {toxinidir}/tests/functional \
14 {posargs:-v --cov=reactive --cov-report=term-missing --cov-branch}
15deps = -r{toxinidir}/tests/unit/requirements.txt
16 -r{toxinidir}/requirements.txt
17setenv =
18 PYTHONPATH={toxinidir}/lib
19 TZ=UTC
20
21[testenv:functional]
22passenv =
23 HOME
24 JUJU_REPOSITORY
25 PATH
26commands =
27 pytest -v --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 .
33deps = black
34
35[testenv:lint]
36commands = flake8
37deps = flake8
38
39[flake8]
40exclude =
41 .git,
42 __pycache__,
43 .tox,
44max-line-length = 120
45max-complexity = 10

Subscribers

People subscribed via source and target branches