Skip to content

Commit

Permalink
Merge ce62d74 into e87da3b
Browse files Browse the repository at this point in the history
  • Loading branch information
lipoja committed Feb 25, 2020
2 parents e87da3b + ce62d74 commit 810fcd6
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 39 deletions.
81 changes: 62 additions & 19 deletions iiblib/iibclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def make_auth(self, iib_session): # pragma: no cover


class IIBBuildDetailsModel(object):
"""Model class hodling data about index build task"""
"""Model class handling data about index build task"""

def __init__(
self,
Expand All @@ -28,16 +28,18 @@ def __init__(
from_index,
from_index_resolved,
bundles,
operators,
removed_operators,
organization,
binary_image,
binary_image_resolved,
index_image,
request_type,
arches,
bundle_mapping,
):
"""
Args:
_id (str)
_id (int)
Id of build
state (str)
State of build
Expand All @@ -49,18 +51,23 @@ def __init__(
Reference of new index image
bundles (list)
List of bundles to be added to index image
operators (list)
removed_operators (list)
List of operators to be removed from index image
organization (str)
Name of organization to push to in the legacy app registry
binary_image (str)
Reference of binary image used for rebuilding
binary_image_resolved (str)
Checksum refence of binary image that was used for rebuilding
Checksum reference of binary image that was used for rebuilding
index_image (str)
Reference of index image to rebuild
request_type (str)
Type of iib build task (add or remove)
arches (list)
List of archictures supported in new index image
List of architectures supported in new index image
bundle_mapping (dict)
Operator names in "bundles" map to: list of "bundles" which
map to the operator key
"""
self.id = _id
self.state = state
Expand All @@ -69,12 +76,14 @@ def __init__(
self.from_index = from_index
self.from_index_resolved = from_index_resolved
self.bundles = bundles
self.operators = operators
self.removed_operators = removed_operators
self.organization = organization
self.binary_image = binary_image
self.binary_image_resolved = binary_image_resolved
self.index_image = index_image
self.request_type = request_type
self.arches = arches
self.bundle_mapping = bundle_mapping

@classmethod
def from_dict(cls, data):
Expand All @@ -86,12 +95,14 @@ def from_dict(cls, data):
data["from_index"],
data["from_index_resolved"],
data.get("bundles", []),
data.get("operators", []),
data.get("removed_operators", []),
data.get("organization"),
data["binary_image"],
data["binary_image_resolved"],
data["index_image"],
data["request_type"],
data["arches"],
data["bundle_mapping"],
)

def __eq__(self, other):
Expand All @@ -103,12 +114,14 @@ def __eq__(self, other):
and self.from_index == other.from_index
and self.from_index_resolved == other.from_index_resolved
and self.bundles == other.bundles
and self.operators == other.operators
and self.removed_operators == other.removed_operators
and self.organization == other.organization
and self.binary_image == other.binary_image
and self.binary_image_resolved == other.binary_image_resolved
and self.index_image == other.index_image
and self.request_type == other.request_type
and self.arches == other.arches
and self.bundle_mapping == other.bundle_mapping
):
return True
return False
Expand Down Expand Up @@ -305,7 +318,7 @@ def _api_url(self, endpoint):
requests.Response
"""

return "https://%s/api/v1/%s/" % (self.hostname, endpoint)
return "https://%s/api/v1/%s" % (self.hostname, endpoint)


# pylint: disable=bad-option-value,useless-object-inheritance
Expand All @@ -329,7 +342,16 @@ def __init__(self, hostname, retries=3, auth=None, poll_interval=30):
if auth:
auth.make_auth(self.iib_session)

def add_bundles(self, index_image, binary_image, bundles, arches, raw=False):
def add_bundles(
self,
index_image,
binary_image,
bundles,
arches,
cnr_token=None,
organization=None,
raw=False
):
"""Rebuild index image with new bundles to be added.
Args:
Expand All @@ -341,6 +363,10 @@ def add_bundles(self, index_image, binary_image, bundles, arches, raw=False):
List of references to bundle images to be added to index image
arches (list)
List of architectures supported in new index image
cnr_token (srt)
optional. CNR token.
organization (str)
optional. Name of the organization in the legacy app registry.
raw (bool)
Return raw json response instead of model instance
Expand All @@ -352,19 +378,30 @@ def add_bundles(self, index_image, binary_image, bundles, arches, raw=False):

