Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / logic / test / servers.py @ 5962d9a6

History | View | Annotate | Download (4.2 kB)

1 5962d9a6 Christos Stavrakakis
# vim: set fileencoding=utf-8 :
2 5962d9a6 Christos Stavrakakis
# Copyright 2013 GRNET S.A. All rights reserved.
3 5962d9a6 Christos Stavrakakis
#
4 5962d9a6 Christos Stavrakakis
# Redistribution and use in source and binary forms, with or without
5 5962d9a6 Christos Stavrakakis
# modification, are permitted provided that the following conditions
6 5962d9a6 Christos Stavrakakis
# are met:
7 5962d9a6 Christos Stavrakakis
#
8 5962d9a6 Christos Stavrakakis
#   1. Redistributions of source code must retain the above copyright
9 5962d9a6 Christos Stavrakakis
#      notice, this list of conditions and the following disclaimer.
10 5962d9a6 Christos Stavrakakis
#
11 5962d9a6 Christos Stavrakakis
#  2. Redistributions in binary form must reproduce the above copyright
12 5962d9a6 Christos Stavrakakis
#     notice, this list of conditions and the following disclaimer in the
13 5962d9a6 Christos Stavrakakis
#     documentation and/or other materials provided with the distribution.
14 5962d9a6 Christos Stavrakakis
#
15 5962d9a6 Christos Stavrakakis
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16 5962d9a6 Christos Stavrakakis
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 5962d9a6 Christos Stavrakakis
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 5962d9a6 Christos Stavrakakis
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19 5962d9a6 Christos Stavrakakis
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 5962d9a6 Christos Stavrakakis
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 5962d9a6 Christos Stavrakakis
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 5962d9a6 Christos Stavrakakis
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 5962d9a6 Christos Stavrakakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 5962d9a6 Christos Stavrakakis
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 5962d9a6 Christos Stavrakakis
# SUCH DAMAGE.
26 5962d9a6 Christos Stavrakakis
#
27 5962d9a6 Christos Stavrakakis
# The views and conclusions contained in the software and documentation are
28 5962d9a6 Christos Stavrakakis
# those of the authors and should not be interpreted as representing official
29 5962d9a6 Christos Stavrakakis
# policies, either expressed or implied, of GRNET S.A.
30 5962d9a6 Christos Stavrakakis
31 5962d9a6 Christos Stavrakakis
# Provides automated tests for logic module
32 5962d9a6 Christos Stavrakakis
from django.test import TestCase
33 5962d9a6 Christos Stavrakakis
#from snf_django.utils.testing import mocked_quotaholder
34 5962d9a6 Christos Stavrakakis
from synnefo.logic import servers
35 5962d9a6 Christos Stavrakakis
from synnefo.db import models_factory as mfactory
36 5962d9a6 Christos Stavrakakis
from mock import patch
37 5962d9a6 Christos Stavrakakis
38 5962d9a6 Christos Stavrakakis
39 5962d9a6 Christos Stavrakakis
@patch("synnefo.logic.rapi_pool.GanetiRapiClient")
40 5962d9a6 Christos Stavrakakis
class ServerTest(TestCase):
41 5962d9a6 Christos Stavrakakis
    def test_connect_network(self, mrapi):
42 5962d9a6 Christos Stavrakakis
        # Common connect
43 5962d9a6 Christos Stavrakakis
        net = mfactory.NetworkFactory(subnet="192.168.2.0/24",
44 5962d9a6 Christos Stavrakakis
                                      gateway="192.168.2.1",
45 5962d9a6 Christos Stavrakakis
                                      state="ACTIVE",
46 5962d9a6 Christos Stavrakakis
                                      dhcp=True,
47 5962d9a6 Christos Stavrakakis
                                      flavor="CUSTOM")
48 5962d9a6 Christos Stavrakakis
        vm = mfactory.VirtualMachineFactory()
49 5962d9a6 Christos Stavrakakis
        mfactory.BackendNetworkFactory(network=net, backend=vm.backend)
