Commit Graph

12 Commits

Author SHA1 Message Date
Nico Pache b9868e98ef kunit: string-stream-test: use KUNIT_DEFINE_ACTION_WRAPPER
commit a96a39457705018ad1aac79e7a8453ee52b512ba
Author: Ivan Orlov <ivan.orlov0322@gmail.com>
Date:   Tue Apr 23 19:27:01 2024 +0100

    kunit: string-stream-test: use KUNIT_DEFINE_ACTION_WRAPPER

    Use KUNIT_DEFINE_ACTION_WRAPPER macro to define the 'kfree' and
    'string_stream_destroy' wrappers for kunit_add_action.

    Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>
    Reviewed-by: Rae Moar <rmoar@google.com>
    Acked-by: David Gow <davidgow@google.com>
    Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

JIRA: https://issues.redhat.com/browse/RHEL-39303
Signed-off-by: Nico Pache <npache@redhat.com>
2024-07-31 20:32:30 -06:00
Nico Pache 26721adf40 kunit: string-stream-test: Avoid cast warning when testing gfp_t flags
commit 37f0d37ffce1d162bfaf7f426afca38f6fe15472
Author: Richard Fitzgerald <rf@opensource.cirrus.com>
Date:   Tue Nov 21 16:24:57 2023 +0000

    kunit: string-stream-test: Avoid cast warning when testing gfp_t flags

    Passing a gfp_t to KUNIT_EXPECT_EQ() causes a cast warning:

      lib/kunit/string-stream-test.c:73:9: sparse: sparse: incorrect type in
      initializer (different base types) expected long long right_value
      got restricted gfp_t const __right

    Avoid this by testing stream->gfp for the expected value and passing the
    boolean result of this comparison to KUNIT_EXPECT_TRUE(), as was already
    done a few lines above in string_stream_managed_init_test().

    Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com>
    Fixes: d1a0d699bfc0 ("kunit: string-stream: Add tests for freeing resource-managed string_stream")
    Reported-by: kernel test robot <lkp@intel.com>
    Closes: https://lore.kernel.org/oe-kbuild-all/202311181918.0mpCu2Xh-lkp@intel.com/
    Reviewed-by: Rae Moar <rmoar@google.com>
    Reviewed-by: David Gow <davidgow@google.com>
    Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

JIRA: https://issues.redhat.com/browse/RHEL-39303
Signed-off-by: Nico Pache <npache@redhat.com>
2024-07-31 20:32:27 -06:00
Nico Pache a9be145a07 kunit: string-stream: Test performance of string_stream
commit 53568b720c4dfb70d8db3da3587120fd0c378cae
Author: Richard Fitzgerald <rf@opensource.cirrus.com>
Date:   Mon Aug 28 11:41:11 2023 +0100

    kunit: string-stream: Test performance of string_stream

    Add a test of the speed and memory use of string_stream.

    string_stream_performance_test() doesn't actually "test" anything (it
    cannot fail unless the system has run out of allocatable memory) but it
    measures the speed and memory consumption of the string_stream and reports
    the result.

    This allows changes in the string_stream implementation to be compared.

    Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com>
    Reviewed-by: David Gow <davidgow@google.com>
    Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

JIRA: https://issues.redhat.com/browse/RHEL-39303
Signed-off-by: Nico Pache <npache@redhat.com>
2024-07-31 20:27:47 -06:00
Nico Pache 89c2324c15 kunit: string-stream: Add tests for freeing resource-managed string_stream
commit d1a0d699bfc00ae5b5e74bb640d791a93e825b68
Author: Richard Fitzgerald <rf@opensource.cirrus.com>
Date:   Mon Aug 28 11:41:09 2023 +0100

    kunit: string-stream: Add tests for freeing resource-managed string_stream

    string_stream_managed_free_test() allocates a resource-managed
    string_stream and tests that kunit_free_string_stream() calls
    string_stream_destroy().

    string_stream_resource_free_test() allocates a resource-managed
    string_stream and tests that string_stream_destroy() is called
    when the test resources are cleaned up.

    The old string_stream_init_test() has been split into two tests,
    one for kunit_alloc_string_stream() and the other for
    alloc_string_stream().

    Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com>
    Reviewed-by: David Gow <davidgow@google.com>
    Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

JIRA: https://issues.redhat.com/browse/RHEL-39303
Signed-off-by: Nico Pache <npache@redhat.com>
2024-07-31 20:27:47 -06:00
Nico Pache 2a517badae kunit: string-stream: Decouple string_stream from kunit
commit a3fdf784780ccb0008d630e8722d1389c49c7499
Author: Richard Fitzgerald <rf@opensource.cirrus.com>
Date:   Mon Aug 28 11:41:08 2023 +0100

    kunit: string-stream: Decouple string_stream from kunit

    Re-work string_stream so that it is not tied to a struct kunit. This is
    to allow using it for the log of struct kunit_suite.

    Instead of resource-managing individual allocations the whole string_stream
    can be resource-managed, if required.

        alloc_string_stream() now allocates a string stream that is
        not resource-managed.

        string_stream_destroy() now works on an unmanaged string_stream
        allocated by alloc_string_stream() and frees the entire
        string_stream (previously it only freed the fragments).

        string_stream_clear() has been made public for callers that
        want to free the fragments without destroying the string_stream.

    For resource-managed allocations use kunit_alloc_string_stream()
    and kunit_free_string_stream().

    In addition to this, string_stream_get_string() now returns an
    unmanaged buffer that the caller must kfree().

    Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com>
    Reviewed-by: David Gow <davidgow@google.com>
    Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

