1- package testutil
1+ // Copyright 2019 The Prometheus Authors
2+ // Licensed under the Apache License, Version 2.0 (the "License");
3+ // you may not use this file except in compliance with the License.
4+ // You may obtain a copy of the License at
5+ //
6+ // http://www.apache.org/licenses/LICENSE-2.0
7+ //
8+ // Unless required by applicable law or agreed to in writing, software
9+ // distributed under the License is distributed on an "AS IS" BASIS,
10+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+ // See the License for the specific language governing permissions and
12+ // limitations under the License.
13+
14+ package db
215
316import (
417 "context"
5- "io/ioutil"
618 "math/rand"
719 "os"
820 "path/filepath"
@@ -11,43 +23,31 @@ import (
1123 "testing"
1224
1325 "github.com/go-kit/kit/log"
26+
1427 "github.com/prometheus/tsdb"
1528 "github.com/prometheus/tsdb/labels"
29+ "github.com/prometheus/tsdb/testutil"
1630 "github.com/prometheus/tsdb/tsdbutil"
1731)
1832
19- // OpenTestDB opens a test Database
20- func OpenTestDB (t testing.TB , opts * tsdb.Options ) (db * tsdb.DB , close func ()) {
21- tmpdir , err := ioutil .TempDir ("" , "test" )
22- Ok (t , err )
23-
24- db , err = tsdb .Open (tmpdir , nil , nil , opts )
25- Ok (t , err )
26-
27- // Do not close the test database by default as it will deadlock on test failures.
28- return db , func () {
29- Ok (t , os .RemoveAll (tmpdir ))
30- }
31- }
32-
3333// CreateBlock creates a block with given set of series and returns its dir.
3434func CreateBlock (tb testing.TB , dir string , series []tsdb.Series ) string {
3535 head := createHead (tb , series )
3636 compactor , err := tsdb .NewLeveledCompactor (context .Background (), nil , log .NewNopLogger (), []int64 {1000000 }, nil )
37- Ok (tb , err )
37+ testutil . Ok (tb , err )
3838
39- Ok (tb , os .MkdirAll (dir , 0777 ))
39+ testutil . Ok (tb , os .MkdirAll (dir , 0777 ))
4040
4141 // Add +1 millisecond to block maxt because block intervals are half-open: [b.MinTime, b.MaxTime).
4242 // Because of this block intervals are always +1 than the total samples it includes.
4343 ulid , err := compactor .Write (dir , head , head .MinTime (), head .MaxTime ()+ 1 , nil )
44- Ok (tb , err )
44+ testutil . Ok (tb , err )
4545 return filepath .Join (dir , ulid .String ())
4646}
4747
4848func createHead (tb testing.TB , series []tsdb.Series ) * tsdb.Head {
4949 head , err := tsdb .NewHead (nil , nil , nil , 2 * 60 * 60 * 1000 )
50- Ok (tb , err )
50+ testutil . Ok (tb , err )
5151 defer head .Close ()
5252
5353 app := head .Appender ()
@@ -63,12 +63,12 @@ func createHead(tb testing.TB, series []tsdb.Series) *tsdb.Head {
6363 }
6464 }
6565 ref , err = app .Add (s .Labels (), t , v )
66- Ok (tb , err )
66+ testutil . Ok (tb , err )
6767 }
68- Ok (tb , it .Err ())
68+ testutil . Ok (tb , it .Err ())
6969 }
7070 err = app .Commit ()
71- Ok (tb , err )
71+ testutil . Ok (tb , err )
7272 return head
7373}
7474
0 commit comments