Merge ~gnuoy/vault-charm:trivial-unit-tests into ~vault-charmers/vault-charm/+git/vault-charm:master

Proposed by Liam Young
Status: Merged
Approved by: Liam Young
Approved revision: 5fd4513e761fc14d36fee4744e01426bf07b13b4
Merged at revision: ebb5635d4ef5c5d68ca4711a2b5624943421cab5
Proposed branch: ~gnuoy/vault-charm:trivial-unit-tests
Merge into: ~vault-charmers/vault-charm/+git/vault-charm:master
Diff against target: 159 lines (+132/-0)
4 files modified
.gitignore (+1/-0)
test-requirements.txt (+2/-0)
unit_tests/__init__.py (+1/-0)
unit_tests/test_vault.py (+128/-0)
Reviewer Review Type Date Requested Status
David Ames (community) Approve
Review via email: mp+337170@code.launchpad.net

Commit message

Add simple unit tests

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
David Ames (thedac) wrote :

Looks good. Approved.

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

Change successfully merged at revision ebb5635d4ef5c5d68ca4711a2b5624943421cab5

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/.gitignore b/.gitignore
2index 17fa1cb..c63ffaf 100644
3--- a/.gitignore
4+++ b/.gitignore
5@@ -3,3 +3,4 @@ build/*
6 .tox/*
7 .stestr/*
8 __pycache__
9+.unit-state.db
10diff --git a/test-requirements.txt b/test-requirements.txt
11index 667d3b9..413132e 100644
12--- a/test-requirements.txt
13+++ b/test-requirements.txt
14@@ -2,3 +2,5 @@ flake8>=2.2.4,<=2.4.1
15 os-testr>=0.4.1
16 mock>=1.2
17 coverage>=3.6
18+psycopg2
19+charms.reactive
20diff --git a/unit_tests/__init__.py b/unit_tests/__init__.py
21index e69de29..35411ac 100644
22--- a/unit_tests/__init__.py
23+++ b/unit_tests/__init__.py
24@@ -0,0 +1 @@
25+# unit tests
26diff --git a/unit_tests/test_vault.py b/unit_tests/test_vault.py
27new file mode 100644
28index 0000000..d46955c
29--- /dev/null
30+++ b/unit_tests/test_vault.py
31@@ -0,0 +1,128 @@
32+import mock
33+import unittest
34+from unittest.mock import patch
35+
36+import charms.reactive
37+
38+# Mock out reactive decorators prior to importing reactive.vault
39+dec_mock = mock.MagicMock()
40+dec_mock.return_value = lambda x: x
41+charms.reactive.hook = dec_mock
42+charms.reactive.when = dec_mock
43+charms.reactive.when_not = dec_mock
44+
45+import reactive.vault as handlers # noqa: E402
46+
47+
48+class TestHandlers(unittest.TestCase):
49+
50+ def test_ssl_available(self):
51+ self.assertFalse(handlers.ssl_available({
52+ 'ssl-cert': '',
53+ 'ssl-key': ''}))
54+ self.assertFalse(handlers.ssl_available({
55+ 'ssl-cert': 'acert',
56+ 'ssl-key': ''}))
57+ self.assertFalse(handlers.ssl_available({
58+ 'ssl-cert': '',
59+ 'ssl-key': 'akey'}))
60+ self.assertTrue(handlers.ssl_available({
61+ 'ssl-cert': 'acert',
62+ 'ssl-key': 'akey'}))
63+
64+ @patch.object(handlers, 'is_state')
65+ @patch.object(handlers, 'config')
66+ @patch.object(handlers, 'open_port')
67+ @patch.object(handlers, 'service_start')
68+ @patch.object(handlers, 'render')
69+ @patch.object(handlers, 'status_set')
70+ @patch.object(handlers, 'remove_state')
71+ def test_configure_vault(self, remove_state, status_set, render,
72+ service_start, open_port, config, is_state):
73+ config.return_value = {'disable-mlock': False}
74+ is_state.return_value = True
75+ psql = mock.MagicMock()
76+ psql = mock.MagicMock()
77+ psql.master = 'myuri'
78+ handlers.configure_vault(psql)
79+ expected_context = {
80+ 'db_conn': 'myuri',
81+ 'disable_mlock': False,
82+ 'ssl_available': True,
83+ }
84+ status_set_calls = [
85+ mock.call('maintenance', 'creating vault config'),
86+ mock.call('maintenance', 'creating vault unit file'),
87+ mock.call('maintenance', 'starting vault'),
88+ mock.call('maintenance', 'opening vault port'),
89+ mock.call('active', '=^_^='),
90+ ]
91+ render_calls = [
92+ mock.call(
93+ 'vault.hcl.j2',
94+ '/var/snap/vault/common/vault.hcl',
95+ expected_context,
96+ perms=0o600),
97+ mock.call(
98+ 'vault.service.j2',
99+ '/etc/systemd/system/vault.service',
100+ {},
101+ perms=0o644)
102+ ]
103+ open_port.assert_called_once_with(8200)
104+ status_set.assert_has_calls(status_set_calls)
105+ render.assert_has_calls(render_calls)
106+
107+ # Check flipping disable-mlock makes it to the context
108+ config.return_value = {'disable-mlock': True}
109+ expected_context['disable_mlock'] = True
110+ handlers.configure_vault(psql)
111+ render_calls = [
112+ mock.call(
113+ 'vault.hcl.j2',
114+ '/var/snap/vault/common/vault.hcl',
115+ expected_context,
116+ perms=0o600),
117+ mock.call(
118+ 'vault.service.j2',
119+ '/etc/systemd/system/vault.service',
120+ {},
121+ perms=0o644)
122+ ]
123+ render.assert_has_calls(render_calls)
124+
125+ @patch.object(handlers, 'remove_state')
126+ def test_disable_mlock_changed(self, remove_state):
127+ handlers.disable_mlock_changed()
128+ remove_state.assert_called_once_with('configured')
129+
130+ @patch.object(handlers, 'remove_state')
131+ def test_upgrade_charm(self, remove_state):
132+ calls = [mock.call('configured'),
133+ mock.call('vault.nrpe.configured'),
134+ mock.call('vault.ssl.configured')]
135+ handlers.upgrade_charm()
136+ remove_state.assert_has_calls(calls)
137+
138+ def test_request_db(self):
139+ psql = mock.MagicMock()
140+ handlers.request_db(psql)
141+ psql.set_database.assert_called_once_with('vault')
142+
143+ @patch.object(handlers, 'set_state')
144+ @patch.object(handlers, 'psycopg2')
145+ @patch.object(handlers, 'status_set')
146+ def test_create_vault_table(self, status_set, psycopg2, set_state):
147+ psql = mock.MagicMock()
148+ psql.master = 'myuri'
149+ handlers.create_vault_table(psql)
150+ db_calls = [
151+ mock.call(handlers.VAULT_TABLE_DDL),
152+ mock.call(handlers.VAULT_INDEX_DDL),
153+ ]
154+ psycopg2.connect().cursor().execute.assert_has_calls(db_calls)
155+
156+ @patch.object(handlers, 'remove_state')
157+ def test_database_not_ready(self, remove_state):
158+ handlers.database_not_ready()
159+ remove_state.assert_called_once_with('vault.schema.created')

Subscribers

People subscribed via source and target branches

to all changes: