From 0e78fe24dad493e84268780dd41029cb848b8d72 Mon Sep 17 00:00:00 2001 From: aviau Date: Wed, 27 May 2015 17:45:58 -0400 Subject: [PATCH] Added integration tests Change-Id: I33ede0f954fdfafc8c5ad4052ff787aa48e0cce2 --- surveil/tests/base.py | 21 +++++ surveil/tests/integration/__init__.py | 0 surveil/tests/integration/integration_test.py | 92 +++++++++++++++++++ surveil/tests/integration/test_surveil.py | 69 ++++++++++++++ test-requirements.txt | 2 + tox.ini | 7 +- 6 files changed, 190 insertions(+), 1 deletion(-) create mode 100644 surveil/tests/integration/__init__.py create mode 100644 surveil/tests/integration/integration_test.py create mode 100644 surveil/tests/integration/test_surveil.py diff --git a/surveil/tests/base.py b/surveil/tests/base.py index c645e63..50b3576 100644 --- a/surveil/tests/base.py +++ b/surveil/tests/base.py @@ -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) diff --git a/surveil/tests/integration/__init__.py b/surveil/tests/integration/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/surveil/tests/integration/integration_test.py b/surveil/tests/integration/integration_test.py new file mode 100644 index 0000000..7f9eff1 --- /dev/null +++ b/surveil/tests/integration/integration_test.py @@ -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() diff --git a/surveil/tests/integration/test_surveil.py b/surveil/tests/integration/test_surveil.py new file mode 100644 index 0000000..37b72d4 --- /dev/null +++ b/surveil/tests/integration/test_surveil.py @@ -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." + ) + ) diff --git a/test-requirements.txt b/test-requirements.txt index eb2b05f..8476ab8 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -8,3 +8,5 @@ testrepository>=0.0.18 mongomock httpretty==0.8.3 sphinx_rtd_theme +docker-compose +python-surveilclient diff --git a/tox.ini b/tox.ini index 15e9561..836c008 100644 --- a/tox.ini +++ b/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