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 = {