Debian Codegraph @

cmd/influx_inspect/verify/seriesfile/verify_test.go

raw symbols SCIP
1 package seriesfile_test
2
3 import (
4 "fmt"
5 "io"
6 "io/ioutil"
7 "os"
8 "path/filepath"
9 "testing"
10 "time"
11
12 "github.com/influxdata/influxdb/cmd/influx_inspect/verify/seriesfile"
13 "github.com/influxdata/influxdb/models"
14 "github.com/influxdata/influxdb/tsdb"
15 "go.uber.org/zap"
16 )
17
18 func TestVerifies_Valid(t *testing.T) {
19 test := NewTest(t)
20 defer test.Close()
21
22 verify := seriesfile.NewVerify()
23 if testing.Verbose() {
24 verify.Logger, _ = zap.NewDevelopment()
25 }
26 passed, err := verify.VerifySeriesFile(test.Path)
27 test.AssertNoError(err)
28 test.Assert(passed)
29 }
30
31 func TestVerifies_Invalid(t *testing.T) {
32 test := NewTest(t)
33 defer test.Close()
34
35 test.AssertNoError(filepath.Walk(test.Path, func(path string, info os.FileInfo, err error) error {
36 if err != nil {
37 return err
38 }
39 if info.IsDir() {
40 return nil
41 }
42
43 test.Backup(path)
44 defer test.Restore(path)
45
46 fh, err := os.OpenFile(path, os.O_RDWR, 0)
47 test.AssertNoError(err)
48 defer fh.Close()
49
50 _, err = fh.WriteAt([]byte("BOGUS"), 0)
51 test.AssertNoError(err)
52 test.AssertNoError(fh.Close())
53
54 passed, err := seriesfile.NewVerify().VerifySeriesFile(test.Path)
55 test.AssertNoError(err)
56 test.Assert(!passed)
57
58 return nil
59 }))
60 }
61
62 //
63 // helpers
64 //
65
66 type Test struct {
67 *testing.T
68 Path string
69 }
70
71 func NewTest(t *testing.T) *Test {
72 t.Helper()
73
74 dir, err := ioutil.TempDir("", "verify-seriesfile-")
75 if err != nil {
76 t.Fatal(err)
77 }
78
79 // create a series file in the directory
80 err = func() error {
81 seriesFile := tsdb.NewSeriesFile(dir)
82 if err := seriesFile.Open(); err != nil {
83 return err
84 }
85 defer seriesFile.Close()
86 seriesFile.EnableCompactions()
87
88 const (
89 compactionThreshold = 100
90 numSeries = 2 * tsdb.SeriesFilePartitionN * compactionThreshold
91 )
92
93 for _, partition := range seriesFile.Partitions() {
94 partition.CompactThreshold = compactionThreshold
95 }
96
97 var names [][]byte
98 var tagsSlice []models.Tags
99
100 for i := 0; i < numSeries; i++ {
101 names = append(names, []byte(fmt.Sprintf("series%d", i)))
102 tagsSlice = append(tagsSlice, nil)
103 }
104
105 _, err := seriesFile.CreateSeriesListIfNotExists(names, tagsSlice)
106 if err != nil {
107 return err
108 }
109
110 // wait for compaction to make sure we detect issues with the index
111 partitions := seriesFile.Partitions()
112 wait:
113 for _, partition := range partitions {
114 if partition.Compacting() {
115 time.Sleep(100 * time.Millisecond)
116 goto wait
117 }
118 }
119
120 return seriesFile.Close()
121 }()
122 if err != nil {
123 os.RemoveAll(dir)
124 t.Fatal(err)
125 }
126
127 return &Test{
128 T: t,
129 Path: dir,
130 }
131 }
132
133 func (t *Test) Close() {
134 os.RemoveAll(t.Path)
135 }
136
137 func (t *Test) AssertNoError(err error) {
138 t.Helper()
139 if err != nil {
140 t.Fatal("unexpected error:", err)
141 }
142 }
143
144 func (t *Test) Assert(x bool) {
145 t.Helper()
146 if !x {
147 t.Fatal("unexpected condition")
148 }
149 }
150
151 // Backup makes a copy of the path for a later Restore.
152 func (t *Test) Backup(path string) {
153 in, err := os.Open(path)
154 t.AssertNoError(err)
155 defer in.Close()
156
157 out, err := os.Create(path + ".backup")
158 t.AssertNoError(err)
159 defer out.Close()
160
161 _, err = io.Copy(out, in)
162 t.AssertNoError(err)
163 }
164
165 // Restore restores the file at the path to the time when Backup was called last.
166 func (t *Test) Restore(path string) {
167 t.AssertNoError(os.Rename(path+".backup", path))
168 }