-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathmeta_test.go
195 lines (173 loc) · 5.19 KB
/
meta_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
// Copyright 2020 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 metamorphic
import (
"context"
"flag"
"fmt"
"io"
"math/rand"
"os"
"path/filepath"
"strings"
"testing"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
)
var (
keep = flag.Bool("keep", false, "keep temp directories after test")
check = flag.String("check", "", "run operations in specified file and check output for equality")
seed = flag.Int64("seed", 456, "specify seed to use for random number generator")
)
func runMetaTestForEngines(ctx context.Context, t *testing.T, seed int64, checkFile io.Reader, outputFile io.Writer, restarts bool, engineImpls []engineImpl) {
tempDir, cleanup := testutils.TempDir(t)
defer func() {
if !*keep {
cleanup()
}
}()
testRunner := metaTestRunner{
ctx: ctx,
t: t,
w: outputFile,
seed: seed,
restarts: restarts,
engineImpls: engineImpls,
path: filepath.Join(tempDir, "store"),
}
testRunner.init()
defer testRunner.closeAll()
if checkFile != nil {
testRunner.parseFileAndRun(checkFile)
} else {
// TODO(itsbilal): Make this configurable.
testRunner.generateAndRun(10000)
}
}
func runMetaTest(
ctx context.Context,
t *testing.T,
seed int64,
checkFile string,
restarts bool,
engineSequences [][]engineImpl,
) {
outerTempDir, cleanup := testutils.TempDir(t)
defer func() {
if !*keep {
cleanup()
}
}()
// The test run with the first engine sequence writes its output to this file.
// All subsequent engine sequence runs compare their output against this file.
firstRunOutput := filepath.Join(outerTempDir, "output.meta")
firstRunExecuted := false
fmt.Printf("first run output file: %s\n", firstRunOutput)
for _, engineSequence := range engineSequences {
var engineNames []string
for _, engineImpl := range engineSequence {
engineNames = append(engineNames, engineImpl.name)
}
t.Run(strings.Join(engineNames, ","), func(t *testing.T) {
innerTempDir, cleanup := testutils.TempDir(t)
defer func() {
if !*keep {
cleanup()
}
}()
// If this is not the first sequence run and a "check" file was not passed
// in, use the first run's output file as the check file.
var checkFileReader io.ReadCloser
if checkFile == "" && firstRunExecuted {
checkFile = firstRunOutput
}
if checkFile != "" {
var err error
checkFileReader, err = os.Open(checkFile)
defer checkFileReader.Close()
if err != nil {
t.Fatal(err)
}
}
var outputFileWriter io.WriteCloser
outputFile := firstRunOutput
if firstRunExecuted {
outputFile = filepath.Join(innerTempDir, "output.meta")
}
var err error
outputFileWriter, err = os.Create(outputFile)
defer outputFileWriter.Close()
if err != nil {
t.Fatal(err)
}
fmt.Printf("check file = %s\noutput file = %s\n", checkFile, outputFile)
runMetaTestForEngines(ctx, t, seed, checkFileReader, outputFileWriter, restarts, engineSequence)
firstRunExecuted = true
})
}
}
// TestRocksPebbleEquivalence runs the MVCC Metamorphic test suite, and checks
// for matching outputs by the test suite between RocksDB and Pebble.
func TestRocksPebbleEquivalence(t *testing.T) {
defer leaktest.AfterTest(t)
ctx := context.Background()
if util.RaceEnabled {
// This test times out with the race detector enabled.
return
}
// Have one fixed seed, one user-specified seed, and one random seed.
seeds := []int64{123, *seed, rand.Int63()}
for _, seed := range seeds {
t.Run(fmt.Sprintf("seed=%d", seed), func(t *testing.T) {
runMetaTest(ctx, t, seed, "", false, [][]engineImpl{
{engineImplRocksDB},
{engineImplPebble},
})
})
}
}
// TestRocksPebbleRestarts runs the MVCC Metamorphic test suite with restarts
// enabled, and ensures that the output remains the same across different
// engine sequences with restarts in between.
func TestRocksPebbleRestarts(t *testing.T) {
defer leaktest.AfterTest(t)
ctx := context.Background()
if util.RaceEnabled {
// This test times out with the race detector enabled.
return
}
// Have one fixed seed, one user-specified seed, and one random seed.
seeds := []int64{123, *seed, rand.Int63()}
for _, seed := range seeds {
t.Run(fmt.Sprintf("seed=%d", seed), func(t *testing.T) {
runMetaTest(ctx, t, seed, "", true, [][]engineImpl{
{engineImplRocksDB},
{engineImplPebble},
{engineImplRocksDB, engineImplPebble},
})
})
}
}
// TestRocksPebbleCheck checks whether the output file specified with --check has
// matching behaviour across rocks/pebble.
func TestRocksPebbleCheck(t *testing.T) {
defer leaktest.AfterTest(t)
ctx := context.Background()
if *check != "" {
if _, err := os.Stat(*check); os.IsNotExist(err) {
t.Fatal(err)
}
runMetaTest(ctx, t, 0, *check, true, [][]engineImpl{
{engineImplRocksDB},
{engineImplPebble},
})
}
}