Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Do not use cat for file read
Browse files Browse the repository at this point in the history
Do not use cat for file read:
   it's difficult for debug operations with source data
   YAMLEditor content calls get_content() if not loaded

Change-Id: I75061c067172085de0e5fb1baa051816a4a707eb
  • Loading branch information
penguinolog committed Oct 10, 2016
1 parent a19cd7b commit 3d542cf
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 152 deletions.
9 changes: 6 additions & 3 deletions fuelweb_test/helpers/checkers.py
Expand Up @@ -833,9 +833,12 @@ def verify_bootstrap_on_node(ip, os_type, uuid=None):
if not uuid:
return

cmd = "cat /etc/nailgun-agent/config.yaml"
output = yaml.load(ssh_manager.execute_on_remote(ip, cmd)['stdout_str'])
actual_uuid = output.get("runtime_uuid")
with ssh_manager.open_on_remote(
ip=ip,
path='/etc/nailgun-agent/config.yaml') as f:
data = yaml.safe_load(f)

actual_uuid = data.get("runtime_uuid")
assert_equal(actual_uuid, uuid,
"Actual uuid {0} is not the same as expected {1}"
.format(actual_uuid, uuid))
Expand Down
50 changes: 17 additions & 33 deletions fuelweb_test/helpers/fuel_actions.py
Expand Up @@ -19,11 +19,7 @@
from devops.models import DiskDevice
from devops.models import Node
from devops.models import Volume
from proboscis.asserts import assert_equal
from proboscis.asserts import assert_true
# noinspection PyUnresolvedReferences
from six.moves import cStringIO
import yaml

from core.helpers.log_helpers import logwrap

Expand Down Expand Up @@ -202,23 +198,17 @@ def clean_generated_image(self, distro):
)

def get_fuel_settings(self):
result = self.ssh_manager.execute_on_remote(
ip=self.admin_ip,
cmd='cat {cfg_file}'.format(cfg_file=FUEL_SETTINGS_YAML)
)
return yaml.load(result['stdout_str'])
return YamlEditor(
file_path=FUEL_SETTINGS_YAML,
ip=self.admin_ip
).get_content()

def save_fuel_settings(self, settings):
cmd = 'echo \'{0}\' > {1}'.format(yaml.dump(settings,
default_style='"',
default_flow_style=False),
FUEL_SETTINGS_YAML)
result = self.ssh_manager.execute(
ip=self.admin_ip,
cmd=cmd
)
assert_equal(result['exit_code'], 0,
"Saving Fuel settings failed: {0}!".format(result))
with YamlEditor(
file_path=FUEL_SETTINGS_YAML,
ip=self.admin_ip
) as data:
data.content = settings

@logwrap
def get_tasks_description(self, release=None):
Expand All @@ -230,8 +220,7 @@ def get_tasks_description(self, release=None):
if not release:
release = ''
cmd = "cat `find /etc/puppet/{} -name tasks.yaml`".format(release)
data = self.ssh_manager.execute_on_remote(self.admin_ip, cmd)
return yaml.load(cStringIO(''.join(data['stdout'])))
return self.ssh_manager.check_call(self.admin_ip, cmd).stdout_yaml

@logwrap
def create_mirror(self, pattern_name, repo_groups, input_file=None):
Expand Down Expand Up @@ -299,15 +288,11 @@ class NailgunActions(BaseActions):

def update_nailgun_settings(self, settings):
cfg_file = '/etc/nailgun/settings.yaml'
with self.ssh_manager.open_on_remote(self.admin_ip, cfg_file) as f:
ng_settings = yaml.load(f)

ng_settings.update(settings)
logger.debug('Uploading new nailgun settings: {!r}'.format(
ng_settings))
with self.ssh_manager.open_on_remote(
self.admin_ip, cfg_file, "w") as f:
yaml.dump(ng_settings, f)
with YamlEditor(file_path=cfg_file, ip=self.admin_ip) as ng_settings:
ng_settings.content.update(settings)

logger.debug('Uploading new nailgun settings: {}'.format(
ng_settings))
self.restart_service("nailgun")

def set_collector_address(self, host, port, ssl=False):
Expand All @@ -323,9 +308,8 @@ def set_collector_address(self, host, port, ssl=False):
'OSWL_COLLECT_PERIOD': 0}
if not ssl:
# replace https endpoints to http endpoints
with self.ssh_manager.open_on_remote(self.admin_ip,
base_cfg_file) as f:
data = yaml.load(f)
data = YamlEditor(base_cfg_file, self.admin_ip).get_content()

for key, value in data.items():
if (isinstance(key, str) and key.startswith("COLLECTOR") and
key.endswith("URL") and value.startswith("https")):
Expand Down
45 changes: 35 additions & 10 deletions fuelweb_test/helpers/utils.py
Expand Up @@ -1078,7 +1078,7 @@ def get_quantity_of_numa(ip):

numa = int(SSHManager().check_call(
ip=ip,
command="lstopo | grep NUMANode| wc -l"
command="lstopo | grep -c NUMANode"
).stdout[0])

