Add with-statment support to VatTerminal. 61/461/11
authorMatej Klotton <mklotton@cisco.com>
Wed, 2 Mar 2016 15:51:30 +0000 (16:51 +0100)
committerStefan Kobza <skobza@cisco.com>
Mon, 7 Mar 2016 15:17:01 +0000 (16:17 +0100)
Change-Id: I7b4b32ce07b9247ccf80bf6b5d3339d00bc0999f
Signed-off-by: Matej Klotton <mklotton@cisco.com>
bootstrap-multilink.sh [new file with mode: 0755]
bootstrap.sh
resources/libraries/python/IPv6Setup.py
resources/libraries/python/InterfaceUtil.py
resources/libraries/python/L2Util.py
resources/libraries/python/Routing.py
resources/libraries/python/VatExecutor.py
resources/libraries/python/VppCounters.py

diff --git a/bootstrap-multilink.sh b/bootstrap-multilink.sh
new file mode 100755 (executable)
index 0000000..950b29c
--- /dev/null
@@ -0,0 +1,32 @@
+#!/bin/bash
+# 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.
+
+
+
+sudo apt-get -y install python-virtualenv
+
+virtualenv env
+. env/bin/activate
+pip install -r requirements.txt
+
+cat > mock.robot <<EOF
+*** test cases ***
+| Temoporary placeholder test for multilink
+| | log | nothing here to see
+EOF
+
+pybot mock.robot
+
+
+exit 0
index 4911c4d..893a8db 100755 (executable)
@@ -1,4 +1,16 @@
 #!/bin/bash
 #!/bin/bash
+# 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.
 
 set -x
 
 
 set -x
 
@@ -130,7 +142,7 @@ if [ "$?" -ne "0" ]; then
     exit ${retval}
 fi
 
     exit ${retval}
 fi
 
-if [ "x${VIRL_SID}" == "x" ]; then
+if [[ ! "${VIRL_SID}" =~ session-[a-zA-Z0-9]{6} ]]; then
     echo "No VIRL session ID reported."
     exit 127
 fi
     echo "No VIRL session ID reported."
     exit 127
 fi
index 86c8876..e04668f 100644 (file)
@@ -161,15 +161,14 @@ class IPv6Setup(object):
            :type prefix: str
         """
         sw_if_index = Topology.get_interface_sw_index(node, interface)
            :type prefix: str
         """
         sw_if_index = Topology.get_interface_sw_index(node, interface)
-        vat = VatTerminal(node)
-        vat.vat_terminal_exec_cmd_from_template('add_ip_address.vat',
-                                                sw_if_index=sw_if_index,
-                                                address=addr,
-                                                prefix_length=prefix)
-        vat.vat_terminal_exec_cmd_from_template('set_if_state.vat',
-                                                sw_if_index=sw_if_index,
-                                                state='admin-up')
-        vat.vat_terminal_close()
+        with VatTerminal(node) as vat:
+            vat.vat_terminal_exec_cmd_from_template('add_ip_address.vat',
+                                                    sw_if_index=sw_if_index,
+                                                    address=addr,
+                                                    prefix_length=prefix)
+            vat.vat_terminal_exec_cmd_from_template('set_if_state.vat',
+                                                    sw_if_index=sw_if_index,
+                                                    state='admin-up')
 
         ssh = SSH()
         ssh.connect(node)
 
         ssh = SSH()
         ssh.connect(node)
@@ -194,15 +193,14 @@ class IPv6Setup(object):
            :type prefix: str
         """
         sw_if_index = Topology.get_interface_sw_index(node, interface)
            :type prefix: str
         """
         sw_if_index = Topology.get_interface_sw_index(node, interface)
