forked from rudderlabs/rudder-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker_test.go
460 lines (390 loc) · 11.5 KB
/
docker_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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// This files implements integration tests for the rudder-server.
// The code is responsible to run all dependencies using docker containers.
// It then runs the service ensuring it is configurated to use the dependencies.
// Finally, it sends events and observe the destinations expecting to get the events back.
package main_test
import (
"bufio"
"bytes"
"context"
"database/sql"
b64 "encoding/base64"
"flag"
"fmt"
"html/template"
"io/ioutil"
"log"
"math/rand"
"net/http"
"net/http/httptest"
"net/http/httputil"
"os"
"os/signal"
"strconv"
"strings"
"sync"
"syscall"
"testing"
"time"
_ "github.com/lib/pq"
"github.com/ory/dockertest"
"github.com/phayes/freeport"
main "github.com/rudderlabs/rudder-server"
backendconfig "github.com/rudderlabs/rudder-server/config/backend-config"
"github.com/rudderlabs/rudder-server/jobsdb"
warehouseutils "github.com/rudderlabs/rudder-server/warehouse/utils"
"github.com/stretchr/testify/require"
"github.com/tidwall/gjson"
)
var (
hold bool
runIntegration bool
db *sql.DB
DB_DSN = "root@tcp(127.0.0.1:3306)/service"
httpPort string
dbHandle *sql.DB
sourceJSON backendconfig.ConfigT
webhookurl string
webhook *WebhookRecorder
writeKey string
workspaceID string
)
type Event struct {
anonymous_id string
user_id string
}
func randString(n int) string {
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
s := make([]rune, n)
for i := range s {
s[i] = letters[rand.Intn(len(letters))]
}
return string(s)
}
type WebhookRecorder struct {
Server *httptest.Server
requestsMu sync.RWMutex
requestDumps [][]byte
}
func NewWebhook() *WebhookRecorder {
whr := WebhookRecorder{}
whr.Server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
dump, err := httputil.DumpRequest(r, true)
if err != nil {
http.Error(w, fmt.Sprint(err), http.StatusInternalServerError)
return
}
whr.requestsMu.Lock()
whr.requestDumps = append(whr.requestDumps, dump)
whr.requestsMu.Unlock()
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}))
return &whr
}
func (whr *WebhookRecorder) Requests() []*http.Request {
whr.requestsMu.RLock()
defer whr.requestsMu.RUnlock()
requests := make([]*http.Request, len(whr.requestDumps))
for i, d := range whr.requestDumps {
requests[i], _ = http.ReadRequest(bufio.NewReader(bytes.NewReader(d)))
}
return requests
}
func (whr *WebhookRecorder) Close() {
whr.Server.Close()
}
func getWorkspaceConfig() backendconfig.ConfigT {
backendConfig := new(backendconfig.WorkspaceConfig)
sourceJSON, _ := backendConfig.Get()
return sourceJSON
}
func createWorkspaceConfig(templatePath string, values map[string]string) string {
t, err := template.ParseFiles(templatePath)
if err != nil {
panic(err)
}
f, err := ioutil.TempFile("", "workspaceConfig.*.json")
if err != nil {
panic(err)
}
err = t.Execute(f, values)
if err != nil {
panic(err)
}
f.Close()
return f.Name()
}
func initializeWarehouseConfig(src string, des string) map[string][]warehouseutils.WarehouseT {
var warehouses = make(map[string][]warehouseutils.WarehouseT)
for _, source := range sourceJSON.Sources {
if source.Name == src {
if len(source.Destinations) > 0 {
for _, destination := range source.Destinations {
if destination.Name == des {
warehouses[destination.DestinationDefinition.Name] = append(warehouses[destination.DestinationDefinition.Name],
warehouseutils.WarehouseT{Source: source, Destination: destination})
return warehouses
}
}
}
}
}
return warehouses
}
func waitUntilReady(ctx context.Context, endpoint string, atMost, interval time.Duration) {
probe := time.NewTicker(interval)
timeout := time.After(atMost)
for {
select {
case <-ctx.Done():
return
case <-timeout:
log.Panicf("application was not ready after %s\n", atMost)
case <-probe.C:
resp, err := http.Get(endpoint)
if err != nil {
continue
}
if resp.StatusCode == http.StatusOK {
log.Println("application ready")
return
}
}
}
}
func blockOnHold() {
if !hold {
return
}
fmt.Println("Test on hold, before cleanup")
fmt.Println("Press Ctrl+C to exit")
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
<-c
}
func CreateTablePostgres() {
_, err := db.Exec("CREATE TABLE example ( id integer, username varchar(255) )")
if err != nil {
panic(err)
}
}
func SendEvent() {
fmt.Println("Sending Track Event")
url := fmt.Sprintf("http://localhost:%s/v1/track", httpPort)
method := "POST"
payload := strings.NewReader(`{
"userId": "identified user id",
"anonymousId":"anon-id-new",
"context": {
"traits": {
"trait1": "new-val"
},
"ip": "14.5.67.21",
"library": {
"name": "http"
}
},
"timestamp": "2020-02-02T00:23:09.544Z"
}`)
client := &http.Client{}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization",
fmt.Sprintf("Basic %s", b64.StdEncoding.EncodeToString(
[]byte(fmt.Sprintf("%s:", writeKey)),
)),
)
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
fmt.Println("Event Sent Successfully")
}
func TestMain(m *testing.M) {
flag.BoolVar(&hold, "hold", false, "hold environment clean-up after test execution until Ctrl+C is provided")
flag.BoolVar(&runIntegration, "integration", false, "run integration level tests")
flag.Parse()
if !runIntegration {
fmt.Println("Skipping integration test. Use `-integration` to run them.")
return
}
// hack to make defer work, without being affected by the os.Exit in TestMain
os.Exit(run(m))
}
func run(m *testing.M) int {
// uses a sensible default on windows (tcp/http) and linux/osx (socket)
pool, err := dockertest.NewPool("")
if err != nil {
log.Fatalf("Could not connect to docker: %s", err)
}
database := "jobsdb"
// pulls an image, creates a container based on it and runs it
resourcePostgres, err := pool.Run("postgres", "11-alpine", []string{
"POSTGRES_PASSWORD=password",
"POSTGRES_DB=" + database,
"POSTGRES_USER=rudder",
})
if err != nil {
log.Fatalf("Could not start resource: %s", err)
}
defer func() {
if err := pool.Purge(resourcePostgres); err != nil {
log.Printf("Could not purge resource: %s \n", err)
}
}()
DB_DSN = fmt.Sprintf("postgres://rudder:password@localhost:%s/%s?sslmode=disable", resourcePostgres.GetPort("5432/tcp"), database)
os.Setenv("JOBS_DB_HOST", "localhost")
os.Setenv("JOBS_DB_NAME", "jobsdb")
os.Setenv("JOBS_DB_USER", "rudder")
os.Setenv("JOBS_DB_PASSWORD", "password")
os.Setenv("JOBS_DB_PORT", resourcePostgres.GetPort("5432/tcp"))
os.Setenv("WAREHOUSE_JOBS_DB_PORT", resourcePostgres.GetPort("5432/tcp"))
// exponential backoff-retry, because the application in the container might not be ready to accept connections yet
if err := pool.Retry(func() error {
var err error
db, err = sql.Open("postgres", DB_DSN)
if err != nil {
return err
}
return db.Ping()
}); err != nil {
log.Fatalf("Could not connect to docker: %s", err)
}
// ----------
// Set Rudder Transformer
// pulls an image, creates a container based on it and runs it
transformerRes, err := pool.RunWithOptions(&dockertest.RunOptions{
Repository: "rudderlabs/rudder-transformer",
Tag: "latest",
ExposedPorts: []string{"9090"},
Env: []string{
"CONFIG_BACKEND_URL=https://api.dev.rudderlabs.com",
},
})
defer func() {
if err := pool.Purge(transformerRes); err != nil {
log.Printf("Could not purge resource: %s \n", err)
}
}()
transformURL := fmt.Sprintf("http://localhost:%s", transformerRes.GetPort("9090/tcp"))
waitUntilReady(
context.Background(),
fmt.Sprintf("%s/health", transformURL),
time.Minute,
time.Second,
)
os.Setenv("DEST_TRANSFORM_URL", transformURL)
os.Setenv("RUDDER_ADMIN_PASSWORD", "password")
os.Setenv("RSERVER_BACKEND_CONFIG_CONFIG_FROM_FILE", "true")
os.Setenv("WORKSPACE_TOKEN", "1vLbwltztKUgpuFxmJlSe1esX8c")
os.Setenv("CONFIG_BACKEND_URL", "https://api.dev.rudderlabs.com")
httpPortInt, err := freeport.GetFreePort()
if err != nil {
log.Panic(err)
}
httpPort = strconv.Itoa(httpPortInt)
os.Setenv("RSERVER_GATEWAY_WEB_PORT", httpPort)
httpAdminPort, err := freeport.GetFreePort()
if err != nil {
log.Panic(err)
}
os.Setenv("RSERVER_GATEWAY_ADMIN_WEB_PORT", strconv.Itoa(httpAdminPort))
os.Setenv("RSERVER_ENABLE_STATS", "false")
webhook = NewWebhook()
defer webhook.Close()
webhookurl = webhook.Server.URL
fmt.Println("webhookurl", webhookurl)
writeKey = randString(27)
workspaceID = randString(27)
workspaceConfigPath := createWorkspaceConfig(
"testdata/workspaceConfigTemplate.json",
map[string]string{
"webhookUrl": webhookurl,
"writeKey": writeKey,
"workspaceId": workspaceID,
"postgresPort": resourcePostgres.GetPort("5432/tcp"),
},
)
defer func() {
err := os.Remove(workspaceConfigPath)
fmt.Println(err)
}()
fmt.Println("workspace config path:", workspaceConfigPath)
os.Setenv("RSERVER_BACKEND_CONFIG_CONFIG_JSONPATH", workspaceConfigPath)
svcCtx, svcCancel := context.WithCancel(context.Background())
go main.Run(svcCtx)
serviceHealthEndpoint := fmt.Sprintf("http://localhost:%s/health", httpPort)
fmt.Println("serviceHealthEndpoint", serviceHealthEndpoint)
waitUntilReady(
context.Background(),
serviceHealthEndpoint,
time.Minute,
time.Second,
)
code := m.Run()
blockOnHold()
_ = svcCancel
// TODO: svcCancel() - don't cancel service until graceful termination is implemented
fmt.Println("test done, ignore errors bellow:")
// // wait for the service to be stopped
// pool.Retry(func() error {
// _, err := http.Get(serviceHealthEndpoint)
// if err != nil {
// return nil
// }
// return fmt.Errorf("still working")
// })
return code
}
func TestWebhook(t *testing.T) {
//Testing postgres Client
CreateTablePostgres()
//
var err error
psqlInfo := jobsdb.GetConnectionString()
dbHandle, err = sql.Open("postgres", psqlInfo)
if err != nil {
panic(err)
}
// Pulling config form workspaceConfig.json
sourceJSON = getWorkspaceConfig()
require.Empty(t, webhook.Requests(), "webhook should have no request before sending the event")
SendEvent()
require.Eventually(t, func() bool {
return 1 == len(webhook.Requests())
}, time.Minute, 10*time.Millisecond)
req := webhook.Requests()[0]
body, err := ioutil.ReadAll(req.Body)
require.Equal(t, "POST", req.Method)
require.Equal(t, "/", req.URL.Path)
require.Equal(t, "application/json", req.Header.Get("Content-Type"))
require.Equal(t, "RudderLabs", req.Header.Get("User-Agent"))
require.Equal(t, gjson.GetBytes(body, "anonymousId").Str, "anon-id-new")
require.Equal(t, gjson.GetBytes(body, "userId").Str, "identified user id")
require.Equal(t, gjson.GetBytes(body, "rudderId").Str, "daf823fb-e8d3-413a-8313-d34cd756f968")
require.Equal(t, gjson.GetBytes(body, "type").Str, "track")
// TODO: Verify in Live Evets API
}
// Verify Event in POSTGRES
func TestPostgres(t *testing.T) {
var myEvent Event
require.Eventually(t, func() bool {
eventSql:= "select anonymous_id, user_id from example.tracks limit 1"
db.QueryRow(eventSql).Scan(&myEvent.anonymous_id, &myEvent.user_id)
return myEvent.anonymous_id == "anon-id-new"
}, time.Minute, 10*time.Millisecond)
require.Equal(t, "identified user id", myEvent.user_id)
}