<NME> test_network_basic_ops.py
<BEF> # Copyright 2012 OpenStack Foundation
# Copyright 2013 Hewlett-Packard Development Company, L.P.
# 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.
import collections
import re

from oslo_log import log as logging
import testtools

from tempest.common import utils
from tempest.common import waiters
from tempest import config
from tempest.lib.common.utils import test_utils
from tempest.lib import decorators
from tempest.lib import exceptions
from tempest.scenario import manager

CONF = config.CONF
LOG = logging.getLogger(__name__)

Floating_IP_tuple = collections.namedtuple('Floating_IP_tuple',
                                           ['floating_ip', 'server'])


class TestNetworkBasicOps(manager.NetworkScenarioTest):

    """The test suite of network basic operations

    This smoke test suite assumes that Nova has been configured to
    boot VM's with Neutron-managed networking, and attempts to
    verify network connectivity as follows:

     There are presumed to be two types of networks: tenant and
     public.  A tenant network may or may not be reachable from the
     Tempest host.  A public network is assumed to be reachable from
     the Tempest host, and it should be possible to associate a public
     ('floating') IP address with a tenant ('fixed') IP address to
     facilitate external connectivity to a potentially unroutable
     tenant IP address.

     This test suite can be configured to test network connectivity to
     a VM via a tenant network, a public network, or both.  If both
     networking types are to be evaluated, tests that need to be
     executed remotely on the VM (via ssh) will only be run against
     one of the networks (to minimize test execution time).

     Determine which types of networks to test as follows:

     * Configure tenant network checks (via the
       'project_networks_reachable' key) if the Tempest host should
       have direct connectivity to tenant networks.  This is likely to
       be the case if Tempest is running on the same host as a
       single-node devstack installation with IP namespaces disabled.

     * Configure checks for a public network if a public network has
       been configured prior to the test suite being run and if the
       Tempest host should have connectivity to that public network.
       Checking connectivity for a public network requires that a
       value be provided for 'public_network_id'.  A value can
       optionally be provided for 'public_router_id' if tenants will
       use a shared router to access a public network (as is likely to
       be the case when IP namespaces are not enabled).  If a value is
       not provided for 'public_router_id', a router will be created
       for each tenant and use the network identified by
       'public_network_id' as its gateway.

    """

    @classmethod
    def skip_checks(cls):
        super(TestNetworkBasicOps, cls).skip_checks()
        if not (CONF.network.project_networks_reachable or
                CONF.network.public_network_id):
            msg = ('Either project_networks_reachable must be "true", or '
                   'public_network_id must be defined.')
            raise cls.skipException(msg)
        for ext in ['router', 'security-group']:
            if not utils.is_extension_enabled(ext, 'network'):
                msg = "%s extension not enabled." % ext
                raise cls.skipException(msg)
        if not CONF.network_feature_enabled.floating_ips:
            raise cls.skipException("Floating ips are not available")

    @classmethod
    def setup_credentials(cls):
        # Create no network resources for these tests.
        cls.set_network_resources()
        super(TestNetworkBasicOps, cls).setup_credentials()

    def setUp(self):
        super(TestNetworkBasicOps, self).setUp()
        self.keypairs = {}
        self.servers = []

    def _setup_network_and_servers(self, **kwargs):
        boot_with_port = kwargs.pop('boot_with_port', False)
        self.network, self.subnet, self.router = (
            self.setup_network_subnet_with_router(**kwargs))
        self.check_networks()

        self.ports = []
        port_id = None
        if boot_with_port:
            # create a port on the network and boot with that
            port_id = self.create_port(self.network['id'])['id']
            self.ports.append({'port': port_id})

        server = self._create_server(self.network, port_id)
        ssh_login = CONF.validation.image_ssh_user
        for server in self.servers:
            # call the common method in the parent class
            self.check_tenant_network_connectivity(
                server, ssh_login, self._get_server_key(server),
                servers_for_debug=self.servers)

        floating_ip = self.create_floating_ip(server)
        self.floating_ip_tuple = Floating_IP_tuple(floating_ip, server)

    def check_networks(self):
        """Checks that we see the newly created network/subnet/router

        via checking the result of list_[networks,routers,subnets]
        """

        seen_nets = self.os_admin.networks_client.list_networks()
        seen_names = [n['name'] for n in seen_nets['networks']]
        seen_ids = [n['id'] for n in seen_nets['networks']]
        self.assertIn(self.network['name'], seen_names)
        self.assertIn(self.network['id'], seen_ids)

        if self.subnet:
            seen_subnets = self.os_admin.subnets_client.list_subnets()
            seen_net_ids = [n['network_id'] for n in seen_subnets['subnets']]
            seen_subnet_ids = [n['id'] for n in seen_subnets['subnets']]
            self.assertIn(self.network['id'], seen_net_ids)
            self.assertIn(self.subnet['id'], seen_subnet_ids)

        if self.router:
            seen_routers = self.os_admin.routers_client.list_routers()
            seen_router_ids = [n['id'] for n in seen_routers['routers']]
            seen_router_names = [n['name'] for n in seen_routers['routers']]
            self.assertIn(self.router['name'],
                          seen_router_names)
            self.assertIn(self.router['id'],
                          seen_router_ids)

    def _create_server(self, network, port_id=None):
        keypair = self.create_keypair()
        self.keypairs[keypair['name']] = keypair
        security_groups = [
            {'name': self.create_security_group()['name']}
        ]
        network = {'uuid': network['id']}
        if port_id is not None:
            network['port'] = port_id

        server = self.create_server(
            networks=[network],
            key_name=keypair['name'],
            security_groups=security_groups)
        self.servers.append(server)
        return server

    def _get_server_key(self, server):
        return self.keypairs[server['key_name']]['private_key']

    def _check_public_network_connectivity(
            self, should_connect=True, msg=None,
            should_check_floating_ip_status=True, mtu=None):
        """Verifies connectivty to a VM via public network and floating IP

        and verifies floating IP has resource status is correct.

        :param should_connect: bool. determines if connectivity check is
        negative or positive.
        :param msg: Failure message to add to Error message. Should describe
        the place in the test scenario where the method was called,
        to indicate the context of the failure
        :param should_check_floating_ip_status: bool. should status of
        floating_ip be checked or not
        :param mtu: int. MTU network to use for connectivity validation
        """
        ssh_login = CONF.validation.image_ssh_user
        floating_ip, server = self.floating_ip_tuple
        ip_address = floating_ip['floating_ip_address']
        private_key = None
        floatingip_status = 'DOWN'
        if should_connect:
            private_key = self._get_server_key(server)
            floatingip_status = 'ACTIVE'

        # Check FloatingIP Status before initiating a connection
        if should_check_floating_ip_status:
            self.check_floating_ip_status(floating_ip, floatingip_status)

        message = 'Public network connectivity check failed'
        if msg:
            message += '. Reason: %s' % msg

        self.check_vm_connectivity(
            ip_address, ssh_login, private_key, should_connect,
            message, server, mtu=mtu)

    def _disassociate_floating_ips(self):
        floating_ip, _ = self.floating_ip_tuple
        floating_ip = self.floating_ips_client.update_floatingip(
            floating_ip['id'], port_id=None)['floatingip']
        self.assertIsNone(floating_ip['port_id'])
        self.floating_ip_tuple = Floating_IP_tuple(floating_ip, None)

    def _reassociate_floating_ips(self):
        floating_ip, server = self.floating_ip_tuple
        # create a new server for the floating ip
        server = self._create_server(self.network)
        port_id, _ = self.get_server_port_id_and_ip4(server)
        floating_ip = self.floating_ips_client.update_floatingip(
            floating_ip['id'], port_id=port_id)['floatingip']
        self.assertEqual(port_id, floating_ip['port_id'])
        self.floating_ip_tuple = Floating_IP_tuple(floating_ip, server)

    def _create_new_network(self, create_gateway=False):
        self.new_net = self.create_network()
        if create_gateway:
            self.new_subnet = self.create_subnet(
                network=self.new_net)
        else:
            self.new_subnet = self.create_subnet(
                network=self.new_net,
                gateway_ip=None)

    def _hotplug_server(self):
        old_floating_ip, server = self.floating_ip_tuple
        ip_address = old_floating_ip['floating_ip_address']
        private_key = self._get_server_key(server)
        ssh_client = self.get_remote_client(
            ip_address, private_key=private_key, server=server)
        old_nic_list = self._get_server_nics(ssh_client)
        # get a port from a list of one item
        port_list = self.os_admin.ports_client.list_ports(
            device_id=server['id'])['ports']
        self.assertEqual(1, len(port_list))
        old_port = port_list[0]
        interface = self.interface_client.create_interface(
            server_id=server['id'],
            net_id=self.new_net['id'])['interfaceAttachment']
        self.addCleanup(self.ports_client.wait_for_resource_deletion,
                        interface['port_id'])
        self.addCleanup(test_utils.call_and_ignore_notfound_exc,
                        self.interface_client.delete_interface,
                        server['id'], interface['port_id'])

        def check_ports():
            self.new_port_list = [
                port for port in
                self.os_admin.ports_client.list_ports(
                    device_id=server['id'])['ports']
                if port['id'] != old_port['id']
            ]
            return len(self.new_port_list) == 1

        if not test_utils.call_until_true(
                check_ports, CONF.network.build_timeout,
                CONF.network.build_interval):
            raise exceptions.TimeoutException(
                "No new port attached to the server in time (%s sec)! "
                "Old port: %s. Number of new ports: %d" % (
                    CONF.network.build_timeout, old_port,
                    len(self.new_port_list)))
        new_port = self.new_port_list[0]

        def check_new_nic():
            new_nic_list = self._get_server_nics(ssh_client)
            self.diff_list = [n for n in new_nic_list if n not in old_nic_list]
            return len(self.diff_list) == 1

        if not test_utils.call_until_true(
                check_new_nic, CONF.network.build_timeout,
                CONF.network.build_interval):
            raise exceptions.TimeoutException("Interface not visible on the "
                                              "guest after %s sec"
                                              % CONF.network.build_timeout)

        _, new_nic = self.diff_list[0]
        ip_output = ssh_client.exec_command('ip a')
        ip_address = new_port['fixed_ips'][0]['ip_address']
        ip_mask = CONF.network.project_network_mask_bits
        # check if the address is not already in use, if not, set it
        if ' ' + ip_address + '/' + str(ip_mask) not in ip_output:
            try:
                ssh_client.exec_command("sudo ip addr add %s/%s dev %s" % (
                                        ip_address, ip_mask, new_nic))
                ssh_client.exec_command("sudo ip link set %s up" % new_nic)
            except exceptions.SSHExecCommandFailed as exc:
                if 'RTNETLINK answers: File exists' in str(exc):
                    LOG.debug(
                        'IP address %(ip_address)s is already set in device '
                        '%(device)s\nPrevious "ip a" output: %(ip_output)s',
                        {'ip_address': ip_address, 'device': new_nic,
                         'ip_output': ip_output})
                else:
                    raise exc

    def _get_server_nics(self, ssh_client):
        reg = re.compile(r'(?P<num>\d+): (?P<nic_name>\w+)[@]?.*:')
        ipatxt = ssh_client.exec_command("ip address")
        return reg.findall(ipatxt)

    def _check_network_internal_connectivity(self, network,
                                             should_connect=True):
        """via ssh check VM internal connectivity:

        - ping internal gateway and DHCP port, implying in-tenant connectivity
        pinging both, because L3 and DHCP agents might be on different nodes
        - ping internal compute port, implying connectivity to other VMs on
        this network
        """
        floating_ip, server = self.floating_ip_tuple
        # get internal ports' ips:
        # get all network and compute ports in the new network
        # NOTE(ralonsoh): device_owner="network:distributed" ports are OVN
        # metadata ports and should be filtered out.
        internal_ips = (
            p['fixed_ips'][0]['ip_address'] for p in
            self.os_admin.ports_client.list_ports(
                project_id=server['tenant_id'],
                network_id=network['id'])['ports']
            if ((p['device_owner'].startswith('network') and
                 not p['device_owner'] == 'network:distributed') or
                p['device_owner'].startswith('compute'))
        )

        self._check_server_connectivity(floating_ip,
                                        internal_ips,
                                        should_connect)

    def _check_network_external_connectivity(self):
        """ping default gateway to imply external connectivity"""
        if not CONF.network.public_network_id:
            msg = 'public network not defined.'
            LOG.info(msg)
            return

        # We ping the external IP from the instance using its floating IP
        # which is always IPv4, so we must only test connectivity to
        # external IPv4 IPs if the external network is dualstack.
        v4_subnets = [
            s for s in self.os_admin.subnets_client.list_subnets(
                network_id=CONF.network.public_network_id)['subnets']
            if s['ip_version'] == 4
        ]

        if len(v4_subnets) > 1:
            self.assertTrue(
                CONF.network.subnet_id,
                "Found %d subnets. Specify subnet using configuration "
                "option [network].subnet_id."
                % len(v4_subnets))
            subnet = self.os_admin.subnets_client.show_subnet(
                CONF.network.subnet_id)['subnet']
            external_ips = [subnet['gateway_ip']]
        else:
            external_ips = [v4_subnets[0]['gateway_ip']]

        self._check_server_connectivity(self.floating_ip_tuple.floating_ip,
                                        external_ips)

    def _check_server_connectivity(self, floating_ip, address_list,
                                   should_connect=True):
        ip_address = floating_ip['floating_ip_address']
        private_key = self._get_server_key(self.floating_ip_tuple.server)
        ssh_source = self.get_remote_client(
            ip_address, private_key=private_key,
            server=self.floating_ip_tuple.server)

        for remote_ip in address_list:
            self.check_remote_connectivity(ssh_source, remote_ip,
                                           should_connect)

    def _update_router_admin_state(self, router, admin_state_up):
        kwargs = dict(admin_state_up=admin_state_up)
        router = self.routers_client.update_router(
            router['id'], **kwargs)['router']
        self.assertEqual(admin_state_up, router['admin_state_up'])

    @decorators.attr(type='smoke')
    @decorators.idempotent_id('f323b3ba-82f8-4db7-8ea6-6a895869ec49')
    @utils.services('compute', 'network')
    def test_network_basic_ops(self):
        """Basic network operation test

        For a freshly-booted VM with an IP address ("port") on a given network:

        - the Tempest host can ping the IP address.  This implies, but
            does not guarantee (see the ssh check that follows), that the
            VM has been assigned the correct IP address and has
            connectivity to the Tempest host.

        - the Tempest host can perform key-based authentication to an
            ssh server hosted at the IP address.  This check guarantees
            that the IP address is associated with the target VM.

        - the Tempest host can ssh into the VM via the IP address and
            successfully execute the following:

            - ping an external IP address, implying external connectivity.

            - ping an external hostname, implying that dns is correctly
                configured.

            - ping an internal IP address, implying connectivity to another
               VM on the same network.

        - detach the floating-ip from the VM and verify that it becomes
            unreachable

        - associate detached floating ip to a new VM and verify connectivity.
            VMs are created with unique keypair so connectivity also asserts
            that floating IP is associated with the new VM instead of the old
            one

        Verifies that floating IP status is updated correctly after each change
        """
        self._setup_network_and_servers()
        self._check_public_network_connectivity(should_connect=True)
        self._check_network_internal_connectivity(network=self.network)
        self._check_network_external_connectivity()
        self._disassociate_floating_ips()
        self._check_public_network_connectivity(should_connect=False,
                                                msg="after disassociate "
                                                    "floating ip")
        self._reassociate_floating_ips()
        self._check_public_network_connectivity(should_connect=True,
                                                msg="after re-associate "
                                                    "floating ip")

    @decorators.idempotent_id('b158ea55-472e-4086-8fa9-c64ac0c6c1d0')
    @testtools.skipUnless(utils.is_extension_enabled('net-mtu', 'network'),
                          'No way to calculate MTU for networks')
    @decorators.attr(type='slow')
    @utils.services('compute', 'network')
    def test_mtu_sized_frames(self):
        """Validate that network MTU sized frames fit through."""
        self._setup_network_and_servers()
        # first check that connectivity works in general for the instance
        self._check_public_network_connectivity(should_connect=True)
        # now that we checked general connectivity, test that full size frames
        # can also pass between nodes
        self._check_public_network_connectivity(
            should_connect=True, mtu=self.network['mtu'])

    @decorators.idempotent_id('1546850e-fbaa-42f5-8b5f-03d8a6a95f15')
    @testtools.skipIf(CONF.network.shared_physical_network,
                      'Connectivity can only be tested when in a '
                      'multitenant network environment')
    @decorators.attr(type='slow')
    @utils.services('compute', 'network')
    def test_connectivity_between_vms_on_different_networks(self):
        """Test connectivity between VMs on different networks

        For a freshly-booted VM with an IP address ("port") on a given network:

        - the Tempest host can ping the IP address.
        - the Tempest host can ssh into the VM via the IP address and
            successfully execute the following:

            - ping an external IP address, implying external connectivity.
            - ping an external hostname, implying that dns is correctly
               configured.
            - ping an internal IP address, implying connectivity to another
               VM on the same network.

        # definitions from subnet
        ssh_client.renew_lease(fixed_ip=floating_ip['fixed_ip_address'])
        self._check_dns_server(ssh_client, [alt_dns_server])
                                        new_server_ips, should_connect=True)

    @decorators.idempotent_id('c5adff73-e961-41f1-b4a9-343614f18cfa')
    @testtools.skipUnless(CONF.compute_feature_enabled.interface_attach,
                          'NIC hotplug not available')
    @testtools.skipIf(CONF.network.port_vnic_type in ['direct', 'macvtap'],
                      'NIC hotplug not supported for '
                      'vnic_type direct or macvtap')
    @utils.services('compute', 'network')
    def test_hotplug_nic(self):
        """Test hotplug network interface

        1. Create a network and a VM.
        2. Check connectivity to the VM via a public network.
        3. Create a new network, with no gateway.
        4. Bring up a new interface
        5. check the VM reach the new network

        """
        self._setup_network_and_servers()
        self._check_public_network_connectivity(should_connect=True)
        self._create_new_network()
        self._hotplug_server()
        self._check_network_internal_connectivity(network=self.new_net)

    @decorators.idempotent_id('04b9fe4e-85e8-4aea-b937-ea93885ac59f')
    @testtools.skipIf(CONF.network.shared_physical_network,
                      'Router state can be altered only with multitenant '
                      'networks capabilities')
    @decorators.attr(type='slow')
    @utils.services('compute', 'network')
    def test_update_router_admin_state(self):
        """Test to update admin state up of router

        1. Check public connectivity before updating
                admin_state_up attribute of router to False
        2. Check public connectivity after updating
                admin_state_up attribute of router to False
        3. Check public connectivity after updating
                admin_state_up attribute of router to True
        """
        self._setup_network_and_servers()
        self._check_public_network_connectivity(
            should_connect=True, msg="before updating "
            "admin_state_up of router to False")
        self._update_router_admin_state(self.router, False)
        # TODO(alokmaurya): Remove should_check_floating_ip_status=False check
        # once bug 1396310 is fixed

        self._check_public_network_connectivity(
            should_connect=False, msg="after updating "
            "admin_state_up of router to False",
            should_check_floating_ip_status=False)
        self._update_router_admin_state(self.router, True)
        self._check_public_network_connectivity(
            should_connect=True, msg="after updating "
            "admin_state_up of router to True")

    @decorators.idempotent_id('d8bb918e-e2df-48b2-97cd-b73c95450980')
    @testtools.skipIf(CONF.network.shared_physical_network,
                      'network isolation not available')
    @testtools.skipUnless(CONF.scenario.dhcp_client,
                          "DHCP client is not available.")
    @decorators.attr(type='slow')
    @utils.services('compute', 'network')
    def test_subnet_details(self):
        """Tests that subnet's extra configuration details are affecting VMs.

        This test relies on non-shared, isolated tenant networks.

        NOTE: Neutron subnets push data to servers via dhcp-agent, so any
        update in subnet requires server to actively renew its DHCP lease.

        1. Configure subnet with dns nameserver
        2. retrieve the VM's configured dns and verify it matches the one
           configured for the subnet.
        3. update subnet's dns
        4. retrieve the VM's configured dns and verify it matches the new one
           configured for the subnet.

        TODO(yfried): add host_routes

        any resolution check would be testing either:

        * l3 forwarding (tested in test_network_basic_ops)
        * Name resolution of an external DNS nameserver - out of scope for
          Tempest
        """
        # this test check only updates (no actual resolution) so using
        # arbitrary ip addresses as nameservers, instead of parsing CONF
        initial_dns_server = '1.2.3.4'
        alt_dns_server = '9.8.7.6'

        # Original timeouts are suggested by salvatore-orlando in
        # https://bugs.launchpad.net/neutron/+bug/1412325/comments/3
        #
        # Compared to that renew_delay was increased, because
        # busybox's udhcpc accepts SIGUSR1 as a renew request. Internally
        # it goes into RENEW_REQUESTED state. If it receives a 2nd SIGUSR1
        # signal while in that state then it calls the deconfig script
        # ("/sbin/cirros-dhcpc deconfig" in sufficiently new cirros versions)
        # which leads to the address being transiently deconfigured which
        # for our case is unwanted.
        renew_delay = 3 * CONF.network.build_interval
        renew_timeout = CONF.network.build_timeout

        self._setup_network_and_servers(dns_nameservers=[initial_dns_server])
        self._check_public_network_connectivity(should_connect=True)

        floating_ip, server = self.floating_ip_tuple
        ip_address = floating_ip['floating_ip_address']
        private_key = self._get_server_key(server)
        ssh_client = self.get_remote_client(
            ip_address, private_key=private_key, server=server)

        # NOTE: Server needs to renew its dhcp lease in order to get new
        # definitions from subnet
        # NOTE(amuller): we are renewing the lease as part of the retry
        # because Neutron updates dnsmasq asynchronously after the
        # subnet-update API call returns.
        ssh_client.renew_lease(fixed_ip=floating_ip['fixed_ip_address'],
                               dhcp_client=CONF.scenario.dhcp_client)
        dns_servers = [initial_dns_server]
        servers = ssh_client.get_dns_servers()
        self.assertEqual(set(dns_servers), set(servers),
                         'Looking for servers: {trgt_serv}. '
                         'Retrieved DNS nameservers: {act_serv} '
                         'From host: {host}.'
                         .format(host=ssh_client.ssh_client.host,
                                 act_serv=servers,
                                 trgt_serv=dns_servers))

        self.subnet = self.subnets_client.update_subnet(
            self.subnet['id'], dns_nameservers=[alt_dns_server])['subnet']

        # asserts that Neutron DB has updated the nameservers
        self.assertEqual([alt_dns_server], self.subnet['dns_nameservers'],
                         "Failed to update subnet's nameservers")

        def check_new_dns_server():
            # NOTE: Server needs to renew its dhcp lease in order to get new
            # definitions from subnet
            # NOTE(amuller): we are renewing the lease as part of the retry
            # because Neutron updates dnsmasq asynchronously after the
            # subnet-update API call returns.
            ssh_client.renew_lease(fixed_ip=floating_ip['fixed_ip_address'],
                                   dhcp_client=CONF.scenario.dhcp_client)
            if ssh_client.get_dns_servers() != [alt_dns_server]:
                LOG.debug("Failed to update DNS nameservers")
                return False
            return True

        self.assertTrue(test_utils.call_until_true(check_new_dns_server,
                                                   renew_timeout,
                                                   renew_delay),
                        msg="DHCP renewal failed to fetch "
                            "new DNS nameservers")

    @decorators.idempotent_id('f5dfcc22-45fd-409f-954c-5bd500d7890b')
    @testtools.skipUnless(CONF.network_feature_enabled.port_admin_state_change,
                          "Changing a port's admin state is not supported "
                          "by the test environment")
    @decorators.attr(type='slow')
    @utils.services('compute', 'network')
    def test_update_instance_port_admin_state(self):
        """Test to update admin_state_up attribute of instance port

        1. Check public and project connectivity before updating
                admin_state_up attribute of instance port to False
        2. Check public and project connectivity after updating
                admin_state_up attribute of instance port to False
        3. Check public and project connectivity after updating
                admin_state_up attribute of instance port to True
        """
        self._setup_network_and_servers()
        _, server = self.floating_ip_tuple
        server_id = server['id']
        port_id = self.os_admin.ports_client.list_ports(
            device_id=server_id)['ports'][0]['id']
        server_pip = server['addresses'][self.network['name']][0]['addr']

        server2 = self._create_server(self.network)
        server2_fip = self.create_floating_ip(server2)

        private_key = self._get_server_key(server2)
        ssh_client = self.get_remote_client(server2_fip['floating_ip_address'],
                                            private_key=private_key,
                                            server=server2)

        self._check_public_network_connectivity(
            should_connect=True, msg="before updating "
            "admin_state_up of instance port to False")
        self.check_remote_connectivity(ssh_client, dest=server_pip,
                                       should_succeed=True)
        self.ports_client.update_port(port_id, admin_state_up=False)
        self._check_public_network_connectivity(
            should_connect=False, msg="after updating "
            "admin_state_up of instance port to False",
            should_check_floating_ip_status=False)
        self.check_remote_connectivity(ssh_client, dest=server_pip,
                                       should_succeed=False)
        self.ports_client.update_port(port_id, admin_state_up=True)
        self._check_public_network_connectivity(
            should_connect=True, msg="after updating "
            "admin_state_up of instance port to True")
        self.check_remote_connectivity(ssh_client, dest=server_pip,
                                       should_succeed=True)

    @decorators.idempotent_id('759462e1-8535-46b0-ab3a-33aa45c55aaa')
    @decorators.attr(type='slow')
    @utils.services('compute', 'network')
    def test_preserve_preexisting_port(self):
        """Test preserve pre-existing port

        Tests that a pre-existing port provided on server boot is not deleted
        if the server is deleted.

        Nova should unbind the port from the instance on delete if the port was
        not created by Nova as part of the boot request.

        We should also be able to boot another server with the same port.
        """
        # Setup the network, create a port and boot the server from that port.
        self._setup_network_and_servers(boot_with_port=True)
        _, server = self.floating_ip_tuple
        self.assertEqual(1, len(self.ports),
                         'There should only be one port created for '
                         'server %s.' % server['id'])
        port_id = self.ports[0]['port']
        self.assertIsNotNone(port_id,
                             'Server should have been created from a '
                             'pre-existing port.')
        # Assert the port is bound to the server.
        port_list = self.os_admin.ports_client.list_ports(
            device_id=server['id'], network_id=self.network['id'])['ports']
        self.assertEqual(1, len(port_list),
                         'There should only be one port created for '
                         'server %s.' % server['id'])
        self.assertEqual(port_id, port_list[0]['id'])
        # Delete the server.
        self.servers_client.delete_server(server['id'])
        waiters.wait_for_server_termination(self.servers_client, server['id'])
        # Assert the port still exists on the network but is unbound from
        # the deleted server.
        port = self.ports_client.show_port(port_id)['port']
        self.assertEqual(self.network['id'], port['network_id'])
        self.assertEqual('', port['device_id'])
        self.assertEqual('', port['device_owner'])

        # Boot another server with the same port to make sure nothing was
        # left around that could cause issues.
        server = self._create_server(self.network, port['id'])
        port_list = self.os_admin.ports_client.list_ports(
            device_id=server['id'], network_id=self.network['id'])['ports']
        self.assertEqual(1, len(port_list),
                         'There should only be one port created for '
                         'server %s.' % server['id'])
        self.assertEqual(port['id'], port_list[0]['id'])

    @utils.requires_ext(service='network', extension='l3_agent_scheduler')
    @decorators.idempotent_id('2e788c46-fb3f-4ac9-8f82-0561555bea73')
    @decorators.attr(type='slow')
    @utils.services('compute', 'network')
    def test_router_rescheduling(self):
        """Tests that router can be removed from agent and add to a new agent.

        1. Verify connectivity
        2. Remove router from all l3-agents
        3. Verify connectivity is down
        4. Assign router to new l3-agent (or old one if no new agent is
           available)
        5. Verify connectivity
        """

        # TODO(yfried): refactor this test to be used for other agents (dhcp)
        # as well

        list_hosts = (self.os_admin.routers_client.
                      list_l3_agents_hosting_router)
        schedule_router = (self.os_admin.network_agents_client.
                           create_router_on_l3_agent)
        unschedule_router = (self.os_admin.network_agents_client.
                             delete_router_from_l3_agent)

        agent_list_alive = set(
            a["id"] for a in
            self.os_admin.network_agents_client.list_agents(
                agent_type="L3 agent")['agents'] if a["alive"] is True
        )
        self._setup_network_and_servers()

        # NOTE(kevinbenton): we have to use the admin credentials to check
        # for the distributed flag because self.router only has a project view.
        admin = self.os_admin.routers_client.show_router(
            self.router['id'])
        if admin['router'].get('distributed', False):
            msg = "Rescheduling test does not apply to distributed routers."
            raise self.skipException(msg)

        self._check_public_network_connectivity(should_connect=True)

        # remove resource from agents
        hosting_agents = set(a["id"] for a in
                             list_hosts(self.router['id'])['agents'])
        no_migration = agent_list_alive == hosting_agents
        LOG.info("Router will be assigned to {mig} hosting agent".
                 format(mig="the same" if no_migration else "a new"))

        for hosting_agent in hosting_agents:
            unschedule_router(hosting_agent, self.router['id'])
            self.assertNotIn(hosting_agent,
                             [a["id"] for a in
                              list_hosts(self.router['id'])['agents']],
                             'unscheduling router failed')

        # verify resource is un-functional
        self._check_public_network_connectivity(
            should_connect=False,
            msg='after router unscheduling',
        )

        # schedule resource to new agent
        target_agent = list(hosting_agents if no_migration else
                            agent_list_alive - hosting_agents)[0]
        schedule_router(target_agent,
                        router_id=self.router['id'])
        self.assertEqual(
            target_agent,
            list_hosts(self.router['id'])['agents'][0]['id'],
            "Router failed to reschedule. Hosting agent doesn't match "
            "target agent")

        # verify resource is functional
        self._check_public_network_connectivity(
            should_connect=True,
            msg='After router rescheduling')

    @utils.requires_ext(service='network', extension='port-security')
    @testtools.skipUnless(CONF.compute_feature_enabled.interface_attach,
                          'NIC hotplug not available')
    @decorators.idempotent_id('7c0bb1a2-d053-49a4-98f9-ca1a1d849f63')
    @decorators.attr(type='slow')
    @utils.services('compute', 'network')
    def test_port_security_macspoofing_port(self):
        """Tests port_security extension enforces mac spoofing

        Neutron security groups always apply anti-spoof rules on the VMs. This
        allows traffic to originate and terminate at the VM as expected, but
        prevents traffic to pass through the VM. Anti-spoof rules are not
        required in cases where the VM routes traffic through it.

        The test steps are:

        1. Create a new network.
        2. Connect (hotplug) the VM to a new network.
        3. Check the VM can ping a server on the new network ("peer")
        4. Spoof the mac address of the new VM interface.
        5. Check the Security Group enforces mac spoofing and blocks pings via
           spoofed interface (VM cannot ping the peer).
        6. Disable port-security of the spoofed port- set the flag to false.
        7. Retest 3rd step and check that the Security Group allows pings via
           the spoofed interface.
        """

        spoof_mac = "00:00:00:00:00:01"

        # Create server
        self._setup_network_and_servers()
        self._check_public_network_connectivity(should_connect=True)
        self._create_new_network()
        self._hotplug_server()
        fip, server = self.floating_ip_tuple
        new_ports = self.os_admin.ports_client.list_ports(
            device_id=server["id"], network_id=self.new_net["id"])['ports']
        spoof_port = new_ports[0]
        private_key = self._get_server_key(server)
        ssh_client = self.get_remote_client(fip['floating_ip_address'],
                                            private_key=private_key,
                                            server=server)
        spoof_nic = ssh_client.get_nic_name_by_mac(spoof_port["mac_address"])
        peer = self._create_server(self.new_net)
        peer_address = peer['addresses'][self.new_net['name']][0]['addr']
        self.check_remote_connectivity(ssh_client, dest=peer_address,
                                       nic=spoof_nic, should_succeed=True)
        # Set a mac address by making nic down temporary
        cmd = ("sudo ip link set {nic} down;"
               "sudo ip link set dev {nic} address {mac};"
               "sudo ip link set {nic} up").format(nic=spoof_nic,
                                                   mac=spoof_mac)
        ssh_client.exec_command(cmd)

        new_mac = ssh_client.get_mac_address(nic=spoof_nic)
        self.assertEqual(spoof_mac, new_mac)
        self.check_remote_connectivity(ssh_client, dest=peer_address,
                                       nic=spoof_nic, should_succeed=False)
        self.ports_client.update_port(spoof_port["id"],
                                      port_security_enabled=False,
                                      security_groups=[])
        self.check_remote_connectivity(ssh_client, dest=peer_address,
                                       nic=spoof_nic, should_succeed=True)

<MSG> test_update_instance_port_admin_state

This test validates instance public connectivity
status by changing instance port admin_state_up
to True & False.

Change-Id: I3ada84cd6a65932869330a657a999266cfb67081

<DFF> @@ -484,3 +484,31 @@ class TestNetworkBasicOps(manager.NetworkScenarioTest):
         # definitions from subnet
         ssh_client.renew_lease(fixed_ip=floating_ip['fixed_ip_address'])
         self._check_dns_server(ssh_client, [alt_dns_server])
+
+    @test.attr(type='smoke')
+    @test.services('compute', 'network')
+    def test_update_instance_port_admin_state(self):
+        """
+        1. Check public connectivity before updating
+                admin_state_up attribute of instance port to False
+        2. Check public connectivity after updating
+                admin_state_up attribute of instance port to False
+        3. Check public connectivity after updating
+                admin_state_up attribute of instance port to True
+        """
+        self._setup_network_and_servers()
+        floating_ip, server = self.floating_ip_tuple
+        server_id = server['id']
+        port_id = self._list_ports(device_id=server_id)[0]['id']
+        self.check_public_network_connectivity(
+            should_connect=True, msg="before updating "
+            "admin_state_up of instance port to False")
+        self.network_client.update_port(port_id, admin_state_up=False)
+        self.check_public_network_connectivity(
+            should_connect=False, msg="after updating "
+            "admin_state_up of instance port to False",
+            should_check_floating_ip_status=False)
+        self.network_client.update_port(port_id, admin_state_up=True)
+        self.check_public_network_connectivity(
+            should_connect=True, msg="after updating "
+            "admin_state_up of instance port to True")

{'commit': 'diff --git a/tempest/scenario/test_network_basic_ops.py b/tempest/scenario/test_network_basic_ops.py\nindex 2cfec14c1..96cbf24e0 100644\n--- a/tempest/scenario/test_network_basic_ops.py\n+++ b/tempest/scenario/test_network_basic_ops.py\n@@ -484,3 +484,31 @@ class TestNetworkBasicOps(manager.NetworkScenarioTest):\n         # definitions from subnet\n         ssh_client.renew_lease(fixed_ip=floating_ip[\'fixed_ip_address\'])\n         self._check_dns_server(ssh_client, [alt_dns_server])\n+\n+    @test.attr(type=\'smoke\')\n+    @test.services(\'compute\', \'network\')\n+    def test_update_instance_port_admin_state(self):\n+        """\n+        1. Check public connectivity before updating\n+                admin_state_up attribute of instance port to False\n+        2. Check public connectivity after updating\n+                admin_state_up attribute of instance port to False\n+        3. Check public connectivity after updating\n+                admin_state_up attribute of instance port to True\n+        """\n+        self._setup_network_and_servers()\n+        floating_ip, server = self.floating_ip_tuple\n+        server_id = server[\'id\']\n+        port_id = self._list_ports(device_id=server_id)[0][\'id\']\n+        self.check_public_network_connectivity(\n+            should_connect=True, msg="before updating "\n+            "admin_state_up of instance port to False")\n+        self.network_client.update_port(port_id, admin_state_up=False)\n+        self.check_public_network_connectivity(\n+            should_connect=False, msg="after updating "\n+            "admin_state_up of instance port to False",\n+            should_check_floating_ip_status=False)\n+        self.network_client.update_port(port_id, admin_state_up=True)\n+        self.check_public_network_connectivity(\n+            should_connect=True, msg="after updating "\n+            "admin_state_up of instance port to True")\n', 'commit_subject': 'test_update_instance_port_admin_state', 'repo_name': 'openstack/tempest', 'committer': {'name': 'Thalabathy Venkatesan', 'email': 'b7adb7cba5217a5dff1da76f5878833aba8e4fd9@hp.com', 'time_sec': '1421649628', 'tz_offset': '-480', 'date': {'seconds': '1421649628'}}, 'commit_tree': '938c447f229c8166af92a457faaa78af3b361f8c', 'commit_message': 'test_update_instance_port_admin_state\n\nThis test validates instance public connectivity\nstatus by changing instance port admin_state_up\nto True & False.\n\nChange-Id: I3ada84cd6a65932869330a657a999266cfb67081\n', 'language_name': 'Shell', 'license': 'apache-2.0', 'hunks': '@@ -484,3 +484,31 @@ class TestNetworkBasicOps(manager.NetworkScenarioTest):\n         # definitions from subnet\n         ssh_client.renew_lease(fixed_ip=floating_ip[\'fixed_ip_address\'])\n         self._check_dns_server(ssh_client, [alt_dns_server])\n+\n+    @test.attr(type=\'smoke\')\n+    @test.services(\'compute\', \'network\')\n+    def test_update_instance_port_admin_state(self):\n+        """\n+        1. Check public connectivity before updating\n+                admin_state_up attribute of instance port to False\n+        2. Check public connectivity after updating\n+                admin_state_up attribute of instance port to False\n+        3. Check public connectivity after updating\n+                admin_state_up attribute of instance port to True\n+        """\n+        self._setup_network_and_servers()\n+        floating_ip, server = self.floating_ip_tuple\n+        server_id = server[\'id\']\n+        port_id = self._list_ports(device_id=server_id)[0][\'id\']\n+        self.check_public_network_connectivity(\n+            should_connect=True, msg="before updating "\n+            "admin_state_up of instance port to False")\n+        self.network_client.update_port(port_id, admin_state_up=False)\n+        self.check_public_network_connectivity(\n+            should_connect=False, msg="after updating "\n+            "admin_state_up of instance port to False",\n+            should_check_floating_ip_status=False)\n+        self.network_client.update_port(port_id, admin_state_up=True)\n+        self.check_public_network_connectivity(\n+            should_connect=True, msg="after updating "\n+            "admin_state_up of instance port to True")\n', 'addition_count': 28, 'deletion_count': 0, 'src_file': 'a/tempest/scenario/test_network_basic_ops.py', 'tgt_file': 'b/tempest/scenario/test_network_basic_ops.py', 'file_extension': '.py', 'before_file': '# Copyright 2012 OpenStack Foundation\n# Copyright 2013 Hewlett-Packard Development Company, L.P.\n# All Rights Reserved.\n#\n#    Licensed under the Apache License, Version 2.0 (the "License"); you may\n#    not use this file except in compliance with the License. You may obtain\n#    a copy of the License at\n#\n#         http://www.apache.org/licenses/LICENSE-2.0\n#\n#    Unless required by applicable law or agreed to in writing, software\n#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT\n#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n#    License for the specific language governing permissions and limitations\n#    under the License.\nimport collections\nimport re\n\nfrom oslo_log import log as logging\nimport testtools\n\nfrom tempest.common import utils\nfrom tempest.common import waiters\nfrom tempest import config\nfrom tempest.lib.common.utils import test_utils\nfrom tempest.lib import decorators\nfrom tempest.lib import exceptions\nfrom tempest.scenario import manager\n\nCONF = config.CONF\nLOG = logging.getLogger(__name__)\n\nFloating_IP_tuple = collections.namedtuple(\'Floating_IP_tuple\',\n                                           [\'floating_ip\', \'server\'])\n\n\nclass TestNetworkBasicOps(manager.NetworkScenarioTest):\n\n    """The test suite of network basic operations\n\n    This smoke test suite assumes that Nova has been configured to\n    boot VM\'s with Neutron-managed networking, and attempts to\n    verify network connectivity as follows:\n\n     There are presumed to be two types of networks: tenant and\n     public.  A tenant network may or may not be reachable from the\n     Tempest host.  A public network is assumed to be reachable from\n     the Tempest host, and it should be possible to associate a public\n     (\'floating\') IP address with a tenant (\'fixed\') IP address to\n     facilitate external connectivity to a potentially unroutable\n     tenant IP address.\n\n     This test suite can be configured to test network connectivity to\n     a VM via a tenant network, a public network, or both.  If both\n     networking types are to be evaluated, tests that need to be\n     executed remotely on the VM (via ssh) will only be run against\n     one of the networks (to minimize test execution time).\n\n     Determine which types of networks to test as follows:\n\n     * Configure tenant network checks (via the\n       \'project_networks_reachable\' key) if the Tempest host should\n       have direct connectivity to tenant networks.  This is likely to\n       be the case if Tempest is running on the same host as a\n       single-node devstack installation with IP namespaces disabled.\n\n     * Configure checks for a public network if a public network has\n       been configured prior to the test suite being run and if the\n       Tempest host should have connectivity to that public network.\n       Checking connectivity for a public network requires that a\n       value be provided for \'public_network_id\'.  A value can\n       optionally be provided for \'public_router_id\' if tenants will\n       use a shared router to access a public network (as is likely to\n       be the case when IP namespaces are not enabled).  If a value is\n       not provided for \'public_router_id\', a router will be created\n       for each tenant and use the network identified by\n       \'public_network_id\' as its gateway.\n\n    """\n\n    @classmethod\n    def skip_checks(cls):\n        super(TestNetworkBasicOps, cls).skip_checks()\n        if not (CONF.network.project_networks_reachable or\n                CONF.network.public_network_id):\n            msg = (\'Either project_networks_reachable must be "true", or \'\n                   \'public_network_id must be defined.\')\n            raise cls.skipException(msg)\n        for ext in [\'router\', \'security-group\']:\n            if not utils.is_extension_enabled(ext, \'network\'):\n                msg = "%s extension not enabled." % ext\n                raise cls.skipException(msg)\n        if not CONF.network_feature_enabled.floating_ips:\n            raise cls.skipException("Floating ips are not available")\n\n    @classmethod\n    def setup_credentials(cls):\n        # Create no network resources for these tests.\n        cls.set_network_resources()\n        super(TestNetworkBasicOps, cls).setup_credentials()\n\n    def setUp(self):\n        super(TestNetworkBasicOps, self).setUp()\n        self.keypairs = {}\n        self.servers = []\n\n    def _setup_network_and_servers(self, **kwargs):\n        boot_with_port = kwargs.pop(\'boot_with_port\', False)\n        self.network, self.subnet, self.router = (\n            self.setup_network_subnet_with_router(**kwargs))\n        self.check_networks()\n\n        self.ports = []\n        port_id = None\n        if boot_with_port:\n            # create a port on the network and boot with that\n            port_id = self.create_port(self.network[\'id\'])[\'id\']\n            self.ports.append({\'port\': port_id})\n\n        server = self._create_server(self.network, port_id)\n        ssh_login = CONF.validation.image_ssh_user\n        for server in self.servers:\n            # call the common method in the parent class\n            self.check_tenant_network_connectivity(\n                server, ssh_login, self._get_server_key(server),\n                servers_for_debug=self.servers)\n\n        floating_ip = self.create_floating_ip(server)\n        self.floating_ip_tuple = Floating_IP_tuple(floating_ip, server)\n\n    def check_networks(self):\n        """Checks that we see the newly created network/subnet/router\n\n        via checking the result of list_[networks,routers,subnets]\n        """\n\n        seen_nets = self.os_admin.networks_client.list_networks()\n        seen_names = [n[\'name\'] for n in seen_nets[\'networks\']]\n        seen_ids = [n[\'id\'] for n in seen_nets[\'networks\']]\n        self.assertIn(self.network[\'name\'], seen_names)\n        self.assertIn(self.network[\'id\'], seen_ids)\n\n        if self.subnet:\n            seen_subnets = self.os_admin.subnets_client.list_subnets()\n            seen_net_ids = [n[\'network_id\'] for n in seen_subnets[\'subnets\']]\n            seen_subnet_ids = [n[\'id\'] for n in seen_subnets[\'subnets\']]\n            self.assertIn(self.network[\'id\'], seen_net_ids)\n            self.assertIn(self.subnet[\'id\'], seen_subnet_ids)\n\n        if self.router:\n            seen_routers = self.os_admin.routers_client.list_routers()\n            seen_router_ids = [n[\'id\'] for n in seen_routers[\'routers\']]\n            seen_router_names = [n[\'name\'] for n in seen_routers[\'routers\']]\n            self.assertIn(self.router[\'name\'],\n                          seen_router_names)\n            self.assertIn(self.router[\'id\'],\n                          seen_router_ids)\n\n    def _create_server(self, network, port_id=None):\n        keypair = self.create_keypair()\n        self.keypairs[keypair[\'name\']] = keypair\n        security_groups = [\n            {\'name\': self.create_security_group()[\'name\']}\n        ]\n        network = {\'uuid\': network[\'id\']}\n        if port_id is not None:\n            network[\'port\'] = port_id\n\n        server = self.create_server(\n            networks=[network],\n            key_name=keypair[\'name\'],\n            security_groups=security_groups)\n        self.servers.append(server)\n        return server\n\n    def _get_server_key(self, server):\n        return self.keypairs[server[\'key_name\']][\'private_key\']\n\n    def _check_public_network_connectivity(\n            self, should_connect=True, msg=None,\n            should_check_floating_ip_status=True, mtu=None):\n        """Verifies connectivty to a VM via public network and floating IP\n\n        and verifies floating IP has resource status is correct.\n\n        :param should_connect: bool. determines if connectivity check is\n        negative or positive.\n        :param msg: Failure message to add to Error message. Should describe\n        the place in the test scenario where the method was called,\n        to indicate the context of the failure\n        :param should_check_floating_ip_status: bool. should status of\n        floating_ip be checked or not\n        :param mtu: int. MTU network to use for connectivity validation\n        """\n        ssh_login = CONF.validation.image_ssh_user\n        floating_ip, server = self.floating_ip_tuple\n        ip_address = floating_ip[\'floating_ip_address\']\n        private_key = None\n        floatingip_status = \'DOWN\'\n        if should_connect:\n            private_key = self._get_server_key(server)\n            floatingip_status = \'ACTIVE\'\n\n        # Check FloatingIP Status before initiating a connection\n        if should_check_floating_ip_status:\n            self.check_floating_ip_status(floating_ip, floatingip_status)\n\n        message = \'Public network connectivity check failed\'\n        if msg:\n            message += \'. Reason: %s\' % msg\n\n        self.check_vm_connectivity(\n            ip_address, ssh_login, private_key, should_connect,\n            message, server, mtu=mtu)\n\n    def _disassociate_floating_ips(self):\n        floating_ip, _ = self.floating_ip_tuple\n        floating_ip = self.floating_ips_client.update_floatingip(\n            floating_ip[\'id\'], port_id=None)[\'floatingip\']\n        self.assertIsNone(floating_ip[\'port_id\'])\n        self.floating_ip_tuple = Floating_IP_tuple(floating_ip, None)\n\n    def _reassociate_floating_ips(self):\n        floating_ip, server = self.floating_ip_tuple\n        # create a new server for the floating ip\n        server = self._create_server(self.network)\n        port_id, _ = self.get_server_port_id_and_ip4(server)\n        floating_ip = self.floating_ips_client.update_floatingip(\n            floating_ip[\'id\'], port_id=port_id)[\'floatingip\']\n        self.assertEqual(port_id, floating_ip[\'port_id\'])\n        self.floating_ip_tuple = Floating_IP_tuple(floating_ip, server)\n\n    def _create_new_network(self, create_gateway=False):\n        self.new_net = self.create_network()\n        if create_gateway:\n            self.new_subnet = self.create_subnet(\n                network=self.new_net)\n        else:\n            self.new_subnet = self.create_subnet(\n                network=self.new_net,\n                gateway_ip=None)\n\n    def _hotplug_server(self):\n        old_floating_ip, server = self.floating_ip_tuple\n        ip_address = old_floating_ip[\'floating_ip_address\']\n        private_key = self._get_server_key(server)\n        ssh_client = self.get_remote_client(\n            ip_address, private_key=private_key, server=server)\n        old_nic_list = self._get_server_nics(ssh_client)\n        # get a port from a list of one item\n        port_list = self.os_admin.ports_client.list_ports(\n            device_id=server[\'id\'])[\'ports\']\n        self.assertEqual(1, len(port_list))\n        old_port = port_list[0]\n        interface = self.interface_client.create_interface(\n            server_id=server[\'id\'],\n            net_id=self.new_net[\'id\'])[\'interfaceAttachment\']\n        self.addCleanup(self.ports_client.wait_for_resource_deletion,\n                        interface[\'port_id\'])\n        self.addCleanup(test_utils.call_and_ignore_notfound_exc,\n                        self.interface_client.delete_interface,\n                        server[\'id\'], interface[\'port_id\'])\n\n        def check_ports():\n            self.new_port_list = [\n                port for port in\n                self.os_admin.ports_client.list_ports(\n                    device_id=server[\'id\'])[\'ports\']\n                if port[\'id\'] != old_port[\'id\']\n            ]\n            return len(self.new_port_list) == 1\n\n        if not test_utils.call_until_true(\n                check_ports, CONF.network.build_timeout,\n                CONF.network.build_interval):\n            raise exceptions.TimeoutException(\n                "No new port attached to the server in time (%s sec)! "\n                "Old port: %s. Number of new ports: %d" % (\n                    CONF.network.build_timeout, old_port,\n                    len(self.new_port_list)))\n        new_port = self.new_port_list[0]\n\n        def check_new_nic():\n            new_nic_list = self._get_server_nics(ssh_client)\n            self.diff_list = [n for n in new_nic_list if n not in old_nic_list]\n            return len(self.diff_list) == 1\n\n        if not test_utils.call_until_true(\n                check_new_nic, CONF.network.build_timeout,\n                CONF.network.build_interval):\n            raise exceptions.TimeoutException("Interface not visible on the "\n                                              "guest after %s sec"\n                                              % CONF.network.build_timeout)\n\n        _, new_nic = self.diff_list[0]\n        ip_output = ssh_client.exec_command(\'ip a\')\n        ip_address = new_port[\'fixed_ips\'][0][\'ip_address\']\n        ip_mask = CONF.network.project_network_mask_bits\n        # check if the address is not already in use, if not, set it\n        if \' \' + ip_address + \'/\' + str(ip_mask) not in ip_output:\n            try:\n                ssh_client.exec_command("sudo ip addr add %s/%s dev %s" % (\n                                        ip_address, ip_mask, new_nic))\n                ssh_client.exec_command("sudo ip link set %s up" % new_nic)\n            except exceptions.SSHExecCommandFailed as exc:\n                if \'RTNETLINK answers: File exists\' in str(exc):\n                    LOG.debug(\n                        \'IP address %(ip_address)s is already set in device \'\n                        \'%(device)s\\nPrevious "ip a" output: %(ip_output)s\',\n                        {\'ip_address\': ip_address, \'device\': new_nic,\n                         \'ip_output\': ip_output})\n                else:\n                    raise exc\n\n    def _get_server_nics(self, ssh_client):\n        reg = re.compile(r\'(?P<num>\\d+): (?P<nic_name>\\w+)[@]?.*:\')\n        ipatxt = ssh_client.exec_command("ip address")\n        return reg.findall(ipatxt)\n\n    def _check_network_internal_connectivity(self, network,\n                                             should_connect=True):\n        """via ssh check VM internal connectivity:\n\n        - ping internal gateway and DHCP port, implying in-tenant connectivity\n        pinging both, because L3 and DHCP agents might be on different nodes\n        - ping internal compute port, implying connectivity to other VMs on\n        this network\n        """\n        floating_ip, server = self.floating_ip_tuple\n        # get internal ports\' ips:\n        # get all network and compute ports in the new network\n        # NOTE(ralonsoh): device_owner="network:distributed" ports are OVN\n        # metadata ports and should be filtered out.\n        internal_ips = (\n            p[\'fixed_ips\'][0][\'ip_address\'] for p in\n            self.os_admin.ports_client.list_ports(\n                project_id=server[\'tenant_id\'],\n                network_id=network[\'id\'])[\'ports\']\n            if ((p[\'device_owner\'].startswith(\'network\') and\n                 not p[\'device_owner\'] == \'network:distributed\') or\n                p[\'device_owner\'].startswith(\'compute\'))\n        )\n\n        self._check_server_connectivity(floating_ip,\n                                        internal_ips,\n                                        should_connect)\n\n    def _check_network_external_connectivity(self):\n        """ping default gateway to imply external connectivity"""\n        if not CONF.network.public_network_id:\n            msg = \'public network not defined.\'\n            LOG.info(msg)\n            return\n\n        # We ping the external IP from the instance using its floating IP\n        # which is always IPv4, so we must only test connectivity to\n        # external IPv4 IPs if the external network is dualstack.\n        v4_subnets = [\n            s for s in self.os_admin.subnets_client.list_subnets(\n                network_id=CONF.network.public_network_id)[\'subnets\']\n            if s[\'ip_version\'] == 4\n        ]\n\n        if len(v4_subnets) > 1:\n            self.assertTrue(\n                CONF.network.subnet_id,\n                "Found %d subnets. Specify subnet using configuration "\n                "option [network].subnet_id."\n                % len(v4_subnets))\n            subnet = self.os_admin.subnets_client.show_subnet(\n                CONF.network.subnet_id)[\'subnet\']\n            external_ips = [subnet[\'gateway_ip\']]\n        else:\n            external_ips = [v4_subnets[0][\'gateway_ip\']]\n\n        self._check_server_connectivity(self.floating_ip_tuple.floating_ip,\n                                        external_ips)\n\n    def _check_server_connectivity(self, floating_ip, address_list,\n                                   should_connect=True):\n        ip_address = floating_ip[\'floating_ip_address\']\n        private_key = self._get_server_key(self.floating_ip_tuple.server)\n        ssh_source = self.get_remote_client(\n            ip_address, private_key=private_key,\n            server=self.floating_ip_tuple.server)\n\n        for remote_ip in address_list:\n            self.check_remote_connectivity(ssh_source, remote_ip,\n                                           should_connect)\n\n    def _update_router_admin_state(self, router, admin_state_up):\n        kwargs = dict(admin_state_up=admin_state_up)\n        router = self.routers_client.update_router(\n            router[\'id\'], **kwargs)[\'router\']\n        self.assertEqual(admin_state_up, router[\'admin_state_up\'])\n\n    @decorators.attr(type=\'smoke\')\n    @decorators.idempotent_id(\'f323b3ba-82f8-4db7-8ea6-6a895869ec49\')\n    @utils.services(\'compute\', \'network\')\n    def test_network_basic_ops(self):\n        """Basic network operation test\n\n        For a freshly-booted VM with an IP address ("port") on a given network:\n\n        - the Tempest host can ping the IP address.  This implies, but\n            does not guarantee (see the ssh check that follows), that the\n            VM has been assigned the correct IP address and has\n            connectivity to the Tempest host.\n\n        - the Tempest host can perform key-based authentication to an\n            ssh server hosted at the IP address.  This check guarantees\n            that the IP address is associated with the target VM.\n\n        - the Tempest host can ssh into the VM via the IP address and\n            successfully execute the following:\n\n            - ping an external IP address, implying external connectivity.\n\n            - ping an external hostname, implying that dns is correctly\n                configured.\n\n            - ping an internal IP address, implying connectivity to another\n               VM on the same network.\n\n        - detach the floating-ip from the VM and verify that it becomes\n            unreachable\n\n        - associate detached floating ip to a new VM and verify connectivity.\n            VMs are created with unique keypair so connectivity also asserts\n            that floating IP is associated with the new VM instead of the old\n            one\n\n        Verifies that floating IP status is updated correctly after each change\n        """\n        self._setup_network_and_servers()\n        self._check_public_network_connectivity(should_connect=True)\n        self._check_network_internal_connectivity(network=self.network)\n        self._check_network_external_connectivity()\n        self._disassociate_floating_ips()\n        self._check_public_network_connectivity(should_connect=False,\n                                                msg="after disassociate "\n                                                    "floating ip")\n        self._reassociate_floating_ips()\n        self._check_public_network_connectivity(should_connect=True,\n                                                msg="after re-associate "\n                                                    "floating ip")\n\n    @decorators.idempotent_id(\'b158ea55-472e-4086-8fa9-c64ac0c6c1d0\')\n    @testtools.skipUnless(utils.is_extension_enabled(\'net-mtu\', \'network\'),\n                          \'No way to calculate MTU for networks\')\n    @decorators.attr(type=\'slow\')\n    @utils.services(\'compute\', \'network\')\n    def test_mtu_sized_frames(self):\n        """Validate that network MTU sized frames fit through."""\n        self._setup_network_and_servers()\n        # first check that connectivity works in general for the instance\n        self._check_public_network_connectivity(should_connect=True)\n        # now that we checked general connectivity, test that full size frames\n        # can also pass between nodes\n        self._check_public_network_connectivity(\n            should_connect=True, mtu=self.network[\'mtu\'])\n\n    @decorators.idempotent_id(\'1546850e-fbaa-42f5-8b5f-03d8a6a95f15\')\n    @testtools.skipIf(CONF.network.shared_physical_network,\n                      \'Connectivity can only be tested when in a \'\n                      \'multitenant network environment\')\n    @decorators.attr(type=\'slow\')\n    @utils.services(\'compute\', \'network\')\n    def test_connectivity_between_vms_on_different_networks(self):\n        """Test connectivity between VMs on different networks\n\n        For a freshly-booted VM with an IP address ("port") on a given network:\n\n        - the Tempest host can ping the IP address.\n        - the Tempest host can ssh into the VM via the IP address and\n            successfully execute the following:\n\n            - ping an external IP address, implying external connectivity.\n            - ping an external hostname, implying that dns is correctly\n               configured.\n            - ping an internal IP address, implying connectivity to another\n               VM on the same network.\n\n        # definitions from subnet\n        ssh_client.renew_lease(fixed_ip=floating_ip[\'fixed_ip_address\'])\n        self._check_dns_server(ssh_client, [alt_dns_server])\n                                        new_server_ips, should_connect=True)\n\n    @decorators.idempotent_id(\'c5adff73-e961-41f1-b4a9-343614f18cfa\')\n    @testtools.skipUnless(CONF.compute_feature_enabled.interface_attach,\n                          \'NIC hotplug not available\')\n    @testtools.skipIf(CONF.network.port_vnic_type in [\'direct\', \'macvtap\'],\n                      \'NIC hotplug not supported for \'\n                      \'vnic_type direct or macvtap\')\n    @utils.services(\'compute\', \'network\')\n    def test_hotplug_nic(self):\n        """Test hotplug network interface\n\n        1. Create a network and a VM.\n        2. Check connectivity to the VM via a public network.\n        3. Create a new network, with no gateway.\n        4. Bring up a new interface\n        5. check the VM reach the new network\n\n        """\n        self._setup_network_and_servers()\n        self._check_public_network_connectivity(should_connect=True)\n        self._create_new_network()\n        self._hotplug_server()\n        self._check_network_internal_connectivity(network=self.new_net)\n\n    @decorators.idempotent_id(\'04b9fe4e-85e8-4aea-b937-ea93885ac59f\')\n    @testtools.skipIf(CONF.network.shared_physical_network,\n                      \'Router state can be altered only with multitenant \'\n                      \'networks capabilities\')\n    @decorators.attr(type=\'slow\')\n    @utils.services(\'compute\', \'network\')\n    def test_update_router_admin_state(self):\n        """Test to update admin state up of router\n\n        1. Check public connectivity before updating\n                admin_state_up attribute of router to False\n        2. Check public connectivity after updating\n                admin_state_up attribute of router to False\n        3. Check public connectivity after updating\n                admin_state_up attribute of router to True\n        """\n        self._setup_network_and_servers()\n        self._check_public_network_connectivity(\n            should_connect=True, msg="before updating "\n            "admin_state_up of router to False")\n        self._update_router_admin_state(self.router, False)\n        # TODO(alokmaurya): Remove should_check_floating_ip_status=False check\n        # once bug 1396310 is fixed\n\n        self._check_public_network_connectivity(\n            should_connect=False, msg="after updating "\n            "admin_state_up of router to False",\n            should_check_floating_ip_status=False)\n        self._update_router_admin_state(self.router, True)\n        self._check_public_network_connectivity(\n            should_connect=True, msg="after updating "\n            "admin_state_up of router to True")\n\n    @decorators.idempotent_id(\'d8bb918e-e2df-48b2-97cd-b73c95450980\')\n    @testtools.skipIf(CONF.network.shared_physical_network,\n                      \'network isolation not available\')\n    @testtools.skipUnless(CONF.scenario.dhcp_client,\n                          "DHCP client is not available.")\n    @decorators.attr(type=\'slow\')\n    @utils.services(\'compute\', \'network\')\n    def test_subnet_details(self):\n        """Tests that subnet\'s extra configuration details are affecting VMs.\n\n        This test relies on non-shared, isolated tenant networks.\n\n        NOTE: Neutron subnets push data to servers via dhcp-agent, so any\n        update in subnet requires server to actively renew its DHCP lease.\n\n        1. Configure subnet with dns nameserver\n        2. retrieve the VM\'s configured dns and verify it matches the one\n           configured for the subnet.\n        3. update subnet\'s dns\n        4. retrieve the VM\'s configured dns and verify it matches the new one\n           configured for the subnet.\n\n        TODO(yfried): add host_routes\n\n        any resolution check would be testing either:\n\n        * l3 forwarding (tested in test_network_basic_ops)\n        * Name resolution of an external DNS nameserver - out of scope for\n          Tempest\n        """\n        # this test check only updates (no actual resolution) so using\n        # arbitrary ip addresses as nameservers, instead of parsing CONF\n        initial_dns_server = \'1.2.3.4\'\n        alt_dns_server = \'9.8.7.6\'\n\n        # Original timeouts are suggested by salvatore-orlando in\n        # https://bugs.launchpad.net/neutron/+bug/1412325/comments/3\n        #\n        # Compared to that renew_delay was increased, because\n        # busybox\'s udhcpc accepts SIGUSR1 as a renew request. Internally\n        # it goes into RENEW_REQUESTED state. If it receives a 2nd SIGUSR1\n        # signal while in that state then it calls the deconfig script\n        # ("/sbin/cirros-dhcpc deconfig" in sufficiently new cirros versions)\n        # which leads to the address being transiently deconfigured which\n        # for our case is unwanted.\n        renew_delay = 3 * CONF.network.build_interval\n        renew_timeout = CONF.network.build_timeout\n\n        self._setup_network_and_servers(dns_nameservers=[initial_dns_server])\n        self._check_public_network_connectivity(should_connect=True)\n\n        floating_ip, server = self.floating_ip_tuple\n        ip_address = floating_ip[\'floating_ip_address\']\n        private_key = self._get_server_key(server)\n        ssh_client = self.get_remote_client(\n            ip_address, private_key=private_key, server=server)\n\n        # NOTE: Server needs to renew its dhcp lease in order to get new\n        # definitions from subnet\n        # NOTE(amuller): we are renewing the lease as part of the retry\n        # because Neutron updates dnsmasq asynchronously after the\n        # subnet-update API call returns.\n        ssh_client.renew_lease(fixed_ip=floating_ip[\'fixed_ip_address\'],\n                               dhcp_client=CONF.scenario.dhcp_client)\n        dns_servers = [initial_dns_server]\n        servers = ssh_client.get_dns_servers()\n        self.assertEqual(set(dns_servers), set(servers),\n                         \'Looking for servers: {trgt_serv}. \'\n                         \'Retrieved DNS nameservers: {act_serv} \'\n                         \'From host: {host}.\'\n                         .format(host=ssh_client.ssh_client.host,\n                                 act_serv=servers,\n                                 trgt_serv=dns_servers))\n\n        self.subnet = self.subnets_client.update_subnet(\n            self.subnet[\'id\'], dns_nameservers=[alt_dns_server])[\'subnet\']\n\n        # asserts that Neutron DB has updated the nameservers\n        self.assertEqual([alt_dns_server], self.subnet[\'dns_nameservers\'],\n                         "Failed to update subnet\'s nameservers")\n\n        def check_new_dns_server():\n            # NOTE: Server needs to renew its dhcp lease in order to get new\n            # definitions from subnet\n            # NOTE(amuller): we are renewing the lease as part of the retry\n            # because Neutron updates dnsmasq asynchronously after the\n            # subnet-update API call returns.\n            ssh_client.renew_lease(fixed_ip=floating_ip[\'fixed_ip_address\'],\n                                   dhcp_client=CONF.scenario.dhcp_client)\n            if ssh_client.get_dns_servers() != [alt_dns_server]:\n                LOG.debug("Failed to update DNS nameservers")\n                return False\n            return True\n\n        self.assertTrue(test_utils.call_until_true(check_new_dns_server,\n                                                   renew_timeout,\n                                                   renew_delay),\n                        msg="DHCP renewal failed to fetch "\n                            "new DNS nameservers")\n\n    @decorators.idempotent_id(\'f5dfcc22-45fd-409f-954c-5bd500d7890b\')\n    @testtools.skipUnless(CONF.network_feature_enabled.port_admin_state_change,\n                          "Changing a port\'s admin state is not supported "\n                          "by the test environment")\n    @decorators.attr(type=\'slow\')\n    @utils.services(\'compute\', \'network\')\n    def test_update_instance_port_admin_state(self):\n        """Test to update admin_state_up attribute of instance port\n\n        1. Check public and project connectivity before updating\n                admin_state_up attribute of instance port to False\n        2. Check public and project connectivity after updating\n                admin_state_up attribute of instance port to False\n        3. Check public and project connectivity after updating\n                admin_state_up attribute of instance port to True\n        """\n        self._setup_network_and_servers()\n        _, server = self.floating_ip_tuple\n        server_id = server[\'id\']\n        port_id = self.os_admin.ports_client.list_ports(\n            device_id=server_id)[\'ports\'][0][\'id\']\n        server_pip = server[\'addresses\'][self.network[\'name\']][0][\'addr\']\n\n        server2 = self._create_server(self.network)\n        server2_fip = self.create_floating_ip(server2)\n\n        private_key = self._get_server_key(server2)\n        ssh_client = self.get_remote_client(server2_fip[\'floating_ip_address\'],\n                                            private_key=private_key,\n                                            server=server2)\n\n        self._check_public_network_connectivity(\n            should_connect=True, msg="before updating "\n            "admin_state_up of instance port to False")\n        self.check_remote_connectivity(ssh_client, dest=server_pip,\n                                       should_succeed=True)\n        self.ports_client.update_port(port_id, admin_state_up=False)\n        self._check_public_network_connectivity(\n            should_connect=False, msg="after updating "\n            "admin_state_up of instance port to False",\n            should_check_floating_ip_status=False)\n        self.check_remote_connectivity(ssh_client, dest=server_pip,\n                                       should_succeed=False)\n        self.ports_client.update_port(port_id, admin_state_up=True)\n        self._check_public_network_connectivity(\n            should_connect=True, msg="after updating "\n            "admin_state_up of instance port to True")\n        self.check_remote_connectivity(ssh_client, dest=server_pip,\n                                       should_succeed=True)\n\n    @decorators.idempotent_id(\'759462e1-8535-46b0-ab3a-33aa45c55aaa\')\n    @decorators.attr(type=\'slow\')\n    @utils.services(\'compute\', \'network\')\n    def test_preserve_preexisting_port(self):\n        """Test preserve pre-existing port\n\n        Tests that a pre-existing port provided on server boot is not deleted\n        if the server is deleted.\n\n        Nova should unbind the port from the instance on delete if the port was\n        not created by Nova as part of the boot request.\n\n        We should also be able to boot another server with the same port.\n        """\n        # Setup the network, create a port and boot the server from that port.\n        self._setup_network_and_servers(boot_with_port=True)\n        _, server = self.floating_ip_tuple\n        self.assertEqual(1, len(self.ports),\n                         \'There should only be one port created for \'\n                         \'server %s.\' % server[\'id\'])\n        port_id = self.ports[0][\'port\']\n        self.assertIsNotNone(port_id,\n                             \'Server should have been created from a \'\n                             \'pre-existing port.\')\n        # Assert the port is bound to the server.\n        port_list = self.os_admin.ports_client.list_ports(\n            device_id=server[\'id\'], network_id=self.network[\'id\'])[\'ports\']\n        self.assertEqual(1, len(port_list),\n                         \'There should only be one port created for \'\n                         \'server %s.\' % server[\'id\'])\n        self.assertEqual(port_id, port_list[0][\'id\'])\n        # Delete the server.\n        self.servers_client.delete_server(server[\'id\'])\n        waiters.wait_for_server_termination(self.servers_client, server[\'id\'])\n        # Assert the port still exists on the network but is unbound from\n        # the deleted server.\n        port = self.ports_client.show_port(port_id)[\'port\']\n        self.assertEqual(self.network[\'id\'], port[\'network_id\'])\n        self.assertEqual(\'\', port[\'device_id\'])\n        self.assertEqual(\'\', port[\'device_owner\'])\n\n        # Boot another server with the same port to make sure nothing was\n        # left around that could cause issues.\n        server = self._create_server(self.network, port[\'id\'])\n        port_list = self.os_admin.ports_client.list_ports(\n            device_id=server[\'id\'], network_id=self.network[\'id\'])[\'ports\']\n        self.assertEqual(1, len(port_list),\n                         \'There should only be one port created for \'\n                         \'server %s.\' % server[\'id\'])\n        self.assertEqual(port[\'id\'], port_list[0][\'id\'])\n\n    @utils.requires_ext(service=\'network\', extension=\'l3_agent_scheduler\')\n    @decorators.idempotent_id(\'2e788c46-fb3f-4ac9-8f82-0561555bea73\')\n    @decorators.attr(type=\'slow\')\n    @utils.services(\'compute\', \'network\')\n    def test_router_rescheduling(self):\n        """Tests that router can be removed from agent and add to a new agent.\n\n        1. Verify connectivity\n        2. Remove router from all l3-agents\n        3. Verify connectivity is down\n        4. Assign router to new l3-agent (or old one if no new agent is\n           available)\n        5. Verify connectivity\n        """\n\n        # TODO(yfried): refactor this test to be used for other agents (dhcp)\n        # as well\n\n        list_hosts = (self.os_admin.routers_client.\n                      list_l3_agents_hosting_router)\n        schedule_router = (self.os_admin.network_agents_client.\n                           create_router_on_l3_agent)\n        unschedule_router = (self.os_admin.network_agents_client.\n                             delete_router_from_l3_agent)\n\n        agent_list_alive = set(\n            a["id"] for a in\n            self.os_admin.network_agents_client.list_agents(\n                agent_type="L3 agent")[\'agents\'] if a["alive"] is True\n        )\n        self._setup_network_and_servers()\n\n        # NOTE(kevinbenton): we have to use the admin credentials to check\n        # for the distributed flag because self.router only has a project view.\n        admin = self.os_admin.routers_client.show_router(\n            self.router[\'id\'])\n        if admin[\'router\'].get(\'distributed\', False):\n            msg = "Rescheduling test does not apply to distributed routers."\n            raise self.skipException(msg)\n\n        self._check_public_network_connectivity(should_connect=True)\n\n        # remove resource from agents\n        hosting_agents = set(a["id"] for a in\n                             list_hosts(self.router[\'id\'])[\'agents\'])\n        no_migration = agent_list_alive == hosting_agents\n        LOG.info("Router will be assigned to {mig} hosting agent".\n                 format(mig="the same" if no_migration else "a new"))\n\n        for hosting_agent in hosting_agents:\n            unschedule_router(hosting_agent, self.router[\'id\'])\n            self.assertNotIn(hosting_agent,\n                             [a["id"] for a in\n                              list_hosts(self.router[\'id\'])[\'agents\']],\n                             \'unscheduling router failed\')\n\n        # verify resource is un-functional\n        self._check_public_network_connectivity(\n            should_connect=False,\n            msg=\'after router unscheduling\',\n        )\n\n        # schedule resource to new agent\n        target_agent = list(hosting_agents if no_migration else\n                            agent_list_alive - hosting_agents)[0]\n        schedule_router(target_agent,\n                        router_id=self.router[\'id\'])\n        self.assertEqual(\n            target_agent,\n            list_hosts(self.router[\'id\'])[\'agents\'][0][\'id\'],\n            "Router failed to reschedule. Hosting agent doesn\'t match "\n            "target agent")\n\n        # verify resource is functional\n        self._check_public_network_connectivity(\n            should_connect=True,\n            msg=\'After router rescheduling\')\n\n    @utils.requires_ext(service=\'network\', extension=\'port-security\')\n    @testtools.skipUnless(CONF.compute_feature_enabled.interface_attach,\n                          \'NIC hotplug not available\')\n    @decorators.idempotent_id(\'7c0bb1a2-d053-49a4-98f9-ca1a1d849f63\')\n    @decorators.attr(type=\'slow\')\n    @utils.services(\'compute\', \'network\')\n    def test_port_security_macspoofing_port(self):\n        """Tests port_security extension enforces mac spoofing\n\n        Neutron security groups always apply anti-spoof rules on the VMs. This\n        allows traffic to originate and terminate at the VM as expected, but\n        prevents traffic to pass through the VM. Anti-spoof rules are not\n        required in cases where the VM routes traffic through it.\n\n        The test steps are:\n\n        1. Create a new network.\n        2. Connect (hotplug) the VM to a new network.\n        3. Check the VM can ping a server on the new network ("peer")\n        4. Spoof the mac address of the new VM interface.\n        5. Check the Security Group enforces mac spoofing and blocks pings via\n           spoofed interface (VM cannot ping the peer).\n        6. Disable port-security of the spoofed port- set the flag to false.\n        7. Retest 3rd step and check that the Security Group allows pings via\n           the spoofed interface.\n        """\n\n        spoof_mac = "00:00:00:00:00:01"\n\n        # Create server\n        self._setup_network_and_servers()\n        self._check_public_network_connectivity(should_connect=True)\n        self._create_new_network()\n        self._hotplug_server()\n        fip, server = self.floating_ip_tuple\n        new_ports = self.os_admin.ports_client.list_ports(\n            device_id=server["id"], network_id=self.new_net["id"])[\'ports\']\n        spoof_port = new_ports[0]\n        private_key = self._get_server_key(server)\n        ssh_client = self.get_remote_client(fip[\'floating_ip_address\'],\n                                            private_key=private_key,\n                                            server=server)\n        spoof_nic = ssh_client.get_nic_name_by_mac(spoof_port["mac_address"])\n        peer = self._create_server(self.new_net)\n        peer_address = peer[\'addresses\'][self.new_net[\'name\']][0][\'addr\']\n        self.check_remote_connectivity(ssh_client, dest=peer_address,\n                                       nic=spoof_nic, should_succeed=True)\n        # Set a mac address by making nic down temporary\n        cmd = ("sudo ip link set {nic} down;"\n               "sudo ip link set dev {nic} address {mac};"\n               "sudo ip link set {nic} up").format(nic=spoof_nic,\n                                                   mac=spoof_mac)\n        ssh_client.exec_command(cmd)\n\n        new_mac = ssh_client.get_mac_address(nic=spoof_nic)\n        self.assertEqual(spoof_mac, new_mac)\n        self.check_remote_connectivity(ssh_client, dest=peer_address,\n                                       nic=spoof_nic, should_succeed=False)\n        self.ports_client.update_port(spoof_port["id"],\n                                      port_security_enabled=False,\n                                      security_groups=[])\n        self.check_remote_connectivity(ssh_client, dest=peer_address,\n                                       nic=spoof_nic, should_succeed=True)\n', 'message': 'test_update_instance_port_admin_state\n\nThis test validates instance public connectivity\nstatus by changing instance port admin_state_up\nto True & False.\n\nChange-Id: I3ada84cd6a65932869330a657a999266cfb67081\n', 'author_name': 'Thalabathy Venkatesan', 'lang': 'py'}