Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / logic / tests.py @ 44e2c577

History | View | Annotate | Download (9.3 kB)

1 b8033991 Georgios Gousios
# vim: set fileencoding=utf-8 :
2 48130e66 Georgios Gousios
# Copyright 2011 GRNET S.A. All rights reserved.
3 09c936a7 Vassilios Karakoidas
#
4 48130e66 Georgios Gousios
# Redistribution and use in source and binary forms, with or without
5 48130e66 Georgios Gousios
# modification, are permitted provided that the following conditions
6 48130e66 Georgios Gousios
# are met:
7 09c936a7 Vassilios Karakoidas
#
8 48130e66 Georgios Gousios
#   1. Redistributions of source code must retain the above copyright
9 48130e66 Georgios Gousios
#      notice, this list of conditions and the following disclaimer.
10 48130e66 Georgios Gousios
#
11 48130e66 Georgios Gousios
#  2. Redistributions in binary form must reproduce the above copyright
12 48130e66 Georgios Gousios
#     notice, this list of conditions and the following disclaimer in the
13 48130e66 Georgios Gousios
#     documentation and/or other materials provided with the distribution.
14 09c936a7 Vassilios Karakoidas
#
15 48130e66 Georgios Gousios
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16 48130e66 Georgios Gousios
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 48130e66 Georgios Gousios
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 48130e66 Georgios Gousios
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19 48130e66 Georgios Gousios
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 48130e66 Georgios Gousios
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 48130e66 Georgios Gousios
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 48130e66 Georgios Gousios
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 48130e66 Georgios Gousios
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 48130e66 Georgios Gousios
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 48130e66 Georgios Gousios
# SUCH DAMAGE.
26 09c936a7 Vassilios Karakoidas
#
27 48130e66 Georgios Gousios
# The views and conclusions contained in the software and documentation are
28 48130e66 Georgios Gousios
# those of the authors and should not be interpreted as representing official
29 48130e66 Georgios Gousios
# policies, either expressed or implied, of GRNET S.A.
30 48130e66 Georgios Gousios
31 48130e66 Georgios Gousios
# Provides automated tests for logic module
32 92c53da1 Vassilios Karakoidas
33 c25cc9ec Vangelis Koukis
import time
34 c25cc9ec Vangelis Koukis
import hashlib
35 c25cc9ec Vangelis Koukis
from random import randint
36 c25cc9ec Vangelis Koukis
37 c25cc9ec Vangelis Koukis
from django.test import TestCase
38 c25cc9ec Vangelis Koukis
from django.conf import settings
39 c25cc9ec Vangelis Koukis
40 420f2c20 Georgios Gousios
from synnefo.db.models import *
41 03805fc8 Vangelis Koukis
from synnefo.logic import backend
42 9fea53cc Vangelis Koukis
from synnefo.logic import reconciliation
43 95aee02c Vangelis Koukis
from synnefo.logic.utils import get_rsapi_state
44 95aee02c Vangelis Koukis
45 92c53da1 Vassilios Karakoidas
46 95aee02c Vangelis Koukis
class ProcessOpStatusTestCase(TestCase):
47 95aee02c Vangelis Koukis
    fixtures = ['db_test_data']
48 95aee02c Vangelis Koukis
    msg_op = {
49 95aee02c Vangelis Koukis
        'instance': 'instance-name',
50 95aee02c Vangelis Koukis
        'type': 'ganeti-op-status',
51 95aee02c Vangelis Koukis
        'operation': 'OP_INSTANCE_STARTUP',
52 95aee02c Vangelis Koukis
        'jobId': 0,
53 95aee02c Vangelis Koukis
        'status': 'success',
54 95aee02c Vangelis Koukis
        'logmsg': 'unittest - simulated message'
55 95aee02c Vangelis Koukis
    }
56 95aee02c Vangelis Koukis
57 95aee02c Vangelis Koukis
    def test_op_startup_success(self):
