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

FOGL-7699 Add a pytest fixture in conftest.py which collects support bundle of fledge #1059

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions tests/system/python/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import pytest
from helpers import utils


__author__ = "Vaibhav Singhal"
__copyright__ = "Copyright (c) 2019 Dianomic Systems"
__license__ = "Apache 2.0"
Expand Down Expand Up @@ -803,6 +802,29 @@ def _disable_sch(fledge_url, sch_name):
return _disable_sch


@pytest.fixture(scope="function", autouse=True)
Copy link
Member

Choose a reason for hiding this comment

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

You typically use autouse fixtures when you want to use them for setup/teardown only.

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, I have used an autouse fixture with a function-limited scope. This ensures that it is executed at the end of every test function/case in any system test, allowing it to collect a support bundle before executing next test function/case.

def collect_support_bundle(request, fledge_url):
def _collect_support_bundle():
PROJECT_ROOT = Path(__file__).absolute().parent.parent.parent.parent
Copy link
Member

Choose a reason for hiding this comment

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

This is wrong practice. We should avoid this and maybe use Pathlib which is a native Python library for handling files and paths and also provides a making code more readable and maintainable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We are currently utilizing the Pathlib library, given that the Path() function is associated with this library. As an alternative approach, we could use the "git rev-parse --show-toplevel" command to pinpoint the precise path of the PROJECT_ROOT. However, I will explore more optimal methods to determine this path.

Copy link
Member

Choose a reason for hiding this comment

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

Try this code snippet to get PROJECT_ROOT

import pathlib
PROJECT_NAME="tests"
PROJECT_ROOT = str(pathlib.Path().resolve()).split(PROJECT_NAME)[0]
print(PROJECT_ROOT)


try:
jdoc = utils.post_request(fledge_url, "/fledge/support", None)
assert jdoc["bundle created"]
ashish-jabble marked this conversation as resolved.
Show resolved Hide resolved
copy_to_cmd = f"mkdir -p {PROJECT_ROOT}/support/ && cp -r {jdoc['bundle created']} {PROJECT_ROOT}/support/"
subprocess.run([copy_to_cmd], shell=True, check=True)
except Exception as e:
print("\n Failed to get Support Bundle. \n {}".format(str(e)))
print("Copying syslog")
log_file = "/var/log/syslog"
if pytest.PKG_MGR == 'yum':
log_file = "/var/log/messages"
ts = time.strftime("%Y_%m_%d_%H_%M_%S")
copy_to_cmd = f"mkdir -p {PROJECT_ROOT}/support/logs/ && grep -i 'fledge' {log_file} > {PROJECT_ROOT}/support/logs/syslog_{ts}"
subprocess.run([copy_to_cmd], shell=True, check=True)
Comment on lines +817 to +823
Copy link
Member

Choose a reason for hiding this comment

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

Fixture should have flexibility to collect support bundle even in success case as well.


request.addfinalizer(_collect_support_bundle)
Copy link
Member

Choose a reason for hiding this comment

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

why do we need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need addfinalizer() function to execute this fixture at end of each test functions.

Copy link
Member

Choose a reason for hiding this comment

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

I think we need only at end of test module not each function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This requirement arises due to the common practice in system tests, where each test function resets the fledge before testing starts. This reset purges older data, regardless of whether the test passes or not. Consequently, the status of the fledge at that specific time remains unknown to us.



def pytest_addoption(parser):
parser.addoption("--storage-plugin", action="store", default="sqlite",
help="Database plugin to use for tests")
Expand Down Expand Up @@ -1042,7 +1064,6 @@ def asset_name(request):
def fledge_url(request):
return request.config.getoption("--fledge-url")


@pytest.fixture
def wait_time(request):
return request.config.getoption("--wait-time")
Expand Down