Skip to content
Snippets Groups Projects
test_alerts.py 3.61 KiB
Newer Older
#!/usr/bin/python3
"""
## © University of Southampton IT Innovation Centre, 2018
##
## Copyright in this software belongs to University of Southampton
## IT Innovation Centre of Gamma House, Enterprise Road,
## Chilworth Science Park, Southampton, SO16 7NS, UK.
##
## This software may not be used, sold, licensed, transferred, copied
## or reproduced in whole or in part in any manner or form or in or
## on any media by any person other than in accordance with the terms
## of the Licence Agreement supplied with the software, or otherwise
## without the prior written consent of the copyright owners.
##
## This software is distributed WITHOUT ANY WARRANTY, without even the
## implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
## PURPOSE, except where stated in the Licence Agreement supplied with
## the software.
##
##      Created By :            Nikolay Stanchev
##      Created Date :          22-08-2018
##      Created for Project :   FLAME
"""

from time import sleep
from requests import post, get
from os import listdir
from os.path import join, dirname
from clmctest.alerts.alert_handler_server import LOG_TEST_FOLDER_PATH


CLMC_SERVICE_PORT = 9080
NGINX_PORT = 80


class TestAlerts(object):

    def test_alert_triggers(self, rspec_config):
        """
        Test is implemented using the following steps:
            * Send clmc service a TOSCA alert spec. file
            * Wait 15 seconds for Kapacitor to configure and start executing the defined tasks
            * Send some test requests to nginx to increase the load
            * Wait 20 seconds for alerts to be triggered
            * Check that 4 log files have been created - one for each alert defined in the alert spec.

        :param rspec_config: fixture from conftest.py
        """

        global CLMC_SERVICE_PORT, NGINX_PORT

        clmc_service_host, nginx_host = None, None
        for host in rspec_config:
            if host["name"] == "clmc-service":
                clmc_service_host = host["ip_address"]
            elif host["name"] == "nginx":
                nginx_host = host["ip_address"]

            if clmc_service_host is not None and nginx_host is not None:
                break

        print("Sending alerts specification to clmc service...")
        alerts_spec = join(dirname(__file__), "alerts_test_config.yaml")
        resources_spec = join(dirname(__file__), "resources_test_config.yaml")

        with open(alerts_spec, 'rb') as alerts:
            with open(resources_spec, 'rb') as resources:
                files = {'alert-spec': alerts, 'resource-spec': resources}
                response = post("http://{0}:{1}/alerts".format(clmc_service_host, CLMC_SERVICE_PORT), files=files)
                assert response.status_code == 200
                clmc_service_response = response.json()
                assert "triggers_specification_errors" not in clmc_service_response, "Unexpected error was returned for triggers specification"
                assert "triggers_action_errors" not in clmc_service_response, "Unexpected error was returned for handlers specification"
        print("Alert spec sent successfully")

        print("Wait 10 seconds for Kapacitor stream/batch tasks to start working...")
        sleep(10)

        print("Sending test requests to nginx...")
        for i in range(20):
            response = get("http://{0}:{1}/".format(nginx_host, NGINX_PORT))
            assert response.status_code == 200

        print("Wait 20 seconds for Kapacitor to trigger alerts...")
        sleep(20)

        assert len(listdir(LOG_TEST_FOLDER_PATH)) == 4, "4 log files must have been created - one for each alert defined in the specification."