def test_build_single_function_definition_image_functions_with_same_metadata(
            self, mock_copy_tree):
        given_build_function = Mock()
        built_image = Mock()
        given_build_function.return_value = built_image
        given_build_layer = Mock()
        given_build_dir = "build_dir"
        default_build_strategy = DefaultBuildStrategy(self.build_graph,
                                                      given_build_dir,
                                                      given_build_function,
                                                      given_build_layer)

        function1 = Mock()
        function1.name = "Function"
        function1.full_path = "Function"
        function1.packagetype = IMAGE
        function2 = Mock()
        function2.name = "Function2"
        function2.full_path = "Function2"
        function2.packagetype = IMAGE
        build_definition = FunctionBuildDefinition("3.7", "codeuri", IMAGE, {})
        # since they have the same metadata, they are put into the same build_definition.
        build_definition.functions = [function1, function2]

        result = default_build_strategy.build_single_function_definition(
            build_definition)
        # both of the function name should show up in results
        self.assertEqual(result, {
            "Function": built_image,
            "Function2": built_image
        })
    def test_build_layers_and_functions(self, mock_copy_tree):
        given_build_function = Mock()
        given_build_function.inlinecode = None
        given_build_layer = Mock()
        given_build_dir = "build_dir"
        default_build_strategy = DefaultBuildStrategy(self.build_graph,
                                                      given_build_dir,
                                                      given_build_function,
                                                      given_build_layer)

        default_build_strategy.build()

        # assert that build function has been called
        given_build_function.assert_has_calls([
            call(
                self.function_build_definition1.get_function_name(),
                self.function_build_definition1.codeuri,
                ZIP,
                self.function_build_definition1.runtime,
                self.function_build_definition1.get_handler_name(),
                self.function_build_definition1.get_build_dir(given_build_dir),
                self.function_build_definition1.metadata,
            ),
            call(
                self.function_build_definition2.get_function_name(),
                self.function_build_definition2.codeuri,
                ZIP,
                self.function_build_definition2.runtime,
                self.function_build_definition2.get_handler_name(),
                self.function_build_definition2.get_build_dir(given_build_dir),
                self.function_build_definition2.metadata,
            ),
        ])

        # assert that layer build function has been called
        given_build_layer.assert_has_calls([
            call(
                self.layer1.name,
                self.layer1.codeuri,
                self.layer1.build_method,
                self.layer1.compatible_runtimes,
                self.layer1.get_build_dir(given_build_dir),
            ),
            call(
                self.layer2.name,
                self.layer2.codeuri,
                self.layer2.build_method,
                self.layer2.compatible_runtimes,
                self.layer2.get_build_dir(given_build_dir),
            ),
        ])
        # previously we also assert artifact dir here.
        # since artifact dir is now determined in samcli/lib/providers/provider.py
        # we will not do assertion here

        # assert that function1_2 artifacts have been copied from already built function1_1
        mock_copy_tree.assert_called_with(
            self.function_build_definition1.get_build_dir(given_build_dir),
            self.function1_2.get_build_dir(given_build_dir),
        )
    def test_layer_build_should_fail_when_no_build_method_is_provided(self, mock_copy_tree, mock_path):
        given_layer = Mock()
        given_layer.build_method = None
        layer_build_definition = LayerBuildDefinition("layer1", "codeuri", "build_method", [])
        layer_build_definition.layer = given_layer

        build_graph = Mock(spec=BuildGraph)
        build_graph.get_layer_build_definitions.return_value = [layer_build_definition]
        build_graph.get_function_build_definitions.return_value = []
        default_build_strategy = DefaultBuildStrategy(build_graph, "build_dir", Mock(), Mock())

        self.assertRaises(MissingBuildMethodException, default_build_strategy.build)
Exemple #4
0
    def build(self):
        """
        Build the entire application

        Returns
        -------
        dict
            Returns the path to where each resource was built as a map of resource's LogicalId to the path string
        """
        build_graph = self._get_build_graph()
        build_strategy = DefaultBuildStrategy(build_graph, self._build_dir,
                                              self._build_function,
                                              self._build_layer)

        if self._parallel:
            if self._cached:
                build_strategy = ParallelBuildStrategy(
                    build_graph,
                    CachedBuildStrategy(build_graph, build_strategy,
                                        self._base_dir, self._build_dir,
                                        self._cache_dir,
                                        self._is_building_specific_resource))
            else:
                build_strategy = ParallelBuildStrategy(build_graph,
                                                       build_strategy)
        elif self._cached:
            build_strategy = CachedBuildStrategy(
                build_graph, build_strategy, self._base_dir, self._build_dir,
                self._cache_dir, self._is_building_specific_resource)

        return build_strategy.build()
 def test_build_call(self, mock_layer_build, mock_function_build, mock_rmtree, mock_copy_tree, mock_path):
     given_build_function = Mock()
     given_build_layer = Mock()
     given_build_dir = "build_dir"
     default_build_strategy = DefaultBuildStrategy(
         self.build_graph, given_build_dir, given_build_function, given_build_layer
     )
     cache_build_strategy = CachedBuildStrategy(
         self.build_graph, default_build_strategy, "base_dir", given_build_dir, "cache_dir", True
     )
     cache_build_strategy.build()
     mock_function_build.assert_called()
     mock_layer_build.assert_called()
Exemple #6
0
    def test_build_layers_and_functions(self, mock_copy_tree, mock_path):
        given_build_function = Mock()
        given_build_function.inlinecode = None
        given_build_layer = Mock()
        given_build_dir = "build_dir"
        default_build_strategy = DefaultBuildStrategy(self.build_graph,
                                                      given_build_dir,
                                                      given_build_function,
                                                      given_build_layer)

        default_build_strategy.build()

        # assert that build function has been called
        given_build_function.assert_has_calls([
            call(
                self.function_build_definition1.get_function_name(),
                self.function_build_definition1.codeuri,
                ZIP,
                self.function_build_definition1.runtime,
                self.function_build_definition1.get_handler_name(),
                ANY,
                self.function_build_definition1.metadata,
            ),
            call(
                self.function_build_definition2.get_function_name(),
                self.function_build_definition2.codeuri,
                ZIP,
                self.function_build_definition2.runtime,
                self.function_build_definition2.get_handler_name(),
                ANY,
                self.function_build_definition2.metadata,
            ),
        ])

        # assert that layer build function has been called
        given_build_layer.assert_has_calls([
            call(
                self.layer_build_definition1.layer.name,
                self.layer_build_definition1.layer.codeuri,
                self.layer_build_definition1.layer.build_method,
                self.layer_build_definition1.layer.compatible_runtimes,
            ),
            call(
                self.layer_build_definition2.layer.name,
                self.layer_build_definition2.layer.codeuri,
                self.layer_build_definition2.layer.build_method,
                self.layer_build_definition2.layer.compatible_runtimes,
            ),
        ])

        # assert that mock path has been called
        mock_path.assert_has_calls(
            [
                call(given_build_dir,
                     self.function_build_definition1.get_function_name()),
                call(given_build_dir,
                     self.function_build_definition2.get_function_name()),
            ],
            any_order=True,
        )

        # assert that function1_2 artifacts have been copied from already built function1_1
        mock_copy_tree.assert_called_with(
            str(
                mock_path(
                    given_build_dir,
                    self.function_build_definition1.get_function_name())),
            str(mock_path(given_build_dir, self.function1_2.name)),
        )