Refactoring ssh commands for Sahara

Refactoring ssh commands for Sahara project
in accordance with a common code

Change-Id: I3c5e5729757f37c94bb3d5d5ef169b4f4868169a
Closes-Bug: #1324143
This commit is contained in:
Vadim Rovachev 2014-05-28 17:47:49 +04:00
parent 67b61ed378
commit a800d0527d
5 changed files with 33 additions and 82 deletions

View File

@ -1,48 +0,0 @@
# vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright 2013 Mirantis, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import logging
import paramiko
from fuel_health.exceptions import SSHExecCommandFailed
import fuel_health.config
LOG = logging.getLogger(__name__)
def ssh_command(cmd):
config = fuel_health.config.FuelConfig()
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
LOG.debug('Remote ssh commad is "%s"', cmd)
try:
ssh.connect(hostname=config.compute.controller_nodes[0],
username=config.compute.controller_node_ssh_user,
key_filename=config.compute.path_to_private_key,
timeout=300)
_, stdout, stderr = ssh.exec_command(cmd)
output = stdout.read()
output_err = stderr.read()
ssh.close()
LOG.debug('Output ssh is "%s%s"', output, output_err)
return output, output_err
except SSHExecCommandFailed as exc:
output_msg = "Command failed."
LOG.debug(exc)
LOG.debug(output_msg)
raise

View File

@ -100,8 +100,9 @@ class Client(object):
s = self._get_ssh_connection()
_, stdout, stderr = s.exec_command(cmd)
res = stdout.read()
err_res = stderr.read()
s.close()
return res
return res, err_res
def _is_timed_out(self, timeout, start_time):
return (time.time() - timeout) > start_time

View File

