wordpress-charmers team mailing list archive
-
wordpress-charmers team
-
Mailing list archive
-
Message #00656
[Merge] ~mthaddon/charm-k8s-wordpress/+git/charm-k8s-wordpress:test-coverage into charm-k8s-wordpress:master
Tom Haddon has proposed merging ~mthaddon/charm-k8s-wordpress/+git/charm-k8s-wordpress:test-coverage into charm-k8s-wordpress:master.
Commit message:
Increase test coverage from 78% to 88% on charm.py, and fix bug with detecting leader in _get_initial_password
Requested reviews:
Wordpress Charmers (wordpress-charmers)
For more details, see:
https://code.launchpad.net/~mthaddon/charm-k8s-wordpress/+git/charm-k8s-wordpress/+merge/396312
--
Your team Wordpress Charmers is requested to review the proposed merge of ~mthaddon/charm-k8s-wordpress/+git/charm-k8s-wordpress:test-coverage into charm-k8s-wordpress:master.
diff --git a/.gitignore b/.gitignore
index ed12673..f02053e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,5 +1,6 @@
build
*.charm
+*.swp
.tox
.coverage
__pycache__
diff --git a/config.yaml b/config.yaml
index acb10d3..55c3b0b 100644
--- a/config.yaml
+++ b/config.yaml
@@ -103,6 +103,6 @@ options:
wp_plugin_openstack-objectstorage_config:
type: string
description: |
- YAML dictionary with keys named after wordpress settings and the desired values.
- Please note that the settings will be reset to values provided every time hooks run
+ YAML dictionary with keys named after WordPress settings and the desired values.
+ Please note that the settings will be reset to values provided every time hooks run.
default: ""
diff --git a/src/charm.py b/src/charm.py
index 7a8f492..cba136a 100755
--- a/src/charm.py
+++ b/src/charm.py
@@ -409,7 +409,7 @@ class WordpressCharm(CharmBase):
or return an empty string if we're not."""
initial_password = self.leader_data["initial_password"]
if not initial_password:
- if self.unit.is_leader:
+ if self.unit.is_leader():
initial_password = password_generator()
self.leader_data["initial_password"] = initial_password
return initial_password
@@ -423,5 +423,5 @@ class WordpressCharm(CharmBase):
event.fail("Initial password has not been set yet.")
-if __name__ == "__main__":
+if __name__ == "__main__": # pragma: no cover
main(WordpressCharm)
diff --git a/tests/unit/test_charm.py b/tests/unit/test_charm.py
index dfd2883..0c3de68 100644
--- a/tests/unit/test_charm.py
+++ b/tests/unit/test_charm.py
@@ -15,12 +15,16 @@ from ops.model import (
MaintenanceStatus,
)
-from test_wordpress import TEST_MODEL_CONFIG
+from test_wordpress import (
+ TEST_MODEL_CONFIG_MINIMAL,
+ TEST_MODEL_CONFIG_FULL,
+)
class TestWordpressCharm(unittest.TestCase):
- test_model_config = TEST_MODEL_CONFIG
+ test_model_config = TEST_MODEL_CONFIG_FULL
+ test_model_config_minimal = TEST_MODEL_CONFIG_MINIMAL
def setUp(self):
self.harness = testing.Harness(WordpressCharm)
@@ -34,10 +38,10 @@ class TestWordpressCharm(unittest.TestCase):
# connection details from the charm config.
charm = self.harness.charm
self.assertFalse(charm.state.has_db_relation)
- self.assertEqual(charm.state.db_host, TEST_MODEL_CONFIG["db_host"])
- self.assertEqual(charm.state.db_name, TEST_MODEL_CONFIG["db_name"])
- self.assertEqual(charm.state.db_user, TEST_MODEL_CONFIG["db_user"])
- self.assertEqual(charm.state.db_password, TEST_MODEL_CONFIG["db_password"])
+ self.assertEqual(charm.state.db_host, TEST_MODEL_CONFIG_FULL["db_host"])
+ self.assertEqual(charm.state.db_name, TEST_MODEL_CONFIG_FULL["db_name"])
+ self.assertEqual(charm.state.db_user, TEST_MODEL_CONFIG_FULL["db_user"])
+ self.assertEqual(charm.state.db_password, TEST_MODEL_CONFIG_FULL["db_password"])
# Add a relation and remote unit providing connection details.
# TODO: ops-lib-mysql should have a helper to set the relation data.
@@ -175,8 +179,8 @@ class TestWordpressCharm(unittest.TestCase):
}
self.assertEqual(self.harness.charm.make_pod_resources(), expected)
- # And now test with no tls config.
- self.harness.update_config({"tls_secret_name": ""})
+ # And now test with minimal config.
+ self.harness.update_config(self.test_model_config_minimal)
expected = {
'kubernetesResources': {
'ingressResources': [
@@ -200,28 +204,6 @@ class TestWordpressCharm(unittest.TestCase):
]
},
},
- {
- 'host': 'cool-newsite.org',
- 'http': {
- 'paths': [
- {
- 'path': '/',
- 'backend': {'serviceName': 'wordpress', 'servicePort': 80},
- }
- ]
- },
- },
- {
- 'host': 'blog.test.com',
- 'http': {
- 'paths': [
- {
- 'path': '/',
- 'backend': {'serviceName': 'wordpress', 'servicePort': 80},
- }
- ]
- },
- }
],
},
}
@@ -230,6 +212,114 @@ class TestWordpressCharm(unittest.TestCase):
}
self.assertEqual(self.harness.charm.make_pod_resources(), expected)
+ def test_make_pod_spec(self):
+ expected = {
+ 'version': 2,
+ 'containers': [{
+ 'config': {
+ 'SWIFT_AUTH_URL': 'auth-url',
+ 'SWIFT_BUCKET': 'bucket',
+ 'SWIFT_COPY_TO_SWIFT': None,
+ 'SWIFT_PASSWORD': 'password',
+ 'SWIFT_PREFIX': None,
+ 'SWIFT_REGION': None,
+ 'SWIFT_REMOVE_LOCAL_FILE': None,
+ 'SWIFT_SERVE_FROM_SWIFT': None,
+ 'SWIFT_TENANT': None,
+ 'SWIFT_URL': None,
+ 'SWIFT_USERNAME': None,
+ 'WORDPRESS_DB_HOST': '10.215.74.139',
+ 'WORDPRESS_DB_NAME': 'wordpress',
+ 'WORDPRESS_DB_PASSWORD': 'letmein123',
+ 'WORDPRESS_DB_USER': 'admin',
+ 'WP_PLUGIN_AKISMET_KEY': 'somerandomstring',
+ 'WP_PLUGIN_OPENID_TEAM_MAP': True,
+ 'test-key': 'test'
+ },
+ 'imageDetails': {
+ 'imagePath': 'testimageregistry/wordpress:bionic-latest',
+ 'password': 'dontleakme',
+ 'username': 'test-image-user'
+ },
+ 'kubernetes': {
+ 'readinessProbe': {
+ 'exec': {'command': ['/srv/wordpress-helpers/ready.sh']}
+ }
+ },
+ 'name': 'wordpress',
+ 'ports': [{
+ 'containerPort': 80,
+ 'name': 'http',
+ 'protocol': 'TCP',
+ }]}
+ ],
+ }
+ self.harness.charm.leader_data = {}
+ actual = self.harness.charm.make_pod_spec()
+ # Remove values that are auto-generated so hard to test.
+ for secret in WORDPRESS_SECRETS:
+ del actual["containers"][0]["config"][secret]
+ self.assertEqual(actual, expected)
+
+ # And now test with minimal config.
+ self.harness.update_config(self.test_model_config_minimal, unset=['image_user', 'image_pass',
+ 'wp_plugin_akismet_key'])
+ expected = {
+ 'version': 2,
+ 'containers': [{
+ 'config': {
+ 'SWIFT_AUTH_URL': 'auth-url',
+ 'SWIFT_BUCKET': 'bucket',
+ 'SWIFT_COPY_TO_SWIFT': None,
+ 'SWIFT_PASSWORD': 'password',
+ 'SWIFT_PREFIX': None,
+ 'SWIFT_REGION': None,
+ 'SWIFT_REMOVE_LOCAL_FILE': None,
+ 'SWIFT_SERVE_FROM_SWIFT': None,
+ 'SWIFT_TENANT': None,
+ 'SWIFT_URL': None,
+ 'SWIFT_USERNAME': None,
+ 'WORDPRESS_DB_HOST': '10.215.74.139',
+ 'WORDPRESS_DB_NAME': 'wordpress',
+ 'WORDPRESS_DB_PASSWORD': 'letmein123',
+ 'WORDPRESS_DB_USER': 'admin',
+ 'WORDPRESS_TLS_DISABLED': 'true',
+ },
+ 'imageDetails': {
+ 'imagePath': 'testimageregistry/wordpress:bionic-latest',
+ },
+ 'kubernetes': {
+ 'readinessProbe': {
+ 'exec': {'command': ['/srv/wordpress-helpers/ready.sh']}
+ }
+ },
+ 'name': 'wordpress',
+ 'ports': [{
+ 'containerPort': 80,
+ 'name': 'http',
+ 'protocol': 'TCP',
+ }]}
+ ],
+ }
+ self.harness.charm.leader_data = {}
+ actual = self.harness.charm.make_pod_spec()
+ # Remove values that are auto-generated so hard to test.
+ for secret in WORDPRESS_SECRETS:
+ del actual["containers"][0]["config"][secret]
+ self.assertEqual(actual, expected)
+
+ def test_get_initial_password(self):
+ self.harness.charm.leader_data = {"initial_password": "supersekrit"}
+ self.assertEqual(self.harness.charm._get_initial_password(), "supersekrit")
+ # Now test with no password, but not leader.
+ self.harness.charm.leader_data = {"initial_password": ""}
+ self.harness.set_leader(False)
+ self.assertEqual(self.harness.charm._get_initial_password(), "")
+ # And with no password, but is leader.
+ self.harness.charm.leader_data = {"initial_password": ""}
+ self.harness.set_leader(True)
+ self.assertEqual(len(self.harness.charm._get_initial_password()), 24)
+
def test_on_get_initial_password_action(self):
action_event = Mock()
# First test with no initial password set.
diff --git a/tests/unit/test_wordpress.py b/tests/unit/test_wordpress.py
index 6083481..3894ff7 100644
--- a/tests/unit/test_wordpress.py
+++ b/tests/unit/test_wordpress.py
@@ -9,7 +9,24 @@ import charm
import wordpress
-TEST_MODEL_CONFIG = {
+TEST_MODEL_CONFIG_MINIMAL = {
+ "image": "testimageregistry/wordpress:bionic-latest",
+ "db_host": "10.215.74.139",
+ "db_name": "wordpress",
+ "db_user": "admin",
+ "db_password": "letmein123",
+ "additional_hostnames": "",
+ "wp_plugin_openid_team_map": False,
+ "container_config": "",
+ "initial_settings": """\
+ user_name: admin
+ admin_email: root@xxxxxxxxxxxxxxxxxxx
+ weblog_title: Test Blog
+ blog_public: False""",
+ "tls_secret_name": "",
+}
+
+TEST_MODEL_CONFIG_FULL = {
"image": "testimageregistry/wordpress:bionic-latest",
"image_user": "test-image-user",
"image_pass": "dontleakme",
@@ -27,6 +44,7 @@ TEST_MODEL_CONFIG = {
weblog_title: Test Blog
blog_public: False""",
"tls_secret_name": "wordpress-tls",
+ "wp_plugin_openstack-objectstorage_config": "auth-url: auth-url\nbucket: bucket\npassword: password",
}
@@ -50,7 +68,8 @@ class RequestsResult:
class HelperTest(unittest.TestCase):
- test_model_config = TEST_MODEL_CONFIG
+ test_model_config_full = TEST_MODEL_CONFIG_FULL
+ test_model_config_minimal = TEST_MODEL_CONFIG_MINIMAL
def test_password_generator(self):
password = wordpress.password_generator()
@@ -61,41 +80,41 @@ class HelperTest(unittest.TestCase):
def test_generate_pod_config(self):
# Ensure that secrets are stripped from config.
- result = charm.generate_pod_config(self.test_model_config, secured=True)
+ result = charm.generate_pod_config(self.test_model_config_full, secured=True)
secured_keys = ("WORDPRESS_DB_PASSWORD", "WP_PLUGIN_AKISMET_KEY")
[self.assertNotIn(key, result) for key in secured_keys]
self.assertIn("WP_PLUGIN_OPENID_TEAM_MAP", result)
# Ensure that we receive the full pod config.
- result = charm.generate_pod_config(self.test_model_config, secured=False)
+ result = charm.generate_pod_config(self.test_model_config_full, secured=False)
[self.assertIn(key, result) for key in secured_keys]
self.assertIn("WP_PLUGIN_AKISMET_KEY", result)
- # Test we don't break with missing non-essential config options.
- non_essential_model_config = copy.deepcopy(self.test_model_config)
- del non_essential_model_config["wp_plugin_openid_team_map"]
- del non_essential_model_config["wp_plugin_akismet_key"]
- result = charm.generate_pod_config(self.test_model_config, secured=False)
- self.assertTrue(result)
-
# Test for initial container config.
- result = charm.generate_pod_config(self.test_model_config)
- test_container_config = yaml.safe_load(self.test_model_config["container_config"])
+ result = charm.generate_pod_config(self.test_model_config_full)
+ test_container_config = yaml.safe_load(self.test_model_config_full["container_config"])
self.assertEqual(test_container_config["test-key"], result["test-key"])
# Test we pass set WORDPRESS_TLS_ENABLED if we have `tls_secret_name`.
- result = charm.generate_pod_config(self.test_model_config)
+ result = charm.generate_pod_config(self.test_model_config_full)
self.assertNotIn("WORDPRESS_TLS_DISABLED", result)
- # Remove `tls_secret_name` and test again.
- non_tls_secret_config = copy.deepcopy(self.test_model_config)
- non_tls_secret_config["tls_secret_name"] = ""
- result = charm.generate_pod_config(non_tls_secret_config)
+
+ # Test `wp_plugin_openstack-objectstorage_config`.
+ result = charm.generate_pod_config(self.test_model_config_full, secured=False)
+ self.assertEqual(result["SWIFT_AUTH_URL"], "auth-url")
+ self.assertEqual(result["SWIFT_BUCKET"], "bucket")
+ self.assertEqual(result["SWIFT_PASSWORD"], "password")
+ self.assertEqual(result["SWIFT_PREFIX"], None)
+
+ # Test we don't break with missing non-essential config options.
+ result = charm.generate_pod_config(self.test_model_config_minimal, secured=False)
+ self.assertTrue(result)
self.assertEqual(result["WORDPRESS_TLS_DISABLED"], "true")
class WordpressTest(unittest.TestCase):
- test_model_config = TEST_MODEL_CONFIG
+ test_model_config = TEST_MODEL_CONFIG_FULL
def setUp(self):
self.test_wordpress = wordpress.Wordpress(copy.deepcopy(self.test_model_config))
Follow ups