forked from dcos/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mock_mesos_server.go
69 lines (62 loc) · 1.84 KB
/
mock_mesos_server.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
package prometheus
// You should run 'go generate' every time you change one of the json files in
// the testdata directory, and commit both the changed json file and the
// changed binary file.
//go:generate go run cmd/gen.go
import (
"bytes"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"testing"
)
// raw protobuf request types:
// ref https://github.com/apache/mesos/blob/master/include/mesos/v1/agent/agent.proto
var (
GET_CONTAINERS = []byte{8, 10}
GET_STATE = []byte{8, 9}
GET_TASKS = []byte{8, 13}
)
// startTestServer starts a server and serves the specified fixture's content
// at /api/v1
func startTestServer(t *testing.T, fixture string) *httptest.Server {
router := http.NewServeMux()
containers, cOK := loadFixture(t, filepath.Join(fixture, "containers.bin"))
state, sOK := loadFixture(t, filepath.Join(fixture, "state.bin"))
tasks, tOK := loadFixture(t, filepath.Join(fixture, "tasks.bin"))
router.HandleFunc("/api/v1", func(w http.ResponseWriter, r *http.Request) {
body, _ := ioutil.ReadAll(r.Body)
w.Header().Set("Content-Type", "application/x-protobuf")
w.WriteHeader(http.StatusOK)
if bytes.Equal(body, GET_CONTAINERS) && cOK {
w.Write(containers)
return
}
if bytes.Equal(body, GET_STATE) && sOK {
w.Write(state)
return
}
if bytes.Equal(body, GET_TASKS) && tOK {
w.Write(tasks)
return
}
t.Errorf("Unknown request to mock-mesos-server: %s", body)
return
})
return httptest.NewServer(router)
}
// loadFixture retrieves data from a file in ./testdata
func loadFixture(t *testing.T, filename string) ([]byte, bool) {
path := filepath.Join("testdata", filename)
if _, err := os.Stat(path); err != nil {
// Can't access file - probably not defined
return []byte{}, false
}
bytes, err := ioutil.ReadFile(path)
if err != nil {
t.Error(err)
}
return bytes, err == nil
}