From dd7d4c8583d22c224b31cf609b272da0414c70b3 Mon Sep 17 00:00:00 2001 From: Scott Bahling Date: Sun, 26 Jan 2020 15:16:36 +0100 Subject: Use unittest mocks for test cases We simply test that the url and params are correct for the HTTP requests and that the result is returned to use properly --- test/test_obs_build_api.py | 217 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 178 insertions(+), 39 deletions(-) (limited to 'test/test_obs_build_api.py') diff --git a/test/test_obs_build_api.py b/test/test_obs_build_api.py index 39b6868..69370b3 100644 --- a/test/test_obs_build_api.py +++ b/test/test_obs_build_api.py @@ -1,5 +1,7 @@ +import os +import pytest from obsapi.buildapi import ObsBuildApi -from lxml import etree +from unittest.mock import MagicMock prj = 'home:sbahling:obsapi:test' pkg = 'suse-hello-1.0' @@ -8,125 +10,262 @@ arch = 'x86_64' buildapi = ObsBuildApi(apiurl='https://api.opensuse.org') +test_dir = os.path.split(os.path.abspath(__file__))[0] -def test_build_get(): + +@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 = '' xml = buildapi.get() + assert mock_get.call_args[0][0] == '{}/build/'.format(buildapi.apiurl) assert '' in xml -def test_build_get_prj(): +# GET /build/ +# Result: directory listing as xml +def test_build_get_prj(setup_mock_get): + mock_get, mock_response = setup_mock_get + mock_response.text = '' xml = buildapi.get(prj) + assert mock_get.call_args[0][0] == '{}/build/{}'.format(buildapi.apiurl, prj) assert '' in xml -def test_build_get_repo(): +# GET /build// +# Result: directory listing as xml +def test_build_get_repo(setup_mock_get): + mock_get, mock_response = setup_mock_get + mock_response.text = '' xml = buildapi.get(prj, repo) + assert mock_get.call_args[0][0] == '{}/build/{}/{}'.format(buildapi.apiurl, prj, repo) assert '' in xml -def test_build_get_arch(): +# GET /build/// +# Result: directory listing as xml +def test_build_get_arch(setup_mock_get): + mock_get, mock_response = setup_mock_get + mock_response.text = '' xml = buildapi.get(prj, repo, arch) + assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}'.format(buildapi.apiurl, prj, repo, arch) assert '' in xml -def test_build_get_binary_list(): +# GET /build//// +# Result: binarylist as xml +def test_build_get_binary_list(setup_mock_get): + mock_get, mock_response = setup_mock_get + mock_response.text = '' xml = buildapi.get(prj, repo, arch, pkg) + assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/{}'.format(buildapi.apiurl, prj, repo, arch, pkg) assert '' in xml - assert '///_statistics +# Result: buildstatistics as xml +def test_build_get_binary(setup_mock_get): + mock_get, mock_response = setup_mock_get + mock_response.content = '' 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 '' in str(binary) -def test_build_get_binary_fileinfo(): +# GET /build////?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 = ''.format(bname) 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 ''.format(bname) in str(xml) -def test_build_get_binary_fileinfo_ext(): +# GET /build////?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 = ''.format(bname) 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 ''.format(bname) in str(xml) - assert '///_builddepinfo +# Result: builddepinfo as xml +def test_build_get_builddepinfo(setup_mock_get): + mock_get, mock_response = setup_mock_get + mock_response.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 '' in xml -def test_build_get_buildconf(): +# GET /build////_buildconfig +# Result: buildconfig as text file +def test_build_get_buildconfig(setup_mock_get): + mock_buildconfig = """%define _project home:sbahling:obsapi:test + +### from SUSE:SLE-12-SP3:GA +%define _repository SLED + +Macros: +%vendor obs://build.opensuse.org/home:sbahling:obsapi:test +%_download_url http://download.opensuse.org/repositories +%_project home:sbahling:obsapi:test +""" + mock_get, mock_response = setup_mock_get + mock_response.text = mock_buildconfig 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 -def test_build_get_buildinfo(): +# GET /build/////_buildinfo +# Result: buildinfo as xml +def test_build_get_buildinfo(setup_mock_get): + mock_get, mock_response = setup_mock_get + mock_response.text = '' xml = buildapi.get_buildinfo(prj, pkg, repo, arch) - assert '///_jobhistory +# Result: jobhistlist as xml +def test_build_get_jobhistory(setup_mock_get): + mock_get, mock_response = setup_mock_get + mock_response.text = '' xml = buildapi.get_jobhistory(prj, repo, arch) + assert mock_get.call_args[0][0] == '{}/build/{}/{}/{}/_jobhistory'.format(buildapi.apiurl, prj, repo, arch) assert '' in xml -def test_build_get_jobhistory_pkg(): +# GET /build////_jobhistory?package= +# Result: jobhistlist as xml +def test_build_get_jobhistory_pkg(setup_mock_get): + mock_get, mock_response = setup_mock_get + mock_response.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 '' in xml -def test_build_get_result(): +# Undocumented api ?? +def test_build_get_result(setup_mock_get): + mock_get, mock_response = setup_mock_get + mock_response.text = '' xml = buildapi.get_result(prj) + assert mock_get.call_args[0][0] == '{}/build/{}/_result'.format(buildapi.apiurl, prj) assert '////_history +# Result: buildhistory as xml +def test_build_get_history(setup_mock_get): + mock_get, mock_response = setup_mock_get + mock_response.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 '' in xml -def test_build_get_reason(): +# GET /build/////_reason +# Result: build reason as xml +def test_build_get_reason(setup_mock_get): + mock_get, mock_response = setup_mock_get + mock_response.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 '' in xml -# def test_build_get_jobstatus(): -# xml = buildapi.get_jobstatus(prj, repo, arch, pkg) -# assert '' in xml +# GET /build/////_jobstatus +# Result: jobstatus as xml +def test_build_get_jobstatus(setup_mock_get): + mock_get, mock_response = setup_mock_get + mock_response.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 '' in xml -def test_build_get_status(): +# GET /build/////_status +# Result: build status as xml +def test_build_get_status(setup_mock_get): + mock_get, mock_response = setup_mock_get + mock_response.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 '////_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}, + )) +def test_build_get_log(setup_mock_get, params): + mock_get, mock_response = setup_mock_get + mock_response.text = '[ 0s] Start of log' + 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 -def test_build_get_repository(): +# GET /build////_repository +# Result: binarylist +def test_build_get_repository(setup_mock_get): + mock_get, mock_response = setup_mock_get + mock_response.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 '///_repository/ +# Result: binary file +def test_build_get_repository_binary(setup_mock_get): + mock_get, mock_response = setup_mock_get + bname = 'suse-hello-kmp-default.rpm' + with open(os.path.join(test_dir, bname), 'rb') as f: + mock_response.content = f.read() + 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 -- cgit v1.2.3