-
Notifications
You must be signed in to change notification settings - Fork 23
/
integration_test.go
135 lines (117 loc) · 3.2 KB
/
integration_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
// Copyright 2021 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package server
import (
"net/url"
"strings"
"testing"
"time"
"path"
"github.com/cockroachdb/cdc-sink/internal/backend/sinktest"
"github.com/cockroachdb/cdc-sink/internal/util/ident"
"github.com/stretchr/testify/assert"
)
func TestIntegration(t *testing.T) {
if testing.Short() {
t.Skip("short tests requested")
}
a := assert.New(t)
ctx, dbInfo, cancel := sinktest.Context()
defer cancel()
sourceDB, cancel, err := sinktest.CreateDB(ctx)
if !a.NoError(err) {
return
}
defer cancel()
targetDB, cancel, err := sinktest.CreateDB(ctx)
if !a.NoError(err) {
return
}
defer cancel()
// Prefer the webhook format for current versions of CRDB.
useWebhook := true
if strings.Contains(dbInfo.Version(), "v20.2.") || strings.Contains(dbInfo.Version(), "v21.1.") {
useWebhook = false
}
*GenerateSelfSigned = useWebhook
defer func() { *GenerateSelfSigned = false }()
srv, err := newServer(ctx, "127.0.0.1:0", dbInfo.Pool().Config().ConnString(), false)
if !a.NoError(err) {
return
}
// Run the server loop in the background.
go srv.serve()
// Set up source and target tables.
source, err := sinktest.CreateTable(ctx, sourceDB, "CREATE TABLE %s (pk INT PRIMARY KEY, val STRING)")
if !a.NoError(err) {
return
}
target := sinktest.NewTableInfo(dbInfo, ident.NewTable(targetDB, ident.Public, source.Name().Table()))
if !a.NoError(target.Exec(ctx, "CREATE TABLE %s (pk INT PRIMARY KEY, val STRING)")) {
return
}
// Add base data to the source table.
a.NoError(source.Exec(ctx, "INSERT INTO %s (pk, val) VALUES (1, 'one')"))
ct, err := source.RowCount(ctx)
a.NoError(err)
a.Equal(1, ct)
// Set up the changefeed.
var feedURL url.URL
if useWebhook {
feedURL = url.URL{
Scheme: "webhook-https",
Host: srv.listener.Addr().String(),
Path: path.Join(target.Name().Database().Raw(), target.Name().Schema().Raw()),
RawQuery: "insecure_tls_skip_verify=true",
}
} else {
feedURL = url.URL{
Scheme: "experimental-http",
Host: srv.listener.Addr().String(),
Path: path.Join(target.Name().Database().Raw(), target.Name().Schema().Raw()),
}
}
t.Logf("changefeed URL is %s", feedURL.String())
if !a.NoError(source.Exec(ctx,
"CREATE CHANGEFEED FOR TABLE %s "+
"INTO '"+feedURL.String()+"' "+
"WITH updated,resolved='1s'")) {
return
}
// Wait for the backfilled value.
for {
ct, err := target.RowCount(ctx)
if !a.NoError(err) {
return
}
if ct >= 1 {
break
}
t.Log("waiting for backfill")
time.Sleep(time.Second)
}
// Insert an additional value
a.NoError(source.Exec(ctx, "INSERT INTO %s (pk, val) VALUES (2, 'two')"))
ct, err = source.RowCount(ctx)
a.NoError(err)
a.Equal(2, ct)
// Wait for the streamed value.
for {
ct, err := target.RowCount(ctx)
if !a.NoError(err) {
return
}
if ct >= 2 {
break
}
t.Log("waiting for stream")
time.Sleep(time.Second)
}
}