Skip to content

Commit

Permalink
Merge pull request #483 from radiofrance/refactor/generate-dag-tests
Browse files Browse the repository at this point in the history
refactor: GenerateDAG tests
  • Loading branch information
antoinegelloz authored Feb 19, 2024
2 parents 3369572 + e9fb779 commit 44705ad
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 223 deletions.
5 changes: 4 additions & 1 deletion cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,10 @@ func doBuild(opts dib.BuildOpts) error {
logger.Infof("Building images in directory \"%s\"", buildPath)

logger.Debugf("Generate DAG")
graph := dib.GenerateDAG(buildPath, opts.RegistryURL, opts.HashListFilePath)
graph, err := dib.GenerateDAG(buildPath, opts.RegistryURL, opts.HashListFilePath)
if err != nil {
return fmt.Errorf("cannot generate DAG: %w", err)
}
logger.Debugf("Generate DAG -- Done")

dibBuilder := dib.Builder{
Expand Down
6 changes: 5 additions & 1 deletion cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ func doList(opts dib.ListOpts) error {
}

buildPath := path.Join(workingDir, opts.BuildPath)
graph := dib.GenerateDAG(buildPath, opts.RegistryURL, opts.HashListFilePath)
graph, err := dib.GenerateDAG(buildPath, opts.RegistryURL, opts.HashListFilePath)
if err != nil {
return fmt.Errorf("cannot generate DAG: %w", err)
}

return dib.GenerateList(graph, formatOpts)
}
14 changes: 7 additions & 7 deletions pkg/dib/generate_dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (

// GenerateDAG discovers and parses all Dockerfiles at a given path,
// and generates the DAG representing the relationships between images.
func GenerateDAG(buildPath string, registryPrefix string, customHashListPath string) *dag.DAG {
func GenerateDAG(buildPath string, registryPrefix string, customHashListPath string) (*dag.DAG, error) {
var allFiles []string
cache := make(map[string]*dag.Node)
allParents := make(map[string][]dockerfile.ImageRef)
Expand Down Expand Up @@ -80,7 +80,7 @@ func GenerateDAG(buildPath string, registryPrefix string, customHashListPath str
return nil
})
if err != nil {
logger.Fatalf("%+v", err)
return nil, err
}

// Fill parents for each image, for simplicity of use in other functions
Expand All @@ -107,19 +107,19 @@ func GenerateDAG(buildPath string, registryPrefix string, customHashListPath str
}
}

DAG := &dag.DAG{}
graph := &dag.DAG{}
// If an image has no parents in the DAG, we consider it a root image
for name, img := range cache {
if len(img.Parents()) == 0 {
DAG.AddNode(cache[name])
graph.AddNode(cache[name])
}
}

if err := generateHashes(DAG, allFiles, customHashListPath); err != nil {
logger.Fatalf("%+v", err)
if err := generateHashes(graph, allFiles, customHashListPath); err != nil {
return nil, err
}

return DAG
return graph, nil
}

func generateHashes(graph *dag.DAG, allFiles []string, customHashListPath string) error {
Expand Down
Loading

0 comments on commit 44705ad

Please sign in to comment.