-
Notifications
You must be signed in to change notification settings - Fork 17
/
ldb_testing.go
242 lines (210 loc) · 5.54 KB
/
ldb_testing.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
package ctlstore
import (
"context"
"database/sql"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/segmentio/ctlstore/pkg/ldb"
"github.com/segmentio/ctlstore/pkg/schema"
"github.com/segmentio/ctlstore/pkg/sqlgen"
)
// NewLDBTestUtil changes the global default LDB path to a temporary path.
//
// This function is NOT concurrency safe.
func NewLDBTestUtil(t testing.TB) (*LDBTestUtil, func()) {
tmpDir, err := ioutil.TempDir("", "ldb_test")
if err != nil {
t.Fatal(err)
}
globalLDBDirPath = tmpDir
path := filepath.Join(tmpDir, ldb.DefaultLDBFilename)
globalLDBReadOnly = false
globalReader = nil
db, err := sql.Open(ldb.LDBDatabaseDriver, fmt.Sprintf(
"file:%s?_journal_mode=wal&mode=%s&cache=shared",
path,
"rwc",
))
if err != nil {
os.RemoveAll(tmpDir)
t.Fatal(err)
}
err = ldb.EnsureLdbInitialized(context.Background(), db)
if err != nil {
os.RemoveAll(tmpDir)
t.Fatal(err)
}
tu := &LDBTestUtil{DB: db, T: t}
return tu, func() {
os.RemoveAll(tmpDir)
}
}
// NewLDBTestUtilLocal is just like NewLDBTestUtil above except it does not rely
// on global state and is therefore threadsafe, at the cost of requiring users
// to use ensure that the DB property is used to initialize the ctlstore Reader
// instead of relying on the global/default init.
func NewLDBTestUtilLocal(t testing.TB) (*LDBTestUtil, func()) {
tmpDir, err := ioutil.TempDir("", "ldb_test")
if err != nil {
t.Fatal(err)
}
path := filepath.Join(tmpDir, ldb.DefaultLDBFilename)
db, err := sql.Open(ldb.LDBDatabaseDriver, fmt.Sprintf(
"file:%s?_journal_mode=wal&mode=%s&cache=shared",
path,
"rwc",
))
if err != nil {
os.RemoveAll(tmpDir)
t.Fatal(err)
}
err = ldb.EnsureLdbInitialized(context.Background(), db)
if err != nil {
os.RemoveAll(tmpDir)
t.Fatal(err)
}
tu := &LDBTestUtil{DB: db, T: t}
return tu, func() {
os.RemoveAll(tmpDir)
}
}
// LDBTestUtil provides basic unit testing facilities for injecting data
// into a "fake" LDB.
type LDBTestUtil struct {
DB *sql.DB
T testing.TB
}
// LDBTestTableDef is used to pass a table definition to CreateTable
// for use in tests that need the LDB. The way the parameters are
// specified mimics the executive interface. Fields are passed as
// tuples of [name string, type string] where type can be something
// like "string" or "integer," just like the standard executive
// interface.
type LDBTestTableDef struct {
Family string
Name string
Fields [][]string
KeyFields []string
Rows [][]interface{}
}
// CreateTable creates a table in the target test LDB.
func (tu *LDBTestUtil) CreateTable(def LDBTestTableDef) {
fns, fts, err := schema.UnzipFieldsParam(def.Fields)
if err != nil {
tu.T.Fatalf("Error unziping field params: %+v", err)
}
_, _, tbl, err := sqlgen.BuildMetaTableFromInput(
sqlgen.SqlDriverToDriverName(tu.DB.Driver()),
def.Family,
def.Name,
fns,
fts,
def.KeyFields,
)
if err != nil {
tu.T.Fatalf("Error building table def from input: %+v", err)
}
err = tbl.Validate()
if err != nil {
tu.T.Fatalf("Table validation error: %+v", err)
}
ddl, err := tbl.AsCreateTableDDL()
if err != nil {
tu.T.Fatalf("Error rendering table DDL: %+v", err)
}
tx, err := tu.DB.BeginTx(context.Background(), nil)
if err != nil {
tu.T.Fatalf("Error beginning DDL tx: %+v", err)
}
_, err = tx.Exec(ddl)
if err != nil {
tu.T.Fatalf("Error executing DDL: %+v", err)
}
err = tx.Commit()
if err != nil {
tu.T.Fatalf("Error committing DDL tx: %+v", err)
}
if def.Rows != nil {
tu.InsertRows(def.Family, def.Name, def.Rows)
}
}
// InsertRows well, inserts rows into the LDB. Rows are passed as
// tuples in the table's column order.
func (tu *LDBTestUtil) InsertRows(family string, table string, rows [][]interface{}) {
hunks := []string{
"INSERT INTO",
fmt.Sprintf("%s___%s", family, table),
"VALUES",
}
params := []interface{}{}
for ri, row := range rows {
if ri > 0 {
hunks = append(hunks, ",")
}
hunks = append(hunks, "(")
for vi, val := range row {
if vi > 0 {
hunks = append(hunks, ",")
}
hunks = append(hunks, "?")
params = append(params, val)
}
hunks = append(hunks, ")")
}
qs := strings.Join(hunks, " ")
_, err := tu.DB.Exec(qs, params...)
if err != nil {
tu.T.Fatalf("Unexpected error inserting data: %+v", err)
}
qs = fmt.Sprintf(
"REPLACE INTO %s (name, timestamp) VALUES (?, ?)",
ldb.LDBLastUpdateTableName,
)
_, err = tu.DB.Exec(qs, ldb.LDBLastLedgerUpdateColumn, time.Now())
if err != nil {
tu.T.Fatalf("Unexpected error updating ledger timestamp: %+v", err)
}
}
// DeleteAll deletes all rows from the given table.
func (tu *LDBTestUtil) DeleteAll(family string, table string) {
hunks := []string{
"DELETE FROM",
fmt.Sprintf("%s___%s", family, table),
}
qs := strings.Join(hunks, " ")
_, err := tu.DB.Exec(qs)
if err != nil {
tu.T.Fatalf("Unexpected error deleting data: %+v", err)
}
}
// Reset completely clears the test LDB
func (tu *LDBTestUtil) Reset() {
qs := "SELECT DISTINCT tbl_name FROM sqlite_master"
tbls, err := tu.DB.Query(qs)
if err != nil {
tu.T.Fatalf("Unexpected error querying table list: %+v", err)
}
tblNames := []string{}
for tbls.Next() {
var name string
err = tbls.Scan(&name)
if err != nil {
tu.T.Fatalf("Unexpected error scanning table list: %+v", err)
}
if !strings.HasPrefix(name, "_") {
tblNames = append(tblNames, name)
}
}
for _, name := range tblNames {
qs := fmt.Sprintf("DROP TABLE '%s'", name)
_, err := tu.DB.Exec(qs)
if err != nil {
tu.T.Fatalf("Unexpected error dropping table: %+v", err)
}
}
}