forked from apache/cassandra-gocql-driver
-
Notifications
You must be signed in to change notification settings - Fork 59
/
recreate_test.go
258 lines (221 loc) · 6.26 KB
/
recreate_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
//go:build integration && scylla
// +build integration,scylla
// Copyright (C) 2017 ScyllaDB
package gocql
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"regexp"
"sort"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
var updateGolden = flag.Bool("update-golden", false, "update golden files")
func TestRecreateSchema(t *testing.T) {
session := createSessionFromCluster(createCluster(), t)
defer session.Close()
getStmtFromCluster := isDescribeKeyspaceSupported(t, session)
tcs := []struct {
Name string
Keyspace string
Input string
Golden string
}{
{
Name: "Keyspace",
Keyspace: "gocqlx_keyspace",
Input: "testdata/recreate/keyspace.cql",
Golden: "testdata/recreate/keyspace_golden.cql",
},
{
Name: "Table",
Keyspace: "gocqlx_table",
Input: "testdata/recreate/table.cql",
Golden: "testdata/recreate/table_golden.cql",
},
{
Name: "Materialized Views",
Keyspace: "gocqlx_mv",
Input: "testdata/recreate/materialized_views.cql",
Golden: "testdata/recreate/materialized_views_golden.cql",
},
{
Name: "Index",
Keyspace: "gocqlx_idx",
Input: "testdata/recreate/index.cql",
Golden: "testdata/recreate/index_golden.cql",
},
{
Name: "Secondary Index",
Keyspace: "gocqlx_sec_idx",
Input: "testdata/recreate/secondary_index.cql",
Golden: "testdata/recreate/secondary_index_golden.cql",
},
{
Name: "UDT",
Keyspace: "gocqlx_udt",
Input: "testdata/recreate/udt.cql",
Golden: "testdata/recreate/udt_golden.cql",
},
{
Name: "Aggregates",
Keyspace: "gocqlx_aggregates",
Input: "testdata/recreate/aggregates.cql",
Golden: "testdata/recreate/aggregates_golden.cql",
},
}
for i := range tcs {
test := tcs[i]
t.Run(test.Name, func(t *testing.T) {
cleanup(t, session, test.Keyspace)
in, err := ioutil.ReadFile(test.Input)
if err != nil {
t.Fatal(err)
}
queries := trimQueries(strings.Split(string(in), ";"))
for _, q := range queries {
qr := session.Query(q, nil)
if err := qr.Exec(); err != nil {
t.Fatal("invalid input query", q, err)
}
qr.Release()
}
km, err := session.KeyspaceMetadata(test.Keyspace)
if err != nil {
t.Fatal("dump schema", err)
}
dump, err := km.ToCQL()
if err != nil {
t.Fatal("recreate schema", err)
}
dump = trimSchema(dump)
var golden []byte
if getStmtFromCluster {
golden, err = getCreateStatements(session, test.Keyspace)
if err != nil {
t.Fatal(err)
}
golden = []byte(trimSchema(string(golden)))
} else {
if *updateGolden {
if err := ioutil.WriteFile(test.Golden, []byte(dump), 0644); err != nil {
t.Fatal(err)
}
}
golden, err = ioutil.ReadFile(test.Golden)
if err != nil {
t.Fatal(err)
}
golden = []byte(trimSchema(string(golden)))
}
goldenQueries := trimQueries(sortQueries(strings.Split(string(golden), ";")))
dumpQueries := trimQueries(sortQueries(strings.Split(dump, ";")))
if len(goldenQueries) != len(dumpQueries) {
t.Errorf("Expected len(dumpQueries) to be %d, got %d", len(goldenQueries), len(dumpQueries))
}
// Compare with golden
for i, dq := range dumpQueries {
gq := goldenQueries[i]
if diff := cmp.Diff(gq, dq); diff != "" {
t.Errorf("dumpQueries[%d] diff\n%s", i, diff)
}
}
// Exec dumped queries to check if they are CQL-correct
cleanup(t, session, test.Keyspace)
session.metadataDescriber.clearSchema(test.Keyspace)
for _, q := range trimQueries(strings.Split(dump, ";")) {
qr := session.Query(q, nil)
if err := qr.Exec(); err != nil {
t.Fatal("invalid dump query", q, err)
}
qr.Release()
}
// Check if new dump is the same as previous
km, err = session.KeyspaceMetadata(test.Keyspace)
if err != nil {
t.Fatal("dump schema", err)
}
secondDump, err := km.ToCQL()
if err != nil {
t.Fatal("recreate schema", err)
}
secondDump = trimSchema(secondDump)
secondDumpQueries := trimQueries(sortQueries(strings.Split(secondDump, ";")))
if !cmp.Equal(secondDumpQueries, dumpQueries) {
t.Errorf("first dump and second one differs: %s", cmp.Diff(secondDumpQueries, dumpQueries))
}
})
}
}
func isDescribeKeyspaceSupported(t *testing.T, s *Session) bool {
t.Helper()
err := s.control.query(fmt.Sprintf(`DESCRIBE KEYSPACE system WITH INTERNALS`)).Close()
if err != nil {
if errFrame, ok := err.(errorFrame); ok && errFrame.code == ErrCodeSyntax {
// DESCRIBE KEYSPACE is not supported on older versions of Cassandra and Scylla
// For such case schema statement is going to be recreated on the client side
return false
}
t.Fatalf("error querying keyspace schema: %v", err)
}
return true
}
func TestScyllaEncryptionOptionsUnmarshaller(t *testing.T) {
const (
input = "testdata/recreate/scylla_encryption_options.bin"
golden = "testdata/recreate/scylla_encryption_options_golden.json"
)
inputBuf, err := ioutil.ReadFile(input)
if err != nil {
t.Fatal(err)
}
goldenBuf, err := ioutil.ReadFile(golden)
if err != nil {
t.Fatal(err)
}
goldenOpts := &scyllaEncryptionOptions{}
if err := json.Unmarshal(goldenBuf, goldenOpts); err != nil {
t.Fatal(err)
}
opts := &scyllaEncryptionOptions{}
if err := opts.UnmarshalBinary(inputBuf); err != nil {
t.Error(err)
}
if !cmp.Equal(goldenOpts, opts) {
t.Error(cmp.Diff(goldenOpts, opts))
}
}
func cleanup(t *testing.T, session *Session, keyspace string) {
qr := session.Query(`DROP KEYSPACE IF EXISTS ` + keyspace)
if err := qr.Exec(); err != nil {
t.Fatalf("unable to drop keyspace: %v", err)
}
qr.Release()
}
func sortQueries(in []string) []string {
q := trimQueries(in)
sort.Strings(q)
return q
}
func trimQueries(in []string) []string {
queries := in[:0]
for _, q := range in {
q = strings.TrimSpace(q)
if q == "" {
continue
}
if len(q) != 0 {
queries = append(queries, q)
}
}
return queries
}
var schemaVersion = regexp.MustCompile(` WITH ID = [0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}[ \t\n]+AND`)
func trimSchema(s string) string {
// Remove temporary items from the scheme, in particular schema version:
// ) WITH ID = cf0364d0-3b85-11ef-b79d-80a2ee1928c0
return strings.ReplaceAll(schemaVersion.ReplaceAllString(s, " WITH"), "\n\n", "\n")
}