Add function to ensure floating IP to shifstack nodes

Change-Id: Ia0e50b15513fffbee1f68b45f4ecf701867b111e
This commit is contained in:
Federico Ressi 2022-06-30 11:52:58 +02:00
parent 3da13ad653
commit 42e9270d9b
4 changed files with 203 additions and 13 deletions

View File

@ -167,10 +167,12 @@ class RouterStackFixture(ExternalNetworkStackFixture):
def ensure_router_interface(self,
subnet: neutron.SubnetIdType = None,
network: neutron.NetworkIdType = None):
ensure_router_interface(network=network,
subnet=subnet,
router=self.router_id,
client=self.neutron_client)
ensure_router_interface(
network=network,
subnet=subnet,
router=self.router_id,
client=self.neutron_client,
create_router_interface_func=self.create_router_interface)
@property
def neutron_client(self) -> neutron.NeutronClientType:
@ -178,13 +180,46 @@ class RouterStackFixture(ExternalNetworkStackFixture):
self._neutron_client = neutron.neutron_client()
return self._neutron_client
@staticmethod
def create_router_interface(
router: neutron.RouterIdType = None,
subnet: neutron.SubnetIdType = None,
network: neutron.NetworkIdType = None,
client: neutron.NeutronClientType = None,
add_cleanup=False) -> neutron.PortType:
return create_router_interface(router=router,
subnet=subnet,
network=network,
client=client,
add_cleanup=add_cleanup)
def create_router_interface(
router: neutron.RouterIdType = None,
subnet: neutron.SubnetIdType = None,
network: neutron.NetworkIdType = None,
client: neutron.NeutronClientType = None,
add_cleanup=False) \
-> neutron.PortType:
stack = RouterInterfaceStackFixture(router=router,
subnet=subnet,
network=network,
neutron_client=client)
if add_cleanup:
tobiko.use_fixture(stack)
else:
tobiko.setup_fixture(stack)
return stack.port_details
def ensure_router_interface(
router: neutron.RouterIdType = None,
subnet: neutron.SubnetIdType = None,
network: neutron.NetworkIdType = None,
client: neutron.NeutronClientType = None,
add_cleanup=False) -> neutron.PortType:
add_cleanup=False,
create_router_interface_func=create_router_interface) \
-> neutron.PortType:
client = neutron.neutron_client(client)
if router is None:
router = get_router_id()
@ -230,16 +265,11 @@ def ensure_router_interface(
subnet = None
LOG.info("Add router interface to network "
f"{neutron.get_network_id(network)}")
stack = RouterInterfaceStackFixture(router=router,
return create_router_interface_func(router=router,
subnet=subnet,
network=network,
neutron_client=client)
if add_cleanup:
tobiko.use_fixture(stack)
else:
tobiko.setup_fixture(stack)
return stack.port_details
client=client,
add_cleanup=add_cleanup)
@neutron.skip_if_missing_networking_extensions('port-security')

View File

@ -0,0 +1,30 @@
# Copyright 2022 Red Hat
#
# 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 __future__ import absolute_import
import tobiko
from tobiko.openstack import heat
from tobiko.shiftstack import _keystone
def shiftstack_heat_client(obj: heat.HeatClientType) -> heat.HeatClient:
if obj is None:
return get_shiftstack_heat_client()
else:
return tobiko.check_valid_type(obj, heat.HeatClient)
def get_shiftstack_heat_client() -> heat.HeatClient:
session = _keystone.shiftstack_keystone_session()
return heat.get_heat_client(session=session)

View File

@ -0,0 +1,95 @@
# Copyright 2022 Red Hat
#
# 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 __future__ import absolute_import
import netaddr
import tobiko
from tobiko.openstack import heat
from tobiko.openstack import neutron
from tobiko.openstack import nova
from tobiko.openstack import stacks
from tobiko.shiftstack import _heat
from tobiko.shiftstack import _neutron
class ShiftstackHeatStackFixture(heat.HeatStackFixture):
def __init__(self,
neutron_client: neutron.NeutronClientType = None,
**params):
super().__init__(**params)
self._neutron_client = neutron_client
def setup_client(self) -> heat.HeatClient:
if self.client is None:
self.client = _heat.get_shiftstack_heat_client()
return self.client
@property
def neutron_client(self) -> neutron.NeutronClientType:
if self._neutron_client is None:
self._neutron_client = _neutron.get_shiftstack_neutron_client()
return self._neutron_client
class ShiftstackRouterStackFixture(ShiftstackHeatStackFixture,
stacks.RouterStackFixture):
@staticmethod
def create_router_interface(
router: neutron.RouterIdType = None,
subnet: neutron.SubnetIdType = None,
network: neutron.NetworkIdType = None,
client: neutron.NeutronClientType = None,
add_cleanup=False) -> neutron.PortType:
stack = ShiftstackRouterInterfaceStackFixture(
router=router,
subnet=subnet,
network=network,
neutron_client=client)
if add_cleanup:
tobiko.use_fixture(stack)
else:
tobiko.setup_fixture(stack)
return stack.port_details
class ShiftstackRouterInterfaceStackFixture(
ShiftstackHeatStackFixture, stacks.RouterInterfaceStackFixture):
pass
class ShiftstackFloatingIpStackFixture(ShiftstackHeatStackFixture,
stacks.FloatingIpStackFixture):
router_stack = tobiko.required_fixture(ShiftstackRouterStackFixture)
def ensure_shiftstack_node_floating_ip(
server: nova.ServerType,
client: neutron.NeutronClientType = None) \
-> netaddr.IPAddress:
client = _neutron.shiftstack_neutron_client(client)
fixed_ip_address = _neutron.find_shiftstack_node_ip_address(
server=server, client=client)
for floating_ip in neutron.list_floating_ips(client=client):
if (netaddr.IPAddress(floating_ip['fixed_ip_address']) ==
fixed_ip_address):
break
else:
fixture = ShiftstackFloatingIpStackFixture(
device_id=nova.get_server_id(server),
fixed_ip_address=str(fixed_ip_address))
floating_ip = tobiko.setup_fixture(fixture).floating_ip_details
return netaddr.IPAddress(floating_ip['floating_ip_address'])

View File

@ -0,0 +1,35 @@
# Copyright 2022 Red Hat
#
# 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 __future__ import absolute_import
import netaddr
import testtools
from tobiko import shiftstack
from tobiko.shell import ping
from tobiko.shiftstack import stacks
@shiftstack.skip_unless_has_shiftstack()
class ShiftstackStacksTest(testtools.TestCase):
def test_ensure_shiftstack_node_floating_ip(self):
nodes = shiftstack.list_shiftstack_nodes()
floating_ips = []
for node in nodes:
floating_ip = stacks.ensure_shiftstack_node_floating_ip(
server=node)
self.assertIsInstance(floating_ip, netaddr.IPAddress)
floating_ips.append(floating_ip)
ping.assert_reachable_hosts(floating_ips)