Skip to content

(kernelci/api/helper) Fix sanitizer and add test #2876

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

Merged
merged 1 commit into from
May 29, 2025
Merged
Show file tree
Hide file tree
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
15 changes: 11 additions & 4 deletions kernelci/api/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,11 +391,18 @@ def should_create_node(self, rules, node, parent=None):
def _fsanitize_node_fields(self, node, field_name):
"""
Sanitize node fields to escape curly braces
We need to walk over multiple levels of the node dict to find the field
and escape the curly braces.
"""
if field_name in node:
# double each found bracket to escape it
node[field_name] = node[field_name].replace('}', '}}')
node[field_name] = node[field_name].replace('{', '{{')
if isinstance(node, dict):
for k, val in node.items():
if k == field_name and isinstance(val, str):
node[k] = val.replace('}', '}}').replace('{', '{{')
else:
self._fsanitize_node_fields(val, field_name)
elif isinstance(node, list):
for item in node:
self._fsanitize_node_fields(item, field_name)
return node

def create_job_node(self, job_config, input_node,
Expand Down
97 changes: 97 additions & 0 deletions tests/api/test_apihelper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
#
# Copyright (C) 2025 Collabora Limited
# Author: Denys Fedoryshchenko <[email protected]>

# pylint: disable=C0301

""" Test the APIHelper class """

from kernelci.api.helper import APIHelper
import kernelci.api


def test_apihelper():
"""
Test the APIHelper class
(i know i should not be testing private methods, but this is it for now)
"""
node = {
"id": "6823c3bafef071f536b6ec3c",
"kind": "checkout",
"name": "checkout",
"path": ["checkout"],
"group": None,
"parent": None,
"state": "done",
"result": "pass",
"artifacts": {
"tarball": "https://files.kernelci.org/linux-mainline-master-v6.15-rc6-52-g9f35e33144ae5.tar.gz" # noqa: E501
},
"data": {
"kernel_revision": {
"tree": "mainline",
"url": "https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git",
"branch": "master",
"commit": "9f35e33144ae5377d6a8de86dd3bd4d995c6ac65",
"describe": "v6.15-rc6-52-g9f35e33144ae5",
"version": {
"version": 6,
"patchlevel": 15,
"extra": "-rc6-52-g9f35e33144ae5",
},
"commit_tags": [],
"commit_message": (
"x86/its: Fix build errors when CONFIG_MODULES=n\n\n"
"Fix several build errors when CONFIG_MODULES=n, including the following:\n\n"
"../arch/x86/kernel/alternative.c:195:25: error: incomplete definition of type 'struct module'\n" # noqa: E501
" 195 | for (int i = 0; i < mod->its_num_pages; i++) {\n\n"
'Fixes: 872df34d7c51 ("x86/its: Use dynamic thunks for indirect branches")\n'
"Cc: [email protected]\n"
"Signed-off-by: Eric Biggers \n"
"Acked-by: Dave Hansen \n"
"Tested-by: Steven Rostedt (Google) \n"
"Reviewed-by: Alexandre Chartre \n"
"Signed-off-by: Linus Torvalds "
),
"tip_of_branch": True,
},
"architecture_filter": ["x86_64", "arm64", "arm"],
},
"debug": {},
"jobfilter": None,
"platform_filter": [],
"created": "2025-05-13T22:12:10.336000",
"updated": "2025-05-13T22:26:20.157000",
"timeout": "2025-05-13T23:12:10.327000",
"holdoff": "2025-05-13T22:25:56.224000",
"owner": "production",
"submitter": "service:pipeline",
"treeid": "c1c7b00a59158432a35f2458f745bfbf142bf513801766850c30f65fdb86d5ab",
"user_groups": [],
"processed_by_kcidb_bridge": True,
}

configs = kernelci.config.load("tests/configs/api-configs.yaml")
api_config = configs["api"]["docker-host"]
testcfg = kernelci.api.get_api(api_config)
apihelper = APIHelper(testcfg)
node = apihelper._fsanitize_node_fields(node, "commit_message") # pylint: disable=protected-access # noqa: E501
# Test that the _fsanitize_fsanitize_node_fields method returns a dictionary
assert isinstance(node, dict)
# test that commit message doesnt contain single curly braces
# they should be replaced with double curly braces
val = node["data"]["kernel_revision"]["commit_message"]
assert val == (
"x86/its: Fix build errors when CONFIG_MODULES=n\n\n"
"Fix several build errors when CONFIG_MODULES=n, including the following:\n\n"
"../arch/x86/kernel/alternative.c:195:25: error: incomplete definition of type 'struct module'\n" # noqa: E501
" 195 | for (int i = 0; i < mod->its_num_pages; i++) {{\n\n"
'Fixes: 872df34d7c51 ("x86/its: Use dynamic thunks for indirect branches")\n'
"Cc: [email protected]\n"
"Signed-off-by: Eric Biggers \n"
"Acked-by: Dave Hansen \n"
"Tested-by: Steven Rostedt (Google) \n"
"Reviewed-by: Alexandre Chartre \n"
"Signed-off-by: Linus Torvalds "
)
Loading