add tests

This commit is contained in:
Yevhenii Shapovalov 2017-11-09 13:39:24 +02:00
parent 8a0d515308
commit 7b824e0268
10 changed files with 271 additions and 0 deletions

3
tests/README.md Normal file
View File

@ -0,0 +1,3 @@
# lua
lua tests

63
tests/Smoke/Makefile Normal file
View File

@ -0,0 +1,63 @@
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Makefile of /CoreOS/lua/Sanity/Smoke
# Description: Basic smoke for lua component
# Author: Stepan Sigut <ssigut@redhat.com>
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Copyright (c) 2016 Red Hat, Inc.
#
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
export TEST=/CoreOS/lua/Sanity/Smoke
export TESTVERSION=1.0
BUILT_FILES=
FILES=$(METADATA) runtest.sh Makefile PURPOSE *.lua
.PHONY: all install download clean
run: $(FILES) build
./runtest.sh
build: $(BUILT_FILES)
test -x runtest.sh || chmod a+x runtest.sh
clean:
rm -f *~ $(BUILT_FILES)
include /usr/share/rhts/lib/rhts-make.include
$(METADATA): Makefile
@echo "Owner: Stepan Sigut <ssigut@redhat.com>" > $(METADATA)
@echo "Name: $(TEST)" >> $(METADATA)
@echo "TestVersion: $(TESTVERSION)" >> $(METADATA)
@echo "Path: $(TEST_DIR)" >> $(METADATA)
@echo "Description: Basic smoke for lua component" >> $(METADATA)
@echo "Type: Sanity" >> $(METADATA)
@echo "TestTime: 5m" >> $(METADATA)
@echo "RunFor: lua" >> $(METADATA)
@echo "Requires: lua" >> $(METADATA)
@echo "Priority: Normal" >> $(METADATA)
@echo "License: GPLv2+" >> $(METADATA)
@echo "Confidential: no" >> $(METADATA)
@echo "Destructive: no" >> $(METADATA)
@echo "Releases: -RHEL4 -RHELClient5 -RHELServer5" >> $(METADATA)
rhts-lint $(METADATA)

3
tests/Smoke/PURPOSE Normal file
View File

@ -0,0 +1,3 @@
PURPOSE of /CoreOS/lua/Sanity/Smoke
Description: Basic smoke for lua component
Author: Stepan Sigut <ssigut@redhat.com>

35
tests/Smoke/account.lua Normal file
View File

@ -0,0 +1,35 @@
-- account.lua
-- from PiL 1, Chapter 16
Account = {balance = 0}
function Account:new (o, name)
o = o or {name=name}
setmetatable(o, self)
self.__index = self
return o
end
function Account:deposit (v)
self.balance = self.balance + v
end
function Account:withdraw (v)
if v > self.balance then error("insufficient funds on account "..self.name) end
self.balance = self.balance - v
end
function Account:show (title)
print(title or "", self.name, self.balance)
end
a = Account:new(nil,"demo")
a:show("after creation")
a:deposit(1000.00)
a:show("after deposit")
a:withdraw(100.00)
a:show("after withdraw")
-- this would raise an error
b = Account:new(nil,"DEMO")
b:withdraw(100.00)

28
tests/Smoke/bisect.lua Normal file
View File

@ -0,0 +1,28 @@
-- bisect.lua
-- bisection method for solving non-linear equations
delta=1e-6 -- tolerance
function bisect(f,a,b,fa,fb)
local c=(a+b)/2
io.write(n," c=",c," a=",a," b=",b,"\n")
if c==a or c==b or math.abs(a-b)<delta then return c,b-a end
n=n+1
local fc=f(c)
if fa*fc<0 then return bisect(f,a,c,fa,fc) else return bisect(f,c,b,fc,fb) end
end
-- find root of f in the inverval [a,b]. needs f(a)*f(b)<0
function solve(f,a,b)
n=0
local z,e=bisect(f,a,b,f(a),f(b))
io.write(string.format("after %d steps, root is %.17g with error %.1e, f=%.1e\n",n,z,e,f(z)))
end
-- our function
function f(x)
return x*x*x-x-1
end
-- find zero in [1,2]
solve(f,1,2)

