Skip to contentSkip to conversation

testimage: add failed test post actions and fetch more data

PATCH Superseded Alexis Lothoré 2 participants 9 messages Feb 20, 2024 – Feb 21, 2024

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

cover testimage: add failed test post actions and fetch more data

Hello,
this small series is related to some disk space issue observed by the SWAT team (see [1]) which eventually leads to tests failure. In order to help diagnose those issues, I propose to enrich the retrieved artifacts with some additional data, like disk usage on target and on host (in case the target is a virtualized guest)

To do so, this series first reworks a bit actions executed on failed tests to allow declaring a sequence of actions. It then introduces two small actions which basically run "df" on target and host. I chose to store those stats in dedicated files, thinking it is better than "contaminating" tests steps logs with those very specific data.

[1] https://bugzilla.yoctoproject.org/show_bug.cgi?id=15220

Alexis Lothoré (4):
testimage: create a list of failed test post actions
testimage: isolate artifacts directory creation in dedicated post
action
testimage: add target disk usage stat as post action
testimage: add host disk usage stat as post action

 .../failed-tests-post-actions.bbclass         | 94 +++++++++++++++++++
 meta/classes-recipe/testimage.bbclass         | 42 +--------
 2 files changed, 96 insertions(+), 40 deletions(-)
 create mode 100644 meta/classes-recipe/failed-tests-post-actions.bbclass
Signature · 1 line
2.43.1
#1 lore raw

PATCH 1/4 testimage: create a list of failed test post actions

From: Alexis Lothoré <[email protected]>

testimage is able to detect whenever a test run leads to some tests failing, and execute some actions in this case. The only action currently defined in such case is to retrieve artifacts from the target under test, as listed in TESTIMAGE_FAILED_QA_ARTIFACTS

In order to be able to add multiple actions, define a central function to gather all "post actions" to run whenever a test has failed (run_failed_tests_post_actions). This function contains a table listing all functions to be called whenever a test fails. Any function in this table will be provided with bitbake internal data dictionary ("d") and the current runtime testing context ("tc"). Isolate all this feature in a dedicated bbclass file inherited by testimage.
This patch does not bring any functional change.

---
 .../failed-tests-post-actions.bbclass         | 64 +++++++++++++++++++
 meta/classes-recipe/testimage.bbclass         | 42 +-----------
 2 files changed, 66 insertions(+), 40 deletions(-)
 create mode 100644 meta/classes-recipe/failed-tests-post-actions.bbclass
meta/classes-recipe/failed-tests-post-actions.bbclass new file +64 -0
new file mode 100644
index 000000000000..7c7d3391298f
@@ -0,0 +1,64 @@
+#
+# Copyright OpenEmbedded Contributors
+#
+# SPDX-License-Identifier: MIT
+#
+
+
+##################################################################
+# Artifacts retrieval
+##################################################################
+
+def get_artifacts_list(target, raw_list):
+    result = []
+    # Passed list may contains patterns in paths, expand them directly on target
+    for raw_path in raw_list.split():
+        cmd = f"for p in {raw_path}; do if [ -e $p ]; then echo $p; fi; done"
+        try:
+            status, output = target.run(cmd)
+            if status != 0 or not output:
+                raise Exception()
+            result += output.split()
+        except:
+            bb.note(f"No file/directory matching path {raw_path}")
+
+    return result
+
+def retrieve_test_artifacts(target, artifacts_list, target_dir):
+    import shutil
+
+    local_artifacts_dir = os.path.join(target_dir, "artifacts")
+    if os.path.isdir(local_artifacts_dir):
+        shutil.rmtree(local_artifacts_dir)
+
+    os.makedirs(local_artifacts_dir)
+    for artifact_path in artifacts_list:
+        if not os.path.isabs(artifact_path):
+            bb.warn(f"{artifact_path} is not an absolute path")
+            continue
+        try:
+            dest_dir = os.path.join(local_artifacts_dir, os.path.dirname(artifact_path[1:]))
+            os.makedirs(dest_dir, exist_ok=True)
+            target.copyFrom(artifact_path, dest_dir)
+        except Exception as e:
+            bb.warn(f"Can not retrieve {artifact_path} from test target: {e}")
+
+def list_and_fetch_failed_tests_artifacts(d, tc):
+    artifacts_list = get_artifacts_list(tc.target, d.getVar("TESTIMAGE_FAILED_QA_ARTIFACTS"))
+    if not artifacts_list:
+        bb.warn("Could not load artifacts list, skip artifacts retrieval")
+    else:
+        retrieve_test_artifacts(tc.target, artifacts_list, get_testimage_json_result_dir(d))
+
+
+##################################################################
+# General post actions runner
+##################################################################
+
+def run_failed_tests_post_actions(d, tc):
+    post_actions=[
+        list_and_fetch_failed_tests_artifacts
+    ]
+
+    for action in post_actions:
+        action(d, tc)
meta/classes-recipe/testimage.bbclass +2 -40

