Skip to content

Commit

Permalink
test: core_log_default_function tests scaffolding
Browse files Browse the repository at this point in the history
Signed-off-by: Tomasz Gromadzki <tomasz.gromadzki@intel.com>
  • Loading branch information
grom72 committed Feb 27, 2024
1 parent c7ad453 commit ff0641e
Show file tree
Hide file tree
Showing 6 changed files with 260 additions and 50 deletions.
1 change: 1 addition & 0 deletions src/test/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ OBJ_TESTS = \
OTHER_TESTS = \
arch_flags\
core_log_last\
core_log_default_function\
core_log_max\
core_log\
checksum\
Expand Down
1 change: 1 addition & 0 deletions src/test/core_log_default_function/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
core_log_default_function
13 changes: 13 additions & 0 deletions src/test/core_log_default_function/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright 2024, Intel Corporation

TARGET = core_log_default_function
OBJS = core_log_default_function.o

BUILD_STATIC_DEBUG=n
BUILD_STATIC_NONDEBUG=n

LIBPMEMCORE=internal-debug

include ../Makefile.inc
LDFLAGS += $(call extract_funcs, core_log_default_function.c)
44 changes: 44 additions & 0 deletions src/test/core_log_default_function/TESTS.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!../env.py
# SPDX-License-Identifier: BSD-3-Clause
# Copyright 2024, Intel Corporation
#


import testframework as t
from testframework import granularity as g


@g.require_granularity(g.ANY)
class CORE_LOG(t.BaseTest):
test_type = t.Short

def run(self, ctx):
ctx.exec('core_log_default_function', self.test_case)


class TEST0(CORE_LOG):
test_case = 'test_CORE_LOG_BASIC'


class TEST1(CORE_LOG):
test_case = 'test_CORE_LOG_BASIC_W_ERRNO'


class TEST2(CORE_LOG):
test_case = 'test_CORE_LOG_BASIC_W_ERRNO_BAD'


class TEST3(CORE_LOG):
test_case = 'test_CORE_LOG_BASIC_LONG'


class TEST4(CORE_LOG):
test_case = 'test_CORE_LOG_BASIC_TOO_LONG'


class TEST5(CORE_LOG):
test_case = 'test_CORE_LOG_BASIC_TOO_LONG_W_ERRNO'


class TEST6(CORE_LOG):
test_case = 'test_CORE_LOG_TRESHOLD'
151 changes: 151 additions & 0 deletions src/test/core_log_default_function/core_log_default_function.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2024, Intel Corporation */

/*
* core_log_default_function.c -- unit test for core_log_default_function
*/

#undef _GNU_SOURCE
#include <string.h>
#include <syslog.h>

#include "unittest.h"
#include "log_internal.h"
#include "log_default.h"

#define NO_ARGS_CONSUMED 0

// #define CORE_LOG_UT_MAX_BUFF_SIZE 4096

FUNC_MOCK(snprintf, int, char *__restrict __s, size_t __maxlen,
const char *__restrict __format, ...)
/* file info */
FUNC_MOCK_RUN(0) {
va_list arg;
va_start(arg, __format);
UT_ASSERTeq(strcmp("%s: %3d: %s: ", __format), 0);
strncpy(__s, "DUMMY FILE_INFO", __maxlen);
return 10;
va_end(arg);
}
/* get time prefix */
FUNC_MOCK_RUN(1) {
va_list arg;
va_start(arg, __format);
UT_ASSERTeq(strcmp("%s.%06ld ", __format), 0);
strncpy(__s, "DUMMY TIMESTAMP ", __maxlen);
va_end(arg);
}
FUNC_MOCK_RUN(2) {
va_list arg;
va_start(arg, __format);
UT_OUT("%s", __format);
// UT_ASSERTeq(strcmp("%s[%ld] %s%s%s\n", __format), 0);
return vsnprintf(__s, __maxlen, __format, arg);
va_end(arg);

}
FUNC_MOCK_RUN_DEFAULT {
va_list arg;
va_start(arg, __format);
UT_OUT("%d", RCOUNTER(snprintf));
return vsnprintf(__s, __maxlen, __format, arg);
va_end(arg);
}
FUNC_MOCK_END

static int Syslog_no_of_calls;
static char Syslog_log_level_name[9] = "";
static const char *Syslog_message;

