Skip to content

Commit 94ba49b

Browse files
committed
Adding unit tests
1 parent c2a4fb4 commit 94ba49b

5 files changed

Lines changed: 79 additions & 1 deletion

File tree

src/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,4 +327,23 @@ target_compile_definitions(unit_test_renderd_config PRIVATE
327327
target_include_directories(unit_test_renderd_config PRIVATE ${PROJECT_SOURCE_DIR}/src ${PROJECT_SOURCE_DIR}/tests)
328328
target_link_libraries(unit_test_renderd_config ${unit_test_renderd_config_LIBS})
329329

330+
331+
#-----------------------------------------------------------------------------
332+
#
333+
# unit_test_sys_utils
334+
#
335+
#-----------------------------------------------------------------------------
336+
337+
set(unit_test_sys_utils_SRCS
338+
$<TARGET_OBJECTS:catch_main_o>
339+
${PROJECT_SOURCE_DIR}/tests/unit_test_sys_utils.cpp
340+
)
341+
set(unit_test_sys_utils_LIBS
342+
${GLIB_LIBRARIES}
343+
)
344+
add_executable(unit_test_sys_utils ${unit_test_sys_utils_SRCS})
345+
set_target_properties(unit_test_sys_utils PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/tests)
346+
target_include_directories(unit_test_sys_utils PRIVATE ${PROJECT_SOURCE_DIR}/src ${PROJECT_SOURCE_DIR}/tests)
347+
target_link_libraries(unit_test_sys_utils ${unit_test_sys_utils_LIBS})
348+
330349
endif()

tests/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,11 @@ add_test(
245245
COMMAND unit_test_renderd_config
246246
)
247247

248+
add_test(
249+
NAME unit_test_sys_utils
250+
COMMAND unit_test_sys_utils
251+
)
252+
248253
foreach(STORAGE_BACKEND_INDEX RANGE ${STORAGE_BACKENDS_LENGTH})
249254
# Get STORAGE_BACKEND from STORAGE_BACKENDS list
250255
list(GET STORAGE_BACKENDS ${STORAGE_BACKEND_INDEX} STORAGE_BACKEND)

tests/catch_test_common.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ bool fail_next_asprintf = false;
4444
bool fail_next_connect = false;
4545
bool fail_next_getaddrinfo = false;
4646
bool fail_next_getaddrinfo_empty_res = false;
47+
bool fail_next_getloadavg = false;
4748
bool fail_next_malloc = false;
4849
bool fail_next_mkdir = false;
4950
bool fail_next_open = false;
@@ -156,7 +157,6 @@ void start_capture(bool debug)
156157
setenv("G_MESSAGES_DEBUG", "all", 1);
157158
#if GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 79
158159
// https://gitlab.gnome.org/GNOME/glib/-/merge_requests/3710
159-
std::cout << "Resetting G_MESSAGES_DEBUG env var in runtime no longer has an effect.\n";
160160
const gchar *domains[] = {"all", NULL};
161161
g_log_writer_default_set_debug_domains(domains);
162162
#endif
@@ -170,6 +170,7 @@ std::tuple<std::string, std::string> end_capture(bool print)
170170
{
171171
setenv("G_MESSAGES_DEBUG", "", 1);
172172
#if GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 79
173+
// https://gitlab.gnome.org/GNOME/glib/-/merge_requests/3710
173174
g_log_writer_default_set_debug_domains(NULL);
174175
#endif
175176
foreground = 0;
@@ -343,6 +344,16 @@ extern "C" {
343344
return getaddrinfo(node, service, hints, res);
344345
}
345346

347+
int mocked_getloadavg(double loadavg[], int nelem)
348+
{
349+
if (fail_next_getloadavg) {
350+
fail_next_getloadavg = false;
351+
return -1;
352+
}
353+
354+
return getloadavg(loadavg, nelem);
355+
}
356+
346357
void *mocked_malloc(size_t size)
347358
{
348359
if (fail_next_malloc) {

tests/catch_test_common.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ extern "C" {
5555
int mocked_asprintf(char **strp, const char *fmt, ...);
5656
int mocked_connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen);
5757
int mocked_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res);
58+
int mocked_getloadavg(double loadavg[], int nelem);
5859
void *mocked_malloc(size_t size);
5960
int mocked_mkdir(const char *path, mode_t mode);
6061
int mocked_open(const char *pathname, int flags, ...);

tests/unit_test_sys_utils.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <fcntl.h>
2+
#include <stdlib.h>
3+
#include <string>
4+
#include <unistd.h>
5+
6+
#include "catch/catch.hpp"
7+
#include "catch_test_common.hpp"
8+
#include "sys_utils.h"
9+
10+
extern bool fail_next_getloadavg;
11+
extern std::string err_log_lines;
12+
13+
#define g_logger mocked_g_logger
14+
#define getloadavg mocked_getloadavg
15+
16+
#include "sys_utils.c"
17+
18+
#undef g_logger
19+
#undef getloadavg
20+
21+
TEST_CASE("get_load_avg function", "[get_load_avg]")
22+
{
23+
err_log_lines.clear();
24+
25+
SECTION("get_load_avg", "should return positive") {
26+
double loadavg = get_load_avg();
27+
28+
REQUIRE(loadavg > 0);
29+
}
30+
31+
SECTION("get_load_avg with unobtainable load average", "should return 1000") {
32+
fail_next_getloadavg = true;
33+
34+
double loadavg = get_load_avg();
35+
36+
#ifdef HAVE_GETLOADAVG
37+
REQUIRE(loadavg == 1000);
38+
#else
39+
REQUIRE(loadavg > 0);
40+
#endif
41+
}
42+
}

0 commit comments

Comments
 (0)