Merge "standardize ssh auth"
[yardstick.git] / yardstick / benchmark / scenarios / networking / pktgen.py
index e6d6893..e6aa7e5 100644 (file)
@@ -6,15 +6,18 @@
 # which accompanies this distribution, and is available at
 # http://www.apache.org/licenses/LICENSE-2.0
 ##############################################################################
-import pkg_resources
+from __future__ import absolute_import
+from __future__ import print_function
+
 import logging
-import json
+
+import pkg_resources
+from oslo_serialization import jsonutils
 
 import yardstick.ssh as ssh
 from yardstick.benchmark.scenarios import base
 
 LOG = logging.getLogger(__name__)
-LOG.setLevel(logging.DEBUG)
 
 
 class Pktgen(base.Scenario):
@@ -38,31 +41,29 @@ class Pktgen(base.Scenario):
 
     TARGET_SCRIPT = 'pktgen_benchmark.bash'
 
-    def __init__(self, context):
-        self.context = context
+    def __init__(self, scenario_cfg, context_cfg):
+        self.scenario_cfg = scenario_cfg
+        self.context_cfg = context_cfg
         self.setup_done = False
 
     def setup(self):
-        '''scenario setup'''
+        """scenario setup"""
         self.target_script = pkg_resources.resource_filename(
             'yardstick.benchmark.scenarios.networking',
             Pktgen.TARGET_SCRIPT)
-        user = self.context.get('user', 'ubuntu')
-        host = self.context.get('host', None)
-        target = self.context.get('target', None)
-        key_filename = self.context.get('key_filename', '~/.ssh/id_rsa')
+        host = self.context_cfg['host']
+        target = self.context_cfg['target']
 
-        LOG.debug("user:%s, target:%s", user, target)
-        self.server = ssh.SSH(user, target, key_filename=key_filename)
+        LOG.info("user:%s, target:%s", target['user'], target['ip'])
+        self.server = ssh.SSH.from_node(target, defaults={"user": "ubuntu"})
         self.server.wait(timeout=600)
 
-        LOG.debug("user:%s, host:%s", user, host)
-        self.client = ssh.SSH(user, host, key_filename=key_filename)
+        LOG.info("user:%s, host:%s", host['user'], host['ip'])
+        self.client = ssh.SSH.from_node(host, defaults={"user": "ubuntu"})
         self.client.wait(timeout=600)
 
         # copy script to host
-        self.client.run("cat > ~/pktgen.sh",
-                        stdin=open(self.target_script, "rb"))
+        self.client._put_file_shell(self.target_script, '~/pktgen.sh')
 
         self.setup_done = True
 
@@ -87,19 +88,20 @@ class Pktgen(base.Scenario):
             raise RuntimeError(stderr)
         return int(stdout)
 
-    def run(self, args):
+    def run(self, result):
         """execute the benchmark"""
 
         if not self.setup_done:
             self.setup()
 
-        ipaddr = args.get("ipaddr", '127.0.0.1')
+        ipaddr = self.context_cfg["target"].get("ipaddr", '127.0.0.1')
 
-        options = args['options']
+        options = self.scenario_cfg['options']
         packetsize = options.get("packetsize", 60)
         self.number_of_ports = options.get("number_of_ports", 10)
         # if run by a duration runner
-        duration_time = self.context.get("duration", None)
+        duration_time = self.scenario_cfg["runner"].get("duration", None) \
+            if "runner" in self.scenario_cfg else None
         # if run by an arithmetic runner
         arithmetic_time = options.get("duration", None)
 
@@ -120,42 +122,49 @@ class Pktgen(base.Scenario):
         if status:
             raise RuntimeError(stderr)
 
-        data = json.loads(stdout)
+        result.update(jsonutils.loads(stdout))
 
-        data['packets_received'] = self._iptables_get_result()
+        result['packets_received'] = self._iptables_get_result()
+        result['packetsize'] = packetsize
 
-        if "sla" in args:
-            sent = data['packets_sent']
-            received = data['packets_received']
+        if "sla" in self.scenario_cfg:
+            sent = result['packets_sent']
+            received = result['packets_received']
             ppm = 1000000 * (sent - received) / sent
-            sla_max_ppm = int(args["sla"]["max_ppm"])
-            assert ppm <= sla_max_ppm, "ppm %d > sla_max_ppm %d" \
+            sla_max_ppm = int(self.scenario_cfg["sla"]["max_ppm"])
+            assert ppm <= sla_max_ppm, "ppm %d > sla_max_ppm %d" \
                 % (ppm, sla_max_ppm)
 
-        return data
-
 
 def _test():
-    '''internal test function'''
+    """internal test function"""
     key_filename = pkg_resources.resource_filename('yardstick.resources',
                                                    'files/yardstick_key')
-    ctx = {'host': '172.16.0.137',
-           'target': '172.16.0.138',
-           'user': 'ubuntu',
-           'key_filename': key_filename
-           }
+    ctx = {
+        'host': {
+            'ip': '10.229.47.137',
+            'user': 'root',
+            'key_filename': key_filename
+        },
+        'target': {
+            'ip': '10.229.47.137',
+            'user': 'root',
+            'key_filename': key_filename,
+            'ipaddr': '10.229.47.137',
+        }
+    }
 
     logger = logging.getLogger('yardstick')
     logger.setLevel(logging.DEBUG)
 
-    p = Pktgen(ctx)
-
     options = {'packetsize': 120}
+    args = {'options': options}
+    result = {}
+
+    p = Pktgen(args, ctx)
+    p.run(result)
+    print(result)
 
-    args = {'options': options,
-            'ipaddr': '192.168.111.31'}
-    result = p.run(args)
-    print result
 
 if __name__ == '__main__':
     _test()