60 lines not on this page · raw message

Signature · 1 line
2.43.1
#2 lore raw

Re: [OE-Core][PATCH 1/4] testimage: create a list of failed test post actions

Quoted 98 lines · Alexis Lothoré via lists.openembedded.org

From: Alexis Lothoré <[email protected]>

testimage is able to detect whenever a test run leads to some tests failing, and execute some actions in this case. The only action currently
defined in such case is to retrieve artifacts from the target under test,
as listed in TESTIMAGE_FAILED_QA_ARTIFACTS

In order to be able to add multiple actions, define a central function to
gather all "post actions" to run whenever a test has failed (run_failed_tests_post_actions). This function contains a table listing all
functions to be called whenever a test fails. Any function in this table
will be provided with bitbake internal data dictionary ("d") and the current runtime testing context ("tc"). Isolate all this feature in a dedicated bbclass file inherited by testimage.
This patch does not bring any functional change.

---
 .../failed-tests-post-actions.bbclass         | 64
+++++++++++++++++++
 meta/classes-recipe/testimage.bbclass         | 42 +-----------

 2 files changed, 66 insertions(+), 40 deletions(-)

 create mode 100644 meta/classes-recipe/failed-tests-post- actions.bbclass

meta/classes-recipe/failed-tests-post-actions.bbclass new file +14 -0
new file mode 100644
index 000000000000..7c7d3391298f
@@ -0,0 +1,64 @@
+#
+# Copyright OpenEmbedded Contributors
+#
+# SPDX-License-Identifier: MIT
+#
+
+
+##################################################################
+# Artifacts retrieval
+##################################################################
+
+def get_artifacts_list(target, raw_list):
+    result = []
+    # Passed list may contains patterns in paths, expand them

directly on target
+    for raw_path in raw_list.split():
+        cmd = f"for p in {raw_path}; do if [ -e $p ]; then echo $p; fi; done"
+        try:
+            status, output = target.run(cmd)
+            if status != 0 or not output:
+                raise Exception()
+            result += output.split()
+        except:
+            bb.note(f"No file/directory matching path {raw_path}")