@ -18,7 +18,6 @@
import logging
import os
import traceback
from fuel_health.common.utils.data_utils import rand_name
import fuel_health.common.ssh
@ -129,23 +128,6 @@ class HeatBaseTest(fuel_health.nmanager.NovaNetworkScenarioTest,
self.wait_interval):
self.fail("Timed out waiting for stack to be deleted.")
def _run_ssh_cmd(self, cmd):
"""
Open SSH session with Controller and and execute command.
"""
if not self.host:
self.fail('Wrong tests configuration: '
'controller_nodes parameter is empty ')
try:
sshclient = fuel_health.common.ssh.Client(
self.host[0], self.usr, self.pwd,
key_filename=self.key, timeout=self.timeout
)
return sshclient.exec_longrun_command(cmd)
except Exception:
LOG.debug(traceback.format_exc())
self.fail("%s command failed." % cmd)
def _find_heat_image(self, image_name):
return image_name in [i.name for i in
self.compute_client.images.list()]
@ -183,24 +165,24 @@ class HeatBaseTest(fuel_health.nmanager.NovaNetworkScenarioTest,
" test -f /tmp/vm_ready.txt && echo -ne YES || echo -ne NO")
def check():
return self._run_ssh_cmd(cmd) == "YES"
return self._run_ssh_cmd(cmd)[0] == "YES"
return fuel_health.test.call_until_true(
check, timeout, interval)
def _save_key_to_file(self, key):
return self._run_ssh_cmd(
"KEY=`mktemp`; echo '%s' > $KEY; echo -ne $KEY;" % key)
"KEY=`mktemp`; echo '%s' > $KEY; echo -ne $KEY;" % key)[0]
def _delete_key_file(self, filepath):
self._run_ssh_cmd("rm -f %s" % filepath)
def _load_vm_cpu(self, connection_string):
return self._run_ssh_cmd(
connection_string + " cat /dev/urandom | gzip -9 > /dev/null &")
connection_string + " cat /dev/urandom | gzip -9 > /dev/null &")[0]
def _release_vm_cpu(self, connection_string):
return self._run_ssh_cmd(connection_string + " pkill cat")
return self._run_ssh_cmd(connection_string + " pkill cat")[0]
def _get_net_uuid(self):
if 'neutron' in self.config.network.network_provider:
@ -226,7 +208,7 @@ class HeatBaseTest(fuel_health.nmanager.NovaNetworkScenarioTest,
cmd = "echo -ne `%s`" % grep
subnet = self._run_ssh_cmd(cmd)
subnet = self._run_ssh_cmd(cmd)[0]
if subnet:
return subnet
# if network has no subnets

View File

@ -396,6 +396,23 @@ class NovaNetworkScenarioTest(OfficialClientTest):
super(NovaNetworkScenarioTest, self).setUp()
self.check_clients_state()
def _run_ssh_cmd(self, cmd):
"""
Open SSH session with Controller and and execute command.
"""
if not self.host:
self.fail('Wrong tests configuration: '
'controller_nodes parameter is empty ')
try:
sshclient = fuel_health.common.ssh.Client(
self.host[0], self.usr, self.pwd,
key_filename=self.key, timeout=self.timeout
)
return sshclient.exec_longrun_command(cmd)
except Exception:
LOG.debug(traceback.format_exc())
self.fail("%s command failed." % cmd)
def _create_keypair(self, client, namestart='ost1_test-keypair-smoke-'):
kp_name = rand_name(namestart)
keypair = client.keypairs.create(kp_name)

View File

@ -19,7 +19,6 @@ import logging
import time
import traceback
from fuel_health.common.savanna_ssh import ssh_command
from fuel_health.common.utils.data_utils import rand_name
import fuel_health.nmanager as nmanager
@ -27,7 +26,7 @@ import fuel_health.nmanager as nmanager
LOG = logging.getLogger(__name__)
class SavannaTest(nmanager.OfficialClientTest):
class SavannaTest(nmanager.NovaNetworkScenarioTest):
"""
Base class for openstack sanity tests for Sahara
"""
@ -234,7 +233,7 @@ class SavannaTest(nmanager.OfficialClientTest):
while True:
cmd = ("timeout 60 bash -c 'echo >/dev/"
"tcp/{0}/{1}'; echo $?".format(host, port))
output, output_err = ssh_command(cmd)
output, output_err = self._run_ssh_cmd(cmd)
print('NC output after %s seconds is "%s"' % (i * 10, output))
LOG.debug('NC output after %s seconds is "%s"',
i * 10, output)
@ -253,8 +252,8 @@ class SavannaTest(nmanager.OfficialClientTest):
cmd_neutron = ('grep -E '
'"network_api_class=nova.network.neutronv2.api.API" '
'/etc/nova/nova.conf')
output_nova, output_nova_err = ssh_command(cmd_nova)
output_neutron, output_neutron_err = ssh_command(cmd_neutron)
output_nova, output_nova_err = self._run_ssh_cmd(cmd_nova)
output_neutron, output_neutron_err = self._run_ssh_cmd(cmd_neutron)
if(output_neutron or str(output_neutron_err).find(
'network_api_class=nova.network.neutronv2.api.API') > 0):
LOG.debug('neutron is found')
@ -500,9 +499,9 @@ class SavannaTest(nmanager.OfficialClientTest):
elif plugin_name == 'hdp':
hadoop_user = self.HDP_HADOOP_USER
node_username = self.HDP_NODE_USERNAME
ssh_command('echo "%s" > /tmp/ostf-savanna.pem' %
self.keys[0].private_key)
ssh_command('chmod 600 /tmp/ostf-savanna.pem')
self._run_ssh_cmd('echo "%s" > /tmp/ostf-savanna.pem' %
self.keys[0].private_key)
self._run_ssh_cmd('chmod 600 /tmp/ostf-savanna.pem')
while True:
cmd = ('ssh -i /tmp/ostf-savanna.pem -l %s '
'-oUserKnownHostsFile=/dev/null '
@ -511,7 +510,7 @@ class SavannaTest(nmanager.OfficialClientTest):
'-list-active-trackers | wc -l"' %
(node_username, node_info['namenode_ip'],
hadoop_user))
stdout, stderr = ssh_command(cmd)
stdout, stderr = self._run_ssh_cmd(cmd)
active_tasktracker_count = int(stdout)
LOG.debug('active_tasktracker_count:%s',
active_tasktracker_count)
@ -524,7 +523,7 @@ class SavannaTest(nmanager.OfficialClientTest):
'\'{print $3}\'' %
(node_username, node_info['namenode_ip'],
hadoop_user))
stdout, stderr = ssh_command(cmd)
stdout, stderr = self._run_ssh_cmd(cmd)
active_datanode_count = int(stdout)
LOG.debug('active_datanode_count:%s', active_datanode_count)
print('active_datanode_count:%s' % active_datanode_count)