From d71e02723e6a089ce4c3385042deb41809a4874f Mon Sep 17 00:00:00 2001 From: Qi Zheng Date: Tue, 23 Jul 2019 23:10:28 +0000 Subject: [PATCH] Add travis test cases Signed-off-by: Qi Zheng --- tests/Makefile | 15 ++++++++++++ tests/README.md | 12 ++++++++++ tests/memcached/memcached.bats | 30 ++++++++++++++++++++++++ tests/nginx/nginx.bats | 24 +++++++++++++++++++ tests/redis/redis.bats | 41 +++++++++++++++++++++++++++++++++ tests/utils.bash | 42 ++++++++++++++++++++++++++++++++++ 6 files changed, 164 insertions(+) create mode 100644 tests/Makefile create mode 100644 tests/README.md create mode 100755 tests/memcached/memcached.bats create mode 100755 tests/nginx/nginx.bats create mode 100755 tests/redis/redis.bats create mode 100644 tests/utils.bash diff --git a/tests/Makefile b/tests/Makefile new file mode 100644 index 0000000..b4939cc --- /dev/null +++ b/tests/Makefile @@ -0,0 +1,15 @@ +HOSTNAME := $(shell hostname -f) +SUBDIR = $(shell ls -d */) + +test_units := $(patsubst %/,%,$(SUBDIR)) + +all: + @echo "use 'make tests' to run all tests'" + @echo "use 'make TEST' to run individual test: ${test_units}" + +.PHONY: $(test_units) +$(test_units): + cd $@; bats -t $@.bats + +.NOTPARALLEL: tests +tests: $(test_units) diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 0000000..9c1c61a --- /dev/null +++ b/tests/README.md @@ -0,0 +1,12 @@ +# Clear Linux micro services unit test + +These are the collections of the Clear Linux micro services unit test. +The micro services are from [page](https://github.com/clearlinux/dockerfiles). + +All the unit tests are writing based on BATS [page](https://github.com/sstephenson/bats). + +To run the tests, you can simple type "make tests", it will run all the test cases. +If you want to run specific micro service test, just type "make SERVICE-NAME". +For example, "make redis" to run tests for redis. + + diff --git a/tests/memcached/memcached.bats b/tests/memcached/memcached.bats new file mode 100755 index 0000000..b0f1e26 --- /dev/null +++ b/tests/memcached/memcached.bats @@ -0,0 +1,30 @@ +#!/usr/bin/env bats +# Copyright (C) 2018 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 + +load ../utils + + +@test "Start memcached server" { + sudo docker run --name memcached-server --rm -d clearlinux/memcached + sleep 5 + run check_container_status memcached-server + [ "$status" -eq 0 ] +} + +@test "Accessing statistics" { + # get ip address + run get_container_ip memcached-server + [ "$status" -eq 0 ] + ipaddr="$output" + + echo stats | nc $ipaddr 11211 +} + + +@test "Stop memcached server" { + sudo docker stop memcached-server + sleep 3 + run check_container_status memcached-server + [ "$status" -eq 1 ] +} diff --git a/tests/nginx/nginx.bats b/tests/nginx/nginx.bats new file mode 100755 index 0000000..4a03a24 --- /dev/null +++ b/tests/nginx/nginx.bats @@ -0,0 +1,24 @@ +#!/usr/bin/env bats +# Copyright (C) 2018 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 + +load ../utils + + +@test "Start nginx server" { + sudo docker run --name nginx-server --rm -d -p 8080:80 clearlinux/nginx + sleep 5 + run check_container_status nginx-server + [ "$status" -eq 0 ] +} + +@test "Connecting to nginx server" { + curl -svL http://localhost:8080/ +} + +@test "Stop nginx server" { + sudo docker stop nginx-server + sleep 3 + run check_container_status nginx-server + [ "$status" -eq 1 ] +} diff --git a/tests/redis/redis.bats b/tests/redis/redis.bats new file mode 100755 index 0000000..9e979e8 --- /dev/null +++ b/tests/redis/redis.bats @@ -0,0 +1,41 @@ +#!/usr/bin/env bats +# Copyright (C) 2018 Intel Corporation +# SPDX-License-Identifier: Apache-2.0 + +load ../utils + +@test "Start redis server" { + sudo docker run --name redis-server --rm -d clearlinux/redis redis-server --protected-mode no + sleep 5 + run check_container_status redis-server + [ "$status" -eq 0 ] +} + +@test "Redis LPUSH test" { + # get ip address + run get_container_ip redis-server + [ "$status" -eq 0 ] + ipaddr="$output" + + # push two integers + sudo docker run --rm clearlinux/redis redis-cli -h $ipaddr lpush mylist x + sudo docker run --rm clearlinux/redis redis-cli -h $ipaddr lpush mylist y +} + +@test "Redis LRANGE test" { + # get ip address + run get_container_ip redis-server + [ "$status" -eq 0 ] + ipaddr="$output" + + # push two integers + sudo docker run --rm clearlinux/redis redis-cli -h $ipaddr lrange mylist 0 -1 +} + + +@test "Stop redis server" { + sudo docker stop redis-server + sleep 3 + run check_container_status redis-server + [ "$status" -eq 1 ] +} diff --git a/tests/utils.bash b/tests/utils.bash new file mode 100644 index 0000000..7e84a20 --- /dev/null +++ b/tests/utils.bash @@ -0,0 +1,42 @@ +# Defines helper functions used across a variety of tests. + +check_os() { + os="unknown" + if [ -f "/etc/os-release" ]; then + local os_name=$(cat /etc/os-release) + if [[ $os_name =~ "Clear Linux" ]]; then + os="clearlinux" + elif [[ $os_name =~ "Ubuntu" ]]; then + os="ubuntu" + elif [[ $os_name =~ "CentOS" ]]; then + os="centos" + else + os="unknown" + fi + fi + + echo $os +} + +# check container status +# if no paramter passed, check the latest created docntainer +# otherwise, check the container name as the parameter +check_container_status() { + if [ -n $1 ]; then + sudo docker ps | grep $1 | grep Up + else + sudo docker ps -l | grep Up + fi +} + +# get container IP address +# param 1, the container name +get_container_ip() { + if [ -n $1 ]; then + local ipaddr=$(sudo docker inspect --format '{{ .NetworkSettings.IPAddress }}' $1) + echo $ipaddr + else + echo "Couldn't get IP address for container $1" + false + fi +}