23
tests/Smoke/globals.lua Normal file
View File

@ -0,0 +1,23 @@
-- globals.lua
-- show all global variables
local seen={}
function dump(t,i)
seen[t]=true
local s={}
local n=0
for k in pairs(t) do
n=n+1 s[n]=k
end
table.sort(s)
for k,v in ipairs(s) do
print(i,v)
v=t[v]
if type(v)=="table" and not seen[v] then
dump(v,i.."\t")
end
end
end
dump(_G,"")

4
tests/Smoke/hello.lua Normal file
View File

@ -0,0 +1,4 @@
-- hello.lua
-- the first program in every language
io.write("Hello world, from ",_VERSION,"!\n")

70
tests/Smoke/runtest.sh Executable file
View File

@ -0,0 +1,70 @@
#!/bin/bash
# vim: dict+=/usr/share/beakerlib/dictionary.vim cpt=.,w,b,u,t,i,k
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# runtest.sh of /CoreOS/lua/Sanity/Smoke
# Description: Basic smoke for lua component
# Author: Stepan Sigut <ssigut@redhat.com>
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
# Copyright (c) 2016 Red Hat, Inc.
#
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Include Beaker environment
. /usr/bin/rhts-environment.sh || exit 1
. /usr/share/beakerlib/beakerlib.sh || exit 1
PACKAGES=${PACKAGES:-"lua"}
LUA=${LUA:-"lua"}
rlJournalStart
rlPhaseStartSetup
rlAssertRpm --all
rlAssertBinaryOrigin $LUA
set -o pipefail
rlRun "TmpDir=\$(mktemp -d)" 0 "Creating tmp directory"
rlRun "cp *.lua $TmpDir"
rlRun "pushd $TmpDir"
rlPhaseEnd
rlPhaseStartTest
rlRun "$LUA hello.lua 2>&1 | tee dump.log" 0
rlAssertGrep "Hello world, from Lua" "dump.log"
rlPhaseEnd
rlPhaseStartTest
rlRun "$LUA bisect.lua 2>&1 | tee dump.log" 0
rlAssertGrep "after 20 steps, root is 1.3247179985046387 with error 9.5e-07, f=1.8e-07" "dump.log"
rlPhaseEnd
rlPhaseStartTest
rlRun "$LUA globals.lua &>/dev/null" 0
rlPhaseEnd
rlPhaseStartTest
rlRun "$LUA account.lua 2>&1 | tee dump.log" 1
rlAssertGrep "after creation" "dump.log"
rlAssertGrep "lua: account.lua:18: insufficient funds on account DEMO" "dump.log"
rlPhaseEnd
rlPhaseStartTest
rlRun "$LUA sieve.lua &>/dev/null" 0
rlPhaseEnd
rlPhaseStartCleanup
rlRun "popd"
rlRun "rm -r $TmpDir" 0 "Removing tmp directory"
rlPhaseEnd
rlJournalPrintText
rlJournalEnd

28
tests/Smoke/sieve.lua Normal file
View File

@ -0,0 +1,28 @@
-- sieve.lua
-- the sieve of Eratosthenes programmed with coroutines
-- typical usage: lua -e N=500 sieve.lua | column
-- generate all the numbers from 2 to n
function gen (n)
return coroutine.wrap(function ()
for i=2,n do coroutine.yield(i) end
end)
end
-- filter the numbers generated by `g', removing multiples of `p'
function filter (p, g)
return coroutine.wrap(function ()
for n in g do
if n%p ~= 0 then coroutine.yield(n) end
end
end)
end
N=N or 500 -- from command line
x = gen(N) -- generate primes up to N
while 1 do
local n = x() -- pick a number until done
if n == nil then break end
print(n) -- must be a prime number
x = filter(n, x) -- now remove its multiples
end

14
tests/tests.yml Normal file
View File

@ -0,0 +1,14 @@
---
# This first play always runs on the local staging system
- hosts: localhost
roles:
- role: standard-test-beakerlib
tags:
- classic
- container
- atomic
tests:
- Smoke
required_packages:
- lua # Required for smoke test
- which # Required for smoke test