test_operations.py 3.79 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# 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/>.

18
from django.test import TestCase
Bach Dániel committed
19
from mock import MagicMock
20

21
from common.operations import operation_registry_name as op_reg_name
22
from vm.models import Instance, InstanceActivity, Node
23
from vm.operations import (
24
    DeployOperation, DestroyOperation, FlushOperation, MigrateOperation,
25 26
    RebootOperation, ResetOperation, SaveAsTemplateOperation,
    ShutdownOperation, ShutOffOperation, SleepOperation, WakeUpOperation,
27
)
28

29 30 31

class DeployOperationTestCase(TestCase):
    def test_operation_registered(self):
32
        assert DeployOperation.id in getattr(Instance, op_reg_name)
33 34 35 36


class DestroyOperationTestCase(TestCase):
    def test_operation_registered(self):
37
        assert DestroyOperation.id in getattr(Instance, op_reg_name)
38 39


40 41 42 43 44
class FlushOperationTestCase(TestCase):
    def test_operation_registered(self):
        assert FlushOperation.id in getattr(Node, op_reg_name)


45 46
class MigrateOperationTestCase(TestCase):
    def test_operation_registered(self):
47
        assert MigrateOperation.id in getattr(Instance, op_reg_name)
48

49 50 51 52 53 54
    def test_operation_wo_to_node_param(self):
        class MigrateException(Exception):
            pass

        inst = MagicMock(spec=Instance)
        act = MagicMock(spec=InstanceActivity)
Bach Dániel committed
55 56
        op = MigrateOperation(inst)
        op._get_remote_args = MagicMock(side_effect=MigrateException())
57 58
        inst.select_node = MagicMock(return_value='test')
        self.assertRaises(
Bach Dániel committed
59
            MigrateException, op._operation,
60 61
            act, to_node=None)
        assert inst.select_node.called
62 63
        op._get_remote_args.assert_called_once_with(
            to_node='test', live_migration=True)
64

65 66 67

class RebootOperationTestCase(TestCase):
    def test_operation_registered(self):
68
        assert RebootOperation.id in getattr(Instance, op_reg_name)
69 70 71 72


class ResetOperationTestCase(TestCase):
    def test_operation_registered(self):
73
        assert ResetOperation.id in getattr(Instance, op_reg_name)
74 75 76 77


class SaveAsTemplateOperationTestCase(TestCase):
    def test_operation_registered(self):
78
        assert SaveAsTemplateOperation.id in getattr(Instance, op_reg_name)
79

80
    def test_rename(self):
81 82 83 84 85 86
        self.assertEqual(SaveAsTemplateOperation._rename("foo"), "foo v1")
        self.assertEqual(SaveAsTemplateOperation._rename("foo v2"), "foo v3")
        self.assertEqual(SaveAsTemplateOperation._rename("foo v"), "foo v v1")
        self.assertEqual(SaveAsTemplateOperation._rename("foo v9"), "foo v10")
        self.assertEqual(
            SaveAsTemplateOperation._rename("foo v111"), "foo v112")
87

88 89 90

class ShutdownOperationTestCase(TestCase):
    def test_operation_registered(self):
91
        assert ShutdownOperation.id in getattr(Instance, op_reg_name)
92 93 94 95


class ShutOffOperationTestCase(TestCase):
    def test_operation_registered(self):
96
        assert ShutOffOperation.id in getattr(Instance, op_reg_name)
97 98 99 100


class SleepOperationTestCase(TestCase):
    def test_operation_registered(self):
101
        assert SleepOperation.id in getattr(Instance, op_reg_name)
102 103 104 105


class WakeUpOperationTestCase(TestCase):
    def test_operation_registered(self):
106
        assert WakeUpOperation.id in getattr(Instance, op_reg_name)