Add travis test cases

Signed-off-by: Qi Zheng <qi.zheng@intel.com>
This commit is contained in:
Qi Zheng
2019-07-26 15:26:32 -07:00
committed by qzheng527
parent b13437467e
commit d71e02723e
6 changed files with 164 additions and 0 deletions
+15
View File
@@ -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)
+12
View File
@@ -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.
+30
View File
@@ -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 ]
}
+24
View File
@@ -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 ]
}
+41
View File
@@ -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 ]
}
+42
View File
@@ -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
}