kernel_optimize_test/lib/kunit/assert.c
Alan Maguire c475c77d5b kunit: allow kunit tests to be loaded as a module
As tests are added to kunit, it will become less feasible to execute
all built tests together.  By supporting modular tests we provide
a simple way to do selective execution on a running system; specifying

CONFIG_KUNIT=y
CONFIG_KUNIT_EXAMPLE_TEST=m

...means we can simply "insmod example-test.ko" to run the tests.

To achieve this we need to do the following:

o export the required symbols in kunit
o string-stream tests utilize non-exported symbols so for now we skip
  building them when CONFIG_KUNIT_TEST=m.
o drivers/base/power/qos-test.c contains a few unexported interface
  references, namely freq_qos_read_value() and freq_constraints_init().
  Both of these could be potentially defined as static inline functions
  in include/linux/pm_qos.h, but for now we simply avoid supporting
  module build for that test suite.
o support a new way of declaring test suites.  Because a module cannot
  do multiple late_initcall()s, we provide a kunit_test_suites() macro
  to declare multiple suites within the same module at once.
o some test module names would have been too general ("test-test"
  and "example-test" for kunit tests, "inode-test" for ext4 tests);
  rename these as appropriate ("kunit-test", "kunit-example-test"
  and "ext4-inode-test" respectively).

Also define kunit_test_suite() via kunit_test_suites()
as callers in other trees may need the old definition.

Co-developed-by: Knut Omang <knut.omang@oracle.com>
Signed-off-by: Knut Omang <knut.omang@oracle.com>
Signed-off-by: Alan Maguire <alan.maguire@oracle.com>
Reviewed-by: Brendan Higgins <brendanhiggins@google.com>
Acked-by: Theodore Ts'o <tytso@mit.edu> # for ext4 bits
Acked-by: David Gow <davidgow@google.com> # For list-test
Reported-by: kbuild test robot <lkp@intel.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2020-01-09 16:42:29 -07:00

152 lines
4.5 KiB
C

// SPDX-License-Identifier: GPL-2.0
/*
* Assertion and expectation serialization API.
*
* Copyright (C) 2019, Google LLC.
* Author: Brendan Higgins <brendanhiggins@google.com>
*/
#include <kunit/assert.h>
#include "string-stream.h"
void kunit_base_assert_format(const struct kunit_assert *assert,
struct string_stream *stream)
{
const char *expect_or_assert = NULL;
switch (assert->type) {
case KUNIT_EXPECTATION:
expect_or_assert = "EXPECTATION";
break;
case KUNIT_ASSERTION:
expect_or_assert = "ASSERTION";
break;
}
string_stream_add(stream, "%s FAILED at %s:%d\n",
expect_or_assert, assert->file, assert->line);
}
EXPORT_SYMBOL_GPL(kunit_base_assert_format);
void kunit_assert_print_msg(const struct kunit_assert *assert,
struct string_stream *stream)
{
if (assert->message.fmt)
string_stream_add(stream, "\n%pV", &assert->message);
}
EXPORT_SYMBOL_GPL(kunit_assert_print_msg);
void kunit_fail_assert_format(const struct kunit_assert *assert,
struct string_stream *stream)
{
kunit_base_assert_format(assert, stream);
string_stream_add(stream, "%pV", &assert->message);
}
EXPORT_SYMBOL_GPL(kunit_fail_assert_format);
void kunit_unary_assert_format(const struct kunit_assert *assert,
struct string_stream *stream)
{
struct kunit_unary_assert *unary_assert = container_of(
assert, struct kunit_unary_assert, assert);
kunit_base_assert_format(assert, stream);
if (unary_assert->expected_true)
string_stream_add(stream,
"\tExpected %s to be true, but is false\n",
unary_assert->condition);
else
string_stream_add(stream,
"\tExpected %s to be false, but is true\n",
unary_assert->condition);
kunit_assert_print_msg(assert, stream);
}
EXPORT_SYMBOL_GPL(kunit_unary_assert_format);
void kunit_ptr_not_err_assert_format(const struct kunit_assert *assert,
struct string_stream *stream)
{
struct kunit_ptr_not_err_assert *ptr_assert = container_of(
assert, struct kunit_ptr_not_err_assert, assert);
kunit_base_assert_format(assert, stream);
if (!ptr_assert->value) {
string_stream_add(stream,
"\tExpected %s is not null, but is\n",
ptr_assert->text);
} else if (IS_ERR(ptr_assert->value)) {
string_stream_add(stream,
"\tExpected %s is not error, but is: %ld\n",
ptr_assert->text,
PTR_ERR(ptr_assert->value));
}
kunit_assert_print_msg(assert, stream);
}
EXPORT_SYMBOL_GPL(kunit_ptr_not_err_assert_format);
void kunit_binary_assert_format(const struct kunit_assert *assert,
struct string_stream *stream)
{
struct kunit_binary_assert *binary_assert = container_of(
assert, struct kunit_binary_assert, assert);
kunit_base_assert_format(assert, stream);
string_stream_add(stream,
"\tExpected %s %s %s, but\n",
binary_assert->left_text,
binary_assert->operation,
binary_assert->right_text);
string_stream_add(stream, "\t\t%s == %lld\n",
binary_assert->left_text,
binary_assert->left_value);
string_stream_add(stream, "\t\t%s == %lld",
binary_assert->right_text,
binary_assert->right_value);
kunit_assert_print_msg(assert, stream);
}
EXPORT_SYMBOL_GPL(kunit_binary_assert_format);
void kunit_binary_ptr_assert_format(const struct kunit_assert *assert,
struct string_stream *stream)
{
struct kunit_binary_ptr_assert *binary_assert = container_of(
assert, struct kunit_binary_ptr_assert, assert);
kunit_base_assert_format(assert, stream);
string_stream_add(stream,
"\tExpected %s %s %s, but\n",
binary_assert->left_text,
binary_assert->operation,
binary_assert->right_text);
string_stream_add(stream, "\t\t%s == %pK\n",
binary_assert->left_text,
binary_assert->left_value);
string_stream_add(stream, "\t\t%s == %pK",
binary_assert->right_text,
binary_assert->right_value);
kunit_assert_print_msg(assert, stream);
}
EXPORT_SYMBOL_GPL(kunit_binary_ptr_assert_format);
void kunit_binary_str_assert_format(const struct kunit_assert *assert,
struct string_stream *stream)
{
struct kunit_binary_str_assert *binary_assert = container_of(
assert, struct kunit_binary_str_assert, assert);
kunit_base_assert_format(assert, stream);
string_stream_add(stream,
"\tExpected %s %s %s, but\n",
binary_assert->left_text,
binary_assert->operation,
binary_assert->right_text);
string_stream_add(stream, "\t\t%s == %s\n",
binary_assert->left_text,
binary_assert->left_value);
string_stream_add(stream, "\t\t%s == %s",
binary_assert->right_text,
binary_assert->right_value);
kunit_assert_print_msg(assert, stream);
}
EXPORT_SYMBOL_GPL(kunit_binary_str_assert_format);