FOGL-7699 Add a pytest fixture in conftest.py which collects support bundle of fledge#1059
FOGL-7699 Add a pytest fixture in conftest.py which collects support bundle of fledge#1059Mohit04tomar wants to merge 17 commits intodevelopfrom
Conversation
Signed-off-by: Mohit Singh Tomar <M.singh19994@gmail.com>
Signed-off-by: Mohit Singh Tomar <M.singh19994@gmail.com>
… and save it at PROJECT_ROOT directory Signed-off-by: Mohit Singh Tomar <M.singh19994@gmail.com>
|
I have ran this Jenkins Job with changes --> http://jenkins.dianomic.com:8080/view/PI%20stuff/job/fledge_packages_pi_web_api/517/ |
Signed-off-by: Mohit Singh Tomar <M.singh19994@gmail.com>
|
This is the latest job build of this PR --> http://jenkins.dianomic.com:8080/view/PI%20stuff/job/fledge_packages_pi_web_api/519/ |
tests/system/python/conftest.py
Outdated
| conn = http.client.HTTPConnection("{}".format(url)) | ||
| conn.request("POST", "/fledge/support") | ||
| r = conn.getresponse() | ||
| assert 200 == r.status | ||
| r = r.read().decode() | ||
| jdoc = json.loads(r) |
There was a problem hiding this comment.
def post_request from utils.py can be used instead of this.
There was a problem hiding this comment.
for that I have to import utils into conftest file
There was a problem hiding this comment.
that is good, we should use utils def
tests/system/python/conftest.py
Outdated
| r = r.read().decode() | ||
| jdoc = json.loads(r) | ||
| assert jdoc["bundle created"] | ||
| subprocess.run(["mkdir -p {0}/support/ && cp -r {1} {0}/support/.".format(PROJECT_ROOT, jdoc["bundle created"])], shell=True, check=True) |
There was a problem hiding this comment.
In case of package based system tests, what will be the PROJECT_ROOT? Are support bundles stored in PROJECT_ROOT path?
What is the value for jdoc["bundle created"] ?
There was a problem hiding this comment.
PROJECT_ROOT is the directory that contains the tests to be executed.
Support Bundle store in PROJECT_ROOT/support/ directory and jdoc["bundle created"] is FLEDGE_ROOT/data/support/support_bundle.tar.gz
tests/system/python/conftest.py
Outdated
| @pytest.fixture(scope="function", autouse=True) | ||
| def collect_support_bundle(request): | ||
| def _collect_support_bundle(): | ||
| PROJECT_ROOT = subprocess.getoutput("git rev-parse --show-toplevel") |
There was a problem hiding this comment.
Let's keep the old way, We can fix it later at all places with other PR
tests/system/python/conftest.py
Outdated
| conn = http.client.HTTPConnection("{}".format(url)) | ||
| conn.request("POST", "/fledge/support") | ||
| r = conn.getresponse() | ||
| assert 200 == r.status | ||
| r = r.read().decode() | ||
| jdoc = json.loads(r) |
There was a problem hiding this comment.
that is good, we should use utils def
tests/system/python/conftest.py
Outdated
| if OS_NAME == 'Ubuntu': | ||
| LOG_FILE = "/var/log/syslog" | ||
| elif OS_NAME == 'CentOS Stream': | ||
| LOG_FILE = "/var/log/messages" |
There was a problem hiding this comment.
We have PKG_MGR to identify platform
tests/system/python/conftest.py
Outdated
| LOG_FILE = "/var/log/syslog" | ||
| elif OS_NAME == 'CentOS Stream': | ||
| LOG_FILE = "/var/log/messages" | ||
| subprocess.run(["mkdir -p {0}/support/ && cp {1} {0}/support/syslog_{2}".format(PROJECT_ROOT, LOG_FILE, time.strftime("%Y_%m_%d_%H_%M_%S"))], shell=True, check=True) |
There was a problem hiding this comment.
let's put {0}/support/syslog_{2} in save_to to copy_to variable to keep this statement short
| subprocess.run(["mkdir -p {0}/support/ && cp {1} {0}/support/syslog_{2}".format(PROJECT_ROOT, LOG_FILE, time.strftime("%Y_%m_%d_%H_%M_%S"))], shell=True, check=True) | ||
|
|
||
| url = request.config.getoption("--fledge-url") | ||
| request.addfinalizer(_collect_support_bundle) |
There was a problem hiding this comment.
We need addfinalizer() function to execute this fixture at end of each test functions.
There was a problem hiding this comment.
I think we need only at end of test module not each function.
There was a problem hiding this comment.
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.
tests/system/python/conftest.py
Outdated
| LOG_FILE = "/var/log/messages" | ||
| subprocess.run(["mkdir -p {0}/support/ && cp {1} {0}/support/syslog_{2}".format(PROJECT_ROOT, LOG_FILE, time.strftime("%Y_%m_%d_%H_%M_%S"))], shell=True, check=True) | ||
|
|
||
| url = request.config.getoption("--fledge-url") |
There was a problem hiding this comment.
use fledge_url def for that
There was a problem hiding this comment.
I tried fledge_url def but that was not working, So I have used this request.config.getoption("--fledge-url")
There was a problem hiding this comment.
We should be able to use another fixture as arg collect_support_bundle(request, fledge_url):
Signed-off-by: Mohit Singh Tomar <M.singh19994@gmail.com>
|
The Jenkins job run on latest commit --> http://jenkins.dianomic.com:8080/view/PI%20stuff/job/fledge_packages_pi_web_api/521/ |
| subprocess.run(["mkdir -p {0}/support/ && cp {1} {0}/support/syslog_{2}".format(PROJECT_ROOT, LOG_FILE, time.strftime("%Y_%m_%d_%H_%M_%S"))], shell=True, check=True) | ||
|
|
||
| url = request.config.getoption("--fledge-url") | ||
| request.addfinalizer(_collect_support_bundle) |
There was a problem hiding this comment.
I think we need only at end of test module not each function.
tests/system/python/conftest.py
Outdated
| LOG_FILE = "/var/log/messages" | ||
| subprocess.run(["mkdir -p {0}/support/ && cp {1} {0}/support/syslog_{2}".format(PROJECT_ROOT, LOG_FILE, time.strftime("%Y_%m_%d_%H_%M_%S"))], shell=True, check=True) | ||
|
|
||
| url = request.config.getoption("--fledge-url") |
There was a problem hiding this comment.
We should be able to use another fixture as arg collect_support_bundle(request, fledge_url):
tests/system/python/conftest.py
Outdated
| 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.
copy_to_cmd = "mkdir -p {0}/support/ && cp -r {1} {0}/support/.".format(PROJECT_ROOT, jdoc['bundle created'])
subprocess.run([copy_to_cmd], shell=True, check=True)
tests/system/python/conftest.py
Outdated
| 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.
ts = time.strftime("%Y_%m_%d_%H_%M_%S")
copy_to_cmd = "mkdir -p {0}/support/logs/ && cp {1} {0}/support/logs/syslog_{1}".format(PROJECT_ROOT, log_file, ts)
subprocess.run([copy_to_cmd], shell=True, check=True)
Signed-off-by: Mohit Singh Tomar <M.singh19994@gmail.com>
| return _disable_sch | ||
|
|
||
|
|
||
| @pytest.fixture(scope="function", autouse=True) |
There was a problem hiding this comment.
You typically use autouse fixtures when you want to use them for setup/teardown only.
There was a problem hiding this comment.
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.
| @pytest.fixture(scope="function", autouse=True) | ||
| def collect_support_bundle(request, fledge_url): | ||
| def _collect_support_bundle(): | ||
| PROJECT_ROOT = Path(__file__).absolute().parent.parent.parent.parent |
There was a problem hiding this comment.
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.
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.
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)
tests/system/python/conftest.py
Outdated
| 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 = "mkdir -p {0}/support/logs/ && cp {1} {0}/support/logs/syslog_{2}".format(PROJECT_ROOT, log_file, ts) |
There was a problem hiding this comment.
IN case of exception we should only get the Fledge related logs. Not the full dump from path.
There was a problem hiding this comment.
ok, I'll update the code to only get logs related to Fledge.
Signed-off-by: Mohit Singh Tomar <M.singh19994@gmail.com>
|
Below are the jobs that I run on latest commit(3c12c2e): |
| 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) |
There was a problem hiding this comment.
Fixture should have flexibility to collect support bundle even in success case as well.
|
@Mohit04tomar Is there anything remaining for this PR, or is it outdated and can be closed? |
Any updates? |
No description provided.