-        vat = VatTerminal(node)
-        vat.vat_terminal_exec_cmd_from_template('del_ip_address.vat',
-                                                sw_if_index=sw_if_index,
-                                                address=addr,
-                                                prefix_length=prefix)
-        vat.vat_terminal_exec_cmd_from_template('set_if_state.vat',
-                                                sw_if_index=sw_if_index,
-                                                state='admin-down')
-        vat.vat_terminal_close()
+        with VatTerminal(node) as vat:
+            vat.vat_terminal_exec_cmd_from_template('del_ip_address.vat',
+                                                    sw_if_index=sw_if_index,
+                                                    address=addr,
+                                                    prefix_length=prefix)
+            vat.vat_terminal_exec_cmd_from_template('set_if_state.vat',
+                                                    sw_if_index=sw_if_index,
+                                                    state='admin-down')
 
     @staticmethod
     def vpp_ra_supress_link_layer(node, interface):
 
     @staticmethod
     def vpp_ra_supress_link_layer(node, interface):
index c074a88..14473ee 100644 (file)
@@ -66,30 +66,29 @@ class InterfaceUtil(object):
         :raises: RuntimeError if the timeout period value has elapsed.
         """
         if_ready = False
         :raises: RuntimeError if the timeout period value has elapsed.
         """
         if_ready = False
-        vat = VatTerminal(node)
-        not_ready = []
-        start = time()
-        while if_ready != True:
-            out = vat.vat_terminal_exec_cmd('sw_interface_dump')
-            if time() - start > timeout:
+        with VatTerminal(node) as vat:
+            not_ready = []
+            start = time()
+            while not if_ready:
+                out = vat.vat_terminal_exec_cmd('sw_interface_dump')
+                if time() - start > timeout:
+                    for interface in out:
+                        if interface.get('admin_up_down') == 1:
+                            if interface.get('link_up_down') != 1:
+                                logger.debug('{0} link-down'.format(
+                                    interface.get('interface_name')))
+                    raise RuntimeError('timeout, not up {0}'.format(not_ready))
+                not_ready = []
                 for interface in out:
                     if interface.get('admin_up_down') == 1:
                         if interface.get('link_up_down') != 1:
                 for interface in out:
                     if interface.get('admin_up_down') == 1:
                         if interface.get('link_up_down') != 1:
-                            logger.debug('{0} link-down'.format(
-                                interface.get('interface_name')))
-                raise RuntimeError('timeout, not up {0}'.format(not_ready))
-            not_ready = []
-            for interface in out:
-                if interface.get('admin_up_down') == 1:
-                    if interface.get('link_up_down') != 1:
-                        not_ready.append(interface.get('interface_name'))
-            if not not_ready:
-                if_ready = True
-            else:
-                logger.debug('Interfaces still in link-down state: {0}, ' \
-                             'waiting...'.format(not_ready))
-                sleep(1)
-        vat.vat_terminal_close()
+                            not_ready.append(interface.get('interface_name'))
+                if not not_ready:
+                    if_ready = True
+                else:
+                    logger.debug('Interfaces still in link-down state: {0}, '
+                                 'waiting...'.format(not_ready))
+                    sleep(1)
 
     @staticmethod
     def vpp_nodes_interfaces_ready_wait(nodes, timeout=10):
 
     @staticmethod
     def vpp_nodes_interfaces_ready_wait(nodes, timeout=10):
@@ -98,7 +97,7 @@ class InterfaceUtil(object):
 
         :param nodes: List of nodes to wait on.
         :param timeout: Seconds to wait per node for all interfaces to come up.
 
         :param nodes: List of nodes to wait on.
         :param timeout: Seconds to wait per node for all interfaces to come up.
-        :type node: list
+        :type nodes: list
         :type timeout: int
         :raises: RuntimeError if the timeout period value has elapsed.
         """
         :type timeout: int
         :raises: RuntimeError if the timeout period value has elapsed.
         """
@@ -112,7 +111,7 @@ class InterfaceUtil(object):
 
         :param nodes: Nodes in the topology.
         :param timeout: Seconds to wait per node for all interfaces to come up.
 
         :param nodes: Nodes in the topology.
         :param timeout: Seconds to wait per node for all interfaces to come up.
-        :type node: dict
+        :type nodes: dict
         :type timeout: int
         :raises: RuntimeError if the timeout period value has elapsed.
         """
         :type timeout: int
         :raises: RuntimeError if the timeout period value has elapsed.
         """
