Update of latest tests.
[csit.git] / resources / libraries / python / TrafficGenerator.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 from ssh import SSH
14 from robot.api import logger
15
16 __all__ = ['TrafficGenerator']
17
18 class TrafficGenerator(object):
19
20     def __init__(self):
21         self._result = None
22         self._loss = None
23         self._sent = None
24         self._received = None
25
26
27     def send_traffic_on(self, node, tx_port, rx_port, duration, rate,
28             framesize):
29         ssh = SSH()
30         ssh.connect(node)
31
32         (ret, stdout, stderr) = ssh.exec_command(
33                 "sh -c 'cd MoonGen && sudo -S build/MoonGen "
34                 "rfc2544/benchmarks/vpp-frameloss.lua --txport 0 --rxport 1 "
35                 "--duration {0} --rate {1} --framesize {2}'".format(
36                     duration, rate, framesize),
37                 timeout=int(duration)+60)
38
39         logger.trace(ret)
40         logger.trace(stdout)
41         logger.trace(stderr)
42
43         for line in stdout.splitlines():
44             pass
45
46         self._result = line
47         logger.info('TrafficGen result: {0}'.format(self._result))
48
49         self._loss = self._result.split(', ')[3].split('=')[1]
50
51         return self._result
52
53     def no_traffic_loss_occured(self):
54         if self._loss is None:
55             raise Exception('The traffic generation has not been issued')
56         if self._loss != '0':
57             raise Exception('Traffic loss occured: {0}'.format(self._loss))