def test_functions_should_be_added_existing_build_graph(self):
        with osutils.mkdir_temp() as temp_base_dir:
            build_dir = Path(temp_base_dir, ".aws-sam", "build")
            build_dir.mkdir(parents=True)

            build_graph_path = Path(build_dir.parent, "build.toml")
            build_graph_path.write_text(TestBuildGraph.BUILD_GRAPH_CONTENTS)

            build_graph = BuildGraph(str(build_dir))

            build_definition1 = FunctionBuildDefinition(
                TestBuildGraph.RUNTIME, TestBuildGraph.CODEURI,
                TestBuildGraph.METADATA, TestBuildGraph.SOURCE_MD5)
            function1 = generate_function(runtime=TestBuildGraph.RUNTIME,
                                          codeuri=TestBuildGraph.CODEURI,
                                          metadata=TestBuildGraph.METADATA)
            build_graph.put_function_build_definition(build_definition1,
                                                      function1)

            self.assertTrue(len(build_graph.get_function_build_definitions()),
                            1)
            for build_definition in build_graph.get_function_build_definitions(
            ):
                self.assertTrue(len(build_definition.functions), 1)
                self.assertTrue(build_definition.functions[0], function1)
                self.assertEqual(build_definition.uuid, TestBuildGraph.UUID)

            build_definition2 = FunctionBuildDefinition(
                "another_runtime", "another_codeuri", None,
                "another_source_md5")
            function2 = generate_function(name="another_function")
            build_graph.put_function_build_definition(build_definition2,
                                                      function2)
            self.assertTrue(len(build_graph.get_function_build_definitions()),
                            2)
Beispiel #2
0
    def test_should_instantiate_first_time_and_update(self):
        with osutils.mkdir_temp() as temp_base_dir:
            build_dir = Path(temp_base_dir, ".aws-sam", "build")
            build_dir.mkdir(parents=True)

            # create a build graph and persist it
            build_graph1 = BuildGraph(str(build_dir))
            build_definition1 = FunctionBuildDefinition(
                TestBuildGraph.RUNTIME,
                TestBuildGraph.CODEURI,
                TestBuildGraph.ZIP,
                TestBuildGraph.METADATA,
                TestBuildGraph.SOURCE_MD5,
            )
            function1 = generate_function(runtime=TestBuildGraph.RUNTIME,
                                          codeuri=TestBuildGraph.CODEURI,
                                          metadata=TestBuildGraph.METADATA)
            build_graph1.put_function_build_definition(build_definition1,
                                                       function1)
            build_graph1.clean_redundant_definitions_and_update(True)

            # read previously persisted graph and compare
            build_graph2 = BuildGraph(str(build_dir))
            self.assertEqual(
                len(build_graph1.get_function_build_definitions()),
                len(build_graph2.get_function_build_definitions()))
            self.assertEqual(
                list(build_graph1.get_function_build_definitions())[0],
                list(build_graph2.get_function_build_definitions())[0],
            )
Beispiel #3
0
    def test_should_instantiate_first_time(self):
        with osutils.mkdir_temp() as temp_base_dir:
            build_dir = Path(temp_base_dir, ".aws-sam", "build")
            build_dir.mkdir(parents=True)
            build_graph1 = BuildGraph(str(build_dir.resolve()))
            build_graph1.clean_redundant_definitions_and_update(True)

            build_graph2 = BuildGraph(str(build_dir.resolve()))

            self.assertEqual(build_graph1.get_function_build_definitions(),
                             build_graph2.get_function_build_definitions())
