-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Export ansible output to a file #234
Conversation
scripts/qesap/lib/cmds.py
Outdated
log.debug("Ansible process return ret:%d", ret) | ||
playbook_path = command['cmd'][4] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does a selection as simple as "5th element" always works to get the playbook name? Is it not the presence or absence of -vvv
enough to break this mechanism? Is it covered by UT?
If there's a motivation that we are aware that will make it always work we should put it as code comment (I'll do the same in the terraform function later in a next PR)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How this works for the first command that is ansible
and not ansible-playbook
so does not have any playbook file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes have a file for ansible command and it is called ansible.all.log.txt
if ret != 0: | ||
log.error("command:%s returned non zero %d", command, ret) | ||
return Status(ret) | ||
return Status(f"Error at {command}") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this is needed? Is it right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes it is right, just to show the command and the error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But it is not showing the value of ret
anymore
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add e2e tests, terraform already have e2e tests about the .log.txt files
I can also provide test to you if you like.
Please evaluate to add UT that cover this new feature
2de3e5e
to
dc9a436
Compare
scripts/qesap/test/e2e/test.sh
Outdated
|
||
test_step "[test_6.yaml] Run Ansible with playbooks" | ||
rm ansible.*.log.txt || echo "Nothing to delete" | ||
cp timbio.yaml "${QESAPROOT}/ansible/playbooks/timbio.yaml" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that as content is mostly the same, none of the 3 new are needed. Please just do:
cp sambuconero.yaml "${QESAPROOT}/ansible/playbooks/timbio.yaml"
cp sambuconero.yaml "${QESAPROOT}/ansible/playbooks/buga.yaml"
cp sambuconero.yaml "${QESAPROOT}/ansible/playbooks/purace.yaml"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point thx
scripts/qesap/test/e2e/test.sh
Outdated
cp purace.yaml "${QESAPROOT}/ansible/playbooks/purace.yaml" | ||
qesap.py -b ${QESAPROOT} -c test_6.yaml ansible || test_die "test_6.yaml fail on ansible" | ||
ansible_logs_number=$(find . -type f -name "ansible.*.log.txt" | wc -l) | ||
[[ $ansible_logs_number -eq 4 ]] || test_die "ansible .log.txt are not 3 files but has ${ansible_logs_number}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[[ $ansible_logs_number -eq 4 ]] || test_die "ansible .log.txt are not 3 files but has ${ansible_logs_number}" | |
# 3 playbooks plus a log file for the initial ansible (not ansible-playbook) call | |
[[ $ansible_logs_number -eq 4 ]] || test_die "ansible .log.txt are not 4 files but has ${ansible_logs_number}" |
@@ -187,4 +187,4 @@ def test_cmd_terraform(subprocess_run, tmpdir): | |||
|
|||
assert ret == 0 | |||
|
|||
subprocess_run.assert_has_calls(calls) | |||
subprocess_run.assert_has_calls(calls) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
last line missing
@mock.patch("lib.process_manager.subprocess_run") | ||
def test_cmd_ansible(subprocess_run, tmpdir): | ||
''' | ||
This test checks if the ansible execution generates log files |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which part of the test checks if the ansible execution generates log files
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
refactored
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test checks if the ansible execution generates log files | |
Given an ansible-playbook command line and some string to simulate an ansible stdout, this test verify that the function write them to file. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, any additional fixes can be applied later
return Status("ok") | ||
|
||
|
||
def export_ansible_output(command, out): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def export_ansible_output(command, out): | |
def export_ansible_output(command, out): | |
""" | |
Utility function that get the ansible command line and the command output. | |
Function calculate the log name by extracting the ansible playbook name from the command line. | |
Function take the content of out and write it to a file in the current directory | |
""" |
''' | ||
|
||
test_dir = os.getcwd() | ||
test_file = os.path.join(test_dir, 'ansible.testAll.log.txt') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the purpose of
'test1':'test_value1', 'test2':'test_value2', 'test3':'test_value3', 'test4':'test_value4',
?
Is it to check that test_export_ansible_output
ignore them?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes
test_file = os.path.join(test_dir, 'ansible.testAll.log.txt') | ||
command_to_sent = {'test1':'test_value1', 'test2':'test_value2', 'test3':'test_value3', 'test4':'test_value4', | ||
'cmd':['/tmp/exec_venv/bin/ansible', '-vvvv', '-i', '/root/qe-sap-deployment/terraform/aws/inventory.yaml', | ||
'testAll', '-a', '--ssh-extra-args="-l cloudadmin -o UpdateHostKeys=yes -o StrictHostKeyChecking=accept-new"']} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'testAll', '-a', '--ssh-extra-args="-l cloudadmin -o UpdateHostKeys=yes -o StrictHostKeyChecking=accept-new"']} | |
'/some/immaginary/path/testAll.yaml', '-a', '--ssh-extra-args="-l cloudadmin -o UpdateHostKeys=yes -o StrictHostKeyChecking=accept-new"']} |
command_to_sent = {'test1':'test_value1', 'test2':'test_value2', 'test3':'test_value3', 'test4':'test_value4', | ||
'cmd':['/tmp/exec_venv/bin/ansible', '-vvvv', '-i', '/root/qe-sap-deployment/terraform/aws/inventory.yaml', | ||
'testAll', '-a', '--ssh-extra-args="-l cloudadmin -o UpdateHostKeys=yes -o StrictHostKeyChecking=accept-new"']} | ||
ansible_output = """DEBUG OUTPUT: ansible [core 2.13.5] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does no matter at all for export_ansible_output
what is the content of this string, but generally speaking content will not be like this but
ansible_output = """ansible [core 2.13.5]
config file = None
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /tmp/exec_venv/lib64/python3.11/site-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /tmp/exec_venv/bin/ansible
python version = 3.11.5 (main, Sep 06 2023, 11:21:05) [GCC]
jinja version = 3.1.4
libyaml = True"""
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes indeed doesn't matter
export_ansible_output(command_to_sent, ansible_output) | ||
|
||
assert os.path.isfile(test_file), f"Ansible output file {test_file} was not created." | ||
os.remove(test_file) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it is ok that you remove the file after the test. Generally speaking I do no t like when a UT write file around. I like to try to always let the UT to confine the place to read and write the FS to https://docs.pytest.org/en/7.1.x/how-to/tmp_path.html
This comment is a generic suggestion and we cal look at this later.
f712813
to
a1d2348
Compare
log = logging.getLogger(__name__) | ||
|
||
|
||
def test_export_ansible_output(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here you can see the execution of the single new test in this file [test_qesap_lib_cmd_ansible.py](https://github.com/SUSE/qe-sap-deployment/pull/234/files#diff-64bd1de2060c01d07e450f4bb3a2ce75e4827907a733d818310885ab8ccced55)
--> log https://github.com/SUSE/qe-sap-deployment/actions/runs/9481063837/job/26123325056#step:7:33
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And here an execution example with verbose mode on
https://github.com/SUSE/qe-sap-deployment/actions/runs/9481063837/job/26123325056#step:8:838
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Let qesap.py to always write ansible log to a separate file like terraform already did.
VR: https://openqaworker15.qa.suse.cz/tests/285632#
Ticket: https://jira.suse.com/browse/TEAM-6844