Skip to contentSkip to conversation

Add support for KUnit tests

PATCH Superseded Isabella Basso 2 participants 4 messages Jun 17, 2022

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover Add support for KUnit tests

This patch series was first developed as part of the LKCamp hackathon that happened last year[1], mainly focusing on refactoring DRM tests to use KUnit.

KUnit[2][3] is a unified test framework that provides helper tools, simplifying their development and execution. Using an x86-64 machine it's possible to run tests in the host's kernel natively using user-mode Linux[4] (aka UML), which simplifies usage in a wide variety of scenarios, including integration to CI.

As the tool's adoption widens into graphics testing territory, I and LKCamp members figured it would be important to support it in IGT, as it's a core tool for GPU drivers maintainers.

I have then added KUnit support into IGT mainly following the KTAP specs, and it can be tested using patch 2/2 in this series together with an AMDGPU patch series[5] available at [6].

[1]: https://groups.google.com/g/kunit-dev/c/YqFR1q2uZvk/m/IbvItSfHBAAJ
[2]: https://kunit.dev
[3]: https://docs.kernel.org/dev-tools/kunit/index.html
[4]: http://user-mode-linux.sourceforge.net
[5]: https://lore.kernel.org/dri-devel/[email protected]/
[6]: https://gitlab.freedesktop.org/isinyaaa/linux/-/tree/kunit-prototype2

Isabella Basso (2):
igt_kmod: add compatibility for KUnit
tests/amdgpu/amd_kunit: add KUnit AMDGPU tests

 lib/igt_kmod.c           | 269 +++++++++++++++++++++++++++++++++++++++
 lib/igt_kmod.h           |   3 +
 tests/amdgpu/amd_kunit.c |  34 +++++
 tests/amdgpu/meson.build |   1 +
 4 files changed, 307 insertions(+)
 create mode 100644 tests/amdgpu/amd_kunit.c
Signature · 1 line
2.36.1
#1 lore raw

PATCH 1/2 igt_kmod: add compatibility for KUnit

This adds functions for both executing the tests as well as parsing (K)TAP kmsg output, as per the KTAP spec [1].

[1] https://www.kernel.org/doc/html/latest/dev-tools/ktap.html

---
 lib/igt_kmod.c | 269 +++++++++++++++++++++++++++++++++++++++++++++++++
 lib/igt_kmod.h |   3 +
 2 files changed, 272 insertions(+)
lib/igt_kmod.c +269 -0
index dfdcfcc5..d38cd221 100644
@@ -25,6 +25,7 @@
 #include <signal.h>
 #include <errno.h>
 #include <sys/utsname.h>
+#include <limits.h>
 
 #include "igt_aux.h"
 #include "igt_core.h"
@@ -32,6 +33,8 @@
 #include "igt_sysfs.h"
 #include "igt_taints.h"
 
+#define BUF_LEN 4096
+
 /**
  * SECTION:igt_kmod
  * @short_description: Wrappers around libkmod for module loading/unloading
@@ -372,6 +375,272 @@ static void *strdup_realloc(char *origptr, const char *strdata)
 	return newptr;
 }
 
+static void find_next_tap_subtest(char *record, int *ret, bool is_subtest)
+{
+	char test_count_ptr[12];
+	const char *test_search_str, *ver_search_str, *lend,
+	      *ver_res_ptr, *test_res_ptr;
+	int test_count_strlen;
+	long test_count;
+
+	/* total tests will appear as 0..N at the beginning of a run */
+	test_search_str = "..";
+	ver_search_str = "TAP version ";
+
+	ver_res_ptr = strstr(record, ver_search_str);
+	test_res_ptr = strstr(record, test_search_str);
+
+	*ret = -1;
+
+	if (test_res_ptr == NULL)
+		return;
+
+	test_res_ptr += strlen(test_search_str);
+	lend = strchrnul(test_res_ptr, '\n');
+	/* in case line > 4096 */
+	if (*lend == '\0') {
+		igt_info("%s\n", record);
+		return;
+	}
+
+	test_count_strlen = (int)(lend - test_res_ptr);
+	if (test_count_strlen > sizeof(test_count_ptr) - 1)
+		return;
+	strncpy(test_count_ptr, test_res_ptr, test_count_strlen);
+
+	test_count_ptr[test_count_strlen] = '\0';
+	test_count = strtol(test_count_ptr, NULL, 10);
+	if (test_count == LONG_MIN || test_count == LONG_MAX)
+		return;
+
+	/* "(K)TAP version XX" should be the first line on all (sub)tests as per
+	 * https://www.kernel.org/doc/html/latest/dev-tools/ktap.html#version-lines
+	 * but actually isn't, as it currently depends on who writes the test
+	 * to print this */
+	if (ver_res_ptr == NULL)
+		igt_info("Missing test version string\n");
+
+	if (is_subtest)
+		igt_info("running %d subtests...\n", (int)test_count);
+	else
+		igt_info("running %d tests...\n", (int)test_count);
+
+	*ret = (int)test_count;
+	return;
+}
+
+static void print_tap(int fd, char *record, int *sublevel, bool *failed_tests)
+{
+	const char *test_fail_search_str;
+	ssize_t r;
+	/* for each sublevel we have 2 spaces indent */
+	const int indent_len = (*sublevel + 1) * 2;;
+
+	test_fail_search_str = "not ok ";
+
+	for (; *sublevel > 0;) {
+		const char *lend, *test_fail_res, *lstart, *i_end;
+		int *ret, retval, llen;
+
+		ret = &retval;
+
+		lend = strchrnul(record, '\n');
+
+		if (*lend != '\0')
+			lseek(fd, (int)(lend - record), SEEK_CUR);
+
+		r = read(fd, record, BUF_LEN);
+
+		if (r <= 0) {
+			switch (errno) {
+			case EINTR:
+				continue;
+			case EPIPE:
+				igt_warn("kmsg truncated: too many messages. You may want to increase log_buf_len in kmcdline\n");
+				continue;
+			case !EAGAIN:
+				igt_warn("kmsg truncated: unknown error (%m)\n");
+			default:
+				break;
+			}
+			break;
+		}
+
+		find_next_tap_subtest(record, ret, true);
+		if (*ret != -1) {
+			(*sublevel)++;
+			print_tap(fd, record, sublevel, failed_tests);
+		}
+
+		lend = strchrnul(record, '\n');
+
+		/* in case line > 4096 */
+		if (*lend == '\0') {
+			igt_info("%s\n", record);
+			continue;
+		}
+
+		lstart = strchrnul(record, ';');
+
+		/* check if we're still in a subtest */
+		if (*lstart == '\0') {
+			igt_warn("kmsg truncated: output malformed (%m)\n");
+			igt_fail(IGT_EXIT_FAILURE);
+		} else {
+			lstart++;
+			i_end = lstart;
+			while (isspace(*i_end))
+				i_end++;
+			llen = (int)(i_end - lstart);
+			if (llen != indent_len)
+				break;
+		}
+
+		test_fail_res = strstr(record, test_fail_search_str);
+		if (test_fail_res != NULL) {
+			igt_warn("kmsg> %.*s\n",
+				 (int)(lend - lstart) - 1, lstart);
+			*failed_tests = true;
+			continue;
+		}
+
+		igt_info("kmsg> %.*s\n",
+			 (int)(lend - lstart) - 1, lstart);
+	}
+	(*sublevel)--;
+}
+
+static void parse_kmsg_for_tap(int fd)
+{
+	char record[BUF_LEN + 1];
+	int test_sublevel;
+	bool failed_tests, skip, found_tests;
+
+	test_sublevel = 0;
+
+	skip = true;
+	failed_tests = false;
+	found_tests = false;
+
+	if (fd == -1) {
+		igt_warn("Unable to retrieve kernel log (from /dev/kmsg)\n");
+		return;
+	}
+
+	record[BUF_LEN] = '\0';
+
+	while (true) {
+		const char *lend;
+		int *ret, retval;
+		ssize_t r;
+		ret = &retval;
+
+		lend = strchrnul(record, '\n');
+
+		if (!skip && *lend != '\0')
+			lseek(fd, (int) (lend - record), SEEK_CUR);
+
+		r = read(fd, record, BUF_LEN);
+
+		if (!skip)
+			lend = strchrnul(record, '\n');
+		else
+			skip = false;
+
+		if (r <= 0) {
+			switch (errno) {
+			case EINTR:
+				continue;
+			case EPIPE:
+				igt_warn("kmsg truncated: too many messages. You may want to increase log_buf_len in kmcdline\n");
+				continue;
+			case !EAGAIN:
+				igt_warn("kmsg truncated: unknown error (%m)\n");
+			default:
+				break;
+			}
+			break;
+		}
+
+		find_next_tap_subtest(record, ret, false);
+		if (*ret != -1) {
+			found_tests = true;
+			test_sublevel++;
+			print_tap(fd, record, &test_sublevel, &failed_tests);
+		}
+	}
+
+	if (failed_tests || !found_tests)
+		igt_fail(IGT_EXIT_FAILURE);
+	else
+		igt_success();
+}
+
+/**
+ * igt_kunit: tests a kunit module
+ * @mod_name: the name of the module
+ * @options: options to load the module
+ * @req_mods: array with additional dependencies to run the KUnit test
+ * @req_mod_len: size of req_mods array
+ *
+ * Loads the kunit module, parses its dmesg output, then unloads it
+ */
+void igt_kunit(const char *mod_name, const char *options,
+	       const char **req_mods, int req_mod_len)
+{
+	int kmsg_fd, cmi = 0;
+	struct igt_kselftest tst;
+	const char *norm_name;
+
+	/* get normalized module name */
+	igt_require(igt_kselftest_init(&tst, mod_name) == 0);
+
+	norm_name = kmod_module_get_name(tst.kmod);
+
+	if (!igt_kmod_is_loaded(norm_name)) {
+		for (cmi = 0; cmi < req_mod_len; cmi++) {
+			if (igt_kmod_load(req_mods[cmi], NULL))
+				goto unload;
+		}
+
+		/* The kunit module is required for running any kunit tests */
+		if (!igt_kmod_is_loaded("kunit"))
+			igt_require(igt_kmod_load("kunit", NULL) == 0);
+
+	} else {
+		/* Unload module if loaded (maybe previous run bailed out?) */
+		igt_kmod_unload(mod_name, 0);
+	}
+
+	kmsg_fd = open("/dev/kmsg", O_RDONLY | O_NONBLOCK);
+
+	if (kmsg_fd < 0) {
+		igt_warn("Could not open /dev/kmsg");
+		goto unload;
+	}
+
+	if (lseek(kmsg_fd, 0, SEEK_END)) {
+		igt_warn("Could not seek the end of /dev/kmsg");
+		close(kmsg_fd);
+		goto unload;
+	}
+
+	igt_require(igt_kmod_load(mod_name, options) == 0);
+
+	parse_kmsg_for_tap(kmsg_fd);
+
+	igt_kmod_unload(mod_name, 0);
+
+	close(kmsg_fd);
+unload:
+	for (cmi--; cmi >= 0; cmi--) {
+		igt_kmod_unload(req_mods[cmi], 0);
+	}
+
+	if (igt_kmod_is_loaded("kunit"))
+		igt_require(igt_kmod_load("kunit", NULL) == 0);
+}
+
 /**
  * igt_i915_driver_load:
  * @opts: options to pass to i915 driver
lib/igt_kmod.h +3 -0
index f98dd29f..6d8b5968 100644
@@ -38,6 +38,9 @@ int igt_kmod_unload(const char *mod_name, unsigned int flags);
 
 int igt_audio_driver_unload(char **whom);
 
+void igt_kunit(const char *mod_name, const char *options,
+	       const char **req_mods, int req_mod_len);
+
 int igt_i915_driver_load(const char *opts);
 int igt_i915_driver_unload(void);
 int __igt_i915_driver_unload(char **whom);
Signature · 1 line
2.36.1
#2 lore raw

PATCH 2/2 tests/amdgpu/amd_kunit: add KUnit AMDGPU tests

In reply to Isabella Basso

This adds an IGT runner for the kernel's newly introduced KUnit tests under the AMDGPU driver.

---
 tests/amdgpu/amd_kunit.c | 34 ++++++++++++++++++++++++++++++++++
 tests/amdgpu/meson.build |  1 +
 2 files changed, 35 insertions(+)
 create mode 100644 tests/amdgpu/amd_kunit.c
tests/amdgpu/amd_kunit.c new file +34 -0
new file mode 100644
index 00000000..258c8383
@@ -0,0 +1,34 @@
+/*
+ * Copyright © 2022 Isabella Basso do Amaral <[email protected]>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ */
+
+#include <libkmod.h>
+
+#include "igt.h"
+#include "igt_kmod.h"
+
+IGT_TEST_DESCRIPTION("KUnit AMDGPU module testing");
+
+igt_simple_main
+{
+	igt_kunit("amdgpu", NULL, NULL, 0);
+}
tests/amdgpu/meson.build +1 -0
index 12c2ba99..0b864e75 100644
@@ -10,6 +10,7 @@ if libdrm_amdgpu.found()
 			  'amd_cs_nop',
 			  'amd_hotplug',
 			  'amd_info',
