Commit 69f407c6 authored by Marek Vavruša's avatar Marek Vavruša

tests: integration tests mock PyModule, build system

The integration tests will run from Python, from here a mock resolution
context is imported, so the Python module can modify time/recursion
endpoints.
parent 5c3a84df
......@@ -3,7 +3,7 @@ AC_PREREQ(2.60)
# Version/identifiers
m4_define([VERSION_MAJOR],[0])
m4_define([VERSION_MINOR],[0])
m4_define([VERSION_MICRO],[1])
m4_define([VERSION_MICRO],[2])
AC_INIT(knot-resolver, m4_defn([VERSION_MAJOR]).m4_defn([VERSION_MINOR]).m4_defn([VERSION_MICRO]), knot-dns@lists.nic.cz, knot-resolver)
AM_INIT_AUTOMAKE([gnits subdir-objects dist-xz -Wall -Werror])
AM_SILENT_RULES([yes])
......@@ -21,9 +21,7 @@ CFLAGS="$CFLAGS -Wall -Werror=format-security"
# Checks for programs.
m4_ifdef([AM_PROG_AR], [AM_PROG_AR]) # call AM_PROG_AR only if available
# Initialize libtool
AC_DISABLE_STATIC
AC_PROG_LIBTOOL
# Linker
LT_INIT
# Use pkg-config
......@@ -34,7 +32,14 @@ AC_CHECK_FUNCS([nftw, mkdtemp])
PKG_CHECK_MODULES([libknot], [libknot])
PKG_CHECK_MODULES([libuv], [libuv], [build_daemon=yes], [build_daemon=no])
PKG_CHECK_MODULES([cmocka], [cmocka], [build_tests=yes], [build_tests=no])
# Check for python (integration tests)
AX_PYTHON_DEVEL([>= '2.5.0'])
if test "x$PYTHON" != x ; then build_integration=yes; else build_integration=no; fi
# Set up conditionals
AM_CONDITIONAL([BUILD_TESTS], [test "$build_tests" == "yes"])
AM_CONDITIONAL([BUILD_INTEGRATION], [test "$build_integration" == "yes"])
AM_CONDITIONAL([BUILD_DAEMON], [test "$build_daemon" == "yes"])
# Search other libraries
......@@ -55,7 +60,7 @@ AC_MSG_RESULT([
Features:
---------
Build tests: ${build_tests}
Build tests: ${build_tests} (integration ${build_integration})
Build daemon: ${build_daemon}
Continue with 'make' command
......
......@@ -37,4 +37,5 @@ tests/Makefile.am
tests/test.h
tests/test_cache.c
tests/test_context.c
tests/test_integration.c
tests/test_resolve.c
This diff is collapsed.
# Unit tests
if BUILD_TESTS
AM_CPPFLAGS = \
-include $(top_builddir)/config.h \
-I$(top_srcdir)/lib
-I$(top_srcdir)/lib \
$(libknot_CFLAGS) \
$(cmocka_CFLAGS)
LDADD = \
$(top_builddir)/lib/libknotresolve.la \
$(libuv_LIBS) \
$(libknot_LIBS) \
$(cmocka_LIBS)
check_PROGRAMS = \
test_cache \
test_context \
test_resolve
check-compile-only: $(check_PROGRAMS)
check-local: $(check_PROGRAMS)
check-local-exec: $(check_PROGRAMS)
@echo "---- Executing unit tests ----"
$(top_builddir)/tests/runtests -b $(top_builddir)/tests $(check_PROGRAMS)
endif
# Integration tests
if BUILD_INTEGRATION
noinst_LTLIBRARIES = _test_integration.la
_test_integration_la_SOURCES = test_integration.c
_test_integration_la_CPPFLAGS = $(PYTHON_CPPFLAGS) $(AM_CPPFLAGS)
_test_integration_la_LDFLAGS = -rpath $(abs_builddir) -module -export-dynamic -shared -avoid-version \
--wrap=gettimeofday $(PYTHON_LDFLAGS)
_test_integration_la_LIBADD = $(LDADD)
convenience-link: $(noinst_LTLIBRARIES)
@for soname in `echo | $(EGREP) "^dlname=" $(noinst_LTLIBRARIES) | $(SED) -e "s|^dlname='\(.*\)'|\1|"`; do \
rm -f $(abs_builddir)/$$soname; $(LN_S) $(abs_builddir)/.libs/$$soname $$soname || true;\
done
check-local-integration: convenience-link
@echo "---- Executing integration tests ----"
@$(LIBTOOL) --mode=execute -dlopen $(top_builddir)/lib/libknotresolve.la ./test_integration.py testdata
clean-local:
@for soname in `echo | $(EGREP) "^dlname=" $(noinst_LTLIBRARIES) | $(SED) -e "s|^dlname='\(.*\)'|\1|"`; do \
test -L $(abs_builddir)/$$soname && rm -f $(abs_builddir)/$$soname || true; \
done
endif
check-local: check-local-exec check-local-integration
......@@ -31,7 +31,6 @@ const char *global_env;
/* Test cache open */
static void test_open(void **state)
{
printf("OPENSDFDSFSDFSFSDFSDF\n");
*state = kr_cache_open(global_env, &global_mm, CACHE_SIZE);
assert_non_null(*state);
}
......
/* Copyright (C) 2014 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include <Python.h>
#include <libknot/descriptor.h>
#include <libknot/packet/pkt.h>
#include <libknot/internal/net.h>
#include "tests/test.h"
#include "lib/resolve.h"
/*
* Globals
*/
mm_ctx_t global_mm; /* Test memory context */
struct kr_context global_context; /* Resolution context */
const char *global_tmpdir = NULL; /* Temporary directory */
struct timeval _mock_time; /* Mocked system time */
int _mock_fd; /* Mocked endpoint for recursive queries */
/*
* PyModule implementation.
*/
static PyObject* init(PyObject* self, PyObject* args)
{
#define CACHE_SIZE 100*1024
test_mm_ctx_init(&global_mm);
kr_context_init(&global_context, &global_mm);
global_tmpdir = test_tmpdir_create();
global_context.cache = kr_cache_open(global_tmpdir, &global_mm, CACHE_SIZE);
if (global_context.cache == NULL) {
test_tmpdir_remove(global_tmpdir);
kr_context_deinit(&global_context);
return NULL;
}
return Py_BuildValue("s", PACKAGE_STRING " (integration tests)");
}
static PyObject* deinit(PyObject* self, PyObject* args)
{
if (global_tmpdir == NULL) {
return NULL;
}
kr_cache_close(global_context.cache);
kr_context_deinit(&global_context);
test_tmpdir_remove(global_tmpdir);
global_tmpdir = NULL;
return Py_BuildValue("");
}
static PyObject* resolve(PyObject *self, PyObject *args)
{
const char *query_wire = NULL;
size_t query_size = 0;
if (!PyArg_ParseTuple(args, "s#", &query_wire, &query_size)) {
return NULL;
}
/* Prepare input */
knot_pkt_t *query = knot_pkt_new((uint8_t *)query_wire, query_size, &global_mm);
assert(query);
int ret = knot_pkt_parse(query, 0);
if (ret != KNOT_EOK) {
knot_pkt_free(&query);
return NULL;
}
/* Resolve query */
knot_pkt_t *answer = knot_pkt_new(NULL, KNOT_WIRE_MAX_PKTSIZE, &global_mm);
assert(answer);
knot_pkt_init_response(answer, query);
ret = kr_resolve(&global_context, answer, knot_pkt_qname(query),
knot_pkt_qclass(query), knot_pkt_qtype(query));
/* Return wire and cleanup. */
PyObject *out = Py_BuildValue("s#", answer->wire, answer->size);
knot_pkt_free(&answer);
knot_pkt_free(&query);
return out;
}
static PyObject* set_time(PyObject *self, PyObject *args)
{
unsigned long arg_time = 0;
if (!PyArg_ParseTuple(args, "k", &arg_time)) {
return NULL;
}
_mock_time.tv_sec = arg_time;
_mock_time.tv_usec = 0;
return Py_BuildValue("");
}
static PyObject* set_endpoint(PyObject *self, PyObject *args)
{
int fd = PyObject_AsFileDescriptor(args);
if (fd < 0) {
return NULL;
}
_mock_fd = fd;
return Py_BuildValue("");
}
static PyMethodDef module_methods[] = {
{"init", init, METH_VARARGS, "Initialize resolution context."},
{"deinit", deinit, METH_VARARGS, "Clean up resolution context."},
{"resolve", resolve, METH_VARARGS, "Resolve query."},
{"set_time", set_time, METH_VARARGS, "Set mock system time."},
{"set_endpoint", set_endpoint, METH_VARARGS, "Set endpoint for recursive queries."},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC init_test_integration(void)
{
(void) Py_InitModule("_test_integration", module_methods);
}
/*
* Mock symbol reimplementation.
* These effectively allow to manipulate time/networking during resolution.
*/
int __wrap_gettimeofday(struct timeval *tv, struct timezone *tz)
{
memcpy(tv, &_mock_time, sizeof(struct timeval));
return 0;
}
int net_unbound_socket(int type, const struct sockaddr_storage *ss)
{
char addr_str[SOCKADDR_STRLEN];
sockaddr_tostr(addr_str, sizeof(addr_str), ss);
fprintf(stderr, "%s (%d, %s)\n", __func__, type, addr_str);
return _mock_fd;
}
int net_bound_socket(int type, const struct sockaddr_storage *ss)
{
char addr_str[SOCKADDR_STRLEN];
sockaddr_tostr(addr_str, sizeof(addr_str), ss);
fprintf(stderr, "%s (%d, %s)\n", __func__, type, addr_str);
return _mock_fd;
}
int net_connected_socket(int type, const struct sockaddr_storage *dst_addr,
const struct sockaddr_storage *src_addr, unsigned flags)
{
char dst_addr_str[SOCKADDR_STRLEN], src_addr_str[SOCKADDR_STRLEN];
sockaddr_tostr(dst_addr_str, sizeof(dst_addr_str), dst_addr);
sockaddr_tostr(src_addr_str, sizeof(src_addr_str), src_addr);
fprintf(stderr, "%s (%d, %s, %s, %u)\n", __func__, type, dst_addr_str, src_addr_str, flags);
return _mock_fd;
}
int net_is_connected(int fd)
{
fprintf(stderr, "%s (%d)\n", __func__, fd);
return true;
}
#!/usr/bin/env python
import sys
import _test_integration
print dir(_test_integration)
print sys.argv
print _test_integration.init()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment