~midokura/nova/network-refactoring

« back to all changes in this revision

Viewing changes to nova/tests/network/base.py

  • Committer: Ryu Ishimoto
  • Date: 2011-06-30 03:39:04 UTC
  • mfrom: (1118.1.112 nova)
  • Revision ID: ryu@midokura.jp-20110630033904-7l6m3wzdyx95tq01
Merged trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
2
 
3
 
# Copyright 2011 Rackspace
 
3
# Copyright 2010 United States Government as represented by the
 
4
# Administrator of the National Aeronautics and Space Administration.
4
5
# All Rights Reserved.
5
6
#
6
 
# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
 
# not use this file except in compliance with the License. You may obtain
8
 
# a copy of the License at
9
 
#
10
 
#      http://www.apache.org/licenses/LICENSE-2.0
11
 
#
12
 
# Unless required by applicable law or agreed to in writing, software
13
 
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 
# License for the specific language governing permissions and limitations
16
 
# under the License.
 
7
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
 
8
#    not use this file except in compliance with the License. You may obtain
 
9
#    a copy of the License at
 
10
#
 
11
#         http://www.apache.org/licenses/LICENSE-2.0
 
12
#
 
13
#    Unless required by applicable law or agreed to in writing, software
 
14
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 
15
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 
16
#    License for the specific language governing permissions and limitations
 
17
#    under the License.
 
18
"""
 
19
Base class of Unit Tests for all network models
 
20
"""
 
21
import netaddr
 
22
import os
17
23
 
18
24
from nova import context
19
25
from nova import db
 
26
from nova import exception
20
27
from nova import flags
 
28
from nova import ipv6
21
29
from nova import log as logging
22
30
from nova import test
23
31
from nova import utils
24
32
from nova.auth import manager
25
 
from nova.tests.db import fakes as db_fakes
26
33
 
27
34
FLAGS = flags.FLAGS
28
35
LOG = logging.getLogger('nova.tests.network')
29
36
 
30
37
 
31
38
class NetworkTestCase(test.TestCase):
 
39
    """Test cases for network code"""
32
40
    def setUp(self):
33
41
        super(NetworkTestCase, self).setUp()
 
42
        # NOTE(vish): if you change these flags, make sure to change the
 
43
        #             flags in the corresponding section in nova-dhcpbridge
34
44
        self.flags(connection_type='fake',
35
45
                   fake_call=True,
36
 
                   fake_network=True,
37
 
                   network_manager=self.network_manager)
 
46
                   fake_network=True)
38
47
        self.manager = manager.AuthManager()
39
 
        self.user = self.manager.create_user('netuser',
40
 
                                             'netuser',
41
 
                                             'netuser')
 
48
        self.user = self.manager.create_user('netuser', 'netuser', 'netuser')
42
49
        self.projects = []
43
50
        self.network = utils.import_object(FLAGS.network_manager)
44
 
        db_fakes.stub_out_db_network_api(self.stubs)
45
 
        self.network.db = db
46
 
        self.network.network_api.db = db
47
 
        self.context = context.RequestContext(project='fake', user=self.user)
 
51
        self.context = context.RequestContext(project=None, user=self.user)
 
52
        for i in range(FLAGS.num_networks):
 
53
            name = 'project%s' % i
 
54
            project = self.manager.create_project(name, 'netuser', name)
 
55
            self.projects.append(project)
 
56
            # create the necessary network data for the project
 
57
            user_context = context.RequestContext(project=self.projects[i],
 
58
                                                     user=self.user)
 
59
            host = self.network.get_network_host(user_context.elevated())
 
60
        instance_ref = self._create_instance(0)
 
61
        self.instance_id = instance_ref['id']
 
62
        instance_ref = self._create_instance(1)
 
63
        self.instance2_id = instance_ref['id']
48
64
 
49
65
    def tearDown(self):
 
66
        # TODO(termie): this should really be instantiating clean datastores
 
67
        #               in between runs, one failure kills all the tests
 
68
        db.instance_destroy(context.get_admin_context(), self.instance_id)
 
69
        db.instance_destroy(context.get_admin_context(), self.instance2_id)
 
70
        for project in self.projects:
 
71
            self.manager.delete_project(project)
 
72
        self.manager.delete_user(self.user)
