-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjobs.go
150 lines (140 loc) · 3.23 KB
/
jobs.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
package main
import (
"context"
"encoding/json"
"fmt"
"net/http"
"os"
"path/filepath"
"sync"
"time"
"github.com/antonmedv/expr"
"github.com/antonmedv/expr/vm"
)
type Job struct {
Assertions []*vm.Program
Name string
File string
Headers map[string]string
}
type JobEnv struct {
Result RspamdResult
}
func createJobs(ctx context.Context, configCh chan Config, jobCh chan Job) {
defer close(jobCh)
exprEnv := expr.Env(JobEnv{})
for {
select {
case <-ctx.Done():
return
case config, ok := <-configCh:
if !ok {
return
}
var err error
job := Job{
Assertions: make([]*vm.Program, len(config.Assertions)),
Name: config.Name,
Headers: config.Headers,
}
for i, v := range config.Assertions {
job.Assertions[i], err = expr.Compile(v, exprEnv)
if err != nil {
fmt.Println("Compile failed (%s): %s", v, err)
return
}
}
for _, input := range config.Inputs {
matches, err := filepath.Glob(input)
if err != nil {
fmt.Println("Glob failed (%s): %s", input, err)
return
}
if len(matches) == 0 {
fmt.Println("Glob matched nothing: %s", input)
return
}
for _, match := range matches {
job.File = match
jobCh <- job
}
}
}
}
}
func processJobs(ctx context.Context, jobCh chan Job, resultCh chan Result, wg *sync.WaitGroup, url string) {
defer wg.Done()
client := &http.Client{
Timeout: time.Second * 10,
}
myVM := vm.VM{}
for {
select {
case <-ctx.Done():
return
case job, ok := <-jobCh:
if !ok {
return
}
jobResult := Result{
File: job.File,
Name: job.Name,
Passed: true,
}
file, err := os.Open(job.File)
if err != nil {
jobResult.Passed = false
jobResult.Errors = append(jobResult.Errors,
fmt.Sprintf("Open failed (%s): %s", job.File, err))
resultCh <- jobResult
continue
}
req, err := http.NewRequestWithContext(ctx, "POST", url, file)
if err != nil {
jobResult.Passed = false
jobResult.Errors = append(jobResult.Errors,
fmt.Sprintf("Creating request failed: %s", err))
resultCh <- jobResult
continue
}
for k, v := range job.Headers {
req.Header.Add(k, v)
}
resp, err := client.Do(req)
if err != nil {
jobResult.Passed = false
jobResult.Errors = append(jobResult.Errors,
fmt.Sprintf("Sending request failed: %s", err))
resultCh <- jobResult
continue
}
jobEnv := JobEnv{}
rspamdResult := RspamdResult{}
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&rspamdResult)
resp.Body.Close()
if err != nil {
jobResult.Passed = false
jobResult.Errors = append(jobResult.Errors,
fmt.Sprintf("Decoding response failed: %s", err))
resultCh <- jobResult
continue
}
jobEnv.Result = rspamdResult
for _, assertion := range job.Assertions {
output, err := myVM.Run(assertion, jobEnv)
if err != nil || output != true {
jobResult.Passed = false
var strErr string
if err != nil {
strErr = fmt.Sprintf("Run failed: %s", err.Error())
} else {
strErr = fmt.Sprintf("Assertion failed: %s", assertion.Source.Content())
}
jobResult.Errors = append(jobResult.Errors, strErr)
}
}
resultCh <- jobResult
}
}
}