summaryrefslogtreecommitdiff
path: root/test/test_obs_build_api.py
diff options
context:
space:
mode:
Diffstat (limited to 'test/test_obs_build_api.py')
-rw-r--r--test/test_obs_build_api.py260
1 files changed, 125 insertions, 135 deletions
diff --git a/test/test_obs_build_api.py b/test/test_obs_build_api.py
index 69370b3..194abf3 100644
--- a/test/test_obs_build_api.py
+++ b/test/test_obs_build_api.py
@@ -1,7 +1,6 @@
import os
import pytest
from obsapi.buildapi import ObsBuildApi
-from unittest.mock import MagicMock
prj = 'home:sbahling:obsapi:test'
pkg = 'suse-hello-1.0'
@@ -13,115 +12,105 @@ buildapi = ObsBuildApi(apiurl='https://api.opensuse.org')
test_dir = os.path.split(os.path.abspath(__file__))[0]
-@pytest.fixture(autouse=True)
-def setup_mock_get(monkeypatch):
- mock_response = MagicMock()
- mock_response.status_code = 200
- mock_get = MagicMock(return_value=mock_response)
- monkeypatch.setattr('requests.get', mock_get)
- return(mock_get, mock_response)
-
-
# GET /build/
# Result: directory listing as xml
-def test_build_get(setup_mock_get):
- mock_get, mock_response = setup_mock_get
- mock_response.text = '<directory></directory>'
+def test_build_get(requests_mock):
+ uri = '{}/build/'.format(buildapi.apiurl)
+ text = '<directory></directory>'
+ requests_mock.get(uri, text=text)
xml = buildapi.get()
- assert mock_get.call_args[0][0] == '{}/build/'.format(buildapi.apiurl)
- assert '<directory>' in xml
+ assert xml == text
# GET /build/<project>
# Result: directory listing as xml
-def test_build_get_prj(setup_mock_get):
- mock_get, mock_response = setup_mock_get
- mock_response.text = '<directory></directory>'
+def test_build_get_prj(requests_mock):
+ uri = '{}/build/{}'.format(buildapi.apiurl, prj)
+ text = '<directory></directory>'
+ requests_mock.get(uri, text=text)
xml = buildapi.get(prj)
- assert mock_get.call_args[0][0] == '{}/build/{}'.format(buildapi.apiurl, prj)
- assert '<directory>' in xml
+ assert xml == text
# GET /build/<project>/<repository>
# Result: directory listing as xml
-def test_build_get_repo(setup_mock_get):
- mock_get, mock_response = setup_mock_get
- mock_response.text = '<directory></directory>'
+def test_build_get_repo(requests_mock):
+ uri = '{}/build/{}/{}'.format(buildapi.apiurl, prj, repo)
+ text = '<directory></directory>'
+ requests_mock.get(uri, text=text)
xml = buildapi.get(prj, repo)
- assert mock_get.call_args[0][0] == '{}/build/{}/{}'.format(buildapi.apiurl, prj, repo)
- assert '<directory>' in xml
+ assert xml == text
# GET /build/<project>/<repository>/<arch>
# Result: directory listing as xml
-def test_build_get_arch(setup_mock_get):
- mock_get, mock_response = setup_mock_get
- mock_response.text = '<directory></directory>'
+def test_build_get_arch(requests_mock):
+ uri = '{}/build/{}/{}/{}'.format(buildapi.apiurl, prj, repo, arch)
+ text = '<directory></directory>'
+ requests_mock.get(uri, text=text)
xml = buildapi.get(prj, repo, arch)
- assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}'.format(buildapi.apiurl, prj, repo, arch)
- assert '<directory>' in xml
+ assert xml == text
# GET /build/<project>/<repository>/<arch>/<package>
# Result: binarylist as xml
-def test_build_get_binary_list(setup_mock_get):
- mock_get, mock_response = setup_mock_get
- mock_response.text = '<binarylist></binarylist>'
+def test_build_get_binary_list(requests_mock):
+ uri = '{}/build/{}/{}/{}/{}'.format(buildapi.apiurl, prj, repo, arch, pkg)
+ text = '<binarylist></binarylist>'
+ requests_mock.get(uri, text=text)
xml = buildapi.get(prj, repo, arch, pkg)
- assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/{}'.format(buildapi.apiurl, prj, repo, arch, pkg)
- assert '<binarylist>' in xml
+ assert xml == text
# Not official api call
# GET /build/<project>/<repository>/<arch>/_statistics
# Result: buildstatistics as xml
-def test_build_get_binary(setup_mock_get):
- mock_get, mock_response = setup_mock_get
- mock_response.content = '<buildstatistics></buildstatistics>'
+def test_build_get_binary(requests_mock):
bname = '_statistics'
- binary = buildapi.get(prj, repo, arch, pkg, bname)
- assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/{}/{}'.format(buildapi.apiurl, prj, repo, arch, pkg, bname)
- assert '<buildstatistics>' in str(binary)
+ uri = '{}/build/{}/{}/{}/{}/{}'.format(buildapi.apiurl, prj, repo, arch, pkg, bname)
+ content = b'<buildstatistics></buildstatistics>'
+ requests_mock.get(uri, content=content)
+ file_content = buildapi.get(prj, repo, arch, pkg, bname)
+ assert file_content == content
# GET /build/<project>/<repository>/<arch>/<package>?view=fileinfo
# Result: fileinfo as xml
-def test_build_get_binary_fileinfo(setup_mock_get):
- mock_get, mock_response = setup_mock_get
- bname = 'suse-hello-1.0-1.1.src.rpm'
- mock_response.content = '<fileinfo filename="{}"></fileinfo>'.format(bname)
+def test_build_get_binary_fileinfo(requests_mock):
+ bname = b'suse-hello-1.0-1.1.src.rpm'
+ uri = '{}/build/{}/{}/{}/{}/{}'.format(buildapi.apiurl, prj, repo, arch, pkg, bname)
+ content = b'<fileinfo filename="%s"></fileinfo>' % bname
+ requests_mock.get(uri, content=content)
xml = buildapi.get(prj, repo, arch, pkg, bname, view='fileinfo')
- assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/{}/{}'.format(buildapi.apiurl, prj, repo, arch, pkg, bname)
- assert mock_get.call_args[1]['params']['view'] == 'fileinfo'
- assert '<fileinfo filename="{}">'.format(bname) in str(xml)
+ assert requests_mock.request_history[0].qs['view'][0] == 'fileinfo'
+ assert xml == content
# GET /build/<project>/<repository>/<arch>/<package>?view=fileinfo_ext
# Result: fileinfo as xml
-def test_build_get_binary_fileinfo_ext(setup_mock_get):
- mock_get, mock_response = setup_mock_get
- bname = 'suse-hello-1.0-1.1.src.rpm'
- mock_response.content = '<fileinfo filename="{}"></fileinfo>'.format(bname)
+def test_build_get_binary_fileinfo_ext(requests_mock):
+ bname = b'suse-hello-1.0-1.1.src.rpm'
+ uri = '{}/build/{}/{}/{}/{}/{}'.format(buildapi.apiurl, prj, repo, arch, pkg, bname)
+ content = b'<fileinfo filename="%s"></fileinfo>' % bname
+ requests_mock.get(uri, content=content)
xml = buildapi.get(prj, repo, arch, pkg, bname, view='fileinfo_ext')
- assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/{}/{}'.format(buildapi.apiurl, prj, repo, arch, pkg, bname)
- assert mock_get.call_args[1]['params']['view'] == 'fileinfo_ext'
- assert '<fileinfo filename="{}">'.format(bname) in str(xml)
+ assert requests_mock.request_history[0].qs['view'][0] == 'fileinfo_ext'
+ assert xml == content
# GET /build/<project>/<repository>/<arch>/_builddepinfo
# Result: builddepinfo as xml
-def test_build_get_builddepinfo(setup_mock_get):
- mock_get, mock_response = setup_mock_get
- mock_response.text = '<builddepinfo></builddepinfo>'
+def test_build_get_builddepinfo(requests_mock):
+ uri = '{}/build/{}/{}/{}/_builddepinfo'.format(buildapi.apiurl, prj, repo, arch)
+ text = '<builddepinfo></builddepinfo>'
+ requests_mock.get(uri, text=text)
xml = buildapi.get_builddepinfo(prj, pkg, repo, arch)
- assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/_builddepinfo'.format(buildapi.apiurl, prj, repo, arch)
- assert mock_get.call_args[1]['params']['package'] == pkg
- assert '<builddepinfo>' in xml
+ assert xml == text
# GET /build/<project>/<repository>/<arch>/_buildconfig
# Result: buildconfig as text file
-def test_build_get_buildconfig(setup_mock_get):
+def test_build_get_buildconfig(requests_mock):
mock_buildconfig = """%define _project home:sbahling:obsapi:test
### from SUSE:SLE-12-SP3:GA
@@ -132,140 +121,141 @@ Macros:
%_download_url http://download.opensuse.org/repositories
%_project home:sbahling:obsapi:test
"""
- mock_get, mock_response = setup_mock_get
- mock_response.text = mock_buildconfig
+ text = mock_buildconfig
+ uri = '{}/build/{}/{}/_buildconfig'.format(buildapi.apiurl, prj, repo)
+ requests_mock.get(uri, text=text)
buildconfig = buildapi.get_buildconfig(prj, repo)
- assert mock_get.call_args[0][0] == '{}/build/{}/{}/_buildconfig'.format(buildapi.apiurl, prj, repo)
- assert '%define _project home:sbahling:obsapi:test' in buildconfig
+ assert buildconfig == text
# GET /build/<project>/<repository>/<arch>/<package>/_buildinfo
# Result: buildinfo as xml
-def test_build_get_buildinfo(setup_mock_get):
- mock_get, mock_response = setup_mock_get
- mock_response.text = '<buildinfo></buildinfo>'
+def test_build_get_buildinfo(requests_mock):
+ uri = '{}/build/{}/{}/{}/{}/_buildinfo'.format(buildapi.apiurl, prj, repo, arch, pkg)
+ text = '<buildinfo></buildinfo>'
+ requests_mock.get(uri, text=text)
xml = buildapi.get_buildinfo(prj, pkg, repo, arch)
- assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/{}/_buildinfo'.format(buildapi.apiurl, prj, repo, arch, pkg)
- assert '<buildinfo' in xml
+ assert xml == text
# GET /build/<project>/<repository>/<arch>/_jobhistory
# Result: jobhistlist as xml
-def test_build_get_jobhistory(setup_mock_get):
- mock_get, mock_response = setup_mock_get
- mock_response.text = '<jobhistlist></jobhistlist>'
+def test_build_get_jobhistory(requests_mock):
+ uri = '{}/build/{}/{}/{}/_jobhistory'.format(buildapi.apiurl, prj, repo, arch)
+ text = '<jobhistlist></jobhistlist>'
+ requests_mock.get(uri, text=text)
xml = buildapi.get_jobhistory(prj, repo, arch)
- assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/_jobhistory'.format(buildapi.apiurl, prj, repo, arch)
- assert '<jobhistlist>' in xml
+ assert xml == text
# GET /build/<project>/<repository>/<arch>/_jobhistory?package=<package>
# Result: jobhistlist as xml
-def test_build_get_jobhistory_pkg(setup_mock_get):
- mock_get, mock_response = setup_mock_get
- mock_response.text = '<jobhistlist></jobhistlist>'
+def test_build_get_jobhistory_pkg(requests_mock):
+ uri = '{}/build/{}/{}/{}/_jobhistory'.format(buildapi.apiurl, prj, repo, arch)
+ text = '<jobhistlist></jobhistlist>'
+ requests_mock.get(uri, text=text)
xml = buildapi.get_jobhistory(prj, repo, arch, pkg)
- assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/_jobhistory'.format(buildapi.apiurl, prj, repo, arch)
- assert mock_get.call_args[1]['params']['package'] == pkg
- assert '<jobhistlist>' in xml
+ assert len(requests_mock.request_history[0].qs['package']) == 1
+ assert requests_mock.request_history[0].qs['package'][0] == pkg
+ assert xml == text
# Undocumented api ??
-def test_build_get_result(setup_mock_get):
- mock_get, mock_response = setup_mock_get
- mock_response.text = '<resultlist></resultlist>'
+def test_build_get_result(requests_mock):
+ uri = '{}/build/{}/_result'.format(buildapi.apiurl, prj)
+ text = '<resultlist></resultlist>'
+ requests_mock.get(uri, text=text)
xml = buildapi.get_result(prj)
- assert mock_get.call_args[0][0] == '{}/build/{}/_result'.format(buildapi.apiurl, prj)
- assert '<resultlist' in xml
+ assert xml == text
# Undocumented api ??
-def test_build_get_workerstatus(setup_mock_get):
- mock_get, mock_response = setup_mock_get
- mock_response.text = '<workerstatus></workerstatus>'
+def test_build_get_workerstatus(requests_mock):
+ uri = '{}/build/_workerstatus'.format(buildapi.apiurl)
+ text = '<workerstatus></workerstatus>'
+ requests_mock.get(uri, text=text)
xml = buildapi.get_workstatus()
- assert mock_get.call_args[0][0] == '{}/build/_workerstatus'.format(buildapi.apiurl)
- assert '<workerstatus' in xml
+ assert xml == text
# GET /build/<project>/<repository>/<arch>/<package>/_history
# Result: buildhistory as xml
-def test_build_get_history(setup_mock_get):
- mock_get, mock_response = setup_mock_get
- mock_response.text = '<buildhistory></buildhistory>'
+def test_build_get_history(requests_mock):
+ uri = '{}/build/{}/{}/{}/{}/_history'.format(buildapi.apiurl, prj, repo, arch, pkg)
+ text = '<buildhistory></buildhistory>'
+ requests_mock.get(uri, text=text)
xml = buildapi.get_history(prj, repo, arch, pkg)
- assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/{}/_history'.format(buildapi.apiurl, prj, repo, arch, pkg)
- assert '<buildhistory>' in xml
+ assert xml == text
# GET /build/<project>/<repository>/<arch>/<package>/_reason
# Result: build reason as xml
-def test_build_get_reason(setup_mock_get):
- mock_get, mock_response = setup_mock_get
- mock_response.text = '<reason></reason>'
+def test_build_get_reason(requests_mock):
+ uri = '{}/build/{}/{}/{}/{}/_reason'.format(buildapi.apiurl, prj, repo, arch, pkg)
+ text = '<reason></reason>'
+ requests_mock.get(uri, text=text)
xml = buildapi.get_reason(prj, repo, arch, pkg)
- assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/{}/_reason'.format(buildapi.apiurl, prj, repo, arch, pkg)
- assert '<reason>' in xml
+ assert xml == text
# GET /build/<project>/<repository>/<arch>/<package>/_jobstatus
# Result: jobstatus as xml
-def test_build_get_jobstatus(setup_mock_get):
- mock_get, mock_response = setup_mock_get
- mock_response.text = '<jobstatus/>'
+def test_build_get_jobstatus(requests_mock):
+ uri = '{}/build/{}/{}/{}/{}/_jobstatus'.format(buildapi.apiurl, prj, repo, arch, pkg)
+ text = '<jobstatus/>'
+ requests_mock.get(uri, text=text)
xml = buildapi.get_jobstatus(prj, repo, arch, pkg)
- assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/{}/_jobstatus'.format(buildapi.apiurl, prj, repo, arch, pkg)
- assert '<jobstatus/>' in xml
+ assert xml == text
# GET /build/<project>/<repository>/<arch>/<package>/_status
# Result: build status as xml
-def test_build_get_status(setup_mock_get):
- mock_get, mock_response = setup_mock_get
- mock_response.text = '<status></status>'
+def test_build_get_status(requests_mock):
+ uri = '{}/build/{}/{}/{}/{}/_status'.format(buildapi.apiurl, prj, repo, arch, pkg)
+ text = '<status></status>'
+ requests_mock.get(uri, text=text)
xml = buildapi.get_status(prj, repo, arch, pkg)
- assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/{}/_status'.format(buildapi.apiurl, prj, repo, arch, pkg)
- assert '<status' in xml
+ assert xml == text
# GET /build/<project>/<repository>/<arch>/<package>/_log
# Result: build log as text file
# params: nostream, last, lastsucceeded, start, end, view=entry
@pytest.mark.parametrize('params', ({},
- {'nostream': 1},
- {'view': 'entry', 'last': 1,
- 'start': 100, 'end': 500},
- {'view': 'entry', 'lastsucceeded': 1,
- 'start': 100, 'end': 500},
+ {'nostream': '1'},
+ {'view': 'entry', 'last': '1',
+ 'start': '100', 'end': '500'},
+ {'view': 'entry', 'lastsucceeded': '1',
+ 'start': '100', 'end': '500'},
))
-def test_build_get_log(setup_mock_get, params):
- mock_get, mock_response = setup_mock_get
- mock_response.text = '[ 0s] Start of log'
+def test_build_get_log(requests_mock, params):
+ uri = '{}/build/{}/{}/{}/{}/_log'.format(buildapi.apiurl, prj, repo, arch, pkg)
+ text = '[ 0s] Start of log'
+ requests_mock.get(uri, text=text)
log = buildapi.get_log(prj, repo, arch, pkg, **params)
- assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/{}/_log'.format(buildapi.apiurl, prj, repo, arch, pkg)
- assert mock_get.call_args[1]['params'] == params
- assert '[ 0s]' in log
+ for key, value in params.items():
+ assert len(requests_mock.request_history[0].qs[key]) == 1
+ assert value == requests_mock.request_history[0].qs[key][0]
+ assert log == text
# GET /build/<project>/<repository>/<arch>/_repository
# Result: binarylist
-def test_build_get_repository(setup_mock_get):
- mock_get, mock_response = setup_mock_get
- mock_response.text = '<binarylist></binarylist>'
+def test_build_get_repository(requests_mock):
+ uri = '{}/build/{}/{}/{}/_repository'.format(buildapi.apiurl, prj, repo, arch)
+ text = '<binarylist></binarylist>'
+ requests_mock.get(uri, text=text)
xml = buildapi.get_repository(prj, repo, arch)
- assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/_repository'.format(buildapi.apiurl, prj, repo, arch)
- assert mock_get.call_args[1]['params']['view'] is None
- assert '<binarylist' in xml
+ assert xml == text
# GET /build/<project>/<repository>/<arch>/_repository/<binaryname>
# Result: binary file
-def test_build_get_repository_binary(setup_mock_get):
- mock_get, mock_response = setup_mock_get
+def test_build_get_repository_binary(requests_mock):
bname = 'suse-hello-kmp-default.rpm'
+ uri = '{}/build/{}/{}/{}/_repository/{}'.format(buildapi.apiurl, prj, repo, arch, bname)
with open(os.path.join(test_dir, bname), 'rb') as f:
- mock_response.content = f.read()
+ data = f.read()
+ requests_mock.get(uri, content=data)
binary = buildapi.get_repository(prj, repo, arch, bname)
- assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/_repository/{}'.format(buildapi.apiurl, prj, repo, arch, bname)
- assert mock_get.call_args[1]['params']['view'] is None
- assert binary == mock_response.content
+ assert binary == data