FUNC_MOCK(syslog, void, int __pri, const char *__fmt, ...)
FUNC_MOCK_RUN_DEFAULT {
va_list arg;
va_start(arg, __fmt);
UT_ASSERTeq(__pri, LOG_ERR);
UT_ASSERTeq(strcmp("%s%s%s", __fmt), 0);
char *log_level_name = va_arg(arg, char *);
UT_ASSERTeq(strncmp(Syslog_log_level_name, &log_level_name[1],
strlen(Syslog_log_level_name)), 0);
char *file_info = va_arg(arg, char *);
UT_ASSERTeq(strcmp("DUMMY FILE_INFO", file_info), 0);
char *message = va_arg(arg, char *);
UT_ASSERTeq(strcmp(Syslog_message, message), 0);
va_end(arg);
Syslog_no_of_calls ++;
}
FUNC_MOCK_END

#define CORE_LOG_UT_MESSAGE "Test message"

#if 0
#define CORE_LOG_UT_MESSAGE_LONG \
"Test message long 20Test message long 40Test message long 60" \
"Test message long 80Test message long100Test message long120" \
"Test message long140Test message long160Test message long180" \
"Test message long200Test message long220Test message long240" \
"Test message long260Test message long280Test message long300"

#define CORE_LOG_UT_MESSAGE_TOO_LONG CORE_LOG_UT_MESSAGE_LONG \
"Test message long 321"
#endif

/* tests */

#define TEST_SETUP(message_to_test) \
Syslog_message = message_to_test;

