-
Notifications
You must be signed in to change notification settings - Fork 13
/
main_test.go
156 lines (137 loc) · 4.93 KB
/
main_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
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/docker/docker/daemon/logger"
"github.com/stretchr/testify/assert"
)
const (
filePathRequestField = "/tmp/test"
)
type mockSumoDriver struct {
StartLoggingCallsCount int
StopLoggingCallsCount int
}
func (m *mockSumoDriver) StartLogging(file string, info logger.Info) error {
m.StartLoggingCallsCount += 1
return nil
}
func (m *mockSumoDriver) StopLogging(file string) error {
m.StopLoggingCallsCount += 1
return nil
}
func NewMockSumoDriver() *mockSumoDriver {
return &mockSumoDriver{
StartLoggingCallsCount: 0,
StopLoggingCallsCount: 0,
}
}
func TestHandlers(t *testing.T) {
mockSumoDriver := NewMockSumoDriver()
mockServer := http.NewServeMux()
mockServer.HandleFunc(startLoggingPath, startLoggingHandler(mockSumoDriver))
mockServer.HandleFunc(stopLoggingPath, stopLoggingHandler(mockSumoDriver))
t.Run("make StartLogging request with missing ContainerID", func(t *testing.T) {
defer resetCallsCount(mockSumoDriver)
req := StartLoggingRequest{
File: filePathRequestField,
Info: logger.Info{
Config: map[string]string{
logOptUrl: "https://example.org",
},
},
}
resp, respBody, err := makeRequest(startLoggingPath, req, mockServer)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, http.StatusOK, resp.StatusCode, "should get a 200 response")
assert.Equal(t, 0, mockSumoDriver.StartLoggingCallsCount, "should not have called StartLogging on the driver")
assert.Equal(t, 0, mockSumoDriver.StopLoggingCallsCount, "should not have called StopLogging on the driver")
assert.Contains(t, respBody.Err, "ContainerID", "error message should mention ContainerID")
})
t.Run(fmt.Sprintf("make StartLogging request with missing log-opt: %s", logOptUrl), func(t *testing.T) {
defer resetCallsCount(mockSumoDriver)
req := StartLoggingRequest{
File: filePathRequestField,
Info: logger.Info{
Config: map[string]string{},
ContainerID: "containeriid",
},
}
resp, respBody, err := makeRequest(startLoggingPath, req, mockServer)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, http.StatusOK, resp.StatusCode, "should get a 200 response")
assert.Equal(t, 0, mockSumoDriver.StartLoggingCallsCount, "should not have called StartLogging on the driver")
assert.Equal(t, 0, mockSumoDriver.StopLoggingCallsCount, "should not have called StopLogging on the driver")
assert.Contains(t, respBody.Err, logOptUrl, "error message should mention %s", logOptUrl)
})
t.Run("make StartLogging request with correct config", func(t *testing.T) {
defer resetCallsCount(mockSumoDriver)
req := StartLoggingRequest{
File: filePathRequestField,
Info: logger.Info{
Config: map[string]string{
logOptUrl: "https://example.org",
},
ContainerID: "containeriid",
},
}
resp, respBody, err := makeRequest(startLoggingPath, req, mockServer)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, http.StatusOK, resp.StatusCode, "should get a 200 response")
assert.Equal(t, 1, mockSumoDriver.StartLoggingCallsCount, "should have called StartLogging on the driver exactly once")
assert.Equal(t, 0, mockSumoDriver.StopLoggingCallsCount, "should not have called StopLogging on the driver")
assert.Equal(t, "", respBody.Err, "error message should be empty")
})
t.Run("make StopLogging request", func(t *testing.T) {
defer resetCallsCount(mockSumoDriver)
req := StopLoggingRequest{
File: filePathRequestField,
}
resp, respBody, err := makeRequest(stopLoggingPath, req, mockServer)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, http.StatusOK, resp.StatusCode, "should get a 200 response")
assert.Equal(t, 0, mockSumoDriver.StartLoggingCallsCount, "should not have called StartLogging on the driver")
assert.Equal(t, 1, mockSumoDriver.StopLoggingCallsCount, "should have called StopLogging on the driver exactly once")
assert.Equal(t, "", respBody.Err, "error message should be empty")
})
}
func resetCallsCount(m *mockSumoDriver) {
m.StartLoggingCallsCount = 0
m.StopLoggingCallsCount = 0
}
func makeRequest(requestPath string, request interface{}, mockServer *http.ServeMux) (*http.Response, *PluginResponse, error) {
requestBody, err := json.Marshal(request)
if err != nil {
return nil, nil, err
}
req, err := http.NewRequest("POST", requestPath, bytes.NewBuffer(requestBody))
if err != nil {
return nil, nil, err
}
respRecorder := httptest.NewRecorder()
mockServer.ServeHTTP(respRecorder, req)
resp := respRecorder.Result()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, nil, err
}
var respBody PluginResponse
err = json.Unmarshal(body, &respBody)
if err != nil {
return nil, nil, err
}
return resp, &respBody, err
}