Skip to content

Commit

Permalink
return err on cyclical dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff-mccoy committed Feb 27, 2023
1 parent 8b48dbd commit 2a612f9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/extensions/bigbang/bigbang.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ func Run(tmpPaths types.ComponentPaths, c types.ZarfComponent) (types.ZarfCompon
c.Repos = append(c.Repos, urls...)

// Generate a list of HelmReleases that need to be deployed in order.
helmReleases := utils.SortDependencies(hrDependencies)
helmReleases, err := utils.SortDependencies(hrDependencies)
if err != nil {
return c, fmt.Errorf("unable to sort Big Bang HelmReleases: %w", err)
}

tenMinsInSecs := 10 * 60

Expand Down
8 changes: 5 additions & 3 deletions src/pkg/utils/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// Package utils provides generic helper functions.
package utils

import "fmt"

// DependsOn is a struct that represents a node in a list of dependencies.
type DependsOn struct {
Name string
Expand All @@ -20,7 +22,7 @@ type DependsOn struct {
//
// Note sort order is dependent on the slice order of the input data for
// nodes with the same in-degree (i.e. the same number of dependencies).
func SortDependencies(data []DependsOn) []string {
func SortDependencies(data []DependsOn) ([]string, error) {
// Initialize the in-degree and out-degree maps.
inDegree := make(map[string]int)
outDegree := make(map[string][]string)
Expand Down Expand Up @@ -70,10 +72,10 @@ func SortDependencies(data []DependsOn) []string {
// Return an empty result list to indicate this.
for _, degree := range inDegree {
if degree > 0 {
return []string{}
return result, fmt.Errorf("dependency cycle detected")
}
}

// Return the result list.
return result
return result, nil
}
33 changes: 32 additions & 1 deletion src/pkg/utils/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@ package utils
import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
)

func TestSortDependencies(t *testing.T) {
tests := []struct {
name string
data []DependsOn // input data: a map of nodes to their dependencies
expected []string // expected output: a slice of nodes in order of their precedence
success bool // whether the test should succeed or fail
}{
{
name: "simple graph",
Expand All @@ -32,6 +35,7 @@ func TestSortDependencies(t *testing.T) {
},
// C has no dependencies, B depends on C, and A depends on both B and C
expected: []string{"C", "B", "A"},
success: true,
},
{
name: "complex graph",
Expand All @@ -57,6 +61,7 @@ func TestSortDependencies(t *testing.T) {
},
},
expected: []string{"E", "D", "C", "B", "A"},
success: true,
},
{
name: "graph with multiple roots",
Expand Down Expand Up @@ -84,6 +89,7 @@ func TestSortDependencies(t *testing.T) {
},
},
expected: []string{"F", "B", "A", "E", "C", "D"},
success: true,
},
{
name: "graph with multiple sinks",
Expand Down Expand Up @@ -115,12 +121,37 @@ func TestSortDependencies(t *testing.T) {
},
},
expected: []string{"F", "C", "E", "B", "G", "D", "A"},
success: true,
},
{
name: "graph with circular dependencies",
data: []DependsOn{
{
Name: "A",
Dependencies: []string{"B"},
},
{
Name: "B",
Dependencies: []string{"C"},
},
{
Name: "C",
Dependencies: []string{"A"},
},
},
expected: []string{},
success: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := SortDependencies(tt.data)
result, err := SortDependencies(tt.data)
if tt.success {
assert.NoError(t, err)
} else {
assert.Error(t, err)
}
if !reflect.DeepEqual(result, tt.expected) {
t.Errorf("expected %v but got %v", tt.expected, result)
}
Expand Down

0 comments on commit 2a612f9

Please sign in to comment.