8fd712f53888c2d4a7db3fb3c042e9c3b03ebc45
[vpp.git] / test / resources / libraries / python / SetupFramework.py
1 # Copyright (c) 2015 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 import shlex
14 from ssh import SSH
15 from subprocess import Popen, PIPE, call
16 from tempfile import NamedTemporaryFile
17 from os.path import basename
18 from constants import Constants as con
19 from robot.api import logger
20
21 __all__ = ["SetupFramework"]
22
23 class SetupFramework(object):
24     """Setup suite run on topology nodes.
25     
26     Many VAT/CLI based tests need the scripts at remote hosts before executing
27     them. This class packs the whole testing directory and copies it over
28     to all nodes in topology under /tmp/
29     
30     """
31
32     def __init__(self):
33         pass
34
35     def __pack_framework_dir(self):
36         """Pack the testing WS into temp file, return its name."""
37
38         tmpfile = NamedTemporaryFile(suffix=".tgz", prefix="openvpp-testing-")
39         file_name = tmpfile.name
40         tmpfile.close()
41
42         proc = Popen(shlex.split("tar -zcf {0} .".format(file_name)),
43                 stdout=PIPE, stderr=PIPE)
44         (stdout, stderr) = proc.communicate()
45
46         logger.debug(stdout)
47         logger.debug(stderr)
48
49         return_code = proc.wait()
50         if 0 != return_code:
51             raise Exception("Could not pack testing framework.")
52
53         return file_name
54
55     def __copy_tarball_to_node(self, tarball, node):
56         logger.console('Copying tarball to {0}'.format(node['host']))
57         ssh = SSH()
58         ssh.connect(node)
59
60         ssh.scp(tarball, "/tmp/")
61
62     def __extract_tarball_at_node(self, tarball, node):
63         logger.console('Extracting tarball to {0} on {1}'.format(
64             con.REMOTE_FW_DIR, node['host'])) 
65         ssh = SSH()
66         ssh.connect(node)
67
68         cmd = 'rm -rf {1}; mkdir {1} ; sudo -Sn tar -zxf {0} -C {1};'.format(
69                 tarball, con.REMOTE_FW_DIR)
70         (ret_code, stdout, stderr) = ssh.exec_command(cmd, timeout=30)
71         if 0 != ret_code:
72             logger.error('Unpack error: {0}'.format(stderr))
73             raise Exception('Failed to unpack {0} at node {1}'.format(
74                 tarball, node['host']))
75
76     def __delete_local_tarball(self, tarball):
77         call(shlex.split('sh -c "rm {0} > /dev/null 2>&1"'.format(tarball)))
78
79     def setup_framework(self, nodes):
80         """Pack the whole directory and extract in temp on each node."""
81
82         tarball = self.__pack_framework_dir()
83         logger.console('Framework packed to {0}'.format(tarball))
84         remote_tarball = "/tmp/{0}".format(basename(tarball))
85
86         for node in nodes.values():
87             self.__copy_tarball_to_node(tarball, node)
88             self.__extract_tarball_at_node(remote_tarball, node)
89
90         logger.trace('Test framework copied to all topology nodes')
91         self.__delete_local_tarball(tarball)
92