Added integration tests
Change-Id: I33ede0f954fdfafc8c5ad4052ff787aa48e0cce2
This commit is contained in:
parent
fa2b06e398
commit
0e78fe24da
@ -13,6 +13,7 @@
|
||||
# under the License.
|
||||
|
||||
import sys
|
||||
import time
|
||||
import unittest
|
||||
|
||||
|
||||
@ -32,3 +33,23 @@ class BaseTestCase(unittest.TestCase):
|
||||
)
|
||||
|
||||
return result
|
||||
|
||||
def try_for_x_seconds(self,
|
||||
function,
|
||||
time_to_wait=108,
|
||||
message="Function did not succeed",
|
||||
cooldown=10,
|
||||
exception=Exception):
|
||||
"""Returns True if the functions raises no exception."""
|
||||
|
||||
now = time.time()
|
||||
while True:
|
||||
if time.time() < (now + time_to_wait):
|
||||
try:
|
||||
function()
|
||||
return True
|
||||
except exception:
|
||||
pass
|
||||
time.sleep(cooldown)
|
||||
else:
|
||||
raise Exception(message)
|
||||
|
0
surveil/tests/integration/__init__.py
Normal file
0
surveil/tests/integration/__init__.py
Normal file
92
surveil/tests/integration/integration_test.py
Normal file
92
surveil/tests/integration/integration_test.py
Normal file
@ -0,0 +1,92 @@
|
||||
# Copyright 2014 - Savoir-Faire Linux inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
import os
|
||||
import time
|
||||
import unittest
|
||||
|
||||
from surveil.tests import base
|
||||
|
||||
from compose.cli import docker_client
|
||||
from compose import config as compose_config
|
||||
from compose import project as compose_project
|
||||
from surveilclient import client as sclient
|
||||
|
||||
|
||||
@unittest.skipIf(os.environ.get('SURVEIL_INTEGRATION_TESTS', None) != 'True',
|
||||
'Skipping integraiton tests')
|
||||
class MergedIntegrationTest(base.BaseTestCase):
|
||||
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
surveil_dir = os.path.realpath(
|
||||
os.path.join(
|
||||
os.path.dirname(os.path.realpath(__file__)),
|
||||
"../../../")
|
||||
)
|
||||
compose_file = os.path.join(surveil_dir, 'docker-compose.yml')
|
||||
project_config = compose_config.load(compose_file)
|
||||
|
||||
cls.project = compose_project.Project.from_dicts(
|
||||
"surveilintegrationtest",
|
||||
project_config,
|
||||
docker_client.docker_client()
|
||||
)
|
||||
cls.project.kill()
|
||||
cls.project.remove_stopped()
|
||||
cls.project.up()
|
||||
|
||||
cls.client = sclient.Client(
|
||||
'http://localhost:8080/v2',
|
||||
auth_url='http://localhost:8080/v2/auth',
|
||||
version='2_0'
|
||||
)
|
||||
|
||||
# Wait until Surveil is available
|
||||
now = time.time()
|
||||
while True:
|
||||
print("Waiting for surveil... %s" % int(time.time() - now))
|
||||
if time.time() < (now + 180):
|
||||
try:
|
||||
# If 'ws-arbiter' is found, Surveil is ready!
|
||||
configured_hosts = cls.client.status.hosts.list()
|
||||
host_found = False
|
||||
for host in configured_hosts:
|
||||
if host['host_name'].decode() == 'ws-arbiter':
|
||||
host_found = True
|
||||
break
|
||||
if host_found:
|
||||
break
|
||||
|
||||
except Exception:
|
||||
pass
|
||||
time.sleep(10)
|
||||
else:
|
||||
raise Exception("Surveil could not start")
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
cls.project.kill()
|
||||
cls.project.remove_stopped()
|
||||
|
||||
|
||||
class SeparatedIntegrationTests(MergedIntegrationTest):
|
||||
|
||||
def setUp(self):
|
||||
SeparatedIntegrationTests.setUpClass()
|
||||
|
||||
def tearDown(self):
|
||||
SeparatedIntegrationTests.tearDownClass()
|
69
surveil/tests/integration/test_surveil.py
Normal file
69
surveil/tests/integration/test_surveil.py
Normal file
@ -0,0 +1,69 @@
|
||||
# Copyright 2014 - Savoir-Faire Linux inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
# not use this file except in compliance with the License. You may obtain
|
||||
# a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import requests
|
||||
|
||||
from surveil.tests.integration import integration_test
|
||||
|
||||
|
||||
class TestMergedIngegrationSurveil(
|
||||
integration_test.MergedIntegrationTest
|
||||
):
|
||||
|
||||
def test_hello(self):
|
||||
self.assertEqual(
|
||||
requests.get("http://localhost:8080/v2/hello").text,
|
||||
'Hello World!'
|
||||
)
|
||||
|
||||
|
||||
class TestSeparatedIntegrationSurveil(
|
||||
integration_test.SeparatedIntegrationTests
|
||||
):
|
||||
|
||||
def test_create_host(self):
|
||||
"""Creates a host and asserts that is is monitored by Alignak."""
|
||||
config_hosts = (TestSeparatedIntegrationSurveil.
|
||||
client.status.hosts.list())
|
||||
|
||||
self.assertFalse(
|
||||
any(host['host_name'] == 'integrationhosttest'
|
||||
for host in config_hosts)
|
||||
)
|
||||
|
||||
TestSeparatedIntegrationSurveil.client.config.hosts.create(
|
||||
host_name='integrationhosttest',
|
||||
address='127.0.0.1',
|
||||
)
|
||||
|
||||
TestSeparatedIntegrationSurveil.client.config.reload_config()
|
||||
|
||||
def function():
|
||||
status_hosts = (TestSeparatedIntegrationSurveil.
|
||||
client.status.hosts.list())
|
||||
self.assertTrue(
|
||||
any(host['host_name'].decode() == 'integrationhosttest'
|
||||
for host in status_hosts)
|
||||
|
||||
)
|
||||
|
||||
self.assertTrue(
|
||||
self.try_for_x_seconds(
|
||||
function,
|
||||
time_to_wait=180,
|
||||
cooldown=10,
|
||||
exception=AssertionError,
|
||||
message="Could not find host in status."
|
||||
)
|
||||
)
|
@ -8,3 +8,5 @@ testrepository>=0.0.18
|
||||
mongomock
|
||||
httpretty==0.8.3
|
||||
sphinx_rtd_theme
|
||||
docker-compose
|
||||
python-surveilclient
|
||||
|
7
tox.ini
7
tox.ini
@ -1,11 +1,12 @@
|
||||
[tox]
|
||||
minverson = 1.6
|
||||
envlist = py34, py27, pep8, docs
|
||||
envlist = py34, py27, pep8, docs, integration
|
||||
skipsdist = True
|
||||
|
||||
[testenv]
|
||||
setenv = LANGUAGE=en_US
|
||||
LC_ALL=en_US.utf-8
|
||||
SURVEIL_INTEGRATION_TESTS=False
|
||||
usedevelop = True
|
||||
install_command = pip install -U --force-reinstall {opts} {packages}
|
||||
deps = -r{toxinidir}/requirements.txt
|
||||
@ -13,6 +14,10 @@ deps = -r{toxinidir}/requirements.txt
|
||||
|
||||
commands = python setup.py testr --slowest --testr-args='{posargs}'
|
||||
|
||||
[testenv:integration]
|
||||
setenv = SURVEIL_INTEGRATION_TESTS=True
|
||||
commands = python setup.py testr --slowest --testr-args='--parallel --concurrency=1 --isolated'
|
||||
|
||||
[testenv:pep8]
|
||||
commands = flake8
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user