Skip to content
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

Merged
merged 1 commit into from
Jun 12, 2024
Merged

Conversation

hadeskun
Copy link
Contributor

@hadeskun hadeskun commented May 27, 2024

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

scripts/qesap/lib/cmds.py Outdated Show resolved Hide resolved
log.debug("Ansible process return ret:%d", ret)
playbook_path = command['cmd'][4]
Copy link
Collaborator

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)

Copy link
Collaborator

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?

Copy link
Contributor Author

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

scripts/qesap/lib/cmds.py Outdated Show resolved Hide resolved
scripts/qesap/lib/cmds.py Outdated Show resolved Hide resolved
if ret != 0:
log.error("command:%s returned non zero %d", command, ret)
return Status(ret)
return Status(f"Error at {command}")
Copy link
Collaborator

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?

Copy link
Contributor Author

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

Copy link
Collaborator

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

Copy link
Collaborator

@mpagot mpagot left a 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

@hadeskun hadeskun force-pushed the exportAnsible branch 6 times, most recently from 2de3e5e to dc9a436 Compare June 7, 2024 14:53

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"
Copy link
Collaborator

@mpagot mpagot Jun 10, 2024

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"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point thx

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}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
[[ $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)
Copy link
Collaborator

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
Copy link
Collaborator

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 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactored

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Copy link
Collaborator

@mpagot mpagot left a 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):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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')
Copy link
Collaborator

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?

Copy link
Contributor Author

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"']}
Copy link
Collaborator

@mpagot mpagot Jun 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'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]
Copy link
Collaborator

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"""

Copy link
Contributor Author

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)
Copy link
Collaborator

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.

@hadeskun hadeskun force-pushed the exportAnsible branch 2 times, most recently from f712813 to a1d2348 Compare June 12, 2024 10:06
log = logging.getLogger(__name__)


def test_export_ansible_output():
Copy link
Collaborator

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

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

@lpalovsky lpalovsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@lpalovsky lpalovsky merged commit c48019e into SUSE:main Jun 12, 2024
4 checks passed
@mpagot mpagot mentioned this pull request Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants