standardize ssh auth
[yardstick.git] / yardstick / benchmark / scenarios / networking / networkcapacity.py
1 ##############################################################################
2 # Copyright (c) 2016 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 from __future__ import absolute_import
10
11 import logging
12
13 import pkg_resources
14 from oslo_serialization import jsonutils
15
16 import yardstick.ssh as ssh
17 from yardstick.benchmark.scenarios import base
18
19 LOG = logging.getLogger(__name__)
20
21
22 class NetworkCapacity(base.Scenario):
23     """Measure Network capacity and scale.
24
25     This scenario reads network status including number of connections,
26     number of frames sent/received.
27     """
28     __scenario_type__ = "NetworkCapacity"
29     TARGET_SCRIPT = "networkcapacity.bash"
30
31     def __init__(self, scenario_cfg, context_cfg):
32         self.scenario_cfg = scenario_cfg
33         self.context_cfg = context_cfg
34         self.setup_done = False
35
36     def setup(self):
37         """scenario setup"""
38         self.target_script = pkg_resources.resource_filename(
39             "yardstick.benchmark.scenarios.networking",
40             NetworkCapacity.TARGET_SCRIPT)
41
42         host = self.context_cfg['host']
43         if host is None:
44             raise RuntimeError('No right node.please check the configuration')
45
46         self.client = ssh.SSH.from_node(host, defaults={"user": "ubuntu"})
47         self.client.wait(timeout=600)
48
49         # copy script to host
50         self.client._put_file_shell(self.target_script, '~/networkcapacity.sh')
51
52         self.setup_done = True
53
54     def run(self, result):
55         """execute the benchmark"""
56
57         if not self.setup_done:
58             self.setup()
59
60         cmd = "sudo bash networkcapacity.sh"
61
62         LOG.debug("Executing command: %s", cmd)
63         status, stdout, stderr = self.client.execute(cmd)
64         if status:
65             raise RuntimeError(stderr)
66
67         result.update(jsonutils.loads(stdout))