-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi_test.go
118 lines (105 loc) · 2.35 KB
/
api_test.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
package main
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"os"
"reflect"
"testing"
"time"
"github.com/gorilla/mux"
log "github.com/sirupsen/logrus"
)
var pConfig = []byte(`
{
"eventFolder": "testdata/eventTypes",
"sources": {
"fileInput": {
"type": "File",
"file_config": {
"path": "testdata/pipelines/input"
}
}
},
"rules": {
"searchRule": {
"source": "fileInput",
"plugin": "testdata/rules/a.so",
"sink": "fileOutput"
}
},
"states": {},
"sinks": {
"fileOutput": {
"type": "File",
"file_config": {
"path": "testdata/pipelines/output"
}
}
}
}
`)
var a api
func TestMain(m *testing.M) {
log.SetLevel(log.DebugLevel)
a = api{}
go a.Start(apiConfig{
ListenAddress: ":8080",
Backend: backendConfig{
Type: "boltdb",
BoltDBConfig: boltDBConfig{
DatabaseName: "apitest.db",
BucketName: "apitest",
},
},
})
for {
if a.apiReady {
break
}
time.Sleep(20 * time.Millisecond)
}
code := m.Run()
os.Exit(code)
}
func executeRequest(req *http.Request) *httptest.ResponseRecorder {
rr := httptest.NewRecorder()
a.Router.ServeHTTP(rr, req)
return rr
}
func TestGetPipelines(t *testing.T) {
mConfig := monitoringConfiguration{
MonitoringService: "", // Noop service
}
mService, err := mConfig.init(mux.NewRouter())
pipeline, err := a.pipelineManager.NewPipeline(pConfig, mService)
if err != nil {
t.Fatalf("Error creating pipeline %s", err)
}
pID, _ := (*pipeline).ID.MarshalText()
t.Logf(fmt.Sprintf("Getting /pipelines/%s", pID))
req, _ := http.NewRequest("GET", fmt.Sprintf("/pipelines/%s", pID), nil)
response := executeRequest(req)
if response.Code != 200 {
t.Errorf("Expected 200 OK, got: %d", response.Code)
}
if !reflect.DeepEqual(response.Body.Bytes(), pConfig) {
t.Errorf("Expected body to equal\n%s\nGot\n%s\n", pConfig, response.Body.String())
}
a.Shutdown()
}
func TestCreatePipeline(t *testing.T) {
t.Logf("Posting to /pipelines")
req, _ := http.NewRequest("POST", "/pipelines", bytes.NewReader(pConfig))
response := executeRequest(req)
if response.Code != 201 {
t.Errorf("Expected 201 Created, got: %d", response.Code)
}
pID := response.Body.Bytes()
config, _ := a.pipelineManager.Get(pID)
if !reflect.DeepEqual(config, pConfig) {
t.Errorf("Expected config\n%s\nGot\n%s", pConfig, config)
}
a.Shutdown()
}