Refactor integration test names

This commit is contained in:
ChillerDragon 2022-11-12 16:47:12 +01:00
parent 7422ee9d94
commit 44e20583e6
8 changed files with 106 additions and 53 deletions

View file

@ -19,16 +19,16 @@ jobs:
- name: Prepare
run: |
sudo apt-get update -y
sudo apt-get install -y shellcheck teeworlds-server
sudo apt-get install -y shellcheck teeworlds teeworlds-server
gem install bundler
gem install rubocop:1.31.2
bundle install --jobs 4 --retry 3
- name: Test sending chat messages
run: |
./integration_test/run.sh chat
./integration_test/run.sh client/chat.rb
- name: Test reconnect
run: |
./integration_test/run.sh reconnect
./integration_test/run.sh client/reconnect.rb
- name: Test rcon
run: |
./integration_test/run.sh rcon
./integration_test/run.sh client/rcon.rb

9
.gitignore vendored
View file

@ -1,5 +1,8 @@
Gemfile.lock
integration_test/*
!integration_test/*.rb
!integration_test/*.sh
integration_test/*.txt
integration_test/*.log
integration_test/*.db
integration_test/*.sql
integration_test/*.sqlite
integration_test/*.sqlite3

View file

@ -1,7 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
require_relative '../lib/teeworlds_client'
require_relative '../../lib/teeworlds_client'
client = TeeworldsClient.new(verbose: false)

View file

@ -1,7 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
require_relative '../lib/teeworlds_client'
require_relative '../../lib/teeworlds_client'
client = TeeworldsClient.new
@ -14,5 +14,10 @@ client.on_rcon_line do |ctx|
puts "[rcon] #{ctx.data[:line]}"
end
client.on_disconnect do
puts 'got disconnect'
exit 0
end
# connect and block main thread
client.connect('localhost', 8377, detach: false)

View file

@ -1,7 +1,7 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
require_relative '../lib/teeworlds_client'
require_relative '../../lib/teeworlds_client'
client = TeeworldsClient.new(verbose: false)

View file

@ -2,91 +2,122 @@
cd "$(dirname "$0")" || exit 1
twbin=teeworlds_srv
rubybin=send_chat_hello.rb
tw_srv_bin=teeworlds_srv
tw_cl_bin=teeworlds
srvcfg='sv_rcon_password rcon;sv_port 8377;killme'
function cleanup() {
echo "[*] shutting down server ..."
pkill -f "$twbin $srvcfg"
[[ "$_timout_pid" != "" ]] && kill "$_timout_pid" &> /dev/null
}
trap cleanup EXIT
function timeout() {
local seconds="$1"
sleep "$seconds"
pkill -f "$rubybin"
echo "killing: $rubybin"
echo "Error: timeouted"
}
if [[ -x "$(command -v teeworlds_srv)" ]]
then
teeworlds_srv "$srvcfg" &> server.txt &
elif [[ -x "$(command -v teeworlds-server)" ]]
then
teeworlds-server "$srvcfg" &> server.txt &
twbin='teeworlds-server'
tw_srv_bin='teeworlds-server'
elif [[ -x "$(command -v teeworlds-srv)" ]]
then
teeworlds-srv "$srvcfg" &> server.txt &
twbin='teeworlds-srv'
tw_srv_bin='teeworlds-srv'
else
echo "Error: please install a teeworlds_srv"
exit 1
fi
testname="${1:-chat}"
echo "[*] running test '$testname' ..."
if [ "$testname" == "chat" ]
if [[ ! -x "$(command -v "$tw_cl_bin")" ]]
then
rubybin=send_chat_hello.rb
elif [ "$testname" == "reconnect" ]
then
rubybin=reconnect.rb
elif [ "$testname" == "rcon" ]
then
rubybin=rcon_shutdown.rb
else
echo "Error: unkown test '$testname'"
echo "Error: please install a teeworlds"
exit 1
fi
timeout 3 killme &
_timout_pid=$!
function get_test_names() {
(find client -name "*.rb";find server -name "*.rb") | tr '\n' ' '
}
function invalid_test() {
local name="$1"
echo "Error: invalid test name '$name'"
echo " valid tests: $(get_test_names)"
exit 1
}
testname="${1:-client/chat.rb}"
if ! [[ "$testname" =~ (client|server)/.*\.rb$ ]]
then
invalid_test "$testname"
fi
if [ ! -f "$testname" ]
then
testname=${testname##*integration_test/}
if [ ! -f "$testname" ]
then
invalid_test "$testname"
fi
fi
echo "[*] running test '$testname' ..."
echo "client log $(date)" > client.txt
echo "server log $(date)" > server.txt
[[ -f timeout.txt ]] && rm timeout.txt
function cleanup() {
echo "[*] shutting down server ..."
pkill -f "$tw_srv_bin $srvcfg"
[[ "$_timout_pid" != "" ]] && kill "$_timout_pid" &> /dev/null
}
trap cleanup EXIT
function fail() {
local msg="$1"
# the first tail get swalloed
# idk why so tail twice to ensure
# getting output
# this is a bit ugly but it works
# maybe a sleep does as well
# or I still did not get flushing
tail client.txt &>/dev/null
echo "[-] end of client log:"
tail client.txt
echo "[-] end of server log:"
tail server.txt
echo "$msg"
exit 1
}
if [ "$testname" == "chat" ]
function timeout() {
local seconds="$1"
sleep "$seconds"
echo "[-] Timeout -> killing: $testname"
touch timeout.txt
pkill -f "$testname killme"
fail "[-] Timeout"
}
timeout 3 killme &
_timout_pid=$!
if [ "$testname" == "client/chat.rb" ]
then
ruby "$rubybin" &> client.txt
ruby "$testname" killme &> client.txt
if ! grep -q 'hello world' server.txt
then
fail "Error: did not find chat message in server log"
fi
elif [ "$testname" == "reconnect" ]
elif [ "$testname" == "client/reconnect.rb" ]
then
ruby "$rubybin" &> client.txt
ruby "$testname" killme &> client.txt
if ! grep -q 'bar' server.txt
then
fail "Error: did not find 2nd chat message in server log"
fi
elif [ "$testname" == "rcon" ]
elif [ "$testname" == "client/rcon.rb" ]
then
ruby "$rubybin" &> client.txt
ruby "$testname" killme &> client.txt
if pgrep -f "$twbin $srvcfg"
sleep 0.1
if pgrep -f "$tw_srv_bin $srvcfg"
then
fail "Error: server still running rcon shutdown failed"
fi
@ -95,5 +126,11 @@ else
exit 1
fi
if [ -f timeout.txt ]
then
echo "[-] Error timouted"
exit 1
fi
echo "[+] Test passed"

View file

@ -108,6 +108,10 @@ class GameClient
@client.send_msg_startinfo
end
def on_disconnect
@client.hooks[:disconnect]&.call
end
def on_rcon_line(chunk)
u = Unpacker.new(chunk.data[1..])
context = Context.new(

View file

@ -72,6 +72,10 @@ class TeeworldsClient
@hooks[:connected] = block
end
def on_disconnect(&block)
@hooks[:disconnect] = block
end
def on_rcon_line(&block)
@hooks[:rcon_line] = block
end
@ -301,7 +305,7 @@ class TeeworldsClient
end
def on_msg_close
puts 'got NET_CTRLMSG_CLOSE'
@game_client.on_disconnect
end
private