Skip to content

Commit

Permalink
Improve coverage of docker_registry.lib
Browse files Browse the repository at this point in the history
Docker-DCO-1.1-Signed-off-by: Gabor Nagy <mail@aigeruth.hu> (github: Aigeruth)
  • Loading branch information
Aigeruth committed Jul 13, 2014
1 parent 9d0ece4 commit f084294
Show file tree
Hide file tree
Showing 9 changed files with 190 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[report]
exclude_lines =
if __name__ == .__main__.:
2 changes: 1 addition & 1 deletion docker_registry/lib/index/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ def results(self, search_term):
{'name': name, 'description': description}
"""
raise NotImplementedError('results method for {!r}'.format(self))
raise NotImplementedError('results method for {0!r}'.format(self))


def load(kind=None):
Expand Down
Empty file added tests/lib/__init__.py
Empty file.
Empty file added tests/lib/index/__init__.py
Empty file.
26 changes: 26 additions & 0 deletions tests/lib/index/test__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import mock
import unittest

from docker_registry.lib import index


class TestIndex(unittest.TestCase):

def setUp(self):
self.index = index.Index()

def test_cover_passed_methods(self):
self.index._handle_repository_created(None, None, None, None)
self.index._handle_repository_updated(None, None, None, None)
self.index._handle_repository_deleted(None, None, None)

def test_results(self):
self.assertRaises(NotImplementedError, self.index.results, None)


class TestLoad(unittest.TestCase):

@mock.patch('docker_registry.lib.config.load')
def test_search_backend(self, load):
load.return_value = mock.MagicMock(search_backend='x')
self.assertRaises(NotImplementedError, index.load)
35 changes: 35 additions & 0 deletions tests/lib/index/test_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import mock
import unittest

from docker_registry.lib.index import db


class TestVersion(unittest.TestCase):

def setUp(self):
self.version = db.Version()

def test_repr(self):
self.assertEqual(type(repr(self.version)), str)


class TestRepository(unittest.TestCase):

def setUp(self):
self.repository = db.Repository()

def test_repr(self):
self.assertEqual(type(repr(self.repository)), str)


class TestSQLAlchemyIndex(unittest.TestCase):

def setUp(self):
self.index = db.SQLAlchemyIndex(database="sqlite://")

@mock.patch('sqlalchemy.orm.query.Query.first')
def test_setup_database(self, first):
first = mock.Mock( # noqa
side_effect=db.sqlalchemy.exc.OperationalError)
self.assertRaises(
NotImplementedError, db.SQLAlchemyIndex, database="sqlite://")
50 changes: 50 additions & 0 deletions tests/lib/test_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import mock

from docker_registry.lib import cache
from tests.base import TestCase


class TestCache(TestCase):

def setUp(self):
self.cache = mock.MagicMock(
host='localhost', port=1234, db=0, password='pass')

def tearDown(self):
cache.redis_conn = None
cache.cache_prefix = None

@mock.patch.object(cache, 'logger')
def test_enable_redis_cache(self, logger):
self.assertEqual(cache.redis_conn, None)
self.assertEqual(cache.cache_prefix, None)
cache.enable_redis_cache(None, None)
self.assertEqual(logger.warn.call_count, 1)

cache.enable_redis_cache(self.cache, None)
self.assertTrue(cache.redis_conn is not None)
self.assertEqual(type(cache.redis_conn), cache.redis.StrictRedis)
self.assertTrue(cache.cache_prefix is not None)
self.assertEqual(cache.cache_prefix, 'cache_path:/')

cache.enable_redis_cache(self.cache, 'test')
self.assertEqual(cache.cache_prefix, 'cache_path:test')

@mock.patch.object(cache, 'logger')
@mock.patch.object(cache.lru, 'init')
def test_enable_redis_lru(self, lru_init, logger):
cache.enable_redis_lru(None, None)
self.assertEqual(logger.warn.call_count, 1)

cache.enable_redis_lru(self.cache, None)
self.assertEqual(logger.info.call_count, 2)
lru_init.assert_called_once_with(
host=self.cache.host, port=self.cache.port, db=self.cache.db,
password=self.cache.password, path='/')

lru_init.reset_mock()
path = 'test'
cache.enable_redis_lru(self.cache, path)
lru_init.assert_called_once_with(
host=self.cache.host, port=self.cache.port, db=self.cache.db,
password=self.cache.password, path=path)
42 changes: 42 additions & 0 deletions tests/lib/test_checksums.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import mock

from docker_registry.lib import checksums
from tests.base import TestCase


class TestShaMethods(TestCase):

def test_sha256_file(self):
self.assertEqual(
checksums.sha256_file(None, None),
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855')
self.assertEqual(
checksums.sha256_file(None, 'test'),
'9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08')

def test_compute_simple(self):
out = checksums.compute_simple(None, '')
self.assertTrue(out.startswith('sha256:'))
nl = '01ba4719c80b6fe911b091a7c05124b64eeece964e09c058ef8f9805daca546b'
self.assertTrue(out.endswith(nl))

out = checksums.compute_simple(None, 'test')
h = 'f2ca1bb6c7e907d06dafe4687e579fce76b37e4e93b7605022da52e6ccc26fd2'
self.assertTrue(out.endswith(h))


class TestTarSum(TestCase):

def setUp(self):
self.tar_sum = checksums.TarSum(None)

def test_append(self):
self.tar_sum.header_fields = tuple()
member = mock.MagicMock(size=1)
tarobj = mock.MagicMock(
extractfile=mock.MagicMock(side_effect=KeyError))
self.tar_sum.append(member, tarobj)
self.assertEqual(len(self.tar_sum.hashes), 1)
self.assertEqual(
self.tar_sum.hashes[0],
'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855')
35 changes: 33 additions & 2 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import mock
import os
import unittest

from docker_registry.lib import config

Expand All @@ -22,14 +23,30 @@ def mockget(key, opt=None):


@mock.patch('os.environ.get', mockget)
class TestConfig():
class TestConfig(unittest.TestCase):

def __init__(self):
def setUp(self):
p = os.path.join(
os.path.dirname(__file__), 'fixtures', 'test_config.yaml')

self.c = config.Config(open(p, 'rb').read())

def test__init__(self):
self.assertRaises(config.exceptions.ConfigError, config.Config, '\1')

@mock.patch('__builtin__.repr')
def test__repr(self, r):
self.c.__repr__()
r.assert_called_once_with(self.c._config)

def test__methods__(self):
self.assertEqual(self.c.__methods__, [])

def test__members__(self):
self.assertEqual(type(self.c.__members__), list)
self.assertEqual(self.c.__members__, self.c.keys())
self.assertEqual(self.c.__members__, self.c.__dir__())

def test_accessors(self):
assert self.c.booltrue == self.c['booltrue']
assert self.c.dict.one == self.c.dict['one']
Expand Down Expand Up @@ -133,3 +150,17 @@ def test_write(self):

def test_unicode(self):
assert self.c.uni == u'ß∞'


class TestLoad(unittest.TestCase):

def setUp(self):
self._config = config._config

def tearDown(self):
config._config = self._config

@mock.patch.object(config.os.environ, 'get')
def test_config_path_exception(self, get):
config._config = None
self.assertRaises(config.exceptions.FileNotFoundError, config.load)

0 comments on commit f084294

Please sign in to comment.