diff --git a/cmd/prometheus/main_test.go b/cmd/prometheus/main_test.go index e82e55bc3..1cf97f8eb 100644 --- a/cmd/prometheus/main_test.go +++ b/cmd/prometheus/main_test.go @@ -172,7 +172,7 @@ func TestFailedStartupExitCode(t *testing.T) { prom := exec.Command(promPath, "--config.file="+fakeInputFile) err := prom.Run() - testutil.NotOk(t, err, "") + testutil.NotOk(t, err) if exitError, ok := err.(*exec.ExitError); ok { status := exitError.Sys().(syscall.WaitStatus) @@ -275,7 +275,7 @@ func TestWALSegmentSizeBounds(t *testing.T) { } err = prom.Wait() - testutil.NotOk(t, err, "") + testutil.NotOk(t, err) if exitError, ok := err.(*exec.ExitError); ok { status := exitError.Sys().(syscall.WaitStatus) testutil.Equals(t, expectedExitStatus, status.ExitStatus()) diff --git a/config/config_test.go b/config/config_test.go index 03150dace..9f744db7a 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -897,7 +897,7 @@ func TestBadStaticConfigsJSON(t *testing.T) { testutil.Ok(t, err) var tg targetgroup.Group err = json.Unmarshal(content, &tg) - testutil.NotOk(t, err, "") + testutil.NotOk(t, err) } func TestBadStaticConfigsYML(t *testing.T) { @@ -905,7 +905,7 @@ func TestBadStaticConfigsYML(t *testing.T) { testutil.Ok(t, err) var tg targetgroup.Group err = yaml.UnmarshalStrict(content, &tg) - testutil.NotOk(t, err, "") + testutil.NotOk(t, err) } func TestEmptyConfig(t *testing.T) { diff --git a/discovery/openstack/hypervisor_test.go b/discovery/openstack/hypervisor_test.go index 1e4463892..c4acbb27b 100644 --- a/discovery/openstack/hypervisor_test.go +++ b/discovery/openstack/hypervisor_test.go @@ -92,7 +92,7 @@ func TestOpenstackSDHypervisorRefreshWithDoneContext(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) cancel() _, 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) mock.TearDownSuite() diff --git a/discovery/openstack/instance_test.go b/discovery/openstack/instance_test.go index 7e2869118..a6ec1cd39 100644 --- a/discovery/openstack/instance_test.go +++ b/discovery/openstack/instance_test.go @@ -140,7 +140,7 @@ func TestOpenstackSDInstanceRefreshWithDoneContext(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) cancel() _, 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) mock.TearDownSuite() diff --git a/discovery/triton/triton_test.go b/discovery/triton/triton_test.go index 3050d36ff..534fa494c 100644 --- a/discovery/triton/triton_test.go +++ b/discovery/triton/triton_test.go @@ -85,7 +85,7 @@ func TestTritonSDNew(t *testing.T) { func TestTritonSDNewBadConfig(t *testing.T) { td, err := newTritonDiscovery(badconf) - testutil.NotOk(t, err, "") + testutil.NotOk(t, err) testutil.Assert(t, td == nil, "") } @@ -140,7 +140,7 @@ func TestTritonSDRefreshNoServer(t *testing.T) { ) _, 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) } @@ -152,7 +152,7 @@ func TestTritonSDRefreshCancelled(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) cancel() _, err := td.refresh(ctx) - testutil.NotOk(t, err, "") + testutil.NotOk(t, err) testutil.Equals(t, strings.Contains(err.Error(), context.Canceled.Error()), true) } diff --git a/pkg/textparse/promparse_test.go b/pkg/textparse/promparse_test.go index 3f1d7b52f..7bfa36fc0 100644 --- a/pkg/textparse/promparse_test.go +++ b/pkg/textparse/promparse_test.go @@ -257,7 +257,7 @@ func TestPromParseErrors(t *testing.T) { for err == nil { _, err = p.Next() } - testutil.NotOk(t, err, "") + testutil.NotOk(t, err) testutil.Equals(t, c.err, err.Error(), "test %d", i) } } @@ -313,7 +313,7 @@ func TestPromNullByteHandling(t *testing.T) { continue } - testutil.NotOk(t, err, "") + testutil.NotOk(t, err) testutil.Equals(t, c.err, err.Error(), "test %d", i) } } diff --git a/tsdb/block_test.go b/tsdb/block_test.go index e9389e99a..a7a159483 100644 --- a/tsdb/block_test.go +++ b/tsdb/block_test.go @@ -28,8 +28,8 @@ import ( "github.com/go-kit/kit/log" "github.com/prometheus/prometheus/tsdb/chunks" "github.com/prometheus/prometheus/tsdb/labels" - "github.com/prometheus/prometheus/tsdb/testutil" "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 diff --git a/tsdb/checkpoint_test.go b/tsdb/checkpoint_test.go index 87753a79e..2fd4bf373 100644 --- a/tsdb/checkpoint_test.go +++ b/tsdb/checkpoint_test.go @@ -25,8 +25,8 @@ import ( "github.com/pkg/errors" "github.com/prometheus/prometheus/tsdb/fileutil" "github.com/prometheus/prometheus/tsdb/labels" - "github.com/prometheus/prometheus/tsdb/testutil" "github.com/prometheus/prometheus/tsdb/wal" + "github.com/prometheus/prometheus/util/testutil" ) func TestLastCheckpoint(t *testing.T) { diff --git a/tsdb/chunkenc/chunk_test.go b/tsdb/chunkenc/chunk_test.go index 720572603..bd344882a 100644 --- a/tsdb/chunkenc/chunk_test.go +++ b/tsdb/chunkenc/chunk_test.go @@ -20,7 +20,7 @@ import ( "reflect" "testing" - "github.com/prometheus/prometheus/tsdb/testutil" + "github.com/prometheus/prometheus/util/testutil" ) type pair struct { diff --git a/tsdb/chunks/chunks_test.go b/tsdb/chunks/chunks_test.go index 0454ea951..d9d952457 100644 --- a/tsdb/chunks/chunks_test.go +++ b/tsdb/chunks/chunks_test.go @@ -16,7 +16,7 @@ package chunks import ( "testing" - "github.com/prometheus/prometheus/tsdb/testutil" + "github.com/prometheus/prometheus/util/testutil" ) func TestReaderWithInvalidBuffer(t *testing.T) { diff --git a/tsdb/cmd/tsdb/main.go b/tsdb/cmd/tsdb/main.go index 005adb2dd..9acc80ad7 100644 --- a/tsdb/cmd/tsdb/main.go +++ b/tsdb/cmd/tsdb/main.go @@ -36,7 +36,7 @@ import ( "github.com/prometheus/prometheus/tsdb/chunks" tsdb_errors "github.com/prometheus/prometheus/tsdb/errors" "github.com/prometheus/prometheus/tsdb/labels" - "gopkg.in/alecthomas/kingpin.v2" + kingpin "gopkg.in/alecthomas/kingpin.v2" ) func main() { diff --git a/tsdb/compact_test.go b/tsdb/compact_test.go index 7f113b39a..3b56cacd2 100644 --- a/tsdb/compact_test.go +++ b/tsdb/compact_test.go @@ -30,7 +30,7 @@ import ( "github.com/prometheus/prometheus/tsdb/chunks" "github.com/prometheus/prometheus/tsdb/fileutil" "github.com/prometheus/prometheus/tsdb/labels" - "github.com/prometheus/prometheus/tsdb/testutil" + "github.com/prometheus/prometheus/util/testutil" ) func TestSplitByRange(t *testing.T) { diff --git a/tsdb/db_test.go b/tsdb/db_test.go index 79c37d335..d8e94215b 100644 --- a/tsdb/db_test.go +++ b/tsdb/db_test.go @@ -33,9 +33,9 @@ import ( "github.com/prometheus/prometheus/tsdb/chunks" "github.com/prometheus/prometheus/tsdb/index" "github.com/prometheus/prometheus/tsdb/labels" - "github.com/prometheus/prometheus/tsdb/testutil" "github.com/prometheus/prometheus/tsdb/tsdbutil" "github.com/prometheus/prometheus/tsdb/wal" + "github.com/prometheus/prometheus/util/testutil" ) func openTestDB(t testing.TB, opts *Options) (db *DB, close func()) { diff --git a/tsdb/fileutil/flock_test.go b/tsdb/fileutil/flock_test.go index a5c880763..f9cbbcf2b 100644 --- a/tsdb/fileutil/flock_test.go +++ b/tsdb/fileutil/flock_test.go @@ -18,7 +18,7 @@ import ( "path/filepath" "testing" - "github.com/prometheus/prometheus/tsdb/testutil" + "github.com/prometheus/prometheus/util/testutil" ) func TestLocking(t *testing.T) { diff --git a/tsdb/head_bench_test.go b/tsdb/head_bench_test.go index f8c78cf8d..85542d1ef 100644 --- a/tsdb/head_bench_test.go +++ b/tsdb/head_bench_test.go @@ -19,7 +19,7 @@ import ( "testing" "github.com/prometheus/prometheus/tsdb/labels" - "github.com/prometheus/prometheus/tsdb/testutil" + "github.com/prometheus/prometheus/util/testutil" ) func BenchmarkHeadStripeSeriesCreate(b *testing.B) { diff --git a/tsdb/head_test.go b/tsdb/head_test.go index 3551996e1..acfc82a5e 100644 --- a/tsdb/head_test.go +++ b/tsdb/head_test.go @@ -30,9 +30,9 @@ import ( "github.com/prometheus/prometheus/tsdb/chunks" "github.com/prometheus/prometheus/tsdb/index" "github.com/prometheus/prometheus/tsdb/labels" - "github.com/prometheus/prometheus/tsdb/testutil" "github.com/prometheus/prometheus/tsdb/tsdbutil" "github.com/prometheus/prometheus/tsdb/wal" + "github.com/prometheus/prometheus/util/testutil" ) func BenchmarkCreateSeries(b *testing.B) { diff --git a/tsdb/index/index_test.go b/tsdb/index/index_test.go index 28622cc90..cfb00c333 100644 --- a/tsdb/index/index_test.go +++ b/tsdb/index/index_test.go @@ -26,7 +26,7 @@ import ( "github.com/prometheus/prometheus/tsdb/chunks" "github.com/prometheus/prometheus/tsdb/encoding" "github.com/prometheus/prometheus/tsdb/labels" - "github.com/prometheus/prometheus/tsdb/testutil" + "github.com/prometheus/prometheus/util/testutil" ) type series struct { diff --git a/tsdb/index/postings_test.go b/tsdb/index/postings_test.go index 42dc6b3f3..eedf59097 100644 --- a/tsdb/index/postings_test.go +++ b/tsdb/index/postings_test.go @@ -20,7 +20,7 @@ import ( "sort" "testing" - "github.com/prometheus/prometheus/tsdb/testutil" + "github.com/prometheus/prometheus/util/testutil" ) func TestMemPostings_addFor(t *testing.T) { diff --git a/tsdb/labels/labels_test.go b/tsdb/labels/labels_test.go index fbf63a0de..c2599781e 100644 --- a/tsdb/labels/labels_test.go +++ b/tsdb/labels/labels_test.go @@ -20,7 +20,7 @@ import ( "sort" "testing" - "github.com/prometheus/prometheus/tsdb/testutil" + "github.com/prometheus/prometheus/util/testutil" ) func TestCompareAndEquals(t *testing.T) { diff --git a/tsdb/querier_test.go b/tsdb/querier_test.go index b9366ec4c..b76a3af5f 100644 --- a/tsdb/querier_test.go +++ b/tsdb/querier_test.go @@ -29,8 +29,8 @@ import ( "github.com/prometheus/prometheus/tsdb/chunks" "github.com/prometheus/prometheus/tsdb/index" "github.com/prometheus/prometheus/tsdb/labels" - "github.com/prometheus/prometheus/tsdb/testutil" "github.com/prometheus/prometheus/tsdb/tsdbutil" + "github.com/prometheus/prometheus/util/testutil" ) type mockSeriesSet struct { diff --git a/tsdb/record_test.go b/tsdb/record_test.go index 7d948c7f4..482780c1c 100644 --- a/tsdb/record_test.go +++ b/tsdb/record_test.go @@ -20,7 +20,7 @@ import ( "github.com/pkg/errors" "github.com/prometheus/prometheus/tsdb/encoding" "github.com/prometheus/prometheus/tsdb/labels" - "github.com/prometheus/prometheus/tsdb/testutil" + "github.com/prometheus/prometheus/util/testutil" ) func TestRecord_EncodeDecode(t *testing.T) { diff --git a/tsdb/repair_test.go b/tsdb/repair_test.go index 3cb5d43de..c3a1d22b2 100644 --- a/tsdb/repair_test.go +++ b/tsdb/repair_test.go @@ -22,7 +22,7 @@ import ( "github.com/prometheus/prometheus/tsdb/fileutil" "github.com/prometheus/prometheus/tsdb/index" "github.com/prometheus/prometheus/tsdb/labels" - "github.com/prometheus/prometheus/tsdb/testutil" + "github.com/prometheus/prometheus/util/testutil" ) func TestRepairBadIndexVersion(t *testing.T) { diff --git a/tsdb/testutil/directory.go b/tsdb/testutil/directory.go deleted file mode 100644 index 5f1c31554..000000000 --- a/tsdb/testutil/directory.go +++ /dev/null @@ -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) -} diff --git a/tsdb/testutil/testutil.go b/tsdb/testutil/testutil.go deleted file mode 100644 index 03784e7f2..000000000 --- a/tsdb/testutil/testutil.go +++ /dev/null @@ -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 "" -} diff --git a/tsdb/tombstones_test.go b/tsdb/tombstones_test.go index 947aa98bf..e37721a98 100644 --- a/tsdb/tombstones_test.go +++ b/tsdb/tombstones_test.go @@ -22,7 +22,7 @@ import ( "time" "github.com/go-kit/kit/log" - "github.com/prometheus/prometheus/tsdb/testutil" + "github.com/prometheus/prometheus/util/testutil" ) func TestWriteAndReadbackTombStones(t *testing.T) { diff --git a/tsdb/tsdbutil/buffer_test.go b/tsdb/tsdbutil/buffer_test.go index 7323a9e7d..b7954ea4e 100644 --- a/tsdb/tsdbutil/buffer_test.go +++ b/tsdb/tsdbutil/buffer_test.go @@ -18,7 +18,7 @@ import ( "sort" "testing" - "github.com/prometheus/prometheus/tsdb/testutil" + "github.com/prometheus/prometheus/util/testutil" ) func TestSampleRing(t *testing.T) { diff --git a/tsdb/wal/reader_test.go b/tsdb/wal/reader_test.go index d9c74453a..eb858a780 100644 --- a/tsdb/wal/reader_test.go +++ b/tsdb/wal/reader_test.go @@ -31,7 +31,7 @@ import ( "github.com/go-kit/kit/log" tsdb_errors "github.com/prometheus/prometheus/tsdb/errors" - "github.com/prometheus/prometheus/tsdb/testutil" + "github.com/prometheus/prometheus/util/testutil" ) type reader interface { diff --git a/tsdb/wal/wal_test.go b/tsdb/wal/wal_test.go index d99a96342..81a845fd4 100644 --- a/tsdb/wal/wal_test.go +++ b/tsdb/wal/wal_test.go @@ -24,7 +24,7 @@ import ( "testing" 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 diff --git a/tsdb/wal_test.go b/tsdb/wal_test.go index 5c8a7489d..71271ae5c 100644 --- a/tsdb/wal_test.go +++ b/tsdb/wal_test.go @@ -29,8 +29,8 @@ import ( "github.com/go-kit/kit/log" "github.com/prometheus/prometheus/tsdb/fileutil" "github.com/prometheus/prometheus/tsdb/labels" - "github.com/prometheus/prometheus/tsdb/testutil" "github.com/prometheus/prometheus/tsdb/wal" + "github.com/prometheus/prometheus/util/testutil" ) func TestSegmentWAL_cut(t *testing.T) { diff --git a/util/testutil/directory.go b/util/testutil/directory.go index d3c9c926f..5f1c31554 100644 --- a/util/testutil/directory.go +++ b/util/testutil/directory.go @@ -14,8 +14,13 @@ package testutil import ( + "crypto/sha256" + "io" "io/ioutil" "os" + "path/filepath" + "strconv" + "testing" ) const ( @@ -127,3 +132,51 @@ func NewTemporaryDirectory(name string, t T) (handler TemporaryDirectory) { 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) +} diff --git a/tsdb/testutil/logging.go b/util/testutil/logging.go similarity index 100% rename from tsdb/testutil/logging.go rename to util/testutil/logging.go diff --git a/util/testutil/testing.go b/util/testutil/testing.go index 52abce5bf..39b44e536 100644 --- a/util/testutil/testing.go +++ b/util/testutil/testing.go @@ -52,10 +52,11 @@ func Ok(tb TB, err error) { } // 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() if err == nil { if len(a) != 0 { + format := a[0].(string) tb.Fatalf("\033[31m"+format+": expected error, got none\033[39m", a...) } 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 { - return fmt.Sprintf("\nmsg: "+msg, msgAndArgs[1:]...) + return fmt.Sprintf("\n\nmsg: "+msg, msgAndArgs[1:]...) } return "" }