def are_tests_registered_in_conf_json_file_or_yml_file(self, test_playbooks: list) -> bool: """ If the file is a test playbook: Validates it is registered in conf.json file If the file is an integration: Validating it is registered in conf.json file or that the yml file has 'No tests' under 'tests' key Args: test_playbooks: The yml file's list of test playbooks Returns: True if all test playbooks are configured in conf.json """ no_tests_explicitly = any(test for test in test_playbooks if 'no test' in test.lower()) if no_tests_explicitly: return True conf_json_tests = self._load_conf_file()['tests'] file_type = self.structure_validator.scheme_name if not isinstance(file_type, str): file_type = file_type.value # type: ignore content_item_id = _get_file_id(file_type, self.current_file) # Test playbook case if file_type == 'testplaybook': is_configured_test = any(test_config for test_config in conf_json_tests if is_test_config_match(test_config, test_playbook_id=content_item_id)) if not is_configured_test: missing_test_playbook_configurations = json.dumps({'playbookID': content_item_id}, indent=4) missing_integration_configurations = json.dumps( {'integrations': '<integration ID>', 'playbookID': content_item_id}, indent=4) error_message, error_code = Errors.test_playbook_not_configured(content_item_id, missing_test_playbook_configurations, missing_integration_configurations) if self.handle_error(error_message, error_code, file_path=self.file_path): return False # Integration case elif file_type == 'integration': is_configured_test = any( test_config for test_config in conf_json_tests if is_test_config_match(test_config, integration_id=content_item_id)) if not is_configured_test: missing_test_playbook_configurations = json.dumps( {'integrations': content_item_id, 'playbookID': '<TestPlaybook ID>'}, indent=4) no_tests_key = yaml.dump({'tests': ['No tests']}) error_message, error_code = Errors.integration_not_registered(self.file_path, missing_test_playbook_configurations, no_tests_key) if self.handle_error(error_message, error_code, file_path=self.file_path): return False return True
def test_find_test_match(test_config, integration_id, test_playbook_id, expected, file_type): # type: (dict, str, str, bool, str) -> None """ Given - A test configuration from 'conf.json' file. test-playbook id and a content item id When - checking if the test configuration matches the content item and the test-playbook Then - Ensure the method 'find_test_match' return answer accordingly """ assert is_test_config_match(test_config, test_playbook_id, integration_id) == expected
def _verify_conf_json_modified(test_playbooks: List, yml_title: str, conf_json_path: str): """ Verifying all test playbooks are configured in conf.json file """ try: with open(conf_json_path) as data_file: conf_json_content = json.load(data_file) for test_playbook in test_playbooks: assert any( test_config for test_config in conf_json_content['tests'] if is_test_config_match(test_config, test_playbook_id=test_playbook, integration_id=yml_title, ) ) except Exception: raise
def are_tests_registered_in_conf_json_file_or_yml_file( self, test_playbooks: list) -> bool: """ If the file is a test playbook: Validates it is registered in conf.json file If the file is an integration: Validating it is registered in conf.json file or that the yml file has 'No tests' under 'tests' key Args: test_playbooks: The yml file's list of test playbooks Returns: True if all test playbooks are configured in conf.json """ no_tests_explicitly = any(test for test in test_playbooks if 'no test' in test.lower()) if no_tests_explicitly: return True conf_json_tests = self._load_conf_file()['tests'] content_item_id = _get_file_id(self.structure_validator.scheme_name, self.current_file) file_type = self.structure_validator.scheme_name # Test playbook case if 'TestPlaybooks' in self.file_path and file_type == 'playbook': is_configured_test = any( test_config for test_config in conf_json_tests if is_test_config_match(test_config, test_playbook_id=content_item_id)) if not is_configured_test: missing_test_playbook_configurations = json.dumps( {'playbookID': content_item_id}, indent=4) missing_integration_configurations = json.dumps( { 'integrations': '<integration ID>', 'playbookID': content_item_id }, indent=4) error_message = \ f'The TestPlaybook {content_item_id} is not registered in {self.CONF_PATH} file.\n' \ f'Please add\n{missing_test_playbook_configurations}\n' \ f'or if this test playbook is for an integration\n{missing_integration_configurations}\n' \ f'to {self.CONF_PATH} path under \'tests\' key.' print_error(error_message) return False # Integration case elif file_type == 'integration': is_configured_test = any( test_config for test_config in conf_json_tests if is_test_config_match(test_config, integration_id=content_item_id)) if not is_configured_test: missing_test_playbook_configurations = json.dumps( { 'integrations': content_item_id, 'playbookID': '<TestPlaybook ID>' }, indent=4) no_tests_key = yaml.dump({'tests': ['No tests']}) error_message = \ f'The following integration is not registered in {self.CONF_PATH} file.\n' \ f'Please add\n{missing_test_playbook_configurations}\nto {self.CONF_PATH} ' \ f'path under \'tests\' key.\n' \ f'If you don\'t want to add a test playbook for this integration, ' \ f'please add \n{no_tests_key}to the ' \ f'file {self.file_path} or run \'demisto-sdk format -p {self.file_path}\'' print_error(error_message) return False return True