-
Notifications
You must be signed in to change notification settings - Fork 50
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
base: develop
Are you sure you want to change the base?
Changes from 9 commits
703142a
1e933b1
9ffc533
432ff12
548d2bf
ca2a784
486cefb
5acbc86
61be221
420d4dc
bf0e187
cfd1cd3
ff08c28
eda71b8
db66585
3c12c2e
b04a5b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,8 @@ | |
from urllib.parse import quote | ||
from pathlib import Path | ||
import pytest | ||
import time | ||
from helpers import utils | ||
|
||
__author__ = "Vaibhav Singhal" | ||
__copyright__ = "Copyright (c) 2019 Dianomic Systems" | ||
|
@@ -668,6 +670,30 @@ def _disable_sch(fledge_url, sch_name): | |
return _disable_sch | ||
|
||
|
||
@pytest.fixture(scope="function", autouse=True) | ||
def collect_support_bundle(request): | ||
def _collect_support_bundle(): | ||
PROJECT_ROOT = Path(__file__).absolute().parent.parent.parent.parent | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try this code snippet to get PROJECT_ROOT
|
||
|
||
try: | ||
jdoc = utils.post_request(url, "/fledge/support", None) | ||
assert jdoc["bundle created"] | ||
ashish-jabble marked this conversation as resolved.
Show resolved
Hide resolved
|
||
copy_to = "mkdir -p {0}/support/ && cp -r {1} {0}/support".format(PROJECT_ROOT, jdoc['bundle created']) | ||
subprocess.run(["{}/.".format(copy_to)], shell=True, check=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
except Exception as e: | ||
print("\n Failed to get Support Bundle from Fledge REST API due to {}".format(str(e))) | ||
ashish-jabble marked this conversation as resolved.
Show resolved
Hide resolved
|
||
print("Copying syslog") | ||
if pytest.PKG_MGR == 'yum': | ||
LOG_FILE = "/var/log/messages" | ||
else: | ||
LOG_FILE = "/var/log/syslog" | ||
ashish-jabble marked this conversation as resolved.
Show resolved
Hide resolved
|
||
copy_to = "mkdir -p {0}/support/ && cp -r {1} {0}/support".format(PROJECT_ROOT, LOG_FILE) | ||
subprocess.run(["{0}/syslog_{1}".format(copy_to, time.strftime("%Y_%m_%d_%H_%M_%S"))], shell=True, check=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
url = request.config.getoption("--fledge-url") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use fledge_url def for that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried fledge_url def but that was not working, So I have used this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should be able to use another fixture as arg collect_support_bundle(request, fledge_url): |
||
request.addfinalizer(_collect_support_bundle) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we need this? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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") | ||
|
@@ -918,7 +944,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") | ||
|
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.
You typically use autouse fixtures when you want to use them for setup/teardown only.
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, 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.