resp = self.iib_session.post(
"builds/add",
data={
"index_image": index_image,
json={
"from_index": index_image,
"binary_image": binary_image,
"bundles": bundles,
"arches": arches,
"add_arches": arches,
"cnr_token": cnr_token,
"organization": organization,
},
)
resp.raise_for_status()
if raw:
return resp.json()
return IIBBuildDetailsModel.from_dict(resp.json())

def remove_operators(self, index_image, binary_image, operators, arches, raw=False):
def remove_operators(
self,
index_image,
binary_image,
operators,
arches,
cnr_token=None,
organization=None,
raw=False
):
"""Rebuild index image with existing operators to be removed.
Args:
Expand All @@ -376,6 +413,10 @@ def remove_operators(self, index_image, binary_image, operators, arches, raw=Fal
List of operators to be removed from existing index image
arches (list)
List of architectures supported in new index image
cnr_token (srt)
optional. CNR token.
organization (str)
optional. Name of the organization in the legacy app registry.
raw (bool)
Return raw json response instead of model instance
Expand All @@ -386,12 +427,14 @@ def remove_operators(self, index_image, binary_image, operators, arches, raw=Fal
"""

resp = self.iib_session.post(
"builds/remove",
data={
"index_image": index_image,
"builds/rm",
json={
"from_index": index_image,
"binary_image": binary_image,
"operators": operators,
"arches": arches,
"add_arches": arches,
"cnr_token": cnr_token,
"organization": organization,
},
)
resp.raise_for_status()
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
requests
requests>=2.4.2
requests-gssapi
gssapi
six
46 changes: 27 additions & 19 deletions tests/test_iib_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ def fixture_build_details_json():
"from_index": "from_index",
"from_index_resolved": "from_index_resolved",
"bundles": ["bundles1"],
"operators": ["operator1"],
"removed_operators": ["operator1"],
"organization": "organization",
"binary_image": "binary_image",
"binary_image_resolved": "binary_image_resolved",
"index_image": "index_image",
"request_type": "request_type",
"arches": ["x86_64"],
"bundle_mapping": {"bundle_mapping": "map"},
}
return json

Expand All @@ -46,12 +48,14 @@ def fixture_build_details_json2():
"from_index": "from_index",
"from_index_resolved": "from_index_resolved",
"bundles": ["bundles1"],
"operators": [],
"removed_operators": ['operator1'],
"organization": "organization",
"binary_image": "binary_image",
"binary_image_resolved": "binary_image_resolved",
"index_image": "index_image",
"request_type": "request_type",
"arches": ["x86_64"],
"bundle_mapping": {"bundle_mapping": "map"},
}
return json

