I am trying to update a particular option in my config.ini file using ConfigParser.
This is my config.ini file:
[remote-server-details]
MACHINE_NAME =
MACHINE_USERNAME =
MACHINE_PASSWORD = Welcome!23
And this is my step file in Python:
#step('I change the following config options in section "{section_name}" in the {config_file_type} config file')
def set_dag_config_section_values(context, section_name, config_file_type):
parameters = ast.literal_eval(context.text)
context.conf[config_file_type].read(context.dag_config_file[config_file_type])
context.conf[config_file_type].remove_section(section_name)
context.conf[config_file_type].add_section(section_name)
for option, value in parameters.iteritems():
context.conf[config_file_type].set(section_name, option, value)
with open(context.dag_config_file[config_file_type], 'w+') as configfile:
context.conf[config_file_type].write(configfile)
context.conf[config_file_type].read(context.dag_config_file[config_file_type])
readin_parameters = dict(context.conf[config_file_type].items(section_name))
sort_dict(parameters)
sort_dict(readin_parameters)
assert readin_parameters == parameters, 'Value could not be set. %s does not match config contents %s' % (parameters, readin_parameters)
I need to update only MACHINE_NAME and MACHINE_USERNAME, as MACHINE_PASSWORD is already contained in the config file, but I'm getting this error:
INFO - Subtask: remote_server_details['MACHINE_PASSWORD'],
[2018-08-17 17:40:04,723] {{base_task_runner.py:98}} INFO - Subtask: KeyError: 'MACHINE_PASSWORD'
Related
If I define my logging config in an .ini file, I can pass default arguments to the config like this:
In the .ini file:
[handler_fileHandler]
class = logging.FileHandler
level = ERROR
formatter = simpleFormatter
# use args to pass arguemnts to the handler
args = (f'{logfilename}', 'a') # filename, append
Loading the config from the file:
# load config and pass default arguemnts
config.fileConfig(
fname="./logging.ini",
# pass the argument filename for the filehandler
defaults={ 'logfilename' : getSomeName() }
disable_existing_loggers=False,
)
Is there any possibility to do the same when I use a yaml file? According to the docs I would say no.
you can set it like this:
args=('%(logfilename)s',)
I have created a pytest.ini file,
addopts = --resultlog=log.txt
This creates a log file, but I would like to create a new log file everytime I run the tests.
I am new to the pytest, and pardon me if I have missed out anything while reading the documentation.
Thanks
Note
--result-log argument is deprecated and scheduled for removal in version 6.0 (see Deprecations and Removals: Result log). The possible replacement implementation is discussed in issue #4488, so watch out for the next major version bump - the code below will stop working with pytest==6.0.
Answer
You can modify the resultlog in the pytest_configure hookimpl. Example: put the code below in the conftest.py file in your project root dir:
import datetime
def pytest_configure(config):
if not config.option.resultlog:
timestamp = datetime.datetime.strftime(datetime.datetime.now(), '%Y-%m-%d_%H-%M-%S')
config.option.resultlog = 'log.' + timestamp
Now if --result-log is not passed explicitly (so you have to remove addopts = --resultlog=log.txt from your pytest.ini), pytest will create a log file ending with a timestamp. Passing --result-log with a log file name will override this behaviour.
Answering my own question.
As hoefling mentioned --result-log is deprecated, I had to find a way to do it without using that flag. Here's how I did it,
conftest.py
from datetime import datetime
import logging
log = logging.getLogger(__name__)
def pytest_assertrepr_compare(op, left, right):
""" This function will print log everytime the assert fails"""
log.error('Comparing Foo instances: vals: %s != %s \n' % (left, right))
return ["Comparing Foo instances:", " vals: %s != %s" % (left, right)]
def pytest_configure(config):
""" Create a log file if log_file is not mentioned in *.ini file"""
if not config.option.log_file:
timestamp = datetime.strftime(datetime.now(), '%Y-%m-%d_%H-%M-%S')
config.option.log_file = 'log.' + timestamp
pytest.ini
[pytest]
log_cli = true
log_cli_level = CRITICAL
log_cli_format = %(message)s
log_file_level = DEBUG
log_file_format = %(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)
log_file_date_format=%Y-%m-%d %H:%M:%S
test_my_code.py
import logging
log = logging.getLogger(__name__)
def test_my_code():
****test code
You can have different pytest run logs by naming the log file the time when test execution starts.
pytest tests --log-file $(date '+%F_%H:%M:%S')
This will create a log file for each test run. And the name of the test run would be the timestamp.
$(date '+%F_%H:%M:%S') is the bash command to get current timestamp in DATE_Hr:Min:Sec format.
When I set the parameter "config.option.file_or_dir" in the "conftest.py" file, the value set is not been used. It runs all the tests.
When I read the parameter "config.option.file_or_dir" it's display my value i had set.
conftest.py
def pytest_configure(config):
if len(config.option.file_or_dir) == 0 and ini_paser.get('TestRun', 'value') is not "":
config.option.file_or_dir.append(ini_paser.get('TestRun', 'value'))
print("file_or_dir - after =" + str(config.option.file_or_dir))
test.ini
[Suites]
value=Smoke
[Browser]
env_title=Browser
value=chrome
[DPR_ENV]
env_title=DRP Environment
value=SQA
[Customer]
env_title=Customer
value=mstqa
[User]
env_title=User
value=mstqa_d_admin,mstqa_c_admin
[TestRun]
value=Software_Update/
command line
xxx/scr/proj/DRP/tests/pytest --ini test.ini
I do not put any parameter in the command line. I will set which to tests to be executed from the ini file via the section "TestRun".
Python has ConfigParser library for parsing mysql configuration files.
https://docs.python.org/2/library/configparser.html
But i need to parse configuration files of PostgreSQL as well. But i couldn't find any parser for them.
In PostgreSQL there is no section or heading, so it can't be done by configparser.
Example:
# The default values of these variables are driven from the -D command-line
# option or PGDATA environment variable, represented here as ConfigDir.
data_directory = '/data/postgresql' # use data in another directory
# (change requires restart)
hba_file = '/etc/postgresql/9.1/main/pg_hba.conf' # host-based authentication file
# (change requires restart)
ident_file = '/etc/postgresql/9.1/main/pg_ident.conf' # ident configuration file
# (change requires restart)
# If external_pid_file is not explicitly set, no extra PID file is written.
external_pid_file = '/var/run/postgresql/9.1-main.pid' # write an extra PID file
# (change requires restart)
I was using the below code for mysql and it's working fine but not for postgresql.conf file.
import ConfigParser
class MysqlParserModule:
def __init__(self):
print 'Mysql Parser Module'
def parse_option(self, conf_file, section, option):
try:
config = ConfigParser.ConfigParser()
config.read(conf_file)
result = config.get(section, option)
print "'%s' value is '%s' under '%s' section" % (option, result, section)
except Exception as ex:
print 'in parse_option exception'
print ex
def test(self):
test = MysqlParserModule()
conf_file = '/root/Desktop/db_config/my_new.cnf'
section = 'client'
option = 'port'
test.parse_option(conf_file, section, option)
if __name__ == '__main__':
test = MysqlParserModule()
test.test()
I am adding validation using schema for CLI that uses docopt, but I cannot seem to get optional to work. I want to validate that:
the input file exists
valid options are used
if the PATH is added that the directory exists.
Here is app so far
"""DVget
Usage:
DVget [-s] FILE [PATH]
Process a file, return data based on selection
and write results to PATH/output-file
Arguments:
FILE specify input file
PATH specify output directory (default: ./)
Options:
-s returns sections
-p returns name-sets
-m returns modules
"""
import os
from docopt import docopt
from schema import Schema, And, Use, Optional, SchemaError
# START OF SCRIPT
if __name__ == "__main__":
arguments = docopt(__doc__, version="0.1")
#print(arguments)
schema = Schema({
'FILE': [Use(open, error='FILE should be readable')],
Optional('PATH'): And(os.path.exists, error='PATH should exist'),
'-': And(str, lambda s: s in ('s', 'p', 'm'))})
try:
arguments = schema.validate(arguments)
# process(arguments)
except SchemaError as e:
exit(e)
running DVget -s "c:\test.txt" gives me the error message 'PATH should exist' even when using Optional in schema and docopt. Any suggestions?