diff --git a/omps/quay.py b/omps/quay.py index 3e12bd5..e1859bd 100644 --- a/omps/quay.py +++ b/omps/quay.py @@ -309,17 +309,18 @@ def _handle_courier_exception(self, e): msg = "Failed to push manifest: {}".format(e) raise_for_courier_exception(e, new_msg=msg) - def _get_repo_content(self, repo): + def _get_org_content(self): """Return content of repository""" - endpoint = '/cnr/api/v1/packages/' - url = '{q}{e}{o}/{r}'.format( - q=self._quay_url, - e=endpoint, - o=self._organization, - r=repo, - ) + endpoint = '/cnr/api/v1/packages' + url = f'{self._quay_url}{endpoint}' headers = {'Authorization': self._token} - res = requests.get(url, headers=headers, timeout=self._timeout) + params = {'namespace': self._organization} + res = requests.get( + url, + headers=headers, + params=params, + timeout=self._timeout + ) res.raise_for_status() return res.json() @@ -345,19 +346,24 @@ def _raise(exc): except ValueError: quay_response = {} raise QuayAuthorizationError(msg, quay_response) - elif exc.response.status_code == 404: - msg = ("Package {}/{} not found" - .format(self._organization, repo)) - raise QuayPackageNotFound(msg) raise QuayPackageError(msg) + def get_repo(org_response): + exp_name = f'{self._organization}/{repo}' + for r in org_response: + if r['name'] == exp_name: + return r + + msg = f"Package {self._organization}/{repo} not found" + raise QuayPackageNotFound(msg) + try: - res = self._get_repo_content(repo) + res = self._get_org_content() except requests.exceptions.RequestException as e: _raise(e) - releases = [package['release'] for package in res] + releases = get_repo(res)['releases'] return releases def get_releases(self, repo): diff --git a/tests/api/v2/conftest.py b/tests/api/v2/conftest.py index eac6a44..3c8da79 100644 --- a/tests/api/v2/conftest.py +++ b/tests/api/v2/conftest.py @@ -15,9 +15,8 @@ True, # endpoint with version False, # endpoint without version ]) -def endpoint_push_zipfile(request, release_version): +def endpoint_push_zipfile(request, release_version, organization): """Returns URL for zipfile endpoints""" - organization = 'testorg' version = release_version if request.param else None url_path = '/v2/{}/zipfile'.format(organization) @@ -34,9 +33,8 @@ def endpoint_push_zipfile(request, release_version): True, # endpoint with version False, # endpoint without version ]) -def endpoint_push_koji(request, release_version): +def endpoint_push_koji(request, release_version, organization): """Returns URL for koji endpoints""" - organization = 'testorg' nvr = 'build-1.0.1-2' version = release_version if request.param else None @@ -54,9 +52,8 @@ def endpoint_push_koji(request, release_version): True, # endpoint with version False, # endpoint without version ]) -def endpoint_packages(request, release_version): +def endpoint_packages(request, release_version, organization): """Returns URL for packages endpoints""" - organization = 'testorg' repo = 'repo-Y' url_path = '/v2/{}/{}'.format(organization, repo) diff --git a/tests/conftest.py b/tests/conftest.py index 1eeab61..53a8634 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -28,6 +28,11 @@ ManifestDirMeta = namedtuple('ManifestDirMeta', ['path', 'pkg_name', 'valid']) +@pytest.fixture() +def organization(): + return 'testorg' + + @pytest.fixture() def release_version(): return "0.0.1" @@ -146,20 +151,23 @@ def mocked_quay_io(): """Mocking quay.io answers""" with requests_mock.Mocker() as m: m.get( - re.compile(r'/cnr/api/v1/packages/.*'), - status_code=404, + re.compile(r'/cnr/api/v1/packages\?.*'), + json=[] ) yield m @pytest.fixture -def mocked_packages_delete_quay_io(release_version): +def mocked_packages_delete_quay_io(release_version, organization): """Mocking quay.io answers for retrieving and deleting packages""" with requests_mock.Mocker() as m: m.get( - re.compile(r'/cnr/api/v1/packages/.*'), + re.compile(r'/cnr/api/v1/packages\?.*'), json=[ - {"release": release_version}, + { + "releases": [release_version], + "name": f'{organization}/repo-Y' + }, ], ) m.delete( diff --git a/tests/test_quay.py b/tests/test_quay.py index e6c8d28..88a1cf2 100644 --- a/tests/test_quay.py +++ b/tests/test_quay.py @@ -269,11 +269,16 @@ def test_get_latest_release_version(self): with requests_mock.Mocker() as m: m.get( - '/cnr/api/v1/packages/{}/{}'.format(org, repo), + f'/cnr/api/v1/packages?namespace={org}', json=[ - {'release': "1.0.0"}, - {'release': "1.2.0"}, - {'release': "1.0.1"}, + { + 'name': 'org/something_else', + 'releases': ["2.0.0"], + }, + { + 'name': f'{org}/{repo}', + 'releases': ["1.2.0", "1.1.0", "1.0.0"] + }, ] ) @@ -288,8 +293,13 @@ def test_get_latest_release_version_not_found(self): with requests_mock.Mocker() as m: m.get( - '/cnr/api/v1/packages/{}/{}'.format(org, repo), - status_code=404, + f'/cnr/api/v1/packages?namespace={org}', + json=[ + { + 'name': 'org/something_else', + 'releases': ["2.0.0"], + } + ] ) qo = QuayOrganization(org, "token") @@ -309,10 +319,13 @@ def test_get_latest_release_version_invalid_version_only(self): with requests_mock.Mocker() as m: m.get( - '/cnr/api/v1/packages/{}/{}'.format(org, repo), + f'/cnr/api/v1/packages?namespace={org}', json=[ - {'release': "1.0.0-invalid"}, - ], + { + 'name': f'{org}/{repo}', + 'releases': ["1.0.0-invalid"] + }, + ] ) qo = QuayOrganization(org, "token") @@ -327,11 +340,16 @@ def test_get_releases_raw(self): with requests_mock.Mocker() as m: m.get( - '/cnr/api/v1/packages/{}/{}'.format(org, repo), + f'/cnr/api/v1/packages?namespace={org}', json=[ - {'release': "1.0.0"}, - {'release': "1.2.0"}, - {'release': "1.0.1-random"}, + { + 'name': 'org/something_else', + 'releases': ["2.0.0"], + }, + { + 'name': f'{org}/{repo}', + 'releases': ["1.2.0", "1.0.1-random", "1.0.0"] + }, ] ) @@ -341,7 +359,6 @@ def test_get_releases_raw(self): @pytest.mark.parametrize('error_code, expected_exc_type', [ (403, QuayAuthorizationError), - (404, QuayPackageNotFound), (500, QuayPackageError) ]) def test_get_releases_raw_errors(self, error_code, expected_exc_type): @@ -353,7 +370,7 @@ def test_get_releases_raw_errors(self, error_code, expected_exc_type): qo = QuayOrganization(org, TOKEN) with requests_mock.Mocker() as m: - m.get(f'/cnr/api/v1/packages/{org}/{repo}', + m.get(f'/cnr/api/v1/packages?namespace={org}', status_code=error_code) with pytest.raises(expected_exc_type):