2016-12-25 10:12:57 +00:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2017-02-20 10:11:44 +00:00
|
|
|
"time"
|
2016-12-25 10:12:57 +00:00
|
|
|
|
2016-12-28 08:16:48 +00:00
|
|
|
"github.com/prometheus/common/log"
|
2016-12-25 10:12:57 +00:00
|
|
|
"github.com/prometheus/prometheus/storage"
|
2016-12-25 10:34:22 +00:00
|
|
|
"github.com/prometheus/prometheus/storage/tsdb"
|
2016-12-25 10:12:57 +00:00
|
|
|
)
|
|
|
|
|
2016-12-25 10:34:22 +00:00
|
|
|
// NewStorage returns a new storage for testing purposes
|
2016-12-25 10:12:57 +00:00
|
|
|
// that removes all associated files on closing.
|
2016-12-25 10:34:22 +00:00
|
|
|
func NewStorage(t T) storage.Storage {
|
2016-12-25 10:12:57 +00:00
|
|
|
dir, err := ioutil.TempDir("", "test_storage")
|
|
|
|
if err != nil {
|
2016-12-25 10:34:22 +00:00
|
|
|
t.Fatalf("Opening test dir failed: %s", err)
|
2016-12-25 10:12:57 +00:00
|
|
|
}
|
2016-12-28 08:16:48 +00:00
|
|
|
|
|
|
|
log.With("dir", dir).Debugln("opening test storage")
|
|
|
|
|
2017-02-28 08:33:14 +00:00
|
|
|
db, err := tsdb.Open(dir, nil, &tsdb.Options{
|
2017-02-20 10:11:44 +00:00
|
|
|
MinBlockDuration: 2 * time.Hour,
|
|
|
|
MaxBlockDuration: 24 * time.Hour,
|
2017-02-01 14:59:37 +00:00
|
|
|
AppendableBlocks: 10,
|
|
|
|
})
|
2016-12-25 10:12:57 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Opening test storage failed: %s", err)
|
|
|
|
}
|
2016-12-25 10:34:22 +00:00
|
|
|
return testStorage{Storage: db, dir: dir}
|
2016-12-25 10:12:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type testStorage struct {
|
2016-12-25 10:34:22 +00:00
|
|
|
storage.Storage
|
2016-12-25 10:12:57 +00:00
|
|
|
dir string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s testStorage) Close() error {
|
2016-12-28 08:16:48 +00:00
|
|
|
log.With("dir", s.dir).Debugln("closing test storage")
|
|
|
|
|
2016-12-25 10:34:22 +00:00
|
|
|
if err := s.Storage.Close(); err != nil {
|
2016-12-25 10:12:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return os.RemoveAll(s.dir)
|
|
|
|
}
|