diff --git a/surveil/tests/integration/backend/__init__.py b/surveil/tests/integration/backend/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/surveil/tests/integration/backend/docker.py b/surveil/tests/integration/backend/docker.py new file mode 100644 index 0000000..d844abb --- /dev/null +++ b/surveil/tests/integration/backend/docker.py @@ -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 diff --git a/surveil/tests/integration/integration.yml b/surveil/tests/integration/backend/integration.yml similarity index 100% rename from surveil/tests/integration/integration.yml rename to surveil/tests/integration/backend/integration.yml diff --git a/surveil/tests/integration/integration_test.py b/surveil/tests/integration/integration_test.py index b89635c..57bc28f 100644 --- a/surveil/tests/integration/integration_test.py +++ b/surveil/tests/integration/integration_test.py @@ -15,15 +15,10 @@ 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 +from surveil.tests.integration.backend import docker @unittest.skipIf(os.environ.get('SURVEIL_INTEGRATION_TESTS', None) != 'True', @@ -32,65 +27,25 @@ class MergedIntegrationTest(base.BaseTestCase): @classmethod def setUpClass(cls): - surveil_dir = os.path.realpath( - os.path.join( - os.path.dirname(os.path.realpath(__file__)), - "../../../") + test_backend = os.environ.get( + 'SURVEIL_INTEGRATION_TESTS_BACKEND', + None ) - 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 - ) - - 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") + if test_backend == 'DOCKER': + MergedIntegrationTest.backend = docker.DockerBackend() + else: + raise Exception( + "Could not identify tests backend: '%s'" % test_backend + ) + cls.backend.setUpClass() @classmethod def tearDownClass(cls): - cls.project.kill() - cls.project.remove_stopped() + cls.backend.tearDownClass() + + def get_surveil_client(self): + return MergedIntegrationTest.backend.get_surveil_client() class SeparatedIntegrationTests(MergedIntegrationTest): diff --git a/surveil/tests/integration/test_surveil.py b/surveil/tests/integration/test_surveil.py index 32738cc..21fe141 100644 --- a/surveil/tests/integration/test_surveil.py +++ b/surveil/tests/integration/test_surveil.py @@ -32,25 +32,23 @@ class TestSeparatedIntegrationSurveil( ): def test_create_host(self): """Creates a host and asserts that is is monitored by Alignak.""" - config_hosts = (TestSeparatedIntegrationSurveil. - client.status.hosts.list()) + config_hosts = self.get_surveil_client().status.hosts.list() self.assertFalse( any(host['host_name'] == 'integrationhosttest' for host in config_hosts) ) - TestSeparatedIntegrationSurveil.client.config.hosts.create( + self.get_surveil_client().config.hosts.create( host_name='integrationhosttest', address='127.0.0.1', use='generic-host', ) - TestSeparatedIntegrationSurveil.client.config.reload_config() + self.get_surveil_client().config.reload_config() def function(): - status_hosts = (TestSeparatedIntegrationSurveil. - client.status.hosts.list()) + status_hosts = self.get_surveil_client().status.hosts.list() self.assertTrue( any(host['host_name'].decode() == 'integrationhosttest' for host in status_hosts) @@ -69,14 +67,14 @@ class TestSeparatedIntegrationSurveil( def test_delete_host(self): self.test_create_host() - TestSeparatedIntegrationSurveil.client.config.hosts.delete( - 'integrationhosttest') + self.get_surveil_client().config.hosts.delete( + 'integrationhosttest' + ) - TestSeparatedIntegrationSurveil.client.config.reload_config() + self.get_surveil_client().config.reload_config() def function(): - status_hosts = (TestSeparatedIntegrationSurveil. - client.status.hosts.list()) + status_hosts = (self.get_surveil_client().status.hosts.list()) self.assertFalse( any(host['host_name'].decode() == 'integrationhosttest' for host in status_hosts) @@ -93,16 +91,16 @@ class TestSeparatedIntegrationSurveil( ) def test_passive_check(self): - TestSeparatedIntegrationSurveil.client.config.hosts.create( + self.get_surveil_client().config.hosts.create( host_name='integrationhosttest', address='127.0.0.1', use='generic-host', ) - TestSeparatedIntegrationSurveil.client.config.commands.create( + self.get_surveil_client().config.commands.create( command_name='check_integrationhosttest', 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_interval="5", check_period="24x7", @@ -117,19 +115,16 @@ class TestSeparatedIntegrationSurveil( passive_checks_enabled="1" ) - TestSeparatedIntegrationSurveil.client.config.reload_config() - (TestSeparatedIntegrationSurveil.client.status.services. - submit_check_result( - host_name='integrationhosttest', - service_description='check_integrationhosttest', - output="Hello", - return_code=0 - ) - ) + self.get_surveil_client().config.reload_config() + self.get_surveil_client().status.services.submit_check_result( + host_name='integrationhosttest', + service_description='check_integrationhosttest', + output="Hello", + return_code=0 + ) def function(): - status_services = (TestSeparatedIntegrationSurveil. - client.status.services.list()) + status_services = self.get_surveil_client().status.services.list() self.assertFalse( any(service['host_name'].decode() == 'integrationhosttest' and service['service_description'].decode() == @@ -150,16 +145,16 @@ class TestSeparatedIntegrationSurveil( ) def test_custom_plugins(self): - TestSeparatedIntegrationSurveil.client.config.hosts.create( + self.get_surveil_client().config.hosts.create( host_name='integrationhosttest', address='127.0.0.1', use='generic-host', ) - TestSeparatedIntegrationSurveil.client.config.commands.create( + self.get_surveil_client().config.commands.create( command_name='check_integrationhosttest', 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_interval="5", check_period="24x7", @@ -174,11 +169,10 @@ class TestSeparatedIntegrationSurveil( passive_checks_enabled="1" ) - TestSeparatedIntegrationSurveil.client.config.reload_config() + self.get_surveil_client().config.reload_config() def function(): - status_services = (TestSeparatedIntegrationSurveil. - client.status.services.list()) + status_services = self.get_surveil_client().status.services.list() self.assertFalse( any(service['host_name'].decode() == 'integrationhosttest' and service['service_description'].decode() == diff --git a/tox.ini b/tox.ini index 1ec82c5..f926b2a 100644 --- a/tox.ini +++ b/tox.ini @@ -16,6 +16,7 @@ commands = python setup.py testr --slowest --testr-args='{posargs}' [testenv:integration] setenv = SURVEIL_INTEGRATION_TESTS=True + SURVEIL_INTEGRATION_TESTS_BACKEND=DOCKER commands = python setup.py testr --slowest --testr-args='--parallel --concurrency=1 --isolated' [testenv:pep8]