def test_get_subnets_attrs(self):
     fake_subnets = [
         stubs.FakeNeutronSubnet(
             name='subnet01',
             cidr='192.168.24.0/24',
             gateway_ip='192.168.24.1',
             host_routes=[{
                 'destination': '192.168.24.0/24',
                 'nexthop': '192.168.25.1'
             }],
             dns_nameservers=['192.168.24.254', '192.168.24.253'],
             ip_version=4,
             tags=['tripleo_vlan_id=24']),
         stubs.FakeNeutronSubnet(
             name='subnet02',
             cidr='192.168.25.0/24',
             gateway_ip='192.168.25.1',
             host_routes=[{
                 'destination': '192.168.24.0/24',
                 'nexthop': '192.168.25.1'
             }],
             dns_nameservers=['192.168.25.254', '192.168.25.253'],
             ip_version=4,
             tags=['tripleo_vlan_id=25'])
     ]
     expected = {
         'subnet01': {
             'name':
             'subnet01',
             'cidr':
             '192.168.24.0/24',
             'gateway_ip':
             '192.168.24.1',
             'host_routes': [{
                 'destination': '192.168.24.0/24',
                 'nexthop': '192.168.25.1'
             }],
             'dns_nameservers': ['192.168.24.254', '192.168.24.253'],
             'ip_version':
             4,
             'tags': ['tripleo_vlan_id=24']
         },
         'subnet02': {
             'name':
             'subnet02',
             'cidr':
             '192.168.25.0/24',
             'gateway_ip':
             '192.168.25.1',
             'host_routes': [{
                 'destination': '192.168.24.0/24',
                 'nexthop': '192.168.25.1'
             }],
             'dns_nameservers': ['192.168.25.254', '192.168.25.253'],
             'ip_version':
             4,
             'tags': ['tripleo_vlan_id=25']
         }
     }
     self.assertEqual(expected, plugin.get_subnets_attrs(fake_subnets))
 def test_get_net_cidrs(self):
     subnets = [
         stubs.FakeNeutronSubnet(cidr='192.168.24.0/24', ip_version=4),
         stubs.FakeNeutronSubnet(cidr='192.168.25.0/24', ip_version=4),
         stubs.FakeNeutronSubnet(cidr='2001:db8:a::/64', ip_version=6),
         stubs.FakeNeutronSubnet(cidr='2001:db8:b::/64', ip_version=6)
     ]
     self.assertEqual(['192.168.24.0/24', '192.168.25.0/24'],
                      plugin.get_net_cidrs(subnets, 4))
     self.assertEqual(['2001:db8:a::/64', '2001:db8:b::/64'],
                      plugin.get_net_cidrs(subnets, 6))
    def test_adopt_the_implicit_segment(self, mock_create_or_update_segment,
                                        mock_create_segment_spec):
        fake_network = stubs.FakeNeutronNetwork(id='net_id', name='net_name')
        fake_segments = [
            stubs.FakeNeutronSegment(id='segment_id',
                                     name=None,
                                     physical_network='physical_net')
        ]
        fake_subnets = [
            stubs.FakeNeutronSubnet(id='subnet_id',
                                    name='subnet_name',
                                    segment_id='segment_id')
        ]

        changed = plugin.adopt_the_implicit_segment(mock.ANY, mock.ANY,
                                                    fake_segments,
                                                    fake_subnets, fake_network)

        mock_create_segment_spec.assert_called_once_with(
            fake_network.id,
            fake_network.name,
            fake_subnets[0].name,
            physical_network=fake_segments[0].physical_network)
        mock_create_or_update_segment.assert_called_once_with(
            mock.ANY, mock.ANY, mock.ANY, segment_id=fake_segments[0].id)
        self.assertTrue(changed)
    def test_find_ctlplane_vip(self, mock_conn):
        fake_network = stubs.FakeNeutronNetwork(id='ctlplane_id',
                                                name='ctlplane')
        fake_subnet = stubs.FakeNeutronSubnet(id='ctlplane_subnet_id',
                                              name='ctlplane-subnet')
        fake_vip_port = stubs.FakeNeutronPort(id='ctlplane_vip_id',
                                              name='control_virtual_ip',
                                              fixed_ips=[{
                                                  'subnet_id':
                                                  'ctlplane_subnet_id',
                                                  'ip_address':
                                                  '4.3.2.1'
                                              }],
                                              dns_name='ctlplane.localdomain')
        mock_conn.network.find_network.return_value = fake_network
        mock_conn.network.get_subnet.return_value = fake_subnet
        mock_conn.network.ports.return_value = self.a2g([fake_vip_port])

        vip_data = list()
        plugin.find_ctlplane_vip(mock_conn, vip_data)
        self.assertEqual([{
            'name': 'control_virtual_ip',
            'network': 'ctlplane',
            'subnet': 'ctlplane-subnet',
            'ip_address': '4.3.2.1',
            'dns_name': 'ctlplane.localdomain'
        }], vip_data)
