-
Notifications
You must be signed in to change notification settings - Fork 5
/
testcase.go
165 lines (124 loc) · 3.2 KB
/
testcase.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package handler
import (
"bytes"
"fmt"
"net/http"
"os/exec"
"time"
"github.com/go-logr/logr"
"github.com/open-cluster-management/applifecycle-backend-e2e/pkg"
gerr "github.com/pkg/errors"
)
type ocCommand string
const (
Apply ocCommand = "apply"
Delete ocCommand = "delete"
)
type AppliedCase struct {
Tc pkg.TestCase
Cfg string
}
func processResource(tURL, kCfgDir string, subCmd ocCommand) error {
var cmd *exec.Cmd
switch subCmd {
case Apply:
cmd = exec.Command("kubectl", "apply", "-f", tURL, "--kubeconfig", kCfgDir)
case Delete:
cmd = exec.Command("kubectl", "delete", "-f", tURL, "--kubeconfig", kCfgDir)
}
var stderr bytes.Buffer
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf(stderr.String())
}
return nil
}
func processResources(urls []string, kCfgDir string, subCmd ocCommand, logger logr.Logger) error {
for _, url := range urls {
if err := processResource(url, kCfgDir, subCmd); err != nil {
return gerr.Wrapf(err, "failed to apply url: %s", url)
}
logger.Info(fmt.Sprintf("%s URL %s\n", subCmd, url))
}
return nil
}
func (s *Processor) CasesRunnerHandler(w http.ResponseWriter, r *http.Request) {
testID := r.URL.Query().Get("id")
s.logger.Info(fmt.Sprintf("Start running %s", testID))
defer s.logger.Info(fmt.Sprintf("Done running %s", testID))
w.Header().Set("Content-Type", "application/json")
tr := &TResponse{
TestID: testID,
Name: fmt.Sprintf("run test case id (%s)", testID),
}
var err error
defer func() {
if err != nil {
s.logger.Error(err, "failed on running test")
err = gerr.Wrap(err, fmt.Sprintf("failed to run test case %s", testID))
tr.Error = err.Error()
}
delete(s.set, testID)
fmt.Fprint(w, tr.String())
}()
if testID == "" {
tr.Status = Unknown
err = fmt.Errorf("unknow id (%s)", testID)
return
}
//make sure unique id is running
_, ok := s.set[testID]
if ok {
tr.Status = Failed
tr.Details = fmt.Sprintf("the request test case (%s) is running", testID)
return
}
s.set[testID] = struct{}{}
_, ok = s.testCases[testID]
if !ok {
tr.Status = Failed
err = fmt.Errorf("ID (%s) doesn't exist", testID)
return
}
applied, err := s.Run(testID, s.testCases)
if err != nil {
tr.Status = Failed
err = gerr.Wrap(err, "failed to apply test ")
return
}
defer func() {
s.Clean(applied)
time.Sleep(10 * time.Second)
}()
tr, err = s.Check(testID, s.timeout, s.expectations)
if err != nil {
err = gerr.Wrap(err, "failed to run checker")
}
return
}
func (s *Processor) DisplayTestCasesHandler(w http.ResponseWriter, r *http.Request) {
testID := r.URL.Query().Get("id")
s.logger.Info(fmt.Sprintf("Start display testcase testID (%s)", testID))
defer s.logger.Info(fmt.Sprintf("Done display testcase testID (%s)", testID))
w.Header().Set("Content-Type", "application/json")
tr := &TResponse{
TestID: testID,
Name: "test case list",
Status: Succeed,
}
if testID == "" {
tr.Details = s.testCases
fmt.Fprint(w, tr.String())
return
}
c, ok := s.testCases[testID]
if !ok {
tr.Status = Failed
tr.Error = fmt.Errorf("ID (%s) doesn't exist", testID).Error()
fmt.Fprint(w, tr.String())
return
}
tr.Details = c
fmt.Fprint(w, tr.String())
return
}