Statistics
| Branch: | Tag: | Revision:

root / snf-cyclades-app / synnefo / logic / tests / networks.py @ 6e9255ab

History | View | Annotate | Download (6.5 kB)

1
# vim: set fileencoding=utf-8 :
2
# Copyright 2013 GRNET S.A. All rights reserved.
3
#
4
# Redistribution and use in source and binary forms, with or without
5
# modification, are permitted provided that the following conditions
6
# are met:
7
#
8
#   1. Redistributions of source code must retain the above copyright
9
#      notice, this list of conditions and the following disclaimer.
10
#
11
#  2. Redistributions in binary form must reproduce the above copyright
12
#     notice, this list of conditions and the following disclaimer in the
13
#     documentation and/or other materials provided with the distribution.
14
#
15
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
# SUCH DAMAGE.
26
#
27
# The views and conclusions contained in the software and documentation are
28
# those of the authors and should not be interpreted as representing official
29
# policies, either expressed or implied, of GRNET S.A.
30

    
31
# Provides automated tests for logic module
32
from django.test import TestCase
33
from snf_django.lib.api import faults
34
from snf_django.utils.testing import mocked_quotaholder
35
from synnefo.logic import networks
36
from synnefo.db import models_factory as mfactory
37
from synnefo.db.models import BridgePoolTable, MacPrefixPoolTable
38
from synnefo import settings
39
from copy import copy
40

    
41

    
42
#@patch("synnefo.logic.rapi_pool.GanetiRapiClient")
43
class NetworkTest(TestCase):
44
    def test_create(self):
45
        kwargs = {
46
            "name": "test",
47
            "user_id": "user",
48
            "subnet": "192.168.20.0/24",
49
            "flavor": "CUSTOM",
50
        }
51
        # wrong gateway
52
        kw = copy(kwargs)
53
        kw["gateway"] = "192.168.3.1"
54
        self.assertRaises(faults.BadRequest, networks.create, **kw)
55
        # wrong subnet
56
        kw = copy(kwargs)
57
        kw["subnet"] = "192.168.2.0"
58
        self.assertRaises(faults.OverLimit, networks.create, **kw)
59
        kw["subnet"] = "192.168.0.0/16"
60
        self.assertRaises(faults.OverLimit, networks.create, **kw)
61
        kw["subnet"] = "192.168.0.3/24"
62
        self.assertRaises(faults.BadRequest, networks.create, **kw)
63
        # wrong flavor
64
        kw = copy(kwargs)
65
        kw["flavor"] = "UNKNOWN"
66
        self.assertRaises(faults.BadRequest, networks.create, **kw)
67
        # Test create objet
68
        kwargs["gateway"] = "192.168.20.1"
69
        kwargs["public"] = True
70
        kwargs["dhcp"] = False
71
        with mocked_quotaholder():
72
            net = networks.create(**kwargs)
73
        self.assertEqual(net.subnet, "192.168.20.0/24")
74
        self.assertEqual(net.gateway, "192.168.20.1")
75
        self.assertEqual(net.public, True)
76
        self.assertEqual(net.flavor, "CUSTOM")
77
        self.assertEqual(net.dhcp, False)
78
        self.assertEqual(net.action, "CREATE")
79
        self.assertEqual(net.state, "ACTIVE")
80
        self.assertEqual(net.name, "test")
81
        self.assertEqual(net.userid, "user")
82

    
83
        # Test for each for flavor type
84
        # MAC_FILTERED
85
        kwargs["flavor"] = "MAC_FILTERED"
86
        # Test exception if no rules exists
87
        self.assertRaises(faults.ServiceUnavailable, networks.create, **kwargs)
88
        mfactory.MacPrefixPoolTableFactory(base="aa:bb:0")
89
        with mocked_quotaholder():
90
                net = networks.create(**kwargs)
91
        self.assertEqual(net.mode, "bridged")
92
        self.assertEqual(net.mac_prefix, "aa:bb:1")
93
        self.assertEqual(net.link, settings.DEFAULT_MAC_FILTERED_BRIDGE)
94
        self.assertEqual(net.backend_tag, ["private-filtered"])
95
        pool = MacPrefixPoolTable.get_pool()
96
        self.assertFalse(pool.is_available("aa:bb:1"))
97

    
98
        # PHYSICAL_VLAN
99
        kwargs["flavor"] = "PHYSICAL_VLAN"
100
        # Test exception if no rules exists
101
        self.assertRaises(faults.ServiceUnavailable, networks.create, **kwargs)
102
        mfactory.BridgePoolTableFactory(base="prv")
103
        with mocked_quotaholder():
104
            net = networks.create(**kwargs)
105
        self.assertEqual(net.mode, "bridged")
106
        self.assertEqual(net.mac_prefix, settings.DEFAULT_MAC_PREFIX)
107
        self.assertEqual(net.link, "prv1")
108
        self.assertEqual(net.backend_tag, ["physical-vlan"])
109
        pool = BridgePoolTable.get_pool()
110
        self.assertFalse(pool.is_available(net.link))
111

    
112
        # IP_LESS_ROUTED
113
        kwargs["flavor"] = "IP_LESS_ROUTED"
114
        with mocked_quotaholder():
115
            net = networks.create(**kwargs)
116
        self.assertEqual(net.mode, "routed")
117
        self.assertEqual(net.mac_prefix, settings.DEFAULT_MAC_PREFIX)
118
        self.assertEqual(net.link, settings.DEFAULT_ROUTING_TABLE)
119
        self.assertEqual(net.backend_tag, ["ip-less-routed"])
120

    
121
        # CUSTOM
122
        kwargs["flavor"] = "CUSTOM"
123
        with mocked_quotaholder():
124
            net = networks.create(**kwargs)
125
        self.assertEqual(net.mode, "bridged")
126
        self.assertEqual(net.mac_prefix, settings.DEFAULT_MAC_PREFIX)
127
        self.assertEqual(net.link, settings.DEFAULT_BRIDGE)
128
        self.assertEqual(net.backend_tag, [])
129

    
130
    def test_create_network_ipv6(self):
131
        kwargs = {
132
            "name": "test",
133
            "user_id": "user",
134
            "flavor": "CUSTOM",
135
            "subnet6": "2001:648:2ffc:1112::/64",
136
        }
137
        # Wrong subnet
138
        kw = copy(kwargs)
139
        kw["subnet6"] = "2001:64q:2ffc:1112::/64"
140
        self.assertRaises(faults.BadRequest, networks.create, **kw)
141
        # Wrong gateway
142
        kw = copy(kwargs)
143
        kw["gateway6"] = "2001:64q:2ffc:1119::1"
144
        self.assertRaises(faults.BadRequest, networks.create, **kw)
145
        # floating_ip_pools can not be ipv6 only
146
        kw = copy(kwargs)
147
        kw["floating_ip_pool"] = True
148
        self.assertRaises(faults.BadRequest, networks.create, **kw)
149
        kwargs["gateway6"] = "2001:648:2ffc:1112::1"
150
        with mocked_quotaholder():
151
            net = networks.create(**kwargs)
152
        self.assertEqual(net.subnet6, "2001:648:2ffc:1112::/64")
153
        self.assertEqual(net.gateway6, "2001:648:2ffc:1112::1")
154
        self.assertRaises(Exception, net.get_pool)