From 1aeb6268e560e8222625bdcb2f880082bf447bf5 Mon Sep 17 00:00:00 2001 From: Felipe Reyes Date: Thu, 15 Sep 2022 11:21:16 -0300 Subject: [PATCH] Enable zuul and git-review - Add .zuul.yaml with jobs definition to run unit tests, pep8 and coverage jobs, yoga and zed are added because python 3.8 and 3.10 is desired. - Add .gitreview file - Fix unit tests, use Mock requests.get() when rendering the configuration. - Format logging messages correctly Change-Id: I78d9c632590d0601d93105ba3d13d987aa0e30d5 --- .gitreview | 4 +++ .zuul.yaml | 5 ++++ src/charm.py | 8 +++--- unit_tests/test_charm.py | 56 ++++++++++++++++++++++++++++------------ 4 files changed, 51 insertions(+), 22 deletions(-) create mode 100644 .gitreview create mode 100644 .zuul.yaml diff --git a/.gitreview b/.gitreview new file mode 100644 index 0000000..5c94c10 --- /dev/null +++ b/.gitreview @@ -0,0 +1,4 @@ +[gerrit] +host=review.opendev.org +port=29418 +project=openstack/charm-keystone-openidc.git diff --git a/.zuul.yaml b/.zuul.yaml new file mode 100644 index 0000000..b8c0dd4 --- /dev/null +++ b/.zuul.yaml @@ -0,0 +1,5 @@ +- project: + templates: + - openstack-python3-charm-yoga-jobs + - openstack-python3-charm-zed-jobs + - openstack-cover-jobs diff --git a/src/charm.py b/src/charm.py index d78278a..2999314 100755 --- a/src/charm.py +++ b/src/charm.py @@ -60,8 +60,7 @@ def when_data_ready(func): def _wrapper(self, event): try: if not self.is_data_ready(): - logger.debug('relation data is not ready yet (%s)', - event) + logger.debug('relation data is not ready yet (%s)', event) return except CharmConfigError as ex: self.unit.status = BlockedStatus(ex.msg) @@ -259,7 +258,7 @@ class KeystoneOpenIDCCharm(ops_openstack.core.OSBaseCharm): def _on_keystone_fid_service_provider_relation_changed(self, event): if not self.is_data_ready(): - logger.debug('relation data is not ready yet: %s', event) + logger.debug('relation data is not ready yet (%s)', event) return self.update_principal_data() self.update_config_if_needed() @@ -290,8 +289,7 @@ class KeystoneOpenIDCCharm(ops_openstack.core.OSBaseCharm): def _on_config_changed(self, event): if not self.is_data_ready(): - logger.debug('relation data is not ready yet', - event) + logger.debug('relation data is not ready yet (%s)', event) return self._stored.is_started = True diff --git a/unit_tests/test_charm.py b/unit_tests/test_charm.py index eab738c..b52fc8e 100644 --- a/unit_tests/test_charm.py +++ b/unit_tests/test_charm.py @@ -1,5 +1,6 @@ import logging import os +import shutil import sys import tempfile import unittest @@ -24,6 +25,7 @@ CRYPTO_PASSPHRASE = '1e19bb8a-a92d-4377-8226-5e8fc475822c' class BaseTestCharm(unittest.TestCase): def setUp(self): + self.tmpdir = tempfile.TemporaryDirectory() self.harness = Harness(charm.KeystoneOpenIDCCharm, meta=''' name: keystone-openidc provides: @@ -40,6 +42,12 @@ class BaseTestCharm(unittest.TestCase): self.addCleanup(self.harness.cleanup) self.harness.begin() + def tearDown(self): + try: + shutil.rmtree(self.tmpdir, ignore_errors=True) + except Exception as ex: + logger.debug(ex) + class TestRelations(BaseTestCharm): def test_add_relation(self): @@ -78,24 +86,38 @@ class TestCharm(BaseTestCharm): @mock.patch('os.fchown') @mock.patch('os.chown') def test_render_config_leader(self, chown, fchown): + opts = { + 'oidc-provider-metadata-url': WELL_KNOWN_URL, + 'oidc-provider-issuer': 'foo', + 'oidc-client-id': 'keystone', + 'oidc-client-secret': 'ubuntu11', + } + + well_known_url_content = { + 'introspection_endpoint': INTROSPECTION_ENDPOINT_INVALID, + } self.harness.set_leader(True) - with tempfile.TemporaryDirectory() as tmpdir: - with mock.patch("charm.KeystoneOpenIDCCharm.config_dir", - new_callable=mock.PropertyMock, - return_value=tmpdir): - self.harness.update_config( - key_values={'oidc-provider-metadata-url': WELL_KNOWN_URL}) - self.harness.charm.render_config() - fpath = self.harness.charm.options.openidc_location_config - self.assertTrue(os.path.isfile(fpath)) - with open(fpath) as f: - content = f.read() - self.assertIn(f'OIDCProviderMetadataURL {WELL_KNOWN_URL}', - content) - self.assertIn( - f'OIDCCryptoPassphrase {str(self.crypto_passphrase)}', - content - ) + with requests_mock.Mocker() as m, \ + mock.patch( # noqa: E127 + "charm.KeystoneOpenIDCCharm.config_dir", + new_callable=mock.PropertyMock, + return_value=self.tmpdir.name): + m.get(WELL_KNOWN_URL, json=well_known_url_content) + self.harness.update_config( + key_values=opts) + self.harness.charm.render_config() + fpath = self.harness.charm.options.openidc_location_config + self.assertTrue(os.path.isfile(fpath)) + with open(fpath) as f: + content = f.read() + self.assertIn( + f'OIDCProviderMetadataURL {WELL_KNOWN_URL}', + content + ) + self.assertIn( + f'OIDCCryptoPassphrase {str(self.crypto_passphrase)}', + content + ) def test_find_missing_keys_no_metadata_url(self): opts = {