[ovs-dev] [kmod-test V2 2/3] autotest: add autotest framework for adding kernel module unit tests

Andy Zhou azhou at nicira.com
Fri Jan 16 02:12:47 UTC 2015


This patch adds a basic infrastructure for developing and running
kernel module unit tests. Currently OVS contains thousands
of useful unit tests for user space programs. It is desirable to
have corresponding kernel module unit tests.

This commit adds basic framework for adding kernel module tests. Like
user space unit tests, Kmod tests are based autotest framework, thus
are similar to existing unit tests. For references, kmod-traffic.at
contains a simple ping test.

"make check-kmod" can be invoked on any build machine as a root
user. Since kernel testing can potentially crash the kernel, it is
not recommended to run those tests directly on a development machine,
but rather a testing VM, such as ones can be launched by vagrant.

Signed-off-by: Andy Zhou <azhou at nicira.com>
---
 tests/automake.mk       | 35 ++++++++++++++++++++---
 tests/kmod-macros.at    | 74 +++++++++++++++++++++++++++++++++++++++++++++++++
 tests/kmod-testsuite.at | 22 +++++++++++++++
 tests/kmod-traffic.at   | 16 +++++++++++
 4 files changed, 143 insertions(+), 4 deletions(-)
 create mode 100644 tests/kmod-macros.at
 create mode 100644 tests/kmod-testsuite.at
 create mode 100644 tests/kmod-traffic.at

diff --git a/tests/automake.mk b/tests/automake.mk
index fceb060..1fe0799 100644
--- a/tests/automake.mk
+++ b/tests/automake.mk
@@ -1,13 +1,20 @@
 EXTRA_DIST += \
+	$(COMMON_MACROS_AT) \
 	$(TESTSUITE_AT) \
+	$(KMOD_TESTSUITE_AT) \
 	$(TESTSUITE) \
+	$(KMOD_TESTSUITE) \
 	tests/atlocal.in \
 	$(srcdir)/package.m4 \
 	$(srcdir)/tests/testsuite
-TESTSUITE_AT = \
-	tests/testsuite.at \
+
+COMMON_MACROS_AT = \
 	tests/ovsdb-macros.at \
 	tests/ovs-macros.at \
+	tests/ofproto-macros.at
+
+TESTSUITE_AT = \
+	tests/testsuite.at \
 	tests/library.at \
 	tests/heap.at \
 	tests/bundle.at \
@@ -45,7 +52,6 @@ TESTSUITE_AT = \
 	tests/ofproto-dpif.at \
 	tests/bridge.at \
 	tests/vlan-splinters.at \
-	tests/ofproto-macros.at \
 	tests/ofproto.at \
 	tests/ovsdb.at \
 	tests/ovsdb-log.at \
@@ -73,7 +79,14 @@ TESTSUITE_AT = \
 	tests/interface-reconfigure.at \
 	tests/vlog.at \
 	tests/vtep-ctl.at
+
+KMOD_TESTSUITE_AT = \
+	tests/kmod-testsuite.at \
+	tests/kmod-macros.at \
+	tests/kmod-traffic.at
+
 TESTSUITE = $(srcdir)/tests/testsuite
+KMOD_TESTSUITE = $(srcdir)/tests/kmod-testsuite
 DISTCLEANFILES += tests/atconfig tests/atlocal
 
 AUTOTEST_PATH = utilities:vswitchd:ovsdb:vtep:tests
@@ -168,11 +181,25 @@ check-ryu: all
 	$(AM_V_at)srcdir='$(srcdir)' $(SHELL) $(srcdir)/tests/run-ryu
 EXTRA_DIST += tests/run-ryu
 
+# Run kmod tests. Assume kernel modules has been installed or linked into the kernel
+check-kernel: all tests/atconfig tests/atlocal $(KMOD_TESTSUITE)
+	$(SHELL) '$(KMOD_TESTSUITE)' -C tests  AUTOTEST_PATH='$(AUTOTEST_PATH)' -d $(TESTSUITEFLAGS)
+
+# Testing the out of tree Kernel module
+check-kmod: all tests/atconfig tests/atlocal $(KMOD_TESTSUITE)
+	$(MAKE) modules_install
+	rmmod openvswitch
+	$(MAKE) check-kernel
+
 clean-local:
 	test ! -f '$(TESTSUITE)' || $(SHELL) '$(TESTSUITE)' -C tests --clean
 
 AUTOTEST = $(AUTOM4TE) --language=autotest
-$(TESTSUITE): package.m4 $(TESTSUITE_AT)
+$(TESTSUITE): package.m4 $(TESTSUITE_AT) $(COMMON_MACROS_AT)
+	$(AM_V_GEN)$(AUTOTEST) -I '$(srcdir)' -o $@.tmp $@.at
+	$(AM_V_at)mv $@.tmp $@
+
+$(KMOD_TESTSUITE): package.m4 $(KMOD_TESTSUITE_AT) $(COMMON_MACROS_AT)
 	$(AM_V_GEN)$(AUTOTEST) -I '$(srcdir)' -o $@.tmp $@.at
 	$(AM_V_at)mv $@.tmp $@
 
