Skip to content

Commit

Permalink
Separate test and modify docs
Browse files Browse the repository at this point in the history
  • Loading branch information
pbortlov committed Oct 12, 2020
1 parent 3f0b209 commit 9666611
Show file tree
Hide file tree
Showing 10 changed files with 327 additions and 204 deletions.
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ Basic usage of IIBClient from iiblib is following
::

$ python
>>> from iiblib.iibclient import IIBClient, IIBKrbAuth
>>> from iiblib.iib_client import IIBClient
>>> from iiblib.iib_authentication import IIBKrbAuth
>>> krbauth = IIBKrbAuth()
>>> iibc = IIBClient('iib-host', auth=krbauth)
>>> build = iibc.add_bundles('index_image', 'binary_image', ['bundle1','bundle2'], ['amd64'])
Expand Down
6 changes: 5 additions & 1 deletion docs/source/modules/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ References
.. toctree::
:maxdepth: 3

.. automodule:: iiblib.iibclient
.. automodule:: iiblib.iib_client
.. automodule:: iiblib.iib_authentication
.. automodule:: iiblib.iib_build_details_pager
.. automodule:: iiblib.iib_build_details_model
.. automodule:: iiblib.iib_session
:members:
:show-inheritance:
:inherited-members:
Expand Down
4 changes: 3 additions & 1 deletion iiblib/iib_authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import subprocess
import tempfile


# pylint: disable=bad-option-value,useless-object-inheritance
class IIBAuth(object):
def __init__(self):
Expand Down Expand Up @@ -62,7 +63,8 @@ def _krb_auth_header(self):
).wait()
krb5ccname = None
if retcode or self.ktfile:
old_krb5ccname = os.environ.get("KRB5CCNAME", "") # can I define it on the higher level out of if?
# can I define old_krb5ccname on the higher level out of if?
old_krb5ccname = os.environ.get("KRB5CCNAME", "")
_, krb5ccname = tempfile.mkstemp(prefix="krb5cc")
if self.ktfile:
retcode = subprocess.Popen(
Expand Down
2 changes: 1 addition & 1 deletion iiblib/iib_build_details_pager.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def from_dict(cls, iibclient, _dict):

def __eq__(self, other):
return (
self._items == other._items # can I rathe use function self.items?
self._items == other._items # can I rather use function self.items?
and self.iibclient == other.iibclient
and self.meta == other.meta
)
1 change: 1 addition & 0 deletions iiblib/iib_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

class IIBException(Exception):
""" General IIB exception"""

pass


Expand Down
86 changes: 86 additions & 0 deletions tests/test_iib_authentication.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
from mock import patch, MagicMock, call

from iiblib.iib_authentication import IIBAuth, IIBBasicAuth, IIBKrbAuth
from iiblib.iib_client import IIBClient


def test_client_auth():
auth = IIBBasicAuth("foo", "bar")
iibc = IIBClient("fake-host", auth=auth)
assert iibc.iib_session.session.headers["auth"] == ("foo", "bar")


def test_iib_basic_auth():
session = MagicMock()
session.session.headers = {}
auth = IIBBasicAuth("foo", "bar")
auth.make_auth(session)
assert session.session.headers["auth"] == ("foo", "bar")


@patch("subprocess.Popen")
@patch("kerberos.authGSSClientStep")
@patch("kerberos.authGSSClientResponse")
@patch("kerberos.authGSSClientInit")
def test_iib_krb_auth(
mocked_auth_gss_client_init,
mocked_auth_gss_client_response,
mocked_auth_gss_client_step,
mocked_popen,
):
mocked_auth_gss_client_init.return_value = ("", None)
mocked_auth_gss_client_response.return_value = ""
session = MagicMock()
session.session.headers = {}
auth = IIBKrbAuth("test_principal", "someservice")
auth.make_auth(session)
mocked_auth_gss_client_init.assert_called_with("HTTP@someservice")
mocked_popen.assert_has_calls([call(["klist"], stderr=-1, stdout=-1)])

auth = IIBKrbAuth("test_principal", "someservice", ktfile="/some/kt/file")
auth.make_auth(session)
mocked_auth_gss_client_init.assert_called_with("HTTP@someservice")


@patch("os.unlink")
@patch("tempfile.mkstemp")
@patch("subprocess.Popen.wait")
@patch("subprocess.Popen")
@patch("kerberos.authGSSClientStep")
@patch("kerberos.authGSSClientResponse")
@patch("kerberos.authGSSClientInit")
def test_iib_krb_auth_no_keytab(
mocked_auth_gss_client_init,
mocked_auth_gss_client_response,
mocked_auth_gss_client_step,
mocked_popen,
mocked_popen_wait,
mocked_mkstemp,
mocked_os_unlink,
):
mocked_mkstemp.return_value = (None, "/tmp/krb5ccomuHss")
mocked_popen_wait.side_effect = [1, 0]
mocked_auth_gss_client_init.return_value = ("", None)
mocked_auth_gss_client_response.return_value = ""
session = MagicMock()
session.session.headers = {}
auth = IIBKrbAuth("test_principal", "someservice")
auth.make_auth(session)
mocked_auth_gss_client_init.assert_called_with("HTTP@someservice")
mocked_popen.assert_has_calls(
[
call(
["kinit", "test_principal", "-k", "-c", "/tmp/krb5ccomuHss"],
stderr=-1,
stdout=-1,
)
]
)


def test_iibauth_abstract():
try:
IIBAuth()
raise AssertionError("Should raise NotImplementedError")
except NotImplementedError:
pass
72 changes: 72 additions & 0 deletions tests/test_iib_build_details_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import pytest

from iiblib.iib_build_details_model import IIBBuildDetailsModel


@pytest.fixture
def fixture_build_details_json():
json = {
"id": 1,
"state": "in_progress",
"state_reason": "state_reason",
"state_history": [],
"from_index": "from_index",
"from_index_resolved": "from_index_resolved",
"bundles": ["bundles1"],
"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"},
"omps_operator_version": {"operator": "1.0"},
}
return json


