Add integration test (closed #4)

This commit is contained in:
ChillerDragon 2022-11-05 18:41:06 +01:00
parent a3f28aaf37
commit d810597892
6 changed files with 141 additions and 0 deletions

31
.github/workflows/integration.yml vendored Normal file
View file

@ -0,0 +1,31 @@
name: Integration
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]
jobs:
test-connect:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up Ruby 3.1
uses: actions/setup-ruby@v1
with:
ruby-version: 3.1.x
- name: Prepare
run: |
sudo apt-get update -y
sudo apt-get install -y shellcheck 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
- name: Test reconnect
run: |
./integration_test/run.sh reconnect

View file

@ -19,9 +19,13 @@ jobs:
ruby-version: 3.1.x
- name: Prepare
run: |
sudo apt-get update
sudo apt-get install shellcheck
gem install bundler
gem install rubocop:1.31.2
bundle install --jobs 4 --retry 3
- name: Check ruby with rubocop
run: |
rubocop
- name: Shellcheck
run: find . -type f -name '*.sh' -print0 | xargs -0 shellcheck

3
.gitignore vendored
View file

@ -1,2 +1,5 @@
Gemfile.lock
integration_test/*
!integration_test/*.rb
!integration_test/*.sh

16
integration_test/reconnect.rb Executable file
View file

@ -0,0 +1,16 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
require_relative '../lib/teeworlds-client'
client = TeeworldsClient.new(verbose: false)
client.connect('localhost', 8377, detach: true)
sleep 1
client.send_chat('foo')
client.connect('localhost', 8377, detach: true)
sleep 1
client.send_chat('bar')

74
integration_test/run.sh Executable file
View file

@ -0,0 +1,74 @@
#!/bin/bash
cd "$(dirname "$0")" || exit 1
twbin=teeworlds_srv
function cleanup() {
echo "[*] shutting down server ..."
pkill -f "$twbin sv_port 8377;killme"
[[ "$_timout_pid" != "" ]] && kill "$_timout_pid" &> /dev/null
}
trap cleanup EXIT
function timeout() {
local seconds="$1"
sleep "$seconds"
pkill -f 'send_chat_hello.rb'
echo "Error: timeouted"
}
if [[ -x "$(command -v teeworlds_srv)" ]]
then
teeworlds_srv "sv_port 8377;killme" &> server.txt &
elif [[ -x "$(command -v teeworlds-server)" ]]
then
teeworlds-server "sv_port 8377;killme" &> server.txt &
twbin='teeworlds-server'
elif [[ -x "$(command -v teeworlds-srv)" ]]
then
teeworlds-srv "sv_port 8377;killme" &> server.txt &
twbin='teeworlds-srv'
else
echo "Error: please install a teeworlds_srv"
exit 1
fi
timeout 3 killme &
_timout_pid=$!
testname="${1:-chat}"
echo "[*] running test '$testname' ..."
function fail() {
local msg="$1"
tail client.txt
tail server.txt
echo "$msg"
exit 1
}
if [ "$testname" == "chat" ]
then
ruby ./send_chat_hello.rb &> client.txt
if ! grep -q 'hello world' server.txt
then
fail "Error: did not find chat message in server log"
fi
elif [ "$testname" == "reconnect" ]
then
ruby ./reconnect.rb &> client.txt
if ! grep -q 'bar' server.txt
then
fail "Error: did not find 2nd chat message in server log"
fi
else
echo "Error: unkown test '$testname'"
exit 1
fi
echo "[+] Test passed client sent chat message to server"

View file

@ -0,0 +1,13 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
require_relative '../lib/teeworlds-client'
client = TeeworldsClient.new(verbose: false)
client.connect('localhost', 8377, detach: true)
sleep 1
client.send_chat('hello world')
client.disconnect