summaryrefslogtreecommitdiff
path: root/test/test_obs_build_api.py
blob: 194abf3f57f39d41e8bb79d44a9817d8ff456d93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import os
import pytest
from obsapi.buildapi import ObsBuildApi

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]


# GET /build/
# Result: directory listing as xml
def test_build_get(requests_mock):
    uri = '{}/build/'.format(buildapi.apiurl)
    text = '<directory></directory>'
    requests_mock.get(uri, text=text)
    xml = buildapi.get()
    assert xml == text


# GET /build/<project>
# Result: directory listing as xml
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 xml == text


# GET /build/<project>/<repository>
# Result: directory listing as xml
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 xml == text


# GET /build/<project>/<repository>/<arch>
# Result: directory listing as xml
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 xml == text


# GET /build/<project>/<repository>/<arch>/<package>
# Result: binarylist as xml
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 xml == text


# Not official api call
# GET /build/<project>/<repository>/<arch>/_statistics
# Result: buildstatistics as xml
def test_build_get_binary(requests_mock):
    bname = '_statistics'
    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(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 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(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 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(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 xml == text


# GET /build/<project>/<repository>/<arch>/_buildconfig
# Result: buildconfig as text file
def test_build_get_buildconfig(requests_mock):
    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
"""
    text = mock_buildconfig
    uri = '{}/build/{}/{}/_buildconfig'.format(buildapi.apiurl, prj, repo)
    requests_mock.get(uri, text=text)
    buildconfig = buildapi.get_buildconfig(prj, repo)
    assert buildconfig == text


# GET /build/<project>/<repository>/<arch>/<package>/_buildinfo
# Result: buildinfo as xml
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 xml == text


# GET /build/<project>/<repository>/<arch>/_jobhistory
# Result: jobhistlist as xml
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 xml == text


# GET /build/<project>/<repository>/<arch>/_jobhistory?package=<package>
# Result: jobhistlist as xml
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 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(requests_mock):
    uri = '{}/build/{}/_result'.format(buildapi.apiurl, prj)
    text = '<resultlist></resultlist>'
    requests_mock.get(uri, text=text)
    xml = buildapi.get_result(prj)
    assert xml == text


# Undocumented api ??
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 xml == text


# GET /build/<project>/<repository>/<arch>/<package>/_history
# Result: buildhistory as xml
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 xml == text


# GET /build/<project>/<repository>/<arch>/<package>/_reason
# Result: build reason as xml
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 xml == text


# GET /build/<project>/<repository>/<arch>/<package>/_jobstatus
# Result: jobstatus as xml
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 xml == text


# GET /build/<project>/<repository>/<arch>/<package>/_status
# Result: build status as xml
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 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'},
                                    ))
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)
    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(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 xml == text


# GET /build/<project>/<repository>/<arch>/_repository/<binaryname>
# Result: binary file
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:
        data = f.read()
    requests_mock.get(uri, content=data)
    binary = buildapi.get_repository(prj, repo, arch, bname)
    assert binary == data