summaryrefslogtreecommitdiff
path: root/nova/tests/db/fakes.py
diff options
context:
space:
mode:
authorTrey Morris <trey.morris@rackspace.com>2011-06-09 11:39:03 -0500
committerTrey Morris <trey.morris@rackspace.com>2011-06-09 11:39:03 -0500
commite95703ee9c358f3e0ef35b79735a4848f43fd888 (patch)
tree0126926bd0317fed1ec6545d426e43c8834495d1 /nova/tests/db/fakes.py
parent71ad924b1e8bfdefaaac3dd533dc14d83b2ab7bd (diff)
parent02f9b1c0f9265a644fabcd5d0c5c6071fc65390f (diff)
downloadnova-e95703ee9c358f3e0ef35b79735a4848f43fd888.tar.gz
merged koelkers tests branch
Diffstat (limited to 'nova/tests/db/fakes.py')
-rw-r--r--nova/tests/db/fakes.py359
1 files changed, 327 insertions, 32 deletions
diff --git a/nova/tests/db/fakes.py b/nova/tests/db/fakes.py
index ecb1a27f85..2d949a26d8 100644
--- a/nova/tests/db/fakes.py
+++ b/nova/tests/db/fakes.py
@@ -20,10 +20,323 @@
import time
from nova import db
+from nova import exception
from nova import test
from nova import utils
+class FakeModel(object):
+ """Stubs out for model."""
+ def __init__(self, values):
+ self.values = values
+
+ def __getattr__(self, name):
+ return self.values[name]
+
+ def __getitem__(self, key):
+ if key in self.values:
+ return self.values[key]
+ else:
+ raise NotImplementedError()
+
+ def __repr__(self):
+ return '<FakeModel: %s>' % self.values
+
+
+def stub_out(stubs, funcs):
+ """
+ Set the stubs in mapping in the db api
+ """
+ for func in funcs:
+ func_name = '_'.join(func.__name__.split('_')[1:])
+ stubs.Set(db, func_name, func)
+
+
+def stub_out_db_network_api(stubs):
+ network_fields = {'id': 0,
+ 'cidr': '192.168.0.0/24',
+ 'netmask': '255.255.255.0',
+ 'cidr_v6': 'dead:beef::/64',
+ 'netmask_v6': '64',
+ 'project_id': 'fake',
+ 'label': 'fake',
+ 'gateway': '192.168.0.1',
+ 'bridge': 'fa0',
+ 'bridge_interface': 'fake_fa0',
+ 'broadcast': '192.168.0.255',
+ 'gateway_v6': 'dead:beef::1',
+ 'dns': '192.168.0.1',
+ 'vlan': None,
+ 'host': None,
+ 'vpn_public_address': '192.168.0.2'}
+
+ fixed_ip_fields = {'id': 0,
+ 'network_id': 0,
+ 'address': '192.168.0.100',
+ 'instance': False,
+ 'instance_id': 0,
+ 'allocated': False,
+ 'mac_address_id': 0,
+ 'mac_address': None,
+ 'floating_ips': []}
+
+ flavor_fields = {'id': 0,
+ 'rxtx_cap': 3}
+
+ floating_ip_fields = {'id': 0,
+ 'address': '192.168.1.100',
+ 'fixed_ip_id': 0,
+ 'fixed_ip': None,
+ 'project_id': 'fake',
+ 'auto_assigned': False}
+
+ mac_address_fields = {'id': 0,
+ 'address': 'DE:AD:BE:EF:00:00',
+ 'network_id': 0,
+ 'instance_id': 0,
+ 'network': FakeModel(network_fields)}
+
+ fixed_ips = [fixed_ip_fields]
+ floating_ips = [floating_ip_fields]
+ mac_addresses = [mac_address_fields]
+ networks = [network_fields]
+
+ def fake_floating_ip_allocate_address(context, project_id):
+ ips = filter(lambda i: i['fixed_ip_id'] == None \
+ and i['project_id'] == None,
+ floating_ips)
+ if not ips:
+ raise db.NoMoreAddresses()
+ ips[0]['project_id'] = project_id
+ return FakeModel(ips[0]['address'])
+
+ def fake_floating_ip_deallocate(context, address):
+ ips = filter(lambda i: i['address'] == address,
+ floating_ips)
+ if ips:
+ ips[0]['project_id'] = None
+ ips[0]['auto_assigned'] = False
+
+ def fake_floating_ip_disassociate(context, address):
+ ips = filter(lambda i: i['address'] == address,
+ floating_ips)
+ if ips:
+ fixed_ip_address = None
+ if ips[0]['fixed_ip']:
+ fixed_ip_address = ips[0]['fixed_ip']['address']
+ ips[0]['fixed_ip'] = None
+ return fixed_ip_address
+
+ def fake_floating_ip_fixed_ip_associate(context, floating_address,
+ fixed_address):
+ float = filter(lambda i: i['address'] == floating_address,
+ floating_ips)
+ fixed = filter(lambda i: i['address'] == fixed_address,
+ fixed_ips)
+ if float and fixed:
+ float[0]['fixed_ip'] = fixed[0]
+ float[0]['fixed_ip_id'] = fixed[0]['id']
+
+ def fake_floating_ip_get_all_by_host(context, host):
+ # TODO(jkoelker): Once we get the patches that remove host from
+ # the floating_ip table, we'll need to stub
+ # this out
+ pass
+
+ def fake_floating_ip_get_by_address(context, address):
+ ips = filter(lambda i: i['address'] == address,
+ floating_ips)
+ if not ips:
+ raise exception.FloatingIpNotFound(address=address)
+ return FakeModel(ips[0])
+
+ def fake_floating_ip_set_auto_assigned(contex, address):
+ ips = filter(lambda i: i['address'] == address,
+ floating_ips)
+ if ips:
+ ips[0]['auto_assigned'] = True
+
+ def fake_fixed_ip_associate(context, address, instance_id):
+ ips = filter(lambda i: i['address'] == address,
+ fixed_ips)
+ if not ips:
+ raise db.NoMoreAddresses()
+ ips[0]['instance'] = True
+ ips[0]['instance_id'] = instance_id
+
+ def fake_fixed_ip_associate_pool(context, network_id, instance_id):
+ ips = filter(lambda i: (i['network_id'] == network_id \
+ or i['network_id'] is None) \
+ and not i['instance'],
+ fixed_ips)
+ if not ips:
+ raise db.NoMoreAddresses()
+ ips[0]['instance'] = True
+ ips[0]['instance_id'] = instance_id
+ return ips[0]['address']
+
+ def fake_fixed_ip_create(context, values):
+ ip = dict(fixed_ip_fields)
+ ip['id'] = max([i['id'] for i in fixed_ips] or [-1]) + 1
+ for key in values:
+ ip[key] = values[key]
+ return ip['address']
+
+ def fake_fixed_ip_disassociate(context, address):
+ ips = filter(lambda i: i['address'] == address,
+ fixed_ips)
+ if ips:
+ ips[0]['instance_id'] = None
+ ips[0]['instance'] = None
+ ips[0]['mac_address'] = None
+ ips[0]['mac_address_id'] = None
+
+ def fake_fixed_ip_disassociate_all_by_timeout(context, host, time):
+ return 0
+
+ def fake_fixed_ip_get_all_by_instance(context, instance_id):
+ ips = filter(lambda i: i['instance_id'] == instance_id,
+ fixed_ips)
+ return [FakeModel(i) for i in ips]
+
+ def fake_fixed_ip_get_by_address(context, address):
+ ips = filter(lambda i: i['address'] == address,
+ fixed_ips)
+ if ips:
+ return FakeModel(ips[0])
+
+ def fake_fixed_ip_get_network(context, address):
+ ips = filter(lambda i: i['address'] == address,
+ fixed_ips)
+ if ips:
+ nets = filter(lambda n: n['id'] == ips[0]['network_id'],
+ networks)
+ if nets:
+ return FakeModel(nets[0])
+
+ def fake_fixed_ip_update(context, address, values):
+ ips = filter(lambda i: i['address'] == address,
+ fixed_ips)
+ if ips:
+ for key in values:
+ ips[0][key] = values[key]
+ if key == 'mac_address_id':
+ mac = filter(lambda x: x['id'] == values[key],
+ mac_addresses)
+ if not mac:
+ continue
+ fixed_ip_fields['mac_address'] = FakeModel(mac[0])
+
+ def fake_instance_type_get_by_id(context, id):
+ if flavor_fields['id'] == id:
+ return FakeModel(flavor_fields)
+
+ def fake_mac_address_create(context, values):
+ mac = dict(mac_address_fields)
+ mac['id'] = max([m['id'] for m in mac_addresses] or [-1]) + 1
+ for key in values:
+ mac[key] = values[key]
+ return FakeModel(mac)
+
+ def fake_mac_address_delete_by_instance(context, instance_id):
+ addresses = [m for m in mac_addresses \
+ if m['instance_id'] == instance_id]
+ try:
+ for address in addresses:
+ mac_addresses.remove(address)
+ except ValueError:
+ pass
+
+ def fake_mac_address_get_all_by_instance(context, instance_id):
+ return [FakeModel(m) for m in mac_addresses \
+ if m['instance_id'] == instance_id]
+
+ def fake_mac_address_get_by_instance_and_network(context, instance_id,
+ network_id):
+ mac = filter(lambda m: m['instance_id'] == instance_id \
+ and m['network_id'] == network_id,
+ mac_addresses)
+ if not mac:
+ return None
+ return FakeModel(mac[0])
+
+ def fake_network_create_safe(context, values):
+ net = dict(network_fields)
+ net['id'] = max([n['id'] for n in networks] or [-1]) + 1
+ for key in values:
+ net[key] = values[key]
+ return FakeModel(net)
+
+ def fake_network_get(context, network_id):
+ net = filter(lambda n: n['id'] == network_id, networks)
+ if not net:
+ return None
+ return FakeModel(net[0])
+
+ def fake_network_get_all(context):
+ return [FakeModel(n) for n in networks]
+
+ def fake_network_get_all_by_host(context, host):
+ nets = filter(lambda n: n['host'] == host, networks)
+ return [FakeModel(n) for n in nets]
+
+ def fake_network_get_all_by_instance(context, instance_id):
+ nets = filter(lambda n: n['instance_id'] == instance_id, networks)
+ return [FakeModel(n) for n in nets]
+
+ def fake_network_set_host(context, network_id, host_id):
+ nets = filter(lambda n: n['id'] == network_id, networks)
+ for net in nets:
+ net['host'] = host_id
+ return host_id
+
+ def fake_network_update(context, network_id, values):
+ nets = filter(lambda n: n['id'] == network_id, networks)
+ for net in nets:
+ for key in values:
+ net[key] = values[key]
+
+ def fake_project_get_networks(context, project_id):
+ return [FakeModel(n) for n in networks \
+ if n['project_id'] == project_id]
+
+ def fake_queue_get_for(context, topic, node):
+ return "%s.%s" % (topic, node)
+
+ funcs = [fake_floating_ip_allocate_address,
+ fake_floating_ip_deallocate,
+ fake_floating_ip_disassociate,
+ fake_floating_ip_fixed_ip_associate,
+ fake_floating_ip_get_all_by_host,
+ fake_floating_ip_get_by_address,
+ fake_floating_ip_set_auto_assigned,
+ fake_fixed_ip_associate,
+ fake_fixed_ip_associate_pool,
+ fake_fixed_ip_create,
+ fake_fixed_ip_disassociate,
+ fake_fixed_ip_disassociate_all_by_timeout,
+ fake_fixed_ip_get_all_by_instance,
+ fake_fixed_ip_get_by_address,
+ fake_fixed_ip_get_network,
+ fake_fixed_ip_update,
+ fake_instance_type_get_by_id,
+ fake_mac_address_create,
+ fake_mac_address_delete_by_instance,
+ fake_mac_address_get_all_by_instance,
+ fake_mac_address_get_by_instance_and_network,
+ fake_network_create_safe,
+ fake_network_get,
+ fake_network_get_all,
+ fake_network_get_all_by_host,
+ fake_network_get_all_by_instance,
+ fake_network_set_host,
+ fake_network_update,
+ fake_project_get_networks,
+ fake_queue_get_for]
+
+ stub_out(stubs, funcs)
+
+
def stub_out_db_instance_api(stubs, injected=True):
"""Stubs out the db API for creating Instances."""
@@ -92,20 +405,6 @@ def stub_out_db_instance_api(stubs, injected=True):
'address_v6': 'fe80::a00:3',
'network_id': 'fake_flat'}
- class FakeModel(object):
- """Stubs out for model."""
- def __init__(self, values):
- self.values = values
-
- def __getattr__(self, name):
- return self.values[name]
-
- def __getitem__(self, key):
- if key in self.values:
- return self.values[key]
- else:
- raise NotImplementedError()
-
def fake_instance_type_get_all(context, inactive=0):
return INSTANCE_TYPES
@@ -132,26 +431,22 @@ def stub_out_db_instance_api(stubs, injected=True):
else:
return [FakeModel(flat_network_fields)]
- def fake_instance_get_fixed_address(context, instance_id):
- return FakeModel(fixed_ip_fields).address
+ def fake_instance_get_fixed_addresses(context, instance_id):
+ return [FakeModel(fixed_ip_fields).address]
- def fake_instance_get_fixed_address_v6(context, instance_id):
- return FakeModel(fixed_ip_fields).address
+ def fake_instance_get_fixed_addresses_v6(context, instance_id):
+ return [FakeModel(fixed_ip_fields).address]
def fake_fixed_ip_get_by_instance(context, instance_id):
return [FakeModel(fixed_ip_fields)]
- stubs.Set(db, 'network_get_by_instance', fake_network_get_by_instance)
- stubs.Set(db, 'network_get_all_by_instance',
- fake_network_get_all_by_instance)
- stubs.Set(db, 'instance_type_get_all', fake_instance_type_get_all)
- stubs.Set(db, 'instance_type_get_by_name', fake_instance_type_get_by_name)
- stubs.Set(db, 'instance_type_get_by_id', fake_instance_type_get_by_id)
- stubs.Set(db, 'instance_get_fixed_address',
- fake_instance_get_fixed_address)
- stubs.Set(db, 'instance_get_fixed_address_v6',
- fake_instance_get_fixed_address_v6)
- stubs.Set(db, 'network_get_all_by_instance',
- fake_network_get_all_by_instance)
- stubs.Set(db, 'fixed_ip_get_by_instance',
- fake_fixed_ip_get_by_instance)
+ funcs = [fake_network_get_by_instance,
+ fake_network_get_all_by_instance,
+ fake_instance_type_get_all,
+ fake_instance_type_get_by_name,
+ fake_instance_type_get_by_id,
+ fake_instance_get_fixed_addresses,
+ fake_instance_get_fixed_addresses_v6,
+ fake_network_get_all_by_instance,
+ fake_fixed_ip_get_by_instance]
+ stub_out(stubs, funcs)