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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
package resource
import (
"errors"
"strings"
"testing"
)
// mockApplier records the order in which Apply was called.
type mockApplier struct {
name string
err error
logs *[]string
}
func (m *mockApplier) Apply() error {
*m.logs = append(*m.logs, m.name)
return m.err
}
func TestApply(t *testing.T) {
tests := []struct {
name string
setup func(r *repository, logs *[]string)
wantOrder []string
wantError bool
errorString string
}{
{
name: "dependency chain",
setup: func(r *repository, logs *[]string) {
// A depends on B, B depends on C
r.registered["A"] = Resource{
Type: "T", Name: "A",
Apply: &mockApplier{name: "A", logs: logs},
dependsOn: map[string]struct{}{"B": {}},
}
r.registered["B"] = Resource{
Type: "T", Name: "B",
Apply: &mockApplier{name: "B", logs: logs},
dependsOn: map[string]struct{}{"C": {}},
}
r.registered["C"] = Resource{
Type: "T", Name: "C",
Apply: &mockApplier{name: "C", logs: logs},
}
},
wantOrder: []string{"C", "B", "A"},
},
{
name: "circular dependency",
setup: func(r *repository, logs *[]string) {
r.registered["A"] = Resource{
Type: "T", Name: "A",
Apply: &mockApplier{name: "A", logs: logs},
dependsOn: map[string]struct{}{"B": {}},
}
r.registered["B"] = Resource{
Type: "T", Name: "B",
Apply: &mockApplier{name: "B", logs: logs},
dependsOn: map[string]struct{}{"A": {}},
}
},
wantError: true,
errorString: "circular dependency",
},
{
name: "missing dependency",
setup: func(r *repository, logs *[]string) {
r.registered["A"] = Resource{
Type: "T", Name: "A",
Apply: &mockApplier{name: "A", logs: logs},
dependsOn: map[string]struct{}{"Missing": {}},
}
},
wantError: true,
errorString: "not registered",
},
{
name: "execution failure",
setup: func(r *repository, logs *[]string) {
r.registered["A"] = Resource{
Type: "T", Name: "A",
Apply: &mockApplier{name: "A", err: errors.New("fail A"), logs: logs},
}
},
wantError: true,
errorString: "failed to apply",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ResetRepository()
logs := make([]string, 0)
r := getRepository()
tt.setup(r, &logs)
err := Apply()
if (err != nil) != tt.wantError {
t.Errorf("Apply() error = %v, wantError %v", err, tt.wantError)
return
}
if tt.wantError && tt.errorString != "" {
if err == nil || !strings.Contains(err.Error(), tt.errorString) {
t.Errorf("Apply() error = %v, want error containing %q", err, tt.errorString)
}
}
if !tt.wantError && tt.wantOrder != nil {
if len(logs) != len(tt.wantOrder) {
t.Errorf("Apply() log length = %d, want %d", len(logs), len(tt.wantOrder))
return
}
for i := range logs {
if logs[i] != tt.wantOrder[i] {
t.Errorf("Apply() order = %v, want %v", logs, tt.wantOrder)
break
}
}
}
})
}
}
|