From 687072cfba2d2474bbf72e4a4359c8c3df794ddf Mon Sep 17 00:00:00 2001 From: Mariana Ulaieva Date: Jun 10 2020 07:09:41 +0000 Subject: [PATCH 1/2] Record the lightblue queries What Freshmaker will do is highly dependent on the data in lightblue. It gets difficult to reproduce issues when by the time we look at the issue, the data in lightblue has already changed. One idea to help us reproduce these issues locally is to record the Lightblue queries per Freshmaker event (configured for only CVE rebuild events). --- diff --git a/freshmaker/config.py b/freshmaker/config.py index 79e936e..8916bf6 100644 --- a/freshmaker/config.py +++ b/freshmaker/config.py @@ -383,6 +383,16 @@ class Config(object): 'type': tuple, 'default': ("Generally Available", "Tech Preview", "Beta",), 'desc': 'Release categories', + }, + 'vcrpy_path': { + 'type': str, + 'default': '', + 'desc': 'vcr path where lightblue queries will be recorded' + }, + 'vcrpy_mode': { + 'type': str, + 'default': 'all', + 'desc': 'vcr mode for recording lightblue queries' } } @@ -516,6 +526,12 @@ class Config(object): fixed_permissions.update(permissions) self._permissions = fixed_permissions + def _setifok_vcrpy_path(self, s): + s = str(s) + if s: + import vcr # noqa: F401 + self._vcrpy_path = s + def _get_krb_auth_ccache_file(self): if not self._krb_auth_ccache_file: return self._krb_auth_ccache_file diff --git a/freshmaker/lightblue.py b/freshmaker/lightblue.py index 10850ee..353ff03 100644 --- a/freshmaker/lightblue.py +++ b/freshmaker/lightblue.py @@ -501,7 +501,7 @@ class LightBlue(object): return self.entity_versions.get(entity_name, '') def _make_request(self, entity, data): - """Make a request to query data from LightBlue + """Make a request to query data from LightBlue and save it if vcrpy is configured :param str entity: the entity part to construct a full URL sent to LightBlue. Refer to callers of ``_make_request`` to learn what @@ -516,11 +516,23 @@ class LightBlue(object): of errors. """ entity_url = '{}/{}'.format(self.api_root, entity) - response = requests.post(entity_url, - data=json.dumps(data), - verify=self.verify_ssl, - cert=(self.cert, self.private_key), - headers={'Content-Type': 'application/json'}) + # Record the Freshmaker lightblue queries + request_kwargs = { + "data": json.dumps(data), + "verify": self.verify_ssl, + "cert": (self.cert, self.private_key), + "headers": {'Content-Type': 'application/json'} + } + if self.event_id and conf.vcrpy_path: + import vcr + my_vcr = vcr.VCR( + cassette_library_dir=conf.vcrpy_path, + record_mode=conf.vcrpy_mode, + ) + with my_vcr.use_cassette(f'{self.event_id}.yml'): + response = requests.post(entity_url, **request_kwargs) + else: + response = requests.post(entity_url, **request_kwargs) status_code = response.status_code diff --git a/tests/conftest.py b/tests/conftest.py index e6127c6..573654a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -21,6 +21,7 @@ import flask import pytest +from unittest import mock @pytest.fixture(autouse=True) @@ -34,3 +35,9 @@ def clear_flask_g(): for attr in ('group', 'user'): if hasattr(flask.g, attr): delattr(flask.g, attr) + + +@pytest.fixture(autouse=True) +def mock_vcrpy(): + with mock.patch('vcr.VCR'): + yield From a4ef061413a6454267df0d04528d89fd3ce630e7 Mon Sep 17 00:00:00 2001 From: Mariana Ulaieva Date: Jun 10 2020 07:09:41 +0000 Subject: [PATCH 2/2] Add information about using vcrpy in docs --- diff --git a/docs/configuration.rst b/docs/configuration.rst index 846d675..c6f3758 100644 --- a/docs/configuration.rst +++ b/docs/configuration.rst @@ -37,3 +37,8 @@ Other * ``rebuilt_nvr_release_suffix`` - a suffix to add to the ``rebuilt_nvr`` release in addition to the timestamp. This defaults to an empty string. + +* vcrpy - use this library for recording the Lightblue queries per + Freshmaker event. The importing of vcrpy and the recording of Lightblue queries is performed if + the vcrpy configuration variables (``vcrpy_path`` and ``vcrpy_mode``) are set in + ``freshmaker/config.py``