Merge pull request #5893 from prometheus/unify-tsdbutil

Removed extra tsdb/testutil after merge.
This commit is contained in:
Bartek Płotka 2019-08-15 12:07:59 +01:00 committed by GitHub
commit 5cb32d67f9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
32 changed files with 88 additions and 303 deletions

View file

@ -172,7 +172,7 @@ func TestFailedStartupExitCode(t *testing.T) {
prom := exec.Command(promPath, "--config.file="+fakeInputFile) prom := exec.Command(promPath, "--config.file="+fakeInputFile)
err := prom.Run() err := prom.Run()
testutil.NotOk(t, err, "") testutil.NotOk(t, err)
if exitError, ok := err.(*exec.ExitError); ok { if exitError, ok := err.(*exec.ExitError); ok {
status := exitError.Sys().(syscall.WaitStatus) status := exitError.Sys().(syscall.WaitStatus)
@ -275,7 +275,7 @@ func TestWALSegmentSizeBounds(t *testing.T) {
} }
err = prom.Wait() err = prom.Wait()
testutil.NotOk(t, err, "") testutil.NotOk(t, err)
if exitError, ok := err.(*exec.ExitError); ok { if exitError, ok := err.(*exec.ExitError); ok {
status := exitError.Sys().(syscall.WaitStatus) status := exitError.Sys().(syscall.WaitStatus)
testutil.Equals(t, expectedExitStatus, status.ExitStatus()) testutil.Equals(t, expectedExitStatus, status.ExitStatus())

View file

@ -897,7 +897,7 @@ func TestBadStaticConfigsJSON(t *testing.T) {
testutil.Ok(t, err) testutil.Ok(t, err)
var tg targetgroup.Group var tg targetgroup.Group
err = json.Unmarshal(content, &tg) err = json.Unmarshal(content, &tg)
testutil.NotOk(t, err, "") testutil.NotOk(t, err)
} }
func TestBadStaticConfigsYML(t *testing.T) { func TestBadStaticConfigsYML(t *testing.T) {
@ -905,7 +905,7 @@ func TestBadStaticConfigsYML(t *testing.T) {
testutil.Ok(t, err) testutil.Ok(t, err)
var tg targetgroup.Group var tg targetgroup.Group
err = yaml.UnmarshalStrict(content, &tg) err = yaml.UnmarshalStrict(content, &tg)
testutil.NotOk(t, err, "") testutil.NotOk(t, err)
} }
func TestEmptyConfig(t *testing.T) { func TestEmptyConfig(t *testing.T) {

View file

@ -92,7 +92,7 @@ func TestOpenstackSDHypervisorRefreshWithDoneContext(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
cancel() cancel()
_, err := hypervisor.refresh(ctx) _, err := hypervisor.refresh(ctx)
testutil.NotOk(t, err, "") testutil.NotOk(t, err)
testutil.Assert(t, strings.Contains(err.Error(), context.Canceled.Error()), "%q doesn't contain %q", err, context.Canceled) testutil.Assert(t, strings.Contains(err.Error(), context.Canceled.Error()), "%q doesn't contain %q", err, context.Canceled)
mock.TearDownSuite() mock.TearDownSuite()

View file

@ -140,7 +140,7 @@ func TestOpenstackSDInstanceRefreshWithDoneContext(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
cancel() cancel()
_, err := hypervisor.refresh(ctx) _, err := hypervisor.refresh(ctx)
testutil.NotOk(t, err, "") testutil.NotOk(t, err)
testutil.Assert(t, strings.Contains(err.Error(), context.Canceled.Error()), "%q doesn't contain %q", err, context.Canceled) testutil.Assert(t, strings.Contains(err.Error(), context.Canceled.Error()), "%q doesn't contain %q", err, context.Canceled)
mock.TearDownSuite() mock.TearDownSuite()

View file

@ -85,7 +85,7 @@ func TestTritonSDNew(t *testing.T) {
func TestTritonSDNewBadConfig(t *testing.T) { func TestTritonSDNewBadConfig(t *testing.T) {
td, err := newTritonDiscovery(badconf) td, err := newTritonDiscovery(badconf)
testutil.NotOk(t, err, "") testutil.NotOk(t, err)
testutil.Assert(t, td == nil, "") testutil.Assert(t, td == nil, "")
} }
@ -140,7 +140,7 @@ func TestTritonSDRefreshNoServer(t *testing.T) {
) )
_, err := td.refresh(context.Background()) _, err := td.refresh(context.Background())
testutil.NotOk(t, err, "") testutil.NotOk(t, err)
testutil.Equals(t, strings.Contains(err.Error(), "an error occurred when requesting targets from the discovery endpoint"), true) testutil.Equals(t, strings.Contains(err.Error(), "an error occurred when requesting targets from the discovery endpoint"), true)
} }
@ -152,7 +152,7 @@ func TestTritonSDRefreshCancelled(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
cancel() cancel()
_, err := td.refresh(ctx) _, err := td.refresh(ctx)
testutil.NotOk(t, err, "") testutil.NotOk(t, err)
testutil.Equals(t, strings.Contains(err.Error(), context.Canceled.Error()), true) testutil.Equals(t, strings.Contains(err.Error(), context.Canceled.Error()), true)
} }

View file

@ -257,7 +257,7 @@ func TestPromParseErrors(t *testing.T) {
for err == nil { for err == nil {
_, err = p.Next() _, err = p.Next()
} }
testutil.NotOk(t, err, "") testutil.NotOk(t, err)
testutil.Equals(t, c.err, err.Error(), "test %d", i) testutil.Equals(t, c.err, err.Error(), "test %d", i)
} }
} }
@ -313,7 +313,7 @@ func TestPromNullByteHandling(t *testing.T) {
continue continue
} }
testutil.NotOk(t, err, "") testutil.NotOk(t, err)
testutil.Equals(t, c.err, err.Error(), "test %d", i) testutil.Equals(t, c.err, err.Error(), "test %d", i)
} }
} }

