aboutsummaryrefslogtreecommitdiffstats
path: root/resources/libraries/python
diff options
context:
space:
mode:
authorZdeno Olsovsky <zolsovsk@cisco.com>2016-06-13 15:46:07 +0200
committerMatej Klotton <mklotton@cisco.com>2016-07-22 09:25:22 +0000
commit7c3e0cc41f55327d6eeb04fe757c6e80064ab28a (patch)
treece700ba4f130b8f153e3e72a6041f68ddb239f85 /resources/libraries/python
parentdec1188c7f89f5f8f97085b5f68c6f1d918586b8 (diff)
CSIT-158: Tap interface tests
Change-Id: I30a4562ea5fca9b839d854118243daa70378b0ae Signed-off-by: Zdeno Olsovsky <zolsovsk@cisco.com>
Diffstat (limited to 'resources/libraries/python')
-rw-r--r--resources/libraries/python/IPv4Util.py65
-rw-r--r--resources/libraries/python/InterfaceUtil.py20
-rw-r--r--resources/libraries/python/L2Util.py2
-rw-r--r--resources/libraries/python/Namespaces.py96
-rw-r--r--resources/libraries/python/Routing.py25
-rw-r--r--resources/libraries/python/Tap.py107
6 files changed, 310 insertions, 5 deletions
diff --git a/resources/libraries/python/IPv4Util.py b/resources/libraries/python/IPv4Util.py
index 4fe711fbff..3043f230c2 100644
--- a/resources/libraries/python/IPv4Util.py
+++ b/resources/libraries/python/IPv4Util.py
@@ -164,26 +164,83 @@ class IPv4Util(object):
@staticmethod
def send_ping_from_node_to_dst(node, destination, namespace=None,
- ping_count=3):
+ ping_count=3, interface=None):
"""Send a ping from node to destination. Optionally, you can define a
- namespace from where to send a ping.
+ namespace and interface from where to send a ping.
:param node: Node to start ping on.
:param destination: IPv4 address where to send ping.
- :param namespace: Namespace to send ping from.
- :param ping_count: Number of pings to send.
+ :param namespace: Namespace to send ping from. Optional
+ :param ping_count: Number of pings to send. Default 3
+ :param interface: Interface from where to send ping. Optional
:type node: dict
:type destination: str
:type namespace: str
:type ping_count: int
+ :type interface: str
:raises RuntimeError: If no response for ping, raise error
"""
cmd = ''
if namespace is not None:
cmd = 'ip netns exec {0} ping -c{1} {2}'.format(
namespace, ping_count, destination)
+ elif interface is not None:
+ cmd = 'ping -I {0} -c{1} {2}'.format(
+ interface, ping_count, destination)
else:
cmd = 'ping -c{0} {1}'.format(ping_count, destination)
rc, stdout, stderr = exec_cmd(node, cmd, sudo=True)
if rc != 0:
raise RuntimeError("Ping Not Successful")
+
+ @staticmethod
+ def set_linux_interface_arp(node, interface, ip, mac, namespace=None):
+ """Set arp on interface in linux.
+
+ :param node: Node where to execute command.
+ :param interface: Interface in namespace.
+ :param ip: IP for arp.
+ :param mac: MAC address.
+ :param namespace: Execute command in namespace. Optional
+ :type node: dict
+ :type interface: str
+ :type ip: str
+ :type mac: str
+ :type namespace: str
+ :raises RuntimeError: Could not set ARP properly.
+ """
+ if namespace is not None:
+ cmd = 'ip netns exec {} arp -i {} -s {} {}'.format(
+ namespace, interface, ip, mac)
+ else:
+ cmd = 'arp -i {} -s {} {}'.format(interface, ip, mac)
+ rc, _, stderr = exec_cmd(node, cmd, sudo=True)
+ if rc != 0:
+ raise RuntimeError("Arp set not successful, reason:{}".
+ format(stderr))
+
+ @staticmethod
+ def set_linux_interface_ip(node, interface, ip, prefix, namespace=None):
+ """Set IP address to interface in linux.
+
+ :param node: Node where to execute command.
+ :param interface: Interface in namespace.
+ :param ip: IP to be set on interface.
+ :param prefix: IP prefix.
+ :param namespace: Execute command in namespace. Optional
+ :type node: dict
+ :type interface: str
+ :type ip: str
+ :type prefix: int
+ :type namespace: str
+ :raises RuntimeError: IP could not be set.
+ """
+ if namespace is not None:
+ cmd = 'ip netns exec {} ip addr add {}/{} dev {}'.format(
+ namespace, ip, prefix, interface)
+ else:
+ cmd = 'ip addr add {}/{} dev {}'.format(ip, prefix, interface)
+ (rc, _, stderr) = exec_cmd(node, cmd, timeout=5, sudo=True)
+ if rc != 0:
+ raise RuntimeError(
+ 'Could not set IP for interface, reason:{}'.format(stderr))
diff --git a/resources/libraries/python/InterfaceUtil.py b/resources/libraries/python/InterfaceUtil.py
index 69d0a59680..2b985bf434 100644
--- a/resources/libraries/python/InterfaceUtil.py
+++ b/resources/libraries/python/InterfaceUtil.py
@@ -837,3 +837,23 @@ class InterfaceUtil(object):
vat.vat_terminal_exec_cmd_from_template("set_fib_to_interface.vat",
sw_index=sw_if_index,
vrf=table_id)
+
+ @staticmethod
+ def set_linux_interface_mac(node, interface, mac, namespace=None):
+ """Set MAC address for interface in linux.
+
+ :param node: Node where to execute command.
+ :param interface: Interface in namespace.
+ :param mac: MAC to be assigned to interface.
+ :param namespace: Execute command in namespace. Optional
+ :type node: dict
+ :type interface: str
+ :type mac: str
+ :type namespace: str
+ """
+ if namespace is not None:
+ cmd = 'ip netns exec {} ip link set {} address {}'.format(
+ namespace, interface, mac)
+ else:
+ cmd = 'ip link set {} address {}'.format(interface, mac)
+ exec_cmd_no_error(node, cmd, sudo=True)
diff --git a/resources/libraries/python/L2Util.py b/resources/libraries/python/L2Util.py
index af4735fdf8..4832ffac1c 100644
--- a/resources/libraries/python/L2Util.py
+++ b/resources/libraries/python/L2Util.py
@@ -211,6 +211,8 @@ class L2Util(object):
exec_cmd_no_error(node, cmd, sudo=True)
cmd = 'brctl addif {0} {1}'.format(br_name, if_2)
exec_cmd_no_error(node, cmd, sudo=True)
+ cmd = 'ip link set dev {0} up'.format(br_name)
+ exec_cmd_no_error(node, cmd, sudo=True)
@staticmethod
def setup_network_namespace(node, namespace_name, interface_name,
diff --git a/resources/libraries/python/Namespaces.py b/resources/libraries/python/Namespaces.py
new file mode 100644
index 0000000000..d92dfd7e26
--- /dev/null
+++ b/resources/libraries/python/Namespaces.py
@@ -0,0 +1,96 @@
+# Copyright (c) 2016 Cisco and/or its affiliates.
+# 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.
+
+"""Linux namespace utilities library."""
+
+from resources.libraries.python.ssh import exec_cmd_no_error, exec_cmd, SSH
+
+
+class Namespaces(object):
+ """Linux namespace utilities."""
+ def __init__(self):
+ self._namespaces = []
+
+ def create_namespace(self, node, namespace_name):
+ """Create namespace and add the name to the list for later clean-up.
+
+ :param node: Where to create namespace.
+ :param namespace_name: Name for namespace.
+ :type node: dict
+ :type namespace_name: str
+ """
+ cmd = ('ip netns add {0}'.format(namespace_name))
+ exec_cmd_no_error(node, cmd, sudo=True)
+ self._namespaces.append(namespace_name)
+
+ @staticmethod
+ def attach_interface_to_namespace(node, namespace, interface):
+ """Attach specific interface to namespace.
+
+ :param node: Node where to execute command.
+ :param namespace: Namespace to execute command on.
+ :param interface: Interface in namespace.
+ :type node: dict
+ :type namespace: str
+ :type interface: str
+ :raises RuntimeError: Interface could not be attached.
+ """
+ cmd = 'ip link set {0} netns {1}'.format(interface, namespace)
+ (rc, _, stderr) = exec_cmd(node, cmd, timeout=5, sudo=True)
+ if rc != 0:
+ raise RuntimeError(
+ 'Could not attach interface, reason:{}'.format(stderr))
+ cmd = 'ip netns exec {} ip link set {} up'.format(
+ namespace, interface)
+ (rc, _, stderr) = exec_cmd(node, cmd, timeout=5, sudo=True)
+ if rc != 0:
+ raise RuntimeError(
+ 'Could not set interface state, reason:{}'.format(stderr))
+
+ @staticmethod
+ def create_bridge_for_int_in_namespace(
+ node, namespace, bridge_name, *interfaces):
+ """Setup bridge domain and add interfaces to it.
+
+ :param node: Node where to execute command.
+ :param namespace: Namespace to execute command on.
+ :param bridge_name: Name of the bridge to be created.
+ :param interfaces: List of interfaces to add to the namespace.
+ :type node: dict
+ :type namespace: str
+ :type bridge_name: str
+ :type interfaces: list
+ """
+ cmd = 'ip netns exec {} brctl addbr {}'.format(namespace, bridge_name)
+ exec_cmd_no_error(node, cmd, sudo=True)
+ for interface in interfaces:
+ cmd = 'ip netns exec {} brctl addif {} {}'.format(
+ namespace, bridge_name, interface)
+ exec_cmd_no_error(node, cmd, sudo=True)
+ cmd = 'ip netns exec {} ip link set dev {} up'.format(
+ namespace, bridge_name)
+ exec_cmd_no_error(node, cmd, sudo=True)
+
+ def clean_up_namespaces(self, node):
+ """Remove all old namespaces.
+
+ :param node: Node where to execute command.
+ :type node: dict
+ :raises RuntimeError: Namespaces could not be cleaned properly.
+ """
+ for namespace in self._namespaces:
+ print "Cleaning namespace {}".format(namespace)
+ cmd = 'ip netns delete {}'.format(namespace)
+ (rc, stdout, stderr) = exec_cmd(node, cmd, timeout=5, sudo=True)
+ if rc != 0:
+ raise RuntimeError('Could not delete namespace')
diff --git a/resources/libraries/python/Routing.py b/resources/libraries/python/Routing.py
index 199b6de8d5..767fb3aff0 100644
--- a/resources/libraries/python/Routing.py
+++ b/resources/libraries/python/Routing.py
@@ -15,9 +15,10 @@
from resources.libraries.python.VatExecutor import VatTerminal
from resources.libraries.python.topology import Topology
-
+from resources.libraries.python.ssh import exec_cmd_no_error
class Routing(object):
+
"""Routing utilities."""
@staticmethod
@@ -93,3 +94,25 @@ class Routing(object):
prefix_length=prefix_len,
fib_number=fib_id,
where=place)
+
+ @staticmethod
+ def add_route(node, ip, prefix, gw, namespace=None):
+ """Add route in namespace.
+
+ :param node: Node where to execute command.
+ :param ip: Route destination IP.
+ :param prefix: IP prefix.
+ :param namespace: Execute command in namespace. Optional
+ :param gw: Gateway.
+ :type node: dict
+ :type ip: str
+ :type prefix: int
+ :type gw: str
+ :type namespace: str
+ """
+ if namespace is not None:
+ cmd = 'ip netns exec {} ip route add {}/{} via {}'.format(
+ namespace, ip, prefix, gw)
+ else:
+ cmd = 'ip route add {}/{} via {}'.format(ip, prefix, gw)
+ exec_cmd_no_error(node, cmd, sudo=True)
diff --git a/resources/libraries/python/Tap.py b/resources/libraries/python/Tap.py
new file mode 100644
index 0000000000..6c346860de
--- /dev/null
+++ b/resources/libraries/python/Tap.py
@@ -0,0 +1,107 @@
+# Copyright (c) 2016 Cisco and/or its affiliates.
+# 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.
+
+"""Tap utilities library."""
+
+from resources.libraries.python.VatExecutor import VatTerminal
+from resources.libraries.python.InterfaceUtil import InterfaceUtil
+
+
+class Tap(object):
+ """Tap utilities."""
+
+ @staticmethod
+ def add_tap_interface(node, tap_name, mac=None):
+ """Add tap interface with name and optionally with MAC.
+
+ :param node: Node to add tap on.
+ :param tap_name: Tap interface name for linux tap.
+ :param mac: Optional MAC address for VPP tap.
+ :type node: dict
+ :type tap_name: str
+ :type mac: str
+ :return: Returns a interface index.
+ :rtype: int
+ """
+ command = 'connect'
+ if mac is not None:
+ args = 'tapname {} mac {}'.format(tap_name, mac)
+ else:
+ args = 'tapname {}'.format(tap_name)
+ with VatTerminal(node) as vat:
+ resp = vat.vat_terminal_exec_cmd_from_template('tap.vat',
+ tap_command=command,
+ tap_arguments=args)
+ return resp[0]['sw_if_index']
+
+ @staticmethod
+ def modify_tap_interface(node, if_index, tap_name, mac=None):
+ """Modify tap interface like linux interface name or VPP MAC.
+
+ :param node: Node to modify tap on.
+ :param if_index: Index of tap interface to be modified.
+ :param tap_name: Tap interface name for linux tap.
+ :param mac: Optional MAC address for VPP tap.
+ :type node: dict
+ :type if_index: int
+ :type tap_name: str
+ :type mac: str
+ :return: Returns a interface index.
+ :rtype: int
+ """
+ command = 'modify'
+ if mac is not None:
+ args = 'sw_if_index {} tapname {} mac {}'.format(
+ if_index, tap_name, mac)
+ else:
+ args = 'sw_if_index {} tapname {}'.format(if_index, tap_name)
+ with VatTerminal(node) as vat:
+ resp = vat.vat_terminal_exec_cmd_from_template('tap.vat',
+ tap_command=command,
+ tap_arguments=args)
+ return resp[0]['sw_if_index']
+
+ @staticmethod
+ def delete_tap_interface(node, if_index):
+ """Delete tap interface.
+
+ :param node: Node to delete tap on.
+ :param if_index: Index of tap interface to be deleted.
+ :type node: dict
+ :type if_index: int
+ :raises RuntimeError: Deletion was not successful.
+ """
+ command = 'delete'
+ args = 'sw_if_index {}'.format(if_index)
+ with VatTerminal(node) as vat:
+ resp = vat.vat_terminal_exec_cmd_from_template('tap.vat',
+ tap_command=command,
+ tap_arguments=args)
+ if int(resp[0]['retval']) != 0:
+ raise RuntimeError(
+ 'Could not remove tap interface: {}'.format(resp))
+
+ @staticmethod
+ def check_tap_present(node, tap_name):
+ """Check whether specific tap interface exists.
+
+ :param node: Node to check tap on.
+ :param tap_name: Tap interface name for linux tap.
+ :type node: dict
+ :type tap_name: str
+ :raises RuntimeError: Specified interface was not found.
+ """
+ tap_if = InterfaceUtil.tap_dump(node, tap_name)
+ if len(tap_if) == 0:
+ raise RuntimeError(
+ 'Tap interface :{} does not exist'.format(tap_name))