if not numa:
Expand Down Expand Up @@ -1480,18 +1480,43 @@ class YamlEditor(object):
"""

def __init__(self, file_path, ip=None, port=None):
self.file_path = file_path
"""YAML files editor
:type file_path: str
:type ip: str
:type port: int
"""
self.__file_path = file_path
self.ip = ip
self.port = port or 22
self.content = None
self.original_content = None
self.__content = None
self.__original_content = None

@property
def file_path(self):
"""Open file path
:rtype: str
"""
return self.__file_path

@property
def content(self):
if self.__content is None:
self.__content = self.get_content()
return self.__content

@content.setter
def content(self, new_content):
self.__content = new_content

def __get_file(self, mode="r"):
if self.ip:
return SSHManager().open_on_remote(self.ip, self.file_path,
mode=mode, port=self.port)
return SSHManager().open_on_remote(
self.ip, self.__file_path,
mode=mode, port=self.port)
else:
return open(self.file_path, mode)
return open(self.__file_path, mode)

def get_content(self):
with self.__get_file() as file_obj:
Expand All @@ -1506,12 +1531,12 @@ def write_content(self, content=None):
default_style='"')

def __enter__(self):
self.content = self.get_content()
self.original_content = copy.deepcopy(self.content)
self.__content = self.get_content()
self.__original_content = copy.deepcopy(self.content)
return self

def __exit__(self, x, y, z):
if self.content == self.original_content:
if self.content == self.__original_content:
return
self.write_content()

Expand Down
38 changes: 18 additions & 20 deletions fuelweb_test/models/environment.py
Expand Up @@ -666,28 +666,26 @@ def modify_resolv_conf(self, nameservers=None, merge=True):
if nameservers is None:
nameservers = []

resolv_conf = self.ssh_manager.execute(
ip=self.ssh_manager.admin_ip,
cmd='cat /etc/resolv.conf'
)
assert_equal(0, resolv_conf['exit_code'],
'Executing "{0}" on the admin node has failed with: {1}'
.format('cat /etc/resolv.conf', resolv_conf['stderr']))
with self.ssh_manager.open_on_remote(
ip=self.ssh_manager.admin_ip,
path='/etc/resolv.conf',
) as f:
resolv_conf = f.readlines()

if merge:
nameservers.extend(resolv_conf['stdout'])
nameservers.extend(resolv_conf)
resolv_keys = ['search', 'domain', 'nameserver']
resolv_new = "".join('{0}\n'.format(ns) for ns in nameservers
if any(x in ns for x in resolv_keys))
logger.debug('echo "{0}" > /etc/resolv.conf'.format(resolv_new))
echo_cmd = 'echo "{0}" > /etc/resolv.conf'.format(resolv_new)
echo_result = self.ssh_manager.execute(
ip=self.ssh_manager.admin_ip,
cmd=echo_cmd
)
assert_equal(0, echo_result['exit_code'],
'Executing "{0}" on the admin node has failed with: {1}'
.format(echo_cmd, echo_result['stderr']))
return resolv_conf['stdout']
resolv_new = "".join(
'{0}\n'.format(ns) for ns in nameservers
if any(x in ns for x in resolv_keys))
with self.ssh_manager.open_on_remote(
ip=self.ssh_manager.admin_ip,
path='/etc/resolv.conf',
mode='w'
) as f:
f.write(resolv_new)

return resolv_conf

@logwrap
def describe_other_admin_interfaces(self, admin):
Expand Down
8 changes: 4 additions & 4 deletions fuelweb_test/models/fuel_web_client.py
Expand Up @@ -453,8 +453,8 @@ def fqdn(self, devops_node):
def get_pcm_nodes(self, ctrl_node, pure=False):
nodes = {}
with self.get_ssh_for_node(ctrl_node) as remote:
pcs_status = remote.execute('pcs status nodes')['stdout']
pcm_nodes = yaml.load(''.join(pcs_status).strip())
pcm_nodes = remote.execute('pcs status nodes').stdout_yaml

for status in ('Online', 'Offline', 'Standby'):
list_nodes = (pcm_nodes['Pacemaker Nodes'][status] or '').split()
if not pure:
Expand Down Expand Up @@ -2658,8 +2658,8 @@ def update_nodegroups(self, cluster_id, node_groups):
def get_nailgun_primary_node(self, slave, role='primary-controller'):
# returns controller or mongo that is primary in nailgun
with self.get_ssh_for_node(slave.name) as remote:
data = yaml.load(''.join(
remote.execute('cat /etc/astute.yaml')['stdout']))
with remote.open('/etc/astute.yaml') as f:
data = yaml.safe_load(f)
nodes = data['network_metadata']['nodes']
node_name = [node['fqdn'] for node in nodes.values()
if role in node['node_roles']][0]
Expand Down
12 changes: 6 additions & 6 deletions fuelweb_test/tests/plugins/plugin_emc/test_plugin_emc.py
Expand Up @@ -17,7 +17,6 @@
from proboscis import test
# pylint: disable=import-error
from six.moves import configparser
from six.moves import cStringIO
# pylint: enable=import-error

from fuelweb_test.helpers import utils
Expand All @@ -41,11 +40,12 @@ def __init__(self):

@classmethod
def check_emc_cinder_config(cls, ip, path):
command = 'cat {0}'.format(path)
conf_data = SSHManager().execute_on_remote(ip, command)['stdout_str']
conf_data = cStringIO(conf_data)
cinder_conf = configparser.ConfigParser()
cinder_conf.readfp(conf_data)
with SSHManager().open_on_remote(
ip=ip,
path=path
) as f:
cinder_conf = configparser.ConfigParser()
cinder_conf.readfp(f)

asserts.assert_equal(
cinder_conf.get('DEFAULT', 'volume_driver'),
Expand Down

0 comments on commit 3d542cf

Please sign in to comment.