~raxnetworking/nova/bare_bones_melange

« back to all changes in this revision

Viewing changes to nova/tests/fake_network.py

  • Committer: Rajaram Mallya
  • Date: 2011-09-12 10:03:21 UTC
  • mfrom: (1265.2.287 nova)
  • Revision ID: rajarammallya@gmail.com-20110912100321-8zw8575a206dc026
Merged from nova trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
 
2
 
 
3
# Copyright 2011 Rackspace
 
4
# All Rights Reserved.
 
5
#
 
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.
 
17
 
 
18
from nova import db
 
19
from nova import flags
 
20
from nova import test
 
21
from nova.network import manager as network_manager
 
22
 
 
23
 
 
24
HOST = "testhost"
 
25
FLAGS = flags.FLAGS
 
26
 
 
27
 
 
28
class FakeModel(dict):
 
29
    """Represent a model from the db"""
 
30
    def __init__(self, *args, **kwargs):
 
31
        self.update(kwargs)
 
32
 
 
33
        def __getattr__(self, name):
 
34
            return self[name]
 
35
 
 
36
 
 
37
flavor = {'id': 0,
 
38
          'name': 'fake_flavor',
 
39
          'memory_mb': 2048,
 
40
          'vcpus': 2,
 
41
          'local_gb': 10,
 
42
          'flavor_id': 0,
 
43
          'swap': 0,
 
44
          'rxtx_quota': 0,
 
45
          'rxtx_cap': 3}
 
46
 
 
47
 
 
48
def fake_network(network_id, ipv6=None):
 
49
    if ipv6 is None:
 
50
        ipv6 = FLAGS.use_ipv6
 
51
    fake_network = {'id': network_id,
 
52
                    'label': 'test%d' % network_id,
 
53
                    'injected': False,
 
54
                    'multi_host': False,
 
55
                    'cidr': '192.168.%d.0/24' % network_id,
 
56
                    'cidr_v6': None,
 
57
                    'netmask': '255.255.255.0',
 
58
                    'netmask_v6': None,
 
59
                    'bridge': 'fake_br%d' % network_id,
 
60
                    'bridge_interface': 'fake_eth%d' % network_id,
 
61
                    'gateway': '192.168.%d.1' % network_id,
 
62
                    'gateway_v6': None,
 
63
                    'broadcast': '192.168.%d.255' % network_id,
 
64
                    'dns1': '192.168.%d.3' % network_id,
 
65
                    'dns2': '192.168.%d.4' % network_id,
 
66
                    'vlan': None,
 
67
                    'host': None,
 
68
                    'project_id': 'fake_project',
 
69
                    'vpn_public_address': '192.168.%d.2' % network_id}
 
70
    if ipv6:
 
71
        fake_network['cidr_v6'] = '2001:db8:0:%x::/64' % network_id
 
72
        fake_network['gateway_v6'] = '2001:db8:0:%x::1' % network_id
 
73
        fake_network['netmask_v6'] = '64'
 
74
 
 
75
    return fake_network
 
76
 
 
77
 
 
78
def vifs(n):
 
79
    for x in xrange(n):
 
80
        yield {'id': x,
 
81
               'address': 'DE:AD:BE:EF:00:%02x' % x,
 
82
               'uuid': '00000000-0000-0000-0000-00000000000000%02d' % x,
 
83
               'network_id': x,
 
84
               'network': FakeModel(**fake_network(x)),
 
85
               'instance_id': 0}
 
86
 
 
87
 
 
88
def floating_ip_ids():
 
89
    for i in xrange(99):
 
90
        yield i
 
91
 
 
92
 
 
93
def fixed_ip_ids():
 
94
    for i in xrange(99):
 
95
        yield i
 
96
 
 
97
 
 
98
floating_ip_id = floating_ip_ids()
 
99
fixed_ip_id = fixed_ip_ids()
 
100
 
 
101
 
 
102
def next_fixed_ip(network_id, num_floating_ips=0):
 
103
    next_id = fixed_ip_id.next()
 
104
    f_ips = [FakeModel(**next_floating_ip(next_id))
 
105
             for i in xrange(num_floating_ips)]
 
106
    return {'id': next_id,
 
107
            'network_id': network_id,
 
108
            'address': '192.168.%d.1%02d' % (network_id, next_id),
 
109
            'instance_id': 0,
 
110
            'allocated': False,
 
111
            # and since network_id and vif_id happen to be equivalent
 
112
            'virtual_interface_id': network_id,
 
113
            'floating_ips': f_ips}
 
114
 
 
115
 
 
116
def next_floating_ip(fixed_ip_id):
 
117
    next_id = floating_ip_id.next()
 
118
    return {'id': next_id,
 
119
            'address': '10.10.10.1%02d' % next_id,
 
120
            'fixed_ip_id': fixed_ip_id,
 
121
            'project_id': None,
 
122
            'auto_assigned': False}
 
123
 
 
124
 
 
125
def ipv4_like(ip, match_string):
 
126
    ip = ip.split('.')
 
127
    match_octets = match_string.split('.')
 
128
 
 
129
    for i, octet in enumerate(match_octets):
 
130
        if octet == '*':
 
131
            continue
 
132
        if octet != ip[i]:
 
133
            return False
 
134
    return True
 
135
 
 
136
 
 
137
def fake_get_instance_nw_info(stubs, num_networks=1, ips_per_vif=2,
 
138
                              floating_ips_per_fixed_ip=0):
 
139
    # stubs is the self.stubs from the test
 
140
    # ips_per_vif is the number of ips each vif will have
 
141
    # num_floating_ips is number of float ips for each fixed ip
 
142
    network = network_manager.FlatManager(host=HOST)
 
143
    network.db = db
 
144
 
 
145
    # reset the fixed and floating ip generators
 
146
    global floating_ip_id, fixed_ip_id
 
147
    floating_ip_id = floating_ip_ids()
 
148
    fixed_ip_id = fixed_ip_ids()
 
149
 
 
150
    def fixed_ips_fake(*args, **kwargs):
 
151
        return [next_fixed_ip(i, floating_ips_per_fixed_ip)
 
152
                for i in xrange(num_networks) for j in xrange(ips_per_vif)]
 
153
 
 
154
    def virtual_interfaces_fake(*args, **kwargs):
 
155
        return [vif for vif in vifs(num_networks)]
 
156
 
 
157
    def instance_type_fake(*args, **kwargs):
 
158
        return flavor
 
159
 
 
160
    stubs.Set(db, 'fixed_ip_get_by_instance', fixed_ips_fake)
 
161
    stubs.Set(db, 'virtual_interface_get_by_instance', virtual_interfaces_fake)
 
162
    stubs.Set(db, 'instance_type_get', instance_type_fake)
 
163
 
 
164
    return network.get_instance_nw_info(None, 0, 0, None)