Skip to content

Commit

Permalink
Rework Dockerfile and tests
Browse files Browse the repository at this point in the history
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 <justin@specialbusservice.com>
  • Loading branch information
justincormack committed Aug 9, 2016
1 parent 24f7789 commit b85382d
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 21 deletions.
5 changes: 3 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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"]
4 changes: 3 additions & 1 deletion docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
sut:
build: .
command: test/test.lua
command: /test/test.lua
volumes:
- ./test:/test
2 changes: 1 addition & 1 deletion test/bsd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/freebsd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
81 changes: 81 additions & 0 deletions test/helpers.lua
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion test/linux.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion test/netbsd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/openbsd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/osx.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/rump.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
File renamed without changes.
16 changes: 5 additions & 11 deletions test/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 = {}}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit b85382d

Please sign in to comment.