tests: add cram based unit tests
authorPetr Štetiar <ynezz@true.cz>
Mon, 4 Nov 2019 19:41:55 +0000 (20:41 +0100)
committerPetr Štetiar <ynezz@true.cz>
Thu, 14 Nov 2019 16:11:34 +0000 (17:11 +0100)
I find them more flexible then shunit2 ones.

Signed-off-by: Petr Štetiar <ynezz@true.cz>
.gitignore
tests/CMakeLists.txt
tests/cram/CMakeLists.txt [new file with mode: 0644]
tests/cram/config/network [new file with mode: 0644]
tests/cram/lua/basic.lua [new file with mode: 0644]
tests/cram/test_ucilua.t [new file with mode: 0644]

index 652da19..8d2927f 100644 (file)
@@ -6,8 +6,11 @@ CMakeFiles
 *.a
 *.so
 *.dylib
 *.a
 *.so
 *.dylib
+*.pyc
 install_manifest.txt
 
 uci
 uci_config.h
 tests/shunit2/save
 install_manifest.txt
 
 uci
 uci_config.h
 tests/shunit2/save
+tests/cram/*.t.err
+.venv
index b7a7ccb..872ed6d 100644 (file)
@@ -1 +1,2 @@
+ADD_SUBDIRECTORY(cram)
 ADD_SUBDIRECTORY(shunit2)
 ADD_SUBDIRECTORY(shunit2)
diff --git a/tests/cram/CMakeLists.txt b/tests/cram/CMakeLists.txt
new file mode 100644 (file)
index 0000000..06c7c94
--- /dev/null
@@ -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=$<TARGET_FILE:uci_lua>")
diff --git a/tests/cram/config/network b/tests/cram/config/network
new file mode 100644 (file)
index 0000000..f42ea7c
--- /dev/null
@@ -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 (file)
index 0000000..ceb706f
--- /dev/null
@@ -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 (file)
index 0000000..1544e23
--- /dev/null
@@ -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