Commit 80e5d17b authored by zauberstuhl's avatar zauberstuhl
Browse files

Refactor testproject

parent 274b1501
Pipeline #2344 passed with stages
in 12 minutes and 43 seconds
......@@ -48,13 +48,13 @@ Say we have a project called `testproject` and it can communicate with other `te
If we have two servers running on `172.17.0.4:3000` and `172.17.0.5:3000` we can send `172.17.0.4`
a simple message by executing following command:
curl http://172.17.0.5:3000/add@172.17.0.4:3000
curl http://172.17.0.5:3000/send?http://172.17.0.4:3000/ping
If we ask the server directly whether it received a message or not:
curl http://172.17.0.4:3000
docker logs <container-172.17.0.4> 2>&1 | grep ping | wc -l
it should return one, otherwise it will return zero!
it should return the number of received pings!
**Getting started**
......@@ -75,9 +75,11 @@ Then you can install the testsuite dependencies:
* github.com/stedolan/jq
* github.com/sstephenson/bats
Simply run the install script or do it manually:
If you prefer running tests within docker you can also use the testsuite image:
bash scripts/install.sh
docker run --rm -ti \
-v /var/run/docker.sock:/var/run/docker.sock \
registry.git.feneas.org/feneas/federation/testsuite:latest bash
## Docker Image
......@@ -114,14 +116,12 @@ Following environment variables will be available while tests are running:
For our `testproject` that could look something like this:
```
FROM debian:jessie
FROM golang:1.12
RUN apt-get update
RUN apt-get install -y \
git-core postgresql-client netcat curl
RUN git clone https://git.feneas.org/feneas/federation/testsuite-testproject.git
WORKDIR testsuite-testproject
RUN git clone \
https://git.feneas.org/feneas/federation/testsuite-testproject.git \
/go/src/feneas/federation/testsuite-testproject
WORKDIR /go/src/feneas/federation/testsuite-testproject
ADD entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
......@@ -147,7 +147,12 @@ if [ "$PROJECT" == "testproject" ]; then
}
fi
./testproject.sh $DATABASE postgreshost $PORT
go build -o server main.go || {
echo "Cannot build sources"
exit 1
}
./server :$PORT
```
### Building
......@@ -156,7 +161,7 @@ fi
Last final step before we have a fully functional docker image is to build the actual image:
docker build --no-cache -t registry.git.feneas.org/feneas/federation/testsuite/testproject:v1.0.3-testproject .
docker build --no-cache -t registry.git.feneas.org/feneas/federation/testsuite/testproject:vX.X.X-testproject .
The testsuite requires a special naming for every image: `registry.git.feneas.org/feneas/federation/testsuite/<project>:<version>-<project>`
......@@ -166,7 +171,7 @@ and create a project tag and we are done with the image part:
cd <workspace>/testsuite/testproject
git add .
git commit -m "Add testproject docker image"
git tag v1.0.3-testproject
git tag vX.X.X-testproject
## Testing
......@@ -186,18 +191,7 @@ I suggest we start with loading the test helper first since it holds many helper
load test_helper
```
Since we need two instances of our `testproject` we have to create two postgresql databases first:
```
@test "$PREFIX create databases" {
for tp in tp1 tp2; do
create_database $tp
[ "$?" -eq 0 ]
done
}
```
and start-up the mentioned servers:
Since we need two instances of our `testproject` we have to create them first:
```
@test "$PREFIX start testproject#1 server" {
......@@ -206,14 +200,14 @@ and start-up the mentioned servers:
}
@test "$PREFIX start testproject#2 server" {
start_app tp2 3000 testproject$(latest_tag testproject)
start_app tp2 3001 testproject$(latest_tag testproject)
[ "$?" -eq 0 ]
}
```
Now the interesting part starts.. We want to test a request to tp2!
If we execute the curl command (mentioned at the beginning of this
document) tp2 should federated/relay the message to tp1.
document) tp2 should federate/relay the message to tp1.
To prove this we send two requests:
......@@ -221,17 +215,31 @@ To prove this we send two requests:
2. to tp1 ensuring the message was received
```
@test "$PREFIX increase count on tp1 via tp2" {
get "http://$(container_ip tp2):3000/add@$(container_ip tp1):3000"
@test "$PREFIX ping both servers, should reply with pong" {
get "http://$(container_ip tp1):3000/ping"
echo "expected 200, got $HTTP_STATUS_CODE"
[ "$HTTP_STATUS_CODE" == "200" ]
echo "expected pong, got $HTTP_BODY"
[ "$HTTP_BODY" == "pong" ]
get "http://$(container_ip tp2):3001/ping"
echo "expected 200, got $HTTP_STATUS_CODE"
[ "$HTTP_STATUS_CODE" == "200" ]
echo "expected pong, got $HTTP_BODY"
[ "$HTTP_BODY" == "pong" ]
}
@test "$PREFIX test count on tp1, it should be one" {
get "http://$(container_ip tp1):3000"
@test "$PREFIX ping testproject#1 via testproject#2 server" {
get "http://$(container_ip tp2):3001/send?http://$(container_ip tp1):3000/ping"
echo "expected 200, got $HTTP_STATUS_CODE"
[ "$HTTP_STATUS_CODE" == "200" ]
echo "expected 1, got $HTTP_BODY"
[ "$HTTP_BODY" == "1" ]
echo "expected ok, got $HTTP_BODY"
[ "$HTTP_BODY" == "ok" ]
# ping count should be two by now
count=$(docker logs $(container_id tp1) 2>&1 | grep ping | wc -l)
echo "expected 2, got $count"
[ "$count" -eq "2" ]
}
```
......@@ -246,13 +254,6 @@ Last but not least we do some cleaning and we are done with our first unit test
[ "$?" -eq 0 ]
done
}
@test "$PREFIX drop databases" {
for tp in tp1 tp2; do
drop_database $tp
[ "$?" -eq 0 ]
done
}
```
{% include info.html text="You will find the full version of testproject.bats [here](https://git.feneas.org/feneas/federation/testsuite/raw/master/tests/testproject.bats)!" %}
......
FROM debian:jessie
FROM golang:1.12
RUN apt-get update
RUN apt-get install -y \
git-core postgresql-client nmap curl inotify-tools
RUN git clone https://git.feneas.org/feneas/federation/testsuite-testproject.git
WORKDIR testsuite-testproject
RUN git clone \
https://git.feneas.org/feneas/federation/testsuite-testproject.git \
/go/src/feneas/federation/testsuite-testproject
WORKDIR /go/src/feneas/federation/testsuite-testproject
ADD entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
......
......@@ -11,4 +11,9 @@ if [ "$PROJECT" == "testproject" ]; then
}
fi
./testproject.sh $DATABASE postgreshost $PORT
go build -o server main.go || {
echo "Cannot build sources"
exit 1
}
./server :$PORT
......@@ -4,13 +4,6 @@
load '../test_helper'
@test "$PREFIX create databases" {
for tp in tp1 tp2; do
create_database $tp
[ "$?" -eq 0 ]
done
}
@test "$PREFIX start testproject#1 server" {
start_app tp1 3000 testproject$(latest_tag testproject)
[ "$?" -eq 0 ]
......@@ -24,45 +17,45 @@ load '../test_helper'
# for this project that could look something like this:
#
cmd="docker logs $(container_id tp1)"
text="CREATE TABLE"
text="Listening on :3000"
code=$(wait_for "$cmd" "$text")
[ "$code" -eq "0" ]
}
@test "$PREFIX start testproject#2 server" {
start_app tp2 3000 testproject$(latest_tag testproject)
start_app tp2 3001 testproject$(latest_tag testproject)
[ "$?" -eq 0 ]
cmd="docker logs $(container_id tp2)"
text="CREATE TABLE"
text="Listening on :3001"
code=$(wait_for "$cmd" "$text")
[ "$code" -eq "0" ]
}
@test "$PREFIX test initial count, it should be zero" {
for tp in tp1 tp2; do
get "http://$(container_ip $tp):3000"
echo "expected 200, got $HTTP_STATUS_CODE"
[ "$HTTP_STATUS_CODE" == "200" ]
echo "expected 0, got $HTTP_BODY"
[ "$HTTP_BODY" == "0" ]
done
}
@test "$PREFIX ping both servers, should reply with pong" {
get "http://$(container_ip tp1):3000/ping"
echo "expected 200, got $HTTP_STATUS_CODE"
[ "$HTTP_STATUS_CODE" == "200" ]
echo "expected pong, got $HTTP_BODY"
[ "$HTTP_BODY" == "pong" ]
@test "$PREFIX increase count on tp1 via tp2" {
get "http://$(container_ip tp2):3000/add@$(container_ip tp1):3000"
get "http://$(container_ip tp2):3001/ping"
echo "expected 200, got $HTTP_STATUS_CODE"
[ "$HTTP_STATUS_CODE" == "200" ]
echo "expected pong, got $HTTP_BODY"
[ "$HTTP_BODY" == "pong" ]
}
@test "$PREFIX test count on tp1, it should be one" {
cmd='get "http://'$(container_ip tp1)':3000"'
code=$(wait_for "$cmd" "1")
[ "$code" -eq "0" ]
}
@test "$PREFIX ping testproject#1 via testproject#2 server" {
get "http://$(container_ip tp2):3001/send?http://$(container_ip tp1):3000/ping"
echo "expected 200, got $HTTP_STATUS_CODE"
[ "$HTTP_STATUS_CODE" == "200" ]
echo "expected ok, got $HTTP_BODY"
[ "$HTTP_BODY" == "ok" ]
@test "$PREFIX check the database on tp1 too" {
cmd='query tp1 "select count(*) from testtable;"'
code=$(wait_for "$cmd" "1")
[ "$code" -eq "0" ]
# ping count should be two by now
count=$(docker logs $(container_id tp1) 2>&1 | grep ping | wc -l)
echo "expected 2, got $count"
[ "$count" -eq "2" ]
}
@test "$PREFIX stop and delete the containers" {
......@@ -73,10 +66,3 @@ load '../test_helper'
[ "$?" -eq 0 ]
done
}
@test "$PREFIX drop databases" {
for tp in tp1 tp2; do
drop_database $tp
[ "$?" -eq 0 ]
done
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment