Skip to content

Commit 71e19b4

Browse files
author
Greg Land
committed
Corrected error in mirror.create filter_with_deps
* Fixed Mirror.Create. It didn't make use of filter_with_deps argument. * Organized Mirror.create statements to make them easier to follow. * Created unit tests for Mirror.Create to make sure post request is created correctly. * Renamed misleading test named test_create to test_mirror_from_response and cleaned up the function.
1 parent 9199346 commit 71e19b4

File tree

2 files changed

+109
-44
lines changed

2 files changed

+109
-44
lines changed

aptly_api/parts/mirrors.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,20 @@ def mirror_from_response(api_response: Dict[str, str]) -> Mirror:
5959
) if "Filter" in api_response else None,
6060
status=cast(int, api_response["Status"]
6161
)if "Status" in api_response else None,
62-
worker_pid=cast(
63-
int, api_response["WorkerPID"])
64-
if "WorkerPID" in api_response else None,
65-
filter_with_deps=cast(bool, api_response["FilterWithDeps"]),
66-
skip_component_check=cast(
67-
bool, api_response["SkipComponentCheck"]),
68-
skip_architecture_check=cast(
69-
bool, api_response["SkipArchitectureCheck"]),
70-
download_sources=cast(bool, api_response["DownloadSources"]),
71-
download_udebs=cast(bool, api_response["DownloadUdebs"]),
72-
download_installer=cast(bool, api_response["DownloadInstaller"])
62+
worker_pid=cast( int, api_response["WorkerPID"]
63+
) if "WorkerPID" in api_response else None,
64+
filter_with_deps=cast(bool, api_response["FilterWithDeps"]
65+
) if "FilterWithDeps" in api_response else False,
66+
skip_component_check=cast(bool, api_response["SkipComponentCheck"]
67+
) if "SkipComponentCheck" in api_response else False,
68+
skip_architecture_check=cast(bool, api_response["SkipArchitectureCheck"]
69+
) if "SkipArchitectureCheck" in api_response else False,
70+
download_sources=cast(bool, api_response["DownloadSources"]
71+
) if "DownloadSources" in api_response else False,
72+
download_udebs=cast(bool, api_response["DownloadUdebs"]
73+
) if "DownloadUdebs" in api_response else False,
74+
download_installer=cast(bool, api_response["DownloadInstaller"]
75+
) if "DownloadInstaller" in api_response else False,
7376
)
7477

7578
def list(self) -> Sequence[Mirror]:
@@ -158,32 +161,37 @@ def create(self, name: str, archiveurl: str, distribution: Optional[str] = None,
158161
architectures: Optional[List[str]] = None, keyrings: Optional[List[str]] = None,
159162
download_sources: bool = False, download_udebs: bool = False,
160163
download_installer: bool = False, filter_with_deps: bool = False,
161-
skip_component_check: bool = False, ignore_signatures: bool = False) -> Mirror:
164+
skip_component_check: bool = False, skip_architecture_check: bool = False,
165+
ignore_signatures: bool = False) -> Mirror:
162166
data = {
163167
"Name": name,
164168
"ArchiveURL": archiveurl
165169
} # type: T_BodyDict
166170

167-
if ignore_signatures:
168-
data["IgnoreSignatures"] = ignore_signatures
169-
if keyrings:
170-
data["Keyrings"] = keyrings
171-
if filter:
172-
data["Filter"] = filter
173171
if distribution:
174172
data["Distribution"] = distribution
173+
if filter:
174+
data["Filter"] = filter
175175
if components:
176176
data["Components"] = components
177177
if architectures:
178178
data["Architectures"] = architectures
179+
if keyrings:
180+
data["Keyrings"] = keyrings
179181
if download_sources:
180182
data["DownloadSources"] = download_sources
181183
if download_udebs:
182184
data["DownloadUdebs"] = download_udebs
183185
if download_installer:
184186
data["DownloadInstaller"] = download_installer
187+
if filter_with_deps:
188+
data["FilterWithDeps"] = filter_with_deps
185189
if skip_component_check:
186190
data["SkipComponentCheck"] = skip_component_check
191+
if skip_architecture_check:
192+
data["SkipArchitectureCheck"] = skip_architecture_check
193+
if ignore_signatures:
194+
data["IgnoreSignatures"] = ignore_signatures
187195

188196
resp = self.do_post("api/mirrors", json=data)
189197

aptly_api/tests/test_mirrors.py

Lines changed: 83 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
from typing import Any
77
from unittest.case import TestCase
8+
from inspect import signature
9+
import json
810

911
import requests_mock
1012

@@ -19,38 +21,93 @@ def __init__(self, *args: Any) -> None:
1921
self.miapi = MirrorsAPISection("http://test/")
2022

2123
def test_create(self, *, rmock: requests_mock.Mocker) -> None:
22-
rmock.post("http://test/api/mirrors",
23-
text="""{"UUID": "2cb5985a-a23f-4a1f-8eb6-d5409193b4eb",
24-
"Name": "aptly-mirror",
25-
"ArchiveRoot": "https://deb.nodesource.com/node_10.x/",
26-
"Distribution": "bionic", "Components": ["main"],
27-
"Architectures": ["amd64"],
28-
"Meta": [{"Architectures": "i386 amd64 armhf arm64",
29-
"Codename": "bionic", "Components": "main",
30-
"Date": "Tue, 06 Apr 2021 21:05:41 UTC",
31-
"Description": " Apt Repository for the Node.JS 10.x Branch",
32-
"Label": "Node Source", "Origin": "Node Source"}],
33-
"LastDownloadDate": "0001-01-01T00:00:00Z",
34-
"Filter": "test", "Status": 0, "WorkerPID": 0,
35-
"FilterWithDeps": true, "SkipComponentCheck": true,
36-
"SkipArchitectureCheck": true, "DownloadSources": true,
37-
"DownloadUdebs": true, "DownloadInstaller": true}"""
38-
)
24+
expected = {"Name": "testname", "ArchiveURL": "http://randomurl.url"}
25+
26+
rmock.post("http://test/api/mirrors", text="""{"Name":"nocheck", "ArchiveRoot":"nocheck"}""")
27+
self.miapi.create(expected["Name"], expected["ArchiveURL"])
28+
29+
self.assertEqual(rmock.request_history[0].method, "POST")
30+
self.assertEqual(len(rmock.request_history[0].json()), len(expected))
31+
self.assertEqual(rmock.request_history[0].json(), expected)
32+
33+
def test_create_all_args(self, *, rmock: requests_mock.Mocker) -> None:
34+
expected = {
35+
"Name": "aptly-mirror",
36+
"ArchiveURL": "https://deb.nodesource.com/node_10.x/",
37+
"Distribution": "bionic",
38+
"Filter": "test",
39+
"Components": ["main"],
40+
"Architectures": ["amd64"],
41+
"Keyrings": ["/path/to/keyring"],
42+
"DownloadSources": True,
43+
"DownloadUdebs": True,
44+
"DownloadInstaller": True,
45+
"FilterWithDeps": True,
46+
"SkipComponentCheck": True,
47+
"SkipArchitectureCheck": True,
48+
"IgnoreSignatures": True,
49+
}
50+
# Keep us from getting out of lockstep with the number of args to create
51+
self.assertEqual(len(signature(self.miapi.create).parameters), len(expected))
52+
53+
rmock.post("http://test/api/mirrors", text="""{"Name":"nocheck", "ArchiveRoot":"nocheck"}""")
54+
self.miapi.create(
55+
name="aptly-mirror",
56+
archiveurl="https://deb.nodesource.com/node_10.x/",
57+
distribution="bionic",
58+
filter="test",
59+
components=["main"],
60+
architectures=["amd64"],
61+
keyrings=["/path/to/keyring"],
62+
download_sources=True,
63+
download_udebs=True,
64+
download_installer=True,
65+
filter_with_deps=True,
66+
skip_component_check=True,
67+
skip_architecture_check=True,
68+
ignore_signatures=True,
69+
)
70+
71+
self.assertEqual(rmock.request_history[0].method, "POST")
72+
self.assertEqual(len(rmock.request_history[0].json()), len(expected))
73+
self.assertEqual(rmock.request_history[0].json(), expected)
74+
75+
def test_mirror_from_response(self, *, rmock: requests_mock.Mocker) -> None:
3976
self.assertSequenceEqual(
40-
self.miapi.create(name="aptly-mirror", archiveurl='https://deb.nodesource.com/node_10.x/',
41-
distribution='bionic', components=["main"],
42-
architectures=["amd64"],
43-
filter="test", download_udebs=True,
44-
download_sources=True, download_installer=True,
45-
skip_component_check=True, filter_with_deps=True,
46-
keyrings=["/path/to/keyring"], ignore_signatures=True),
77+
self.miapi.mirror_from_response(
78+
json.loads("""{
79+
"UUID": "2cb5985a-a23f-4a1f-8eb6-d5409193b4eb",
80+
"Name": "aptly-mirror",
81+
"ArchiveRoot": "https://deb.nodesource.com/node_10.x/",
82+
"Distribution": "bionic",
83+
"Components": ["main"],
84+
"Architectures": ["amd64"],
85+
"LastDownloadDate": "0001-01-01T00:00:00Z",
86+
"Meta": [{"Architectures": "i386 amd64 armhf arm64",
87+
"Codename": "bionic",
88+
"Components": "main",
89+
"Date": "Tue, 06 Apr 2021 21:05:41 UTC",
90+
"Description": " Apt Repository for the Node.JS 10.x Branch",
91+
"Label": "Node Source",
92+
"Origin": "Node Source"}],
93+
"Filter": "test",
94+
"Status": 0,
95+
"WorkerPID": 0,
96+
"FilterWithDeps": true,
97+
"SkipComponentCheck": true,
98+
"SkipArchitectureCheck": true,
99+
"DownloadSources": true,
100+
"DownloadUdebs": true,
101+
"DownloadInstaller": true
102+
}""")
103+
),
47104
Mirror(
48105
uuid='2cb5985a-a23f-4a1f-8eb6-d5409193b4eb',
49106
name="aptly-mirror",
50107
archiveurl="https://deb.nodesource.com/node_10.x/",
51108
distribution='bionic',
52-
components=["main"],
53-
architectures=["amd64"],
109+
components=['main'],
110+
architectures=['amd64'],
54111
downloaddate='0001-01-01T00:00:00Z',
55112
meta=[{"Architectures": "i386 amd64 armhf arm64",
56113
"Codename": "bionic",

0 commit comments

Comments
 (0)