VIRL test: Replace IP probe for VXLAN test
[csit.git] / resources / libraries / python / Namespaces.py
1 # Copyright (c) 2016 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """Linux namespace utilities library."""
15
16 from resources.libraries.python.ssh import exec_cmd_no_error, exec_cmd
17
18
19 class Namespaces(object):
20     """Linux namespace utilities."""
21     def __init__(self):
22         self._namespaces = []
23
24     def create_namespace(self, node, namespace_name):
25         """Create namespace and add the name to the list for later clean-up.
26
27         :param node: Where to create namespace.
28         :param namespace_name: Name for namespace.
29         :type node: dict
30         :type namespace_name: str
31         """
32         cmd = ('ip netns add {0}'.format(namespace_name))
33         exec_cmd_no_error(node, cmd, sudo=True)
34         self._namespaces.append(namespace_name)
35
36     @staticmethod
37     def attach_interface_to_namespace(node, namespace, interface):
38         """Attach specific interface to namespace.
39
40         :param node: Node where to execute command.
41         :param namespace: Namespace to execute command on.
42         :param interface: Interface in namespace.
43         :type node: dict
44         :type namespace: str
45         :type interface: str
46         :raises RuntimeError: Interface could not be attached.
47         """
48         cmd = 'ip link set {0} netns {1}'.format(interface, namespace)
49         (ret_code, _, stderr) = exec_cmd(node, cmd, timeout=5, sudo=True)
50         if ret_code != 0:
51             raise RuntimeError(
52                 'Could not attach interface, reason:{}'.format(stderr))
53         cmd = 'ip netns exec {} ip link set {} up'.format(
54             namespace, interface)
55         (ret_code, _, stderr) = exec_cmd(node, cmd, timeout=5, sudo=True)
56         if ret_code != 0:
57             raise RuntimeError(
58                 'Could not set interface state, reason:{}'.format(stderr))
59
60     @staticmethod
61     def create_bridge_for_int_in_namespace(
62             node, namespace, bridge_name, *interfaces):
63         """Setup bridge domain and add interfaces to it.
64
65         :param node: Node where to execute command.
66         :param namespace: Namespace to execute command on.
67         :param bridge_name: Name of the bridge to be created.
68         :param interfaces: List of interfaces to add to the namespace.
69         :type node: dict
70         :type namespace: str
71         :type bridge_name: str
72         :type interfaces: list
73         """
74         cmd = 'ip netns exec {} brctl addbr {}'.format(namespace, bridge_name)
75         exec_cmd_no_error(node, cmd, sudo=True)
76         for interface in interfaces:
77             cmd = 'ip netns exec {} brctl addif {} {}'.format(
78                 namespace, bridge_name, interface)
79             exec_cmd_no_error(node, cmd, sudo=True)
80         cmd = 'ip netns exec {} ip link set dev {} up'.format(
81             namespace, bridge_name)
82         exec_cmd_no_error(node, cmd, sudo=True)
83
84     def clean_up_namespaces(self, node):
85         """Remove all old namespaces.
86
87         :param node: Node where to execute command.
88         :type node: dict
89         :raises RuntimeError: Namespaces could not be cleaned properly.
90         """
91         for namespace in self._namespaces:
92             print "Cleaning namespace {}".format(namespace)
93             cmd = 'ip netns delete {}'.format(namespace)
94             (ret_code, _, _) = exec_cmd(node, cmd, timeout=5, sudo=True)
95             if ret_code != 0:
96                 raise RuntimeError('Could not delete namespace')