Commit e6aa5a24 authored by Daniele Venzano's avatar Daniele Venzano

Add integration tests to CI pipeline

parent bb2768eb
stages:
- static-test
- integration-test
- build
- integration-test
- deploy
- cleanup
......@@ -10,7 +10,7 @@ variables:
POSTGRES_DB: zoe
POSTGRES_USER: zoeuser
POSTGRES_PASSWORD: zoepass
ZOE_TEST_IMAGE: zoe:$CI_PIPELINE_ID
ZOE_TEST_IMAGE: zoe-test:$CI_PIPELINE_ID
ZOE_COMMON_OPTIONS: --debug --backend-swarm-url ${SWARM_URL} --deployment-name test${CI_BUILD_REF} --dbuser ${POSTGRES_USER} --dbhost postgres --dbport 5432 --dbname ${POSTGRES_DB} --dbpass ${POSTGRES_PASSWORD} --master-url tcp://localhost:4850 --auth-type text --listen-port 5100 --workspace-base-path /tmp
cache:
......@@ -25,7 +25,7 @@ pylint:
- pip install -U pip setuptools
- pip install -r requirements.txt
- pip install -r requirements_tests.txt
- pylint *.py zoe_* tests/*.py
- pylint *.py zoe_*
unittests:
stage: static-test
......@@ -36,7 +36,7 @@ unittests:
- pip install -U pip setuptools
- pip install -r requirements.txt
- pip install -r requirements_tests.txt
- pytest --ignore tests --tb=short --cov-report=term --cov zoe_api --cov zoe_lib --cov zoe_master
- pytest --ignore integration_tests --tb=short --cov-report=term --cov zoe_api --cov zoe_lib --cov zoe_master
docs-test:
stage: static-test
......@@ -48,35 +48,6 @@ docs-test:
- doc8 docs/
- sh ./build_docs.sh
#api-test:
# stage: integration-test
# services:
# - postgres:9.3
# image: python:3.4
# before_script:
# - pip install -U pip setuptools
# - pip install -r requirements.txt
# - pip install -r requirements_tests.txt
# - echo "admin,admin,admin" > zoepass.csv
# - mkdir /tmp/test${CI_BUILD_REF}
# - python3 create_db_tables.py ${ZOE_COMMON_OPTIONS}
# - python3 zoe-api.py ${ZOE_COMMON_OPTIONS} --log-file zoe-api-${CI_BUILD_REF}.log &
# - sleep 1
# - python3 zoe-master.py ${ZOE_COMMON_OPTIONS} --log-file zoe-master-${CI_BUILD_REF}.log &
# - sleep 1
# script:
# - cd tests
# - coverage run -p basic_auth_success_test.py localhost:5100
# - coverage run -p cookie_auth_success_test.py localhost:5100
# - coverage combine
# - coverage report
# artifacts:
# paths:
# - zoe-api-${CI_BUILD_REF}.log
# - zoe-master-${CI_BUILD_REF}.log
# - tests
# when: always
images:
image: docker:latest
stage: build
......@@ -86,10 +57,19 @@ images:
- mkdir -p $HOME/.docker
- echo $DOCKER_AUTH_CONFIG > $HOME/.docker/config.json
script:
- docker build --pull -t ${DOCKER_REGISTRY}/ci/${ZOE_TEST_IMAGE} .
- docker push ${DOCKER_REGISTRY}/ci/${ZOE_TEST_IMAGE}
only:
- master
- docker build --pull -t zoerepo/${ZOE_TEST_IMAGE} -f Dockerfile.test .
- docker push zoerepo/${ZOE_TEST_IMAGE}
api-test:
stage: integration-test
services:
- postgres:9.3
image: zoerepo/${ZOE_TEST_IMAGE}
- run_integration_tests.sh
artifacts:
paths:
- integration_tests
when: always
mirror-github:
stage: deploy
......
"""Test script for unsuccessful basic authentication."""
import requests
ZOE_API_URI = 'http://127.0.0.1:5100/api/0.7/'
WRONG_AUTH = ('test', '123')
TIMEOUT = 5
class TestZoeRestAuthFails:
"""Test case class."""
def test_userinfo(self, zoe_api_process):
"""Test userinfo api endpoint."""
print('Test userinfo api endpoint')
req = requests.get(ZOE_API_URI + 'userinfo', auth=WRONG_AUTH)
assert req.status_code == 401
def test_3_execution_details(self, zoe_api_process):
"""Test execution details api endpoint."""
print('Test execution details api endpoint')
req = requests.get(ZOE_API_URI + 'execution/345', auth=WRONG_AUTH)
assert req.status_code == 401
def test_4_terminate_execution(self, zoe_api_process):
"""Test terminate no execution api endpoint."""
print('Test terminate no execution api endpoint')
req = requests.delete(ZOE_API_URI + 'execution/345', auth=WRONG_AUTH)
assert req.status_code == 401
def test_6_delete_execution(self, zoe_api_process):
"""Test delete execution api endpoint."""
print('Test delete execution api endpoint')
req = requests.delete(ZOE_API_URI + 'execution/delete/234', auth=WRONG_AUTH)
assert req.status_code == 401
def test_2_start_failed_execution(self, zoe_api_process):
"""Test fail submit execution api endpoint."""
print('Test fail submit execution api endpoint')
req = requests.post(ZOE_API_URI + 'execution', auth=WRONG_AUTH, json={"application": "data", "name": "requests"})
assert req.status_code == 401
"""Test script for successful basic authentication."""
import subprocess
import time
import json
import sys
import os
import pytest
import requests
ZOE_API_URI = 'http://127.0.0.1:5100/api/0.7/'
ZOE_AUTH = ('admin', 'admin')
TIMEOUT = 5
ARGS = [
'--debug',
'--deployment-name', 'integration_test',
'--dbuser', 'zoeuser',
'--dbhost', 'postgres',
'--dbport', '5432',
'--dbname', 'zoe',
'--dbpass', 'zoepass',
'--master-url', 'tcp://localhost:4850',
'--auth-type', 'text',
'--listen-port', '5100',
'--workspace-base-path', '/tmp',
'--workspace-deployment-path', 'integration_test',
'--auth-file', 'zoepass.csv',
'--backend', 'DockerEngine',
'--backend-docker-config-file', 'tests/sample_docker.conf',
'--zapp-shop-path', 'contrib/zapp-shop-sample'
]
@pytest.fixture(scope="session")
def zoe_api_process(request):
"""Fixture that starts the Zoe API process."""
proc = subprocess.Popen(["python", "zoe-api.py"] + ARGS, stderr=sys.stderr, stdout=sys.stdout)
request.addfinalizer(proc.terminate)
time.sleep(2)
@pytest.fixture(scope="session")
def zoe_master_process(request):
"""Fixture that starts the Zoe Master process."""
os.mkdir('/tmp/integration_test')
proc = subprocess.Popen(["python", "zoe-master.py"] + ARGS, stderr=sys.stderr, stdout=sys.stdout)
request.addfinalizer(proc.terminate)
time.sleep(4)
class TestZoeRest:
"""Test case class."""
......@@ -85,7 +45,7 @@ class TestZoeRest:
"""Test start execution api endpoint."""
print('Test start execution api endpoint')
with open('tests/zapp.json', encoding='utf-8') as data_file:
with open('integration_tests/zapp.json', encoding='utf-8') as data_file:
data = json.loads(data_file.read())
req = requests.post(ZOE_API_URI + 'execution', auth=ZOE_AUTH, json={"application": data, "name": "requests"}, timeout=TIMEOUT)
......@@ -118,7 +78,7 @@ class TestZoeRest:
def test_zapp_validate(self, zoe_api_process):
"""Test ZApp validation endpoint"""
print("Test ZApp validation endpoint")
with open('tests/zapp.json', encoding='utf-8') as data_file:
with open('integration_tests/zapp.json', encoding='utf-8') as data_file:
data = json.loads(data_file.read())
req = requests.post(ZOE_API_URI + 'zapp_validate', json={"application": data}, timeout=TIMEOUT)
......@@ -127,7 +87,7 @@ class TestZoeRest:
def test_zapp_validate_fail(self, zoe_api_process):
"""Test ZApp validation endpoint"""
print("Test ZApp validation endpoint")
with open('tests/zapp.json', encoding='utf-8') as data_file:
with open('integration_tests/zapp.json', encoding='utf-8') as data_file:
data = json.loads(data_file.read())
del data['version']
......
import os
import subprocess
import sys
import time
import pytest
ARGS = [
'--debug',
'--deployment-name', 'integration_test',
'--dbuser', 'zoeuser',
'--dbhost', 'postgres',
'--dbport', '5432',
'--dbname', 'zoe',
'--dbpass', 'zoepass',
'--master-url', 'tcp://localhost:4850',
'--auth-type', 'text',
'--listen-port', '5100',
'--workspace-base-path', '/tmp',
'--workspace-deployment-path', 'integration_test',
'--auth-file', 'zoepass.csv',
'--backend', 'DockerEngine',
'--backend-docker-config-file', 'integration_tests/sample_docker.conf',
'--zapp-shop-path', 'contrib/zapp-shop-sample'
]
@pytest.fixture(scope="session")
def zoe_api_process(request):
"""Fixture that starts the Zoe API process."""
proc = subprocess.Popen(["python", "zoe-api.py"] + ARGS, stderr=sys.stderr, stdout=sys.stdout)
request.addfinalizer(proc.terminate)
time.sleep(2)
@pytest.fixture(scope="session")
def zoe_master_process(request):
"""Fixture that starts the Zoe Master process."""
os.mkdir('/tmp/integration_test')
proc = subprocess.Popen(["python", "zoe-master.py"] + ARGS, stderr=sys.stderr, stdout=sys.stdout)
request.addfinalizer(proc.terminate)
time.sleep(4)
#!/usr/bin/env bash
docker build -f Dockerfile.test -t zoe_test_image .
if [ -z ${CI_PIPELINE_ID} ]; then
docker build -f Dockerfile.test -t zoe_test_image .
docker network create zoe_test
docker network create zoe_test
docker run -d --network zoe_test --name postgres -p 5432:5432 -e POSTGRES_DB=zoe -e POSTGRES_USER=zoeuser -e POSTGRES_PASSWORD=zoepass postgres:9.3
docker pull nginx:alpine
sleep 4 # give postgres the time to start
docker run -it --network zoe_test --name zoe zoe_test_image pytest --tb=short --cov-report=term --cov zoe_api --cov zoe_lib --cov zoe_master tests/basic_auth_success_test.py
docker run -d --network zoe_test --name postgres -p 5432:5432 -e POSTGRES_DB=zoe -e POSTGRES_USER=zoeuser -e POSTGRES_PASSWORD=zoepass postgres:9.3
docker pull nginx:alpine
sleep 4 # give postgres the time to start
docker run -it --network zoe_test --name zoe zoe_test_image pytest integration_tests/
docker rm -f zoe postgres
docker network rm zoe_test
docker rm -f zoe postgres
docker network rm zoe_test
else # running in CI
pytest --tb=short integration_tests/
fi
"""Test script for unsuccessful basic authentication."""
import time
import sys
import unittest
import requests
ZOE_API_URI = 'http://192.168.12.2:5100/api/0.7/'
ZOE_AUTH = ('admin', 'admin')
class ZoeRestTestSuccess(unittest.TestCase):
"""Test case class."""
uri = ZOE_API_URI
auth = ZOE_AUTH
wrong_auth = ('test', '123')
id = '-1'
def tearDown(self):
"""Test end."""
time.sleep(3)
def test_1_userinfo(self):
"""Test userinfo api endpoint."""
print('Test userinfo api endpoint')
req = requests.get(self.__class__.uri + 'userinfo', auth=self.__class__.wrong_auth)
self.assertEqual(req.status_code, 401)
def test_3_execution_details(self):
"""Test execution details api endpoint."""
print('Test execution details api endpoint')
req = requests.get(self.__class__.uri + 'execution/' + self.__class__.id, auth=self.__class__.auth)
self.assertEqual(req.status_code, 404)
def test_4_terminate_execution(self):
"""Test terminate no execution api endpoint."""
print('Test terminate no execution api endpoint')
req = requests.delete(self.__class__.uri + 'execution/' + self.__class__.id, auth=self.__class__.auth)
self.assertEqual(req.status_code, 404)
def test_6_delete_execution(self):
"""Test delete execution api endpoint."""
print('Test delete execution api endpoint')
req = requests.delete(self.__class__.uri + 'execution/delete/' + self.__class__.id, auth=self.__class__.auth)
self.assertEqual(req.status_code, 404)
def test_2_start_failed_execution(self):
"""Test fail submit execution api endpoint."""
print('Test fail submit execution api endpoint')
req = requests.post(self.__class__.uri + 'execution', auth=self.__class__.auth, json={"application": "data", "name": "requests"})
self.assertEqual(req.status_code, 400)
if __name__ == '__main__':
if len(sys.argv) > 1:
API_SERVER = sys.argv.pop()
ZoeRestTestSuccess.uri = 'http://' + API_SERVER + '/api/0.7/'
unittest.main()
"""Test script for unsuccessful cookie authentication."""
import sys
import time
import unittest
import requests
ZOE_API_URI = 'http://192.168.12.2:5100/api/0.7/'
ZOE_AUTH = ('admin', 'admin')
class ZoeRestTestSuccess(unittest.TestCase):
"""Test case class."""
uri = ZOE_API_URI
id = '-1'
session = None
def tearDown(self):
"""Test end."""
time.sleep(3)
def test_0_login_fail(self):
"""Test failed login api endpoint."""
print('Test failed login api endpoint')
session = requests.Session()
req = session.get(self.__class__.uri + 'login', auth=('test', '123'))
self.assertEqual(req.status_code, 401)
def test_1_login(self):
"""Test login api endpoint."""
print('Test login api endpoint')
session = requests.Session()
req = session.get(self.__class__.uri + 'login', auth=('test', '1234'))
self.assertEqual(req.status_code, 200)
self.assertGreater(len(session.cookies.items()), 0)
self.__class__.session = session
def test_4_execution_details(self):
"""Test execution details api endpoint."""
print('Test execution details api endpoint')
session = self.__class__.session
req = session.get(self.__class__.uri + 'execution/' + self.__class__.id)
self.assertEqual(req.status_code, 404)
def test_5_terminate_execution(self):
"""Test terminate execution api endpoint."""
print('Test terminate execution api endpoint')
session = self.__class__.session
req = session.delete(self.__class__.uri + 'execution/' + self.__class__.id)
self.assertEqual(req.status_code, 404)
def test_7_delete_execution(self):
"""Test delete execution api endpoint."""
print('Test delete execution api endpoint')
session = self.__class__.session
req = session.delete(self.__class__.uri + 'execution/delete/' + self.__class__.id)
self.assertEqual(req.status_code, 404)
def test_3_start_execution(self):
"""Test start execution api endpoint."""
print('Test start execution api endpoint')
session = self.__class__.session
req = session.post(self.__class__.uri + 'execution', json={"application": "data", "name": "requests"})
self.assertEqual(req.status_code, 400)
if __name__ == '__main__':
if len(sys.argv) > 1:
API_SERVER = sys.argv.pop()
ZoeRestTestSuccess.uri = 'http://' + API_SERVER + '/api/0.7/'
unittest.main()
"""Test script for successful cookie authentication."""
import json
import time
import sys
import unittest
import requests
ZOE_API_URI = 'http://192.168.12.2:5100/api/0.7/'
ZOE_AUTH = ('admin', 'admin')
class ZoeRestTestSuccess(unittest.TestCase):
"""Test case class."""
uri = ZOE_API_URI
id = ''
session = None
def tearDown(self):
"""Test end."""
time.sleep(3)
def test_0_login(self):
"""Test login api endpoint."""
print('Test login api endpoint')
session = requests.Session()
req = session.get(self.__class__.uri + 'login', auth=ZOE_AUTH)
self.assertEqual(req.status_code, 200)
self.assertGreater(len(session.cookies.items()), 0)
self.__class__.session = session
def test_1_info(self):
"""Test info api endpoint."""
print('Test info api endpoint')
session = self.__class__.session
req = session.get(self.__class__.uri + 'info')
self.assertEqual(req.status_code, 200)
def test_2_userinfo(self):
"""Test userinfo api endpoint."""
print('Test userinfo api endpoint')
session = self.__class__.session
req = session.get(self.__class__.uri + 'userinfo')
self.assertEqual(req.status_code, 200)
def test_4_execution_details(self):
"""Test execution details api endpoint."""
print('Test execution details api endpoint')
session = self.__class__.session
req = session.get(self.__class__.uri + 'execution/' + self.__class__.id)
self.assertEqual(req.status_code, 200)
def test_4_service_details(self):
"""Test service details api endpoint."""
print('Test service details api endpoint')
session = self.__class__.session
req = session.get(self.__class__.uri + 'execution/' + self.__class__.id)
sid = req.json()["services"][0]
req = session.get(self.__class__.uri + 'service/' + str(sid))
self.assertEqual(req.status_code, 200)
def test_5_terminate_execution(self):
"""Test terminate execution api endpoint."""
print('Test terminate execution api endpoint')
session = self.__class__.session
req = session.delete(self.__class__.uri + 'execution/' + self.__class__.id)
self.assertEqual(req.status_code, 204)
def test_6_list_all_executions(self):
"""Test list all executions api endpoint."""
print('Test list all executions api endpoint')
session = self.__class__.session
req = session.get(self.__class__.uri + 'execution')
self.assertEqual(req.status_code, 200)
def test_7_delete_execution(self):
"""Test delete execution api endpoint."""
print('Test delete execution api endpoint')
session = self.__class__.session
req = session.delete(self.__class__.uri + 'execution/delete/' + self.__class__.id)
self.assertEqual(req.status_code, 204)
def test_3_start_execution(self):
"""Test start execution api endpoint."""
print('Test start execution api endpoint')
with open('zapp.json', encoding='utf-8') as data_file:
data = json.loads(data_file.read())
session = self.__class__.session
req = session.post(self.__class__.uri + 'execution', json={"application": data, "name": "requests"})
self.assertEqual(req.status_code, 201)
self.__class__.id = str(req.json()['execution_id'])
def test_zapp_validate(self):
"""Test ZApp validation endpoint"""
print("Test ZApp validation endpoint")
with open('zapp.json', encoding='utf-8') as data_file:
data = json.loads(data_file.read())
req = requests.post(self.__class__.uri + 'zapp_validate', json={"application": data})
self.assertEqual(req.status_code, 200)
if __name__ == '__main__':
if len(sys.argv) > 1:
API_SERVER = sys.argv.pop()
ZoeRestTestSuccess.uri = 'http://' + API_SERVER + '/api/0.7/'
unittest.main()
"""Corner test."""
import json
import sys
import time
import unittest
import requests
class ZoeRestTestSuccess(unittest.TestCase):
"""Test case class."""
uri = 'http://localhost:5001/api/0.6/'
auth = ('test', '1234')
id = ''
def tearDown(self):
"""Test end."""
time.sleep(3)
def test_3_execution_details(self):
"""Test execution details api endpoint."""
print('Test execution details api endpoint')
req = requests.get(self.__class__.uri + 'execution/' + self.__class__.id, auth=self.__class__.auth)
self.assertEqual(req.status_code, 200)
def test_4_terminate_execution(self):
"""Test terminate no execution api endpoint."""
print('Test terminate no execution api endpoint')
req = requests.delete(self.__class__.uri + 'execution/' + self.__class__.id, auth=self.__class__.auth)
self.assertEqual(req.status_code, 400)
def test_6_delete_execution(self):
"""Test delete execution api endpoint."""
print('Test delete execution api endpoint')
req = requests.delete(self.__class__.uri + 'execution/delete/' + self.__class__.id, auth=self.__class__.auth)
self.assertEqual(req.status_code, 204)
def test_2_start_failed_execution(self):
"""Test fail submit execution api endpoint."""
print('Test fail submit execution api endpoint')
with open('dummy.json', encoding='utf-8') as data_file:
data = json.loads(data_file.read())
req = requests.post(self.__class__.uri + 'execution', auth=self.__class__.auth, json={"application": data, "name": "requests"})
self.assertEqual(req.status_code, 201)
self.__class__.id = str(req.json()['execution_id'])
if __name__ == '__main__':
if len(sys.argv) > 1:
API_SERVER = sys.argv.pop()
ZoeRestTestSuccess.uri = 'http://' + API_SERVER + '/api/0.7/'
unittest.main()
......@@ -28,4 +28,4 @@ class TestDockerEngineBackendConfig:
def test_parsing_config_file(self):
"""Test Docker backend config parsing."""
hosts = config.DockerConfig(config_file='tests/sample_docker.conf').read_config()
assert len(hosts) == 16
assert len(hosts) == 1
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