forked from vmware-archive/atc
/
match_plan_matcher.go
144 lines (112 loc) · 3.14 KB
/
match_plan_matcher.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package testhelpers
import (
"encoding/json"
"fmt"
"net/http"
"github.com/concourse/atc"
"github.com/onsi/gomega"
"github.com/onsi/gomega/types"
)
type PlanMatcher struct {
ExpectedPlan atc.Plan
failedMatcher types.GomegaMatcher
failedValue interface{}
}
func MatchPlan(plan atc.Plan) *PlanMatcher {
return &PlanMatcher{
ExpectedPlan: plan,
}
}
func VerifyPlan(expectedPlan atc.Plan) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var plan atc.Plan
err := json.NewDecoder(r.Body).Decode(&plan)
gomega.Expect(err).ToNot(gomega.HaveOccurred())
gomega.Expect(plan).To(MatchPlan(expectedPlan))
}
}
func (matcher *PlanMatcher) Match(actual interface{}) (bool, error) {
actualPlan, ok := actual.(atc.Plan)
if !ok {
return false, fmt.Errorf("expected a %T, got a %T", matcher.ExpectedPlan, actual)
}
expectedStripped, expectedIDs := stripIDs(matcher.ExpectedPlan)
actualStripped, actualIDs := stripIDs(actualPlan)
planMatcher := gomega.Equal(expectedStripped)
idsMatcher := gomega.ConsistOf(expectedIDs)
matched, err := planMatcher.Match(actualStripped)
if err != nil {
return false, err
}
if !matched {
matcher.failedMatcher = planMatcher
matcher.failedValue = actualStripped
return false, nil
}
matched, err = idsMatcher.Match(actualIDs)
if err != nil {
return false, err
}
if !matched {
matcher.failedMatcher = idsMatcher
matcher.failedValue = actualIDs
return false, nil
}
return true, nil
}
func (matcher *PlanMatcher) FailureMessage(actual interface{}) string {
return matcher.failedMatcher.FailureMessage(matcher.failedValue)
}
func (matcher *PlanMatcher) NegatedFailureMessage(actual interface{}) string {
return matcher.failedMatcher.NegatedFailureMessage(matcher.failedValue)
}
func stripIDs(plan atc.Plan) (atc.Plan, []string) {
var ids []string
var subIDs []string
plan.ID = "<stripped>"
if plan.Aggregate != nil {
for i, p := range *plan.Aggregate {
(*plan.Aggregate)[i], subIDs = stripIDs(p)
ids = append(ids, subIDs...)
}
}
if plan.Do != nil {
for i, p := range *plan.Do {
(*plan.Do)[i], subIDs = stripIDs(p)
ids = append(ids, subIDs...)
}
}
if plan.OnSuccess != nil {
plan.OnSuccess.Step, subIDs = stripIDs(plan.OnSuccess.Step)
ids = append(ids, subIDs...)
plan.OnSuccess.Next, subIDs = stripIDs(plan.OnSuccess.Next)
ids = append(ids, subIDs...)
}
if plan.OnFailure != nil {
plan.OnFailure.Step, subIDs = stripIDs(plan.OnFailure.Step)
ids = append(ids, subIDs...)
plan.OnFailure.Next, subIDs = stripIDs(plan.OnFailure.Next)
ids = append(ids, subIDs...)
}
if plan.Ensure != nil {
plan.Ensure.Step, subIDs = stripIDs(plan.Ensure.Step)
ids = append(ids, subIDs...)
plan.Ensure.Next, subIDs = stripIDs(plan.Ensure.Next)
ids = append(ids, subIDs...)
}
if plan.Timeout != nil {
plan.Timeout.Step, subIDs = stripIDs(plan.Timeout.Step)
ids = append(ids, subIDs...)
}
if plan.Try != nil {
plan.Try.Step, subIDs = stripIDs(plan.Try.Step)
ids = append(ids, subIDs...)
}
if plan.Get != nil {
if plan.Get.VersionFrom != nil {
planID := atc.PlanID("<stripped>")
plan.Get.VersionFrom = &planID
}
}
return plan, ids
}