1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2011 Rackspace
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
10
# http://www.apache.org/licenses/LICENSE-2.0
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
19
from nova import flags
21
from nova.network import manager as network_manager
28
class FakeModel(dict):
29
"""Represent a model from the db"""
30
def __init__(self, *args, **kwargs):
33
def __getattr__(self, name):
38
'name': 'fake_flavor',
48
def fake_network(network_id, ipv6=None):
51
fake_network = {'id': network_id,
52
'label': 'test%d' % network_id,
55
'cidr': '192.168.%d.0/24' % network_id,
57
'netmask': '255.255.255.0',
59
'bridge': 'fake_br%d' % network_id,
60
'bridge_interface': 'fake_eth%d' % network_id,
61
'gateway': '192.168.%d.1' % network_id,
63
'broadcast': '192.168.%d.255' % network_id,
64
'dns1': '192.168.%d.3' % network_id,
65
'dns2': '192.168.%d.4' % network_id,
68
'project_id': 'fake_project',
69
'vpn_public_address': '192.168.%d.2' % network_id}
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'
81
'address': 'DE:AD:BE:EF:00:%02x' % x,
82
'uuid': '00000000-0000-0000-0000-00000000000000%02d' % x,
84
'network': FakeModel(**fake_network(x)),
88
def floating_ip_ids():
98
floating_ip_id = floating_ip_ids()
99
fixed_ip_id = fixed_ip_ids()
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),
111
# and since network_id and vif_id happen to be equivalent
112
'virtual_interface_id': network_id,
113
'floating_ips': f_ips}
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,
122
'auto_assigned': False}
125
def ipv4_like(ip, match_string):
127
match_octets = match_string.split('.')
129
for i, octet in enumerate(match_octets):
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)
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()
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)]
154
def virtual_interfaces_fake(*args, **kwargs):
155
return [vif for vif in vifs(num_networks)]
157
def instance_type_fake(*args, **kwargs):
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)
164
return network.get_instance_nw_info(None, 0, 0, None)