+			  'amd_kunit',
 			  'amd_prime',
 			  'amd_max_bpc',
 			  'amd_module_load',
Signature · 1 line
2.36.1
#3 lore raw

[igt-dev] ✗ Fi.CI.BAT: failure for Add support for KUnit tests

In reply to Isabella Basso
== Series Details ==

== Series Details ==

Series: Add support for KUnit tests
URL : https://patchwork.freedesktop.org/series/105294/
State : failure

== Summary ==

CI Bug Log - changes from CI_DRM_11776 -> IGTPW_7337
====================================================

Summary
-------

**FAILURE**

Serious unknown changes coming with IGTPW_7337 absolutely need to be verified manually.

If you think the reported changes have nothing to do with the changes introduced in IGTPW_7337, please notify your bug team to allow them to document this new failure mode, which will reduce false positives in CI.

External URL: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_7337/index.html

Participating hosts (35 -> 34)
------------------------------

Missing (1): fi-bdw-samus

Possible new issues
-------------------

Here are the unknown changes that may have been introduced in IGTPW_7337:

### IGT changes ###

#### Possible regressions ####

* igt@i915_selftest@live@gt_engines:
- fi-glk-j4005: [PASS][1] -> [FAIL][2]
[1]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_11776/fi-glk-j4005/igt@i915_selftest@live@gt_engines.html
[2]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_7337/fi-glk-j4005/igt@i915_selftest@live@gt_engines.html

