-
Notifications
You must be signed in to change notification settings - Fork 54
/
test.py
executable file
·365 lines (282 loc) · 12.5 KB
/
test.py
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#!/usr/bin/env python
import os
import sys
import io
import unittest
import multiprocessing
import tempfile
import artifactory
import json
import requests
import datetime
import dateutil
from mock import MagicMock as MM
class UtilTest(unittest.TestCase):
def test_matrix_encode(self):
params = {"foo": "bar",
"qux": "asdf"}
s = artifactory.encode_matrix_parameters(params)
self.assertEqual(s, "foo=bar;qux=asdf")
params = {'baz': ['bar', 'quux'], 'foo': 'asdf'}
s = artifactory.encode_matrix_parameters(params)
self.assertEqual(s, "baz=bar;baz=quux;foo=asdf")
def test_escape_chars(self):
s = artifactory.escape_chars('a,b|c=d')
self.assertEqual(s, "a\,b\|c\=d")
def test_properties_encode(self):
params = {"foo": "bar,baz", "qux": "as=df"}
s = artifactory.encode_properties(params)
self.assertEqual(s, "foo=bar\\,baz|qux=as\\=df")
def test_properties_encode_multi(self):
params = {'baz': ['ba\\r', 'qu|ux'], 'foo': 'a,s=df'}
s = artifactory.encode_properties(params)
self.assertEqual(s, "baz=ba\\r,qu\|ux|foo=a\,s\=df")
class ArtifactoryFlavorTest(unittest.TestCase):
flavour = artifactory._artifactory_flavour
def _check_parse_parts(self, arg, expected):
f = self.flavour.parse_parts
sep = self.flavour.sep
altsep = self.flavour.altsep
actual = f([x.replace('/', sep) for x in arg])
self.assertEqual(actual, expected)
if altsep:
actual = f([x.replace('/', altsep) for x in arg])
self.assertEqual(actual, expected)
def setUp(self):
artifactory.global_config = {
'http://custom/root': {}
}
def tearDown(self):
artifactory.global_config = None
def _check_splitroot(self, arg, expected):
f = self.flavour.splitroot
actual = f(arg)
self.assertEqual(actual, expected)
def test_splitroot(self):
check = self._check_splitroot
check(".com",
('', '', '.com'))
check("example1.com",
('', '', 'example1.com'))
check("example2.com/artifactory",
('example2.com/artifactory', '', ''))
check("example2.com/artifactory/",
('example2.com/artifactory', '', ''))
check("example3.com/artifactory/foo",
('example3.com/artifactory', '/foo/', ''))
check("example3.com/artifactory/foo/bar",
('example3.com/artifactory', '/foo/', 'bar'))
check("artifactory.local/artifactory/foo/bar",
('artifactory.local/artifactory', '/foo/', 'bar'))
check("http://artifactory.local/artifactory/foo/bar",
('http://artifactory.local/artifactory', '/foo/', 'bar'))
check("https://artifactory.a.b.c.d/artifactory/foo/bar",
('https://artifactory.a.b.c.d/artifactory', '/foo/', 'bar'))
def test_splitroot_custom_root(self):
check = self._check_splitroot
check("http://custom/root",
('http://custom/root', '', ''))
check("custom/root",
('custom/root', '', ''))
check("https://custom/root",
('https://custom/root', '', ''))
check("http://custom/root/",
('http://custom/root', '', ''))
check("http://custom/root/artifactory",
('http://custom/root', '/artifactory/', ''))
check("http://custom/root/foo/bar",
('http://custom/root', '/foo/', 'bar'))
check("https://custom/root/foo/baz",
('https://custom/root', '/foo/', 'baz'))
def test_parse_parts(self):
check = self._check_parse_parts
check(['.txt'],
('', '', ['.txt']))
check(['http://b/artifactory/c/d.xml'],
('http://b/artifactory', '/c/',
['http://b/artifactory/c/', 'd.xml']))
check(['http://example.com/artifactory/foo'],
('http://example.com/artifactory', '/foo/',
['http://example.com/artifactory/foo/']))
check(['http://example.com/artifactory/foo/bar'],
('http://example.com/artifactory', '/foo/',
['http://example.com/artifactory/foo/', 'bar']))
check(['http://example.com/artifactory/foo/bar/artifactory'],
('http://example.com/artifactory', '/foo/',
['http://example.com/artifactory/foo/', 'bar', 'artifactory']))
class PureArtifactoryPathTest(unittest.TestCase):
cls = artifactory.PureArtifactoryPath
def test_root(self):
P = self.cls
self.assertEqual(P('http://a/artifactory/b').root,
'/b/')
self.assertEqual(P('http://a/artifactory/').root,
'')
def test_anchor(self):
P = self.cls
b = P("http://b/artifactory/c/d.xml")
self.assertEqual(b.anchor, "http://b/artifactory/c/")
def test_with_suffix(self):
P = self.cls
b = P("http://b/artifactory/c/d.xml")
c = b.with_suffix(".txt")
self.assertEqual(str(c), "http://b/artifactory/c/d.txt")
class ArtifactoryAccessorTest(unittest.TestCase):
""" Test the real artifactory integration """
cls = artifactory._ArtifactoryAccessor
def setUp(self):
self.file_stat = """
{
"repo" : "ext-release-local",
"path" : "/org/company/tool/1.0/tool-1.0.tar.gz",
"created" : "2014-02-24T21:20:59.999+04:00",
"createdBy" : "someuser",
"lastModified" : "2014-02-24T21:20:36.000+04:00",
"modifiedBy" : "anotheruser",
"lastUpdated" : "2014-02-24T21:20:36.000+04:00",
"downloadUri" : "http://artifactory.local/artifactory/ext-release-local/org/company/tool/1.0/tool-1.0.tar.gz",
"mimeType" : "application/octet-stream",
"size" : "26776462",
"checksums" : {
"sha1" : "fc6c9e8ba6eaca4fa97868ac900570282133c095",
"md5" : "2af7d54a09e9c36d704cb3a2de28aff3"
},
"originalChecksums" : {
"sha1" : "fc6c9e8ba6eaca4fa97868ac900570282133c095",
"md5" : "2af7d54a09e9c36d704cb3a2de28aff3"
},
"uri" : "http://artifactory.local/artifactory/api/storage/ext-release-local/org/company/tool/1.0/tool-1.0.tar.gz"
}
"""
self.dir_stat = """
{
"repo" : "libs-release-local",
"path" : "/",
"created" : "2014-02-18T15:35:29.361+04:00",
"lastModified" : "2014-02-18T15:35:29.361+04:00",
"lastUpdated" : "2014-02-18T15:35:29.361+04:00",
"children" : [ {
"uri" : "/.index",
"folder" : true
}, {
"uri" : "/com",
"folder" : true
} ],
"uri" : "http://artifactory.local/artifactory/api/storage/libs-release-local"
}
"""
def test_stat(self):
a = self.cls()
P = artifactory.ArtifactoryPath
# Regular File
p = P("http://artifactory.local/artifactory/api/storage/ext-release-local/org/company/tool/1.0/tool-1.0.tar.gz")
a.rest_get = MM(return_value=(self.file_stat, 200))
s = a.stat(p)
self.assertEqual(s.ctime, dateutil.parser.parse("2014-02-24T21:20:59.999+04:00"))
self.assertEqual(s.mtime, dateutil.parser.parse("2014-02-24T21:20:36.000+04:00"))
self.assertEqual(s.created_by, "someuser")
self.assertEqual(s.modified_by, "anotheruser")
self.assertEqual(s.mime_type, "application/octet-stream")
self.assertEqual(s.size, 26776462)
self.assertEqual(s.sha1, "fc6c9e8ba6eaca4fa97868ac900570282133c095")
self.assertEqual(s.md5, "2af7d54a09e9c36d704cb3a2de28aff3")
self.assertEqual(s.is_dir, False)
# Directory
p = P("http://artifactory.local/artifactory/api/storage/libs-release-local")
a.rest_get = MM(return_value=(self.dir_stat, 200))
s = a.stat(p)
self.assertEqual(s.ctime, dateutil.parser.parse("2014-02-18T15:35:29.361+04:00"))
self.assertEqual(s.mtime, dateutil.parser.parse("2014-02-18T15:35:29.361+04:00"))
self.assertEqual(s.created_by, None)
self.assertEqual(s.modified_by, None)
self.assertEqual(s.mime_type, None)
self.assertEqual(s.size, 0)
self.assertEqual(s.sha1, None)
self.assertEqual(s.md5, None)
self.assertEqual(s.is_dir, True)
def test_listdir(self):
a = self.cls()
P = artifactory.ArtifactoryPath
# Directory
p = P("http://artifactory.local/artifactory/api/storage/libs-release-local")
a.rest_get = MM(return_value=(self.dir_stat, 200))
children = a.listdir(p)
self.assertEqual(children, ['.index', 'com'])
# Regular File
p = P("http://artifactory.local/artifactory/api/storage/ext-release-local/org/company/tool/1.0/tool-1.0.tar.gz")
a.rest_get = MM(return_value=(self.file_stat, 200))
self.assertRaises(OSError, a.listdir, p)
def test_mkdir(self):
pass
def test_deploy(self):
a = self.cls()
P = artifactory.ArtifactoryPath
p = P("http://b/artifactory/c/d")
params = {'foo': 'bar', 'baz': 'quux'}
a.rest_put_stream = MM(return_value=('OK', 200))
f = io.StringIO()
a.deploy(p, f, parameters=params)
url = "http://b/artifactory/c/d;baz=quux;foo=bar"
a.rest_put_stream.assert_called_with(url, f, headers={}, auth=None, verify=True, cert=None)
class ArtifactoryPathTest(unittest.TestCase):
""" Test the filesystem-accessing fuctionality """
cls = artifactory.ArtifactoryPath
def test_basic(self):
P = self.cls
a = P("http://a/artifactory/")
def test_auth(self):
P = self.cls
a = P("http://a/artifactory/", auth=('foo', 'bar'))
self.assertEqual(a.auth, ('foo', 'bar'))
def test_auth_inheritance(self):
P = self.cls
b = P("http://b/artifactory/c/d", auth=('foo', 'bar'))
c = b.parent
self.assertEqual(c.auth, ('foo', 'bar'))
b = P("http://b/artifactory/c/d", auth=('foo', 'bar'))
c = b.relative_to("http://b/artifactory/c")
self.assertEqual(c.auth, ('foo', 'bar'))
b = P("http://b/artifactory/c/d", auth=('foo', 'bar'))
c = b.joinpath('d')
self.assertEqual(c.auth, ('foo', 'bar'))
b = P("http://b/artifactory/c/d", auth=('foo', 'bar'))
c = b / 'd'
self.assertEqual(c.auth, ('foo', 'bar'))
b = P("http://b/artifactory/c/d.xml", auth=('foo', 'bar'))
c = b.with_name("d.txt")
self.assertEqual(c.auth, ('foo', 'bar'))
b = P("http://b/artifactory/c/d.xml", auth=('foo', 'bar'))
c = b.with_suffix(".txt")
self.assertEqual(c.auth, ('foo', 'bar'))
class TestArtifactoryConfig(unittest.TestCase):
def test_artifactory_config(self):
cfg = "[foo.net/artifactory]\n" + \
"username=admin\n" + \
"password=ilikerandompasswords\n" + \
"verify=False\n" + \
"cert=~/path-to-cert\n" + \
"[http://bar.net/artifactory]\n" + \
"username=foo\n" + \
"password=bar\n"
with tempfile.NamedTemporaryFile(mode='w+') as tf:
tf.write(cfg)
tf.flush()
cfg = artifactory.read_config(tf.name)
c = artifactory.get_config_entry(cfg, 'foo.net/artifactory')
self.assertEqual(c['username'], 'admin')
self.assertEqual(c['password'], 'ilikerandompasswords')
self.assertEqual(c['verify'], False)
self.assertEqual(c['cert'],
os.path.expanduser('~/path-to-cert'))
c = artifactory.get_config_entry(cfg, 'http://bar.net/artifactory')
self.assertEqual(c['username'], 'foo')
self.assertEqual(c['password'], 'bar')
self.assertEqual(c['verify'], True)
c = artifactory.get_config_entry(cfg, 'bar.net/artifactory')
self.assertEqual(c['username'], 'foo')
self.assertEqual(c['password'], 'bar')
c = artifactory.get_config_entry(cfg, 'https://bar.net/artifactory')
self.assertEqual(c['username'], 'foo')
self.assertEqual(c['password'], 'bar')
if __name__ == '__main__':
unittest.main()