import os import pytest from obsapi.buildapi import ObsBuildApi from unittest.mock import MagicMock prj = 'home:sbahling:obsapi:test' pkg = 'suse-hello-1.0' repo = 'SLE_12_SP3' arch = 'x86_64' 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 = '' xml = buildapi.get() assert mock_get.call_args[0][0] == '{}/build/'.format(buildapi.apiurl) assert '' in xml # 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 # 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 # 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 # 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 # Not official api call # GET /build////_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) # 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) # 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) # GET /build////_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 # 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 # 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 mock_get.call_args[0][0] == '{}/build/{}/{}/{}/{}/_buildinfo'.format(buildapi.apiurl, prj, repo, arch, pkg) 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 # 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 # 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 # 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 # 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 # 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 # 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