2014-08-01 19:50:25 +00:00
|
|
|
import unittest
|
2016-06-23 17:40:57 +00:00
|
|
|
import magic
|
2014-08-01 19:50:25 +00:00
|
|
|
|
|
|
|
from itertools import islice
|
2015-12-15 21:21:06 +00:00
|
|
|
from semantic_version import Version, Spec
|
2014-08-01 19:50:25 +00:00
|
|
|
|
2016-09-20 19:21:26 +00:00
|
|
|
from util.validation import generate_valid_usernames, validate_username
|
2015-08-03 19:49:10 +00:00
|
|
|
from util.registry.generatorfile import GeneratorFile
|
2015-12-15 21:21:06 +00:00
|
|
|
from util.registry.dockerver import docker_version
|
2016-03-18 19:48:41 +00:00
|
|
|
from util import slash_join
|
2016-06-23 17:40:57 +00:00
|
|
|
from _pyio import BufferedReader
|
2015-07-02 14:52:43 +00:00
|
|
|
|
2016-09-20 19:21:26 +00:00
|
|
|
class TestUsernameValidation(unittest.TestCase):
|
|
|
|
def assertValid(self, username):
|
|
|
|
result, _ = validate_username(username)
|
|
|
|
self.assertTrue(result)
|
|
|
|
|
|
|
|
def assertInvalid(self, username):
|
|
|
|
result, _ = validate_username(username)
|
|
|
|
self.assertFalse(result)
|
|
|
|
|
|
|
|
def test_valid(self):
|
|
|
|
self.assertValid('jake')
|
|
|
|
self.assertValid('ja_ke')
|
|
|
|
self.assertValid('te-st')
|
|
|
|
self.assertValid('te.st')
|
|
|
|
|
|
|
|
def test_properlength(self):
|
|
|
|
self.assertValid('z' * 30)
|
|
|
|
|
|
|
|
def test_tooshort(self):
|
|
|
|
self.assertInvalid('j')
|
|
|
|
self.assertInvalid('ja')
|
|
|
|
self.assertInvalid('jk')
|
|
|
|
|
|
|
|
def test_toolong(self):
|
|
|
|
self.assertInvalid('z' * 31)
|
|
|
|
|
|
|
|
def test_invalids(self):
|
|
|
|
self.assertInvalid('_test')
|
|
|
|
self.assertInvalid('Test')
|
|
|
|
self.assertInvalid('hello world')
|
|
|
|
self.assertInvalid('te---st')
|
|
|
|
|
|
|
|
|
2015-07-02 14:52:43 +00:00
|
|
|
class TestGeneratorFile(unittest.TestCase):
|
|
|
|
def sample_generator(self):
|
|
|
|
yield 'this'
|
|
|
|
yield 'is'
|
|
|
|
yield 'a'
|
|
|
|
yield 'test'
|
|
|
|
|
|
|
|
def test_basic_generator(self):
|
|
|
|
with GeneratorFile(self.sample_generator()) as f:
|
2015-08-28 16:10:03 +00:00
|
|
|
self.assertEquals(0, f.tell())
|
2015-07-02 14:52:43 +00:00
|
|
|
self.assertEquals("thisisatest", f.read())
|
2015-08-28 16:10:03 +00:00
|
|
|
self.assertEquals(len("thisisatest"), f.tell())
|
2015-07-02 14:52:43 +00:00
|
|
|
|
|
|
|
def test_same_lengths(self):
|
|
|
|
with GeneratorFile(self.sample_generator()) as f:
|
|
|
|
self.assertEquals("this", f.read(4))
|
2015-08-28 16:10:03 +00:00
|
|
|
self.assertEquals(4, f.tell())
|
|
|
|
|
2015-07-02 14:52:43 +00:00
|
|
|
self.assertEquals("is", f.read(2))
|
2015-08-28 16:10:03 +00:00
|
|
|
self.assertEquals(6, f.tell())
|
|
|
|
|
2015-07-02 14:52:43 +00:00
|
|
|
self.assertEquals("a", f.read(1))
|
2015-08-28 16:10:03 +00:00
|
|
|
self.assertEquals(7, f.tell())
|
|
|
|
|
2015-07-02 14:52:43 +00:00
|
|
|
self.assertEquals("test", f.read(4))
|
2015-08-28 16:10:03 +00:00
|
|
|
self.assertEquals(11, f.tell())
|
2015-07-02 14:52:43 +00:00
|
|
|
|
|
|
|
def test_indexed_lengths(self):
|
|
|
|
with GeneratorFile(self.sample_generator()) as f:
|
|
|
|
self.assertEquals("thisis", f.read(6))
|
2015-08-28 16:10:03 +00:00
|
|
|
self.assertEquals(6, f.tell())
|
|
|
|
|
2015-07-02 14:52:43 +00:00
|
|
|
self.assertEquals("atest", f.read(5))
|
2015-08-28 16:10:03 +00:00
|
|
|
self.assertEquals(11, f.tell())
|
2015-07-02 14:52:43 +00:00
|
|
|
|
|
|
|
def test_misindexed_lengths(self):
|
|
|
|
with GeneratorFile(self.sample_generator()) as f:
|
|
|
|
self.assertEquals("thisis", f.read(6))
|
2015-08-28 16:10:03 +00:00
|
|
|
self.assertEquals(6, f.tell())
|
|
|
|
|
2015-07-02 14:52:43 +00:00
|
|
|
self.assertEquals("ate", f.read(3))
|
2015-08-28 16:10:03 +00:00
|
|
|
self.assertEquals(9, f.tell())
|
|
|
|
|
2015-07-02 14:52:43 +00:00
|
|
|
self.assertEquals("st", f.read(2))
|
2015-08-28 16:10:03 +00:00
|
|
|
self.assertEquals(11, f.tell())
|
|
|
|
|
2015-07-02 14:52:43 +00:00
|
|
|
self.assertEquals("", f.read(2))
|
2015-08-28 16:10:03 +00:00
|
|
|
self.assertEquals(11, f.tell())
|
2015-07-02 14:52:43 +00:00
|
|
|
|
|
|
|
def test_misindexed_lengths_2(self):
|
|
|
|
with GeneratorFile(self.sample_generator()) as f:
|
|
|
|
self.assertEquals("thisisat", f.read(8))
|
2015-08-28 16:10:03 +00:00
|
|
|
self.assertEquals(8, f.tell())
|
|
|
|
|
2015-07-02 14:52:43 +00:00
|
|
|
self.assertEquals("e", f.read(1))
|
2015-08-28 16:10:03 +00:00
|
|
|
self.assertEquals(9, f.tell())
|
|
|
|
|
2015-07-02 14:52:43 +00:00
|
|
|
self.assertEquals("st", f.read(2))
|
2015-08-28 16:10:03 +00:00
|
|
|
self.assertEquals(11, f.tell())
|
|
|
|
|
2015-07-02 14:52:43 +00:00
|
|
|
self.assertEquals("", f.read(2))
|
2015-08-28 16:10:03 +00:00
|
|
|
self.assertEquals(11, f.tell())
|
2015-07-02 14:52:43 +00:00
|
|
|
|
|
|
|
def test_overly_long(self):
|
|
|
|
with GeneratorFile(self.sample_generator()) as f:
|
|
|
|
self.assertEquals("thisisatest", f.read(60))
|
2015-08-28 16:10:03 +00:00
|
|
|
self.assertEquals(11, f.tell())
|
2015-07-02 14:52:43 +00:00
|
|
|
|
2016-06-23 17:40:57 +00:00
|
|
|
def test_with_bufferedreader(self):
|
|
|
|
with GeneratorFile(self.sample_generator()) as f:
|
|
|
|
buffered = BufferedReader(f)
|
|
|
|
self.assertEquals("thisisatest", buffered.peek(10))
|
|
|
|
self.assertEquals("thisisates", buffered.read(10))
|
|
|
|
|
|
|
|
def mimed_html_generator(self):
|
|
|
|
yield '<html>'
|
|
|
|
yield '<body>'
|
|
|
|
yield 'sometext' * 1024
|
|
|
|
yield '</body>'
|
|
|
|
yield '</html>'
|
|
|
|
|
|
|
|
def test_magic(self):
|
|
|
|
mgc = magic.Magic(mime=True)
|
|
|
|
|
|
|
|
with GeneratorFile(self.mimed_html_generator()) as f:
|
|
|
|
buffered = BufferedReader(f)
|
|
|
|
file_header_bytes = buffered.peek(1024)
|
|
|
|
self.assertEquals("text/html", mgc.from_buffer(file_header_bytes))
|
|
|
|
|
|
|
|
with GeneratorFile(self.sample_generator()) as f:
|
|
|
|
buffered = BufferedReader(f)
|
|
|
|
file_header_bytes = buffered.peek(1024)
|
|
|
|
self.assertEquals("text/plain", mgc.from_buffer(file_header_bytes))
|
|
|
|
|
2014-08-01 19:50:25 +00:00
|
|
|
|
|
|
|
class TestUsernameGenerator(unittest.TestCase):
|
|
|
|
def assert_generated_output(self, input_username, expected_output):
|
|
|
|
name_gen = generate_valid_usernames(input_username)
|
|
|
|
generated_output = list(islice(name_gen, 1))[0]
|
|
|
|
self.assertEquals(expected_output, generated_output)
|
|
|
|
|
|
|
|
def test_basic_ascii_names(self):
|
|
|
|
self.assert_generated_output('jake', 'jake')
|
|
|
|
self.assert_generated_output('frank', 'frank')
|
2015-10-14 18:41:39 +00:00
|
|
|
self.assert_generated_output('fra-nk', 'fra_nk')
|
2014-08-01 19:50:25 +00:00
|
|
|
|
|
|
|
def test_names_with_caps(self):
|
|
|
|
self.assert_generated_output('Jake', 'jake')
|
|
|
|
self.assert_generated_output('FranK', 'frank')
|
|
|
|
|
2015-10-14 18:41:39 +00:00
|
|
|
def test_multiple_underscores(self):
|
|
|
|
self.assert_generated_output('ja__ke', 'ja_ke')
|
|
|
|
self.assert_generated_output('ja___ke', 'ja_ke')
|
|
|
|
|
|
|
|
def test_trailing_underscores(self):
|
|
|
|
self.assert_generated_output('ja__', 'ja00')
|
|
|
|
self.assert_generated_output('jake__', 'jake')
|
|
|
|
|
2016-09-20 19:21:26 +00:00
|
|
|
def test_starting_underscore(self):
|
|
|
|
self.assert_generated_output('_jake', 'jake')
|
|
|
|
|
2014-08-01 19:50:25 +00:00
|
|
|
def test_short_names(self):
|
2015-10-14 18:41:39 +00:00
|
|
|
self.assert_generated_output('a', 'a000')
|
|
|
|
self.assert_generated_output('ab', 'ab00')
|
|
|
|
self.assert_generated_output('abc', 'abc0')
|
2014-08-01 19:50:25 +00:00
|
|
|
|
|
|
|
def test_long_names(self):
|
|
|
|
self.assert_generated_output('abcdefghijklmnopqrstuvwxyz1234567890',
|
|
|
|
'abcdefghijklmnopqrstuvwxyz1234')
|
|
|
|
|
|
|
|
def test_unicode_transliteration(self):
|
|
|
|
self.assert_generated_output(u'\xc6neid', 'aeneid')
|
|
|
|
self.assert_generated_output(u'\xe9tude', 'etude')
|
|
|
|
self.assert_generated_output(u'\u5317\u4eb0', 'bei_jing')
|
|
|
|
self.assert_generated_output(u'\u1515\u14c7\u14c7', 'shanana')
|
|
|
|
self.assert_generated_output(u'\u13d4\u13b5\u13c6', 'taliqua')
|
|
|
|
self.assert_generated_output(u'\u0726\u071b\u073d\u0710\u073a', 'ptu_i')
|
|
|
|
self.assert_generated_output(u'\u0905\u092d\u093f\u091c\u0940\u0924', 'abhijiit')
|
|
|
|
self.assert_generated_output(u'\u0985\u09ad\u09bf\u099c\u09c0\u09a4', 'abhijiit')
|
|
|
|
self.assert_generated_output(u'\u0d05\u0d2d\u0d3f\u0d1c\u0d40\u0d24', 'abhijiit')
|
|
|
|
self.assert_generated_output(u'\u0d2e\u0d32\u0d2f\u0d3e\u0d32\u0d2e\u0d4d', 'mlyaalm')
|
2015-10-14 18:41:39 +00:00
|
|
|
self.assert_generated_output(u'\ue000', '0000')
|
|
|
|
self.assert_generated_output(u'\u03ff', '0000')
|
|
|
|
|
|
|
|
self.assert_generated_output(u'\u0d2e\u0d32\u03ff\u03ff\u0d2e\u0d32', 'mlml')
|
2014-08-01 19:50:25 +00:00
|
|
|
|
|
|
|
def test_multiple_suggestions(self):
|
|
|
|
name_gen = generate_valid_usernames('a')
|
|
|
|
generated_output = list(islice(name_gen, 4))
|
2015-10-14 18:41:39 +00:00
|
|
|
self.assertEquals('a000', generated_output[0])
|
|
|
|
self.assertEquals('a001', generated_output[1])
|
|
|
|
self.assertEquals('a002', generated_output[2])
|
|
|
|
self.assertEquals('a003', generated_output[3])
|
2015-07-02 14:52:43 +00:00
|
|
|
|
|
|
|
|
2015-12-15 21:21:06 +00:00
|
|
|
class TestDockerVersionParsing(unittest.TestCase):
|
|
|
|
def test_parsing(self):
|
|
|
|
tests_cases = [
|
|
|
|
('docker/1.6.0 go/go1.4.2 git-commit/1234567 kernel/4.2.0-18-generic os/linux arch/amd64',
|
|
|
|
Version('1.6.0')),
|
|
|
|
('docker/1.7.1 go/go1.4.2 kernel/4.1.7-15.23.amzn1.x86_64 os/linux arch/amd64',
|
|
|
|
Version('1.7.1')),
|
|
|
|
('docker/1.6.2 go/go1.4.2 git-commit/7c8fca2-dirty kernel/4.0.5 os/linux arch/amd64',
|
|
|
|
Version('1.6.2')),
|
|
|
|
('docker/1.9.0 go/go1.4.2 git-commit/76d6bc9 kernel/3.16.0-4-amd64 os/linux arch/amd64',
|
|
|
|
Version('1.9.0')),
|
|
|
|
('docker/1.9.1 go/go1.4.2 git-commit/a34a1d5 kernel/3.10.0-229.20.1.el7.x86_64 os/linux arch/amd64',
|
|
|
|
Version('1.9.1')),
|
|
|
|
('docker/1.8.2-circleci go/go1.4.2 git-commit/a8b52f5 kernel/3.13.0-71-generic os/linux arch/amd64',
|
|
|
|
Version('1.8.2')),
|
|
|
|
('Go 1.1 package http', Version('1.5.0')),
|
|
|
|
('curl', None),
|
|
|
|
('docker/1.8 stuff', Version('1.8.0')),
|
|
|
|
]
|
|
|
|
|
|
|
|
for ua_string, ver_info in tests_cases:
|
|
|
|
parsed_ver = docker_version(ua_string)
|
|
|
|
self.assertEquals(ver_info, parsed_ver)
|
|
|
|
|
|
|
|
def test_specs(self):
|
|
|
|
test_cases = [
|
|
|
|
# (Spec, no_match_case_list, matching_case_list)
|
|
|
|
(Spec('<1.6.0'), ['1.6.0', '1.6.1', '1.9.0', '100.5.2'], ['0.0.0', '1.5.99']),
|
|
|
|
(Spec('<1.9.0'), ['1.9.0', '100.5.2'], ['0.0.0', '1.5.99', '1.6.0', '1.6.1']),
|
|
|
|
(Spec('<1.6.0,>0.0.1'), ['1.6.0', '1.6.1', '1.9.0', '0.0.0'], ['1.5.99']),
|
|
|
|
]
|
|
|
|
|
|
|
|
for spec, no_match_cases, match_cases in test_cases:
|
|
|
|
for no_match_case in no_match_cases:
|
|
|
|
self.assertFalse(spec.match(Version(no_match_case)),
|
|
|
|
'Spec: %s Case: %s' % (spec, no_match_case))
|
|
|
|
|
|
|
|
for match_case in match_cases:
|
|
|
|
self.assertTrue(spec.match(Version(match_case)),
|
|
|
|
'Spec: %s Case: %s' % (spec, match_case))
|
|
|
|
|
2015-12-22 19:55:13 +00:00
|
|
|
|
|
|
|
class TestSlashJoining(unittest.TestCase):
|
|
|
|
def test_joining(self):
|
|
|
|
test_cases = [
|
|
|
|
(['https://github.com', '/coreos-inc/' 'quay/pull/1092/files'],
|
|
|
|
'https://github.com/coreos-inc/quay/pull/1092/files'),
|
2016-06-30 18:10:22 +00:00
|
|
|
|
2015-12-22 19:55:13 +00:00
|
|
|
(['https://', 'github.com/', '/coreos-inc', '/quay/pull/1092/files/'],
|
|
|
|
'https://github.com/coreos-inc/quay/pull/1092/files'),
|
2016-06-30 18:10:22 +00:00
|
|
|
|
|
|
|
(['https://somegithub.com/', '/api/v3/'],
|
|
|
|
'https://somegithub.com/api/v3'),
|
|
|
|
|
|
|
|
(['https://github.somedomain.com/', '/api/v3/'],
|
|
|
|
'https://github.somedomain.com/api/v3'),
|
2015-12-22 19:55:13 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
for args, url in test_cases:
|
|
|
|
joined_url = slash_join(*args)
|
|
|
|
self.assertEquals(url, joined_url)
|
|
|
|
|
|
|
|
|
2015-07-02 14:52:43 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
|
|
|
|