View file

@ -28,8 +28,8 @@ import (
"github.com/go-kit/kit/log" "github.com/go-kit/kit/log"
"github.com/prometheus/prometheus/tsdb/chunks" "github.com/prometheus/prometheus/tsdb/chunks"
"github.com/prometheus/prometheus/tsdb/labels" "github.com/prometheus/prometheus/tsdb/labels"
"github.com/prometheus/prometheus/tsdb/testutil"
"github.com/prometheus/prometheus/tsdb/tsdbutil" "github.com/prometheus/prometheus/tsdb/tsdbutil"
"github.com/prometheus/prometheus/util/testutil"
) )
// In Prometheus 2.1.0 we had a bug where the meta.json version was falsely bumped // In Prometheus 2.1.0 we had a bug where the meta.json version was falsely bumped

View file

@ -25,8 +25,8 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/prometheus/prometheus/tsdb/fileutil" "github.com/prometheus/prometheus/tsdb/fileutil"
"github.com/prometheus/prometheus/tsdb/labels" "github.com/prometheus/prometheus/tsdb/labels"
"github.com/prometheus/prometheus/tsdb/testutil"
"github.com/prometheus/prometheus/tsdb/wal" "github.com/prometheus/prometheus/tsdb/wal"
"github.com/prometheus/prometheus/util/testutil"
) )
func TestLastCheckpoint(t *testing.T) { func TestLastCheckpoint(t *testing.T) {

View file

@ -20,7 +20,7 @@ import (
"reflect" "reflect"
"testing" "testing"
"github.com/prometheus/prometheus/tsdb/testutil" "github.com/prometheus/prometheus/util/testutil"
) )
type pair struct { type pair struct {

View file

@ -16,7 +16,7 @@ package chunks
import ( import (
"testing" "testing"
"github.com/prometheus/prometheus/tsdb/testutil" "github.com/prometheus/prometheus/util/testutil"
) )
func TestReaderWithInvalidBuffer(t *testing.T) { func TestReaderWithInvalidBuffer(t *testing.T) {

View file

@ -36,7 +36,7 @@ import (
"github.com/prometheus/prometheus/tsdb/chunks" "github.com/prometheus/prometheus/tsdb/chunks"
tsdb_errors "github.com/prometheus/prometheus/tsdb/errors" tsdb_errors "github.com/prometheus/prometheus/tsdb/errors"
"github.com/prometheus/prometheus/tsdb/labels" "github.com/prometheus/prometheus/tsdb/labels"
"gopkg.in/alecthomas/kingpin.v2" kingpin "gopkg.in/alecthomas/kingpin.v2"
) )
func main() { func main() {

View file

@ -30,7 +30,7 @@ import (
"github.com/prometheus/prometheus/tsdb/chunks" "github.com/prometheus/prometheus/tsdb/chunks"
"github.com/prometheus/prometheus/tsdb/fileutil" "github.com/prometheus/prometheus/tsdb/fileutil"
"github.com/prometheus/prometheus/tsdb/labels" "github.com/prometheus/prometheus/tsdb/labels"
"github.com/prometheus/prometheus/tsdb/testutil" "github.com/prometheus/prometheus/util/testutil"
) )
func TestSplitByRange(t *testing.T) { func TestSplitByRange(t *testing.T) {

View file

@ -33,9 +33,9 @@ import (
"github.com/prometheus/prometheus/tsdb/chunks" "github.com/prometheus/prometheus/tsdb/chunks"
"github.com/prometheus/prometheus/tsdb/index" "github.com/prometheus/prometheus/tsdb/index"
"github.com/prometheus/prometheus/tsdb/labels" "github.com/prometheus/prometheus/tsdb/labels"
"github.com/prometheus/prometheus/tsdb/testutil"
"github.com/prometheus/prometheus/tsdb/tsdbutil" "github.com/prometheus/prometheus/tsdb/tsdbutil"
"github.com/prometheus/prometheus/tsdb/wal" "github.com/prometheus/prometheus/tsdb/wal"
"github.com/prometheus/prometheus/util/testutil"
) )
func openTestDB(t testing.TB, opts *Options) (db *DB, close func()) { func openTestDB(t testing.TB, opts *Options) (db *DB, close func()) {

View file

@ -18,7 +18,7 @@ import (
"path/filepath" "path/filepath"
"testing" "testing"
"github.com/prometheus/prometheus/tsdb/testutil" "github.com/prometheus/prometheus/util/testutil"
) )
func TestLocking(t *testing.T) { func TestLocking(t *testing.T) {

View file

@ -19,7 +19,7 @@ import (
"testing" "testing"
"github.com/prometheus/prometheus/tsdb/labels" "github.com/prometheus/prometheus/tsdb/labels"
"github.com/prometheus/prometheus/tsdb/testutil" "github.com/prometheus/prometheus/util/testutil"
) )
func BenchmarkHeadStripeSeriesCreate(b *testing.B) { func BenchmarkHeadStripeSeriesCreate(b *testing.B) {

View file

@ -30,9 +30,9 @@ import (
"github.com/prometheus/prometheus/tsdb/chunks" "github.com/prometheus/prometheus/tsdb/chunks"
"github.com/prometheus/prometheus/tsdb/index" "github.com/prometheus/prometheus/tsdb/index"
"github.com/prometheus/prometheus/tsdb/labels" "github.com/prometheus/prometheus/tsdb/labels"
"github.com/prometheus/prometheus/tsdb/testutil"
"github.com/prometheus/prometheus/tsdb/tsdbutil" "github.com/prometheus/prometheus/tsdb/tsdbutil"
"github.com/prometheus/prometheus/tsdb/wal" "github.com/prometheus/prometheus/tsdb/wal"
"github.com/prometheus/prometheus/util/testutil"
) )
func BenchmarkCreateSeries(b *testing.B) { func BenchmarkCreateSeries(b *testing.B) {

View file

@ -26,7 +26,7 @@ import (
"github.com/prometheus/prometheus/tsdb/chunks" "github.com/prometheus/prometheus/tsdb/chunks"
"github.com/prometheus/prometheus/tsdb/encoding" "github.com/prometheus/prometheus/tsdb/encoding"
"github.com/prometheus/prometheus/tsdb/labels" "github.com/prometheus/prometheus/tsdb/labels"
"github.com/prometheus/prometheus/tsdb/testutil" "github.com/prometheus/prometheus/util/testutil"
) )
type series struct { type series struct {

View file

@ -20,7 +20,7 @@ import (
"sort" "sort"
"testing" "testing"
"github.com/prometheus/prometheus/tsdb/testutil" "github.com/prometheus/prometheus/util/testutil"
) )
func TestMemPostings_addFor(t *testing.T) { func TestMemPostings_addFor(t *testing.T) {

View file

@ -20,7 +20,7 @@ import (
"sort" "sort"
"testing" "testing"
"github.com/prometheus/prometheus/tsdb/testutil" "github.com/prometheus/prometheus/util/testutil"
) )
func TestCompareAndEquals(t *testing.T) { func TestCompareAndEquals(t *testing.T) {

View file

@ -29,8 +29,8 @@ import (
"github.com/prometheus/prometheus/tsdb/chunks" "github.com/prometheus/prometheus/tsdb/chunks"
"github.com/prometheus/prometheus/tsdb/index" "github.com/prometheus/prometheus/tsdb/index"
"github.com/prometheus/prometheus/tsdb/labels" "github.com/prometheus/prometheus/tsdb/labels"
"github.com/prometheus/prometheus/tsdb/testutil"
"github.com/prometheus/prometheus/tsdb/tsdbutil" "github.com/prometheus/prometheus/tsdb/tsdbutil"
"github.com/prometheus/prometheus/util/testutil"
) )
type mockSeriesSet struct { type mockSeriesSet struct {

View file

@ -20,7 +20,7 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/prometheus/prometheus/tsdb/encoding" "github.com/prometheus/prometheus/tsdb/encoding"
"github.com/prometheus/prometheus/tsdb/labels" "github.com/prometheus/prometheus/tsdb/labels"
"github.com/prometheus/prometheus/tsdb/testutil" "github.com/prometheus/prometheus/util/testutil"
) )
func TestRecord_EncodeDecode(t *testing.T) { func TestRecord_EncodeDecode(t *testing.T) {

View file

@ -22,7 +22,7 @@ import (
"github.com/prometheus/prometheus/tsdb/fileutil" "github.com/prometheus/prometheus/tsdb/fileutil"
"github.com/prometheus/prometheus/tsdb/index" "github.com/prometheus/prometheus/tsdb/index"
"github.com/prometheus/prometheus/tsdb/labels" "github.com/prometheus/prometheus/tsdb/labels"
"github.com/prometheus/prometheus/tsdb/testutil" "github.com/prometheus/prometheus/util/testutil"
) )
func TestRepairBadIndexVersion(t *testing.T) { func TestRepairBadIndexVersion(t *testing.T) {

View file

@ -1,182 +0,0 @@
// Copyright 2013 The Prometheus Authors
// 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 testutil
import (
"crypto/sha256"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"testing"
)
const (
// The base directory used for test emissions, which instructs the operating
// system to use the default temporary directory as the base or TMPDIR
// environment variable.
defaultDirectory = ""
// NilCloser is a no-op Closer.
NilCloser = nilCloser(true)
// The number of times that a TemporaryDirectory will retry its removal
temporaryDirectoryRemoveRetries = 2
)
type (
// Closer is the interface that wraps the Close method.
Closer interface {
// Close reaps the underlying directory and its children. The directory
// could be deleted by its users already.
Close()
}
nilCloser bool
// TemporaryDirectory models a closeable path for transient POSIX disk
// activities.
TemporaryDirectory interface {
Closer
// Path returns the underlying path for access.
Path() string
}
// temporaryDirectory is kept as a private type due to private fields and
// their interactions.
temporaryDirectory struct {
path string
tester T
}
callbackCloser struct {
fn func()
}
// T implements the needed methods of testing.TB so that we do not need
// to actually import testing (which has the side effect of adding all
// the test flags, which we do not want in non-test binaries even if
// they make use of these utilities for some reason).
T interface {
Fatal(args ...interface{})
Fatalf(format string, args ...interface{})
}
)
func (c nilCloser) Close() {
}
func (c callbackCloser) Close() {
c.fn()
}
// NewCallbackCloser returns a Closer that calls the provided function upon
// closing.
func NewCallbackCloser(fn func()) Closer {
return &callbackCloser{
fn: fn,
}
}
func (t temporaryDirectory) Close() {
retries := temporaryDirectoryRemoveRetries
err := os.RemoveAll(t.path)
for err != nil && retries > 0 {
switch {
case os.IsNotExist(err):
err = nil
default:
retries--
err = os.RemoveAll(t.path)
}
}
if err != nil {
t.tester.Fatal(err)
}
}
func (t temporaryDirectory) Path() string {
return t.path
}
// NewTemporaryDirectory creates a new temporary directory for transient POSIX
// activities.
func NewTemporaryDirectory(name string, t T) (handler TemporaryDirectory) {
var (
directory string
err error
)
directory, err = ioutil.TempDir(defaultDirectory, name)
if err != nil {
t.Fatal(err)
}
handler = temporaryDirectory{
path: directory,
tester: t,
}
return
}
// DirSize returns the size in bytes of all files in a directory.
func DirSize(t *testing.T, path string) int64 {
var size int64
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
Ok(t, err)
if !info.IsDir() {
size += info.Size()
}
return nil
})
Ok(t, err)
return size
}
// DirHash returns a hash of all files attribites and their content within a directory.
func DirHash(t *testing.T, path string) []byte {
hash := sha256.New()
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
Ok(t, err)
if info.IsDir() {
return nil
}
f, err := os.Open(path)
Ok(t, err)
defer f.Close()
_, err = io.Copy(hash, f)
Ok(t, err)
_, err = io.WriteString(hash, strconv.Itoa(int(info.Size())))
Ok(t, err)
_, err = io.WriteString(hash, info.Name())
Ok(t, err)
modTime, err := info.ModTime().GobEncode()
Ok(t, err)
_, err = io.WriteString(hash, string(modTime))
Ok(t, err)
return nil
})
Ok(t, err)
return hash.Sum(nil)
}

View file

@ -1,87 +0,0 @@
// The MIT License (MIT)
// Copyright (c) 2014 Ben Johnson
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
package testutil
import (
"fmt"
"path/filepath"
"reflect"
"runtime"
"testing"
)
// Assert fails the test if the condition is false.
func Assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
if !condition {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d: "+msg+"\033[39m\n\n", append([]interface{}{filepath.Base(file), line}, v...)...)
tb.FailNow()
}
}
// Ok fails the test if an err is not nil.
func Ok(tb testing.TB, err error) {
if err != nil {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d: unexpected error: %s\033[39m\n\n", filepath.Base(file), line, err.Error())
tb.FailNow()
}
}
// NotOk fails the test if an err is nil.
func NotOk(tb testing.TB, err error) {
if err == nil {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d: expected error, got nothing \033[39m\n\n", filepath.Base(file), line)
tb.FailNow()
}
}
// Equals fails the test if exp is not equal to act.
func Equals(tb testing.TB, exp, act interface{}, msgAndArgs ...interface{}) {
if !reflect.DeepEqual(exp, act) {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d:%s\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, formatMessage(msgAndArgs), exp, act)
tb.FailNow()
}
}
// NotEquals fails the test if exp is equal to act.
func NotEquals(tb testing.TB, exp, act interface{}) {
if reflect.DeepEqual(exp, act) {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d: Expected different exp and got\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, exp, act)
tb.FailNow()
}
}
func formatMessage(msgAndArgs []interface{}) string {
if len(msgAndArgs) == 0 {
return ""
}
if msg, ok := msgAndArgs[0].(string); ok {
return fmt.Sprintf("\n\nmsg: "+msg, msgAndArgs[1:]...)
}
return ""
}

View file

@ -22,7 +22,7 @@ import (
"time" "time"
"github.com/go-kit/kit/log" "github.com/go-kit/kit/log"
"github.com/prometheus/prometheus/tsdb/testutil" "github.com/prometheus/prometheus/util/testutil"
) )
func TestWriteAndReadbackTombStones(t *testing.T) { func TestWriteAndReadbackTombStones(t *testing.T) {

View file

@ -18,7 +18,7 @@ import (
"sort" "sort"
"testing" "testing"
"github.com/prometheus/prometheus/tsdb/testutil" "github.com/prometheus/prometheus/util/testutil"
) )
func TestSampleRing(t *testing.T) { func TestSampleRing(t *testing.T) {

View file

@ -31,7 +31,7 @@ import (
"github.com/go-kit/kit/log" "github.com/go-kit/kit/log"
tsdb_errors "github.com/prometheus/prometheus/tsdb/errors" tsdb_errors "github.com/prometheus/prometheus/tsdb/errors"
"github.com/prometheus/prometheus/tsdb/testutil" "github.com/prometheus/prometheus/util/testutil"
) )
type reader interface { type reader interface {

View file

@ -24,7 +24,7 @@ import (
"testing" "testing"
client_testutil "github.com/prometheus/client_golang/prometheus/testutil" client_testutil "github.com/prometheus/client_golang/prometheus/testutil"
"github.com/prometheus/prometheus/tsdb/testutil" "github.com/prometheus/prometheus/util/testutil"
) )
// TestWALRepair_ReadingError ensures that a repair is run for an error // TestWALRepair_ReadingError ensures that a repair is run for an error

View file

@ -29,8 +29,8 @@ import (
"github.com/go-kit/kit/log" "github.com/go-kit/kit/log"
"github.com/prometheus/prometheus/tsdb/fileutil" "github.com/prometheus/prometheus/tsdb/fileutil"
"github.com/prometheus/prometheus/tsdb/labels" "github.com/prometheus/prometheus/tsdb/labels"
"github.com/prometheus/prometheus/tsdb/testutil"
"github.com/prometheus/prometheus/tsdb/wal" "github.com/prometheus/prometheus/tsdb/wal"
"github.com/prometheus/prometheus/util/testutil"
) )
func TestSegmentWAL_cut(t *testing.T) { func TestSegmentWAL_cut(t *testing.T) {

View file

@ -14,8 +14,13 @@
package testutil package testutil
import ( import (
"crypto/sha256"
"io"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath"
"strconv"
"testing"
) )
const ( const (
@ -127,3 +132,51 @@ func NewTemporaryDirectory(name string, t T) (handler TemporaryDirectory) {
return return
} }
// DirSize returns the size in bytes of all files in a directory.
func DirSize(t *testing.T, path string) int64 {
var size int64
err := filepath.Walk(path, func(_ string, info os.FileInfo, err error) error {
Ok(t, err)
if !info.IsDir() {
size += info.Size()
}
return nil
})
Ok(t, err)
return size
}
// DirHash returns a hash of all files attribites and their content within a directory.
func DirHash(t *testing.T, path string) []byte {
hash := sha256.New()
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
Ok(t, err)
if info.IsDir() {
return nil
}
f, err := os.Open(path)
Ok(t, err)
defer f.Close()
_, err = io.Copy(hash, f)
Ok(t, err)
_, err = io.WriteString(hash, strconv.Itoa(int(info.Size())))
Ok(t, err)
_, err = io.WriteString(hash, info.Name())
Ok(t, err)
modTime, err := info.ModTime().GobEncode()
Ok(t, err)
_, err = io.WriteString(hash, string(modTime))
Ok(t, err)
return nil
})
Ok(t, err)
return hash.Sum(nil)
}

View file

@ -52,10 +52,11 @@ func Ok(tb TB, err error) {
} }
// NotOk fails the test if an err is nil. // NotOk fails the test if an err is nil.
func NotOk(tb TB, err error, format string, a ...interface{}) { func NotOk(tb TB, err error, a ...interface{}) {
tb.Helper() tb.Helper()
if err == nil { if err == nil {
if len(a) != 0 { if len(a) != 0 {
format := a[0].(string)
tb.Fatalf("\033[31m"+format+": expected error, got none\033[39m", a...) tb.Fatalf("\033[31m"+format+": expected error, got none\033[39m", a...)
} }
tb.Fatalf("\033[31mexpected error, got none\033[39m") tb.Fatalf("\033[31mexpected error, got none\033[39m")
@ -76,7 +77,7 @@ func formatMessage(msgAndArgs []interface{}) string {
} }
if msg, ok := msgAndArgs[0].(string); ok { if msg, ok := msgAndArgs[0].(string); ok {
return fmt.Sprintf("\nmsg: "+msg, msgAndArgs[1:]...) return fmt.Sprintf("\n\nmsg: "+msg, msgAndArgs[1:]...)
} }
return "" return ""
} }