Skip to content

Commit

Permalink
Adding unit-tests to cover mirrors API functionality:
Browse files Browse the repository at this point in the history
  • Loading branch information
xaoc7 authored and jdelic committed Aug 13, 2024
1 parent 941c9d0 commit 74488b9
Show file tree
Hide file tree
Showing 4 changed files with 292 additions and 22 deletions.
44 changes: 27 additions & 17 deletions aptly_api/parts/mirrors.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
Mirror = NamedTuple('Mirror', [
('uuid', str),
('name', str),
('archiveroot', str),
('archiveurl', str),
('distribution', str),
('components', Sequence[str]),
('architectures', Sequence[str]),
('meta', Sequence[Dict[str, str]]),
('downloaddate', str),
('filter', str),
('status', str),
('status', int),
('worker_pid', int),
('filter_with_deps', bool),
('skip_component_check', bool),
Expand All @@ -35,17 +35,27 @@ class MirrorsAPISection(BaseAPIClient):
@staticmethod
def mirror_from_response(api_response: Dict[str, str]) -> Mirror:
return Mirror(
uuid=cast(str, api_response["UUID"]),
uuid=cast(str, api_response["UUID"]
) if "UUID" in api_response else None,
name=cast(str, api_response["Name"]),
archiveroot=cast(str, api_response["ArchiveRoot"]),
distribution=cast(str, api_response["Distribution"]),
components=cast(List[str], api_response["Components"]),
architectures=cast(List[str], api_response["Architectures"]),
meta=cast(List[Dict[str, str]], api_response["Meta"]),
downloaddate=cast(str, api_response["LastDownloadDate"]),
filter=cast(str, api_response["Filter"]),
status=cast(str, api_response["Status"]),
worker_pid=cast(int, api_response["WorkerPID"]),
archiveurl=cast(
str, api_response["ArchiveRoot"]),
distribution=cast(
str, api_response["Distribution"]) if "Distribution" in api_response else None,
components=cast(List[str], api_response["Components"]
)if "Components" in api_response else None,
architectures=cast(List[str], api_response["Architectures"]
) if "Architectures" in api_response else None,
meta=cast(List[Dict[str, str]], api_response["Meta"]
) if "Meta" in api_response else None,
downloaddate=cast(
str, api_response["LastDownloadDate"]) if "LastDownloadDate" in api_response else None,
filter=cast(str, api_response["Filter"]
) if "Filter" in api_response else None,
status=cast(int, api_response["Status"]
)if "Status" in api_response else None,
worker_pid=cast(
int, api_response["WorkerPID"])if "WorkerPID" in api_response else None,
filter_with_deps=cast(bool, api_response["FilterWithDeps"]),
skip_component_check=cast(
bool, api_response["SkipComponentCheck"]),
Expand Down Expand Up @@ -73,7 +83,7 @@ def update(self, name: str, ignore_signatures: bool = False) -> Sequence[Mirror]
resp = self.do_put("api/mirrors/%s" % (quote(name)), json=body)
return resp

def edit(self, name: str, newname: Optional[str] = None, archiveroot: Optional[str] = None,
def edit(self, name: str, newname: Optional[str] = None, archiveurl: Optional[str] = None,
filter: Optional[str] = None, architectures: Optional[List[str]] = None,
components: Optional[List[str]] = None, keyrings: Optional[List[str]] = None,
filter_with_deps: bool = False, skip_existing_packages: bool = False,
Expand All @@ -84,8 +94,8 @@ def edit(self, name: str, newname: Optional[str] = None, archiveroot: Optional[s
body = {}
if newname:
body["Name"] = newname
if archiveroot:
body["ArchiveURL"] = archiveroot
if archiveurl:
body["ArchiveURL"] = archiveurl
if filter:
body["Filter"] = filter
if architectures:
Expand Down Expand Up @@ -140,15 +150,15 @@ def delete(self, name: str) -> Sequence[Mirror]:
resp = self.do_delete("api/mirrors/%s" % quote(name))
return resp

def create(self, name: str, archiveroot: str, distribution: Optional[str] = None,
def create(self, name: str, archiveurl: str, distribution: Optional[str] = None,
filter: Optional[str] = None, components: Optional[List[str]] = None,
architectures: Optional[List[str]] = None, keyrings: Optional[List[str]] = None,
download_sources: bool = False, download_udebs: bool = False,
download_installer: bool = False, filter_with_deps: bool = False,
skip_component_check: bool = False, ignore_signatures: bool = False) -> Mirror:
data = {
"Name": name,
"ArchiveURL": archiveroot
"ArchiveURL": archiveurl
}

if ignore_signatures:
Expand Down
1 change: 1 addition & 0 deletions aptly_api/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@
from .test_publish import * # noqa
from .test_repos import * # noqa
from .test_snapshots import * # noqa
from .test_mirrors import *
240 changes: 240 additions & 0 deletions aptly_api/tests/test_mirrors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
# -* encoding: utf-8 *-

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from typing import Any
from unittest.case import TestCase

import requests_mock

from aptly_api.base import AptlyAPIException
from aptly_api.parts.packages import Package
from aptly_api.parts.mirrors import MirrorsAPISection, Mirror


@requests_mock.Mocker(kw='rmock')
class MirrorsAPISectionTests(TestCase):
def __init__(self, *args: Any) -> None:
super().__init__(*args)
self.mapi = MirrorsAPISection("http://test/")

def test_create(self, *, rmock: requests_mock.Mocker) -> None:
rmock.post("http://test/api/mirrors",
text='{"UUID": "2cb5985a-a23f-4a1f-8eb6-d5409193b4eb", "Name": "aptly-mirror", "ArchiveRoot": "https://deb.nodesource.com/node_10.x/", "Distribution": "bionic", "Components": ["main"], "Architectures": ["amd64"], "Meta": {"Architectures": "i386 amd64 armhf arm64", "Codename": "bionic", "Components": "main", "Date": "Tue, 06 Apr 2021 21:05:41 UTC","Description": " Apt Repository for the Node.JS 10.x Branch", "Label": "Node Source", "Origin": "Node Source"}, "LastDownloadDate": "0001-01-01T00:00:00Z", "Filter": "test", "Status": 0, "WorkerPID": 0, "FilterWithDeps": true, "SkipComponentCheck": true, "SkipArchitectureCheck": true, "DownloadSources": true, "DownloadUdebs": true, "DownloadInstaller": true}'
)
self.assertEqual(
self.mapi.create(name="aptly-mirror", archiveurl='https://deb.nodesource.com/node_10.x/',
distribution='bionic', components=["main"],
architectures=["amd64"],
filter="test", download_udebs=True,
download_sources=True, download_installer=True,
skip_component_check=True, filter_with_deps=True,
keyrings="/path/to/keyring", ignore_signatures=True),
Mirror(
uuid='2cb5985a-a23f-4a1f-8eb6-d5409193b4eb',
name="aptly-mirror",
archiveurl="https://deb.nodesource.com/node_10.x/",
distribution='bionic',
components=["main"],
architectures=["amd64"],
downloaddate='0001-01-01T00:00:00Z',
meta={"Architectures": "i386 amd64 armhf arm64",
"Codename": "bionic",
"Components": "main",
"Date": "Tue, 06 Apr 2021 21:05:41 UTC",
"Description": " Apt Repository for the Node.JS 10.x Branch",
"Label": "Node Source", "Origin": "Node Source"},
filter="test",
status=0,
worker_pid=0,
filter_with_deps=True,
skip_component_check=True,
skip_architecture_check=True,
download_sources=True,
download_udebs=True,
download_installer=True

)
)

def test_list(self, *, rmock: requests_mock.Mocker) -> None:
rmock.get("http://test/api/mirrors",
text='[{"UUID": "2cb5985a-a23f-4a1f-8eb6-d5409193b4eb", "Name": "aptly-mirror", "ArchiveRoot": "https://deb.nodesource.com/node_10.x/", "Distribution": "bionic", "Components": ["main"], "Architectures": ["amd64"], "Meta": {"Architectures": "i386 amd64 armhf arm64", "Codename": "bionic", "Components": "main", "Date": "Tue, 06 Apr 2021 21:05:41 UTC","Description": " Apt Repository for the Node.JS 10.x Branch", "Label": "Node Source", "Origin": "Node Source"}, "LastDownloadDate": "0001-01-01T00:00:00Z", "Filter": "", "Status": 0, "WorkerPID": 0, "FilterWithDeps": false, "SkipComponentCheck": false, "SkipArchitectureCheck": false, "DownloadSources": false, "DownloadUdebs": false, "DownloadInstaller": false}]'
)
self.assertSequenceEqual(
self.mapi.list(),
[
Mirror(
uuid='2cb5985a-a23f-4a1f-8eb6-d5409193b4eb',
name="aptly-mirror",
archiveurl="https://deb.nodesource.com/node_10.x/",
distribution='bionic',
components=["main"],
architectures=["amd64"],
downloaddate='0001-01-01T00:00:00Z',
meta={"Architectures": "i386 amd64 armhf arm64",
"Codename": "bionic",
"Components": "main",
"Date": "Tue, 06 Apr 2021 21:05:41 UTC",
"Description": " Apt Repository for the Node.JS 10.x Branch",
"Label": "Node Source", "Origin": "Node Source"},
filter="",
status=0,
worker_pid=0,
filter_with_deps=False,
skip_component_check=False,
skip_architecture_check=False,
download_sources=False,
download_udebs=False,
download_installer=False

)
]
)

def test_show(self, *, rmock: requests_mock.Mocker) -> None:
rmock.get("http://test/api/mirrors/aptly-mirror",
text='{"UUID": "2cb5985a-a23f-4a1f-8eb6-d5409193b4eb", "Name": "aptly-mirror", "ArchiveRoot": "https://deb.nodesource.com/node_10.x/", "Distribution": "bionic", "Components": ["main"], "Architectures": ["amd64"], "Meta": {"Architectures": "i386 amd64 armhf arm64", "Codename": "bionic", "Components": "main", "Date": "Tue, 06 Apr 2021 21:05:41 UTC","Description": " Apt Repository for the Node.JS 10.x Branch", "Label": "Node Source", "Origin": "Node Source"}, "LastDownloadDate": "0001-01-01T00:00:00Z", "Filter": "", "Status": 0, "WorkerPID": 0, "FilterWithDeps": false, "SkipComponentCheck": false, "SkipArchitectureCheck": false, "DownloadSources": false, "DownloadUdebs": false, "DownloadInstaller": false}'
)
self.assertEqual(
self.mapi.show(name="aptly-mirror"),
Mirror(
uuid='2cb5985a-a23f-4a1f-8eb6-d5409193b4eb',
name="aptly-mirror",
archiveurl="https://deb.nodesource.com/node_10.x/",
distribution='bionic',
components=["main"],
architectures=["amd64"],
downloaddate='0001-01-01T00:00:00Z',
meta={"Architectures": "i386 amd64 armhf arm64",
"Codename": "bionic",
"Components": "main",
"Date": "Tue, 06 Apr 2021 21:05:41 UTC",
"Description": " Apt Repository for the Node.JS 10.x Branch",
"Label": "Node Source", "Origin": "Node Source"},
filter="",
status=0,
worker_pid=0,
filter_with_deps=False,
skip_component_check=False,
skip_architecture_check=False,
download_sources=False,
download_udebs=False,
download_installer=False

)
)

def test_list_packages(self, *, rmock: requests_mock.Mocker) -> None:
rmock.get("http://test/api/mirrors/aptly-mirror/packages",
text='["Pamd64 nodejs 10.24.1-1nodesource1 1f74a6abf6acc572"]')
self.assertSequenceEqual(
self.mapi.list_packages(
name="aptly-mirror", query=("nodejs"), with_deps=True),
[
Package(
key="Pamd64 nodejs 10.24.1-1nodesource1 1f74a6abf6acc572",
short_key=None,
files_hash=None,
fields=None,
)
],
)

def test_list_packages_details(self, *, rmock: requests_mock.Mocker) -> None:
rmock.get(
"http://test/api/mirrors/aptly-mirror/packages?format=details",
text="""[{
"Architecture":"amd64",
"Conflicts": "nodejs-dev, nodejs-legacy, npm",
"Depends":"1libc6 (>= 2.9), libgcc1 (>= 1:3.4), libstdc++6 (>= 4.4.0), python-minimal, ca-certificates",
"Description":"Node.js event-based server-side javascript engine\\n",
"Filename":"nodejs_10.24.1-1nodesource1_amd64.deb",
"FilesHash":"1f74a6abf6acc572",
"Homepage":"https://nodejs.org",
"Installed-Size":"78630",
"Key":"Pamd64 nodejs 10.24.1-1nodesource1 1f74a6abf6acc572",
"License":"unknown",
"MD5sum":"6d9f0e30396cb6c20945ff6de2f9f322",
"Maintainer":"Ivan Iguaran <[email protected]>",
"Package":"nodejs",
"Priority":"optional",
"Provides":"nodejs-dev, nodejs-legacy, npm",
"SHA1":"a3bc5a29614eab366bb3644abb1e602b5c8953d5",
"SHA256":"4b374d16b536cf1a3963ddc4575ed2b68b28b0b5ea6eefe93c942dfc0ed35177",
"SHA512":"bf203bb319de0c5f7ed3b6ba69de39b1ea8b5086b872561379bd462dd93f07969ca64fa01ade01ff08fa13a4e5e28625b59292ba44bc01ba876ec95875630460",
"Section":"web",
"ShortKey":"Pamd64 nodejs 10.24.1-1nodesource1",
"Size":"15949164",
"Version":"10.24.1-1nodesource1"
}]"""
)
self.assertEqual(
self.mapi.list_packages(
"aptly-mirror", detailed=True, with_deps=True, query="nodejs"),
[
Package(
key='Pamd64 nodejs 10.24.1-1nodesource1 1f74a6abf6acc572',
short_key='Pamd64 nodejs 10.24.1-1nodesource1',
files_hash='1f74a6abf6acc572',
fields={
'Architecture': 'amd64',
'Conflicts': 'nodejs-dev, nodejs-legacy, npm',
'Depends': '1libc6 (>= 2.9), libgcc1 (>= 1:3.4), libstdc++6 (>= 4.4.0), python-minimal, ca-certificates',
'Description': 'Node.js event-based server-side javascript engine\n',
'Filename': 'nodejs_10.24.1-1nodesource1_amd64.deb',
'FilesHash': '1f74a6abf6acc572',
'Homepage': 'https://nodejs.org',
'Installed-Size': '78630',
'Key': 'Pamd64 nodejs 10.24.1-1nodesource1 1f74a6abf6acc572',
'License': 'unknown',
'MD5sum': '6d9f0e30396cb6c20945ff6de2f9f322',
'Maintainer': 'Ivan Iguaran <[email protected]>',
'Package': 'nodejs',
'Priority': 'optional',
'Provides': 'nodejs-dev, nodejs-legacy, npm',
'SHA1': 'a3bc5a29614eab366bb3644abb1e602b5c8953d5',
'SHA256': '4b374d16b536cf1a3963ddc4575ed2b68b28b0b5ea6eefe93c942dfc0ed35177',
'SHA512': 'bf203bb319de0c5f7ed3b6ba69de39b1ea8b5086b872561379bd462dd93f0796'
'9ca64fa01ade01ff08fa13a4e5e28625b59292ba44bc01ba876ec95875630460',
'Section': 'web',
'ShortKey': 'Pamd64 nodejs 10.24.1-1nodesource1',
'Size': '15949164',
'Version': '10.24.1-1nodesource1'
}
)
]
)

def test_delete(self, *, rmock: requests_mock.Mocker) -> None:
with self.assertRaises(requests_mock.NoMockAddress):
self.mapi.delete(name="aptly-mirror")

def test_update(self, *, rmock: requests_mock.Mocker) -> None:
with self.assertRaises(requests_mock.NoMockAddress):
self.mapi.update(name="aptly-mirror", ignore_signatures=True)

def test_edit(self, *, rmock: requests_mock.Mocker) -> None:
with self.assertRaises(requests_mock.NoMockAddress):
self.mapi.edit(name="aptly-mirror", newname="aptly-mirror-renamed",
archiveurl='https://deb.nodesource.com/node_10.x/',
architectures=["i386", "amd64"], filter="test",
components=["main"], keyrings="/path/to/keyring",
skip_existing_packages=True, ignore_checksums=True,
download_udebs=True, download_sources=True,
skip_component_check=True, filter_with_deps=True,
ignore_signatures=True, force_update=True),

def test_delete_validation(self, *, rmock: requests_mock.Mocker) -> None:
rmock.delete("http://test/api/mirrors/aptly-mirror")
self.mapi.delete(name="aptly-mirror")

def test_update_validation(self, *, rmock: requests_mock.Mocker) -> None:
rmock.put("http://test/api/mirrors/aptly-mirror")
self.mapi.update(name="aptly-mirror")

def test_edit_validation(self, *, rmock: requests_mock.Mocker) -> None:
rmock.put("http://test/api/mirrors/aptly-mirror",
text='{"Name":"aptly-mirror-bla", "IgnoreSignatures": true}')
self.mapi.edit(name="aptly-mirror", newname="aptly-mirror-renamed")
Loading

0 comments on commit 74488b9

Please sign in to comment.