forked from gruntwork-io/terragrunt
-
Notifications
You must be signed in to change notification settings - Fork 2
/
graph_test.go
87 lines (72 loc) · 2.8 KB
/
graph_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package configstack
import (
"testing"
"github.com/coveooss/terragrunt/v2/tgerrors"
"github.com/stretchr/testify/assert"
)
func TestCheckForCycles(t *testing.T) {
t.Parallel()
////////////////////////////////////
// These modules have no dependencies
////////////////////////////////////
a := &TerraformModule{Path: "a"}
b := &TerraformModule{Path: "b"}
c := &TerraformModule{Path: "c"}
d := &TerraformModule{Path: "d"}
////////////////////////////////////
// These modules have dependencies, but no cycles
////////////////////////////////////
// e -> a
e := &TerraformModule{Path: "e", Dependencies: []*TerraformModule{a}}
// f -> a, b
f := &TerraformModule{Path: "f", Dependencies: []*TerraformModule{a, b}}
// g -> e -> a
g := &TerraformModule{Path: "g", Dependencies: []*TerraformModule{e}}
// h -> g -> e -> a
// | /
// --> f -> b
// |
// --> c
h := &TerraformModule{Path: "h", Dependencies: []*TerraformModule{g, f, c}}
////////////////////////////////////
// These modules have dependencies and cycles
////////////////////////////////////
// i -> i
i := &TerraformModule{Path: "i", Dependencies: []*TerraformModule{}}
i.Dependencies = append(i.Dependencies, i)
// j -> k -> j
j := &TerraformModule{Path: "j", Dependencies: []*TerraformModule{}}
k := &TerraformModule{Path: "k", Dependencies: []*TerraformModule{j}}
j.Dependencies = append(j.Dependencies, k)
// l -> m -> n -> o -> l
l := &TerraformModule{Path: "l", Dependencies: []*TerraformModule{}}
o := &TerraformModule{Path: "o", Dependencies: []*TerraformModule{l}}
n := &TerraformModule{Path: "n", Dependencies: []*TerraformModule{o}}
m := &TerraformModule{Path: "m", Dependencies: []*TerraformModule{n}}
l.Dependencies = append(l.Dependencies, m)
testCases := []struct {
modules []*TerraformModule
expected errDependencyCycle
}{
{[]*TerraformModule{}, nil},
{[]*TerraformModule{a}, nil},
{[]*TerraformModule{a, b, c, d}, nil},
{[]*TerraformModule{a, e}, nil},
{[]*TerraformModule{a, b, f}, nil},
{[]*TerraformModule{a, e, g}, nil},
{[]*TerraformModule{a, b, c, e, f, g, h}, nil},
{[]*TerraformModule{i}, errDependencyCycle([]string{"i", "i"})},
{[]*TerraformModule{j, k}, errDependencyCycle([]string{"j", "k", "j"})},
{[]*TerraformModule{l, o, n, m}, errDependencyCycle([]string{"l", "m", "n", "o", "l"})},
{[]*TerraformModule{a, l, b, o, n, f, m, h}, errDependencyCycle([]string{"l", "m", "n", "o", "l"})},
}
for _, testCase := range testCases {
actual := checkForCycles(testCase.modules)
if testCase.expected == nil {
assert.Nil(t, actual)
} else if assert.NotNil(t, actual, "For modules %v", testCase.modules) {
actualErr := tgerrors.Unwrap(actual).(errDependencyCycle)
assert.Equal(t, []string(testCase.expected), []string(actualErr), "For modules %v", testCase.modules)
}
}
}