58 95aee02c Vangelis Koukis
        """Test notification for successful OP_INSTANCE_START"""
59 95aee02c Vangelis Koukis
        msg = self.msg_op
60 95aee02c Vangelis Koukis
        msg['operation'] = 'OP_INSTANCE_STARTUP'
61 95aee02c Vangelis Koukis
        msg['status'] = 'success'
62 95aee02c Vangelis Koukis
63 95aee02c Vangelis Koukis
        # This machine is initially in BUILD
64 95aee02c Vangelis Koukis
        vm = VirtualMachine.objects.get(pk=30002)
65 95aee02c Vangelis Koukis
        backend.process_op_status(vm, msg["jobId"], msg["operation"],
66 95aee02c Vangelis Koukis
                                  msg["status"], msg["logmsg"])
67 95aee02c Vangelis Koukis
        self.assertEquals(get_rsapi_state(vm), 'ACTIVE')
68 95aee02c Vangelis Koukis
69 95aee02c Vangelis Koukis
    def test_op_shutdown_success(self):
70 95aee02c Vangelis Koukis
        """Test notification for successful OP_INSTANCE_SHUTDOWN"""
71 95aee02c Vangelis Koukis
        msg = self.msg_op
72 95aee02c Vangelis Koukis
        msg['operation'] = 'OP_INSTANCE_SHUTDOWN'
73 95aee02c Vangelis Koukis
        msg['status'] = 'success'
74 95aee02c Vangelis Koukis
75 95aee02c Vangelis Koukis
        # This machine is initially in BUILD
76 95aee02c Vangelis Koukis
        vm = VirtualMachine.objects.get(pk=30002)
77 95aee02c Vangelis Koukis
        backend.process_op_status(vm, msg["jobId"], msg["operation"],
78 95aee02c Vangelis Koukis
                                  msg["status"], msg["logmsg"])
79 95aee02c Vangelis Koukis
        self.assertEquals(get_rsapi_state(vm), 'STOPPED')
80 95aee02c Vangelis Koukis
81 95aee02c Vangelis Koukis
    def test_op_reboot_success(self):
82 95aee02c Vangelis Koukis
        """Test notification for successful OP_INSTANCE_REBOOT"""
83 95aee02c Vangelis Koukis
        msg = self.msg_op
84 95aee02c Vangelis Koukis
        msg['operation'] = 'OP_INSTANCE_REBOOT'
85 95aee02c Vangelis Koukis
        msg['status'] = 'success'
86 95aee02c Vangelis Koukis
87 95aee02c Vangelis Koukis
        # This machine is initially in BUILD
88 95aee02c Vangelis Koukis
        vm = VirtualMachine.objects.get(pk=30002)
89 95aee02c Vangelis Koukis
        backend.process_op_status(vm, msg["jobId"], msg["operation"],
90 95aee02c Vangelis Koukis
                                  msg["status"], msg["logmsg"])
91 95aee02c Vangelis Koukis
        self.assertEquals(get_rsapi_state(vm), 'ACTIVE')
92 95aee02c Vangelis Koukis
93 95aee02c Vangelis Koukis
    def test_op_create_success(self):
94 95aee02c Vangelis Koukis
        """Test notification for successful OP_INSTANCE_CREATE"""
95 95aee02c Vangelis Koukis
        msg = self.msg_op
96 95aee02c Vangelis Koukis
        msg['operation'] = 'OP_INSTANCE_CREATE'
97 95aee02c Vangelis Koukis
        msg['status'] = 'success'
98 95aee02c Vangelis Koukis
99 95aee02c Vangelis Koukis
        # This machine is initially in BUILD
100 95aee02c Vangelis Koukis
        vm = VirtualMachine.objects.get(pk=30002)
101 95aee02c Vangelis Koukis
        backend.process_op_status(vm, msg["jobId"], msg["operation"],
102 95aee02c Vangelis Koukis
                                  msg["status"], msg["logmsg"])