Exemplo n.º 5
0
 def test_update_port(self, mock_conn):
     fixed_ips = [{'subnet': 'test'}]
     fake_net = stubs.FakeNeutronNetwork(name='test', id='net_id')
     fake_subnet = stubs.FakeNeutronSubnet(name='test', id='subnet_id')
     fake_port = stubs.FakeNeutronPort(name='test_virtual_ip',
                                       id='port_id',
                                       fixed_ips=[{
                                           'ip_address': '10.0.0.10',
                                           'subnet_id': 'subnet_id'
                                       }],
                                       tags=[])
     mock_conn.network.find_subnet.return_value = fake_subnet
     mock_conn.network.ports.return_value = self.a2g([fake_port])
     mock_conn.network.update_port.return_value = fake_port
     plugin.create_or_update_port(mock_conn,
                                  fake_net,
                                  stack='test',
                                  service='test',
                                  fixed_ips=fixed_ips)
     mock_conn.network.create_port.assert_not_called()
     mock_conn.network.update_port.assert_called_once_with(
         fake_port,
         name='test_virtual_ip',
         network_id='net_id',
         fixed_ips=[{
             'subnet_id': 'subnet_id'
         }])
     mock_conn.network.set_tags.assert_called_once_with(
         fake_port, [mock.ANY, mock.ANY])
 def test_get_subnet_info_ipv6(self, conn_mock):
     fake_subnet = stubs.FakeNeutronSubnet(
         name='public_subnet',
         is_dhcp_enabled=False,
         tags=['tripleo_vlan_id=200'],
         ip_version=6,
         cidr='2001:db8:a::/64',
         allocation_pools=[{
             'start': '2001:db8:a::0010',
             'end': '2001:db8:a::fff9'
         }],
         gateway_ip='2001:db8:a::1',
         host_routes=[{
             'destination': '2001:db8:b::/64',
             'nexthop': '2001:db8:a::1'
         }],
         ipv6_address_mode=None,
         ipv6_ra_mode=None,
     )
     fake_segment = stubs.FakeNeutronSegment(
         name='public_subnet',
         network_type='flat',
         physical_network='public_subnet')
     conn_mock.network.get_subnet.return_value = fake_subnet
     conn_mock.network.get_segment.return_value = fake_segment
     expected = {
         'vlan':
         200,
         'ipv6_subnet':
         '2001:db8:a::/64',
         'ipv6_allocation_pools': [{
             'start': '2001:db8:a::0010',
             'end': '2001:db8:a::fff9'
         }],
         'gateway_ipv6':
         '2001:db8:a::1',
         'routes_ipv6': [{
             'destination': '2001:db8:b::/64',
             'nexthop': '2001:db8:a::1'
         }],
         'physical_network':
         'public_subnet',
     }
     name, subnet = plugin.get_subnet_info(conn_mock, mock.Mock())
     self.assertEqual(name, 'public_subnet')
     self.assertEqual(expected, subnet)
    def test_find_net_vips(self, mock_conn):
        fake_net_resources = {
            'StorageNetwork': {
                'InternalApiNetwork': {
                    'physical_resource_id': 'fake-id',
                    'resource_type': n_utils.TYPE_NET
                },
                'StorageSubnet': {
                    'physical_resource_id': 'fake-id',
                    'resource_type': n_utils.TYPE_SUBNET
                },
                'StorageSubnet_leaf1': {
                    'physical_resource_id': 'fake-id',
                    'resource_type': n_utils.TYPE_SUBNET
                }
            }
        }
        fake_network = stubs.FakeNeutronNetwork(id='internal_api_id',
                                                name='internal_api')
        fake_subnet = stubs.FakeNeutronSubnet(id='internal_api_subnet_id',
                                              name='internal_api_subnet')
        fake_vip_port = stubs.FakeNeutronPort(
            id='internal_api_vip_id',
            name='internal_api_virtual_ip',
            fixed_ips=[{
                'subnet_id': 'internal_api_subnet_id',
                'ip_address': '1.2.3.4'
            }],
            dns_name='internalapi.localdomain')
        mock_conn.network.get_network.return_value = fake_network
        mock_conn.network.get_subnet.return_value = fake_subnet
        mock_conn.network.ports.return_value = self.a2g([fake_vip_port])

        vip_data = list()
        plugin.find_net_vips(mock_conn, fake_net_resources, vip_data)
        self.assertEqual([{
            'name': 'internal_api_virtual_ip',
            'network': 'internal_api',
            'subnet': 'internal_api_subnet',
            'ip_address': '1.2.3.4',
            'dns_name': 'internalapi.localdomain'
        }], vip_data)
 def test_get_subnet_attrs(self):
     fake_subnet = stubs.FakeNeutronSubnet(
         name='subnet_name',
         cidr='192.168.24.0/24',
         gateway_ip='192.168.24.1',
         host_routes=[],
         dns_nameservers=['192.168.24.254', '192.168.24.253'],
         ip_version=4,
         tags=['tripleo_vlan_id=1'])
     expected = {
         'name': 'subnet_name',
         'cidr': '192.168.24.0/24',
         'gateway_ip': '192.168.24.1',
         'host_routes': [],
         'dns_nameservers': ['192.168.24.254', '192.168.24.253'],
         'ip_version': 4,
         'tags': ['tripleo_vlan_id=1']
     }
     name, attrs = plugin.get_subnet_attrs(fake_subnet)
     self.assertEqual('subnet_name', name)
     self.assertEqual(expected, attrs)
 def test_get_subnet_info_ipv4(self, conn_mock):
     fake_subnet = stubs.FakeNeutronSubnet(
         name='public_subnet',
         is_dhcp_enabled=False,
         tags=['tripleo_vlan_id=100'],
         ip_version=4,
         cidr='10.0.0.0/24',
         allocation_pools=[{
             'start': '10.0.0.10',
             'end': '10.0.0.150'
         }],
         gateway_ip='10.0.0.1',
         host_routes=[{
             'destination': '172.17.1.0/24',
             'nexthop': '10.0.0.1'
         }],
     )
     fake_segment = stubs.FakeNeutronSegment(
         name='public_subnet',
         network_type='flat',
         physical_network='public_subnet')
     conn_mock.network.get_subnet.return_value = fake_subnet
     conn_mock.network.get_segment.return_value = fake_segment
     expected = {
         'vlan': 100,
         'ip_subnet': '10.0.0.0/24',
         'allocation_pools': [{
             'start': '10.0.0.10',
             'end': '10.0.0.150'
         }],
         'gateway_ip': '10.0.0.1',
         'routes': [{
             'destination': '172.17.1.0/24',
             'nexthop': '10.0.0.1'
         }],
         'physical_network': 'public_subnet',
     }
     name, subnet = plugin.get_subnet_info(conn_mock, mock.Mock())
     self.assertEqual(name, 'public_subnet')
     self.assertEqual(expected, subnet)
 def test_get_net_ip_version(self):
     net_data = {}
     subnets = [
         stubs.FakeNeutronSubnet(ip_version=4),
         stubs.FakeNeutronSubnet(ip_version=4)
     ]
     self.assertEqual(4, plugin.get_net_ip_version(subnets, net_data))
     subnets = [
         stubs.FakeNeutronSubnet(ip_version=6),
         stubs.FakeNeutronSubnet(ip_version=6)
     ]
     self.assertEqual(6, plugin.get_net_ip_version(subnets, net_data))
     subnets = [
         stubs.FakeNeutronSubnet(ip_version=4),
         stubs.FakeNeutronSubnet(ip_version=6)
     ]
     self.assertEqual(4, plugin.get_net_ip_version(subnets, net_data))
     net_data = {'ipv6': True}
     self.assertEqual(6, plugin.get_net_ip_version(subnets, net_data))
 def test_update_subnet(self, mock_conn):
     mock_module = mock.Mock()
     subnet_spec = {
         'ip_version':
         4,
         'name':
         'subnet_name',
         'network_id':
         'net_id',
         'enable_dhcp':
         False,
         'gateway_ip':
         '192.168.24.1',
         'cidr':
         '192.168.24.0/24',
         'allocation_pools': [{
             'start': '192.168.24.100',
             'end': '192.168.24.200'
         }],
         'host_routes': [{
             'destination': '192.168.25.0/24',
             'nexthop': '192.168.24.1'
         }],
         'tags': ['tripleo_vlan_id=100'],
         'segment_id':
         'segment_id',
     }
     fake_subnet = stubs.FakeNeutronSubnet(id='foo',
                                           name='subnet_name',
                                           network_id='net_id',
                                           is_dhcp_enabled=False,
                                           gateway_ip='192.168.24.254',
                                           cidr='192.168.24.0/24',
                                           allocation_pools=[{
                                               'start':
                                               '192.168.24.100',
                                               'end':
                                               '192.168.24.200'
                                           }],
                                           host_routes=[{
                                               'destination':
                                               '192.168.25.0/24',
                                               'nexthop':
                                               '192.168.24.254'
                                           }],
                                           tags=['tripleo_vlan_id=100'],
                                           segment_id='segment_id')
     mock_conn.network.find_subnet.return_value = fake_subnet
     changed = plugin.create_or_update_subnet(mock_conn, mock_module,
                                              subnet_spec)
     mock_conn.network.find_subnet.ssert_called_once_with(
         'subnet_name', network_id='net_id')
     mock_conn.network.create_subnet.assert_not_called()
     mock_conn.network.update_subnet.assert_called_once_with(
         'foo', **{
             'gateway_ip':
             '192.168.24.1',
             'host_routes': [{
                 'destination': '192.168.25.0/24',
                 'nexthop': '192.168.24.1'
             }]
         })
     self.assertTrue(changed)
 def test_validate_subnet_update(self):
     module = mock.Mock()
     module.fail = mock.Mock()
     subnet_spec = {
         'ip_version':
         6,
         'network_id':
         'new_net_id',
         'cidr':
         '192.168.24.0/25',
         'segment_id':
         'new_segment_id',
         'name':
         'new_name',
         'enable_dhcp':
         True,
         'ipv6_address_mode':
         'slaac',
         'ipv6_ra_mode':
         'slaac',
         'gateway_ip':
         '192.168.24.254',
         'allocation_pools': [{
             'start': '192.168.24.100',
             'end': '200'
         }],
         'host_routes': [{
             'destination': '192.168.25.0/24',
             'nexthop': '192.168.24.254'
         }],
     }
     fake_subnet = stubs.FakeNeutronSubnet(
         id='foo',
         name='subnet01',
         cidr='192.168.24.0/24',
         gateway_ip='192.168.24.1',
         allocation_pools=[{
             'start': '192.168.24.50',
             'end': '99'
         }],
         host_routes={
             'destination': '192.168.25.0/24',
             'nexthop': '192.168.24.1'
         },
         ip_version=4,
         network_id='net_id',
         segment_id='segment_id',
         is_dhcp_enabled=False,
     )
     result = plugin.validate_subnet_update(module, fake_subnet,
                                            subnet_spec)
     module.fail_json.assert_has_calls([
         mock.call(msg='Cannot update ip_version in existing subnet'),
         mock.call(msg='Cannot update network_id in existing subnet'),
         mock.call(msg='Cannot update cidr in existing subnet'),
         mock.call(
             msg='Cannot update segment_id in existing subnet, Current '
             'segment_id: {} Update segment_id: {}'.format(
                 'segment_id', 'new_segment_id')),
     ])
     expected_spec = {
         'name':
         'new_name',
         'enable_dhcp':
         True,
         'ipv6_address_mode':
         'slaac',
         'ipv6_ra_mode':
         'slaac',
         'gateway_ip':
         '192.168.24.254',
         'allocation_pools': [{
             'start': '192.168.24.100',
             'end': '200'
         }],
         'host_routes': [{
             'destination': '192.168.25.0/24',
             'nexthop': '192.168.24.254'
         }],
     }
     self.assertEqual(expected_spec, result)
