def test_iib_client_no_overwrite_from_index_or_token( fixture_add_build_details_json, fixture_rm_build_details_json ): error_msg = ( "Either both or neither of overwrite-from-index " "and overwrite-from-index-token should be specified." ) with requests_mock.Mocker() as m: m.register_uri( "POST", "/api/v1/builds/add", status_code=200, json=fixture_add_build_details_json, ) m.register_uri( "POST", "/api/v1/builds/rm", status_code=200, json=fixture_rm_build_details_json, ) iibc = IIBClient("fake-host") with pytest.raises(ValueError, match=error_msg): iibc.remove_operators( "index-image", ["operator1"], [], binary_image="binary", overwrite_from_index=True, ) with pytest.raises(ValueError, match=error_msg): iibc.remove_operators( "index-image", ["operator1"], [], binary_image="binary", overwrite_from_index_token="str", ) with pytest.raises(ValueError, match=error_msg): iibc.add_bundles( "index-image", ["bundles-map"], [], binary_image="binary", cnr_token="cnr", organization="org", overwrite_from_index=True, ) with pytest.raises(ValueError, match=error_msg): iibc.add_bundles( "index-image", ["bundles-map"], [], binary_image="binary", cnr_token="cnr", organization="org", overwrite_from_index_token="str", )
def test_client_wait_for_build_retry(fixture_add_build_details_json): iibc = IIBClient("fake-host.test", poll_interval=1, retries=10, backoff_factor=0) with pytest.raises( requests.exceptions.RequestException, match=".*Max retries exceeded*." ): iibc.wait_for_build( IIBBuildDetailsModel.from_dict(fixture_add_build_details_json) )
def test_iib_error_response(): with requests_mock.Mocker() as m: m.register_uri( "GET", "/api/v1/builds", status_code=400, json={"error": "An ugly error has occurred!"}, ) iibc = IIBClient("fake-host") with pytest.raises(IIBException): iibc.get_builds()
def test_http_error_response(): with requests_mock.Mocker() as m: m.register_uri( "GET", "/api/v1/builds", status_code=400, text="An ugly HTTP error has occurred!", ) iibc = IIBClient("fake-host") with pytest.raises(HTTPError): iibc.get_builds()
def test_client_wait_for_build_timeout(fixture_add_build_details_json): # set wait timeout for 2 seconds iibc = IIBClient("fake-host.test", poll_interval=1, wait_for_build_timeout=2) with requests_mock.Mocker() as m: m.register_uri( "GET", "/api/v1/builds/{}".format(fixture_add_build_details_json["id"]), status_code=200, json=fixture_add_build_details_json, ) with pytest.raises(IIBException, match="Timeout*."): iibc.wait_for_build( IIBBuildDetailsModel.from_dict(fixture_add_build_details_json) )
def test_client_wait_for_build(fixture_add_build_details_json): iibc = IIBClient("fake-host", poll_interval=1) bdetails_finished = copy.copy(fixture_add_build_details_json) bdetails_finished["state"] = "complete" with requests_mock.Mocker() as m: m.register_uri( "GET", "/api/v1/builds/1", [ {"json": fixture_add_build_details_json, "status_code": 200}, {"json": bdetails_finished, "status_code": 200}, ], ) iibc.wait_for_build( IIBBuildDetailsModel.from_dict(fixture_add_build_details_json) ) bdetails_finished["state"] = "failed" with requests_mock.Mocker() as m: m.register_uri( "GET", "/api/v1/builds/1", [ {"json": fixture_add_build_details_json, "status_code": 200}, {"json": bdetails_finished, "status_code": 200}, ], ) iibc.wait_for_build( IIBBuildDetailsModel.from_dict(fixture_add_build_details_json) )
def test_iib_build_details_pager( fixture_builds_page1_json, fixture_builds_page2_json, fixture_add_build_details_json, fixture_add_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]) ]
def test_iib_client_deprecation_list( fixture_add_build_details_json, fixture_deprecation_list_build_details_json, ): with requests_mock.Mocker() as m: m.register_uri( "POST", "/api/v1/builds/add", status_code=200, json=fixture_add_build_details_json, ) iibc = IIBClient("fake-host") assert iibc.add_bundles( "index-image", ["bundles-map"], [], binary_image="binary" ) == AddModel.from_dict(fixture_add_build_details_json) assert ( iibc.add_bundles( "index-image", ["bundles-map"], [], binary_image="binary", cnr_token="cnr", organization="org", overwrite_from_index=True, overwrite_from_index_token="str", ) == AddModel.from_dict(fixture_add_build_details_json) ) with requests_mock.Mocker() as m: m.register_uri( "POST", "/api/v1/builds/add", status_code=200, json=fixture_deprecation_list_build_details_json, ) iibc = IIBClient("fake-host") assert ( iibc.add_bundles( "index-image", ["bundles-map"], [], binary_image="binary", cnr_token="cnr", deprecation_list=["deprecation_list"], organization="org", overwrite_from_index=True, overwrite_from_index_token="str", ) == AddModel.from_dict(fixture_deprecation_list_build_details_json) )
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_rebuild_index(): iibc = IIBClient("fake-host") iibc.rebuild_index("some index")
def test_health(): iibc = IIBClient("fake-host") iibc.health()
def test_iib_client( fixture_add_build_details_json, fixture_rm_build_details_json, fixture_regenerate_bundle_build_details_json, fixture_create_empty_index_build_details_json, fixture_builds_page1_json, ): with requests_mock.Mocker() as m: m.register_uri( "POST", "/api/v1/builds/add", status_code=200, json=fixture_add_build_details_json, ) m.register_uri( "POST", "/api/v1/builds/rm", status_code=200, json=fixture_rm_build_details_json, ) m.register_uri( "POST", "/api/v1/builds/regenerate-bundle", status_code=200, json=fixture_regenerate_bundle_build_details_json, ) m.register_uri( "POST", "/api/v1/builds/create-empty-index", status_code=200, json=fixture_create_empty_index_build_details_json, ) m.register_uri( "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_add_build_details_json, ) m.register_uri( "GET", "/api/v1/builds/2", status_code=200, json=fixture_rm_build_details_json, ) m.register_uri( "GET", "/api/v1/builds/3", status_code=200, json=fixture_regenerate_bundle_build_details_json, ) m.register_uri( "GET", "/api/v1/builds/4", status_code=200, json=fixture_create_empty_index_build_details_json, ) iibc = IIBClient("fake-host") ret = iibc.add_bundles( "index-image", ["bundles-map"], [], binary_image="binary" ) assert ret == AddModel.from_dict(fixture_add_build_details_json) ret = iibc.add_bundles( "index-image", ["bundles-map"], [], binary_image="binary", cnr_token="cnr", organization="org", overwrite_from_index=True, overwrite_from_index_token="str", build_tags=["extra-tag1"], ) assert ret == AddModel.from_dict(fixture_add_build_details_json) assert ( iibc.add_bundles( "index-image", ["bundles-map"], [], binary_image="binary", raw=True, ) == fixture_add_build_details_json ) assert ( iibc.remove_operators( "index-image", ["operator1"], [], binary_image="binary", build_tags=["v4.5-2020-10-10"], overwrite_from_index=True, overwrite_from_index_token="str", ) == RmModel.from_dict(fixture_rm_build_details_json) ) assert ( iibc.remove_operators( "index-image", ["operator1"], [], binary_image="binary", raw=True ) == fixture_rm_build_details_json ) assert iibc.regenerate_bundle( bundle_image="bundle_image", organization="organization", ) == RegenerateBundleModel.from_dict( fixture_regenerate_bundle_build_details_json ) assert ( iibc.regenerate_bundle( bundle_image="bundle_image", organization="organization", raw=True ) == fixture_regenerate_bundle_build_details_json ) assert iibc.create_empty_index( index_image="from_index", binary_image="binary_image", labels={"version": "v1"}, ) == CreateEmptyIndexModel.from_dict( fixture_create_empty_index_build_details_json ) assert ( iibc.create_empty_index( index_image="from_index", binary_image="binary_image", labels={"version": "v1"}, raw=True, ) == fixture_create_empty_index_build_details_json ) # get_builds - request_type is "add" assert iibc.get_build(1) == IIBBuildDetailsModel.from_dict( fixture_add_build_details_json ) assert iibc.get_build(1, raw=True) == fixture_add_build_details_json # get_builds - request_type is "rm" assert iibc.get_build(2) == IIBBuildDetailsModel.from_dict( fixture_rm_build_details_json ) assert iibc.get_build(2, raw=True) == fixture_rm_build_details_json # get_builds - request_type is "regenerate-bundle" assert iibc.get_build(3) == IIBBuildDetailsModel.from_dict( fixture_regenerate_bundle_build_details_json ) assert ( iibc.get_build(3, raw=True) == fixture_regenerate_bundle_build_details_json ) # get_builds - request_type is "create-empty-index" assert iibc.get_build(4) == IIBBuildDetailsModel.from_dict( fixture_create_empty_index_build_details_json ) assert ( iibc.get_build(4, raw=True) == fixture_create_empty_index_build_details_json ) assert iibc.get_builds() == IIBBuildDetailsPager.from_dict( iibc, fixture_builds_page1_json ) assert iibc.get_builds(raw=True) == fixture_builds_page1_json
def test_iib_client( fixture_add_build_details_json, fixture_rm_build_details_json, fixture_regenerate_bundle_build_details_json, fixture_builds_page1_json, ): with requests_mock.Mocker() as m: m.register_uri( "POST", "/api/v1/builds/add", status_code=200, json=fixture_add_build_details_json, ) m.register_uri( "POST", "/api/v1/builds/rm", status_code=200, json=fixture_rm_build_details_json, ) m.register_uri( "POST", "/api/v1/builds/regenerate-bundle", status_code=200, json=fixture_regenerate_bundle_build_details_json, ) m.register_uri("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_add_build_details_json, ) m.register_uri( "GET", "/api/v1/builds/2", status_code=200, json=fixture_rm_build_details_json, ) m.register_uri( "GET", "/api/v1/builds/3", status_code=200, json=fixture_regenerate_bundle_build_details_json, ) iibc = IIBClient("fake-host") assert iibc.add_bundles("index-image", ["bundles-map"], [], binary_image="binary") == AddModel.from_dict( fixture_add_build_details_json) assert (iibc.add_bundles( "index-image", ["bundles-map"], [], binary_image="binary", cnr_token="cnr", organization="org", overwrite_from_index=True, overwrite_from_index_token="str", ) == AddModel.from_dict(fixture_add_build_details_json)) assert (iibc.add_bundles("index-image", ["bundles-map"], [], binary_image="binary", raw=True) == fixture_add_build_details_json) assert (iibc.remove_operators( "index-image", ["operator1"], [], binary_image="binary", overwrite_from_index=True, overwrite_from_index_token="str", ) == RmModel.from_dict(fixture_rm_build_details_json)) assert (iibc.remove_operators( "index-image", ["operator1"], [], binary_image="binary", raw=True) == fixture_rm_build_details_json) assert iibc.regenerate_bundle( bundle_image="bundle_image", organization="organization", ) == RegenerateBundleModel.from_dict( fixture_regenerate_bundle_build_details_json) assert (iibc.regenerate_bundle( bundle_image="bundle_image", organization="organization", raw=True) == fixture_regenerate_bundle_build_details_json) # get_builds - request_type is "add" assert iibc.get_build(1) == IIBBuildDetailsModel.from_dict( fixture_add_build_details_json) assert iibc.get_build(1, raw=True) == fixture_add_build_details_json # get_builds - request_type is "rm" assert iibc.get_build(2) == IIBBuildDetailsModel.from_dict( fixture_rm_build_details_json) assert iibc.get_build(2, raw=True) == fixture_rm_build_details_json # get_builds - request_type is "regenerate-bundle" assert iibc.get_build(3) == IIBBuildDetailsModel.from_dict( fixture_regenerate_bundle_build_details_json) assert (iibc.get_build( 3, raw=True) == fixture_regenerate_bundle_build_details_json) assert iibc.get_builds() == IIBBuildDetailsPager.from_dict( iibc, fixture_builds_page1_json) assert iibc.get_builds(raw=True) == fixture_builds_page1_json