tests.py 5.78 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# Copyright 2014 Budapest University of Technology and Economics (BME IK)
#
# This file is part of CIRCLE Cloud.
#
# CIRCLE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option)
# any later version.
#
# CIRCLE is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with CIRCLE.  If not, see <http://www.gnu.org/licenses/>.

from django.test import TestCase
from django.test.client import Client
from django.contrib.auth.models import User, Permission

Karsa Zoltán István committed
22
from unittest.mock import Mock, patch
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157


from common.tests.celery_mock import MockCeleryMixin
from vm.models import Instance, InstanceTemplate, Lease
from dashboard.models import Profile
from request.models import Request, LeaseType, TemplateAccessType, RequestField
from dashboard.tests.test_views import LoginMixin
from vm.operations import ResourcesOperation


class RequestTest(LoginMixin, MockCeleryMixin, TestCase):
    fixtures = ['test-vm-fixture.json', 'node.json']

    def setUp(self):
        Instance.get_remote_queue_name = Mock(return_value='test')
        self.u1 = User.objects.create(username='user1')
        self.u1.set_password('password')
        self.u1.save()
        self.us = User.objects.create(username='superuser', is_superuser=True)
        self.us.set_password('password')
        self.us.save()
        self.u1.user_permissions.add(Permission.objects.get(
            codename='create_vm'))
        # superusers are notified uppon
        for u in User.objects.filter(is_superuser=True):
            p = Profile(user=u)
            p.save()

        self.lease = Lease(name="new lease", suspend_interval_seconds=1,
                           delete_interval_seconds=1)
        self.lease.save()
        LeaseType(name="lease type #1", lease=self.lease).save()
        tat = TemplateAccessType(name="a")
        tat.save()
        tat.templates.add(InstanceTemplate.objects.get(pk=1))

    def tearDown(self):
        super(RequestTest, self).tearDown()
        self.u1.delete()
        self.us.delete()

    def test_resources_request(self):
        c = Client()
        self.login(c, "user1")
        inst = Instance.objects.get(pk=1)
        inst.set_level(self.u1, 'owner')

        field = RequestField(fieldname="Oka", type="Char",
                             request_type="resource", required=True)
        field.save()

        req_count = Request.objects.count()
        resp = c.post("/request/resource/1/", {
            'num_cores': 5,
            'ram_size': 512,
            'priority': 30,
            'field1': "szia",
        })
        self.assertEqual(resp.status_code, 302)
        self.assertEqual(req_count + 1, Request.objects.count())
        new_request = Request.objects.latest("pk")
        self.assertEqual(new_request.status, "PENDING")

        self.assertEqual(inst.num_cores, 2)
        self.assertEqual(inst.ram_size, 200)
        self.assertEqual(inst.priority, 10)

        # workaround for NOSTATE
        inst.emergency_change_state(new_state="STOPPED", system=True)
        with patch.object(ResourcesOperation, 'async') as mock_method:
            mock_method.side_effect = (
                new_request.action.instance.resources_change)
            new_request.accept(self.us)

        inst = Instance.objects.get(pk=1)
        self.assertEqual(inst.num_cores, 5)
        self.assertEqual(inst.ram_size, 512)
        self.assertEqual(inst.priority, 30)

        new_request = Request.objects.latest("pk")
        self.assertEqual(new_request.status, "ACCEPTED")

    def test_template_access_request(self):
        c = Client()
        self.login(c, "user1")
        template = InstanceTemplate.objects.get(pk=1)
        self.assertFalse(template.has_level(self.u1, "user"))

        field = RequestField(fieldname="Tanszek", type="Char",
                             request_type="template", required=True)
        field.save()
        field = RequestField(fieldname="Szobaszam", type="Integer",
                             request_type="template", required=False)
        field.save()

        req_count = Request.objects.count()
        resp = c.post("/request/template/", {
            'template': 1,
            'level': "user",
            'field1': "IIT",
            'field2': 10,
        })
        self.assertEqual(resp.status_code, 302)
        self.assertEqual(req_count + 1, Request.objects.count())
        new_request = Request.objects.latest("pk")
        self.assertEqual(new_request.status, "PENDING")
        self.assertEqual(new_request.message, "Tanszek: IIT\nSzobaszam: 10\n")
        new_request.accept(self.us)

        new_request = Request.objects.latest("pk")
        self.assertEqual(new_request.status, "ACCEPTED")
        self.assertTrue(template.has_level(self.u1, "user"))

    def test_lease_request(self):
        c = Client()
        self.login(c, "user1")
        inst = Instance.objects.get(pk=1)
        inst.set_level(self.u1, 'owner')

        req_count = Request.objects.count()
        resp = c.post("/request/lease/1/", {
            'lease': 1,
            'message': "szia",
        })
        self.assertEqual(resp.status_code, 302)
        self.assertEqual(req_count + 1, Request.objects.count())
        new_request = Request.objects.latest("pk")
        self.assertEqual(new_request.status, "PENDING")

        new_request.accept(self.us)

        inst = Instance.objects.get(pk=1)
        new_request = Request.objects.latest("pk")
        self.assertEqual(new_request.status, "ACCEPTED")
        self.assertEqual(inst.lease, self.lease)