PageRenderTime 131ms CodeModel.GetById 71ms app.highlight 6ms RepoModel.GetById 52ms app.codeStats 0ms

/tests/unit/test_rewrite_map.py

https://bitbucket.org/ianb/silverlining/
Python | 77 lines | 73 code | 4 blank | 0 comment | 0 complexity | 4e5323ca108c87e49b8e311c3ace21e2 MD5 | raw file
 1import os
 2import new
 3from cStringIO import StringIO
 4
 5rewrite_map_fn = os.path.abspath(os.path.join(
 6    __file__, '../../../silverlining/mgr-scripts/rewrite-map.py'))
 7
 8rewritemap = new.module('rewritemap')
 9rewritemap.__file__ = rewrite_map_fn
10execfile(rewrite_map_fn, rewritemap.__dict__)
11
12
13def test_rewrite_map():
14    data = StringIO("""\
15test1.example.com / test1-root
16test1.example.com /testy test1-testy
17test1.example.com /testy/another test1-another
18test2.example.com seeother test1.example.com
19.example.com / example-generic
20* /blog all-blog
21* / all-root
22""")
23    rewritemap.read_file_data(data)
24    assert 'test1.example.com' in rewritemap.abs_hostnames, (
25        rewritemap.abs_hostnames)
26
27    assert '*' in rewritemap.glob_hostnames, (
28        rewritemap.glob_hostnames)
29    assert '.example.com' in rewritemap.glob_hostnames
30    tests = """\
31test1.example.com^/ / test1-root
32test1.example.com^/something / test1-root
33test1.example.com^/testy/something /testy test1-testy
34test1.example.com^/testy/another/something /testy/another test1-another
35test2.example.com^/testy/ /testy test1-testy
36foobar.example.com^/ / example-generic
37nothing.notfound^/foo / all-root
38nothing.notfound^/blog/foo /blog all-blog""".splitlines()
39    for line in tests:
40        input, output = line.split(None, 1)
41        hostname, path = input.split('^')
42        path_match, data, _rest = rewritemap.lookup(hostname, path)
43        result = '%s %s' % (path_match, data)
44        assert result == output, (
45            "Bad result: %r -> %r" % (line, result))
46
47
48def test_appdata():
49    from silversupport import appdata
50    locations = ['http://foo.com/bar', 'example.com/',
51                 'test1.example.com', 'example.com/blog']
52    assert [appdata.normalize_location(l) for l in locations] == (
53        [('foo.com', '/bar'), ('example.com', '/'),
54         ('test1.example.com', '/'), ('example.com', '/blog')])
55    existing = """\
56# Here are some lines:
57example.com / app_name.1|general|/dev/null|python|
58example.com /blog blog_name|general|/var/lib/silverlining/app/writable-roots/blog_name|python|
59example.com /wiki media_name|general|/dev/null|php|mediawiki
60"""
61    vars = dict(instance_name='app_name.2', platform='python',
62                php_root='', process_group='general_debug',
63                write_root='/dev/null')
64    new = appdata.rewrite_lines(existing, [('example.com', '/'), ('test2.example.com', '/stage')],
65                                True, vars)
66    assert new == """\
67# Here are some lines:
68prev.example.com / app_name.1|general|/dev/null|python|
69example.com /blog blog_name|general|/var/lib/silverlining/app/writable-roots/blog_name|python|
70example.com /wiki media_name|general|/dev/null|php|mediawiki
71example.com / app_name.2|general_debug|/dev/null|python|
72test2.example.com /stage app_name.2|general_debug|/dev/null|python|
73""", new
74
75if __name__ == '__main__':
76    test_rewrite_map()
77    test_appdata()