/usr/share/pyshared/melange/tests/unit/test_ipam_views.py is in python-melange 1:2012.1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | # vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2011 OpenStack LLC.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from melange import tests
from melange.ipam import models
from melange.ipam import views
from melange.tests.factories import models as factory_models
class TestIpConfigurationView(tests.BaseTest):
def test_data_returns_block_ip_info(self):
block1 = factory_models.IpBlockFactory()
block2 = factory_models.IpBlockFactory()
interface = factory_models.InterfaceFactory(vif_id_on_device="123")
ip1 = factory_models.IpAddressFactory(ip_block_id=block1.id,
interface_id=interface.id)
ip2 = factory_models.IpAddressFactory(ip_block_id=block2.id,
interface_id=interface.id)
expected_ip1_config = _ip_data(ip1, block1)
expected_ip2_config = _ip_data(ip2, block2)
ip_configuration_view = views.IpConfigurationView(ip1, ip2)
self.assertEqual(expected_ip1_config, ip_configuration_view.data()[0])
self.assertEqual(expected_ip2_config, ip_configuration_view.data()[1])
def test_data_returns_deallocated_ip_info(self):
block = factory_models.IpBlockFactory()
interface = factory_models.InterfaceFactory(vif_id_on_device="123")
ip = factory_models.IpAddressFactory(ip_block_id=block.id,
interface_id=interface.id)
ip.deallocate()
deallocated_ip = models.IpAddress.find(ip.id)
expected_ip_config = _ip_data(deallocated_ip, block)
ip_configuration_view = views.IpConfigurationView(deallocated_ip)
self.assertEqual(expected_ip_config, ip_configuration_view.data()[0])
def test_data_returns_route_info(self):
block = factory_models.IpBlockFactory()
interface = factory_models.InterfaceFactory(vif_id_on_device="123")
route1 = factory_models.IpRouteFactory(source_block_id=block.id)
route2 = factory_models.IpRouteFactory(source_block_id=block.id)
ip = factory_models.IpAddressFactory(ip_block_id=block.id,
interface_id=interface.id)
expected_ip_config_routes = [_route_data(route1), _route_data(route2)]
ip_configuration_view = views.IpConfigurationView(ip).data()[0]
ip1_config_routes = ip_configuration_view['ip_block'].pop('ip_routes')
self.assertItemsEqual(expected_ip_config_routes, ip1_config_routes)
def _ip_data(ip, block):
return {
'id': ip.id,
'interface_id': ip.virtual_interface_id,
'address': ip.address,
'version': ip.version,
'ip_block': {
'id': block.id,
'cidr': block.cidr,
'network_id': block.network_id,
'broadcast': block.broadcast,
'gateway': block.gateway,
'netmask': block.netmask,
'dns1': block.dns1,
'dns2': block.dns2,
'ip_routes': [],
'tenant_id': block.tenant_id,
},
}
def _route_data(route):
return {
'id': route.id,
'destination': route.destination,
'gateway': route.gateway,
'netmask': route.netmask,
}
class TestInterfaceConfigurationView(tests.BaseTest):
def test_data_returns_mac_address(self):
interface = factory_models.InterfaceFactory()
mac = models.MacAddress.create(interface_id=interface.id,
address="ab-bc-cd-12-23-34")
data = views.InterfaceConfigurationView(interface).data()
self.assertEqual(data['mac_address'], mac.unix_format)
self.assertEqual(data['id'], interface.virtual_interface_id)
def test_data_returns_ip_address_configuration_information(self):
interface = factory_models.InterfaceFactory()
ip1 = factory_models.IpAddressFactory(interface_id=interface.id)
ip2 = factory_models.IpAddressFactory(interface_id=interface.id)
data = views.InterfaceConfigurationView(interface).data()
self.assertEqual(len(data['ip_addresses']), 2)
self.assertItemsEqual(data['ip_addresses'],
views.IpConfigurationView(ip1, ip2).data())
|