103 95aee02c Vangelis Koukis
        self.assertEquals(get_rsapi_state(vm), 'ACTIVE')
104 95aee02c Vangelis Koukis
105 95aee02c Vangelis Koukis
    def test_op_remove_success(self):
106 95aee02c Vangelis Koukis
        """Test notification for successful OP_INSTANCE_REMOVE"""
107 95aee02c Vangelis Koukis
        msg = self.msg_op
108 95aee02c Vangelis Koukis
        msg['operation'] = 'OP_INSTANCE_REMOVE'
109 95aee02c Vangelis Koukis
        msg['status'] = 'success'
110 95aee02c Vangelis Koukis
111 95aee02c Vangelis Koukis
        # This machine is initially in BUILD
112 95aee02c Vangelis Koukis
        vm = VirtualMachine.objects.get(pk=30002)
113 95aee02c Vangelis Koukis
        backend.process_op_status(vm, msg["jobId"], msg["operation"],
114 95aee02c Vangelis Koukis
                                  msg["status"], msg["logmsg"])
115 95aee02c Vangelis Koukis
        self.assertEquals(get_rsapi_state(vm), 'DELETED')
116 95aee02c Vangelis Koukis
        self.assertTrue(vm.deleted)
117 95aee02c Vangelis Koukis
118 95aee02c Vangelis Koukis
    def test_op_create_error(self):
119 95aee02c Vangelis Koukis
        """Test notification for failed OP_INSTANCE_CREATE"""
120 95aee02c Vangelis Koukis
        msg = self.msg_op
121 95aee02c Vangelis Koukis
        msg['operation'] = 'OP_INSTANCE_CREATE'
122 95aee02c Vangelis Koukis
        msg['status'] = 'error'
123 95aee02c Vangelis Koukis
124 95aee02c Vangelis Koukis
        # This machine is initially in BUILD
125 95aee02c Vangelis Koukis
        vm = VirtualMachine.objects.get(pk=30002)
126 95aee02c Vangelis Koukis
        backend.process_op_status(vm, msg["jobId"], msg["operation"],
127 95aee02c Vangelis Koukis
                                  msg["status"], msg["logmsg"])
128 95aee02c Vangelis Koukis
        self.assertEquals(get_rsapi_state(vm), 'ERROR')
129 95aee02c Vangelis Koukis
        self.assertFalse(vm.deleted)
130 95aee02c Vangelis Koukis
131 95aee02c Vangelis Koukis
    def test_remove_machine_in_error(self):
132 95aee02c Vangelis Koukis
        """Test notification for failed OP_INSTANCE_REMOVE, server in ERROR"""
133 95aee02c Vangelis Koukis
        msg = self.msg_op
134 95aee02c Vangelis Koukis
        msg['operation'] = 'OP_INSTANCE_REMOVE'
135 95aee02c Vangelis Koukis
        msg['status'] = 'error'
136 95aee02c Vangelis Koukis
137 95aee02c Vangelis Koukis
        # This machine is initially in BUILD
138 95aee02c Vangelis Koukis
        vm = VirtualMachine.objects.get(pk=30002)
139 95aee02c Vangelis Koukis
        backend.process_op_status(vm, 0, "OP_INSTANCE_CREATE", "error", "test")
140 95aee02c Vangelis Koukis
        self.assertEquals(get_rsapi_state(vm), 'ERROR')
141 95aee02c Vangelis Koukis
142 95aee02c Vangelis Koukis
        backend.process_op_status(vm, msg["jobId"], msg["operation"],
143 95aee02c Vangelis Koukis
                                  msg["status"], msg["logmsg"])
144 95aee02c Vangelis Koukis
        self.assertEquals(get_rsapi_state(vm), 'DELETED')
145 95aee02c Vangelis Koukis
        self.assertTrue(vm.deleted)
