From d810597892d23f0236ba6f992b57392ac43c1fcb Mon Sep 17 00:00:00 2001 From: ChillerDragon Date: Sat, 5 Nov 2022 18:41:06 +0100 Subject: [PATCH] Add integration test (closed #4) --- .github/workflows/integration.yml | 31 ++++++++++++ .github/workflows/style.yml | 4 ++ .gitignore | 3 ++ integration_test/reconnect.rb | 16 +++++++ integration_test/run.sh | 74 +++++++++++++++++++++++++++++ integration_test/send_chat_hello.rb | 13 +++++ 6 files changed, 141 insertions(+) create mode 100644 .github/workflows/integration.yml create mode 100755 integration_test/reconnect.rb create mode 100755 integration_test/run.sh create mode 100755 integration_test/send_chat_hello.rb diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml new file mode 100644 index 0000000..a0139cf --- /dev/null +++ b/.github/workflows/integration.yml @@ -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 \ No newline at end of file diff --git a/.github/workflows/style.yml b/.github/workflows/style.yml index 47d56f2..fef716b 100644 --- a/.github/workflows/style.yml +++ b/.github/workflows/style.yml @@ -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 diff --git a/.gitignore b/.gitignore index 80e3957..35d2855 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ Gemfile.lock +integration_test/* +!integration_test/*.rb +!integration_test/*.sh diff --git a/integration_test/reconnect.rb b/integration_test/reconnect.rb new file mode 100755 index 0000000..13bbf18 --- /dev/null +++ b/integration_test/reconnect.rb @@ -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') diff --git a/integration_test/run.sh b/integration_test/run.sh new file mode 100755 index 0000000..9509b08 --- /dev/null +++ b/integration_test/run.sh @@ -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" + diff --git a/integration_test/send_chat_hello.rb b/integration_test/send_chat_hello.rb new file mode 100755 index 0000000..ec3c12e --- /dev/null +++ b/integration_test/send_chat_hello.rb @@ -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