meta/classes-recipe/failed-tests-post-actions.bbclass new file +17 -0
+
+    return result
+
+def retrieve_test_artifacts(target, artifacts_list, target_dir):
+    import shutil
+
+    local_artifacts_dir = os.path.join(target_dir, "artifacts")
+    if os.path.isdir(local_artifacts_dir):
+        shutil.rmtree(local_artifacts_dir)
+
+    os.makedirs(local_artifacts_dir)
+    for artifact_path in artifacts_list:
+        if not os.path.isabs(artifact_path):
+            bb.warn(f"{artifact_path} is not an absolute path")
+            continue
+        try:
+            dest_dir = os.path.join(local_artifacts_dir,

os.path.dirname(artifact_path[1:]))
+            os.makedirs(dest_dir, exist_ok=True)
+            target.copyFrom(artifact_path, dest_dir)
+        except Exception as e:
+            bb.warn(f"Can not retrieve {artifact_path} from test
target: {e}")

meta/classes-recipe/failed-tests-post-actions.bbclass new file +3 -0
+
+def list_and_fetch_failed_tests_artifacts(d, tc):
+    artifacts_list = get_artifacts_list(tc.target,

d.getVar("TESTIMAGE_FAILED_QA_ARTIFACTS"))
+    if not artifacts_list:
+        bb.warn("Could not load artifacts list, skip artifacts retrieval")
+    else:
+        retrieve_test_artifacts(tc.target, artifacts_list, get_testimage_json_result_dir(d))

meta/classes-recipe/failed-tests-post-actions.bbclass new file +3 -0
+
+
+##################################################################

meta/classes-recipe/failed-tests-post-actions.bbclass line 64

Quoted 10 lines
meta/classes-recipe/failed-tests-post-actions.bbclass new file +10 -0
+# General post actions runner
+##################################################################
+
+def run_failed_tests_post_actions(d, tc):
+    post_actions=[
+        list_and_fetch_failed_tests_artifacts
+    ]
+
+    for action in post_actions:
+        action(d, tc)

Rather than create a bbclass class of python functions, these should move to lib/oe and become a proper python library file?

Moving functions out of the class files is on my long term todo list so this seems like an idea opportunity.

Cheers,

Richard

#3 lore raw

Re: [OE-Core][PATCH 1/4] testimage: create a list of failed test post actions

Hello Richard,

Quoted 3 lines · Richard Purdie
Quoted 1 line · Alexis Lothoré via lists.openembedded.org

From: Alexis Lothoré <[email protected]>

[...]

Quoted 6 lines
Quoted 6 lines
meta/classes-recipe/failed-tests-post-actions.bbclass new file +6 -0
+##################################################################
+# General post actions runner
+##################################################################
+
+def run_failed_tests_post_actions(d, tc):
+    post_actions=[
Quoted 8 lines
Quoted 5 lines

+        list_and_fetch_failed_tests_artifacts
+    ]
+
+    for action in post_actions:
+        action(d, tc)

Rather than create a bbclass class of python functions, these should move to lib/oe and become a proper python library file?

ACK, I will do that and create a proper python file for this.

Quoted 2 lines

Moving functions out of the class files is on my long term todo list so this seems like an idea opportunity.

So should this series take the opportunity to move all already existing python functions from testimage to a lib ? I can certainly do that if that's your point :)

Signature · 3 lines
Alexis Lothoré, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#4 lore raw

Re: [OE-Core][PATCH 1/4] testimage: create a list of failed test post actions

Quoted 27 lines · Alexis Lothoré
Quoted 3 lines · Richard Purdie
Quoted 1 line · Alexis Lothoré via lists.openembedded.org

From: Alexis Lothoré <[email protected]>

[...]

Quoted 9 lines
Quoted 9 lines

+################################################################
##

(quoted diff) +2 -0
+# General post actions runner
+################################################################

##

meta/classes-recipe/failed-tests-post-actions.bbclass new file +4 -0
+
+def run_failed_tests_post_actions(d, tc):
+    post_actions=[
+        list_and_fetch_failed_tests_artifacts
Quoted 8 lines
Quoted 4 lines

+    ]
+
+    for action in post_actions:
+        action(d, tc)

Rather than create a bbclass class of python functions, these should
move to lib/oe and become a proper python library file?

ACK, I will do that and create a proper python file for this.

Quoted 8 lines
Quoted 3 lines

Moving functions out of the class files is on my long term todo list so
this seems like an idea opportunity.

So should this series take the opportunity to move all already existing python
functions from testimage to a lib ? I can certainly do that if that's your point :)

I'm saying over time I think many of the python functions need to move. This close to feature freeze may not be the best time, I just wanted to give a clear view of my intent.

Sometimes the variable dependencies don't work the same way from the python library so we need to be careful.

Cheers,

Richard

#5 lore raw

Re: [OE-Core][PATCH 1/4] testimage: create a list of failed test post actions

Quoted 37 lines · Richard Purdie
Quoted 27 lines · Alexis Lothoré
Quoted 3 lines · Richard Purdie
Quoted 1 line · Alexis Lothoré via lists.openembedded.org

From: Alexis Lothoré <[email protected]>

[...]

Quoted 9 lines
Quoted 9 lines

+################################################################
##

(quoted diff) +2 -0
+# General post actions runner
+################################################################

##

meta/classes-recipe/failed-tests-post-actions.bbclass new file +4 -0
+
+def run_failed_tests_post_actions(d, tc):
+    post_actions=[
+        list_and_fetch_failed_tests_artifacts
Quoted 8 lines
Quoted 4 lines

+    ]
+
+    for action in post_actions:
+        action(d, tc)

Rather than create a bbclass class of python functions, these should
move to lib/oe and become a proper python library file?

ACK, I will do that and create a proper python file for this.

Quoted 8 lines
Quoted 3 lines

Moving functions out of the class files is on my long term todo list so
this seems like an idea opportunity.

So should this series take the opportunity to move all already existing python
functions from testimage to a lib ? I can certainly do that if that's your point :)

Quoted 6 lines

I'm saying over time I think many of the python functions need to move. This close to feature freeze may not be the best time, I just wanted to give a clear view of my intent.

Sometimes the variable dependencies don't work the same way from the python library so we need to be careful.

Ok, thanks for the clarification. I will then only fix my series to not create a new bbclass but a lib for now.

Quoted 6 lines

Cheers,

Richard

Signature · 3 lines
Alexis Lothoré, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#6 lore raw

PATCH 2/4 testimage: isolate artifacts directory creation in dedicated post action

In reply to Alexis Lothoré

From: Alexis Lothoré <[email protected]>

In order to be able to create actions that could store new files during failed test post actions, we need to split artifacts directory creation from artifacts retrieval.

Create a new dedicated action to create artifacts main directory so we can add actions creating files in this new directory, without worrying about actions order if at least this action is set first.

---
 .../failed-tests-post-actions.bbclass            | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)
meta/classes-recipe/failed-tests-post-actions.bbclass +10 -6
index 7c7d3391298f..eaf08fb792f3 100644
@@ -9,6 +9,15 @@
 # Artifacts retrieval
 ##################################################################
 
