Merge "Change PTL informatin in INFO"
[bottlenecks.git] / testsuites / vstf / vstf_scripts / vstf / agent / env / basic / vm9pfs.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import os
11 import logging
12 import textwrap
13 from vstf.common.utils import my_sleep
14 from vstf.agent.env.fsmonitor import constant
15
16 LOG = logging.getLogger(__name__)
17
18
19 class VMConfigBy9pfs(object):
20     """
21     host side implemetation of a self-defined communication protocol using libvirt 9pfs to give commands to the Virtual Machine.
22
23     """
24
25     def __init__(self, vm_9p_path):
26         """
27         :param vm_9p_path: The host path of libvirt 9pfs for a vm.
28         :return:
29         """
30         self.vm_9p_path = vm_9p_path
31
32     def clean(self):
33         self._unlink(self._path(constant.VM_CMD_RETURN_CODE_FILE))
34         self._unlink(self._path(constant.VM_CMD_DONE_FLAG_FILE))
35
36     def _path(self, relative_path):
37         return os.path.join(self.vm_9p_path, relative_path)
38
39     def _unlink(self, file_path):
40         os.unlink(file_path)
41         LOG.info("os.unlink(%s)", file_path)
42
43     def _read(self, filename):
44         filepath = self._path(filename)
45         with open(filepath, 'r') as f:
46             ret = f.read()
47             LOG.info("read(%s) -> %s", filepath, ret)
48         return ret
49
50     def _write(self, filename, cmd):
51         filepath = self._path(filename)
52         with open(filepath, 'w') as f:
53             f.write("%s" % cmd)
54             LOG.info("write(%s) <- %s", filepath, cmd)
55
56     def _wait_flag_file_to_exist(self, filename, timeout):
57         filepath = self._path(filename)
58         while timeout > 0:
59             if os.path.exists(filepath):
60                 LOG.info("wait and find file:%s", filepath)
61                 return True
62             my_sleep(1)
63             timeout -= 1
64             LOG.info("waiting file to exist:%s", filepath)
65         return False
66
67     def _get_cmd_return_code(self):
68         ret = self._read(constant.VM_CMD_RETURN_CODE_FILE)
69         return ret == constant.VM_CMD_EXCUTE_SUCCES_FLAG_CONTENT
70
71     def _wait_command_done(self):
72         done = self._wait_flag_file_to_exist(
73             constant.VM_CMD_DONE_FLAG_FILE,
74             constant.VM_COMMON_CMD_EXCUTE_TIME_OUT)
75         if done:
76             return self._get_cmd_return_code()
77         else:
78             return 'timeout'
79
80     def _set_cmd(self, cmd):
81         self._write(constant.VM_CMD_CONTENT_FILE, cmd)
82         self._write(constant.VM_CMD_SET_FLAG_FILE, '')
83         ret = self._wait_command_done()
84         if ret:
85             self.clean()
86             return ret
87         else:
88             raise Exception("9pfs command failure: timeout.")
89
90     def wait_up(self):
91         return self._wait_flag_file_to_exist(
92             constant.VM_UP_Flag_FILE, constant.VM_UP_TIME_OUT)
93
94     def config_ip(self, mac, ip):
95         cmd = 'config_ip %s %s' % (mac, ip)
96         return self._set_cmd(cmd)
97
98     def config_gw(self, ip):
99         cmd = 'config_gw %s' % ip
100         return self._set_cmd(cmd)
101
102     def set_pktloop_dpdk(self, macs):
103         """
104         To connect two network devices together in the vm and loop the packets received to another.
105         Use dpdk testpmd to loop the packets. See FSMonitor.
106
107         :param macs: the mac address list of network cards of the vm.
108         :return: True for success, Exception for Failure.
109         """
110         mac_str = ' '.join(macs)
111         cmd = 'set_pktloop_dpdk ' + mac_str
112         return self._set_cmd(cmd)
113
114     def recover_nic_binding(self, macs):
115         """
116         in contrast to set_pktloop_dpdk, disconnect the looping.
117         :param macs:  the mac address list of network cards of the vm.
118         :return: True for success, Exception for Failure.
119         """
120         mac_str = ' '.join(macs)
121         cmd = 'recover_nic_binding ' + mac_str
122         return self._set_cmd(cmd)
123
124     def config_amqp(
125             self,
126             identity,
127             server,
128             port=5672,
129             user="guest",
130             passwd="guest"):
131         data = {
132             'server': server,
133             'port': port,
134             'id': identity,
135             'user': user,
136             'passwd': passwd
137         }
138         header = "[rabbit]"
139         content = '''
140         user=%(user)s
141         passwd=%(passwd)s
142         host=%(server)s
143         port=%(port)s
144         id=%(id)s''' % data
145         file_name = "amqp.ini"
146         dedented_text = textwrap.dedent(content)
147         self._write(file_name, header + dedented_text)
148         cmd = 'config_amqp %s' % file_name
149         return self._set_cmd(cmd)
150
151     def stop_vstf(self):
152         cmd = "stop_vstf"
153         return self._set_cmd(cmd)
154
155     def __repr__(self):
156         return self.__class__.__name__ + ':' + self.vm_9p_path
157
158
159 if __name__ == '__main__':
160     fs = VMConfigBy9pfs('/tmp/tmp4T6p7L')
161     print os.listdir(os.curdir)
162     print fs.config_ip('56:6f:44:a5:3f:a4', '192.168.188.200/23')
163     print fs.config_gw('192.168.188.1')
164     print fs.set_pktloop_dpdk(['56:6f:44:a5:3f:a2', '56:6f:44:a5:3f:a3'])
165     print fs.recover_nic_binding(['56:6f:44:a5:3f:a2', '56:6f:44:a5:3f:a3'])
166     print fs.config_amqp('192.168.188.200', '192.168.188.10')
167     print os.listdir(os.curdir)