test_models.py 2.78 KB
Newer Older
1
from django.test import TestCase
2 3 4

from mock import Mock

5
from ..models.instance import (
Őry Máté committed
6 7
    InstanceTemplate, Instance, post_state_changed
    # pre_state_changed,
8
)
9 10 11
from ..models.network import (
    Interface
)
Őry Máté committed
12 13 14
from ..models.common import (
    Lease
)
15 16 17


class TemplateTestCase(TestCase):
18

19
    def test_template_creation(self):
20 21
        template = InstanceTemplate(name='My first template',
                                    access_method='ssh', )
Őry Máté committed
22
        template.clean()
23
        # TODO add images & net
24 25 26 27


class InstanceTestCase(TestCase):

Őry Máté committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#    def test_pre_state_changed_w_exception(self):
#        """Signal handler of pre_state_changed prevents save with Exception."""
#        def callback(sender, new_state, **kwargs):
#            if new_state == 'invalid value':
#                raise Exception()
#        pre_state_changed.connect(callback)
#        i = Instance(state='NOSTATE')
#        i.save = Mock()
#        i.state_changed('invalid value')
#        assert i.state == 'NOSTATE'
#        assert not i.save.called
#
#    def test_pre_state_changed_wo_exception(self):
#        """Signal handler of pre_state_changed allows save."""
#        mock = Mock()
#        pre_state_changed.connect(mock)
#        i = Instance(state='NOSTATE')
#        i.save = Mock()
#        i.state_changed('RUNNING')
#        assert i.state == 'RUNNING'
#        assert mock.called
#        assert i.save.called
50 51 52 53 54 55 56 57 58 59 60

    def test_post_state_changed(self):
        """Signal handler of post_state_changed runs."""
        mock = Mock()
        post_state_changed.connect(mock)
        i = Instance(state='NOSTATE')
        i.save = Mock()
        i.state_changed('RUNNING')
        assert mock.called
        assert i.save.called
        assert i.state == 'RUNNING'
61 62 63 64 65 66 67 68 69


class InterfaceTestCase(TestCase):

    def test_interface_create(self):
        from firewall.models import Vlan, Domain
        from django.contrib.auth.models import User
        owner = User()
        owner.save()
Bach Dániel committed
70
        i = Instance(id=10, owner=owner, access_method='rdp')
71 72 73 74 75 76
        d = Domain(owner=owner)
        d.save()
        v = Vlan(vid=55, network4='127.0.0.1/8',
                 network6='2001::1/32', domain=d)
        v.save()
        Interface.create(i, v, managed=True, owner=owner)
Őry Máté committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100


class LeaseTestCase(TestCase):

    fixtures = ['lease.json']

    def test_methods(self):
        from datetime import timedelta
        td = timedelta(seconds=1)
        l = Lease.objects.get(pk=1)

        assert "never" not in unicode(l)
        assert l.delete_interval > td
        assert l.suspend_interval > td

        l.delete_interval = None
        assert "never" in unicode(l)
        assert l.delete_interval is None

        l.delete_interval = td * 2
        assert "never" not in unicode(l)

        l.suspend_interval = None
        assert "never" in unicode(l)