fake_storage_mgmt = stubs.FakeNeutronNetwork(
    id='storage_mgmt_id',
    name='storage_mgmt',
    dns_domain='storagemgmt.localdomain.',
    tags=['tripleo_network_name=StorageMgmt', 'tripleo_stack_name=stack'])
fake_external = stubs.FakeNeutronNetwork(
    id='external_id',
    name='external',
    dns_domain='external.localdomain.',
    tags=['tripleo_network_name=External', 'tripleo_stack_name=stack'])
fake_ctlplane = stubs.FakeNeutronNetwork(id='ctlplane_id',
                                         name='ctlplane',
                                         dns_domain='ctlplane.localdomain.',
                                         tags=['foo', 'bar'])
fake_ctlplane_subnet = stubs.FakeNeutronSubnet(id='ctlplane_subnet_id',
                                               name='ctlplane-subnet',
                                               cidr='192.168.24.0/24',
                                               ip_version=4)
fake_internal_api_subnet = stubs.FakeNeutronSubnet(id='internal_api_subnet_id',
                                                   name='internal_api_subnet',
                                                   cidr='10.0.1.0/24')
fake_storage_mgmt_subnet = stubs.FakeNeutronSubnet(id='storage_mgmt_subnet_id',
                                                   name='storage_mgmt_subnet',
                                                   cidr='10.0.3.0/24')