#define TEST_STEP_SETUP(level) \
Syslog_no_of_calls = 0; \
strcpy(Syslog_log_level_name, #level)

#define TEST_STEP_CHECK() \
UT_ASSERTeq(Syslog_no_of_calls, 1)

#define TEST_STEP(LEVEL, MESSAGE) \
TEST_STEP_SETUP(LEVEL); \
core_log_default_function(NULL, CORE_LOG_LEVEL_ERROR, \
"file name", 123, "function_name", MESSAGE); \
TEST_STEP_CHECK()

/* basic tests with a normal message */
static int
test_1(const struct test_case *tc, int argc, char *argv[])
{
TEST_SETUP(CORE_LOG_UT_MESSAGE);
TEST_STEP(ERROR, CORE_LOG_UT_MESSAGE);
return NO_ARGS_CONSUMED;
}

/* test to check that core_log_to_last() works w/ every thresholds */
static int
test_CORE_LOG_TRESHOLD(const struct test_case *tc, int argc, char *argv[])
{
// core_log_set_function(CORE_LOG_USE_DEFAULT_FUNCTION, NULL);
TEST_SETUP(CORE_LOG_UT_MESSAGE);
for (enum core_log_level level = CORE_LOG_LEVEL_HARK;
level < CORE_LOG_LEVEL_MAX; level ++) {
core_log_set_threshold(CORE_LOG_THRESHOLD, level);
/* must be in one line for proper __LINE__ value */
}
return NO_ARGS_CONSUMED;
}

static struct test_case test_cases[] = {
TEST_CASE(test_1),
TEST_CASE(test_CORE_LOG_TRESHOLD),
};

/* force the test to use the default logging function */
#undef LOG_SET_PMEMCORE_FUNC
#define LOG_SET_PMEMCORE_FUNC

int
main(int argc, char *argv[])
{
START(argc, argv, "core_log_default_function");

TEST_CASE_PROCESS(argc, argv, test_cases, ARRAY_SIZE(test_cases));

DONE(NULL);
}
100 changes: 50 additions & 50 deletions src/test/core_log_max/call_all.c.generated
Original file line number Diff line number Diff line change
Expand Up @@ -360,73 +360,73 @@ call_all_ERR_WO_ERRNO(void)
ERR_WO_ERRNO("error initializing lock");
// src/libpmemobj/sync.c:50
ERR_WO_ERRNO("error setting lock runid");
// src/libpmemobj/tx.c:1236
// src/libpmemobj/tx.c:1237
ERR_WO_ERRNO("snapshot size too large");
// src/libpmemobj/tx.c:1243
// src/libpmemobj/tx.c:1244
ERR_WO_ERRNO("object outside of heap");
// src/libpmemobj/tx.c:1383
// src/libpmemobj/tx.c:1384
ERR_WO_ERRNO("out of memory");
// src/libpmemobj/tx.c:1410
// src/libpmemobj/tx.c:1411
ERR_WO_ERRNO("object outside of pool");
// src/libpmemobj/tx.c:1448
// src/libpmemobj/tx.c:1449
ERR_WO_ERRNO("unknown flags 0x%lx", _lu);
// src/libpmemobj/tx.c:1456
// src/libpmemobj/tx.c:1457
ERR_WO_ERRNO("object outside of pool");
// src/libpmemobj/tx.c:1493
// src/libpmemobj/tx.c:1494
ERR_WO_ERRNO("invalid pool uuid");
// src/libpmemobj/tx.c:1531
// src/libpmemobj/tx.c:1532
ERR_WO_ERRNO("unknown flags 0x%lx", _lu);
// src/libpmemobj/tx.c:1539
// src/libpmemobj/tx.c:1540
ERR_WO_ERRNO("invalid pool uuid");
// src/libpmemobj/tx.c:1576
// src/libpmemobj/tx.c:1577
ERR_WO_ERRNO("allocation with size 0");
// src/libpmemobj/tx.c:1607
// src/libpmemobj/tx.c:1608
ERR_WO_ERRNO("allocation with size 0");
// src/libpmemobj/tx.c:1638
// src/libpmemobj/tx.c:1639
ERR_WO_ERRNO("allocation with size 0");
// src/libpmemobj/tx.c:1645
// src/libpmemobj/tx.c:1646
ERR_WO_ERRNO("unknown flags 0x%lx", _lu);
// src/libpmemobj/tx.c:1714
// src/libpmemobj/tx.c:1715
ERR_WO_ERRNO("unknown flags 0x%lx", _lu);
// src/libpmemobj/tx.c:1722
// src/libpmemobj/tx.c:1723
ERR_WO_ERRNO("cannot duplicate NULL string");
// src/libpmemobj/tx.c:1772
// src/libpmemobj/tx.c:1773
ERR_WO_ERRNO("unknown flags 0x%lx", _lu);
// src/libpmemobj/tx.c:1780
// src/libpmemobj/tx.c:1781
ERR_WO_ERRNO("cannot duplicate NULL string");
// src/libpmemobj/tx.c:1831
// src/libpmemobj/tx.c:1832
ERR_WO_ERRNO("unknown flags 0x%lx", _lu);
// src/libpmemobj/tx.c:1842
// src/libpmemobj/tx.c:1843
ERR_WO_ERRNO("invalid pool uuid");
// src/libpmemobj/tx.c:1914
// src/libpmemobj/tx.c:1915
ERR_WO_ERRNO("unknown flags 0x%lx", _lu);
// src/libpmemobj/tx.c:1959
// src/libpmemobj/tx.c:1960
ERR_WO_ERRNO("unknown flags 0x%lx", _lu);
// src/libpmemobj/tx.c:2208
// src/libpmemobj/tx.c:2209
ERR_WO_ERRNO("invalid cache size, must be between 0 and max alloc size");
// src/libpmemobj/tx.c:2232
// src/libpmemobj/tx.c:2233
ERR_WO_ERRNO("tx.cache.threshold parameter is deprecated");
// src/libpmemobj/tx.c:2247
// src/libpmemobj/tx.c:2248
ERR_WO_ERRNO("tx.cache.threshold parameter is deprecated");
// src/libpmemobj/tx.c:513
// src/libpmemobj/tx.c:514
ERR_WO_ERRNO("Unrecognized lock type");
// src/libpmemobj/tx.c:550
// src/libpmemobj/tx.c:551
ERR_WO_ERRNO("Unrecognized lock type");
// src/libpmemobj/tx.c:588
// src/libpmemobj/tx.c:589
ERR_WO_ERRNO("requested size too large");
// src/libpmemobj/tx.c:617
// src/libpmemobj/tx.c:618
ERR_WO_ERRNO("out of memory");
// src/libpmemobj/tx.c:633
// src/libpmemobj/tx.c:634
ERR_WO_ERRNO("requested size too large");
// src/libpmemobj/tx.c:647
// src/libpmemobj/tx.c:648
ERR_WO_ERRNO("pmemobj_tx_free failed");
// src/libpmemobj/tx.c:665
// src/libpmemobj/tx.c:666
ERR_WO_ERRNO("pmemobj_tx_free failed");
// src/libpmemobj/tx.c:682
// src/libpmemobj/tx.c:683
ERR_WO_ERRNO("Buffer from a different pool");
// src/libpmemobj/tx.c:739
// src/libpmemobj/tx.c:740
ERR_WO_ERRNO("nested transaction for different pool");
// src/libpmemobj/tx.c:865
// src/libpmemobj/tx.c:866
ERR_WO_ERRNO("unknown flags 0x%lx", _lu);
// src/libpmemobj/ulog.c:245
ERR_WO_ERRNO("cannot auto reserve next ulog");
Expand Down Expand Up @@ -769,23 +769,23 @@ call_all_CORE_LOG_FATAL(void)
CORE_LOG_FATAL("pmemobj_mutex_unlock");
// src/libpmemobj/sync.h:87
CORE_LOG_FATAL("pmemobj_mutex_lock");
// src/libpmemobj/tx.c:1049
// src/libpmemobj/tx.c:1050
CORE_LOG_FATAL("pmemobj_tx_end called without pmemobj_tx_commit");
// src/libpmemobj/tx.c:1053
// src/libpmemobj/tx.c:1054
CORE_LOG_FATAL("pmemobj_tx_end called without pmemobj_tx_begin");
// src/libpmemobj/tx.c:161
// src/libpmemobj/tx.c:162
CORE_LOG_FATAL("%s called outside of transaction", _s);
// src/libpmemobj/tx.c:272
// src/libpmemobj/tx.c:273
CORE_LOG_FATAL("Malloc");
// src/libpmemobj/tx.c:284
// src/libpmemobj/tx.c:285
CORE_LOG_FATAL("Malloc");
// src/libpmemobj/tx.c:321
// src/libpmemobj/tx.c:322
CORE_LOG_FATAL("Malloc");
// src/libpmemobj/tx.c:574
// src/libpmemobj/tx.c:575
CORE_LOG_FATAL("invalid state of ranges tree");
// src/libpmemobj/tx.c:769
// src/libpmemobj/tx.c:770
CORE_LOG_FATAL("Invalid stage %d to begin new transaction", _d);
// src/libpmemobj/tx.c:807
// src/libpmemobj/tx.c:808
CORE_LOG_FATAL("transaction callback is already set, old %p new %p old_arg %p new_arg %p", _p, _p, _p, _p);
}

