django-orchestra-test/orchestra/apps/orchestration/tests/test_route.py

42 lines
1.6 KiB
Python
Raw Normal View History

2014-05-08 16:59:35 +00:00
from orchestra.utils.tests import BaseTestCase
2014-10-04 09:29:18 +00:00
from .. import backends
from ..models import Route, Server, BackendOperation as Operation
2014-05-08 16:59:35 +00:00
class RouterTests(BaseTestCase):
def setUp(self):
self.host = Server.objects.create(name='web.example.com')
self.host1 = Server.objects.create(name='web1.example.com')
self.host2 = Server.objects.create(name='web2.example.com')
def test_list_backends(self):
# TODO count actual, register and compare
choices = list(Route._meta.get_field_by_name('backend')[0]._choices)
self.assertLess(1, len(choices))
def test_get_instances(self):
2014-10-04 09:29:18 +00:00
class TestBackend(backends.ServiceController):
2014-05-08 16:59:35 +00:00
verbose_name = 'Route'
2014-10-04 09:29:18 +00:00
models = ['routes.Route']
def save(self, instance):
pass
2014-05-08 16:59:35 +00:00
2014-10-04 09:29:18 +00:00
choices = backends.ServiceBackend.get_plugin_choices()
2014-05-08 16:59:35 +00:00
Route._meta.get_field_by_name('backend')[0]._choices = choices
backend = TestBackend.get_name()
2014-10-04 09:29:18 +00:00
route = Route.objects.create(backend=backend, host=self.host, match='True')
operation = Operation(backend=TestBackend, instance=route, action='save')
2014-05-08 16:59:35 +00:00
self.assertEqual(1, len(Route.get_servers(operation)))
2014-10-04 09:29:18 +00:00
2014-05-08 16:59:35 +00:00
route = Route.objects.create(backend=backend, host=self.host1,
2014-10-04 09:29:18 +00:00
match='route.backend == "%s"' % TestBackend.get_name())
2014-05-08 16:59:35 +00:00
self.assertEqual(2, len(Route.get_servers(operation)))
route = Route.objects.create(backend=backend, host=self.host2,
2014-10-04 09:29:18 +00:00
match='route.backend == "something else"')
2014-05-08 16:59:35 +00:00
self.assertEqual(2, len(Route.get_servers(operation)))