-
-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathcollect_eval_data_test.go
260 lines (241 loc) · 7.8 KB
/
collect_eval_data_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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package controller_test
import (
"context"
"fmt"
"io"
"log/slog"
"net/http"
"net/http/httptest"
"os"
"regexp"
"strings"
"testing"
"time"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
ffclient "github.com/thomaspoignant/go-feature-flag"
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/controller"
"github.com/thomaspoignant/go-feature-flag/cmd/relayproxy/metric"
"github.com/thomaspoignant/go-feature-flag/exporter/fileexporter"
"github.com/thomaspoignant/go-feature-flag/retriever/fileretriever"
"go.uber.org/zap"
)
func Test_collect_eval_data_Handler(t *testing.T) {
type want struct {
httpCode int
bodyFile string
handlerErr bool
errorMsg string
errorCode int
collectedDataFile string
}
type args struct {
bodyFile string
}
tests := []struct {
name string
args args
want want
}{
{
name: "valid usecase",
args: args{
bodyFile: "../testdata/controller/collect_eval_data/valid_request.json",
},
want: want{
httpCode: http.StatusOK,
bodyFile: "../testdata/controller/collect_eval_data/valid_response.json",
collectedDataFile: "../testdata/controller/collect_eval_data/valid_collected_data.json",
},
},
{
name: "valid with source field",
args: args{
bodyFile: "../testdata/controller/collect_eval_data/request_with_source_field.json",
},
want: want{
httpCode: http.StatusOK,
bodyFile: "../testdata/controller/collect_eval_data/valid_response.json",
collectedDataFile: "../testdata/controller/collect_eval_data/collected_data_with_source_field.json",
},
},
{
name: "invalid json",
args: args{
bodyFile: "../testdata/controller/collect_eval_data/invalid_request.json",
},
want: want{
handlerErr: true,
httpCode: http.StatusBadRequest,
errorMsg: "collectEvalData: invalid input data code=400, message=Syntax error: offset=322, " +
"error=invalid character '}' after array element, internal=invalid character '}' after array " +
"element",
errorCode: http.StatusBadRequest,
},
},
{
name: "invalid data field",
args: args{
bodyFile: "../testdata/controller/collect_eval_data/invalid_request_data_null.json",
},
want: want{
handlerErr: true,
httpCode: http.StatusBadRequest,
errorMsg: "collectEvalData: invalid input data",
errorCode: http.StatusBadRequest,
},
},
{
name: "be sure that the creation date is a unix timestamp",
args: args{
"../testdata/controller/collect_eval_data/valid_request_with_timestamp_ms.json",
},
want: want{
httpCode: http.StatusOK,
bodyFile: "../testdata/controller/collect_eval_data/valid_response.json",
collectedDataFile: "../testdata/controller/collect_eval_data/valid_collected_data_with_timestamp_ms.json",
},
},
{
name: "should have the metadata in the exporter",
args: args{
"../testdata/controller/collect_eval_data/valid_request_metadata.json",
},
want: want{
httpCode: http.StatusOK,
bodyFile: "../testdata/controller/collect_eval_data/valid_response_metadata.json",
collectedDataFile: "../testdata/controller/collect_eval_data/valid_collected_data_metadata.json",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exporterFile, err := os.CreateTemp("", "exporter.json")
assert.NoError(t, err)
defer os.Remove(exporterFile.Name())
// init go-feature-flag
goFF, _ := ffclient.New(ffclient.Config{
PollingInterval: 10 * time.Second,
LeveledLogger: slog.Default(),
Context: context.Background(),
Retriever: &fileretriever.Retriever{
Path: configFlagsLocation,
},
DataExporter: ffclient.DataExporter{
FlushInterval: 10 * time.Second,
MaxEventInMemory: 10000,
Exporter: &fileexporter.Exporter{Filename: exporterFile.Name()},
},
})
logger, err := zap.NewDevelopment()
require.NoError(t, err)
ctrl := controller.NewCollectEvalData(goFF, metric.Metrics{}, logger)
e := echo.New()
rec := httptest.NewRecorder()
// read wantBody request file
var bodyReq io.Reader
if tt.args.bodyFile != "" {
bodyReqContent, err := os.ReadFile(tt.args.bodyFile)
assert.NoError(t, err, "request wantBody file missing %s", tt.args.bodyFile)
bodyReq = strings.NewReader(string(bodyReqContent))
}
req := httptest.NewRequest(echo.POST, "/v1/data/collector", bodyReq)
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
c := e.NewContext(req, rec)
c.SetPath("/v1/data/collector")
handlerErr := ctrl.Handler(c)
if tt.want.handlerErr {
assert.Error(t, handlerErr, "handler should return an error")
he, ok := handlerErr.(*echo.HTTPError)
if ok {
assert.Equal(t, tt.want.errorCode, he.Code)
assert.Equal(t, tt.want.errorMsg, he.Message)
} else {
assert.Equal(t, tt.want.errorMsg, handlerErr.Error())
}
return
}
goFF.Close()
wantBody, err := os.ReadFile(tt.want.bodyFile)
assert.NoError(t, err)
wantCollectData, err := os.ReadFile(tt.want.collectedDataFile)
assert.NoError(t, err)
exportedData, err := os.ReadFile(exporterFile.Name())
assert.NoError(t, err)
// replace the timestamps in the response
regex := regexp.MustCompile(`\d{10}`)
replacedStr := regex.ReplaceAllString(rec.Body.String(), "1652273630")
// validate the result
assert.NoError(t, err, "Impossible the expected wantBody file %s", tt.want.bodyFile)
assert.Equal(t, tt.want.httpCode, rec.Code, "Invalid HTTP Code")
assert.JSONEq(t, string(wantBody), replacedStr, "Invalid response wantBody")
assert.JSONEq(t, string(wantCollectData), string(exportedData), "Invalid exported data")
})
}
}
func Test_collect_tracking_and_evaluation_events(t *testing.T) {
//
//
//
//
// PLEASE REWORK THIS TEST
//
// TODO: Do some tests that the exporterEventType is correctly set in the exporter
//
//
//
evalExporter, err := os.CreateTemp("", "evalExport.json")
assert.NoError(t, err)
trackingExporter, err := os.CreateTemp("", "trackExport.json")
assert.NoError(t, err)
defer func() {
_ = os.Remove(evalExporter.Name())
_ = os.Remove(trackingExporter.Name())
}()
// init go-feature-flag
goFF, _ := ffclient.New(ffclient.Config{
PollingInterval: 10 * time.Second,
LeveledLogger: slog.Default(),
Context: context.Background(),
Retriever: &fileretriever.Retriever{
Path: configFlagsLocation,
},
DataExporters: []ffclient.DataExporter{
{
FlushInterval: 10 * time.Second,
MaxEventInMemory: 10000,
Exporter: &fileexporter.Exporter{Filename: evalExporter.Name()},
},
{
FlushInterval: 10 * time.Second,
MaxEventInMemory: 10000,
Exporter: &fileexporter.Exporter{Filename: trackingExporter.Name()},
ExporterEventType: ffclient.TrackingEventExporter,
},
},
})
logger, err := zap.NewDevelopment()
require.NoError(t, err)
ctrl := controller.NewCollectEvalData(goFF, metric.Metrics{}, logger)
bodyReq, err := os.ReadFile(
"../testdata/controller/collect_eval_data/valid_request_mix_tracking_evaluation.json")
assert.NoError(t, err)
e := echo.New()
rec := httptest.NewRecorder()
req := httptest.NewRequest(echo.POST, "/v1/data/collector", strings.NewReader(string(bodyReq)))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
c := e.NewContext(req, rec)
c.SetPath("/v1/data/collector")
handlerErr := ctrl.Handler(c)
assert.NoError(t, handlerErr)
goFF.Close()
fmt.Println("Evaluation events:")
evalEvents, err := os.ReadFile(evalExporter.Name())
assert.NoError(t, err)
fmt.Println(string(evalEvents))
fmt.Println("Tracking events:")
trackingEvents, err := os.ReadFile(trackingExporter.Name())
assert.NoError(t, err)
fmt.Println(string(trackingEvents))
}