From 7b824e0268575e4c13c624b54b2a1280aa295f7f Mon Sep 17 00:00:00 2001 From: Yevhenii Shapovalov Date: Thu, 9 Nov 2017 13:39:24 +0200 Subject: add tests --- tests/README.md | 3 +++ tests/Smoke/Makefile | 63 ++++++++++++++++++++++++++++++++++++++++++++ tests/Smoke/PURPOSE | 3 +++ tests/Smoke/account.lua | 35 +++++++++++++++++++++++++ tests/Smoke/bisect.lua | 28 ++++++++++++++++++++ tests/Smoke/globals.lua | 23 ++++++++++++++++ tests/Smoke/hello.lua | 4 +++ tests/Smoke/runtest.sh | 70 +++++++++++++++++++++++++++++++++++++++++++++++++ tests/Smoke/sieve.lua | 28 ++++++++++++++++++++ tests/tests.yml | 14 ++++++++++ 10 files changed, 271 insertions(+) create mode 100644 tests/README.md create mode 100644 tests/Smoke/Makefile create mode 100644 tests/Smoke/PURPOSE create mode 100644 tests/Smoke/account.lua create mode 100644 tests/Smoke/bisect.lua create mode 100644 tests/Smoke/globals.lua create mode 100644 tests/Smoke/hello.lua create mode 100755 tests/Smoke/runtest.sh create mode 100644 tests/Smoke/sieve.lua create mode 100644 tests/tests.yml diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 0000000..5683070 --- /dev/null +++ b/tests/README.md @@ -0,0 +1,3 @@ +# lua + +lua tests \ No newline at end of file diff --git a/tests/Smoke/Makefile b/tests/Smoke/Makefile new file mode 100644 index 0000000..cc6503b --- /dev/null +++ b/tests/Smoke/Makefile @@ -0,0 +1,63 @@ +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# +# Makefile of /CoreOS/lua/Sanity/Smoke +# Description: Basic smoke for lua component +# Author: Stepan Sigut +# +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# +# 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 " > $(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) diff --git a/tests/Smoke/PURPOSE b/tests/Smoke/PURPOSE new file mode 100644 index 0000000..fc1a3f3 --- /dev/null +++ b/tests/Smoke/PURPOSE @@ -0,0 +1,3 @@ +PURPOSE of /CoreOS/lua/Sanity/Smoke +Description: Basic smoke for lua component +Author: Stepan Sigut diff --git a/tests/Smoke/account.lua b/tests/Smoke/account.lua new file mode 100644 index 0000000..e783c01 --- /dev/null +++ b/tests/Smoke/account.lua @@ -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) diff --git a/tests/Smoke/bisect.lua b/tests/Smoke/bisect.lua new file mode 100644 index 0000000..77ac36c --- /dev/null +++ b/tests/Smoke/bisect.lua @@ -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) +# +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +# +# 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 diff --git a/tests/Smoke/sieve.lua b/tests/Smoke/sieve.lua new file mode 100644 index 0000000..4ce8456 --- /dev/null +++ b/tests/Smoke/sieve.lua @@ -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 diff --git a/tests/tests.yml b/tests/tests.yml new file mode 100644 index 0000000..2613885 --- /dev/null +++ b/tests/tests.yml @@ -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 -- cgit