From 94be8731f352a514888967b9af012d91455d194b Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Wed, 22 Feb 2017 15:45:06 -0500 Subject: [PATCH] Change Docker Version tests to pytest --- test/test_util.py | 49 ++-------------------------- util/registry/test/test_dockerver.py | 37 +++++++++++++++++++++ 2 files changed, 40 insertions(+), 46 deletions(-) create mode 100644 util/registry/test/test_dockerver.py diff --git a/test/test_util.py b/test/test_util.py index f7b6cc6f1..bcf134e8c 100644 --- a/test/test_util.py +++ b/test/test_util.py @@ -1,14 +1,13 @@ import unittest -import magic +from _pyio import BufferedReader from itertools import islice -from semantic_version import Version, Spec + +import magic from util.validation import generate_valid_usernames, validate_username from util.registry.generatorfile import GeneratorFile -from util.registry.dockerver import docker_version from util import slash_join -from _pyio import BufferedReader class TestUsernameValidation(unittest.TestCase): def assertValid(self, username): @@ -200,48 +199,6 @@ class TestUsernameGenerator(unittest.TestCase): self.assertEquals('a3', generated_output[3]) -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)) - - class TestSlashJoining(unittest.TestCase): def test_joining(self): test_cases = [ diff --git a/util/registry/test/test_dockerver.py b/util/registry/test/test_dockerver.py new file mode 100644 index 000000000..f0b2c59a7 --- /dev/null +++ b/util/registry/test/test_dockerver.py @@ -0,0 +1,37 @@ +import pytest + +from util.registry.dockerver import docker_version +from semantic_version import Version, Spec + +@pytest.mark.parametrize('ua_string, ver_info', [ + ('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')), +]) +def test_parsing(ua_string, ver_info): + parsed_ver = docker_version(ua_string) + assert parsed_ver == ver_info + +@pytest.mark.parametrize('spec, no_match_cases, match_cases', [ + (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']), +]) +def test_specs(spec, no_match_cases, match_cases): + for no_match_case in no_match_cases: + assert not spec.match(Version(no_match_case)) + + for match_case in match_cases: + assert spec.match(Version(match_case))