JIRA: https://issues.redhat.com/browse/RHEL-39303
Signed-off-by: Nico Pache <npache@redhat.com>
2024-07-31 20:27:47 -06:00
Nico Pache d0c53e56b4 kunit: string-stream: Add kunit_alloc_string_stream()
commit 20631e154c78f4140fffe111f5c79464fae3c38c
Author: Richard Fitzgerald <rf@opensource.cirrus.com>
Date:   Mon Aug 28 11:41:07 2023 +0100

    kunit: string-stream: Add kunit_alloc_string_stream()

    Add function kunit_alloc_string_stream() to do a resource-managed
    allocation of a string stream, and corresponding
    kunit_free_string_stream() to free the resource-managed stream.

    This is preparing for decoupling the string_stream
    implementation from struct kunit, to reduce the amount of code
    churn when that happens. Currently:
     - kunit_alloc_string_stream() only calls alloc_string_stream().
     - kunit_free_string_stream() takes a struct kunit* which
       isn't used yet.

    Callers of the old alloc_string_stream() and
    string_stream_destroy() are all requesting a managed allocation
    so have been changed to use the new functions.

    alloc_string_stream() has been temporarily made static because
    its current behavior has been replaced with
    kunit_alloc_string_stream().

    Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com>
    Reviewed-by: David Gow <davidgow@google.com>
    Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

JIRA: https://issues.redhat.com/browse/RHEL-39303
Signed-off-by: Nico Pache <npache@redhat.com>
2024-07-31 20:27:47 -06:00
Nico Pache 76bca138c2 kunit: string-stream-test: Add cases for string_stream newline appending
commit 1f58cdb173a4ce1bc670fa4662af0fe3556253cf
Author: Richard Fitzgerald <rf@opensource.cirrus.com>
Date:   Mon Aug 28 11:41:05 2023 +0100

    kunit: string-stream-test: Add cases for string_stream newline appending

    Add test cases for testing the string_stream feature that appends a
    newline to strings that do not already end with a newline.

    string_stream_no_auto_newline_test() tests with this feature disabled.
    Newlines should not be added or dropped.

    string_stream_auto_newline_test() tests with this feature enabled.
    Newlines should be added to lines that do not end with a newline.

    string_stream_append_auto_newline_test() tests appending the
    content of one stream to another stream when the target stream
    has newline appending enabled.

    Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com>
    Reviewed-by: Rae Moar <rmoar@google.com>
    Reviewed-by: David Gow <davidgow@google.com>
    Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

JIRA: https://issues.redhat.com/browse/RHEL-39303
Signed-off-by: Nico Pache <npache@redhat.com>
2024-07-31 20:27:47 -06:00
Nico Pache 89f9b8e613 kunit: string-stream: Improve testing of string_stream
commit 4551caca6ab67fb0b5199ca43580c4f8d27bf28a
Author: Richard Fitzgerald <rf@opensource.cirrus.com>
Date:   Mon Aug 28 11:41:03 2023 +0100

    kunit: string-stream: Improve testing of string_stream

    Replace the minimal tests with more-thorough testing.

    string_stream_init_test() tests that struct string_stream is
    initialized correctly.

    string_stream_line_add_test() adds a series of numbered lines and
    checks that the resulting string contains all the lines.

    string_stream_variable_length_line_test() adds a large number of
    lines of varying length to create many fragments, then tests that all
    lines are present.

    string_stream_append_test() tests various cases of using
    string_stream_append() to append the content of one stream to another.

    Adds string_stream_append_empty_string_test() to test that adding an
    empty string to a string_stream doesn't create a new empty fragment.

    Signed-off-by: Richard Fitzgerald <rf@opensource.cirrus.com>
    Reviewed-by: Rae Moar <rmoar@google.com>
    Reviewed-by: David Gow <davidgow@google.com>
    Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>

JIRA: https://issues.redhat.com/browse/RHEL-39303
Signed-off-by: Nico Pache <npache@redhat.com>
2024-07-31 20:27:47 -06:00
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
Alan Maguire 109fb06fdc kunit: move string-stream.h to lib/kunit
string-stream interfaces are not intended for external use;
move them from include/kunit to lib/kunit accordingly.

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>
Tested-by: Brendan Higgins <brendanhiggins@google.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2020-01-09 16:41:05 -07:00
Brendan Higgins e4aea8f853 kunit: test: add the concept of assertions
Add support for assertions which are like expectations except the test
terminates if the assertion is not satisfied.

The idea with assertions is that you use them to state all the
preconditions for your test. Logically speaking, these are the premises
of the test case, so if a premise isn't true, there is no point in
continuing the test case because there are no conclusions that can be
drawn without the premises. Whereas, the expectation is the thing you
are trying to prove. It is not used universally in x-unit style test
frameworks, but I really like it as a convention.  You could still
express the idea of a premise using the above idiom, but I think
KUNIT_ASSERT_* states the intended idea perfectly.

Signed-off-by: Brendan Higgins <brendanhiggins@google.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Logan Gunthorpe <logang@deltatee.com>
Reviewed-by: Stephen Boyd <sboyd@kernel.org>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2019-09-30 17:35:01 -06:00
Brendan Higgins d8e2a76b4c kunit: test: add initial tests
Add a test for string stream along with a simpler example.

Signed-off-by: Brendan Higgins <brendanhiggins@google.com>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Logan Gunthorpe <logang@deltatee.com>
Reviewed-by: Stephen Boyd <sboyd@kernel.org>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
2019-09-30 17:35:01 -06:00