+def create_artifacts_directory(d, tc):
+    import shutil
+
+    local_artifacts_dir = os.path.join(get_testimage_json_result_dir(d), "artifacts")
+    if os.path.isdir(local_artifacts_dir):
+        shutil.rmtree(local_artifacts_dir)
+
+    os.makedirs(local_artifacts_dir)
+
 def get_artifacts_list(target, raw_list):
     result = []
     # Passed list may contains patterns in paths, expand them directly on target
@@ -25,13 +34,7 @@ def get_artifacts_list(target, raw_list):
     return result
 
 def retrieve_test_artifacts(target, artifacts_list, target_dir):
-    import shutil
-
     local_artifacts_dir = os.path.join(target_dir, "artifacts")
-    if os.path.isdir(local_artifacts_dir):
-        shutil.rmtree(local_artifacts_dir)
-
-    os.makedirs(local_artifacts_dir)
     for artifact_path in artifacts_list:
         if not os.path.isabs(artifact_path):
             bb.warn(f"{artifact_path} is not an absolute path")
@@ -57,6 +60,7 @@ def list_and_fetch_failed_tests_artifacts(d, tc):
 
 def run_failed_tests_post_actions(d, tc):
     post_actions=[
+        create_artifacts_directory,
         list_and_fetch_failed_tests_artifacts
     ]
 
Signature · 1 line
2.43.1
#7 lore raw

PATCH 3/4 testimage: add target disk usage stat as post action

In reply to Alexis Lothoré

From: Alexis Lothoré <[email protected]>

In order to debug issues related to disk space (see [1]), add a failed tests post action to retrieve disk usage on the target. Rely on the test context object to run the corresponding command onto the target

[1] https://bugzilla.yoctoproject.org/show_bug.cgi?id=15220

---
 .../failed-tests-post-actions.bbclass           | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)
meta/classes-recipe/failed-tests-post-actions.bbclass +16 -1
index eaf08fb792f3..a8604fca2b9d 100644
@@ -5,6 +5,20 @@
 #
 
 
+##################################################################
+# Host/target statistics
+##################################################################
+
+def get_target_disk_usage(d, tc):
+    output_file = os.path.join(get_testimage_json_result_dir(d), "artifacts", "target_disk_usage.txt")
+    try:
+        (status, output) = tc.target.run('df -hl')
+        with open(output_file, 'w') as f:
+            f.write(output)
+            f.write("\n")
+    except Exception as e:
+        bb.warn(f"Can not get target disk usage: {e}")
+
 ##################################################################
 # Artifacts retrieval
 ##################################################################
@@ -61,7 +75,8 @@ def list_and_fetch_failed_tests_artifacts(d, tc):
 def run_failed_tests_post_actions(d, tc):
     post_actions=[
         create_artifacts_directory,
-        list_and_fetch_failed_tests_artifacts
+        list_and_fetch_failed_tests_artifacts,
+        get_target_disk_usage
     ]
 
     for action in post_actions:
Signature · 1 line
2.43.1
#8 lore raw

PATCH 4/4 testimage: add host disk usage stat as post action

In reply to Alexis Lothoré

From: Alexis Lothoré <[email protected]>

Since the target under test can be a virtualized guest, when some tests fail because of disk usage (see [1]), also fetch disk usage statistics from host to allow checking whether a host disk space saturation could affect running tests.

[1] https://bugzilla.yoctoproject.org/show_bug.cgi?id=15220

---
 .../failed-tests-post-actions.bbclass               | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)
meta/classes-recipe/failed-tests-post-actions.bbclass +12 -1
index a8604fca2b9d..a9e92aa2427e 100644
@@ -19,6 +19,16 @@ def get_target_disk_usage(d, tc):
     except Exception as e:
         bb.warn(f"Can not get target disk usage: {e}")
 
+def get_host_disk_usage(d, tc):
+    import subprocess
+
+    output_file = os.path.join(get_testimage_json_result_dir(d), "artifacts", "host_disk_usage.txt")
+    try:
+        with open(output_file, 'w') as f:
+            output = subprocess.run(['/usr/bin/df', '-hl'], check=True, text=True, stdout=f)
+    except Exception as e:
+        bb.warn(f"Can not get host disk usage: {e}")
+
 ##################################################################
 # Artifacts retrieval
 ##################################################################
@@ -76,7 +86,8 @@ def run_failed_tests_post_actions(d, tc):
     post_actions=[
         create_artifacts_directory,
         list_and_fetch_failed_tests_artifacts,
-        get_target_disk_usage
+        get_target_disk_usage,
+        get_host_disk_usage
     ]
 
     for action in post_actions:
Signature · 1 line
2.43.1