|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +# from nose import tools |
| 4 | + |
| 5 | +# from docker_registry.core import exceptions |
| 6 | +# import docker_registry.testing as testing |
| 7 | + |
| 8 | +import mock |
| 9 | +import os |
| 10 | + |
| 11 | +from docker_registry.lib import config |
| 12 | + |
| 13 | + |
| 14 | +fakeenv = {} |
| 15 | + |
| 16 | + |
| 17 | +def mockget(key, opt=None): |
| 18 | + if key in fakeenv: |
| 19 | + print('%s key is %s' % (key, fakeenv[key])) |
| 20 | + return fakeenv[key] |
| 21 | + return opt |
| 22 | + |
| 23 | + |
| 24 | +@mock.patch('os.environ.get', mockget) |
| 25 | +class TestConfig(): |
| 26 | + |
| 27 | + def __init__(self): |
| 28 | + p = os.path.join( |
| 29 | + os.path.dirname(__file__), 'fixtures', 'test_config.yaml') |
| 30 | + |
| 31 | + self.c = config.Config(open(p, 'rb').read()) |
| 32 | + |
| 33 | + def test_accessors(self): |
| 34 | + assert self.c.booltrue == self.c['booltrue'] |
| 35 | + assert self.c.dict.one == self.c.dict['one'] |
| 36 | + assert self.c.dict.one == self.c['dict']['one'] |
| 37 | + |
| 38 | + def test_key_existence(self): |
| 39 | + assert 'boolfalse' in self.c |
| 40 | + assert 'whatevertheflush' not in self.c |
| 41 | + |
| 42 | + def test_non_existent_access(self): |
| 43 | + assert self.c['whatevertheflush'] is None |
| 44 | + assert self.c.whatevertheflush is None |
| 45 | + |
| 46 | + def test_simple_types(self): |
| 47 | + conf = self.c |
| 48 | + assert conf.booltrue is True |
| 49 | + assert not conf.booltrue == 'True' |
| 50 | + assert conf.boolfalse is False |
| 51 | + assert not conf.booltrue == 'False' |
| 52 | + assert conf.uint == 10 |
| 53 | + assert not conf.uint == '10' |
| 54 | + assert conf.int == -10 |
| 55 | + assert not conf.int == '-10' |
| 56 | + assert conf.float == 0.01 |
| 57 | + assert not conf.float == '0.01' |
| 58 | + assert conf.emptystring == '' |
| 59 | + assert conf.emptystring is not None |
| 60 | + assert conf.isnone is None |
| 61 | + assert conf.nonemptystring == 'nonemptystring' |
| 62 | + assert conf.anothernonemptystring == 'nonemptystring' |
| 63 | + assert conf.yetanothernonemptystring == 'nonemptystring' |
| 64 | + assert conf.array[2] == 'three' |
| 65 | + assert len(conf.array) == 3 |
| 66 | + assert conf.dict.two == 'valuetwo' |
| 67 | + |
| 68 | + def test_env_defaults(self): |
| 69 | + global fakeenv |
| 70 | + fakeenv = {} |
| 71 | + |
| 72 | + conf = self.c.ENV |
| 73 | + assert conf.booltrue is True |
| 74 | + assert conf.boolfalse is False |
| 75 | + assert conf.uint == 10 |
| 76 | + assert conf.int == -10 |
| 77 | + assert conf.float == 0.01 |
| 78 | + assert conf.emptystring == '' |
| 79 | + assert conf.emptystring is not None |
| 80 | + assert conf.isnone is None |
| 81 | + assert conf.nonemptystring == 'nonemptystring' |
| 82 | + assert conf.anothernonemptystring == 'nonemptystring' |
| 83 | + assert conf.yetanothernonemptystring == 'nonemptystring' |
| 84 | + assert conf.bugger == 'bug:me:endlessly' |
| 85 | + assert conf.array[2] == 'three' |
| 86 | + assert len(conf.array) == 3 |
| 87 | + |
| 88 | + def test_env_overrides(self): |
| 89 | + global fakeenv |
| 90 | + fakeenv['BOOLTRUE'] = 'False' |
| 91 | + fakeenv['BOOLFALSE'] = 'True' |
| 92 | + fakeenv['UINT'] = '0' |
| 93 | + fakeenv['INT'] = '0' |
| 94 | + fakeenv['FLOAT'] = '0' |
| 95 | + fakeenv['EMPTYSTRING'] = 'NOTREALLY' |
| 96 | + fakeenv['ISNONE'] = 'False' |
| 97 | + fakeenv['NONEMPTYSTRING'] = '""' |
| 98 | + fakeenv['BUGGER'] = '"whatever:the:flush:"' |
| 99 | + fakeenv['ARRAY'] = '[one, again]' |
| 100 | + |
| 101 | + conf = self.c.ENV |
| 102 | + assert conf.booltrue is False |
| 103 | + assert conf.boolfalse is True |
| 104 | + assert conf.uint == 0 |
| 105 | + assert conf.int == 0 |
| 106 | + assert conf.float == 0 |
| 107 | + assert conf.emptystring == 'NOTREALLY' |
| 108 | + assert conf.isnone is False |
| 109 | + assert conf.isnone is not None |
| 110 | + assert conf.nonemptystring == '' |
| 111 | + assert conf.anothernonemptystring == 'nonemptystring' |
| 112 | + assert conf.yetanothernonemptystring == 'nonemptystring' |
| 113 | + assert conf.bugger == 'whatever:the:flush:' |
| 114 | + assert conf.array[1] == 'again' |
| 115 | + assert len(conf.array) == 2 |
| 116 | + |
| 117 | + fakeenv['ISNONE'] = '' |
| 118 | + assert conf.isnone is None |
| 119 | + |
| 120 | + def test_write(self): |
| 121 | + conf = self.c |
| 122 | + assert conf.something == 'else' |
| 123 | + conf.something = 'or' |
| 124 | + assert conf.something == 'or' |
| 125 | + conf.something = None |
| 126 | + assert conf.something is None |
| 127 | + |
| 128 | + def test_unicode(self): |
| 129 | + assert self.c.uni == u'ß∞' |
0 commit comments