예제 #1
0
    def test_should_run_build_for_only_unique_builds(self, persist_mock,
                                                     read_mock, osutils_mock):
        build_function_mock = Mock()

        # create 3 function resources where 2 of them would have same codeuri, runtime and metadata
        function1_1 = generate_function("function1_1")
        function1_2 = generate_function("function1_2")
        function2 = generate_function("function2", runtime="different_runtime")
        resources_to_build_collector = ResourcesToBuildCollector()
        resources_to_build_collector.add_functions(
            [function1_1, function1_2, function2])

        build_dir = "builddir"

        # instantiate the builder and run build method
        builder = ApplicationBuilder(resources_to_build_collector, "builddir",
                                     "basedir", "cachedir")
        builder._build_function = build_function_mock
        build_function_mock.side_effect = [
            os.path.join(build_dir, function1_1.name),
            os.path.join(build_dir, function1_2.name),
            os.path.join(build_dir, function1_2.name),
        ]

        result = builder.build()

        # result should contain all 3 functions as expected
        self.assertEqual(
            result,
            {
                function1_1.name: os.path.join(build_dir, function1_1.name),
                function1_2.name: os.path.join(build_dir, function1_2.name),
                function2.name: os.path.join(build_dir, function1_2.name),
            },
        )

        # actual build should only be called twice since only 2 of the functions have unique build
        build_function_mock.assert_has_calls(
            [
                call(
                    function1_1.name,
                    function1_1.codeuri,
                    ZIP,
                    function1_1.runtime,
                    function1_1.handler,
                    ANY,
                    function1_1.metadata,
                ),
                call(
                    function2.name,
                    function2.codeuri,
                    ZIP,
                    function2.runtime,
                    function2.handler,
                    ANY,
                    function2.metadata,
                ),
            ],
            any_order=True,
        )
예제 #2
0
    def _collect_single_buildable_layer(
            self, resource_identifier: str,
            resource_collector: ResourcesToBuildCollector) -> None:
        """
        Populate resource_collector with layer with provided identifier.

        Parameters
        ----------
        resource_collector

        Returns
        -------

        """
        layer = self.layer_provider.get(resource_identifier)
        if not layer:
            # No layer found
            return
        if layer and layer.build_method is None:
            LOG.error("Layer %s is missing BuildMethod Metadata.",
                      self._function_provider)
            raise MissingBuildMethodException(
                f"Build method missing in layer {resource_identifier}.")

        resource_collector.add_layer(layer)
예제 #3
0
 def setUp(self):
     self.layer1 = Mock()
     self.layer2 = Mock()
     self.container_manager = Mock()
     resources_to_build_collector = ResourcesToBuildCollector()
     resources_to_build_collector.add_layers([self.layer1, self.layer2])
     self.builder = ApplicationBuilder(resources_to_build_collector, "builddir", "basedir")
예제 #4
0
    def _collect_single_function_and_dependent_layers(
        self, resource_identifier: str, resource_collector: ResourcesToBuildCollector
    ) -> None:
        """
        Populate resource_collector with function with provided identifier and all layers that function need to be
        build in resource_collector
        Parameters
        ----------
        resource_collector: Collector that will be populated with resources.
        """
        function = self.function_provider.get(resource_identifier)
        if not function:
            # No function found
            return

        resource_collector.add_function(function)
        resource_collector.add_layers([l for l in function.layers if l.build_method is not None])
예제 #5
0
    def resources_to_build(self):
        """
        Function return resources that should be build by current build command. This function considers
        Lambda Functions and Layers with build method as buildable resources.
        Returns
        -------
        ResourcesToBuildCollector
        """
        result = ResourcesToBuildCollector()
        if self._resource_identifier:
            self._collect_single_function_and_dependent_layers(
                self._resource_identifier, result)
            self._collect_single_buildable_layer(self._resource_identifier,
                                                 result)

            if not result.functions and not result.layers:
                all_resources = [
                    f.name for f in self._function_provider.get_all()
                ]
                all_resources.extend(
                    [l.name for l in self._layer_provider.get_all()])

                available_resource_message = f"{self._resource_identifier} not found. Possible options in your " \
                                             f"template: {all_resources}"
                LOG.info(available_resource_message)
                raise ResourceNotFound(
                    f"Unable to find a function or layer with name '{self._resource_identifier}'"
                )
            return result
        result.add_functions(self._function_provider.get_all())
        result.add_layers([
            l for l in self._layer_provider.get_all()
            if l.build_method is not None
        ])
        return result
예제 #6
0
    def setUp(self):
        self.func1 = Mock()
        self.func2 = Mock()
        self.layer1 = Mock()
        self.layer2 = Mock()

        resources_to_build_collector = ResourcesToBuildCollector()
        resources_to_build_collector.add_functions([self.func1, self.func2])
        resources_to_build_collector.add_layers([self.layer1, self.layer2])
        self.builder = ApplicationBuilder(resources_to_build_collector, "builddir", "basedir")
예제 #7
0
    def setUp(self):
        self.func1 = Mock()
        self.func1.packagetype = ZIP
        self.func1.name = "function_name1"
        self.func2 = Mock()
        self.func2.packagetype = ZIP
        self.func2.name = "function_name2"
        self.imageFunc1 = Mock()
        self.imageFunc1.name = "function_name3"

        self.layer1 = Mock()
        self.layer2 = Mock()

        self.imageFunc1.packagetype = IMAGE
        self.layer1.build_method = "build_method"
        self.layer2.build_method = "build_method"

        resources_to_build_collector = ResourcesToBuildCollector()
        resources_to_build_collector.add_functions(
            [self.func1, self.func2, self.imageFunc1])
        resources_to_build_collector.add_layers([self.layer1, self.layer2])
        self.builder = ApplicationBuilder(resources_to_build_collector,
                                          "builddir", "basedir", "cachedir")