2013-03-27 04:52:08 -07:00
|
|
|
// Copyright 2013 Prometheus Team
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package test
|
|
|
|
|
|
|
|
import (
|
2013-06-08 01:27:44 -07:00
|
|
|
"code.google.com/p/goprotobuf/proto"
|
|
|
|
|
2013-03-27 04:52:08 -07:00
|
|
|
"github.com/prometheus/prometheus/storage/raw/leveldb"
|
|
|
|
"github.com/prometheus/prometheus/utility/test"
|
|
|
|
)
|
|
|
|
|
2013-08-03 08:25:03 -07:00
|
|
|
const cacheCapacity = 0
|
2013-03-27 04:52:08 -07:00
|
|
|
|
|
|
|
type (
|
2014-02-14 10:36:27 -08:00
|
|
|
// Pair models a prospective (key, value) double that will be committed
|
|
|
|
// to a database.
|
2013-03-27 04:52:08 -07:00
|
|
|
Pair interface {
|
2014-02-26 14:47:25 -08:00
|
|
|
Get() (key proto.Message, value interface{})
|
2013-03-27 04:52:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Pairs models a list of Pair for disk committing.
|
|
|
|
Pairs []Pair
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// Preparer readies a LevelDB store for a given raw state given the
|
|
|
|
// fixtures definitions passed into it.
|
2013-03-27 04:52:08 -07:00
|
|
|
Preparer interface {
|
2014-02-14 10:36:27 -08:00
|
|
|
// Prepare furnishes the database and returns its path along
|
|
|
|
// with any encountered anomalies.
|
2013-03-27 04:52:08 -07:00
|
|
|
Prepare(namespace string, f FixtureFactory) test.TemporaryDirectory
|
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// FixtureFactory is an iterator emitting fixture data.
|
2013-03-27 04:52:08 -07:00
|
|
|
FixtureFactory interface {
|
2014-02-14 10:36:27 -08:00
|
|
|
// HasNext indicates whether the FixtureFactory has more pending
|
|
|
|
// fixture data to build.
|
2013-03-27 04:52:08 -07:00
|
|
|
HasNext() (has bool)
|
|
|
|
// Next emits the next (key, value) double for storage.
|
2014-02-26 14:47:25 -08:00
|
|
|
Next() (key proto.Message, value interface{})
|
2013-03-27 04:52:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
preparer struct {
|
|
|
|
tester test.Tester
|
|
|
|
}
|
|
|
|
|
|
|
|
cassetteFactory struct {
|
|
|
|
index int
|
|
|
|
count int
|
|
|
|
pairs Pairs
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func (p preparer) Prepare(n string, f FixtureFactory) (t test.TemporaryDirectory) {
|
|
|
|
t = test.NewTemporaryDirectory(n, p.tester)
|
2013-08-10 08:02:28 -07:00
|
|
|
persistence, err := leveldb.NewLevelDBPersistence(leveldb.LevelDBOptions{
|
2013-08-03 08:25:03 -07:00
|
|
|
Path: t.Path(),
|
|
|
|
CacheSizeBytes: cacheCapacity,
|
2013-08-10 08:02:28 -07:00
|
|
|
})
|
2013-03-27 04:52:08 -07:00
|
|
|
if err != nil {
|
|
|
|
defer t.Close()
|
|
|
|
p.tester.Fatal(err)
|
|
|
|
}
|
2013-04-01 04:22:38 -07:00
|
|
|
|
|
|
|
defer persistence.Close()
|
2013-03-27 04:52:08 -07:00
|
|
|
|
|
|
|
for f.HasNext() {
|
2013-04-05 04:07:13 -07:00
|
|
|
key, value := f.Next()
|
2013-03-27 04:52:08 -07:00
|
|
|
|
2014-02-26 14:47:25 -08:00
|
|
|
switch v := value.(type) {
|
|
|
|
case proto.Message:
|
|
|
|
err = persistence.Put(key, v)
|
|
|
|
case []byte:
|
|
|
|
err = persistence.PutRaw(key, v)
|
|
|
|
default:
|
|
|
|
panic("illegal value type")
|
|
|
|
}
|
2013-03-27 04:52:08 -07:00
|
|
|
if err != nil {
|
|
|
|
defer t.Close()
|
|
|
|
p.tester.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// HasNext implements FixtureFactory.
|
2013-03-27 04:52:08 -07:00
|
|
|
func (f cassetteFactory) HasNext() bool {
|
|
|
|
return f.index < f.count
|
|
|
|
}
|
|
|
|
|
2014-02-14 10:36:27 -08:00
|
|
|
// Next implements FixtureFactory.
|
2014-02-26 14:47:25 -08:00
|
|
|
func (f *cassetteFactory) Next() (key proto.Message, value interface{}) {
|
2013-03-27 04:52:08 -07:00
|
|
|
key, value = f.pairs[f.index].Get()
|
|
|
|
|
|
|
|
f.index++
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPreparer creates a new Preparer for use in testing scenarios.
|
|
|
|
func NewPreparer(t test.Tester) Preparer {
|
|
|
|
return preparer{t}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewCassetteFactory builds a new FixtureFactory that uses Pairs as the basis
|
|
|
|
// for generated fixture data.
|
|
|
|
func NewCassetteFactory(pairs Pairs) FixtureFactory {
|
|
|
|
return &cassetteFactory{
|
|
|
|
pairs: pairs,
|
|
|
|
count: len(pairs),
|
|
|
|
}
|
|
|
|
}
|