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
This commit is contained in:
Felipe Reyes 2022-09-15 11:21:16 -03:00
parent 98901bc318
commit 1aeb6268e5
4 changed files with 51 additions and 22 deletions

4
.gitreview Normal file
View File

@ -0,0 +1,4 @@
[gerrit]
host=review.opendev.org
port=29418
project=openstack/charm-keystone-openidc.git

5
.zuul.yaml Normal file
View File

@ -0,0 +1,5 @@
- project:
templates:
- openstack-python3-charm-yoga-jobs
- openstack-python3-charm-zed-jobs
- openstack-cover-jobs

View File

@ -60,8 +60,7 @@ def when_data_ready(func):
def _wrapper(self, event): def _wrapper(self, event):
try: try:
if not self.is_data_ready(): if not self.is_data_ready():
logger.debug('relation data is not ready yet (%s)', logger.debug('relation data is not ready yet (%s)', event)
event)
return return
except CharmConfigError as ex: except CharmConfigError as ex:
self.unit.status = BlockedStatus(ex.msg) 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): def _on_keystone_fid_service_provider_relation_changed(self, event):
if not self.is_data_ready(): 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 return
self.update_principal_data() self.update_principal_data()
self.update_config_if_needed() self.update_config_if_needed()
@ -290,8 +289,7 @@ class KeystoneOpenIDCCharm(ops_openstack.core.OSBaseCharm):
def _on_config_changed(self, event): def _on_config_changed(self, event):
if not self.is_data_ready(): if not self.is_data_ready():
logger.debug('relation data is not ready yet', logger.debug('relation data is not ready yet (%s)', event)
event)
return return
self._stored.is_started = True self._stored.is_started = True

View File

@ -1,5 +1,6 @@
import logging import logging
import os import os
import shutil
import sys import sys
import tempfile import tempfile
import unittest import unittest
@ -24,6 +25,7 @@ CRYPTO_PASSPHRASE = '1e19bb8a-a92d-4377-8226-5e8fc475822c'
class BaseTestCharm(unittest.TestCase): class BaseTestCharm(unittest.TestCase):
def setUp(self): def setUp(self):
self.tmpdir = tempfile.TemporaryDirectory()
self.harness = Harness(charm.KeystoneOpenIDCCharm, meta=''' self.harness = Harness(charm.KeystoneOpenIDCCharm, meta='''
name: keystone-openidc name: keystone-openidc
provides: provides:
@ -40,6 +42,12 @@ class BaseTestCharm(unittest.TestCase):
self.addCleanup(self.harness.cleanup) self.addCleanup(self.harness.cleanup)
self.harness.begin() self.harness.begin()
def tearDown(self):
try:
shutil.rmtree(self.tmpdir, ignore_errors=True)
except Exception as ex:
logger.debug(ex)
class TestRelations(BaseTestCharm): class TestRelations(BaseTestCharm):
def test_add_relation(self): def test_add_relation(self):
@ -78,24 +86,38 @@ class TestCharm(BaseTestCharm):
@mock.patch('os.fchown') @mock.patch('os.fchown')
@mock.patch('os.chown') @mock.patch('os.chown')
def test_render_config_leader(self, chown, fchown): 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) self.harness.set_leader(True)
with tempfile.TemporaryDirectory() as tmpdir: with requests_mock.Mocker() as m, \
with mock.patch("charm.KeystoneOpenIDCCharm.config_dir", mock.patch( # noqa: E127
new_callable=mock.PropertyMock, "charm.KeystoneOpenIDCCharm.config_dir",
return_value=tmpdir): new_callable=mock.PropertyMock,
self.harness.update_config( return_value=self.tmpdir.name):
key_values={'oidc-provider-metadata-url': WELL_KNOWN_URL}) m.get(WELL_KNOWN_URL, json=well_known_url_content)
self.harness.charm.render_config() self.harness.update_config(
fpath = self.harness.charm.options.openidc_location_config key_values=opts)
self.assertTrue(os.path.isfile(fpath)) self.harness.charm.render_config()
with open(fpath) as f: fpath = self.harness.charm.options.openidc_location_config
content = f.read() self.assertTrue(os.path.isfile(fpath))
self.assertIn(f'OIDCProviderMetadataURL {WELL_KNOWN_URL}', with open(fpath) as f:
content) content = f.read()
self.assertIn( self.assertIn(
f'OIDCCryptoPassphrase {str(self.crypto_passphrase)}', f'OIDCProviderMetadataURL {WELL_KNOWN_URL}',
content content
) )
self.assertIn(
f'OIDCCryptoPassphrase {str(self.crypto_passphrase)}',
content
)
def test_find_missing_keys_no_metadata_url(self): def test_find_missing_keys_no_metadata_url(self):
opts = { opts = {