test_models.py 2.56 KB
Newer Older
1
from django.test import TestCase
2
from mock import Mock, MagicMock, patch
3

4 5 6
from ..models.common import (
    Lease
)
7
from ..models.instance import (
8
    find_unused_port, InstanceTemplate, Instance, ActivityInProgressError
9
)
10 11 12
from ..models.network import (
    Interface
)
13 14 15 16 17 18 19 20 21 22 23 24


class PortFinderTestCase(TestCase):

    def test_find_unused_port_without_used_ports(self):
        port = find_unused_port(port_range=(1000, 2000))
        assert port is not None

    def test_find_unused_port_with_fully_saturated_range(self):
        r = (10, 20)
        port = find_unused_port(port_range=r, used_ports=range(*r))
        assert port is None
25 26 27


class TemplateTestCase(TestCase):
28

29
    def test_template_creation(self):
30 31
        template = InstanceTemplate(name='My first template',
                                    access_method='ssh', )
Őry Máté committed
32
        template.clean()
33
        # TODO add images & net
34 35


36 37 38 39 40 41
class InstanceTestCase(TestCase):

    def test_is_running(self):
        inst = Mock(state='RUNNING')
        assert Instance.is_running.getter(inst)

42 43 44 45 46 47 48 49 50 51 52
    def test_mon_stopped_while_activity_running(self):
        node = Mock()
        port = Mock()
        inst = MagicMock(spec=Instance, node=node, vnc_port=port)
        inst.save.side_effect = AssertionError
        with patch('vm.models.instance.InstanceActivity') as ia:
            ia.create.side_effect = ActivityInProgressError(MagicMock())
            Instance.vm_state_changed(inst, 'STOPPED')
        self.assertEquals(inst.node, node)
        self.assertEquals(inst.vnc_port, port)

53

54 55 56 57 58 59 60
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
61
        i = Instance(id=10, owner=owner, access_method='rdp')
62 63 64 65 66 67
        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
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91


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)