Expand Down Expand Up @@ -1087,16 +1087,16 @@ call_all_ERR_W_ERRNO(int errnum)
// src/libpmemobj/stats.c:124
ERR_W_ERRNO("Malloc");
UT_ASSERTeq(errno, errnum);
// src/libpmemobj/tx.c:499
// src/libpmemobj/tx.c:500
ERR_W_ERRNO("pmemobj_mutex_lock");
UT_ASSERTeq(errno, errnum);
// src/libpmemobj/tx.c:508
// src/libpmemobj/tx.c:509
ERR_W_ERRNO("pmemobj_rwlock_wrlock");
UT_ASSERTeq(errno, errnum);
// src/libpmemobj/tx.c:776
// src/libpmemobj/tx.c:777
ERR_W_ERRNO("Malloc");
UT_ASSERTeq(errno, errnum);
// src/libpmemobj/tx.c:945
// src/libpmemobj/tx.c:946
ERR_W_ERRNO("explicit transaction abort");
UT_ASSERTeq(errno, errnum);
}
Expand Down Expand Up @@ -1234,13 +1234,13 @@ call_all_CORE_LOG_FATAL_W_ERRNO(int errnum)
// src/libpmemobj/sync.h:87
CORE_LOG_FATAL_W_ERRNO("pmemobj_mutex_lock");
UT_ASSERTeq(errno, errnum);
// src/libpmemobj/tx.c:272
// src/libpmemobj/tx.c:273
CORE_LOG_FATAL_W_ERRNO("Malloc");
UT_ASSERTeq(errno, errnum);
// src/libpmemobj/tx.c:284
// src/libpmemobj/tx.c:285
CORE_LOG_FATAL_W_ERRNO("Malloc");
UT_ASSERTeq(errno, errnum);
// src/libpmemobj/tx.c:321
// src/libpmemobj/tx.c:322
CORE_LOG_FATAL_W_ERRNO("Malloc");
UT_ASSERTeq(errno, errnum);
}

0 comments on commit ff0641e

Please sign in to comment.