From 703142a39821b2918d53afbe5503aaf4b2560755 Mon Sep 17 00:00:00 2001 From: Mohit Singh Tomar Date: Wed, 26 Apr 2023 19:08:36 +0530 Subject: [PATCH 1/7] Added Fixture for collecting Support Bundle Signed-off-by: Mohit Singh Tomar --- tests/system/python/conftest.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/system/python/conftest.py b/tests/system/python/conftest.py index c71caf924f..f8c7f6e998 100644 --- a/tests/system/python/conftest.py +++ b/tests/system/python/conftest.py @@ -668,6 +668,23 @@ def _disable_sch(fledge_url, sch_name): return _disable_sch + +@pytest.fixture(scope="session", autouse=True) +def collect_support_bundle(): + def _collect_support_bundle(fledge_url): + conn = http.client.HTTPConnection(fledge_url) + conn.request("POST", "fledge/support") + r = conn.getresponse() + assert 200 == r.status + r = r.read().decode() + jdoc = json.loads(r) + assert jdoc["bundle created"] + + subprocess.run(["cp {} $FLEDGE_ROOT/.".format(jdoc["bundle created"])], shell=True, check=True) + + return _collect_support_bundle + + def pytest_addoption(parser): parser.addoption("--storage-plugin", action="store", default="sqlite", help="Database plugin to use for tests") From 1e933b17651a71fcaf2d40db7c9d869c672f177b Mon Sep 17 00:00:00 2001 From: Mohit Singh Tomar Date: Thu, 27 Apr 2023 15:42:55 +0530 Subject: [PATCH 2/7] Fixed code to generate support bundle Signed-off-by: Mohit Singh Tomar --- tests/system/python/conftest.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/tests/system/python/conftest.py b/tests/system/python/conftest.py index f8c7f6e998..602b993ab5 100644 --- a/tests/system/python/conftest.py +++ b/tests/system/python/conftest.py @@ -668,21 +668,18 @@ def _disable_sch(fledge_url, sch_name): return _disable_sch - -@pytest.fixture(scope="session", autouse=True) -def collect_support_bundle(): - def _collect_support_bundle(fledge_url): - conn = http.client.HTTPConnection(fledge_url) - conn.request("POST", "fledge/support") +@pytest.fixture(autouse=True) +def collect_support_bundle(request): + def _collect_support_bundle(): + 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) assert jdoc["bundle created"] - - subprocess.run(["cp {} $FLEDGE_ROOT/.".format(jdoc["bundle created"])], shell=True, check=True) - - return _collect_support_bundle + url = request.config.getoption("--fledge-url") + request.addfinalizer(_collect_support_bundle) def pytest_addoption(parser): @@ -913,7 +910,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") From 9ffc533e1b1725cd52d7570ebb0330cff39ed537 Mon Sep 17 00:00:00 2001 From: Mohit Singh Tomar Date: Thu, 27 Apr 2023 17:26:50 +0530 Subject: [PATCH 3/7] Added code for collecting support bundle at end of evry test function and save it at PROJECT_ROOT directory Signed-off-by: Mohit Singh Tomar --- tests/system/python/conftest.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/system/python/conftest.py b/tests/system/python/conftest.py index 602b993ab5..c4b6791086 100644 --- a/tests/system/python/conftest.py +++ b/tests/system/python/conftest.py @@ -668,7 +668,7 @@ def _disable_sch(fledge_url, sch_name): return _disable_sch -@pytest.fixture(autouse=True) +@pytest.fixture(scope="function", autouse=True) def collect_support_bundle(request): def _collect_support_bundle(): conn = http.client.HTTPConnection("{}".format(url)) @@ -678,6 +678,9 @@ def _collect_support_bundle(): r = r.read().decode() jdoc = json.loads(r) assert jdoc["bundle created"] + PROJECT_ROOT = subprocess.getoutput("git rev-parse --show-toplevel") + subprocess.run(["mkdir -p {0}/support/ && cp -r {1} {0}/support/.".format(PROJECT_ROOT, jdoc["bundle created"])], shell=True, check=True) + url = request.config.getoption("--fledge-url") request.addfinalizer(_collect_support_bundle) From 548d2bf243f280be52997358eda84c3a0a95d527 Mon Sep 17 00:00:00 2001 From: Mohit Singh Tomar Date: Fri, 28 Apr 2023 18:23:00 +0530 Subject: [PATCH 4/7] Added code to handle failure of fledge when it is down Signed-off-by: Mohit Singh Tomar --- tests/system/python/conftest.py | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/tests/system/python/conftest.py b/tests/system/python/conftest.py index c4b6791086..9e74cc4122 100644 --- a/tests/system/python/conftest.py +++ b/tests/system/python/conftest.py @@ -18,7 +18,7 @@ from urllib.parse import quote from pathlib import Path import pytest - +import time __author__ = "Vaibhav Singhal" __copyright__ = "Copyright (c) 2019 Dianomic Systems" @@ -671,15 +671,25 @@ def _disable_sch(fledge_url, sch_name): @pytest.fixture(scope="function", autouse=True) def collect_support_bundle(request): def _collect_support_bundle(): - 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) - assert jdoc["bundle created"] PROJECT_ROOT = subprocess.getoutput("git rev-parse --show-toplevel") - subprocess.run(["mkdir -p {0}/support/ && cp -r {1} {0}/support/.".format(PROJECT_ROOT, jdoc["bundle created"])], shell=True, check=True) + try: + 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) + 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) + except Exception as e: + print("\n Failed to get Support Bundle from Fledge REST API due to {}".format(str(e))) + print("Copying syslog") + OS_NAME = subprocess.getoutput('cat /etc/os-release | grep -E ^NAME= | cut -d= -f2 | tr -d \\" ') + if OS_NAME == 'Ubuntu': + 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) url = request.config.getoption("--fledge-url") request.addfinalizer(_collect_support_bundle) From 486cefb9423332116bbf77107e9169cdaa797caa Mon Sep 17 00:00:00 2001 From: Mohit Singh Tomar Date: Tue, 2 May 2023 15:31:13 +0530 Subject: [PATCH 5/7] Feedback chnages Signed-off-by: Mohit Singh Tomar --- tests/system/python/conftest.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/tests/system/python/conftest.py b/tests/system/python/conftest.py index 59d27330fa..6dc453e3dd 100644 --- a/tests/system/python/conftest.py +++ b/tests/system/python/conftest.py @@ -19,6 +19,7 @@ from pathlib import Path import pytest import time +from helpers import utils __author__ = "Vaibhav Singhal" __copyright__ = "Copyright (c) 2019 Dianomic Systems" @@ -672,25 +673,22 @@ def _disable_sch(fledge_url, sch_name): @pytest.fixture(scope="function", autouse=True) def collect_support_bundle(request): def _collect_support_bundle(): - PROJECT_ROOT = subprocess.getoutput("git rev-parse --show-toplevel") + PROJECT_ROOT = Path(__file__).absolute().parent.parent.parent.parent + try: - 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) + jdoc = utils.post_request(url, "/fledge/support", None) 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) + 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) except Exception as e: print("\n Failed to get Support Bundle from Fledge REST API due to {}".format(str(e))) print("Copying syslog") - OS_NAME = subprocess.getoutput('cat /etc/os-release | grep -E ^NAME= | cut -d= -f2 | tr -d \\" ') - if OS_NAME == 'Ubuntu': - LOG_FILE = "/var/log/syslog" - elif OS_NAME == 'CentOS Stream': + if pytest.PKG_MGR == 'yum': 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) + else: + LOG_FILE = "/var/log/syslog" + 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) url = request.config.getoption("--fledge-url") request.addfinalizer(_collect_support_bundle) From 420d4dcefca1a7687415557e9829c20c87d6294a Mon Sep 17 00:00:00 2001 From: Mohit Singh Tomar Date: Thu, 4 May 2023 13:19:29 +0530 Subject: [PATCH 6/7] Feedback changes Signed-off-by: Mohit Singh Tomar --- tests/system/python/conftest.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/tests/system/python/conftest.py b/tests/system/python/conftest.py index 6dc453e3dd..6593c9f47f 100644 --- a/tests/system/python/conftest.py +++ b/tests/system/python/conftest.py @@ -671,26 +671,25 @@ def _disable_sch(fledge_url, sch_name): @pytest.fixture(scope="function", autouse=True) -def collect_support_bundle(request): +def collect_support_bundle(request, fledge_url): def _collect_support_bundle(): PROJECT_ROOT = Path(__file__).absolute().parent.parent.parent.parent try: - jdoc = utils.post_request(url, "/fledge/support", None) + jdoc = utils.post_request(fledge_url, "/fledge/support", None) assert jdoc["bundle created"] - 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) + 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) except Exception as e: - print("\n Failed to get Support Bundle from Fledge REST API due to {}".format(str(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" - else: - LOG_FILE = "/var/log/syslog" - 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) - - url = request.config.getoption("--fledge-url") + 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) + subprocess.run([copy_to_cmd], shell=True, check=True) + request.addfinalizer(_collect_support_bundle) From eda71b871ad01a7769808a755f052cae5b71d0ad Mon Sep 17 00:00:00 2001 From: Mohit Singh Tomar Date: Wed, 9 Aug 2023 18:26:31 +0530 Subject: [PATCH 7/7] Feedback changes Signed-off-by: Mohit Singh Tomar --- tests/system/python/conftest.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/system/python/conftest.py b/tests/system/python/conftest.py index 60e56bfde8..79a3de067f 100644 --- a/tests/system/python/conftest.py +++ b/tests/system/python/conftest.py @@ -810,7 +810,7 @@ def _collect_support_bundle(): try: jdoc = utils.post_request(fledge_url, "/fledge/support", None) assert jdoc["bundle created"] - copy_to_cmd = "mkdir -p {0}/support/ && cp -r {1} {0}/support".format(PROJECT_ROOT, jdoc['bundle created']) + 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))) @@ -819,7 +819,7 @@ def _collect_support_bundle(): 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) + 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) request.addfinalizer(_collect_support_bundle)