#!/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 : 14-08-2018 // Created for Project : FLAME """ # Python standard libs from os import listdir from os.path import isfile, join from yaml import load # PIP installed libs import pytest from pyramid import testing from toscaparser.tosca_template import ToscaTemplate # CLMC-service imports from clmcservice.alertsapi.utilities import adjust_tosca_definitions_import from clmcservice.alertsapi.alerts_specification_schema import validate_clmc_alerts_specification from clmcservice.alertsapi.views import AlertsConfigurationAPI from clmcservice import ROOT_DIR class TestAlertsConfigurationAPI(object): """ A pytest-implementation test for the Alerts Configuration API endpoints. """ @pytest.fixture(autouse=True) def print_fixture(self): """ Fixture to adjust the printing format when running pytest with the "-s" flag - by default print messages mix up with pytest's output """ print() @pytest.fixture() def app_config(self): """ A fixture to implement setUp/tearDown functionality for all tests by initializing configuration structure for the web service """ self.registry = testing.setUp() yield testing.tearDown() def test_alerts_config_tosca_parsing(self): """ Tests that what we consider a valid/invalid alerts specification is successfully/unsuccessfully parsed by the TOSCA-parser. """ for path_suffix, valid_expected in (("valid", True), ("invalid", False)): test_data_path = join(ROOT_DIR, *["resources", "tosca", "test-data", "tosca-parser", path_suffix]) for test_file_path in listdir(test_data_path): alert_config_abs_path = join(test_data_path, test_file_path) if not isfile(alert_config_abs_path): continue # skip directories if not test_file_path.lower().endswith('.yaml'): continue # non-yaml files are not intended for being tested print(alert_config_abs_path, valid_expected) with open(alert_config_abs_path, 'r') as fh: yaml_content = load(fh) adjust_tosca_definitions_import(yaml_content) valid_real = True try: ToscaTemplate(yaml_dict_tpl=yaml_content) except Exception: valid_real = False assert valid_expected == valid_real, "TOSCA parser test failed for file: {0}".format(alert_config_abs_path) def test_alerts_config_clmc_validation(self): """ Tests the custom CLMC validation of the TOSCA alerts specification. """ for path_suffix, valid_expected in (("valid", True), ("invalid", False)): test_data_path = join(ROOT_DIR, *["resources", "tosca", "test-data", "clmc-validator", path_suffix]) for test_file_path in listdir(test_data_path): alert_config_abs_path = join(test_data_path, test_file_path) if not isfile(alert_config_abs_path): continue # skip directories if not test_file_path.lower().endswith('.yaml'): continue # non-yaml files are not intended for being tested print(alert_config_abs_path, valid_expected) with open(alert_config_abs_path, 'r') as fh: yaml_content = load(fh) adjust_tosca_definitions_import(yaml_content) # do not catch exceptions here since we are testing the clmc validator, the tosca parsing is tested in the previous test method alert_tosca_spec = ToscaTemplate(yaml_dict_tpl=yaml_content) valid_real, err = validate_clmc_alerts_specification(alert_tosca_spec.tpl, include_error=True) assert valid_expected == valid_real, "CLMC alerts specification validator test failed for file: {0}".format(alert_config_abs_path) def test_alerts_config_api_post(self, app_config): """ Tests the POST API endpoint of the alerts configuration API responsible for receiving alerts specifications. Unit test consists of: * Traverse all valid TOSCA Alerts Specifications in the src/service/clmcservice/resources/tosca/test-data/clmc-validator/valid * Sending a valid TOSCA Alert Specification to the view responsible for configuring Kapacitor * Check that Kapacitor alerts, topics and handlers are created with the correct identifier and arguments :param app_config: fixture for setUp/tearDown of the web service registry """ pass