def test_iibbuilddetailsmodel(fixture_build_details_json):
unexpected_model = IIBBuildDetailsModel(
1,
"finished",
"state_reason",
[],
"from_index",
"from_index_resolved",
["bundles1"],
["operator1"],
"organization",
"binary_image",
"binary_image_resolved",
"index_image",
"request_type",
["x86_64"],
{"bundle_mapping": "map"},
{"operator": "1.0"},
)
expected_model = IIBBuildDetailsModel(
1,
"in_progress",
"state_reason",
[],
"from_index",
"from_index_resolved",
["bundles1"],
["operator1"],
"organization",
"binary_image",
"binary_image_resolved",
"index_image",
"request_type",
["x86_64"],
{"bundle_mapping": "map"},
{"operator": "1.0"},
)
model = IIBBuildDetailsModel.from_dict(fixture_build_details_json)
assert model == expected_model
assert model != unexpected_model

model = IIBBuildDetailsModel.from_dict(fixture_build_details_json).to_dict()
assert model == expected_model.to_dict()
assert model != unexpected_model.to_dict()
126 changes: 126 additions & 0 deletions tests/test_iib_build_details_pager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import pytest
import requests_mock


from iiblib.iib_build_details_model import IIBBuildDetailsModel
from iiblib.iib_client import IIBClient


@pytest.fixture
def fixture_build_details_json():
json = {
"id": 1,
"state": "in_progress",
"state_reason": "state_reason",
"state_history": [],
"from_index": "from_index",
"from_index_resolved": "from_index_resolved",
"bundles": ["bundles1"],
"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"},
"omps_operator_version": {"operator": "1.0"},
}
return json


@pytest.fixture
def fixture_build_details_json2():
json = {
"id": 2,
"state": "in_progress",
"state_reason": "state_reason",
"state_history": [],
"from_index": "from_index",
"from_index_resolved": "from_index_resolved",
"bundles": ["bundles1"],
"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"},
"omps_operator_version": {"operator": "1.0"},
}
return json


@pytest.fixture
def fixture_builds_page1_json(fixture_build_details_json):
json = {
"items": [fixture_build_details_json],
"meta": {
"first": "",
"last": "",
"next": "",
"page": 1,
"pages": 2,
"per_page": 1,
"previous": "",
"total": 2,
},
}
return json


@pytest.fixture
def fixture_builds_page2_json(fixture_build_details_json2):
json = {
"items": [fixture_build_details_json2],
"meta": {
"first": "",
"last": "",
"next": "",
"page": 2,
"pages": 2,
"per_page": 1,
"previous": "",
"total": 2,
},
}
return json


def test_iibbuilddetails_pager(
fixture_builds_page1_json,
fixture_builds_page2_json,
fixture_build_details_json,
fixture_build_details_json2,
):
with requests_mock.Mocker() as m:
m.register_uri(
"GET", "/api/v1/builds", status_code=200, json=fixture_builds_page1_json
)
m.register_uri(
"GET",
"/api/v1/builds?page=2",
status_code=200,
json=fixture_builds_page2_json,
)
m.register_uri(
"GET",
"/api/v1/builds?page=1",
status_code=200,
json=fixture_builds_page1_json,
)

iibc = IIBClient("fake-host")
pager = iibc.get_builds()
assert pager.items() == [
IIBBuildDetailsModel.from_dict(fixture_builds_page1_json["items"][0])
]
pager.next()
assert pager.items() == [
IIBBuildDetailsModel.from_dict(fixture_builds_page2_json["items"][0])
]
pager.prev()
assert pager.items() == [
IIBBuildDetailsModel.from_dict(fixture_builds_page1_json["items"][0])
]
Loading

0 comments on commit 9666611

Please sign in to comment.