50
73
        super(NetworkTestCase, self).tearDown()
51
 
        self.manager.delete_user(self.user.id)
52
 
        reload(db)
53
 
 
54
 
 
55
 
class TestFuncs(object):
56
 
    def _compare_fields(self, dict1, dict2, fields):
57
 
        for field in fields:
58
 
            self.assertEqual(dict1[field], dict2[field])
59
 
 
60
 
    def test_set_network_hosts(self):
61
 
        self.network.set_network_hosts(self.context)
62
 
 
63
 
    def test_set_network_host(self):
64
 
        host = self.network.host
65
 
        self.assertEqual(self.network.set_network_host(self.context, 0),
66
 
                         host)
67
 
 
68
 
    def test_allocate_for_instance(self):
69
 
        instance_id = 0
70
 
        project_id = self.context.project_id
71
 
        type_id = 0
72
 
        self.network.set_network_hosts(self.context)
73
 
        nw = self.network.allocate_for_instance(self.context,
74
 
                                                instance_id=instance_id,
75
 
                                                project_id=project_id,
76
 
                                                instance_type_id=type_id)
77
 
        static_info = [({'bridge': 'fa0', 'id': 0},
78
 
                        {'broadcast': '192.168.0.255',
79
 
                         'dns': ['192.168.0.1'],
80
 
                         'gateway': '192.168.0.1',
81
 
                         'gateway6': 'dead:beef::1',
82
 
                         'ip6s': [{'enabled': '1',
83
 
                                   'ip': 'dead:beef::dcad:beff:feef:0',
84
 
                                         'netmask': '64'}],
85
 
                         'ips': [{'enabled': '1',
86
 
                                  'ip': '192.168.0.100',
87
 
                                  'netmask': '255.255.255.0'}],
88
 
                         'label': 'fake',
89
 
                         'mac': 'DE:AD:BE:EF:00:00',
90
 
                         'rxtx_cap': 3})]
91
 
 
92
 
        self._compare_fields(nw[0][0], static_info[0][0], ('bridge',))
93
 
        self._compare_fields(nw[0][1], static_info[0][1], ('ips',
94
 
                                                           'broadcast',
95
 
                                                           'gateway',
96
 
                                                           'ip6s'))
97
 
 
98
 
    def test_deallocate_for_instance(self):
99
 
        instance_id = 0
100
 
        network_id = 0
101
 
        self.network.set_network_hosts(self.context)
102
 
        self.network.add_fixed_ip_to_instance(self.context,
103
 
                                              instance_id=instance_id,
104
 
                                              network_id=network_id)
105
 
        ips = db.fixed_ip_get_by_instance(self.context, instance_id)
106
 
        for ip in ips:
107
 
            self.assertTrue(ip['allocated'])
108
 
        self.network.deallocate_for_instance(self.context,
109
 
                                             instance_id=instance_id)
110
 
        ips = db.fixed_ip_get_by_instance(self.context, instance_id)
111
 
        for ip in ips:
112
 
            self.assertFalse(ip['allocated'])
113
 
 
114
 
    def test_lease_release_fixed_ip(self):
115
 
        instance_id = 0
116
 
        project_id = self.context.project_id
117
 
        type_id = 0
118
 
        self.network.set_network_hosts(self.context)
119
 
        nw = self.network.allocate_for_instance(self.context,
120
 
                                                instance_id=instance_id,
121
 
                                                project_id=project_id,
122
 
                                                instance_type_id=type_id)
123
 
        self.assertTrue(nw)
124
 
        self.assertTrue(nw[0])
125
 
        network_id = nw[0][0]['id']
126
 
 
127
 
        ips = db.fixed_ip_get_by_instance(self.context, instance_id)
128
 
        vif = db.virtual_interface_get_by_instance_and_network(self.context,
129
 
                                                               instance_id,
130
 
                                                               network_id)
131
 
        self.assertTrue(ips)
132
 
        address = ips[0]['address']
133
 
 
134
 
        db.fixed_ip_associate(self.context, address, instance_id)
135
 
        db.fixed_ip_update(self.context, address,
136
 
                           {'virtual_interface_id': vif['id']})
137
 
 
138
 
        self.network.lease_fixed_ip(self.context, vif['address'], address)
