Merge "Replace neutron router creation with shade."
[yardstick.git] / yardstick / benchmark / scenarios / lib / check_connectivity.py
1 ##############################################################################
2 # Copyright (c) 2017 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
10 from __future__ import print_function
11 from __future__ import absolute_import
12
13 import logging
14 import yardstick.ssh as ssh
15
16 from yardstick.benchmark.scenarios import base
17
18 LOG = logging.getLogger(__name__)
19
20
21 class CheckConnectivity(base.Scenario):
22     """Check connectivity between two VMs"""
23
24     __scenario_type__ = "CheckConnectivity"
25
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']
30
31         try:
32             self.source_ip_addr = self.options['src_ip_addr']
33             self.dest_ip_addr = self.options['dest_ip_addr']
34         except KeyError:
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
44         else:
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)
50
51         self.connection = None
52         self.setup_done = False
53
54     def setup(self):
55         """scenario setup"""
56
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,
61                                       self.source_ip_addr,
62                                       password=self.ssh_passwd,
63                                       port=self.ssh_port)
64         else:
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,
68                                       self.source_ip_addr,
69                                       key_filename=self.ssh_key,
70                                       port=self.ssh_port)
71
72         self.connection.wait(timeout=self.ssh_timeout)
73         self.setup_done = True
74
75     def run(self, result):     # pragma: no cover
76         """execute the test"""
77
78         if not self.setup_done:
79             self.setup()
80
81         cmd = 'ping -c 4 ' + self.dest_ip_addr
82         parameter = self.options.get('ping_parameter', None)
83         if parameter:
84             cmd += (" %s" % parameter)
85
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)
89
90         conn_status = self.scenario_cfg['sla']['status']
91
92         if bool(status) != bool(conn_status):
93             LOG.info("%s ===> %s connectivity check passed!" % (self.source_ip_addr,
94                                                                 self.dest_ip_addr))
95             result['Check_Connectivity'] = 'PASS'
96         else:
97             LOG.info("%s ===> %s connectivity check failed!" % (self.source_ip_addr,
98                                                                 self.dest_ip_addr))
99             result['Check_Connectivity'] = 'FAIL'