-
Notifications
You must be signed in to change notification settings - Fork 182
/
fake_kubelet.go
57 lines (49 loc) · 1.2 KB
/
fake_kubelet.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
package kubelet
import (
"io"
"net/http"
"net/http/httptest"
"net/url"
"sync"
)
// FakeKubelet is a mock of the ingest server. Holds all of the received
// datapoints for later inspection
type FakeKubelet struct {
server *httptest.Server
lock sync.Mutex
PodJSON []byte
}
// NewFakeKubelet creates a new instance of FakeKubelet but does not start
// the server
func NewFakeKubelet() *FakeKubelet {
return &FakeKubelet{}
}
// Start creates and starts the mock HTTP server
func (f *FakeKubelet) Start() {
f.server = httptest.NewUnstartedServer(f)
f.server.Start()
}
// Close stops the mock HTTP server
func (f *FakeKubelet) Close() {
f.server.Close()
}
// URL is the of the mock server to point your objects under test to
func (f *FakeKubelet) URL() *url.URL {
url, err := url.Parse(f.server.URL)
if err != nil {
panic("Bad URL " + url.String())
}
return url
}
// ServeHTTP handles a single request
func (f *FakeKubelet) ServeHTTP(rw http.ResponseWriter, r *http.Request) {
//contents, _ := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if r.URL.Path == "/pods" {
rw.WriteHeader(http.StatusOK)
rw.Write(f.PodJSON)
} else {
rw.WriteHeader(http.StatusNotFound)
io.WriteString(rw, "Not found")
}
}