139
 
        ip = db.fixed_ip_get_by_address(self.context, address)
140
 
        self.assertTrue(ip['leased'])
141
 
 
142
 
        self.network.release_fixed_ip(self.context, vif['address'], address)
143
 
        ip = db.fixed_ip_get_by_address(self.context, address)
144
 
        self.assertFalse(ip['leased'])
 
74
 
 
75
    def _create_instance(self, project_num, mac=None):
 
76
        if not mac:
 
77
            mac = utils.generate_mac()
 
78
        project = self.projects[project_num]
 
79
        self.context._project = project
 
80
        self.context.project_id = project.id
 
81
        return db.instance_create(self.context,
 
82
                                  {'project_id': project.id,
 
83
                                   'mac_address': mac})
 
84
 
 
85
    def _create_address(self, project_num, instance_id=None):
 
86
        """Create an address in given project num"""
 
87
        if instance_id is None:
 
88
            instance_id = self.instance_id
 
89
        self.context._project = self.projects[project_num]
 
90
        self.context.project_id = self.projects[project_num].id
 
91
        return self.network.allocate_fixed_ip(self.context, instance_id)
 
92
 
 
93
    def _deallocate_address(self, project_num, address):
 
94
        self.context._project = self.projects[project_num]
 
95
        self.context.project_id = self.projects[project_num].id
 
96
        self.network.deallocate_fixed_ip(self.context, address)
 
97
 
 
98
    def _is_allocated_in_project(self, address, project_id):
 
99
        """Returns true if address is in specified project"""
 
100
        project_net = db.network_get_by_bridge(context.get_admin_context(),
 
101
                                           FLAGS.flat_network_bridge)
 
102
        network = db.fixed_ip_get_network(context.get_admin_context(),
 
103
                                          address)
 
104
        instance = db.fixed_ip_get_instance(context.get_admin_context(),
 
105
                                            address)
 
106
        # instance exists until release
 
107
        return instance is not None and network['id'] == project_net['id']
 
108
 
 
109
    def test_private_ipv6(self):
 
110
        """Make sure ipv6 is OK"""
 
111
        if FLAGS.use_ipv6:
 
112
            instance_ref = self._create_instance(0)
 
113
            address = self._create_address(0, instance_ref['id'])
 
114
            network_ref = db.project_get_network(
 
115
                                                 context.get_admin_context(),
 
116
                                                 self.context.project_id)
 
117
            address_v6 = db.instance_get_fixed_address_v6(
 
118
                                                 context.get_admin_context(),
 
119
                                                 instance_ref['id'])
 
120
            self.assertEqual(instance_ref['mac_address'],
 
121
                             ipv6.to_mac(address_v6))
 
122
            instance_ref2 = db.fixed_ip_get_instance_v6(
 
123
                                                 context.get_admin_context(),
 
124
                                                 address_v6)
 
125
            self.assertEqual(instance_ref['id'], instance_ref2['id'])
 
126
            self.assertEqual(address_v6,
 
127
                             ipv6.to_global(network_ref['cidr_v6'],
 
128
                                            instance_ref['mac_address'],
 
129
                                            'test'))
 
130
            self._deallocate_address(0, address)
 
131
            db.instance_destroy(context.get_admin_context(),
 
132
                                instance_ref['id'])
 
133
 
 
134
    def test_available_ips(self):
 
135
        """Make sure the number of available ips for the network is correct
 
136
 
 
137
        The number of available IP addresses depends on the test
 
138
        environment's setup.
 
139
 
 
140
        Network size is set in test fixture's setUp method.
 
141
 
 
142
        There are ips reserved at the bottom and top of the range.
 
143
        services (network, gateway, CloudPipe, broadcast)
 
144
        """
 
145
        network = db.project_get_network(context.get_admin_context(),
 
146
                                         self.projects[0].id)
 
147
        net_size = flags.FLAGS.network_size
 
148
        admin_context = context.get_admin_context()
 
149
        total_ips = (db.network_count_available_ips(admin_context,
 
150
                                                    network['id']) +
 
151
                     db.network_count_reserved_ips(admin_context,
 
152
                                                   network['id']) +
 
153
                     db.network_count_allocated_ips(admin_context,
 
154
                                                    network['id']))
 
155
        self.assertEqual(total_ips, net_size)