diff --git a/tests/kmod-macros.at b/tests/kmod-macros.at
new file mode 100644
index 0000000..b792c0d
--- /dev/null
+++ b/tests/kmod-macros.at
@@ -0,0 +1,74 @@
+# OVS_KMOD_VSWITCHD_START([vsctl-args], [vsctl-output], [=override])
+#
+# Creates a database and starts ovsdb-server, starts ovs-vswitchd
+# connected to that database, calls ovs-vsctl to create a bridge named
+# br0 with predictable settings, passing 'vsctl-args' as additional
+# commands to ovs-vsctl.  If 'vsctl-args' causes ovs-vsctl to provide
+# output (e.g. because it includes "create" commands) then 'vsctl-output'
+# specifies the expected output after filtering through uuidfilt.pl.
+#
+m4_define([OVS_KMOD_VSWITCHD_START],
+  [ AT_CHECK([modprobe openvswitch])
+   _OVS_VSWITCHD_START([])
+   dnl Add bridges, ports, etc.
+   AT_CHECK([ovs-vsctl -- add-br br0 -- set bridge br0 protocols=[[OpenFlow10,OpenFlow11,OpenFlow12,OpenFlow13,OpenFlow14,OpenFlow15]] fail-mode=secure -- $1 m4_if([$2], [], [], [| ${PERL} $srcdir/uuidfilt.pl])], [0], [$2])
+])
+#
+#
+# OVS_KMOD_VSWITCHD_STOP([WHITELIST])
+#
+# Gracefully stops ovs-vswitchd and ovsdb-server, checking their log files
+# for messages with severity WARN or higher and signaling an error if any
+# is present.  The optional WHITELIST may contain shell-quoted "sed"
+# commands to delete any warnings that are actually expected, e.g.:
+#
+#   OVS_KMOD_VSWITCHD_STOP(["/expected error/d"])
+m4_define([OVS_KMOD_VSWITCHD_STOP],
+  [AT_CHECK([ovs-vsctl del-br br0])
+   OVS_SWITCHD_STOP([$1])
+   AT_CHECK([modprobe -r openvswitch])
+  ])
+
+# DEL_NAMESPACES(ns [, ns ... ])
+#
+# Delete namespaces from the running OS
+m4_define([DEL_NAMESPACES],
+   [m4_foreach([ns], [$@],
+               [ip netns del ns
+               ])
+   ]
+)
+#
+# ADD_NAMESPACES(ns [, ns ... ])
+#
+# Add new namespaces, if ns exists, the old one
+# will be remove before new ones are installed.
+m4_define([ADD_NAMESPACES],
+   [m4_foreach([ns], [$@],
+               [DEL_NAMESPACES(ns)
+                AT_CHECK([ip netns add ns])
+               ])
+   ]
+)
+
+# ADD_VETH([port], [namespace], [ovs-br], [ip_addr])
+#
+# Add a pair of veth ports. 'port' will be added to name space 'namespace',
+# and "ovs-'port'" will be added to ovs bridge 'ovs-br'.
+#
+# The 'port' in 'namespace' will be brought up with static IP address
+# with 'ip_addr' in CIDR notation.
+#
+# The existing 'port' or 'ovs-port' will be removed before new ones are added.
+#
+m4_define([ADD_VETH],
+    [ ovs-vsctl del-port $3 ovs-$1
+      ip netns exec $2 ip link del $1
+      AT_CHECK([ip link add $1 type veth peer name ovs-$1])
+      AT_CHECK([ip link set $1 netns $2])
+      AT_CHECK([ovs-vsctl add-port $3 ovs-$1])
+      AT_CHECK([ip link set dev ovs-$1 up])
+      AT_CHECK([ip netns exec $2 ip addr add $4 dev $1])
+      AT_CHECK([ip netns exec $2 ip link set dev $1 up])
+    ]
+)
diff --git a/tests/kmod-testsuite.at b/tests/kmod-testsuite.at
new file mode 100644
index 0000000..e925367
--- /dev/null
+++ b/tests/kmod-testsuite.at
@@ -0,0 +1,22 @@
+AT_INIT
+
+AT_COPYRIGHT([Copyright (c) 2015 Nicira, Inc.
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at:
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.])
+
+m4_include([tests/ovs-macros.at])
+m4_include([tests/ovsdb-macros.at])
+m4_include([tests/ofproto-macros.at])
+m4_include([tests/kmod-macros.at])
+
+m4_include([tests/kmod-traffic.at])
diff --git a/tests/kmod-traffic.at b/tests/kmod-traffic.at
new file mode 100644
index 0000000..685e1af
--- /dev/null
+++ b/tests/kmod-traffic.at
@@ -0,0 +1,16 @@
+AT_BANNER([kmod-sanity])
+
+AT_SETUP([kmod - ping between two ports])
+OVS_KMOD_VSWITCHD_START(
+   [set-fail-mode br0 standalone -- ])
+
+ADD_NAMESPACES(at_ns0, at_ns1)
+
+ADD_VETH(p0, at_ns0, br0, "10.1.1.1/24")
+ADD_VETH(p1, at_ns1, br0, "10.1.1.2/24")
+
+AT_CHECK([ip netns exec at_ns0 bash -c "ping -q -c 3 -i 0.3 -w 2 10.1.1.2 >/dev/null"])
+
+DEL_NAMESPACES(at_ns0, at_ns1)
+OVS_KMOD_VSWITCHD_STOP
+AT_CLEANUP
-- 
1.9.1




More information about the dev mailing list