Fix pylint warnings hapening in the build process to make Zuul builds clean

Warnings Fixed:
W1514: Using open without explicitly specifying an encoding (unspecified-encoding)
E0702: Raising str while only classes or instances are allowed (raising-bad-type)

Story: 2005892
Task: 43821

Change-Id: I2083fa4f8d994480c79ff557c56cf2f29c56c19f
Signed-off-by: Felipe Desiglo Ferrare <Felipe.DesigloFerrare@windriver.com>
This commit is contained in:
Felipe Desiglo Ferrare 2021-10-25 10:19:40 -03:00 committed by Felipe Desiglo Ferrare
parent 005b2d5dc2
commit 1516b57d62
21 changed files with 40 additions and 47 deletions

View File

@ -52,7 +52,6 @@ class MakeReport:
msg = "***Failure at test {}: {}".format(call.when, call.excinfo) msg = "***Failure at test {}: {}".format(call.when, call.excinfo)
print(msg) print(msg)
LOG.debug(msg + "\n***Details: {}".format(report.longrepr)) LOG.debug(msg + "\n***Details: {}".format(report.longrepr))
global tracebacks
tracebacks.append(str(report.longrepr)) tracebacks.append(str(report.longrepr))
self.test_results[call.when] = ['Failed', call.excinfo] self.test_results[call.when] = ['Failed', call.excinfo]
elif report.skipped: elif report.skipped:
@ -83,7 +82,7 @@ class TestRes:
def _write_results(res_in_tests, test_name): def _write_results(res_in_tests, test_name):
global tc_start_time global tc_start_time
with open(ProjVar.get_var("TCLIST_PATH"), mode='a') as f: with open(ProjVar.get_var("TCLIST_PATH"), mode='a', encoding='utf8') as f:
f.write('\n{}\t{}\t{}'.format(res_in_tests, tc_start_time, test_name)) f.write('\n{}\t{}\t{}'.format(res_in_tests, tc_start_time, test_name))
global test_count global test_count
test_count += 1 test_count += 1
@ -510,7 +509,7 @@ def pytest_unconfigure(config):
round(TestRes.PASSNUM * 100 / total_exec, 2)) round(TestRes.PASSNUM * 100 / total_exec, 2))
fail_rate = "{}%".format( fail_rate = "{}%".format(
round(TestRes.FAILNUM * 100 / total_exec, 2)) round(TestRes.FAILNUM * 100 / total_exec, 2))
with open(tc_res_path, mode='a') as f: with open(tc_res_path, mode='a', encoding='utf8') as f:
# Append general info to result log # Append general info to result log
f.write('\n\nLab: {}\n' f.write('\n\nLab: {}\n'
'Build ID: {}\n' 'Build ID: {}\n'
@ -534,7 +533,7 @@ def pytest_unconfigure(config):
f.write('------------\nSkipped: {}'.format(TestRes.SKIPNUM)) f.write('------------\nSkipped: {}'.format(TestRes.SKIPNUM))
LOG.info("Test Results saved to: {}".format(tc_res_path)) LOG.info("Test Results saved to: {}".format(tc_res_path))
with open(tc_res_path, 'r') as fin: with open(tc_res_path, 'r', encoding='utf8') as fin:
print(fin.read()) print(fin.read())
except Exception as e: except Exception as e:
LOG.exception( LOG.exception(

View File

@ -330,7 +330,7 @@ def _scp_on_local(cmd, remote_password, logdir=None, timeout=900):
logdir = logdir or ProjVar.get_var('LOG_DIR') logdir = logdir or ProjVar.get_var('LOG_DIR')
logfile = os.path.join(logdir, 'scp_files.log') logfile = os.path.join(logdir, 'scp_files.log')
with open(logfile, mode='a') as f: with open(logfile, mode='a', encoding='utf8') as f:
local_child = pexpect.spawn(command=cmd, encoding='utf-8', logfile=f) local_child = pexpect.spawn(command=cmd, encoding='utf-8', logfile=f)
index = local_child.expect([pexpect.EOF, 'assword:', 'yes/no'], index = local_child.expect([pexpect.EOF, 'assword:', 'yes/no'],
timeout=timeout) timeout=timeout)
@ -571,7 +571,7 @@ def write_to_file(file_path, content, mode='a'):
""" """
time_stamp = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime()) time_stamp = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime())
with open(file_path, mode=mode) as f: with open(file_path, mode=mode, encoding='utf8') as f:
f.write( f.write(
'\n-----------------[{}]-----------------\n{}\n'.format(time_stamp, '\n-----------------[{}]-----------------\n{}\n'.format(time_stamp,
content)) content))
@ -812,7 +812,7 @@ def get_yaml_data(filepath):
Return(json): Return(json):
returns the json data returns the json data
""" """
with open(filepath, 'r') as f: with open(filepath, 'r', encoding='utf8') as f:
data = yaml.safe_load(f) data = yaml.safe_load(f)
return data return data
@ -830,7 +830,7 @@ def write_yaml_data_to_file(data, filename, directory=None):
if directory is None: if directory is None:
directory = ProjVar.get_var('LOG_DIR') directory = ProjVar.get_var('LOG_DIR')
src_path = "{}/{}".format(directory, filename) src_path = "{}/{}".format(directory, filename)
with open(src_path, 'w') as f: with open(src_path, 'w', encoding='utf8') as f:
yaml.dump(data, f) yaml.dump(data, f)
return src_path return src_path

View File

@ -4180,7 +4180,7 @@ def _create_cloud_init_if_conf(guest_os, nics_num):
# else: # else:
# shell = '/bin/bash' # shell = '/bin/bash'
with open(tmp_file, mode='a') as f: with open(tmp_file, mode='a', encoding='utf8') as f:
f.write("#cloud-config\n") f.write("#cloud-config\n")
if new_user is not None: if new_user is not None:
@ -5279,7 +5279,7 @@ def collect_guest_logs(vm_id):
else: else:
output = vm_ssh.exec_cmd('tail -n 200 {}'.format(log_path), output = vm_ssh.exec_cmd('tail -n 200 {}'.format(log_path),
fail_ok=False)[1] fail_ok=False)[1]
with open(local_log_path, mode='w') as f: with open(local_log_path, mode='w', encoding='utf8') as f:
f.write(output) f.write(output)
return return

View File

@ -487,7 +487,7 @@ def get_system_mode_from_lab_info(lab, multi_region_lab=False,
def add_ping_failure(test_name): def add_ping_failure(test_name):
file_path = '{}{}'.format(ProjVar.get_var('PING_FAILURE_DIR'), file_path = '{}{}'.format(ProjVar.get_var('PING_FAILURE_DIR'),
'ping_failures.txt') 'ping_failures.txt')
with open(file_path, mode='a') as f: with open(file_path, mode='a', encoding='utf8') as f:
f.write(test_name + '\n') f.write(test_name + '\n')

View File

@ -125,7 +125,7 @@ def test_cpu_pol_vm_actions(flv_vcpus, cpu_pol, pol_source, boot_source):
**image_meta)[1] **image_meta)[1]
if boot_source == 'volume': if boot_source == 'volume':
LOG.tc_step("Create a volume from image") LOG.tc_step("Create a volume from image")
source_id = cinder_helper.create_volume(name='cpu_pol'.format(cpu_pol), source_id = cinder_helper.create_volume(name='cpu_pol_{}'.format(cpu_pol),
source_id=image_id)[1] source_id=image_id)[1]
ResourceCleanup.add('volume', source_id) ResourceCleanup.add('volume', source_id)
else: else:

View File

@ -215,8 +215,6 @@ def test_ima_no_event(operation, file_path):
""" """
global files_to_delete
start_time = common.get_date_in_format() start_time = common.get_date_in_format()
source_file = file_path source_file = file_path
con_ssh = ControllerClient.get_active_controller() con_ssh = ControllerClient.get_active_controller()
@ -296,7 +294,6 @@ def test_ima_event_generation(operation, file_path):
- Check IMA violation event is logged - Check IMA violation event is logged
""" """
global files_to_delete
con_ssh = ControllerClient.get_active_controller() con_ssh = ControllerClient.get_active_controller()
start_time = common.get_date_in_format() start_time = common.get_date_in_format()
@ -315,8 +312,7 @@ def test_ima_event_generation(operation, file_path):
execute_cmd = "{} -p".format(dest_file) execute_cmd = "{} -p".format(dest_file)
else: else:
LOG.tc_step("Append to copy of monitored file") LOG.tc_step("Append to copy of monitored file")
cmd = 'echo "output" | sudo -S tee -a /usr/sbin/TEMP'.format( cmd = 'echo "output" | sudo -S tee -a /usr/sbin/TEMP'
HostLinuxUser.get_password())
con_ssh.exec_cmd(cmd, fail_ok=False) con_ssh.exec_cmd(cmd, fail_ok=False)
LOG.tc_step("Execute modified file") LOG.tc_step("Execute modified file")
con_ssh.exec_sudo_cmd(dest_file) con_ssh.exec_sudo_cmd(dest_file)

View File

@ -32,11 +32,11 @@ def modify_yaml(file_dir, file_name, str_to_add, hugepage_value):
Return(str): Return(str):
returns the file_dir and filename with modified values returns the file_dir and filename with modified values
""" """
with open("{}/{}".format(file_dir, file_name), 'r') as f: with open("{}/{}".format(file_dir, file_name), 'r', encoding='utf8') as f:
data = yaml.safe_load(f) data = yaml.safe_load(f)
data['spec']['containers'][0]['resources']['limits'][str_to_add] = hugepage_value data['spec']['containers'][0]['resources']['limits'][str_to_add] = hugepage_value
newfile = "hugepages_pod_{}.yaml".format(hugepage_value) newfile = "hugepages_pod_{}.yaml".format(hugepage_value)
with open("{}/{}".format(ProjVar.get_var('LOG_DIR'), newfile), 'w') as f: with open("{}/{}".format(ProjVar.get_var('LOG_DIR'), newfile), 'w', encoding='utf8') as f:
yaml.dump(data, f) yaml.dump(data, f)
return ProjVar.get_var('LOG_DIR'), newfile return ProjVar.get_var('LOG_DIR'), newfile

View File

@ -87,7 +87,6 @@ def snapshot_from_instance(request, create_flavour_and_image, volume_for_instanc
@fixture(scope="module") @fixture(scope="module")
def launch_instances(create_flavour_and_image, create_network_sanity, snapshot_from_instance): def launch_instances(create_flavour_and_image, create_network_sanity, snapshot_from_instance):
global VM_IDS
net_id_list = list() net_id_list = list()
net_id_list.append({"net-id": create_network_sanity}) net_id_list.append({"net-id": create_network_sanity})
host = system_helper.get_active_controller_name() host = system_helper.get_active_controller_name()

View File

@ -77,7 +77,6 @@ def volume_from_instance(request, create_flavour_and_image):
@fixture(scope="module") @fixture(scope="module")
def launch_instances(create_flavour_and_image, create_network_sanity, volume_from_instance): def launch_instances(create_flavour_and_image, create_network_sanity, volume_from_instance):
global VM_IDS
net_id_list = list() net_id_list = list()
net_id_list.append({"net-id": create_network_sanity}) net_id_list.append({"net-id": create_network_sanity})
host = system_helper.get_active_controller_name() host = system_helper.get_active_controller_name()

View File

@ -92,7 +92,7 @@ class LocalHostClient(SSHClient):
self.logpath = self._get_logpath() self.logpath = self._get_logpath()
if self.logpath: if self.logpath:
self.session.logfile = open(self.logpath, 'w+') self.session.logfile = open(self.logpath, 'w+', encoding='utf8')
# Set prompt for matching # Set prompt for matching
self.set_prompt(prompt) self.set_prompt(prompt)

View File

@ -164,7 +164,7 @@ class SSHClient:
self.logpath = self._get_logpath() self.logpath = self._get_logpath()
if self.logpath: if self.logpath:
self.session.logfile = open(self.logpath, 'a+') self.session.logfile = open(self.logpath, 'a+', encoding='utf8')
# Login # Login
self.session.login(self.host, self.user, self.password, self.session.login(self.host, self.user, self.password,

View File

@ -542,7 +542,7 @@ class TelnetClient(Telnet):
def get_log_file(self, log_dir): def get_log_file(self, log_dir):
if log_dir: if log_dir:
logfile = open(log_dir, 'a') logfile = open(log_dir, 'a', encoding='utf8')
else: else:
logfile = None logfile = None

View File

@ -129,10 +129,10 @@ class BaseWebObject:
elt = element() if hasattr(element, '__call__') else element elt = element() if hasattr(element, '__call__') else element
except: except:
return False return False
for text in texts: # for text in texts:
if self._is_text_visible(elt, 'Error'): # if self._is_text_visible(elt, 'Error'): #
s = 'error' # s = 'error'
raise s # raise Exception(s)
if self._is_text_visible(elt, text): if self._is_text_visible(elt, text):
return text return text
return False return False

View File

@ -66,7 +66,7 @@ class MetadatadefinitionsPage(basepage.BasePage):
:return = json data container :return = json data container
""" """
try: try:
with open(namespace_template_name, 'r') as template: with open(namespace_template_name, 'r', encoding='utf8') as template:
json_template = json.load(template) json_template = json.load(template)
except Exception: except Exception:
raise EOFError("Can not read template file: [{0}]".format( raise EOFError("Can not read template file: [{0}]".format(

View File

@ -55,7 +55,7 @@ class ApiAccessPage(basepage.BasePage):
self._wait_until( self._wait_until(
lambda _: len(self.list_of_files(directory, template)) > 0) lambda _: len(self.list_of_files(directory, template)) > 0)
file_name = self.list_of_files(directory, template)[0] file_name = self.list_of_files(directory, template)[0]
with open(join(directory, file_name)) as cred_file: with open(join(directory, file_name), encoding='utf8') as cred_file:
content = cred_file.read() content = cred_file.read()
username_re = r'export OS_USERNAME="([^"]+)"' username_re = r'export OS_USERNAME="([^"]+)"'
if version == 2: if version == 2:

View File

@ -35,7 +35,7 @@ class VideoRecorder(object):
LOG.warning('Video recording is running already') LOG.warning('Video recording is running already')
return return
fnull = open(os.devnull, 'w') fnull = open(os.devnull, 'w', encoding='utf8')
LOG.info('Record video via %s', ' '.join(self._cmd)) LOG.info('Record video via %s', ' '.join(self._cmd))
self._popen = subprocess.Popen(self._cmd, stdout=fnull, stderr=fnull) self._popen = subprocess.Popen(self._cmd, stdout=fnull, stderr=fnull)
self.is_launched = True self.is_launched = True

View File

@ -30,7 +30,7 @@ def _get_failed_test_names(log_dir):
if not path.exists(test_res_path): if not path.exists(test_res_path):
return [] return []
with open(test_res_path, 'r') as file: with open(test_res_path, 'r', encoding='utf8') as file:
failed_tests = [] failed_tests = []
for line in file: for line in file:
@ -68,7 +68,7 @@ def get_tracebacks_from_pytestlog(log_dir, traceback_lines=10,
current_failure = None current_failure = None
next_failure = failed_tests.pop(0) next_failure = failed_tests.pop(0)
traceback_for_test = [] traceback_for_test = []
with open(path.join(log_dir, 'pytestlog.log'), 'r') as file: with open(path.join(log_dir, 'pytestlog.log'), 'r', encoding='utf8') as file:
for line in file: for line in file:
if current_failure is not None: if current_failure is not None:
if re.match(new_test_pattern, line): if re.match(new_test_pattern, line):
@ -151,8 +151,8 @@ def parse_test_steps(log_dir, failures_only=True):
if failures_only and not failed_tests: if failures_only and not failed_tests:
return return
with open("{}/TIS_AUTOMATION.log".format(log_dir), 'r') as file, \ with open("{}/TIS_AUTOMATION.log".format(log_dir), 'r', encoding='utf8') as file, \
open("{}/test_steps.log".format(log_dir), 'w') as log: open("{}/test_steps.log".format(log_dir), 'w', encoding='utf8') as log:
for line in file: for line in file:
if test_steps_length >= 500: if test_steps_length >= 500:

View File

@ -132,7 +132,7 @@ def update_config_ini(**kwargs):
# ----------------------------------------------------------------------- # -----------------------------------------------------------------------
if count != 0: if count != 0:
with open(config_ini, 'w') as configfile: with open(config_ini, 'w', encoding='utf8') as configfile:
configurations.write(configfile) configurations.write(configfile)
status = True status = True
message = '{}: was updated successfully'.format(os.path.basename( message = '{}: was updated successfully'.format(os.path.basename(
@ -410,7 +410,7 @@ def get_controllers_ip(env, config_file, config_type, lab_file):
""" """
# Read Configurtion File # Read Configurtion File
conf = yaml.safe_load(open(config_file)) conf = yaml.safe_load(open(config_file, encoding='utf8'))
cont_data = {} cont_data = {}
# Get Controllers IP's # Get Controllers IP's
@ -423,7 +423,7 @@ def get_controllers_ip(env, config_file, config_type, lab_file):
if env == 'baremetal': if env == 'baremetal':
# Get phyisical interfaces # Get phyisical interfaces
conf_lab = yaml.safe_load(open(lab_file)) conf_lab = yaml.safe_load(open(lab_file, encoding='utf8'))
cont_data['OAM_IF'] = conf_lab['nodes']['controller-0']['oam_if'] cont_data['OAM_IF'] = conf_lab['nodes']['controller-0']['oam_if']
cont_data['MGMT_IF'] = conf_lab['nodes']['controller-0']['mgmt_if'] cont_data['MGMT_IF'] = conf_lab['nodes']['controller-0']['mgmt_if']

View File

@ -31,7 +31,7 @@ PROMPT = '$'
LOG_FILENAME = 'iso_setup_baremetal.log' LOG_FILENAME = 'iso_setup_baremetal.log'
LOG_PATH = config.get('general', 'LOG_PATH') LOG_PATH = config.get('general', 'LOG_PATH')
LOG = logger.setup_logging('iso_setup_baremetal', LOG = logger.setup_logging('iso_setup_baremetal',
log_file='%s/%s'.format(LOG_PATH, LOG_FILENAME), log_file='{}/{}'.format(LOG_PATH, LOG_FILENAME),
console_log=False) console_log=False)
@ -133,9 +133,9 @@ class PxeServer(object):
@staticmethod @staticmethod
def check_pxe_services(): def check_pxe_services():
"""This function is intended to restart DHCP service """This function is intended to restart DHCP service
DHCP service needs to be restarted in order to grab the changes on the DHCP service needs to be restarted in order to grab the changes on the
dhcp config file""" dhcp config file"""
LOG.info('Checking PXE needed services') LOG.info('Checking PXE needed services')
services = ['dhcpd', 'tftp', 'httpd'] services = ['dhcpd', 'tftp', 'httpd']
@ -221,7 +221,7 @@ class PxeServer(object):
linuxefi=grub_lines['linuxefi'], linuxefi=grub_lines['linuxefi'],
initrdefi=grub_lines['initrdefi'])) initrdefi=grub_lines['initrdefi']))
grub_timeout = 'timeout=5\n' grub_timeout = 'timeout=5\n'
with open(os.path.join(self.tftp_dir, 'grub.cfg'), 'w') as grub_file: with open(os.path.join(self.tftp_dir, 'grub.cfg'), 'w', encoding='utf8') as grub_file:
grub_file.writelines(grub_timeout) grub_file.writelines(grub_timeout)
grub_file.write(grub_entry) grub_file.write(grub_entry)
@ -352,7 +352,7 @@ def analyze_grub(grub_cfg_file):
Get linuxefi command and initrdefi command from grub_dict according to Get linuxefi command and initrdefi command from grub_dict according to
selected option in config file selected option in config file
""" """
with open(grub_cfg_file, 'r') as grub: with open(grub_cfg_file, 'r', encoding='utf8') as grub:
lines = grub.readlines() lines = grub.readlines()
cmd_lines = list() cmd_lines = list()
@ -397,7 +397,7 @@ def install_iso_master_controller():
nodes_file = os.path.join(os.environ['PYTHONPATH'], 'baremetal', nodes_file = os.path.join(os.environ['PYTHONPATH'], 'baremetal',
'baremetal_setup.yaml') 'baremetal_setup.yaml')
nodes = yaml.safe_load(open(nodes_file)) nodes = yaml.safe_load(open(nodes_file, encoding='utf8'))
# Update config.ini with OAM and MGMT interfaces # Update config.ini with OAM and MGMT interfaces
network_interfaces = [] network_interfaces = []
@ -423,7 +423,7 @@ def get_controller0_ip():
nodes_file = os.path.join(THIS_PATH, '..', 'BareMetal', nodes_file = os.path.join(THIS_PATH, '..', 'BareMetal',
'installation_setup.yaml') 'installation_setup.yaml')
nodes = yaml.load(open(nodes_file)) nodes = yaml.load(open(nodes_file, encoding='utf8'))
controller_0 = nodes['controller-0'] controller_0 = nodes['controller-0']
master_controller = Node(controller_0) master_controller = Node(controller_0)
@ -437,7 +437,7 @@ def config_controller(config_file):
'iso_installer', 'CONFIG_CONTROLLER_TIMEOUT')) 'iso_installer', 'CONFIG_CONTROLLER_TIMEOUT'))
nodes_file = os.path.join(os.environ['PYTHONPATH'], 'baremetal', nodes_file = os.path.join(os.environ['PYTHONPATH'], 'baremetal',
'baremetal_setup.yaml') 'baremetal_setup.yaml')
nodes = yaml.safe_load(open(nodes_file)) nodes = yaml.safe_load(open(nodes_file, encoding='utf8'))
controller_0 = nodes['nodes']['controller-0'] controller_0 = nodes['nodes']['controller-0']
master_controller = Node(controller_0) master_controller = Node(controller_0)
serial_cmd = ('ipmitool -I lanplus -H {node_bmc_ip} -U {node_bmc_user} ' serial_cmd = ('ipmitool -I lanplus -H {node_bmc_ip} -U {node_bmc_user} '
@ -475,7 +475,7 @@ def install_secondary_nodes():
nodes_file = os.path.join(THIS_PATH, '..', 'BareMetal', nodes_file = os.path.join(THIS_PATH, '..', 'BareMetal',
'installation_setup.yml') 'installation_setup.yml')
nodes = yaml.load(open(nodes_file)) nodes = yaml.load(open(nodes_file, encoding='utf8'))
# Removing controller-0 from Nodes # Removing controller-0 from Nodes
controller_0 = nodes.pop('controller-0') controller_0 = nodes.pop('controller-0')

View File

@ -577,7 +577,7 @@ def setup_qemu(iso_file, configuration_file):
check_preconditions() check_preconditions()
# loading all the configurations from yaml file # loading all the configurations from yaml file
configurations = yaml.safe_load(open(configuration_file)) configurations = yaml.safe_load(open(configuration_file, encoding='utf8'))
# fixme(Humberto): check_system_resources is commented out due # fixme(Humberto): check_system_resources is commented out due
# check is giving problems when configuring an instance on qemu virtual # check is giving problems when configuring an instance on qemu virtual

View File

@ -17,7 +17,7 @@ def setup_logging_using_config(name, config_file):
logger logger
:return: returns the instance of the logger already configured :return: returns the instance of the logger already configured
""" """
with open(config_file, 'r') as file_manager: with open(config_file, 'r', encoding='utf8') as file_manager:
config = yaml.safe_load(file_manager.read()) config = yaml.safe_load(file_manager.read())
logging.config.dictConfig(config) logging.config.dictConfig(config)