-
Notifications
You must be signed in to change notification settings - Fork 5
/
expectation.go
129 lines (98 loc) · 2.72 KB
/
expectation.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
package handler
import (
"fmt"
"net/http"
"github.com/open-cluster-management/applifecycle-backend-e2e/pkg"
)
func (s *Processor) dispatchExpectation(testID string, exps pkg.Expectations) (*TResponse, error) {
s.logger.Info(fmt.Sprintf("running test id %s", testID))
tr := &TResponse{
TestID: testID,
Name: "checked expectations",
}
var err error
out := pkg.Expectations{}
defer func() {
tr.Details = out
}()
for _, e := range exps {
s.logger.V(DebugLevel).Info(fmt.Sprintf("running test id %s, expectation %s", testID, e.Desc))
cName := e.TargetCluster
cUnit, ok := s.configs[cName]
if !ok {
err = fmt.Errorf("unregister cluster name: (%s)", cName)
tr.Error = err.Error()
tr.Status = Failed
return tr, err
}
mName := e.Matcher
matcher := s.getMatcher(mName)
if matcher == nil {
err := fmt.Errorf("unregister matcher: (%s)", mName)
tr.Status = Failed
tr.Error = err.Error()
return tr, err
}
if nerr := matcher.Match(cUnit.Clt, e, s.logger); nerr != nil {
tr.Status = Failed
tr.Error = nerr.Error()
return tr, nerr
}
out = append(out, e)
}
tr.Status = Succeed
return tr, err
}
func (s *Processor) ExpectationCheckerHandler(w http.ResponseWriter, r *http.Request) {
// make sure we can dynamically change the expectations records when the
// endpoint is called
tr := &TResponse{
TestID: "",
Name: "list registered expectation",
}
var err error
testID := r.URL.Query().Get("id")
s.logger.Info(fmt.Sprintf("Start checking test ID %s", testID))
defer s.logger.Info(fmt.Sprintf("Done checking test ID %s", testID))
defer w.Header().Set("Content-Type", "application/json")
exps, ok := s.expectations[testID]
if !ok {
tr.Status = Failed
tr.Error = fmt.Errorf("ID (%s) doesn't exist", testID).Error()
fmt.Fprint(w, tr.String())
return
}
tr, err = s.dispatchExpectation(testID, exps)
if err == nil {
tr.Status = Succeed
} else {
tr.Status = Failed
tr.Error = err.Error()
}
fmt.Fprint(w, tr.String())
return
}
func (s *Processor) DisplayExpectationHandler(w http.ResponseWriter, r *http.Request) {
tr := &TResponse{Name: "expectations list"}
testID := r.URL.Query().Get("id")
s.logger.Info(fmt.Sprintf("Start display expectations testID (%s)", testID))
defer s.logger.Info(fmt.Sprintf("Done display expectations testID (%s)", testID))
w.Header().Set("Content-Type", "application/json")
if testID == "" {
tr.Status = Succeed
tr.Details = s.expectations
fmt.Fprint(w, tr.String())
return
}
c, ok := s.expectations[testID]
if !ok {
tr.Status = Unknown
tr.Error = fmt.Errorf("ID (%s) doesn't exist", testID).Error()
fmt.Fprint(w, tr.String())
return
}
tr.Status = Succeed
tr.Details = c
fmt.Fprint(w, tr.String())
return
}