From 0e6bb2badbdc146f1a805e44857742968e9862e7 Mon Sep 17 00:00:00 2001 From: Antoine Gelloz Date: Mon, 7 Oct 2024 14:22:02 +0200 Subject: [PATCH] refact(GenerateDAG): split the algorithm in two parts, buildGraph and computeHashes. The first buildGraph needs to be unit tested, because we found some issues with its logic. --- pkg/dib/generate_dag.go | 35 +++++++++++++++------------ pkg/dib/generate_dag_internal_test.go | 9 +++++++ 2 files changed, 29 insertions(+), 15 deletions(-) create mode 100644 pkg/dib/generate_dag_internal_test.go diff --git a/pkg/dib/generate_dag.go b/pkg/dib/generate_dag.go index 49bf7585..c2644ca3 100644 --- a/pkg/dib/generate_dag.go +++ b/pkg/dib/generate_dag.go @@ -28,6 +28,15 @@ const ( // GenerateDAG discovers and parses all Dockerfiles at a given path, // and generates the DAG representing the relationships between images. func GenerateDAG(buildPath, registryPrefix, customHashListPath string, buildArgs map[string]string) (*dag.DAG, error) { + graph, err := buildGraph(buildPath, registryPrefix) + if err != nil { + return nil, err + } + + return computeHashes(graph, customHashListPath, buildArgs) +} + +func buildGraph(buildPath, registryPrefix string) (*dag.DAG, error) { var allFiles []string cache := make(map[string]*dag.Node) allParents := make(map[string][]dockerfile.ImageRef) @@ -121,19 +130,6 @@ func GenerateDAG(buildPath, registryPrefix, customHashListPath string, buildArgs } } - if err := generateHashes(graph, allFiles, customHashListPath, buildArgs); err != nil { - return nil, err - } - - return graph, nil -} - -func generateHashes(graph *dag.DAG, allFiles []string, customHashListPath string, buildArgs map[string]string) error { - customHumanizedHashList, err := LoadCustomHashList(customHashListPath) - if err != nil { - return fmt.Errorf("could not load custom humanized hash list: %w", err) - } - fileBelongsTo := map[string]*dag.Node{} for _, file := range allFiles { fileBelongsTo[file] = nil @@ -173,6 +169,15 @@ func generateHashes(graph *dag.DAG, allFiles []string, customHashListPath string } }) + return graph, nil +} + +func computeHashes(graph *dag.DAG, customHashListPath string, buildArgs map[string]string) (*dag.DAG, error) { + customHumanizedHashList, err := LoadCustomHashList(customHashListPath) + if err != nil { + return nil, fmt.Errorf("could not load custom humanized hash list: %w", err) + } + for { needRepass := false err := graph.WalkErr(func(node *dag.Node) error { @@ -221,10 +226,10 @@ func generateHashes(graph *dag.DAG, allFiles []string, customHashListPath string return nil }) if err != nil { - return err + return nil, err } if !needRepass { - return nil + return graph, nil } } } diff --git a/pkg/dib/generate_dag_internal_test.go b/pkg/dib/generate_dag_internal_test.go new file mode 100644 index 00000000..205d8052 --- /dev/null +++ b/pkg/dib/generate_dag_internal_test.go @@ -0,0 +1,9 @@ +package dib + +import "testing" + +func Test_buildGraph(t *testing.T) { + t.Parallel() + + // Implement me. +}