class TestDoCli(TestCase): @patch("samcli.commands.build.command.BuildContext") @patch("samcli.commands.build.command.ApplicationBuilder") @patch("samcli.commands.build.command.yaml_dump") @patch("samcli.commands.build.command.os") def test_must_succeed_build(self, os_mock, yaml_dump_mock, ApplicationBuilderMock, BuildContextMock): ctx_mock = Mock() BuildContextMock.return_value.__enter__ = Mock() BuildContextMock.return_value.__enter__.return_value = ctx_mock builder_mock = ApplicationBuilderMock.return_value = Mock() artifacts = builder_mock.build.return_value = "artifacts" modified_template = builder_mock.update_template.return_value = "modified template" dumped_yaml = yaml_dump_mock.return_value = "dumped yaml" m = mock_open() with patch("samcli.commands.build.command.open", m): do_cli("template", "base_dir", "build_dir", "clean", "use_container", "manifest_path", "docker_network", "skip_pull", "parameter_overrides") ApplicationBuilderMock.assert_called_once_with( ctx_mock.function_provider, ctx_mock.build_dir, ctx_mock.base_dir, manifest_path_override=ctx_mock.manifest_path_override, container_manager=ctx_mock.container_manager) builder_mock.build.assert_called_once() builder_mock.update_template.assert_called_once_with( ctx_mock.template_dict, ctx_mock.output_template_path, artifacts) yaml_dump_mock.assert_called_with(modified_template) m.assert_called_with(ctx_mock.output_template_path, 'w') m.return_value.write.assert_called_with(dumped_yaml) @parameterized.expand([ (UnsupportedRuntimeException(), ), (BuildError(), ), (UnsupportedBuilderLibraryVersionError(container_name="name", error_msg="msg"), ) ]) @patch("samcli.commands.build.command.BuildContext") @patch("samcli.commands.build.command.ApplicationBuilder") def test_must_catch_known_exceptions(self, exception, ApplicationBuilderMock, BuildContextMock): ctx_mock = Mock() BuildContextMock.return_value.__enter__ = Mock() BuildContextMock.return_value.__enter__.return_value = ctx_mock builder_mock = ApplicationBuilderMock.return_value = Mock() builder_mock.build.side_effect = exception with self.assertRaises(UserException) as ctx: do_cli("template", "base_dir", "build_dir", "clean", "use_container", "manifest_path", "docker_network", "skip_pull", "parameteroverrides") self.assertEquals(str(ctx.exception), str(exception))
def test_must_raise_on_method_not_found(self): msg = "invalid method" data = {"error": {"code": -32601, "message": msg}} with self.assertRaises(UnsupportedBuilderLibraryVersionError) as ctx: self.builder._parse_builder_response(json.dumps(data), self.image_name) expected = str(UnsupportedBuilderLibraryVersionError(self.image_name, msg)) self.assertEquals(str(ctx.exception), expected)
def test_must_raise_on_version_mismatch(self): msg = "invalid params" data = {"error": {"code": 505, "message": msg}} with self.assertRaises(UnsupportedBuilderLibraryVersionError) as ctx: self.builder._parse_builder_response(json.dumps(data), self.image_name) expected = str(UnsupportedBuilderLibraryVersionError(self.image_name, msg)) self.assertEqual(str(ctx.exception), expected)
class TestDoCli(TestCase): @patch("samcli.commands.build.build_context.BuildContext") @patch("samcli.lib.build.app_builder.ApplicationBuilder") @patch("samcli.commands._utils.template.move_template") @patch("samcli.commands.build.command.os") def test_must_succeed_build(self, os_mock, move_template_mock, ApplicationBuilderMock, BuildContextMock): ctx_mock = Mock() # create stack mocks root_stack = Mock() root_stack.is_root_stack = True root_stack.get_output_template_path = Mock( return_value="./build_dir/template.yaml") child_stack = Mock() child_stack.get_output_template_path = Mock( return_value="./build_dir/abcd/template.yaml") ctx_mock.stacks = [root_stack, child_stack] stack_output_template_path_by_stack_path = { root_stack.stack_path: "./build_dir/template.yaml", child_stack.stack_path: "./build_dir/abcd/template.yaml", } BuildContextMock.return_value.__enter__ = Mock() BuildContextMock.return_value.__enter__.return_value = ctx_mock builder_mock = ApplicationBuilderMock.return_value = Mock() artifacts = builder_mock.build.return_value = "artifacts" modified_template_root = "modified template 1" modified_template_child = "modified template 2" builder_mock.update_template.side_effect = [ modified_template_root, modified_template_child ] do_cli( "function_identifier", "template", "base_dir", "build_dir", "cache_dir", "clean", "use_container", "cached", "parallel", "manifest_path", "docker_network", "skip_pull", "parameter_overrides", "mode", (""), "container_env_var_file", (), ) ApplicationBuilderMock.assert_called_once_with( ctx_mock.resources_to_build, ctx_mock.build_dir, ctx_mock.base_dir, ctx_mock.cache_dir, ctx_mock.cached, ctx_mock.is_building_specific_resource, manifest_path_override=ctx_mock.manifest_path_override, container_manager=ctx_mock.container_manager, mode=ctx_mock.mode, parallel="parallel", container_env_var={}, container_env_var_file="container_env_var_file", build_images={}, ) builder_mock.build.assert_called_once() builder_mock.update_template.assert_has_calls( [ call( root_stack, artifacts, stack_output_template_path_by_stack_path, ) ], [ call( child_stack, artifacts, stack_output_template_path_by_stack_path, ) ], ) move_template_mock.assert_has_calls([ call( root_stack.location, stack_output_template_path_by_stack_path[ root_stack.stack_path], modified_template_root, ), call( child_stack.location, stack_output_template_path_by_stack_path[ child_stack.stack_path], modified_template_child, ), ]) @parameterized.expand([ (UnsupportedRuntimeException(), "UnsupportedRuntimeException"), (BuildInsideContainerError(), "BuildInsideContainerError"), (BuildError(wrapped_from=DeepWrap().__class__.__name__, msg="Test"), "DeepWrap"), (ContainerBuildNotSupported(), "ContainerBuildNotSupported"), ( UnsupportedBuilderLibraryVersionError(container_name="name", error_msg="msg"), "UnsupportedBuilderLibraryVersionError", ), ]) @patch("samcli.commands.build.build_context.BuildContext") @patch("samcli.lib.build.app_builder.ApplicationBuilder") def test_must_catch_known_exceptions(self, exception, wrapped_exception, ApplicationBuilderMock, BuildContextMock): ctx_mock = Mock() BuildContextMock.return_value.__enter__ = Mock() BuildContextMock.return_value.__enter__.return_value = ctx_mock builder_mock = ApplicationBuilderMock.return_value = Mock() builder_mock.build.side_effect = exception with self.assertRaises(UserException) as ctx: do_cli( "function_identifier", "template", "base_dir", "build_dir", "cache_dir", "clean", "use_container", "cached", "parallel", "manifest_path", "docker_network", "skip_pull", "parameteroverrides", "mode", (""), "container_env_var_file", (), ) self.assertEqual(str(ctx.exception), str(exception)) self.assertEqual(wrapped_exception, ctx.exception.wrapped_from) @patch("samcli.commands.build.build_context.BuildContext") @patch("samcli.lib.build.app_builder.ApplicationBuilder") def test_must_catch_function_not_found_exception(self, ApplicationBuilderMock, BuildContextMock): ctx_mock = Mock() BuildContextMock.return_value.__enter__ = Mock() BuildContextMock.return_value.__enter__.return_value = ctx_mock ApplicationBuilderMock.side_effect = FunctionNotFound( "Function Not Found") with self.assertRaises(UserException) as ctx: do_cli( "function_identifier", "template", "base_dir", "build_dir", "cache_dir", "clean", "use_container", "cached", "parallel", "manifest_path", "docker_network", "skip_pull", "parameteroverrides", "mode", (""), "container_env_var_file", (), ) self.assertEqual(str(ctx.exception), "Function Not Found")
class TestDoCli(TestCase): @patch("samcli.commands.build.command.BuildContext") @patch("samcli.commands.build.command.ApplicationBuilder") @patch("samcli.commands.build.command.move_template") @patch("samcli.commands.build.command.os") def test_must_succeed_build(self, os_mock, move_template_mock, ApplicationBuilderMock, BuildContextMock): ctx_mock = Mock() BuildContextMock.return_value.__enter__ = Mock() BuildContextMock.return_value.__enter__.return_value = ctx_mock builder_mock = ApplicationBuilderMock.return_value = Mock() artifacts = builder_mock.build.return_value = "artifacts" modified_template = builder_mock.update_template.return_value = "modified template" do_cli( "function_identifier", "template", "base_dir", "build_dir", "clean", "use_container", "manifest_path", "docker_network", "skip_pull", "parameter_overrides", "mode", ) ApplicationBuilderMock.assert_called_once_with( ctx_mock.functions_to_build, ctx_mock.build_dir, ctx_mock.base_dir, manifest_path_override=ctx_mock.manifest_path_override, container_manager=ctx_mock.container_manager, mode=ctx_mock.mode, ) builder_mock.build.assert_called_once() builder_mock.update_template.assert_called_once_with( ctx_mock.template_dict, ctx_mock.original_template_path, artifacts) move_template_mock.assert_called_once_with( ctx_mock.original_template_path, ctx_mock.output_template_path, modified_template) @parameterized.expand([ (UnsupportedRuntimeException(), ), (BuildError(), ), (UnsupportedBuilderLibraryVersionError(container_name="name", error_msg="msg"), ), ]) @patch("samcli.commands.build.command.BuildContext") @patch("samcli.commands.build.command.ApplicationBuilder") def test_must_catch_known_exceptions(self, exception, ApplicationBuilderMock, BuildContextMock): ctx_mock = Mock() BuildContextMock.return_value.__enter__ = Mock() BuildContextMock.return_value.__enter__.return_value = ctx_mock builder_mock = ApplicationBuilderMock.return_value = Mock() builder_mock.build.side_effect = exception with self.assertRaises(UserException) as ctx: do_cli( "function_identifier", "template", "base_dir", "build_dir", "clean", "use_container", "manifest_path", "docker_network", "skip_pull", "parameteroverrides", "mode", ) self.assertEqual(str(ctx.exception), str(exception)) @patch("samcli.commands.build.command.BuildContext") @patch("samcli.commands.build.command.ApplicationBuilder") def test_must_catch_function_not_found_exception(self, ApplicationBuilderMock, BuildContextMock): ctx_mock = Mock() BuildContextMock.return_value.__enter__ = Mock() BuildContextMock.return_value.__enter__.return_value = ctx_mock ApplicationBuilderMock.side_effect = FunctionNotFound( "Function Not Found") with self.assertRaises(UserException) as ctx: do_cli( "function_identifier", "template", "base_dir", "build_dir", "clean", "use_container", "manifest_path", "docker_network", "skip_pull", "parameteroverrides", "mode", ) self.assertEqual(str(ctx.exception), "Function Not Found")