146 03805fc8 Vangelis Koukis
147 03805fc8 Vangelis Koukis
148 03805fc8 Vangelis Koukis
class ProcessNetStatusTestCase(TestCase):
149 03805fc8 Vangelis Koukis
    fixtures = ['db_test_data']
150 c25cc9ec Vangelis Koukis
151 03805fc8 Vangelis Koukis
    def test_set_ipv4(self):
152 03805fc8 Vangelis Koukis
        """Test reception of a net status notification"""
153 03805fc8 Vangelis Koukis
        msg = {'instance': 'instance-name',
154 03805fc8 Vangelis Koukis
               'type':     'ganeti-net-status',
155 03805fc8 Vangelis Koukis
               'nics': [
156 03805fc8 Vangelis Koukis
                   {'ip': '192.168.33.1', 'mac': 'aa:00:00:58:1e:b9'}
157 03805fc8 Vangelis Koukis
               ]
158 03805fc8 Vangelis Koukis
        }
159 03805fc8 Vangelis Koukis
        vm = VirtualMachine.objects.get(pk=30000)
160 03805fc8 Vangelis Koukis
        backend.process_net_status(vm, msg['nics'])
161 f533f224 Vangelis Koukis
        self.assertEquals(vm.nics.all()[0].ipv4, '192.168.33.1')
162 03805fc8 Vangelis Koukis
163 03805fc8 Vangelis Koukis
    def test_set_empty_ipv4(self):
164 03805fc8 Vangelis Koukis
        """Test reception of a net status notification with no IPv4 assigned"""
165 03805fc8 Vangelis Koukis
        msg = {'instance': 'instance-name',
166 03805fc8 Vangelis Koukis
               'type':     'ganeti-net-status',
167 03805fc8 Vangelis Koukis
               'nics': [
168 03805fc8 Vangelis Koukis
                   {'ip': '', 'mac': 'aa:00:00:58:1e:b9'}
169 03805fc8 Vangelis Koukis
               ]
170 03805fc8 Vangelis Koukis
        }
171 03805fc8 Vangelis Koukis
        vm = VirtualMachine.objects.get(pk=30000)
172 03805fc8 Vangelis Koukis
        backend.process_net_status(vm, msg['nics'])
173 f533f224 Vangelis Koukis
        self.assertEquals(vm.nics.all()[0].ipv4, '')
174 ec63ae8a Georgios Gousios
175 ec63ae8a Georgios Gousios
176 9068cd85 Georgios Gousios
class ProcessProgressUpdateTestCase(TestCase):
177 c25cc9ec Vangelis Koukis
    fixtures = ['db_test_data']
178 9068cd85 Georgios Gousios
179 9068cd85 Georgios Gousios
    def test_progress_update(self):
180 c25cc9ec Vangelis Koukis
        """Test reception of a create progress notification"""
181 9068cd85 Georgios Gousios
182 c25cc9ec Vangelis Koukis
        # This machine is in BUILD
183 9068cd85 Georgios Gousios
        vm = VirtualMachine.objects.get(pk=30002)
184 c25cc9ec Vangelis Koukis
        rprogress = randint(10, 100)
185 9068cd85 Georgios Gousios
186 c25cc9ec Vangelis Koukis
        backend.process_create_progress(vm, rprogress, 0)
187 c25cc9ec Vangelis Koukis
        self.assertEquals(vm.buildpercentage, rprogress)
188 9068cd85 Georgios Gousios
189 2c980c7e Vangelis Koukis
        #self.assertRaises(ValueError, backend.process_create_progress,
190 2c980c7e Vangelis Koukis
        #                  vm, 9, 0)
191 c25cc9ec Vangelis Koukis
        self.assertRaises(ValueError, backend.process_create_progress,
192 c25cc9ec Vangelis Koukis
                          vm, -1, 0)
193 c25cc9ec Vangelis Koukis
        self.assertRaises(ValueError, backend.process_create_progress,
194 c25cc9ec Vangelis Koukis
                          vm, 'a', 0)
