Skip to content

Commit

Permalink
Extract unit tests to a separate module
Browse files Browse the repository at this point in the history
related to:
#5 - Load balancing
  • Loading branch information
vdmit11 committed Oct 7, 2014
1 parent f74e132 commit 75d3221
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 32 deletions.
10 changes: 1 addition & 9 deletions tempesta_fw/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,4 @@ tempesta_fw-objs = cache.o classifier.o client.o connection.o filter.o gfsm.o \
session.o sock_backend.o sock_frontend.o stress.o addr.o debugfs.o \
pool.o str.o

# FIXME:
# This may be a bit confusing because usually such sub-directories have their
# own Makefile files. We need to decide how to properly integrate these
# sub-directories to the build system.
#
tempesta_fw-objs += t/test_tfw_str.o t/runner.o
tempesta_fw-objs +=

obj-m += log/ classifier/ stress/ sched/ filter/
obj-m += log/ classifier/ stress/ sched/ filter/ t/
8 changes: 0 additions & 8 deletions tempesta_fw/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ void tfw_connection_exit(void);
int tfw_sched_dummy_init(void);
void tfw_sched_dummy_exit(void);

#ifdef DEBUG
extern void tfw_run_all_tests(void);
#else
#define tfw_run_all_tests()
#endif


static int __init
tfw_init(void)
Expand Down Expand Up @@ -104,8 +98,6 @@ tfw_init(void)
if (r)
goto err_connection;

tfw_run_all_tests();

return 0;

err_connection:
Expand Down
1 change: 1 addition & 0 deletions tempesta_fw/str.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ tfw_str_len(const TfwStr *str)

return total_len;
}
EXPORT_SYMBOL(tfw_str_len);

static bool
str_eq_cstr(const TfwStr *str, const char *cstr, int cstr_len, bool ci)
Expand Down
7 changes: 3 additions & 4 deletions tempesta_fw/t/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
# this program; if not, write to the Free Software Foundation, Inc., 59
# Temple Place - Suite 330, Boston, MA 02111-1307, USA.

EXTRA_CFLAGS += -I$(src)/../sync_socket -I$(src)/../tempesta_db -DDEBUG

obj-y += runner.o test_tfw_str.o

EXTRA_CFLAGS += -I$(src)/../ -I$(src)/../../tempesta_db -DDEBUG

obj-m += tfw_test.o
tfw_test-objs = main.o str.o test.o
54 changes: 54 additions & 0 deletions tempesta_fw/t/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* Tempesta FW
*
* Copyright (C) 2012-2014 NatSys Lab. (info@natsys-lab.com).
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59
* Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include <linux/module.h>

#include "tempesta.h"
#include "test.h"

MODULE_AUTHOR(TFW_AUTHOR);
MODULE_DESCRIPTION("Tempesta FW tests");
MODULE_VERSION("0.0.1");
MODULE_LICENSE("GPL");

static int __init
tfw_test_init(void)
{
int fail_count = 0;

printk("tfw_test: start\n");
fail_count = run_all_tests();

printk("tfw_test: finish - ");
if (fail_count)
printk("failed %d assertions\n", fail_count);
else
printk("all passed\n");

return 0;
}

void
tfw_test_exit(void)
{
}

module_init(tfw_test_init);
module_exit(tfw_test_exit);
9 changes: 9 additions & 0 deletions tempesta_fw/t/run_all_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
#
# A script that simply runs all tests for Tempesta FW.
#
# 2012-2014. Written by NatSys Lab. (info@natsys-lab.com).

insmod $(dirname $0)/tfw_test.ko
rmmod tfw_test
dmesg | tac | grep -m 1 -B 200 "tfw_test: start" | tac
File renamed without changes.
17 changes: 15 additions & 2 deletions tempesta_fw/t/runner.c → tempesta_fw/t/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,25 @@
* Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include <linux/module.h>
#include "test.h"

int test_fail_counter = 0;

TEST_SUITE(tfw_str);

void
tfw_run_all_tests(void)
int
run_all_tests(void)
{
test_fail_counter = 0;

RUN_TEST_SUITE(tfw_str);

return test_fail_counter;
}

void
register_test_failure(void)
{
++test_fail_counter;
}
24 changes: 15 additions & 9 deletions tempesta_fw/t/test.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,44 @@
#ifndef __TFW_TEST_H__
#define __TFW_TEST_H__

#include <linux/kernel.h>

int run_all_tests(void);
void register_test_failure(void);

#define TEST(unit, assertion) static void test__ ##unit ##__ ##assertion(void)
#define RUN_TEST(unit, assertion) test__ ##unit ##__ ##assertion()

#define TEST_SUITE(name) void test_suite__##name(void)
#define RUN_TEST_SUITE(name) test_suite__##name()
#define RUN_TEST_SUITE(name) \
do { \
printk("RUN_SUITE(%s)\n", #name); \
test_suite__##name(); \
} while (0)

#define __FAIL_MSG(...) \
#define __FAIL(...) \
do { \
printk("FAIL: %s():%d: ", __func__, __LINE__); \
printk("FAIL:\n"); \
printk(" %s():%d\n ", __func__, __LINE__); \
printk(__VA_ARGS__); \
printk("\n"); \
register_test_failure(); \
} while (0)

#define FAIL() __FAIL_MSG("FAIL()");
#define FAIL() __FAIL("FAIL()");

#define EXPECT_TRUE(cond) \
do { \
bool _test_val = (cond); \
if (_test_val) \
break; \
__FAIL_MSG("EXPECT_TRUE(%s) => %d", #cond, _test_val); \
__FAIL("EXPECT_TRUE(%s) => %d", #cond, _test_val); \
} while (0)

#define EXPECT_FALSE(cond) \
do { \
bool _test_val = (cond); \
if (!_test_val) \
break; \
__FAIL_MSG("EXPECT_FALSE(%s) => %d", #cond, _test_val); \
__FAIL("EXPECT_FALSE(%s) => %d", #cond, _test_val); \
} while (0)

#define __EXPECT_CMP(name, expr1, expr2, cmp_expr) \
Expand All @@ -68,7 +74,7 @@ do { \
unsigned long _val2 = (expr2); \
if (cmp_expr) \
break; \
__FAIL_MSG("%s(%s, %s) => (%#lx, %#lx)", \
__FAIL("%s(%s, %s) => (%#lx, %#lx)", \
name, #expr1, #expr2, _val1, _val2); \
} while (0)

Expand Down

0 comments on commit 75d3221

Please sign in to comment.