Known issues
------------

Here are the changes found in IGTPW_7337 that come from known issues:

### IGT changes ###

#### Issues hit ####

* igt@i915_selftest@live@requests:
- fi-pnv-d510: [PASS][3] -> [DMESG-FAIL][4] ([i915#4528])
[3]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_11776/fi-pnv-d510/igt@i915_selftest@[email protected]
[4]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_7337/fi-pnv-d510/igt@i915_selftest@[email protected]

* igt@i915_suspend@basic-s3-without-i915:
- fi-rkl-11600: [PASS][5] -> [INCOMPLETE][6] ([i915#5982])
[5]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_11776/fi-rkl-11600/igt@[email protected]
[6]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_7337/fi-rkl-11600/igt@[email protected]

* igt@kms_chamelium@common-hpd-after-suspend:
- fi-hsw-4770: NOTRUN -> [SKIP][7] ([fdo#109271] / [fdo#111827])
[7]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_7337/fi-hsw-4770/igt@[email protected]

* igt@kms_flip@basic-flip-vs-dpms@a-edp1:
- fi-tgl-u2: [PASS][8] -> [DMESG-WARN][9] ([i915#402])
[8]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_11776/fi-tgl-u2/igt@kms_flip@[email protected]
[9]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_7337/fi-tgl-u2/igt@kms_flip@[email protected]

* igt@runner@aborted:
- fi-pnv-d510: NOTRUN -> [FAIL][10] ([fdo#109271] / [i915#2403] / [i915#4312])
[10]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_7337/fi-pnv-d510/igt@[email protected]

#### Possible fixes ####

* igt@gem_exec_suspend@basic-s0@smem:
- {fi-ehl-2}: [DMESG-WARN][11] ([i915#5122]) -> [PASS][12]
[11]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_11776/fi-ehl-2/igt@gem_exec_suspend@[email protected]
[12]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_7337/fi-ehl-2/igt@gem_exec_suspend@[email protected]

* igt@i915_selftest@live@hangcheck:
- fi-hsw-4770: [INCOMPLETE][13] ([i915#3303] / [i915#4785]) -> [PASS][14]
[13]: https://intel-gfx-ci.01.org/tree/drm-tip/CI_DRM_11776/fi-hsw-4770/igt@i915_selftest@[email protected]
[14]: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_7337/fi-hsw-4770/igt@i915_selftest@[email protected]

{name}: This element is suppressed. This means it is ignored when computing
the status of the difference (SUCCESS, WARNING, or FAILURE).

[fdo#109271]: https://bugs.freedesktop.org/show_bug.cgi?id=109271
[fdo#111827]: https://bugs.freedesktop.org/show_bug.cgi?id=111827
[i915#2403]: https://gitlab.freedesktop.org/drm/intel/issues/2403
[i915#3303]: https://gitlab.freedesktop.org/drm/intel/issues/3303
[i915#402]: https://gitlab.freedesktop.org/drm/intel/issues/402
[i915#4312]: https://gitlab.freedesktop.org/drm/intel/issues/4312
[i915#4528]: https://gitlab.freedesktop.org/drm/intel/issues/4528
[i915#4785]: https://gitlab.freedesktop.org/drm/intel/issues/4785
[i915#5122]: https://gitlab.freedesktop.org/drm/intel/issues/5122
[i915#5982]: https://gitlab.freedesktop.org/drm/intel/issues/5982

Build changes
-------------

* CI: CI-20190529 -> None
* IGT: IGT_6536 -> IGTPW_7337

CI-20190529: 20190529
CI_DRM_11776: ac17a5249380aaabe5d1eaebd9b3a2eedc08ccdc @ git://anongit.freedesktop.org/gfx-ci/linux
IGTPW_7337: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_7337/index.html
IGT_6536: e3de4d32b7a509635fbff4d5131c05a7767699f7 @ https://gitlab.freedesktop.org/drm/igt-gpu-tools.git

Testlist changes
----------------

+igt@amdgpu/amd_kunit

== Logs ==

For more details see: https://intel-gfx-ci.01.org/tree/drm-tip/IGTPW_7337/index.html