index a891308..c0a764f 100644 (file)
@@ -167,11 +167,10 @@ class L2Util(object):
         """
         sw_iface1 = Topology().get_interface_sw_index(node, interface1)
         sw_iface2 = Topology().get_interface_sw_index(node, interface2)
         """
         sw_iface1 = Topology().get_interface_sw_index(node, interface1)
         sw_iface2 = Topology().get_interface_sw_index(node, interface2)
-        vat = VatTerminal(node)
-        vat.vat_terminal_exec_cmd_from_template('l2_xconnect.vat',
-                                                interface1=sw_iface1,
-                                                interface2=sw_iface2)
-        vat.vat_terminal_exec_cmd_from_template('l2_xconnect.vat',
-                                                interface1=sw_iface2,
-                                                interface2=sw_iface1)
-        vat.vat_terminal_close()
+        with VatTerminal(node) as vat:
+            vat.vat_terminal_exec_cmd_from_template('l2_xconnect.vat',
+                                                    interface1=sw_iface1,
+                                                    interface2=sw_iface2)
+            vat.vat_terminal_exec_cmd_from_template('l2_xconnect.vat',
+                                                    interface1=sw_iface2,
+                                                    interface2=sw_iface1)
index b22516a..7795e3f 100644 (file)
@@ -29,17 +29,16 @@ class Routing(object):
         :param prefix_len: Route destination network prefix length.
         :param gateway: Route gateway address.
         :param interface: Route interface.
         :param prefix_len: Route destination network prefix length.
         :param gateway: Route gateway address.
         :param interface: Route interface.
-        :type node: str
+        :type node: dict
         :type network: str
         :type prefix_len: int
         :type gateway: str
         :type interface: str
         """
         sw_if_index = Topology.get_interface_sw_index(node, interface)
         :type network: str
         :type prefix_len: int
         :type gateway: str
         :type interface: str
         """
         sw_if_index = Topology.get_interface_sw_index(node, interface)
-        vat = VatTerminal(node)
-        vat.vat_terminal_exec_cmd_from_template('add_route.vat',
-                                                network=network,
-                                                prefix_length=prefix_len,
-                                                gateway=gateway,
-                                                sw_if_index=sw_if_index)
-        vat.vat_terminal_close()
+        with VatTerminal(node) as vat:
+            vat.vat_terminal_exec_cmd_from_template('add_route.vat',
+                                                    network=network,
+                                                    prefix_length=prefix_len,
+                                                    gateway=gateway,
+                                                    sw_if_index=sw_if_index)
index d53e57c..136b7cc 100644 (file)
@@ -34,7 +34,6 @@ def cleanup_vat_json_output(json_output):
 
 
 class VatExecutor(object):
 
 
 class VatExecutor(object):
-
     def __init__(self):
         self._stdout = None
         self._stderr = None
     def __init__(self):
         self._stdout = None
         self._stderr = None
@@ -77,11 +76,12 @@ class VatExecutor(object):
         # TODO: download vpp_api_test output file
         # self._delete_files(node, remote_file_path, remote_file_out)
 
         # TODO: download vpp_api_test output file
         # self._delete_files(node, remote_file_path, remote_file_out)
 
-    def execute_script_json_out(self, vat_name, node, timeout=10,):
+    def execute_script_json_out(self, vat_name, node, timeout=10):
         self.execute_script(vat_name, node, timeout, json_out=True)
         self._stdout = cleanup_vat_json_output(self._stdout)
 
         self.execute_script(vat_name, node, timeout, json_out=True)
         self._stdout = cleanup_vat_json_output(self._stdout)
 
-    def _delete_files(self, node, *files):
+    @staticmethod
+    def _delete_files(node, *files):
         ssh = SSH()
         ssh.connect(node)
         files = " ".join([str(x) for x in files])
         ssh = SSH()
         ssh.connect(node)
         files = " ".join([str(x) for x in files])