Expand Down Expand Up @@ -103,46 +107,46 @@ def test_iib_session_methods(patched_delete, patched_put, patched_post, patched_
iibs.put("fake-end-point")
iibs.delete("fake-end-point")

patched_get.assert_called_with("https://fake-host/api/v1/fake-end-point/")
patched_post.assert_called_with("https://fake-host/api/v1/fake-end-point/")
patched_put.assert_called_with("https://fake-host/api/v1/fake-end-point/")
patched_delete.assert_called_with("https://fake-host/api/v1/fake-end-point/")
patched_get.assert_called_with("https://fake-host/api/v1/fake-end-point")
patched_post.assert_called_with("https://fake-host/api/v1/fake-end-point")
patched_put.assert_called_with("https://fake-host/api/v1/fake-end-point")
patched_delete.assert_called_with("https://fake-host/api/v1/fake-end-point")


def test_iib_client(fixture_build_details_json, fixture_builds_page1_json):
with requests_mock.Mocker() as m:
m.register_uri(
"POST",
"/api/v1/builds/add/",
"/api/v1/builds/add",
status_code=200,
json=fixture_build_details_json,
)
m.register_uri(
"POST",
"/api/v1/builds/remove/",
"/api/v1/builds/rm",
status_code=200,
json=fixture_build_details_json,
)
m.register_uri(
"GET", "/api/v1/builds/", status_code=200, json=fixture_builds_page1_json
"GET", "/api/v1/builds", status_code=200, json=fixture_builds_page1_json
)
m.register_uri(
"GET", "/api/v1/builds/1/", status_code=200, json=fixture_build_details_json
"GET", "/api/v1/builds/1", status_code=200, json=fixture_build_details_json
)

iibc = IIBClient("fake-host")
assert iibc.add_bundles(
"index-image", "binary", "bundles-map", {}
"index-image", "binary", ["bundles-map"], []
) == IIBBuildDetailsModel.from_dict(fixture_build_details_json)
assert (
iibc.add_bundles("index-image", "binary", "bundles-map", {}, raw=True)
iibc.add_bundles("index-image", "binary", ["bundles-map"], [], raw=True)
== fixture_build_details_json
)
assert iibc.remove_operators(
"index-image", "binary", ["operator1"], {}
"index-image", "binary", ["operator1"], []
) == IIBBuildDetailsModel.from_dict(fixture_build_details_json)
assert (
iibc.remove_operators("index-image", "binary", ["operator1"], {}, raw=True)
iibc.remove_operators("index-image", "binary", ["operator1"], [], raw=True)
== fixture_build_details_json
)
assert iibc.get_build(1) == IIBBuildDetailsModel.from_dict(
Expand All @@ -163,7 +167,7 @@ def test_client_wait_for_build(fixture_build_details_json):
with requests_mock.Mocker() as m:
m.register_uri(
"GET",
"/api/v1/builds/1/",
"/api/v1/builds/1",
[
{"json": fixture_build_details_json, "status_code": 200},
{"json": bdetails_finished, "status_code": 200},
Expand All @@ -175,7 +179,7 @@ def test_client_wait_for_build(fixture_build_details_json):
with requests_mock.Mocker() as m:
m.register_uri(
"GET",
"/api/v1/builds/1/",
"/api/v1/builds/1",
[
{"json": fixture_build_details_json, "status_code": 200},
{"json": bdetails_finished, "status_code": 200},
Expand Down Expand Up @@ -250,11 +254,13 @@ def test_iibbuilddetailsmodel(fixture_build_details_json):
"from_index_resolved",
["bundles1"],
["operator1"],
"organization",
"binary_image",
"binary_image_resolved",
"index_image",
"request_type",
["x86_64"],
{"bundle_mapping": "map"},
)
expected_model = IIBBuildDetailsModel(
1,
Expand All @@ -265,11 +271,13 @@ def test_iibbuilddetailsmodel(fixture_build_details_json):
"from_index_resolved",
["bundles1"],
["operator1"],
"organization",
"binary_image",
"binary_image_resolved",
"index_image",
"request_type",
["x86_64"],
{"bundle_mapping": "map"},
)
model = IIBBuildDetailsModel.from_dict(fixture_build_details_json)
assert model == expected_model
Expand All @@ -284,17 +292,17 @@ def test_iibbuilddetails_pager(
):
with requests_mock.Mocker() as m:
m.register_uri(
"GET", "/api/v1/builds/", status_code=200, json=fixture_builds_page1_json
"GET", "/api/v1/builds", status_code=200, json=fixture_builds_page1_json
)
m.register_uri(
"GET",
"/api/v1/builds/?page=1",
"/api/v1/builds?page=1",
status_code=200,
json=fixture_builds_page2_json,
)
m.register_uri(
"GET",
"/api/v1/builds/?page=0",
"/api/v1/builds?page=0",
status_code=200,
json=fixture_builds_page1_json,
)
Expand Down

0 comments on commit 810fcd6

Please sign in to comment.