ba93ba5b5cb4eca199a28461cfc68e05430fc114
[functest.git] / functest / opnfv_tests / openstack / shaker / shaker.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2018 Orange and others.
4 #
5 # All rights reserved. This program and the accompanying materials
6 # are made available under the terms of the Apache License, Version 2.0
7 # which accompanies this distribution, and is available at
8 # http://www.apache.org/licenses/LICENSE-2.0
9
10 """
11 Shaker_ wraps around popular system network testing tools like iperf, iperf3
12 and netperf (with help of flent). Shaker is able to deploy OpenStack instances
13 and networks in different topologies. Shaker scenario specifies the deployment
14 and list of tests to execute.
15
16 .. _Shaker: http://pyshaker.readthedocs.io/en/latest/
17 """
18
19 import logging
20 import os
21
22 import scp
23
24 from functest.core import singlevm
25
26
27 class Shaker(singlevm.SingleVm2):
28     """Run shaker full+perf l2 and l3"""
29     # pylint: disable=too-many-instance-attributes
30
31     __logger = logging.getLogger(__name__)
32
33     filename = '/home/opnfv/functest/images/shaker-image.qcow2'
34     flavor_ram = 512
35     flavor_vcpus = 1
36     flavor_disk = 3
37     username = 'ubuntu'
38     port = 9000
39
40     def prepare(self):
41         super(Shaker, self).prepare()
42         self.cloud.create_security_group_rule(
43             self.sec.id, port_range_min=self.port, port_range_max=self.port,
44             protocol='tcp', direction='ingress')
45
46     def execute(self):
47         """
48         Returns:
49             - 0 if success
50             - 1 on operation error
51         """
52         assert self.ssh
53         scpc = scp.SCPClient(self.ssh.get_transport())
54         scpc.put('/home/opnfv/functest/conf/env_file', '~/env_file')
55         (_, stdout, stderr) = self.ssh.exec_command(
56             'source ~/env_file && export OS_INTERFACE=public &&'
57             'shaker --server-endpoint {}:9000 --scenario '
58             'openstack/full_l2,openstack/full_l3_east_west,'
59             'openstack/full_l3_north_south,openstack/perf_l2,'
60             'openstack/perf_l3_east_west,openstack/perf_l3_north_south '
61             '--report report.html --output report.json'.format(
62                 self.sshvm.public_v4))
63         self.__logger.info("output:\n%s", stdout.read())
64         self.__logger.info("error:\n%s", stderr.read())
65         if not os.path.exists(self.res_dir):
66             os.makedirs(self.res_dir)
67         try:
68             scpc.get('report.json', self.res_dir)
69             scpc.get('report.html', self.res_dir)
70         except scp.SCPException:
71             self.__logger.exception("cannot get report files")
72             return 1
73         return stdout.channel.recv_exit_status()