-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacceptance_test.go
183 lines (154 loc) · 5.05 KB
/
acceptance_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
// Copyright © 2022 Meroxa, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package vitess implements Vitess connector for Conduit.
// It provides both, a source and a destination Vitess connector.
package vitess
import (
"context"
"fmt"
"os"
"strings"
"sync/atomic"
"testing"
"time"
"github.com/brianvoe/gofakeit"
"github.com/conduitio-labs/conduit-connector-vitess/destination"
"github.com/conduitio-labs/conduit-connector-vitess/source"
"github.com/conduitio/conduit-commons/opencdc"
sdk "github.com/conduitio/conduit-connector-sdk"
"github.com/matryer/is"
"go.uber.org/goleak"
"vitess.io/vitess/go/vt/vitessdriver"
)
var (
queryCreateTestTable = `create table %s (id int, name text, primary key(id));`
queryCreateTestVindex = `alter vschema on %s add vindex hash(id) using hash;`
queryDropTestTable = `drop table if exists %s;`
)
type driver struct {
sdk.ConfigurableAcceptanceTestDriver
idCounter int64
}
// GenerateRecord generates a random opencdc.Record.
func (d *driver) GenerateRecord(_ *testing.T, operation opencdc.Operation) opencdc.Record {
atomic.AddInt64(&d.idCounter, 1)
return opencdc.Record{
Position: nil,
Operation: operation,
Metadata: map[string]string{
destination.ConfigTable: d.Config.DestinationConfig[destination.ConfigTable],
},
Key: opencdc.StructuredData{
"id": d.idCounter,
},
Payload: opencdc.Change{
After: opencdc.StructuredData{
"id": d.idCounter,
"name": gofakeit.Name(),
},
},
}
}
//nolint:paralleltest // we don't need paralleltest for the Acceptance tests.
func TestAcceptance(t *testing.T) {
cfg := prepareConfig(t)
sdk.AcceptanceTest(t, &driver{
ConfigurableAcceptanceTestDriver: sdk.ConfigurableAcceptanceTestDriver{
Config: sdk.ConfigurableAcceptanceTestDriverConfig{
Connector: Connector,
SourceConfig: cfg,
DestinationConfig: cfg,
BeforeTest: beforeTest(cfg),
AfterTest: afterTest(cfg),
GoleakOptions: []goleak.Option{
// this leak spawn Vitess libraries, there's no way to stop it manually
goleak.IgnoreTopFunction("github.com/golang/glog.(*loggingT).flushDaemon"),
goleak.IgnoreTopFunction("google.golang.org/grpc.(*ccBalancerWrapper).watcher"),
goleak.IgnoreTopFunction("google.golang.org/grpc/internal/transport.(*http2Client).keepalive"),
goleak.IgnoreTopFunction("google.golang.org/grpc/internal/transport.(*controlBuffer).get"),
goleak.IgnoreTopFunction("internal/poll.runtime_pollWait"),
},
},
},
})
}
// beforeTest creates new table before each test.
func beforeTest(cfg map[string]string) func(t *testing.T) {
return func(t *testing.T) {
is := is.New(t)
table := randomIdentifier(t)
t.Logf("table under test: %v", table)
cfg[destination.ConfigTable] = table
err := prepareData(cfg)
is.NoErr(err)
}
}
// afterTest drops a test table.
func afterTest(cfg map[string]string) func(t *testing.T) {
return func(t *testing.T) {
target := strings.Join([]string{cfg[source.ConfigKeyspace], cfg[source.ConfigTabletType]}, "@")
db, err := vitessdriver.Open(cfg[source.ConfigAddress], target)
if err != nil {
t.Errorf("open vitess connection: %v", err)
}
queryDropTable := fmt.Sprintf(queryDropTestTable, cfg[source.ConfigTable])
_, err = db.Exec(queryDropTable)
if err != nil {
t.Errorf("drop test table: %v", err)
}
if err = db.Close(); err != nil {
t.Errorf("close database: %v", err)
}
}
}
func prepareConfig(t *testing.T) map[string]string {
address := os.Getenv("VITESS_ADDRESS")
if address == "" {
t.Skip("VITESS_ADDRESS env var must be set")
return nil
}
return map[string]string{
source.ConfigAddress: address,
source.ConfigKeyspace: "test",
source.ConfigTabletType: "primary",
source.ConfigOrderingColumn: "id",
source.ConfigKeyColumn: "id",
source.ConfigColumns: "id,name",
}
}
func prepareData(cfg map[string]string) error {
target := strings.Join([]string{cfg[source.ConfigKeyspace], cfg[source.ConfigTabletType]}, "@")
db, err := vitessdriver.Open(cfg[source.ConfigAddress], target)
if err != nil {
return err
}
err = db.PingContext(context.Background())
if err != nil {
return err
}
_, err = db.Exec(fmt.Sprintf(queryCreateTestTable, cfg[source.ConfigTable]))
if err != nil {
return err
}
_, err = db.Exec(fmt.Sprintf(queryCreateTestVindex, cfg[source.ConfigTable]))
if err != nil {
return err
}
return db.Close()
}
func randomIdentifier(t *testing.T) string {
return fmt.Sprintf("%v_%d",
strings.ReplaceAll(strings.ToLower(t.Name()), "/", "_"),
time.Now().UnixMicro()%1000)
}