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