def test_config(self): base_path = "./" if os.getcwd().endswith("/tests") else "./tests/" base_path = Path(base_path + "data/").resolve() # ingest config with args: config = Config( project_config_path=base_path / "standalone_template_no_metadata/test.template.yaml", project_root=base_path / "standalone_template_no_metadata", create_clients=False, args={ "template_file": "test.template.yaml", "regions": "us-east-1", "parameter_input": "params.json", "no_cleanup": True, }, ) self.assertEqual(config.tests["default"].template_file.name, "test.template.yaml") self.assertEqual(config.no_cleanup, True) for config_path, project_root in [ [None, base_path / "create_fail"], [ base_path / "delete_fail/ci/taskcat.yml", base_path / "delete_fail" ], [ base_path / "lambda_build_with_submodules/.taskcat.yml", base_path / "lambda_build_with_submodules", ], [base_path / "lint-error/.taskcat.yml", base_path / "lint-error"], [ base_path / "lint-warning/.taskcat.yml", base_path / "lint-warning" ], [ base_path / "nested-fail/ci/taskcat.yml", base_path / "nested-fail" ], [ base_path / "regional_client_and_bucket/ci/taskcat.yml", base_path / "regional_client_and_bucket", ], [ base_path / "standalone_template_no_metadata/test.template.yaml", base_path / "standalone_template_no_metadata", ], ]: print(f"testing {config_path} {project_root}") # noqa: T001 Config( project_config_path=config_path, project_root=project_root, create_clients=False, )
def run(self, input_file, project_root="./"): """tests whether CloudFormation templates are able to successfully launch :param input_file: path to either a taskat project config file or a CloudFormation template :param project_root: root path of the project relative to input_file """ config = Config(input_file, project_root=project_root) # pylint: disable=unused-variable
def test_passed(self): cwd = os.getcwd() try: lint = Lint( config=Config( project_config_path=str( build_test_case("/tmp/lint_test_output/", test_cases[0]) ), project_root="../", create_clients=False, ) ) self.assertEqual(lint.passed, True) lint_key = [t for t in lint.lints[0]][0] result_key = [t for t in lint.lints[0][lint_key]["results"]][0] test = lint.lints[0][lint_key]["results"][result_key] rule = mock.Mock(return_val="[E0001] some error") rule.rule.id = "E0001" rule.linenumber = 123 rule.rule.shortdesc = "short error" rule.message = "some error" test.append(rule) lint.strict = True self.assertEqual(lint.passed, False) finally: shutil.rmtree("/tmp/lint_test_output/") os.chdir(cwd) pass
def __init__(self, input_file: str, project_root: str = "./", strict: bool = False): """ :param input_file: path to project config or CloudFormation template :param project_root: base path for project :param strict: fail on lint warnings as well as errors """ config = Config( project_config_path=input_file, project_root=project_root, create_clients=False, ) lint = TaskCatLint(config, strict) errors = lint.lints[1] lint.output_results() if errors or not lint.passed: raise TaskCatException("Lint failed with errors")
def test_lint(self): cwd = os.getcwd() base_path = "/tmp/lint_test/" mkdir(base_path) try: for test_case in test_cases: config = Config( project_config_path=str(build_test_case(base_path, test_case)), project_root="../", create_clients=False, ) lint = Lint(config=config) self.assertEqual( test_case["expected_lints"], flatten_rule(lint.lints[0]) ) finally: shutil.rmtree(base_path) os.chdir(cwd) pass
def test_nested_submodules(self): tmp = Path(mkdtemp()) test_proj = ( Path(__file__).parent / "./data/lambda_build_with_submodules" ).resolve() copytree(test_proj, tmp / "test") c = Config( project_config_path=str(tmp / "test" / ".taskcat.yml"), project_root=str((tmp / "test").resolve()), create_clients=False, ) LambdaBuild(c) path = tmp / "test" zip_suffix = Path("functions") / "packages" / "TestFunc" / "lambda.zip" self.assertEqual((path / "functions" / "packages").is_dir(), True) self.assertEqual((path / zip_suffix).is_file(), True) path = path / "submodules" / "SomeSub" self.assertEqual((path / "functions" / "packages").is_dir(), True) self.assertEqual((path / zip_suffix).is_file(), True) path = path / "submodules" / "DeepSub" self.assertEqual((path / "functions" / "packages").is_dir(), True) self.assertEqual((path / zip_suffix).is_file(), True)
def from_dict( cls: Type[T], input_config: dict, project_root: str = "./", regions: str = "ALL", enable_sig_v2: bool = False, ) -> T: """Creates a Test from a Taskcat configuration in dictionary form. Args: input_config (dict): A Taskcat configuration in the form of a dict. project_root (str, optional): The path to the directory with your template and config file. Defaults to "./". regions (str, optional): A comma separated list of regions to test in. Defaults to "ALL". enable_sig_v2 (bool, optional): Enable legacy sigv2 requests for auto-created buckets. Defaults to False. Returns: T: Returns a Test instance. """ # noqa: B950 project_root_path: Path = Path(project_root).expanduser().resolve() # pylint: disable=too-many-arguments args = _build_args(enable_sig_v2, regions, GLOBAL_ARGS.profile) sources = [ { "source": "Manual", "config": input_config }, { "source": "CliArgument", "config": args }, ] config = Config(uid=uuid.uuid4(), project_root=project_root_path, sources=sources) return cls(config)
def __init__( self, project_root: str = "./", source_folder: str = "functions/source", zip_folder: str = "functions/packages", config_file: str = ".taskcat.yml", ): """ :param project_root: base path for project :param source_folder: folder containing the lambda source files, relative to the project_root :param zip_folder: folder to output zip files, relative to the project root :param config_file: path to taskcat project config file """ config = Config( project_config_path=config_file, project_root=project_root, create_clients=False, ) config.lambda_source_path = (Path(config.project_root) / source_folder).resolve() config.lambda_zip_path = (Path(config.project_root) / zip_folder).resolve() LambdaBuild(config)
def test_output_results(self, mock_log_error, mock_log_warning, mock_log_info): cwd = os.getcwd() try: lint = Lint( config=Config( project_config_path=str( build_test_case("/tmp/lint_test_output/", test_cases[0]) ), project_root="../", create_clients=False, ) ) lint.output_results() self.assertTrue( mock_log_info.call_args[0][0].startswith( "Lint passed for test test1 on template " ) ) self.assertEqual(mock_log_error.called, False) self.assertEqual(mock_log_warning.called, False) mock_log_info.reset_mock() lint_key = [t for t in lint.lints[0]][0] result_key = [t for t in lint.lints[0][lint_key]["results"]][0] test = lint.lints[0][lint_key]["results"][result_key] rule = mock.Mock(return_val="[W0001] some warning") rule.rule.id = "W0001" rule.linenumber = 123 rule.rule.shortdesc = "short warning" rule.message = "some warning" test.append(rule) lint.output_results() self.assertTrue( mock_log_warning.call_args_list[0][0][0].startswith( "Lint detected issues for test test1 on template " ) ) mock_log_warning.assert_has_calls( [mock.call(" line 123 [0001] [short warning] some warning")] ) self.assertEqual(mock_log_info.called, False) self.assertEqual(mock_log_error.called, False) mock_log_warning.reset_mock() test.pop(0) rule = mock.Mock(return_val="[E0001] some error") rule.rule.id = "E0001" rule.linenumber = 123 rule.rule.shortdesc = "short error" rule.message = "some error" test.append(rule) lint.output_results() self.assertTrue( mock_log_warning.call_args[0][0].startswith( "Lint detected issues for test test1 on template " ) ) mock_log_error.assert_called_once_with( " line 123 [0001] [short error] some error" ) self.assertEqual(mock_log_info.called, False) finally: shutil.rmtree("/tmp/lint_test_output/") os.chdir(cwd) pass