From 18049a84fe402068ba10dfcffe6dbb088aefc53a Mon Sep 17 00:00:00 2001 From: =?utf8?q?Petr=20=C5=A0tetiar?= Date: Mon, 4 Nov 2019 20:41:55 +0100 Subject: [PATCH] tests: add cram based unit tests MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit I find them more flexible then shunit2 ones. Signed-off-by: Petr Å tetiar --- .gitignore | 3 ++ tests/CMakeLists.txt | 1 + tests/cram/CMakeLists.txt | 21 ++++++++++++++ tests/cram/config/network | 25 +++++++++++++++++ tests/cram/lua/basic.lua | 44 +++++++++++++++++++++++++++++ tests/cram/test_ucilua.t | 58 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 152 insertions(+) create mode 100644 tests/cram/CMakeLists.txt create mode 100644 tests/cram/config/network create mode 100644 tests/cram/lua/basic.lua create mode 100644 tests/cram/test_ucilua.t diff --git a/.gitignore b/.gitignore index 652da19..8d2927f 100644 --- a/.gitignore +++ b/.gitignore @@ -6,8 +6,11 @@ CMakeFiles *.a *.so *.dylib +*.pyc install_manifest.txt uci uci_config.h tests/shunit2/save +tests/cram/*.t.err +.venv diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b7a7ccb..872ed6d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1 +1,2 @@ +ADD_SUBDIRECTORY(cram) ADD_SUBDIRECTORY(shunit2) diff --git a/tests/cram/CMakeLists.txt b/tests/cram/CMakeLists.txt new file mode 100644 index 0000000..06c7c94 --- /dev/null +++ b/tests/cram/CMakeLists.txt @@ -0,0 +1,21 @@ +FIND_PACKAGE(PythonInterp 3 REQUIRED) +FILE(GLOB test_cases "test_*.t") + +SET(PYTHON_VENV_DIR "${CMAKE_CURRENT_BINARY_DIR}/.venv") +SET(PYTHON_VENV_PIP "${PYTHON_VENV_DIR}/bin/pip") +SET(PYTHON_VENV_CRAM "${PYTHON_VENV_DIR}/bin/cram") + +ADD_CUSTOM_COMMAND( + OUTPUT ${PYTHON_VENV_CRAM} + COMMAND ${PYTHON_EXECUTABLE} -m venv ${PYTHON_VENV_DIR} + COMMAND ${PYTHON_VENV_PIP} install cram +) +ADD_CUSTOM_TARGET(prepare-cram-venv ALL DEPENDS ${PYTHON_VENV_CRAM}) + +ADD_TEST( + NAME cram + COMMAND ${PYTHON_VENV_CRAM} ${test_cases} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} +) + +SET_PROPERTY(TEST cram APPEND PROPERTY ENVIRONMENT "UCI_LUA=$") diff --git a/tests/cram/config/network b/tests/cram/config/network new file mode 100644 index 0000000..f42ea7c --- /dev/null +++ b/tests/cram/config/network @@ -0,0 +1,25 @@ +config 'alias' 'a' + option 'interface' 'lan' + +config 'alias' 'b' + option 'interface' 'lan' + +config 'interface' 'lan' + option 'proto' 'static' + option 'ifname' 'eth0' + option 'test' '123' + option 'enabled' 'off' + option 'ipaddr' '2.3.4.5' + +config 'interface' 'wan' + option 'proto' 'dhcp' + option 'ifname' 'eth1' + option 'enabled' 'on' + option 'aliases' 'c d' + +config 'alias' 'c' + option 'interface' 'wan' + +config 'alias' 'd' + option 'interface' 'wan' + diff --git a/tests/cram/lua/basic.lua b/tests/cram/lua/basic.lua new file mode 100644 index 0000000..ceb706f --- /dev/null +++ b/tests/cram/lua/basic.lua @@ -0,0 +1,44 @@ +local A = assert +local c = uci.cursor(os.getenv("CONFIG_DIR")) + +c:foreach("network", "interface", function(s) + print("---------------") + for k, v in pairs(s) do + print(k .. ': ' .. tostring(v)) + end +end) + +local t = c:get_all("network") + +A(t.wan.ifname == 'eth1') +A(t.wan.proto == 'dhcp') +A(c:get("network", "wan", "ifname") == "eth1") +A(c:get("network", "wan", "proto") == "dhcp") + +A(t.lan.ifname == 'eth0') +A(t.lan.enabled == 'off') +A(c:get("network", "lan", "ifname") == "eth0") +A(c:get("network", "lan", "enabled") == "off") + +A(c:set("network", "lan", "ifname", "eth5")) +A(c:get("network", "lan", "ifname") == "eth5") +A(c:revert("network")) +A(c:get("network", "lan", "ifname") == "eth0") + +A(c:set("network", "lan", "ifname", "eth5")) +A(c:get("network", "lan", "ifname") == "eth5") +A(c:commit("network")) +A(c:set("network", "lan", "ifname", "eth0")) +A(c:revert("network")) +A(c:commit("network")) +A(c:get("network", "lan", "ifname") == "eth5") + +A(c:set("network", "lan", "dns", { + "ns1.king.banik.cz", + "ns2.openwrt.org", +})) + +local t = c:get("network", "lan", "dns") +A(#t == 2) +A(t[1] == "ns1.king.banik.cz") +A(t[2] == "ns2.openwrt.org") diff --git a/tests/cram/test_ucilua.t b/tests/cram/test_ucilua.t new file mode 100644 index 0000000..1544e23 --- /dev/null +++ b/tests/cram/test_ucilua.t @@ -0,0 +1,58 @@ +set LUA_CPATH and ucilua for convenience: + + $ export LC_ALL=C + $ [ -n "$UCI_LUA" ] && export LUA_CPATH="$(dirname "$UCI_LUA")/?.so" + $ alias ucilua="valgrind --quiet --leak-check=full lua -luci" + +check available methods: + + $ ucilua -e 'table.foreach(uci,function(m) print(m) end)' + add_history + add_delta + close + set_confdir + save + cursor + get_all + foreach + __gc + delete + set_savedir + set + revert + get_savedir + changes + reorder + get_confdir + list_configs + commit + unload + rename + add + load + get + +run basic Lua tests: + + $ cp -R "$TESTDIR/config" . + $ export CONFIG_DIR=$(pwd)/config + $ ucilua $TESTDIR/lua/basic.lua + --------------- + enabled: off + .anonymous: false + ipaddr: 2.3.4.5 + .index: 2 + .name: lan + test: 123 + .type: interface + ifname: eth0 + proto: static + --------------- + .name: wan + .type: interface + .index: 3 + enabled: on + ifname: eth1 + proto: dhcp + .anonymous: false + aliases: c d -- 2.25.1