Integration tests: Multiple backends

Change-Id: I4f932b939e5e1e8b3feb6de1b9be7e8d773f2495
This commit is contained in:
aviau 2015-07-02 11:17:49 -04:00 committed by peyre
parent 2c205c7b43
commit d1f2fbfd86
6 changed files with 132 additions and 91 deletions

View File

@ -0,0 +1,91 @@
# 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 os
import time
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
class DockerBackend():
def __init__(self):
pass
def setUpClass(self):
surveil_dir = os.path.realpath(
os.path.join(
os.path.dirname(os.path.realpath(__file__)),
"../../../../"
)
)
compose_file = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
'integration.yml'
)
project_config = compose_config.from_dictionary(
compose_config.load_yaml(compose_file),
working_dir=surveil_dir,
filename=compose_file
)
self.project = compose_project.Project.from_dicts(
"surveilintegrationtest",
project_config,
docker_client.docker_client()
)
self.project.kill()
self.project.remove_stopped()
self.project.build()
self.project.up()
self.surveil_client = sclient.Client(
'http://localhost:8999/v2',
auth_url='http://localhost:8999/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 + 280):
try:
# If 'ws-arbiter' is found, Surveil is ready!
configured_hosts = self.surveil_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")
def tearDownClass(self):
self.project.kill()
self.project.remove_stopped()
def get_surveil_client(self):
return self.surveil_client

View File

@ -15,15 +15,10 @@
from __future__ import print_function from __future__ import print_function
import os import os
import time
import unittest import unittest
from surveil.tests import base from surveil.tests import base
from surveil.tests.integration.backend import docker
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', @unittest.skipIf(os.environ.get('SURVEIL_INTEGRATION_TESTS', None) != 'True',
@ -32,65 +27,25 @@ class MergedIntegrationTest(base.BaseTestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
surveil_dir = os.path.realpath( test_backend = os.environ.get(
os.path.join( 'SURVEIL_INTEGRATION_TESTS_BACKEND',
os.path.dirname(os.path.realpath(__file__)), None
"../../../")
) )
compose_file = os.path.join( if test_backend == 'DOCKER':
os.path.dirname(os.path.realpath(__file__)), MergedIntegrationTest.backend = docker.DockerBackend()
'integration.yml' else:
) raise Exception(
"Could not identify tests backend: '%s'" % test_backend
project_config = compose_config.from_dictionary( )
compose_config.load_yaml(compose_file), cls.backend.setUpClass()
working_dir=surveil_dir,
filename=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.build()
cls.project.up()
cls.client = sclient.Client(
'http://localhost:8999/v2',
auth_url='http://localhost:8999/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 @classmethod
def tearDownClass(cls): def tearDownClass(cls):
cls.project.kill() cls.backend.tearDownClass()
cls.project.remove_stopped()
def get_surveil_client(self):
return MergedIntegrationTest.backend.get_surveil_client()
class SeparatedIntegrationTests(MergedIntegrationTest): class SeparatedIntegrationTests(MergedIntegrationTest):

View File

@ -32,25 +32,23 @@ class TestSeparatedIntegrationSurveil(
): ):
def test_create_host(self): def test_create_host(self):
"""Creates a host and asserts that is is monitored by Alignak.""" """Creates a host and asserts that is is monitored by Alignak."""
config_hosts = (TestSeparatedIntegrationSurveil. config_hosts = self.get_surveil_client().status.hosts.list()
client.status.hosts.list())
self.assertFalse( self.assertFalse(
any(host['host_name'] == 'integrationhosttest' any(host['host_name'] == 'integrationhosttest'
for host in config_hosts) for host in config_hosts)
) )
TestSeparatedIntegrationSurveil.client.config.hosts.create( self.get_surveil_client().config.hosts.create(
host_name='integrationhosttest', host_name='integrationhosttest',
address='127.0.0.1', address='127.0.0.1',
use='generic-host', use='generic-host',
) )
TestSeparatedIntegrationSurveil.client.config.reload_config() self.get_surveil_client().config.reload_config()
def function(): def function():
status_hosts = (TestSeparatedIntegrationSurveil. status_hosts = self.get_surveil_client().status.hosts.list()
client.status.hosts.list())
self.assertTrue( self.assertTrue(
any(host['host_name'].decode() == 'integrationhosttest' any(host['host_name'].decode() == 'integrationhosttest'
for host in status_hosts) for host in status_hosts)
@ -69,14 +67,14 @@ class TestSeparatedIntegrationSurveil(
def test_delete_host(self): def test_delete_host(self):
self.test_create_host() self.test_create_host()
TestSeparatedIntegrationSurveil.client.config.hosts.delete( self.get_surveil_client().config.hosts.delete(
'integrationhosttest') 'integrationhosttest'
)
TestSeparatedIntegrationSurveil.client.config.reload_config() self.get_surveil_client().config.reload_config()
def function(): def function():
status_hosts = (TestSeparatedIntegrationSurveil. status_hosts = (self.get_surveil_client().status.hosts.list())
client.status.hosts.list())
self.assertFalse( self.assertFalse(
any(host['host_name'].decode() == 'integrationhosttest' any(host['host_name'].decode() == 'integrationhosttest'
for host in status_hosts) for host in status_hosts)
@ -93,16 +91,16 @@ class TestSeparatedIntegrationSurveil(
) )
def test_passive_check(self): def test_passive_check(self):
TestSeparatedIntegrationSurveil.client.config.hosts.create( self.get_surveil_client().config.hosts.create(
host_name='integrationhosttest', host_name='integrationhosttest',
address='127.0.0.1', address='127.0.0.1',
use='generic-host', use='generic-host',
) )
TestSeparatedIntegrationSurveil.client.config.commands.create( self.get_surveil_client().config.commands.create(
command_name='check_integrationhosttest', command_name='check_integrationhosttest',
command_line='/usr/lib/monitoring/plugins/sfl/check_example' command_line='/usr/lib/monitoring/plugins/sfl/check_example'
) )
TestSeparatedIntegrationSurveil.client.config.services.create( self.get_surveil_client().config.services.create(
check_command="check_integrationhosttest", check_command="check_integrationhosttest",
check_interval="5", check_interval="5",
check_period="24x7", check_period="24x7",
@ -117,19 +115,16 @@ class TestSeparatedIntegrationSurveil(
passive_checks_enabled="1" passive_checks_enabled="1"
) )
TestSeparatedIntegrationSurveil.client.config.reload_config() self.get_surveil_client().config.reload_config()
(TestSeparatedIntegrationSurveil.client.status.services. self.get_surveil_client().status.services.submit_check_result(
submit_check_result( host_name='integrationhosttest',
host_name='integrationhosttest', service_description='check_integrationhosttest',
service_description='check_integrationhosttest', output="Hello",
output="Hello", return_code=0
return_code=0 )
)
)
def function(): def function():
status_services = (TestSeparatedIntegrationSurveil. status_services = self.get_surveil_client().status.services.list()
client.status.services.list())
self.assertFalse( self.assertFalse(
any(service['host_name'].decode() == 'integrationhosttest' and any(service['host_name'].decode() == 'integrationhosttest' and
service['service_description'].decode() == service['service_description'].decode() ==
@ -150,16 +145,16 @@ class TestSeparatedIntegrationSurveil(
) )
def test_custom_plugins(self): def test_custom_plugins(self):
TestSeparatedIntegrationSurveil.client.config.hosts.create( self.get_surveil_client().config.hosts.create(
host_name='integrationhosttest', host_name='integrationhosttest',
address='127.0.0.1', address='127.0.0.1',
use='generic-host', use='generic-host',
) )
TestSeparatedIntegrationSurveil.client.config.commands.create( self.get_surveil_client().config.commands.create(
command_name='check_integrationhosttest', command_name='check_integrationhosttest',
command_line='/usr/lib/monitoring/plugins/sfl/check_example' command_line='/usr/lib/monitoring/plugins/sfl/check_example'
) )
TestSeparatedIntegrationSurveil.client.config.services.create( self.get_surveil_client().config.services.create(
check_command="check_integrationhosttest", check_command="check_integrationhosttest",
check_interval="5", check_interval="5",
check_period="24x7", check_period="24x7",
@ -174,11 +169,10 @@ class TestSeparatedIntegrationSurveil(
passive_checks_enabled="1" passive_checks_enabled="1"
) )
TestSeparatedIntegrationSurveil.client.config.reload_config() self.get_surveil_client().config.reload_config()
def function(): def function():
status_services = (TestSeparatedIntegrationSurveil. status_services = self.get_surveil_client().status.services.list()
client.status.services.list())
self.assertFalse( self.assertFalse(
any(service['host_name'].decode() == 'integrationhosttest' and any(service['host_name'].decode() == 'integrationhosttest' and
service['service_description'].decode() == service['service_description'].decode() ==

View File

@ -16,6 +16,7 @@ commands = python setup.py testr --slowest --testr-args='{posargs}'
[testenv:integration] [testenv:integration]
setenv = SURVEIL_INTEGRATION_TESTS=True setenv = SURVEIL_INTEGRATION_TESTS=True
SURVEIL_INTEGRATION_TESTS_BACKEND=DOCKER
commands = python setup.py testr --slowest --testr-args='--parallel --concurrency=1 --isolated' commands = python setup.py testr --slowest --testr-args='--parallel --concurrency=1 --isolated'
[testenv:pep8] [testenv:pep8]