forked from scylladb/gocql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recreate_test.go
206 lines (177 loc) · 4.69 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
//+build integration
//+build scylla
// Copyright (C) 2017 ScyllaDB
package gocql
import (
"encoding/json"
"io/ioutil"
"sort"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestRecreateSchema(t *testing.T) {
session := createSessionFromCluster(createCluster(), t)
defer session.Close()
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)
}
golden, err := ioutil.ReadFile(test.Golden)
if err != nil {
t.Fatal(err)
}
goldenQueries := sortQueries(strings.Split(string(golden), ";"))
dumpQueries := 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.schemaDescriber.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)
}
secondDumpQueries := sortQueries(strings.Split(secondDump, ";"))
if !cmp.Equal(secondDumpQueries, dumpQueries) {
t.Errorf("first dump and second one differs: %s", cmp.Diff(secondDumpQueries, dumpQueries))
}
})
}
}
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 len(q) != 0 {
queries = append(queries, q)
}
}
return queries
}