195 c25cc9ec Vangelis Koukis
196 c25cc9ec Vangelis Koukis
        # This machine is ACTIVE
197 c25cc9ec Vangelis Koukis
        #vm = VirtualMachine.objects.get(pk=30000)
198 c25cc9ec Vangelis Koukis
        #self.assertRaises(VirtualMachine.IllegalState,
199 c25cc9ec Vangelis Koukis
        #                  backend.process_create_progress, vm, 1)
200 9068cd85 Georgios Gousios
201 9fea53cc Vangelis Koukis
202 9fea53cc Vangelis Koukis
class ReconciliationTestCase(TestCase):
203 9fea53cc Vangelis Koukis
    SERVERS = 1000
204 9fea53cc Vangelis Koukis
    fixtures = ['db_test_data']
205 9fea53cc Vangelis Koukis
206 9fea53cc Vangelis Koukis
    def test_get_servers_from_db(self):
207 9fea53cc Vangelis Koukis
        """Test getting a dictionary from each server to its operstate"""
208 9fea53cc Vangelis Koukis
        reconciliation.get_servers_from_db()
209 9fea53cc Vangelis Koukis
        self.assertEquals(reconciliation.get_servers_from_db(),
210 9fea53cc Vangelis Koukis
                          {30000: 'STARTED', 30001: 'STOPPED', 30002: 'BUILD'})
211 9fea53cc Vangelis Koukis
212 9fea53cc Vangelis Koukis
    def test_stale_servers_in_db(self):
213 9fea53cc Vangelis Koukis
        """Test discovery of stale entries in DB"""
214 9fea53cc Vangelis Koukis
215 9fea53cc Vangelis Koukis
        D = {1: 'STARTED', 2: 'STOPPED', 3: 'STARTED', 4: 'BUILD', 5: 'BUILD'}
216 9fea53cc Vangelis Koukis
        G = {1: True, 3: True}
217 9fea53cc Vangelis Koukis
        self.assertEquals(reconciliation.stale_servers_in_db(D, G),
218 46704e06 Vangelis Koukis
                          set([2, 4, 5]))
219 9fea53cc Vangelis Koukis
220 9fea53cc Vangelis Koukis
    def test_orphan_instances_in_ganeti(self):
221 9fea53cc Vangelis Koukis
        """Test discovery of orphan instances in Ganeti, without a DB entry"""
222 9fea53cc Vangelis Koukis
223 9fea53cc Vangelis Koukis
        G = {1: True, 2: False, 3: False, 4: True, 50: True}
224 9fea53cc Vangelis Koukis
        D = {1: True, 3: False}
225 9fea53cc Vangelis Koukis
        self.assertEquals(reconciliation.orphan_instances_in_ganeti(D, G),
226 46704e06 Vangelis Koukis
                          set([2, 4, 50]))
227 9fea53cc Vangelis Koukis
228 9fea53cc Vangelis Koukis
    def test_unsynced_operstate(self):
229 9fea53cc Vangelis Koukis
        """Test discovery of unsynced operstate between the DB and Ganeti"""
230 9fea53cc Vangelis Koukis
231 9fea53cc Vangelis Koukis
        G = {1: True, 2: False, 3: True, 4: False, 50: True}
232 9fea53cc Vangelis Koukis
        D = {1: 'STARTED', 2: 'STARTED', 3: 'BUILD', 4: 'STARTED', 50: 'BUILD'}
233 9fea53cc Vangelis Koukis
        self.assertEquals(reconciliation.unsynced_operstate(D, G),
234 46704e06 Vangelis Koukis
                          set([(2, 'STARTED', False),
235 9fea53cc Vangelis Koukis
                           (3, 'BUILD', True), (4, 'STARTED', False),
236 46704e06 Vangelis Koukis
                           (50, 'BUILD', True)]))