Browse Source

tests: add cram based unit tests

I find them more flexible then shunit2 ones.

Signed-off-by: Petr Štetiar <ynezz@true.cz>
Petr Štetiar 4 years ago
parent
commit
18049a84fe
6 changed files with 152 additions and 0 deletions
  1. 3 0
      .gitignore
  2. 1 0
      tests/CMakeLists.txt
  3. 21 0
      tests/cram/CMakeLists.txt
  4. 25 0
      tests/cram/config/network
  5. 44 0
      tests/cram/lua/basic.lua
  6. 58 0
      tests/cram/test_ucilua.t

+ 3 - 0
.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

+ 1 - 0
tests/CMakeLists.txt

@@ -1 +1,2 @@
+ADD_SUBDIRECTORY(cram)
 ADD_SUBDIRECTORY(shunit2)

+ 21 - 0
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=$<TARGET_FILE:uci_lua>")

+ 25 - 0
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'
+

+ 44 - 0
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")

+ 58 - 0
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