1 ##############################################################################
2 # Copyright (c) 2017 Huawei Technologies Co.,Ltd and others.
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 ##############################################################################
10 from __future__ import print_function
11 from __future__ import absolute_import
14 import yardstick.ssh as ssh
16 from yardstick.benchmark.scenarios import base
18 LOG = logging.getLogger(__name__)
21 class CheckConnectivity(base.Scenario):
22 """Check connectivity between two VMs"""
24 __scenario_type__ = "CheckConnectivity"
26 def __init__(self, scenario_cfg, context_cfg):
27 self.scenario_cfg = scenario_cfg
28 self.context_cfg = context_cfg
29 self.options = self.scenario_cfg['options']
32 self.source_ip_addr = self.options['src_ip_addr']
33 self.dest_ip_addr = self.options['dest_ip_addr']
35 host = self.context_cfg['host']
36 target = self.context_cfg['target']
37 self.ssh_user = host.get('user', 'ubuntu')
38 self.ssh_port = host.get("ssh_port", 22)
39 self.source_ip_addr = host.get('ip', None)
40 self.dest_ip_addr = target.get('ipaddr', None)
41 self.ssh_key = host.get('key_filename', '/root/.ssh/id_rsa')
42 self.ssh_passwd = host.get('password', None)
43 self.ssh_timeout = 600
45 self.ssh_user = self.options.get("ssh_user", 'ubuntu')
46 self.ssh_port = self.options.get("ssh_port", 22)
47 self.ssh_key = self.options.get("ssh_key", '/root/.ssh/id_rsa')
48 self.ssh_passwd = self.options.get("ssh_passwd", None)
49 self.ssh_timeout = self.options.get("ssh_timeout", 600)
51 self.connection = None
52 self.setup_done = False
57 if self.ssh_passwd is not None:
58 LOG.info("Log in via pw, user:%s, host:%s, pw:%s",
59 self.ssh_user, self.source_ip_addr, self.ssh_passwd)
60 self.connection = ssh.SSH(self.ssh_user,
62 password=self.ssh_passwd,
65 LOG.info("Log in via key, user:%s, host:%s, key_filename:%s",
66 self.ssh_user, self.source_ip_addr, self.ssh_key)
67 self.connection = ssh.SSH(self.ssh_user,
69 key_filename=self.ssh_key,
72 self.connection.wait(timeout=self.ssh_timeout)
73 self.setup_done = True
75 def run(self, result): # pragma: no cover
76 """execute the test"""
78 if not self.setup_done:
81 cmd = 'ping -c 4 ' + self.dest_ip_addr
82 parameter = self.options.get('ping_parameter', None)
84 cmd += (" %s" % parameter)
86 LOG.info("Executing command: %s", cmd)
87 LOG.info("ping %s ==> %s", self.source_ip_addr, self.dest_ip_addr)
88 status, stdout, stderr = self.connection.execute(cmd)
90 conn_status = self.scenario_cfg['sla']['status']
92 if bool(status) != bool(conn_status):
93 LOG.info("%s ===> %s connectivity check passed!" % (self.source_ip_addr,
95 result['Check_Connectivity'] = 'PASS'
97 LOG.info("%s ===> %s connectivity check failed!" % (self.source_ip_addr,
99 result['Check_Connectivity'] = 'FAIL'