50 5962d9a6 Christos Stavrakakis
        mrapi().ModifyInstance.return_value = 42
51 5962d9a6 Christos Stavrakakis
        servers.connect(vm, net)
52 5962d9a6 Christos Stavrakakis
        pool = net.get_pool(with_lock=False)
53 5962d9a6 Christos Stavrakakis
        self.assertFalse(pool.is_available("192.168.2.2"))
54 5962d9a6 Christos Stavrakakis
        args, kwargs = mrapi().ModifyInstance.call_args
55 5962d9a6 Christos Stavrakakis
        nics = kwargs["nics"][0]
56 5962d9a6 Christos Stavrakakis
        self.assertEqual(args[0], vm.backend_vm_id)
57 5962d9a6 Christos Stavrakakis
        self.assertEqual(nics[0], "add")
58 5962d9a6 Christos Stavrakakis
        self.assertEqual(nics[1]["ip"], "192.168.2.2")
59 5962d9a6 Christos Stavrakakis
        self.assertEqual(nics[1]["network"], net.backend_id)
60 5962d9a6 Christos Stavrakakis
61 5962d9a6 Christos Stavrakakis
        # No dhcp
62 5962d9a6 Christos Stavrakakis
        vm = mfactory.VirtualMachineFactory()
63 5962d9a6 Christos Stavrakakis
        net = mfactory.NetworkFactory(subnet="192.168.2.0/24",
64 5962d9a6 Christos Stavrakakis
                                      gateway="192.168.2.1",
65 5962d9a6 Christos Stavrakakis
                                      state="ACTIVE",
66 5962d9a6 Christos Stavrakakis
                                      dhcp=False)
67 5962d9a6 Christos Stavrakakis
        mfactory.BackendNetworkFactory(network=net, backend=vm.backend)
68 5962d9a6 Christos Stavrakakis
        servers.connect(vm, net)
69 5962d9a6 Christos Stavrakakis
        pool = net.get_pool(with_lock=False)
70 5962d9a6 Christos Stavrakakis
        self.assertTrue(pool.is_available("192.168.2.2"))
71 5962d9a6 Christos Stavrakakis
        args, kwargs = mrapi().ModifyInstance.call_args
72 5962d9a6 Christos Stavrakakis
        nics = kwargs["nics"][0]
73 5962d9a6 Christos Stavrakakis
        self.assertEqual(args[0], vm.backend_vm_id)
74 5962d9a6 Christos Stavrakakis
        self.assertEqual(nics[0], "add")
75 5962d9a6 Christos Stavrakakis
        self.assertEqual(nics[1]["ip"], None)
76 5962d9a6 Christos Stavrakakis
        self.assertEqual(nics[1]["network"], net.backend_id)
77 5962d9a6 Christos Stavrakakis
78 5962d9a6 Christos Stavrakakis
        # Test connect to IPv6 only network
79 5962d9a6 Christos Stavrakakis
        vm = mfactory.VirtualMachineFactory()
80 5962d9a6 Christos Stavrakakis
        net = mfactory.NetworkFactory(subnet6="2000::/64",
81 5962d9a6 Christos Stavrakakis
                                      state="ACTIVE",
82 5962d9a6 Christos Stavrakakis
                                      gateway="2000::1")
83 5962d9a6 Christos Stavrakakis
        mfactory.BackendNetworkFactory(network=net, backend=vm.backend)
84 5962d9a6 Christos Stavrakakis
        servers.connect(vm, net)
85 5962d9a6 Christos Stavrakakis
        args, kwargs = mrapi().ModifyInstance.call_args
86 5962d9a6 Christos Stavrakakis
        nics = kwargs["nics"][0]
87 5962d9a6 Christos Stavrakakis
        self.assertEqual(args[0], vm.backend_vm_id)
88 5962d9a6 Christos Stavrakakis
        self.assertEqual(nics[0], "add")
89 5962d9a6 Christos Stavrakakis
        self.assertEqual(nics[1]["ip"], None)
90 5962d9a6 Christos Stavrakakis
        self.assertEqual(nics[1]["network"], net.backend_id)