Skip to content

Commit

Permalink
Merge pull request #6047 from grom72/core-log-set-function-ut
Browse files Browse the repository at this point in the history
test: UT for core_log_set_function()
  • Loading branch information
grom72 authored Mar 14, 2024
2 parents e152df6 + 9a96de0 commit 3fda80e
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/test/core_log/TESTS.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,7 @@ class TEST5(CORE_LOG):

class TEST6(CORE_LOG):
test_case = 'test_happy_day'


class TEST7(CORE_LOG):
test_case = 'test_set_custom_function'
41 changes: 38 additions & 3 deletions src/test/core_log/core_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
/* Copyright 2024, Intel Corporation */

/*
* core_log.c -- unit test for core_log() and core_log_va()
* core_log.c -- unit test for core_log(), core_log_va() and
* core_log_set_function()
*/

#include <syslog.h>
Expand Down Expand Up @@ -172,11 +173,25 @@ test_level_gt_threshold(const struct test_case *tc, int argc, char *argv[])
}

static int
test_happy_day(const struct test_case *tc, int argc, char *argv[])
test_happy_day_helper(core_log_function *log_function)
{
/* Pass the message all the way to the logging function. */
core_log_set_threshold(CORE_LOG_THRESHOLD, CORE_LOG_LEVEL_ERROR);

/*
* Disable the validation as custom_log_function() may be called from
* core_log_set_function().
*/
if (log_function == CORE_LOG_USE_DEFAULT_FUNCTION) {
FUNC_MOCK_RCOUNTER_SET(core_log_default_function,
NOT_VALIDATED_CALL);
} else {
FUNC_MOCK_RCOUNTER_SET(custom_log_function,
NOT_VALIDATED_CALL);
}

core_log_set_function(log_function);

reset_mocks();

/* set the expectations */
Expand All @@ -195,11 +210,30 @@ test_happy_day(const struct test_case *tc, int argc, char *argv[])
UT_ASSERTeq(RCOUNTER(last_error_msg_get), CALLED);
UT_ASSERTeq(RCOUNTER(vsnprintf), CALLED);
UT_ASSERTeq(RCOUNTER(__xpg_strerror_r), CALLED);
UT_ASSERTeq(RCOUNTER(core_log_default_function), CALLED);
if (log_function == CORE_LOG_USE_DEFAULT_FUNCTION) {
UT_ASSERTeq(RCOUNTER(core_log_default_function), CALLED);
UT_ASSERTeq(RCOUNTER(custom_log_function), NOT_CALLED);
} else {
UT_ASSERTeq(RCOUNTER(core_log_default_function), NOT_CALLED);
UT_ASSERTeq(RCOUNTER(custom_log_function), CALLED);
}

return NO_ARGS_CONSUMED;
}

static int
test_happy_day(const struct test_case *tc, int argc, char *argv[])
{
return test_happy_day_helper(CORE_LOG_USE_DEFAULT_FUNCTION);
}

/* Happy day scenario with custom logging function */
static int
test_set_custom_function(const struct test_case *tc, int argc, char *argv[])
{
return test_happy_day_helper(custom_log_function);
}

static struct test_case test_cases[] = {
TEST_CASE(test_CORE_LOG_LEVEL_ERROR_LAST),
TEST_CASE(test_vsnprintf_fail),
Expand All @@ -208,6 +242,7 @@ static struct test_case test_cases[] = {
TEST_CASE(test_strerror_r_fail),
TEST_CASE(test_level_gt_threshold),
TEST_CASE(test_happy_day),
TEST_CASE(test_set_custom_function),
};

#define NTESTS ARRAY_SIZE(test_cases)
Expand Down
29 changes: 29 additions & 0 deletions src/test/core_log/core_log_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,34 @@ FUNC_MOCK_RUN_DEFAULT {
}
FUNC_MOCK_END

FUNC_MOCK_NONSTATIC(custom_log_function, void, enum core_log_level level,
const char *file_name, const int line_no, const char *function_name,
const char *message)
FUNC_MOCK_RUN(VALIDATED_CALL) {
UT_ASSERTeq(level, Log_function_.exp_level);
UT_ASSERTstreq(file_name, FILE_NAME);
UT_ASSERTeq(line_no, LINE_NO);
UT_ASSERTstreq(function_name, FUNC_NAME);
if (Common.use_last_error_msg) {
UT_ASSERTeq(message, LAST_ERROR_MSG_MOCK);
} else {
UT_ASSERTne(message, LAST_ERROR_MSG_MOCK);
}
return;
}
FUNC_MOCK_RUN_DEFAULT {
_FUNC_REAL(custom_log_function)(level, file_name, line_no,
function_name, message);
}
FUNC_MOCK_END

void
custom_log_function(enum core_log_level level, const char *file_name,
const int line_no, const char *function_name, const char *message)
{
SUPPRESS_UNUSED(level, file_name, line_no, function_name, message);
}

/* helpers */

void
Expand All @@ -93,6 +121,7 @@ reset_mocks(void)
FUNC_MOCK_RCOUNTER_SET(vsnprintf, VALIDATED_CALL);
FUNC_MOCK_RCOUNTER_SET(__xpg_strerror_r, VALIDATED_CALL);
FUNC_MOCK_RCOUNTER_SET(core_log_default_function, VALIDATED_CALL);
FUNC_MOCK_RCOUNTER_SET(custom_log_function, VALIDATED_CALL);
}

void
Expand Down
5 changes: 5 additions & 0 deletions src/test/core_log/core_log_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#define VALIDATED_CALL 127
#define NOT_CALLED VALIDATED_CALL
#define CALLED (VALIDATED_CALL + 1)
#define NOT_VALIDATED_CALL 0

extern struct common_ctx {
bool use_last_error_msg;
Expand All @@ -49,6 +50,7 @@ FUNC_MOCK_EXTERN(last_error_msg_get);
FUNC_MOCK_EXTERN(vsnprintf);
FUNC_MOCK_EXTERN(__xpg_strerror_r);
FUNC_MOCK_EXTERN(core_log_default_function);
FUNC_MOCK_EXTERN(custom_log_function);

/* helpers */

Expand All @@ -60,3 +62,6 @@ void test_strerror_r_fail_helper(bool before_glibc_2_13);

void test_log_function_call_helper(enum core_log_level level,
bool call_log_function);

void custom_log_function(enum core_log_level level, const char *file_name,
const int line_no, const char *function_name, const char *message);

0 comments on commit 3fda80e

Please sign in to comment.