2016-12-25 10:12:57 +00:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
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")
|
|
|
|
|
2016-12-25 10:12:57 +00:00
|
|
|
db, err := tsdb.Open(dir)
|
|
|
|
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)
|
|
|
|
}
|