Beispiel #4
0
    def test_if_cached_invalid_with_no_cached_folder(self, build_layer_mock,
                                                     build_function_mock,
                                                     copytree_mock):
        with osutils.mkdir_temp() as temp_base_dir:
            build_dir = Path(temp_base_dir, ".aws-sam", "build")
            build_dir.mkdir(parents=True)
            cache_dir = Path(temp_base_dir, ".aws-sam", "cache")
            cache_dir.mkdir(parents=True)

            build_function_mock.return_value = {
                "HelloWorldPython": "artifact1",
                "HelloWorldPython2": "artifact2"
            }
            build_layer_mock.return_value = {"SumLayer": "artifact3"}

            build_graph_path = Path(build_dir.parent, "build.toml")
            build_graph_path.write_text(
                CachedBuildStrategyTest.BUILD_GRAPH_CONTENTS)
            build_graph = BuildGraph(str(build_dir))
            cached_build_strategy = CachedBuildStrategy(
                build_graph, DefaultBuildStrategy, temp_base_dir, build_dir,
                cache_dir, True)
            cached_build_strategy.build_single_function_definition(
                build_graph.get_function_build_definitions()[0])
            cached_build_strategy.build_single_layer_definition(
                build_graph.get_layer_build_definitions()[0])
            build_function_mock.assert_called_once()
            build_layer_mock.assert_called_once()
            self.assertEqual(copytree_mock.call_count, 2)
    def test_if_cached_valid_when_build_single_function_definition(
            self, dir_checksum_mock, exists_mock, copytree_mock):
        pass
        with osutils.mkdir_temp() as temp_base_dir:
            build_dir = Path(temp_base_dir, ".aws-sam", "build")
            build_dir.mkdir(parents=True)
            cache_dir = Path(temp_base_dir, ".aws-sam", "cache")
            cache_dir.mkdir(parents=True)

            exists_mock.return_value = True
            dir_checksum_mock.return_value = CachedBuildStrategyTest.SOURCE_MD5

            build_graph_path = Path(build_dir.parent, "build.toml")
            build_graph_path.write_text(
                CachedBuildStrategyTest.BUILD_GRAPH_CONTENTS)
            build_graph = BuildGraph(str(build_dir))
            cached_build_strategy = CachedBuildStrategy(
                build_graph, DefaultBuildStrategy, temp_base_dir, build_dir,
                cache_dir, True)
            func1 = Mock()
            func1.name = "func1_name"
            func2 = Mock()
            func2.name = "func2_name"
            build_definition = build_graph.get_function_build_definitions()[0]
            layer_definition = build_graph.get_layer_build_definitions()[0]
            build_graph.put_function_build_definition(build_definition, func1)
            build_graph.put_function_build_definition(build_definition, func2)
            layer = Mock()
            layer.name = "layer_name"
            build_graph.put_layer_build_definition(layer_definition, layer)
            cached_build_strategy.build_single_function_definition(
                build_definition)
            cached_build_strategy.build_single_layer_definition(
                layer_definition)
            self.assertEqual(copytree_mock.call_count, 3)
Beispiel #6
0
    def _build_functions(self, build_graph: BuildGraph) -> Dict[str, str]:
        """
        Iterates through build graph and runs each unique build and copies outcome to the corresponding function folder
        """
        function_build_results = {}
        for build_definition in build_graph.get_function_build_definitions():
            function_build_results.update(self.build_single_function_definition(build_definition))

        return function_build_results
    def test_should_read_existing_build_graph(self):
        with osutils.mkdir_temp() as temp_base_dir:
            build_dir = Path(temp_base_dir, ".aws-sam", "build")
            build_dir.mkdir(parents=True)

            build_graph_path = Path(build_dir.parent, "build.toml")
            build_graph_path.write_text(TestBuildGraph.BUILD_GRAPH_CONTENTS)

            build_graph = BuildGraph(str(build_dir))
            for build_definition in build_graph.get_function_build_definitions(
            ):
                self.assertEqual(build_definition.codeuri,
                                 TestBuildGraph.CODEURI)
                self.assertEqual(build_definition.runtime,
                                 TestBuildGraph.RUNTIME)
                self.assertEqual(build_definition.metadata,
                                 TestBuildGraph.METADATA)
                self.assertEqual(build_definition.source_md5,
                                 TestBuildGraph.SOURCE_MD5)