fake_external_subnet = stubs.FakeNeutronSubnet(id='external_subnet_id',
                                               name='external_subnet',
                                               cidr='10.0.5.0/24')

fake_ctlplane_port = stubs.FakeNeutronPort(
    name='control_virtual_ip',
    id='ctlplane_port_id',
    dns_name='overcloud',
 def test_set_composable_network_attrs(self, mock_conn):
     module = mock.Mock()
     net_data = {'name': 'NetName'}
     fake_network = stubs.FakeNeutronNetwork(
         id='net_id',
         name='netname',
         mtu=1500,
         dns_domain='netname.localdomain.',
         tags=['tripleo_vlan_id=100'],
         subnet_ids=['subnet01_id', 'subnet02_id'])
     fake_subnets = [
         stubs.FakeNeutronSubnet(
             name='subnet01',
             cidr='192.168.24.0/24',
             gateway_ip='192.168.24.1',
             host_routes=[{
                 'destination': '192.168.24.0/24',
                 'nexthop': '192.168.25.1'
             }],
             dns_nameservers=['192.168.24.254', '192.168.24.253'],
             ip_version=4,
             tags=['tripleo_vlan_id=24']),
         stubs.FakeNeutronSubnet(
             name='subnet02',
             cidr='192.168.25.0/24',
             gateway_ip='192.168.25.1',
             host_routes=[{
                 'destination': '192.168.24.0/24',
                 'nexthop': '192.168.25.1'
             }],
             dns_nameservers=['192.168.25.254', '192.168.25.253'],
             ip_version=4,
             tags=['tripleo_vlan_id=25'])
     ]
     mock_conn.network.find_network.return_value = fake_network
     mock_conn.network.get_subnet.side_effect = fake_subnets
     attrs = dict()
     cidr_map = dict()
     ip_version_map = dict()
     plugin.set_composable_network_attrs(module,
                                         mock_conn,
                                         net_data['name'].lower(),
                                         net_data,
                                         attrs=attrs,
                                         cidr_map=cidr_map,
                                         ip_version_map=ip_version_map)
     mock_conn.network.find_network.assert_called_with(
         net_data['name'].lower())
     mock_conn.network.get_subnet.assert_has_calls(
         [mock.call('subnet01_id'),
          mock.call('subnet02_id')])
     self.assertEqual(
         {
             'network': {
                 'dns_domain': 'netname.localdomain.',
                 'mtu': 1500,
                 'name': 'netname',
                 'tags': ['tripleo_vlan_id=100']
             },
             'subnets': {
                 'subnet01': {
                     'name':
                     'subnet01',
                     'cidr':
                     '192.168.24.0/24',
                     'gateway_ip':
                     '192.168.24.1',
                     'host_routes': [{
                         'destination': '192.168.24.0/24',
                         'nexthop': '192.168.25.1'
                     }],
                     'dns_nameservers':
                     ['192.168.24.254', '192.168.24.253'],
                     'ip_version':
                     4,
                     'tags': ['tripleo_vlan_id=24']
                 },
                 'subnet02': {
                     'name':
                     'subnet02',
                     'cidr':
                     '192.168.25.0/24',
                     'gateway_ip':
                     '192.168.25.1',
                     'host_routes': [{
                         'destination': '192.168.24.0/24',
                         'nexthop': '192.168.25.1'
                     }],
                     'dns_nameservers':
                     ['192.168.25.254', '192.168.25.253'],
                     'ip_version':
                     4,
                     'tags': ['tripleo_vlan_id=25']
                 }
             }
         }, attrs)
     self.assertEqual({'netname': 4}, ip_version_map)
     self.assertEqual({'netname': ['192.168.24.0/24', '192.168.25.0/24']},
                      cidr_map)
 def test_create_name_id_maps(self, conn_mock):
     subnet1 = stubs.FakeNeutronSubnet(id='subnet1_id',
                                       name='subnet1',
                                       cidr='192.168.24.0/24')
     subnet2 = stubs.FakeNeutronSubnet(id='subnet2_id',
                                       name='subnet2',
                                       cidr='192.168.25.0/25')
     subnet3 = stubs.FakeNeutronSubnet(id='subnet3_id',
                                       name='subnet3',
                                       cidr='192.168.26.0/26')
     subnet4 = stubs.FakeNeutronSubnet(id='subnet4_id',
                                       name='subnet4',
                                       cidr='192.168.27.0/27')
     network1 = stubs.FakeNeutronNetwork(
         id='network1_id',
         name='network1',
         subnet_ids=['subnet1_id', 'subnet2_id'],
         tags=['tripleo_network_name=Network1']
     )
     network2 = stubs.FakeNeutronNetwork(
         id='network2_id',
         name='network2',
         subnet_ids=['subnet3_id', 'subnet4_id'],
         tags=['tripleo_network_name=Network2']
     )
     conn_mock.network.networks.return_value = self.a2g([network1,
                                                         network2])
     conn_mock.network.subnets.side_effect = [self.a2g([subnet1, subnet2]),
                                              self.a2g([subnet3, subnet4])]
     net_maps = network_data_v2.create_name_id_maps(conn_mock)
     expected_by_name_map = {
         'network1': {
             'id': 'network1_id',
             'name_upper': 'Network1',
             'subnets': {
                 'subnet1': 'subnet1_id',
                 'subnet2': 'subnet2_id'
             }
         },
         'network2': {
             'id': 'network2_id',
             'name_upper': 'Network2',
             'subnets': {
                 'subnet3': 'subnet3_id',
                 'subnet4': 'subnet4_id'
             }
         }
     }
     expected_by_id_map = {
         'network1_id': 'network1',
         'network2_id': 'network2',
     }
     expected_cidr_prefix_map = {
         'subnet1_id': '24',
         'subnet2_id': '25',
         'subnet3_id': '26',
         'subnet4_id': '27',
     }
     self.assertEqual(expected_by_name_map, net_maps['by_name'])
     self.assertEqual(expected_by_id_map, net_maps['by_id'])
     self.assertEqual(expected_cidr_prefix_map, net_maps['cidr_prefix_map'])