From b85382d38669f23fa66dfde5aa7fd9aaf681dff3 Mon Sep 17 00:00:00 2001 From: Justin Cormack Date: Tue, 9 Aug 2016 23:27:46 +0100 Subject: [PATCH 1/2] Rework Dockerfile and tests Rework Dockerfile so that it installs code in a more useful place, much easier for reuse. Rework tests to be more standalone and work against installed version. Signed-off-by: Justin Cormack --- Dockerfile | 5 +- docker-compose.test.yml | 4 +- test/bsd.lua | 2 +- test/freebsd.lua | 2 +- test/helpers.lua | 81 +++++++++++++++++++++++++++ test/linux.lua | 2 +- {include/luaunit => test}/luaunit.lua | 0 test/netbsd.lua | 2 +- test/openbsd.lua | 2 +- test/osx.lua | 2 +- test/rump.lua | 2 +- {include/strict => test}/strict.lua | 0 test/test.lua | 16 ++---- 13 files changed, 99 insertions(+), 21 deletions(-) create mode 100644 test/helpers.lua rename {include/luaunit => test}/luaunit.lua (100%) rename {include/strict => test}/strict.lua (100%) diff --git a/Dockerfile b/Dockerfile index b755e10ff2..4db4a4ab73 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,8 @@ FROM alpine:3.4 -RUN apk update && apk add luajit strace +RUN apk update && apk add luajit luajit-dev strace && mkdir -p /usr/share/lua/5.1 -COPY . . +COPY syscall.lua /usr/share/lua/5.1/ +COPY syscall /usr/share/lua/5.1/syscall/ ENTRYPOINT ["luajit"] diff --git a/docker-compose.test.yml b/docker-compose.test.yml index 5170470c8f..300cd4b981 100644 --- a/docker-compose.test.yml +++ b/docker-compose.test.yml @@ -1,3 +1,5 @@ sut: build: . - command: test/test.lua + command: /test/test.lua + volumes: + - ./test:/test diff --git a/test/bsd.lua b/test/bsd.lua index 20d138b9ed..5fcb4a3b74 100644 --- a/test/bsd.lua +++ b/test/bsd.lua @@ -2,7 +2,7 @@ local function init(S) -local helpers = require "syscall.helpers" +local helpers = require "test.helpers" local types = S.types local c = S.c local abi = S.abi diff --git a/test/freebsd.lua b/test/freebsd.lua index 4a57af07ad..e296017f4b 100644 --- a/test/freebsd.lua +++ b/test/freebsd.lua @@ -2,7 +2,7 @@ local function init(S) -local helpers = require "syscall.helpers" +local helpers = require "test.helpers" local types = S.types local c = S.c local abi = S.abi diff --git a/test/helpers.lua b/test/helpers.lua new file mode 100644 index 0000000000..6f29c0a1d3 --- /dev/null +++ b/test/helpers.lua @@ -0,0 +1,81 @@ +-- misc helper functions + +local require, error, assert, tonumber, tostring, +setmetatable, pairs, ipairs, unpack, rawget, rawset, +pcall, type, table, string, math = +require, error, assert, tonumber, tostring, +setmetatable, pairs, ipairs, unpack, rawget, rawset, +pcall, type, table, string, math + +local debug, collectgarbage = require "debug", collectgarbage + +local ffi = require "ffi" +local bit = require "bit" + +local h = {} + +-- generic assert helper, mainly for tests +function h.assert(cond, err, ...) + if not cond then + error(tostring(err or "unspecified error")) -- annoyingly, assert does not call tostring! + end + collectgarbage("collect") -- force gc, to test for bugs + if type(cond) == "function" then return cond, err, ... end + if cond == true then return ... end + return cond, ... +end + +-- endian conversion +if ffi.abi("be") then -- nothing to do + function h.htonl(b) return b end + function h.htons(b) return b end + function h.convle32(b) return bit.bswap(b) end -- used by file system capabilities, always stored as le +else + function h.htonl(b) return bit.bswap(b) end + function h.htons(b) return bit.rshift(bit.bswap(b), 16) end + function h.convle32(b) return b end -- used by file system capabilities, always stored as le +end +h.ntohl = h.htonl -- reverse is the same +h.ntohs = h.htons -- reverse is the same + +function h.octal(s) return tonumber(s, 8) end +local octal = h.octal + +function h.split(delimiter, text) + if delimiter == "" then return {text} end + if #text == 0 then return {} end + local list = {} + local pos = 1 + while true do + local first, last = text:find(delimiter, pos) + if first then + list[#list + 1] = text:sub(pos, first - 1) + pos = last + 1 + else + list[#list + 1] = text:sub(pos) + break + end + end + return list +end + +function h.trim(s) -- TODO should replace underscore with space + return (s:gsub("^%s*(.-)%s*$", "%1")) +end + +local split, trim = h.split, h.trim + +h.divmod = function(a, b) + return math.floor(a / b), a % b +end + +h.booltoc = setmetatable({ + [0] = 0, + [1] = 1, + [false] = 0, + [true] = 1, +}, {__call = function(tb, arg) return tb[arg or 0] end}) -- allow nil as false + +function h.ctobool(i) return tonumber(i) ~= 0 end + +return h diff --git a/test/linux.lua b/test/linux.lua index 391d25c463..26f3dd74ff 100644 --- a/test/linux.lua +++ b/test/linux.lua @@ -2,7 +2,7 @@ local function init(S) -local helpers = require "syscall.helpers" +local helpers = require "test.helpers" local abi = S.abi local types = S.types local c = S.c diff --git a/include/luaunit/luaunit.lua b/test/luaunit.lua similarity index 100% rename from include/luaunit/luaunit.lua rename to test/luaunit.lua diff --git a/test/netbsd.lua b/test/netbsd.lua index 9570d393d4..3039ed49f9 100644 --- a/test/netbsd.lua +++ b/test/netbsd.lua @@ -2,7 +2,7 @@ local function init(S) -local helpers = require "syscall.helpers" +local helpers = require "test.helpers" local types = S.types local c = S.c local abi = S.abi diff --git a/test/openbsd.lua b/test/openbsd.lua index ce2b4a7125..755c32094a 100644 --- a/test/openbsd.lua +++ b/test/openbsd.lua @@ -2,7 +2,7 @@ local function init(S) -local helpers = require "syscall.helpers" +local helpers = require "test.helpers" local types = S.types local c = S.c local abi = S.abi diff --git a/test/osx.lua b/test/osx.lua index 75ff6b932c..5346ccdbce 100644 --- a/test/osx.lua +++ b/test/osx.lua @@ -2,7 +2,7 @@ local function init(S) -local helpers = require "syscall.helpers" +local helpers = require "test.helpers" local types = S.types local c = S.c local abi = S.abi diff --git a/test/rump.lua b/test/rump.lua index d46a47cb3a..afd161209d 100644 --- a/test/rump.lua +++ b/test/rump.lua @@ -3,7 +3,7 @@ local function init(S) -local helpers = require "syscall.helpers" +local helpers = require "test.helpers" local types = S.types local c = S.c local abi = S.abi diff --git a/include/strict/strict.lua b/test/strict.lua similarity index 100% rename from include/strict/strict.lua rename to test/strict.lua diff --git a/test/test.lua b/test/test.lua index ca35cebd68..aab6a98aae 100644 --- a/test/test.lua +++ b/test/test.lua @@ -4,12 +4,9 @@ arg = arg or {} --- only use this installation for tests -package.path = "./?.lua;" +local strict = require "test.strict" -local strict = require "include.strict.strict" - -local helpers = require "syscall.helpers" +local helpers = require "test.helpers" local assert = helpers.assert @@ -108,7 +105,7 @@ local function assert_equal(...) end USE_EXPECTED_ACTUAL_IN_ASSERT_EQUALS = true -- strict wants this to be set -local luaunit = require "include.luaunit.luaunit" +local luaunit = require "test.luaunit" local sysfile = debug.getinfo(S.open).source local cov = {active = {}, cov = {}} @@ -1960,9 +1957,6 @@ test_raw_socket = { assert(cs == expected, "expect correct ip checksum: got " .. string.format("%%%04X", cs) .. " expected " .. string.format("%%%04X", expected)) end, test_raw_udp_root = function() -- TODO create some helper functions, this is not very nice - - local h = require "syscall.helpers" -- TODO should not have to use later - local loop = "127.0.0.1" local raw = assert(S.socket("inet", "raw", "raw")) -- needed if not on Linux @@ -1986,8 +1980,8 @@ test_raw_socket = { local ca = cl:getsockname() -- TODO iphdr should have __index helpers for endianness etc (note use raw s_addr) - iphdr[0] = {ihl = 5, version = 4, tos = 0, id = 0, frag_off = h.htons(0x4000), ttl = 64, protocol = c.IPPROTO.UDP, check = 0, - saddr = sa.sin_addr.s_addr, daddr = ca.sin_addr.s_addr, tot_len = h.htons(len)} + iphdr[0] = {ihl = 5, version = 4, tos = 0, id = 0, frag_off = helpers.htons(0x4000), ttl = 64, protocol = c.IPPROTO.UDP, check = 0, + saddr = sa.sin_addr.s_addr, daddr = ca.sin_addr.s_addr, tot_len = helpers.htons(len)} --udphdr[0] = {src = sport, dst = ca.port, length = udplen} -- doesnt work with metamethods udphdr[0].src = sport From a779caa7a3f6ae244d24c08cef62bdff19c158c1 Mon Sep 17 00:00:00 2001 From: Justin Cormack Date: Tue, 9 Aug 2016 23:52:46 +0100 Subject: [PATCH 2/2] Docker Cloud does not start processes at priority 0, remove from test Signed-off-by: Justin Cormack --- test/test.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/test.lua b/test/test.lua index aab6a98aae..fddee0a6be 100644 --- a/test/test.lua +++ b/test/test.lua @@ -2309,10 +2309,10 @@ test_mmap = { test_processes = { test_nice = function() local n = assert(S.getpriority("process")) - assert_equal(n, 0, "process should start at priority 0") - local nn = assert(S.nice(1)) - assert_equal(nn, 1) - local nn = assert(S.setpriority("process", 0, 1)) -- sets to 1, which it already is + --assert_equal(n, 0, "process should start at priority 0") + --local nn = assert(S.nice(1)) + --assert_equal(nn, 1) + --local nn = assert(S.setpriority("process", 0, n)) -- sets to 1, which it already is end, test_fork_wait = function() local pid0 = S.getpid()