@@ -111,16 +111,14 @@ class VatExecutor(object):
     def cmd_from_template(node, vat_template_file, **vat_args):
         """Execute VAT script on specified node. This method supports
          script templates with parameters
     def cmd_from_template(node, vat_template_file, **vat_args):
         """Execute VAT script on specified node. This method supports
          script templates with parameters
-        :param node: node in topology on witch the scrtipt is executed
+        :param node: node in topology on witch the script is executed
         :param vat_template_file: template file of VAT script
         :param vat_args: arguments to the template file
         :return: list of json objects returned by VAT
         """
         :param vat_template_file: template file of VAT script
         :param vat_args: arguments to the template file
         :return: list of json objects returned by VAT
         """
-        vat = VatTerminal(node)
-        ret = vat.vat_terminal_exec_cmd_from_template(vat_template_file,
-                                                      **vat_args)
-        vat.vat_terminal_close()
-        return ret
+        with VatTerminal(node) as vat:
+            return vat.vat_terminal_exec_cmd_from_template(vat_template_file,
+                                                           **vat_args)
 
     @staticmethod
     def copy_config_to_remote(node, local_path, remote_path):
 
     @staticmethod
     def copy_config_to_remote(node, local_path, remote_path):
@@ -158,6 +156,12 @@ class VatTerminal(object):
             'sudo -S {vat} json'.format(vat=Constants.VAT_BIN_NAME),
             self.__VAT_PROMPT)
 
             'sudo -S {vat} json'.format(vat=Constants.VAT_BIN_NAME),
             self.__VAT_PROMPT)
 
+    def __enter__(self):
+        return self
+
+    def __exit__(self, exc_type, exc_val, exc_tb):
+        self.vat_terminal_close()
+
     def vat_terminal_exec_cmd(self, cmd):
         """Execute command on the opened VAT terminal.
 
     def vat_terminal_exec_cmd(self, cmd):
         """Execute command on the opened VAT terminal.
 
@@ -165,11 +169,11 @@ class VatTerminal(object):
 
            :return: Command output in python representation of JSON format.
         """
 
            :return: Command output in python representation of JSON format.
         """
-        logger.debug("Executing command in VAT terminal: {}".format(cmd));
+        logger.debug("Executing command in VAT terminal: {}".format(cmd))
         out = self._ssh.interactive_terminal_exec_command(self._tty,
                                                           cmd,
                                                           self.__VAT_PROMPT)
         out = self._ssh.interactive_terminal_exec_command(self._tty,
                                                           cmd,
                                                           self.__VAT_PROMPT)
-        logger.debug("VAT output: {}".format(out));
+        logger.debug("VAT output: {}".format(out))
         json_out = json.loads(out)
         return json_out
 
         json_out = json.loads(out)
         return json_out
 
index b99d652..caae06c 100644 (file)
@@ -83,19 +83,16 @@ class VppCounters(object):
            :type node: dict
            :return: Stats table.
         """
            :type node: dict
            :return: Stats table.
         """
-        vat = VatTerminal(node)
-        vat.vat_terminal_exec_cmd('want_stats enable')
-        for _ in range(0, 12):
-            stats_table = vat.vat_terminal_exec_cmd('dump_stats_table')
-            if_counters = stats_table['interface_counters']
-            if len(if_counters) > 0:
-                self._stats_table = stats_table
-                vat.vat_terminal_close()
-                return stats_table
-            time.sleep(1)
-
-        vat.vat_terminal_close()
-        return None
+        with VatTerminal(node) as vat:
+            vat.vat_terminal_exec_cmd('want_stats enable')
+            for _ in range(0, 12):
+                stats_table = vat.vat_terminal_exec_cmd('dump_stats_table')
+                if_counters = stats_table['interface_counters']
+                if len(if_counters) > 0:
+                    self._stats_table = stats_table
+                    return stats_table
+                time.sleep(1)
+            return None
 
     def vpp_get_ipv4_interface_counter(self, node, interface):
         return self.vpp_get_ipv46_interface_counter(node, interface, False)
 
     def vpp_get_ipv4_interface_counter(self, node, interface):
         return self.vpp_get_ipv46_interface_counter(node, interface, False)