From e1406c9406be0ae1bd8ea7f4d6df26747ab799c3 Mon Sep 17 00:00:00 2001 From: Frederic Branczyk Date: Thu, 15 Oct 2020 07:47:55 +0200 Subject: [PATCH 01/65] *: Cut v2.22.0 Signed-off-by: Frederic Branczyk --- CHANGELOG.md | 4 ++-- VERSION | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b85bb12713..2451fb2483 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,11 +1,11 @@ -## 2.22.0-rc.0 / 2020-10-07 +## 2.22.0 / 2020-10-07 As announced in the 2.21.0 release notes, the experimental gRPC API v2 has been removed. * [CHANGE] web: Remove APIv2. #7935 * [ENHANCEMENT] React UI: Implement missing TSDB head stats section. #7876 -* [ENHANCEMENT] UI: Add Collapse all button. #6957 +* [ENHANCEMENT] UI: Add Collapse all button to targets page. #6957 * [ENHANCEMENT] UI: Clarify alert state toggle via checkbox icon. #7936 * [ENHANCEMENT] Add `rule_group_last_evaluation_samples` and `prometheus_tsdb_data_replay_duration_seconds` metrics. #7737 #7977 * [ENHANCEMENT] Gracefully handle unknown WAL record types. #8004 diff --git a/VERSION b/VERSION index 1ed0304cd5..f48f82fa2c 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.22.0-rc.0 +2.22.0 From 5de7d15879b47d60bda8ab266839235fadb59f97 Mon Sep 17 00:00:00 2001 From: Ganesh Vernekar <15064823+codesome@users.noreply.github.com> Date: Mon, 19 Oct 2020 14:27:19 +0530 Subject: [PATCH 02/65] Consistent naming and better names for fields in TSDB format docs (#8077) Signed-off-by: Ganesh Vernekar --- tsdb/docs/format/index.md | 4 ++-- tsdb/docs/format/tombstones.md | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/tsdb/docs/format/index.md b/tsdb/docs/format/index.md index 1d87970dd3..854316cb4f 100644 --- a/tsdb/docs/format/index.md +++ b/tsdb/docs/format/index.md @@ -24,9 +24,9 @@ It is terminated by a table of contents which serves as an entry point into the │ ├──────────────────────────────────────────────┤ │ │ │ Postings N │ │ │ ├──────────────────────────────────────────────┤ │ -│ │ Label Index Table │ │ +│ │ Label Offset Table │ │ │ ├──────────────────────────────────────────────┤ │ -│ │ Postings Table │ │ +│ │ Postings Offset Table │ │ │ ├──────────────────────────────────────────────┤ │ │ │ TOC │ │ │ └──────────────────────────────────────────────┘ │ diff --git a/tsdb/docs/format/tombstones.md b/tsdb/docs/format/tombstones.md index 058f5f1e2f..2dc62bb4c2 100644 --- a/tsdb/docs/format/tombstones.md +++ b/tsdb/docs/format/tombstones.md @@ -25,7 +25,7 @@ The stones section is 0 padded to a multiple of 4 for fast scans. # Tombstone ``` -┌────────────────┬─────────────────┬────────────────┐ -│ref │ mint │ maxt │ -└────────────────┴─────────────────┴────────────────┘ +┌───────────────────────┬─────────────────┬────────────────┐ +│series ref │ mint │ maxt │ +└───────────────────────┴─────────────────┴────────────────┘ ``` From 59733b1a267028a7ba76295d7441ec32407f724e Mon Sep 17 00:00:00 2001 From: Julien Pivotto Date: Mon, 19 Oct 2020 13:21:54 +0200 Subject: [PATCH 03/65] TSDB: use `blocks` instead of db.blocks in condition (#8068) Signed-off-by: Julien Pivotto --- tsdb/db.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tsdb/db.go b/tsdb/db.go index 4c70a40527..9d8308f066 100644 --- a/tsdb/db.go +++ b/tsdb/db.go @@ -1077,7 +1077,7 @@ func deletableBlocks(db *DB, blocks []*Block) map[ulid.ULID]struct{} { // set in the db options. func BeyondTimeRetention(db *DB, blocks []*Block) (deletable map[ulid.ULID]struct{}) { // Time retention is disabled or no blocks to work with. - if len(db.blocks) == 0 || db.opts.RetentionDuration == 0 { + if len(blocks) == 0 || db.opts.RetentionDuration == 0 { return } @@ -1100,7 +1100,7 @@ func BeyondTimeRetention(db *DB, blocks []*Block) (deletable map[ulid.ULID]struc // set in the db options. func BeyondSizeRetention(db *DB, blocks []*Block) (deletable map[ulid.ULID]struct{}) { // Size retention is disabled or no blocks to work with. - if len(db.blocks) == 0 || db.opts.MaxBytes <= 0 { + if len(blocks) == 0 || db.opts.MaxBytes <= 0 { return } From d9cd913219eb079f135209f19e06289672af1863 Mon Sep 17 00:00:00 2001 From: Ganesh Vernekar <15064823+codesome@users.noreply.github.com> Date: Mon, 19 Oct 2020 17:17:10 +0530 Subject: [PATCH 04/65] Add links to TSDB blog post series in tsdb/README.md (#8078) Signed-off-by: Ganesh Vernekar --- tsdb/README.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/tsdb/README.md b/tsdb/README.md index 61f8670882..248004b9d3 100644 --- a/tsdb/README.md +++ b/tsdb/README.md @@ -2,7 +2,7 @@ [![GoDoc](https://godoc.org/github.com/prometheus/prometheus/tsdb?status.svg)](https://godoc.org/github.com/prometheus/prometheus/tsdb) -This repository contains the Prometheus storage layer that is used in its 2.x releases. +This directory contains the Prometheus storage layer that is used in its 2.x releases. A writeup of its design can be found [here](https://fabxc.org/blog/2017-04-10-writing-a-tsdb/). @@ -11,3 +11,9 @@ Based on the Gorilla TSDB [white papers](http://www.vldb.org/pvldb/vol8/p1816-te Video: [Storing 16 Bytes at Scale](https://youtu.be/b_pEevMAC3I) from [PromCon 2017](https://promcon.io/2017-munich/). See also the [format documentation](docs/format/README.md). + +A series of blog posts explaining different components of TSDB: +* [The Head Block](https://ganeshvernekar.com/blog/prometheus-tsdb-the-head-block/) +* [WAL and Checkpoint](https://ganeshvernekar.com/blog/prometheus-tsdb-wal-and-checkpoint/) +* [Memory Mapping of Head Chunks from Disk](https://ganeshvernekar.com/blog/prometheus-tsdb-mmapping-head-chunks-from-disk/) +* [Persistent Block and its Index](https://ganeshvernekar.com/blog/prometheus-tsdb-persistent-block-and-its-index/) \ No newline at end of file From 2fe1e9fa93772fd831b1c040968bb748eac12795 Mon Sep 17 00:00:00 2001 From: Bartlomiej Plotka Date: Mon, 19 Oct 2020 17:27:08 +0200 Subject: [PATCH 05/65] Create a checkpoint only at the end of Compact call (#8067) * Create a checkpoint only at the end of Compact call Signed-off-by: Ganesh Vernekar * Fix review comments Signed-off-by: Ganesh Vernekar * Fix Bartek's offline reviews Signed-off-by: Ganesh Vernekar * Introduce TruncateInMemory and TruncateWAL Signed-off-by: Ganesh Vernekar * Small enhancements and test fixing attempts Signed-off-by: Ganesh Vernekar * Fix tests Signed-off-by: Ganesh Vernekar * Add TestOneCheckpointPerCompactCall Signed-off-by: Ganesh Vernekar * Don't truncate WAL on block compaction Signed-off-by: Ganesh Vernekar * Simplified the algo. Signed-off-by: Bartlomiej Plotka * Better protection around calling truncateWAL, truncate WAL on Head compaction error Signed-off-by: Ganesh Vernekar Co-authored-by: Ganesh Vernekar --- tsdb/block.go | 2 +- tsdb/compact_test.go | 12 ++-- tsdb/db.go | 122 ++++++++++++++++++++++---------------- tsdb/db_test.go | 135 ++++++++++++++++++++++++++++++++++++++++--- tsdb/head.go | 47 +++++++++++---- tsdb/head_test.go | 6 +- 6 files changed, 247 insertions(+), 77 deletions(-) diff --git a/tsdb/block.go b/tsdb/block.go index 0df30e846c..c6de088d33 100644 --- a/tsdb/block.go +++ b/tsdb/block.go @@ -172,7 +172,7 @@ type BlockMetaCompaction struct { // ULIDs of all source head blocks that went into the block. Sources []ulid.ULID `json:"sources,omitempty"` // Indicates that during compaction it resulted in a block without any samples - // so it should be deleted on the next reload. + // so it should be deleted on the next reloadBlocks. Deletable bool `json:"deletable,omitempty"` // Short descriptions of the direct blocks that were used to create // this block. diff --git a/tsdb/compact_test.go b/tsdb/compact_test.go index 160c25fff2..9ba0c64499 100644 --- a/tsdb/compact_test.go +++ b/tsdb/compact_test.go @@ -1235,7 +1235,7 @@ func TestCancelCompactions(t *testing.T) { } } -// TestDeleteCompactionBlockAfterFailedReload ensures that a failed reload immediately after a compaction +// TestDeleteCompactionBlockAfterFailedReload ensures that a failed reloadBlocks immediately after a compaction // deletes the resulting block to avoid creatings blocks with the same time range. func TestDeleteCompactionBlockAfterFailedReload(t *testing.T) { tests := map[string]func(*DB) int{ @@ -1265,7 +1265,7 @@ func TestDeleteCompactionBlockAfterFailedReload(t *testing.T) { createBlock(t, db.Dir(), genSeries(1, 1, m.MinTime, m.MaxTime)) } testutil.Ok(t, db.reload()) - testutil.Equals(t, len(blocks), len(db.Blocks()), "unexpected block count after a reload") + testutil.Equals(t, len(blocks), len(db.Blocks()), "unexpected block count after a reloadBlocks") return len(blocks) }, @@ -1281,7 +1281,7 @@ func TestDeleteCompactionBlockAfterFailedReload(t *testing.T) { expBlocks := bootStrap(db) - // Create a block that will trigger the reload to fail. + // Create a block that will trigger the reloadBlocks to fail. blockPath := createBlock(t, db.Dir(), genSeries(1, 1, 200, 300)) lastBlockIndex := path.Join(blockPath, indexFilename) actBlocks, err := blockDirs(db.Dir()) @@ -1289,15 +1289,15 @@ func TestDeleteCompactionBlockAfterFailedReload(t *testing.T) { testutil.Equals(t, expBlocks, len(actBlocks)-1) // -1 to exclude the corrupted block. testutil.Ok(t, os.RemoveAll(lastBlockIndex)) // Corrupt the block by removing the index file. - testutil.Equals(t, 0.0, prom_testutil.ToFloat64(db.metrics.reloadsFailed), "initial 'failed db reload' count metrics mismatch") + testutil.Equals(t, 0.0, prom_testutil.ToFloat64(db.metrics.reloadsFailed), "initial 'failed db reloadBlocks' count metrics mismatch") testutil.Equals(t, 0.0, prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.ran), "initial `compactions` count metric mismatch") testutil.Equals(t, 0.0, prom_testutil.ToFloat64(db.metrics.compactionsFailed), "initial `compactions failed` count metric mismatch") // Do the compaction and check the metrics. - // Compaction should succeed, but the reload should fail and + // Compaction should succeed, but the reloadBlocks should fail and // the new block created from the compaction should be deleted. testutil.NotOk(t, db.Compact()) - testutil.Equals(t, 1.0, prom_testutil.ToFloat64(db.metrics.reloadsFailed), "'failed db reload' count metrics mismatch") + testutil.Equals(t, 1.0, prom_testutil.ToFloat64(db.metrics.reloadsFailed), "'failed db reloadBlocks' count metrics mismatch") testutil.Equals(t, 1.0, prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.ran), "`compaction` count metric mismatch") testutil.Equals(t, 1.0, prom_testutil.ToFloat64(db.metrics.compactionsFailed), "`compactions failed` count metric mismatch") diff --git a/tsdb/db.go b/tsdb/db.go index 9d8308f066..94cd401bb6 100644 --- a/tsdb/db.go +++ b/tsdb/db.go @@ -216,7 +216,7 @@ func newDBMetrics(db *DB, r prometheus.Registerer) *dbMetrics { }) m.reloadsFailed = prometheus.NewCounter(prometheus.CounterOpts{ Name: "prometheus_tsdb_reloads_failures_total", - Help: "Number of times the database failed to reload block data from disk.", + Help: "Number of times the database failed to reloadBlocks block data from disk.", }) m.compactionsTriggered = prometheus.NewCounter(prometheus.CounterOpts{ Name: "prometheus_tsdb_compactions_triggered_total", @@ -769,20 +769,26 @@ func (a dbAppender) Commit() error { } // Compact data if possible. After successful compaction blocks are reloaded -// which will also trigger blocks to be deleted that fall out of the retention -// window. -// If no blocks are compacted, the retention window state doesn't change. Thus, -// this is sufficient to reliably delete old data. -// Old blocks are only deleted on reload based on the new block's parent information. -// See DB.reload documentation for further information. -func (db *DB) Compact() (err error) { +// which will also delete the blocks that fall out of the retention window. +// Old blocks are only deleted on reloadBlocks based on the new block's parent information. +// See DB.reloadBlocks documentation for further information. +func (db *DB) Compact() (returnErr error) { db.cmtx.Lock() defer db.cmtx.Unlock() defer func() { - if err != nil { + if returnErr != nil { db.metrics.compactionsFailed.Inc() } }() + + lastBlockMaxt := int64(math.MinInt64) + defer func() { + var merr tsdb_errors.MultiError + merr.Add(returnErr) + merr.Add(errors.Wrap(db.head.truncateWAL(lastBlockMaxt), "WAL truncation in Compact defer")) + returnErr = merr.Err() + }() + // Check whether we have pending head blocks that are ready to be persisted. // They have the highest priority. for { @@ -804,55 +810,59 @@ func (db *DB) Compact() (err error) { // so in order to make sure that overlaps are evaluated // consistently, we explicitly remove the last value // from the block interval here. - head := NewRangeHead(db.head, mint, maxt-1) - if err := db.compactHead(head); err != nil { - return err + if err := db.compactHead(NewRangeHead(db.head, mint, maxt-1)); err != nil { + return errors.Wrap(err, "compact head") } + // Consider only successful compactions for WAL truncation. + lastBlockMaxt = maxt + } + + // Clear some disk space before compacting blocks, especially important + // when Head compaction happened over a long time range. + if err := db.head.truncateWAL(lastBlockMaxt); err != nil { + return errors.Wrap(err, "WAL truncation in Compact") } return db.compactBlocks() } -// CompactHead compacts the given the RangeHead. -func (db *DB) CompactHead(head *RangeHead) (err error) { +// CompactHead compacts the given RangeHead. +func (db *DB) CompactHead(head *RangeHead) error { db.cmtx.Lock() defer db.cmtx.Unlock() - return db.compactHead(head) + if err := db.compactHead(head); err != nil { + return errors.Wrap(err, "compact head") + } + + if err := db.head.truncateWAL(head.BlockMaxTime()); err != nil { + return errors.Wrap(err, "WAL truncation") + } + return nil } -// compactHead compacts the given the RangeHead. +// compactHead compacts the given RangeHead. // The compaction mutex should be held before calling this method. -func (db *DB) compactHead(head *RangeHead) (err error) { - // Add +1 millisecond to block maxt because block intervals are half-open: [b.MinTime, b.MaxTime). - // Because of this block intervals are always +1 than the total samples it includes. - maxt := head.MaxTime() + 1 - uid, err := db.compactor.Write(db.dir, head, head.MinTime(), maxt, nil) +func (db *DB) compactHead(head *RangeHead) error { + uid, err := db.compactor.Write(db.dir, head, head.MinTime(), head.BlockMaxTime(), nil) if err != nil { return errors.Wrap(err, "persist head block") } runtime.GC() - - if err := db.reload(); err != nil { + if err := db.reloadBlocks(); err != nil { if errRemoveAll := os.RemoveAll(filepath.Join(db.dir, uid.String())); errRemoveAll != nil { var merr tsdb_errors.MultiError - merr.Add(errors.Wrap(err, "reload blocks")) - merr.Add(errors.Wrapf(errRemoveAll, "delete persisted head block after failed db reload:%s", uid)) + merr.Add(errors.Wrap(err, "reloadBlocks blocks")) + merr.Add(errors.Wrapf(errRemoveAll, "delete persisted head block after failed db reloadBlocks:%s", uid)) return merr.Err() } - return errors.Wrap(err, "reload blocks") + return errors.Wrap(err, "reloadBlocks blocks") } - if (uid == ulid.ULID{}) { - // Compaction resulted in an empty block. - // Head truncating during db.reload() depends on the persisted blocks and - // in this case no new block will be persisted so manually truncate the head. - if err = db.head.Truncate(maxt); err != nil { - return errors.Wrap(err, "head truncate failed (in compact)") - } + if err = db.head.truncateMemory(head.BlockMaxTime()); err != nil { + return errors.Wrap(err, "head memory truncate") } runtime.GC() - return nil } @@ -881,11 +891,11 @@ func (db *DB) compactBlocks() (err error) { } runtime.GC() - if err := db.reload(); err != nil { + if err := db.reloadBlocks(); err != nil { if err := os.RemoveAll(filepath.Join(db.dir, uid.String())); err != nil { - return errors.Wrapf(err, "delete compacted block after failed db reload:%s", uid) + return errors.Wrapf(err, "delete compacted block after failed db reloadBlocks:%s", uid) } - return errors.Wrap(err, "reload blocks") + return errors.Wrap(err, "reloadBlocks blocks") } runtime.GC() } @@ -904,9 +914,23 @@ func getBlock(allBlocks []*Block, id ulid.ULID) (*Block, bool) { return nil, false } -// reload blocks and trigger head truncation if new blocks appeared. +// reload reloads blocks and truncates the head and its WAL. +func (db *DB) reload() error { + if err := db.reloadBlocks(); err != nil { + return errors.Wrap(err, "reloadBlocks") + } + if len(db.blocks) == 0 { + return nil + } + if err := db.head.Truncate(db.blocks[len(db.blocks)-1].MaxTime()); err != nil { + return errors.Wrap(err, "head truncate") + } + return nil +} + +// reloadBlocks reloads blocks without touching head. // Blocks that are obsolete due to replacement or retention will be deleted. -func (db *DB) reload() (err error) { +func (db *DB) reloadBlocks() (err error) { defer func() { if err != nil { db.metrics.reloadsFailed.Inc() @@ -989,7 +1013,7 @@ func (db *DB) reload() (err error) { blockMetas = append(blockMetas, b.Meta()) } if overlaps := OverlappingBlocks(blockMetas); len(overlaps) > 0 { - level.Warn(db.logger).Log("msg", "Overlapping blocks found during reload", "detail", overlaps.String()) + level.Warn(db.logger).Log("msg", "Overlapping blocks found during reloadBlocks", "detail", overlaps.String()) } // Append blocks to old, deletable blocks, so we can close them. @@ -999,15 +1023,9 @@ func (db *DB) reload() (err error) { } } if err := db.deleteBlocks(deletable); err != nil { - return err + return errors.Wrapf(err, "delete %v blocks", len(deletable)) } - - // Garbage collect data in the head if the most recent persisted block - // covers data of its current time range. - if len(toLoad) == 0 { - return nil - } - return errors.Wrap(db.head.Truncate(toLoad[len(toLoad)-1].Meta().MaxTime), "head truncate failed") + return nil } func openBlocks(l log.Logger, dir string, loaded []*Block, chunkPool chunkenc.Pool) (blocks []*Block, corrupted map[ulid.ULID]error, err error) { @@ -1020,7 +1038,7 @@ func openBlocks(l log.Logger, dir string, loaded []*Block, chunkPool chunkenc.Po for _, bDir := range bDirs { meta, _, err := readMetaFile(bDir) if err != nil { - level.Error(l).Log("msg", "Failed to read meta.json for a block during reload. Skipping", "dir", bDir, "err", err) + level.Error(l).Log("msg", "Failed to read meta.json for a block during reloadBlocks. Skipping", "dir", bDir, "err", err) continue } @@ -1502,7 +1520,11 @@ func (db *DB) CleanTombstones() (err error) { newUIDs = append(newUIDs, *uid) } } - return errors.Wrap(db.reload(), "reload blocks") + + if err := db.reloadBlocks(); err != nil { + return errors.Wrap(err, "reload blocks") + } + return nil } func isBlockDir(fi os.FileInfo) bool { diff --git a/tsdb/db_test.go b/tsdb/db_test.go index e1969c5127..023db63af0 100644 --- a/tsdb/db_test.go +++ b/tsdb/db_test.go @@ -151,7 +151,7 @@ func TestDB_reloadOrder(t *testing.T) { createBlock(t, db.Dir(), genSeries(1, 1, m.MinTime, m.MaxTime)) } - testutil.Ok(t, db.reload()) + testutil.Ok(t, db.reloadBlocks()) blocks := db.Blocks() testutil.Equals(t, 3, len(blocks)) testutil.Equals(t, metas[1].MinTime, blocks[0].Meta().MinTime) @@ -1195,11 +1195,11 @@ func TestTimeRetention(t *testing.T) { createBlock(t, db.Dir(), genSeries(10, 10, m.MinTime, m.MaxTime)) } - testutil.Ok(t, db.reload()) // Reload the db to register the new blocks. + testutil.Ok(t, db.reloadBlocks()) // Reload the db to register the new blocks. testutil.Equals(t, len(blocks), len(db.Blocks())) // Ensure all blocks are registered. db.opts.RetentionDuration = blocks[2].MaxTime - blocks[1].MinTime - testutil.Ok(t, db.reload()) + testutil.Ok(t, db.reloadBlocks()) expBlocks := blocks[1:] actBlocks := db.Blocks() @@ -1249,7 +1249,7 @@ func TestSizeRetention(t *testing.T) { testutil.Ok(t, headApp.Commit()) // Test that registered size matches the actual disk size. - testutil.Ok(t, db.reload()) // Reload the db to register the new db size. + testutil.Ok(t, db.reloadBlocks()) // Reload the db to register the new db size. testutil.Equals(t, len(blocks), len(db.Blocks())) // Ensure all blocks are registered. blockSize := int64(prom_testutil.ToFloat64(db.metrics.blocksBytes)) // Use the actual internal metrics. walSize, err := db.Head().wal.Size() @@ -1277,8 +1277,8 @@ func TestSizeRetention(t *testing.T) { // Check total size, total count and check that the oldest block was deleted. firstBlockSize := db.Blocks()[0].Size() sizeLimit := actSize - firstBlockSize - db.opts.MaxBytes = sizeLimit // Set the new db size limit one block smaller that the actual size. - testutil.Ok(t, db.reload()) // Reload the db to register the new db size. + db.opts.MaxBytes = sizeLimit // Set the new db size limit one block smaller that the actual size. + testutil.Ok(t, db.reloadBlocks()) // Reload the db to register the new db size. expBlocks := blocks[1:] actBlocks := db.Blocks() @@ -1811,7 +1811,7 @@ func TestNoEmptyBlocks(t *testing.T) { } oldBlocks := db.Blocks() - testutil.Ok(t, db.reload()) // Reload the db to register the new blocks. + testutil.Ok(t, db.reloadBlocks()) // Reload the db to register the new blocks. testutil.Equals(t, len(blocks)+len(oldBlocks), len(db.Blocks())) // Ensure all blocks are registered. testutil.Ok(t, db.Delete(math.MinInt64, math.MaxInt64, defaultMatcher)) testutil.Ok(t, db.Compact()) @@ -2838,3 +2838,124 @@ func TestOpen_VariousBlockStates(t *testing.T) { } testutil.Equals(t, len(expectedIgnoredDirs), ignored) } + +func TestOneCheckpointPerCompactCall(t *testing.T) { + blockRange := int64(1000) + tsdbCfg := &Options{ + RetentionDuration: blockRange * 1000, + NoLockfile: true, + MinBlockDuration: blockRange, + MaxBlockDuration: blockRange, + } + + tmpDir, err := ioutil.TempDir("", "test") + testutil.Ok(t, err) + t.Cleanup(func() { + testutil.Ok(t, os.RemoveAll(tmpDir)) + }) + + db, err := Open(tmpDir, log.NewNopLogger(), prometheus.NewRegistry(), tsdbCfg) + testutil.Ok(t, err) + t.Cleanup(func() { + testutil.Ok(t, db.Close()) + }) + db.DisableCompactions() + + // Case 1: Lot's of uncompacted data in Head. + + lbls := labels.Labels{labels.Label{Name: "foo_d", Value: "choco_bar"}} + // Append samples spanning 59 block ranges. + app := db.Appender(context.Background()) + for i := int64(0); i < 60; i++ { + _, err := app.Add(lbls, blockRange*i, rand.Float64()) + testutil.Ok(t, err) + _, err = app.Add(lbls, (blockRange*i)+blockRange/2, rand.Float64()) + testutil.Ok(t, err) + // Rotate the WAL file so that there is >3 files for checkpoint to happen. + testutil.Ok(t, db.head.wal.NextSegment()) + } + testutil.Ok(t, app.Commit()) + + // Check the existing WAL files. + first, last, err := wal.Segments(db.head.wal.Dir()) + testutil.Ok(t, err) + testutil.Equals(t, 0, first) + testutil.Equals(t, 60, last) + + testutil.Equals(t, 0.0, prom_testutil.ToFloat64(db.head.metrics.checkpointCreationTotal)) + testutil.Ok(t, db.Compact()) + testutil.Equals(t, 1.0, prom_testutil.ToFloat64(db.head.metrics.checkpointCreationTotal)) + + // As the data spans for 59 blocks, 58 go to disk and 1 remains in Head. + testutil.Equals(t, 58, len(db.Blocks())) + // Though WAL was truncated only once, head should be truncated after each compaction. + testutil.Equals(t, 58.0, prom_testutil.ToFloat64(db.head.metrics.headTruncateTotal)) + + // The compaction should have only truncated first 2/3 of WAL (while also rotating the files). + first, last, err = wal.Segments(db.head.wal.Dir()) + testutil.Ok(t, err) + testutil.Equals(t, 40, first) + testutil.Equals(t, 61, last) + + // The first checkpoint would be for first 2/3rd of WAL, hence till 39. + // That should be the last checkpoint. + _, cno, err := wal.LastCheckpoint(db.head.wal.Dir()) + testutil.Ok(t, err) + testutil.Equals(t, 39, cno) + + // Case 2: Old blocks on disk. + // The above blocks will act as old blocks. + + // Creating a block to cover the data in the Head so that + // Head will skip the data during replay and start fresh. + blocks := db.Blocks() + newBlockMint := blocks[len(blocks)-1].Meta().MaxTime + newBlockMaxt := db.Head().MaxTime() + 1 + testutil.Ok(t, db.Close()) + + createBlock(t, db.dir, genSeries(1, 1, newBlockMint, newBlockMaxt)) + + db, err = Open(db.dir, log.NewNopLogger(), prometheus.NewRegistry(), tsdbCfg) + testutil.Ok(t, err) + db.DisableCompactions() + + // 1 block more. + testutil.Equals(t, 59, len(db.Blocks())) + // No series in Head because of this new block. + testutil.Equals(t, 0, int(db.head.NumSeries())) + + // Adding sample way into the future. + app = db.Appender(context.Background()) + _, err = app.Add(lbls, blockRange*120, rand.Float64()) + testutil.Ok(t, err) + testutil.Ok(t, app.Commit()) + + // The mint of head is the last block maxt, that means the gap between mint and maxt + // of Head is too large. This will trigger many compactions. + testutil.Equals(t, newBlockMaxt, db.head.MinTime()) + + // Another WAL file was rotated. + first, last, err = wal.Segments(db.head.wal.Dir()) + testutil.Ok(t, err) + testutil.Equals(t, 40, first) + testutil.Equals(t, 62, last) + + testutil.Equals(t, 0.0, prom_testutil.ToFloat64(db.head.metrics.checkpointCreationTotal)) + testutil.Ok(t, db.Compact()) + testutil.Equals(t, 1.0, prom_testutil.ToFloat64(db.head.metrics.checkpointCreationTotal)) + + // No new blocks should be created as there was not data in between the new samples and the blocks. + testutil.Equals(t, 59, len(db.Blocks())) + + // The compaction should have only truncated first 2/3 of WAL (while also rotating the files). + first, last, err = wal.Segments(db.head.wal.Dir()) + testutil.Ok(t, err) + testutil.Equals(t, 55, first) + testutil.Equals(t, 63, last) + + // The first checkpoint would be for first 2/3rd of WAL, hence till 54. + // That should be the last checkpoint. + _, cno, err = wal.LastCheckpoint(db.head.wal.Dir()) + testutil.Ok(t, err) + testutil.Equals(t, 54, cno) +} diff --git a/tsdb/head.go b/tsdb/head.go index 3a577763d6..56b597f3df 100644 --- a/tsdb/head.go +++ b/tsdb/head.go @@ -52,11 +52,12 @@ var ( // Head handles reads and writes of time series data within a time window. type Head struct { - chunkRange atomic.Int64 - numSeries atomic.Uint64 - minTime, maxTime atomic.Int64 // Current min and max of the samples included in the head. - minValidTime atomic.Int64 // Mint allowed to be added to the head. It shouldn't be lower than the maxt of the last persisted block. - lastSeriesID atomic.Uint64 + chunkRange atomic.Int64 + numSeries atomic.Uint64 + minTime, maxTime atomic.Int64 // Current min and max of the samples included in the head. + minValidTime atomic.Int64 // Mint allowed to be added to the head. It shouldn't be lower than the maxt of the last persisted block. + lastWALTruncationTime atomic.Int64 + lastSeriesID atomic.Uint64 metrics *headMetrics wal *wal.WAL @@ -323,6 +324,7 @@ func NewHead(r prometheus.Registerer, l log.Logger, wal *wal.WAL, chunkRange int h.chunkRange.Store(chunkRange) h.minTime.Store(math.MaxInt64) h.maxTime.Store(math.MinInt64) + h.lastWALTruncationTime.Store(math.MinInt64) h.metrics = newHeadMetrics(h, r) if pool == nil { @@ -776,8 +778,20 @@ func (h *Head) removeCorruptedMmappedChunks(err error) map[uint64][]*mmappedChun return mmappedChunks } -// Truncate removes old data before mint from the head. +// Truncate removes old data before mint from the head and WAL. func (h *Head) Truncate(mint int64) (err error) { + initialize := h.MinTime() == math.MaxInt64 + if err := h.truncateMemory(mint); err != nil { + return err + } + if initialize { + return nil + } + return h.truncateWAL(mint) +} + +// truncateMemory removes old data before mint from the head. +func (h *Head) truncateMemory(mint int64) (err error) { defer func() { if err != nil { h.metrics.headTruncateFail.Inc() @@ -813,11 +827,16 @@ func (h *Head) Truncate(mint int64) (err error) { if err := h.chunkDiskMapper.Truncate(mint); err != nil { return errors.Wrap(err, "truncate chunks.HeadReadWriter") } + return nil +} - if h.wal == nil { +// truncateWAL removes old data before mint from the WAL. +func (h *Head) truncateWAL(mint int64) error { + if h.wal == nil || mint <= h.lastWALTruncationTime.Load() { return nil } - start = time.Now() + start := time.Now() + h.lastWALTruncationTime.Store(mint) first, last, err := wal.Segments(h.wal.Dir()) if err != nil { @@ -825,8 +844,7 @@ func (h *Head) Truncate(mint int64) (err error) { } // Start a new segment, so low ingestion volume TSDB don't have more WAL than // needed. - err = h.wal.NextSegment() - if err != nil { + if err := h.wal.NextSegment(); err != nil { return errors.Wrap(err, "next segment") } last-- // Never consider last segment for checkpoint. @@ -950,10 +968,19 @@ func (h *RangeHead) MinTime() int64 { return h.mint } +// MaxTime returns the max time of actual data fetch-able from the head. +// This controls the chunks time range which is closed [b.MinTime, b.MaxTime]. func (h *RangeHead) MaxTime() int64 { return h.maxt } +// BlockMaxTime returns the max time of the potential block created from this head. +// It's different to MaxTime as we need to add +1 millisecond to block maxt because block +// intervals are half-open: [b.MinTime, b.MaxTime). Block intervals are always +1 than the total samples it includes. +func (h *RangeHead) BlockMaxTime() int64 { + return h.MaxTime() + 1 +} + func (h *RangeHead) NumSeries() uint64 { return h.head.NumSeries() } diff --git a/tsdb/head_test.go b/tsdb/head_test.go index 75e20add9b..62ab33baa3 100644 --- a/tsdb/head_test.go +++ b/tsdb/head_test.go @@ -579,14 +579,14 @@ func TestHeadDeleteSimple(t *testing.T) { } testutil.Ok(t, app.Commit()) - // Compare the samples for both heads - before and after the reload. - reloadedW, err := wal.New(nil, nil, w.Dir(), compress) // Use a new wal to ensure deleted samples are gone even after a reload. + // Compare the samples for both heads - before and after the reloadBlocks. + reloadedW, err := wal.New(nil, nil, w.Dir(), compress) // Use a new wal to ensure deleted samples are gone even after a reloadBlocks. testutil.Ok(t, err) reloadedHead, err := NewHead(nil, nil, reloadedW, 1000, reloadedW.Dir(), nil, DefaultStripeSize, nil) testutil.Ok(t, err) testutil.Ok(t, reloadedHead.Init(0)) - // Compare the query results for both heads - before and after the reload. + // Compare the query results for both heads - before and after the reloadBlocks. Outer: for _, h := range []*Head{head, reloadedHead} { q, err := NewBlockQuerier(h, h.MinTime(), h.MaxTime()) From 13ba013a2451afdb3e52f59f429d74da7ee0e84b Mon Sep 17 00:00:00 2001 From: Matthias Loibl Date: Tue, 20 Oct 2020 11:42:30 +0200 Subject: [PATCH 06/65] Use absolute jsonnet import paths This should be the way forward when importing libraries in jsonnet. It's closer to how Go imports look and makes it more obvious where packages live. This is not breaking anything, as the old imports were already symlinks to the now directly used directories. Signed-off-by: Matthias Loibl --- documentation/prometheus-mixin/dashboards.libsonnet | 4 ++-- documentation/prometheus-mixin/jsonnetfile.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/documentation/prometheus-mixin/dashboards.libsonnet b/documentation/prometheus-mixin/dashboards.libsonnet index 1c4521b0a0..31cea10214 100644 --- a/documentation/prometheus-mixin/dashboards.libsonnet +++ b/documentation/prometheus-mixin/dashboards.libsonnet @@ -1,5 +1,5 @@ -local g = import 'grafana-builder/grafana.libsonnet'; -local grafana = import 'grafonnet/grafana.libsonnet'; +local grafana = import 'github.com/grafana/grafonnet-lib/grafonnet/grafana.libsonnet'; +local g = import 'github.com/grafana/jsonnet-libs/grafana-builder/grafana.libsonnet'; local dashboard = grafana.dashboard; local row = grafana.row; local singlestat = grafana.singlestat; diff --git a/documentation/prometheus-mixin/jsonnetfile.json b/documentation/prometheus-mixin/jsonnetfile.json index c45a96b03b..1c64fd0151 100644 --- a/documentation/prometheus-mixin/jsonnetfile.json +++ b/documentation/prometheus-mixin/jsonnetfile.json @@ -20,5 +20,5 @@ "version": "master" } ], - "legacyImports": true + "legacyImports": false } From fa8722be4a9d80c995c12317c9c5f11cbc1d6845 Mon Sep 17 00:00:00 2001 From: Ricky Pai Date: Wed, 21 Oct 2020 00:37:18 -0700 Subject: [PATCH 07/65] add link to circleci config in README (#8089) Signed-off-by: Ricky Pai --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f529864107..87f35bf3e8 100644 --- a/README.md +++ b/README.md @@ -102,7 +102,7 @@ For more information on building, running, and developing on the new React-based ## More information * The source code is periodically indexed: [Prometheus Core](https://godoc.org/github.com/prometheus/prometheus). - * You will find a CircleCI configuration in `.circleci/config.yml`. + * You will find a CircleCI configuration in [`.circleci/config.yml`](.circleci/config.yml). * See the [Community page](https://prometheus.io/community) for how to reach the Prometheus developers and users on various communication channels. ## Contributing From 2624d827fadc604323db979e1d7b59415531f3d3 Mon Sep 17 00:00:00 2001 From: Ganesh Vernekar <15064823+codesome@users.noreply.github.com> Date: Wed, 21 Oct 2020 18:27:13 +0530 Subject: [PATCH 08/65] Read repair empty last file in chunks_head (#8061) * Read repair empty file in chunks_head Signed-off-by: Ganesh Vernekar * Refactor and introduce repairLastChunkFile Signed-off-by: Ganesh Vernekar * Attempt windows test fix Signed-off-by: Ganesh Vernekar * Fix review comments Signed-off-by: Ganesh Vernekar * Fix review comments Signed-off-by: Ganesh Vernekar --- tsdb/chunks/head_chunks.go | 36 +++++++++++++++++ tsdb/chunks/head_chunks_test.go | 70 +++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) diff --git a/tsdb/chunks/head_chunks.go b/tsdb/chunks/head_chunks.go index 632682218c..154149d0cc 100644 --- a/tsdb/chunks/head_chunks.go +++ b/tsdb/chunks/head_chunks.go @@ -167,6 +167,11 @@ func (cdm *ChunkDiskMapper) openMMapFiles() (returnErr error) { return err } + files, err = repairLastChunkFile(files) + if err != nil { + return err + } + chkFileIndices := make([]int, 0, len(files)) for seq, fn := range files { f, err := fileutil.OpenMmapFile(fn) @@ -226,9 +231,40 @@ func listChunkFiles(dir string) (map[int]string, error) { } res[int(seq)] = filepath.Join(dir, fi.Name()) } + return res, nil } +// repairLastChunkFile deletes the last file if it's empty. +// Because we don't fsync when creating these file, we could end +// up with an empty file at the end during an abrupt shutdown. +func repairLastChunkFile(files map[int]string) (_ map[int]string, returnErr error) { + lastFile := -1 + for seq := range files { + if seq > lastFile { + lastFile = seq + } + } + + if lastFile <= 0 { + return files, nil + } + + info, err := os.Stat(files[lastFile]) + if err != nil { + return files, errors.Wrap(err, "file stat during last head chunk file repair") + } + if info.Size() == 0 { + // Corrupt file, hence remove it. + if err := os.RemoveAll(files[lastFile]); err != nil { + return files, errors.Wrap(err, "delete corrupted, empty head chunk file during last file repair") + } + delete(files, lastFile) + } + + return files, nil +} + // WriteChunk writes the chunk to the disk. // The returned chunk ref is the reference from where the chunk encoding starts for the chunk. func (cdm *ChunkDiskMapper) WriteChunk(seriesRef uint64, mint, maxt int64, chk chunkenc.Chunk) (chkRef uint64, err error) { diff --git a/tsdb/chunks/head_chunks_test.go b/tsdb/chunks/head_chunks_test.go index bfb4262ad1..f8ed26cf1e 100644 --- a/tsdb/chunks/head_chunks_test.go +++ b/tsdb/chunks/head_chunks_test.go @@ -19,6 +19,7 @@ import ( "io/ioutil" "math/rand" "os" + "strconv" "testing" "github.com/prometheus/prometheus/tsdb/chunkenc" @@ -363,6 +364,75 @@ func TestHeadReadWriter_TruncateAfterFailedIterateChunks(t *testing.T) { testutil.Ok(t, hrw.Truncate(2000)) } +func TestHeadReadWriter_ReadRepairOnEmptyLastFile(t *testing.T) { + hrw := testHeadReadWriter(t) + defer func() { + testutil.Ok(t, hrw.Close()) + }() + + timeRange := 0 + addChunk := func() { + step := 100 + mint, maxt := timeRange+1, timeRange+step-1 + _, err := hrw.WriteChunk(1, int64(mint), int64(maxt), randomChunk(t)) + testutil.Ok(t, err) + timeRange += step + } + nonEmptyFile := func() { + testutil.Ok(t, hrw.CutNewFile()) + addChunk() + } + + addChunk() // 1. Created with the first chunk. + nonEmptyFile() // 2. + nonEmptyFile() // 3. + + testutil.Equals(t, 3, len(hrw.mmappedChunkFiles)) + lastFile := 0 + for idx := range hrw.mmappedChunkFiles { + if idx > lastFile { + lastFile = idx + } + } + testutil.Equals(t, 3, lastFile) + dir := hrw.dir.Name() + testutil.Ok(t, hrw.Close()) + + // Write an empty last file mimicking an abrupt shutdown on file creation. + emptyFileName := segmentFile(dir, lastFile+1) + f, err := os.OpenFile(emptyFileName, os.O_WRONLY|os.O_CREATE, 0666) + testutil.Ok(t, err) + testutil.Ok(t, f.Sync()) + stat, err := f.Stat() + testutil.Ok(t, err) + testutil.Equals(t, int64(0), stat.Size()) + testutil.Ok(t, f.Close()) + + // Open chunk disk mapper again, corrupt file should be removed. + hrw, err = NewChunkDiskMapper(dir, chunkenc.NewPool()) + testutil.Ok(t, err) + testutil.Assert(t, !hrw.fileMaxtSet, "") + testutil.Ok(t, hrw.IterateAllChunks(func(_, _ uint64, _, _ int64, _ uint16) error { return nil })) + testutil.Assert(t, hrw.fileMaxtSet, "") + + // Removed from memory. + testutil.Equals(t, 3, len(hrw.mmappedChunkFiles)) + for idx := range hrw.mmappedChunkFiles { + testutil.Assert(t, idx <= lastFile, "file index is bigger than previous last file") + } + + // Removed even from disk. + files, err := ioutil.ReadDir(dir) + testutil.Ok(t, err) + testutil.Equals(t, 3, len(files)) + for _, fi := range files { + seq, err := strconv.ParseUint(fi.Name(), 10, 64) + testutil.Ok(t, err) + testutil.Assert(t, seq <= uint64(lastFile), "file index on disk is bigger than previous last file") + } + +} + func testHeadReadWriter(t *testing.T) *ChunkDiskMapper { tmpdir, err := ioutil.TempDir("", "data") testutil.Ok(t, err) From ce4b3ac28217669a3f55af057cb538f1e29c7189 Mon Sep 17 00:00:00 2001 From: Ganesh Vernekar <15064823+codesome@users.noreply.github.com> Date: Wed, 21 Oct 2020 18:41:39 +0530 Subject: [PATCH 09/65] Simplify TestHeadReadWriter_Truncate (#7437) * Simplify TestHeadReadWriter_Truncate Signed-off-by: Ganesh Vernekar * Fix review comments Signed-off-by: Ganesh Vernekar --- tsdb/chunks/head_chunks_test.go | 115 ++++++++++++++------------------ 1 file changed, 49 insertions(+), 66 deletions(-) diff --git a/tsdb/chunks/head_chunks_test.go b/tsdb/chunks/head_chunks_test.go index f8ed26cf1e..3bc47f7bf6 100644 --- a/tsdb/chunks/head_chunks_test.go +++ b/tsdb/chunks/head_chunks_test.go @@ -26,8 +26,8 @@ import ( "github.com/prometheus/prometheus/util/testutil" ) -func TestHeadReadWriter_WriteChunk_Chunk_IterateChunks(t *testing.T) { - hrw := testHeadReadWriter(t) +func TestChunkDiskMapper_WriteChunk_Chunk_IterateChunks(t *testing.T) { + hrw := testChunkDiskMapper(t) defer func() { testutil.Ok(t, hrw.Close()) }() @@ -157,23 +157,20 @@ func TestHeadReadWriter_WriteChunk_Chunk_IterateChunks(t *testing.T) { } -// TestHeadReadWriter_Truncate tests +// TestChunkDiskMapper_Truncate tests // * If truncation is happening properly based on the time passed. // * The active file is not deleted even if the passed time makes it eligible to be deleted. // * Empty current file does not lead to creation of another file after truncation. // * Non-empty current file leads to creation of another file after truncation. -func TestHeadReadWriter_Truncate(t *testing.T) { - hrw := testHeadReadWriter(t) +func TestChunkDiskMapper_Truncate(t *testing.T) { + hrw := testChunkDiskMapper(t) defer func() { testutil.Ok(t, hrw.Close()) }() timeRange := 0 fileTimeStep := 100 - totalFiles := 7 - startIndexAfter1stTruncation, startIndexAfter2ndTruncation := 3, 6 - filesDeletedAfter1stTruncation, filesDeletedAfter2ndTruncation := 2, 5 - var timeToTruncate, timeToTruncateAfterRestart int64 + var thirdFileMinT, sixthFileMinT int64 addChunk := func() int { mint := timeRange + 1 // Just after the new file cut. @@ -188,51 +185,36 @@ func TestHeadReadWriter_Truncate(t *testing.T) { return mint } - cutFile := func(i int) { - testutil.Ok(t, hrw.CutNewFile()) - - mint := addChunk() - - if i == startIndexAfter1stTruncation { - timeToTruncate = int64(mint) - } else if i == startIndexAfter2ndTruncation { - timeToTruncateAfterRestart = int64(mint) - } - } - - // Cut segments. - for i := 1; i <= totalFiles; i++ { - cutFile(i) - } - - // Verifying the files. - verifyFiles := func(remainingFiles, startIndex int) { + verifyFiles := func(remainingFiles []int) { t.Helper() files, err := ioutil.ReadDir(hrw.dir.Name()) testutil.Ok(t, err) - testutil.Equals(t, remainingFiles, len(files), "files on disk") - testutil.Equals(t, remainingFiles, len(hrw.mmappedChunkFiles), "hrw.mmappedChunkFiles") - testutil.Equals(t, remainingFiles, len(hrw.closers), "closers") + testutil.Equals(t, len(remainingFiles), len(files), "files on disk") + testutil.Equals(t, len(remainingFiles), len(hrw.mmappedChunkFiles), "hrw.mmappedChunkFiles") + testutil.Equals(t, len(remainingFiles), len(hrw.closers), "closers") - for i := 1; i <= totalFiles; i++ { + for _, i := range remainingFiles { _, ok := hrw.mmappedChunkFiles[i] - if i < startIndex { - testutil.Equals(t, false, ok) - } else { - testutil.Equals(t, true, ok) - } + testutil.Equals(t, true, ok) } } - // Verify the number of segments. - verifyFiles(totalFiles, 1) + // Create segments 1 to 7. + for i := 1; i <= 7; i++ { + testutil.Ok(t, hrw.CutNewFile()) + mint := int64(addChunk()) + if i == 3 { + thirdFileMinT = mint + } else if i == 6 { + sixthFileMinT = mint + } + } + verifyFiles([]int{1, 2, 3, 4, 5, 6, 7}) // Truncating files. - testutil.Ok(t, hrw.Truncate(timeToTruncate)) - totalFiles++ // Truncation creates a new file as the last file is not empty. - verifyFiles(totalFiles-filesDeletedAfter1stTruncation, startIndexAfter1stTruncation) - addChunk() // Add a chunk so that new file is not truncated. + testutil.Ok(t, hrw.Truncate(thirdFileMinT)) + verifyFiles([]int{3, 4, 5, 6, 7, 8}) dir := hrw.dir.Name() testutil.Ok(t, hrw.Close()) @@ -246,29 +228,31 @@ func TestHeadReadWriter_Truncate(t *testing.T) { testutil.Ok(t, hrw.IterateAllChunks(func(_, _ uint64, _, _ int64, _ uint16) error { return nil })) testutil.Assert(t, hrw.fileMaxtSet, "") - // Truncating files after restart. As the last file was empty, this creates no new files. - testutil.Ok(t, hrw.Truncate(timeToTruncateAfterRestart)) - verifyFiles(totalFiles-filesDeletedAfter2ndTruncation, startIndexAfter2ndTruncation) - - // First chunk after restart creates a new file. + verifyFiles([]int{3, 4, 5, 6, 7, 8}) + // New file is created after restart even if last file was empty. + addChunk() + verifyFiles([]int{3, 4, 5, 6, 7, 8, 9}) + + // Truncating files after restart. + testutil.Ok(t, hrw.Truncate(sixthFileMinT)) + verifyFiles([]int{6, 7, 8, 9, 10}) + + // As the last file was empty, this creates no new files. + testutil.Ok(t, hrw.Truncate(sixthFileMinT+1)) + verifyFiles([]int{6, 7, 8, 9, 10}) addChunk() - totalFiles++ // Truncating till current time should not delete the current active file. - testutil.Ok(t, hrw.Truncate(int64(timeRange+fileTimeStep))) - verifyFiles(2, totalFiles) // One file is the active file and one was newly created. + testutil.Ok(t, hrw.Truncate(int64(timeRange+(2*fileTimeStep)))) + verifyFiles([]int{10, 11}) // One file is the previously active file and one currently created. } -// TestHeadReadWriter_Truncate_NoUnsequentialFiles tests -// that truncation leaves no unsequential files on disk, mainly under the following case -// * There is an empty file in between the sequence while the truncation -// deletes files only up to a sequence before that (i.e. stops deleting -// after it has found a file that is not deletable). -// This tests https://github.com/prometheus/prometheus/issues/7412 where -// the truncation used to check all the files for deletion and end up -// deleting empty files in between and breaking the sequence. -func TestHeadReadWriter_Truncate_NoUnsequentialFiles(t *testing.T) { - hrw := testHeadReadWriter(t) +// TestChunkDiskMapper_Truncate_PreservesFileSequence tests that truncation doesn't poke +// holes into the file sequence, even if there are empty files in between non-empty files. +// This test exposes https://github.com/prometheus/prometheus/issues/7412 where the truncation +// simply deleted all empty files instead of stopping once it encountered a non-empty file. +func TestChunkDiskMapper_Truncate_PreservesFileSequence(t *testing.T) { + hrw := testChunkDiskMapper(t) defer func() { testutil.Ok(t, hrw.Close()) }() @@ -296,7 +280,6 @@ func TestHeadReadWriter_Truncate_NoUnsequentialFiles(t *testing.T) { nonEmptyFile() // 5. emptyFile() // 6. - // Verifying the files. verifyFiles := func(remainingFiles []int) { t.Helper() @@ -308,7 +291,7 @@ func TestHeadReadWriter_Truncate_NoUnsequentialFiles(t *testing.T) { for _, i := range remainingFiles { _, ok := hrw.mmappedChunkFiles[i] - testutil.Equals(t, true, ok) + testutil.Assert(t, ok, "remaining file %d not in hrw.mmappedChunkFiles", i) } } @@ -339,7 +322,7 @@ func TestHeadReadWriter_Truncate_NoUnsequentialFiles(t *testing.T) { // TestHeadReadWriter_TruncateAfterIterateChunksError tests for // https://github.com/prometheus/prometheus/issues/7753 func TestHeadReadWriter_TruncateAfterFailedIterateChunks(t *testing.T) { - hrw := testHeadReadWriter(t) + hrw := testChunkDiskMapper(t) defer func() { testutil.Ok(t, hrw.Close()) }() @@ -365,7 +348,7 @@ func TestHeadReadWriter_TruncateAfterFailedIterateChunks(t *testing.T) { } func TestHeadReadWriter_ReadRepairOnEmptyLastFile(t *testing.T) { - hrw := testHeadReadWriter(t) + hrw := testChunkDiskMapper(t) defer func() { testutil.Ok(t, hrw.Close()) }() @@ -433,7 +416,7 @@ func TestHeadReadWriter_ReadRepairOnEmptyLastFile(t *testing.T) { } -func testHeadReadWriter(t *testing.T) *ChunkDiskMapper { +func testChunkDiskMapper(t *testing.T) *ChunkDiskMapper { tmpdir, err := ioutil.TempDir("", "data") testutil.Ok(t, err) t.Cleanup(func() { From fdf1c292245ea48fb3c013657c5699663036277f Mon Sep 17 00:00:00 2001 From: Brian Brazil Date: Wed, 21 Oct 2020 14:35:40 +0100 Subject: [PATCH 10/65] Fix metric description of prometheus_tsdb_symbol_table_size_bytes. (#8080) This is how much memory we use to load in the on-disk symbol tables, not the size of the tables themselves. Signed-off-by: Brian Brazil --- tsdb/db.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tsdb/db.go b/tsdb/db.go index 94cd401bb6..213a44122b 100644 --- a/tsdb/db.go +++ b/tsdb/db.go @@ -199,7 +199,7 @@ func newDBMetrics(db *DB, r prometheus.Registerer) *dbMetrics { }) m.symbolTableSize = prometheus.NewGaugeFunc(prometheus.GaugeOpts{ Name: "prometheus_tsdb_symbol_table_size_bytes", - Help: "Size of symbol table on disk (in bytes)", + Help: "Size of symbol table in memory for loaded blocks", }, func() float64 { db.mtx.RLock() blocks := db.blocks[:] From c5a832b39440547f681dc08f06343c79d5ba7f45 Mon Sep 17 00:00:00 2001 From: Arthur Silva Sens Date: Wed, 21 Oct 2020 12:08:28 -0300 Subject: [PATCH 11/65] Close resources after failing to startup TSDB (#8031) * Close resources after failing to startup TSDB Signed-off-by: arthursens * Return close error instead of logging Signed-off-by: arthursens * Change named return's name Signed-off-by: arthursens --- tsdb/db.go | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/tsdb/db.go b/tsdb/db.go index 213a44122b..5be60b670b 100644 --- a/tsdb/db.go +++ b/tsdb/db.go @@ -553,7 +553,7 @@ func validateOpts(opts *Options, rngs []int64) (*Options, []int64) { return opts, rngs } -func open(dir string, l log.Logger, r prometheus.Registerer, opts *Options, rngs []int64) (db *DB, err error) { +func open(dir string, l log.Logger, r prometheus.Registerer, opts *Options, rngs []int64) (db *DB, returnedErr error) { if err := os.MkdirAll(dir, 0777); err != nil { return nil, err } @@ -595,6 +595,19 @@ func open(dir string, l log.Logger, r prometheus.Registerer, opts *Options, rngs chunkPool: chunkenc.NewPool(), blocksToDelete: opts.BlocksToDelete, } + defer func() { + // Close files if startup fails somewhere. + if returnedErr == nil { + return + } + + var merr tsdb_errors.MultiError + merr.Add(returnedErr) + merr.Add(errors.Wrap(db.Close(), "close DB after failed startup")) + + returnedErr = merr.Err() + }() + if db.blocksToDelete == nil { db.blocksToDelete = DefaultBlocksToDelete(db) } @@ -612,10 +625,10 @@ func open(dir string, l log.Logger, r prometheus.Registerer, opts *Options, rngs } ctx, cancel := context.WithCancel(context.Background()) - db.compactor, err = NewLeveledCompactor(ctx, r, l, rngs, db.chunkPool) - if err != nil { + db.compactor, returnedErr = NewLeveledCompactor(ctx, r, l, rngs, db.chunkPool) + if returnedErr != nil { cancel() - return nil, errors.Wrap(err, "create leveled compactor") + return nil, errors.Wrap(returnedErr, "create leveled compactor") } db.compactCancel = cancel @@ -627,15 +640,15 @@ func open(dir string, l log.Logger, r prometheus.Registerer, opts *Options, rngs if opts.WALSegmentSize > 0 { segmentSize = opts.WALSegmentSize } - wlog, err = wal.NewSize(l, r, walDir, segmentSize, opts.WALCompression) - if err != nil { - return nil, err + wlog, returnedErr = wal.NewSize(l, r, walDir, segmentSize, opts.WALCompression) + if returnedErr != nil { + return nil, returnedErr } } - db.head, err = NewHead(r, l, wlog, rngs[0], dir, db.chunkPool, opts.StripeSize, opts.SeriesLifecycleCallback) - if err != nil { - return nil, err + db.head, returnedErr = NewHead(r, l, wlog, rngs[0], dir, db.chunkPool, opts.StripeSize, opts.SeriesLifecycleCallback) + if returnedErr != nil { + return nil, returnedErr } // Register metrics after assigning the head block. @@ -1333,7 +1346,9 @@ func (db *DB) Close() error { if db.lockf != nil { merr.Add(db.lockf.Release()) } - merr.Add(db.head.Close()) + if db.head != nil { + merr.Add(db.head.Close()) + } return merr.Err() } From 4e5b1722b342948a55b3d7753f6539040db0e5f0 Mon Sep 17 00:00:00 2001 From: Julien Pivotto Date: Thu, 22 Oct 2020 11:00:08 +0200 Subject: [PATCH 12/65] Move away from testutil, refactor imports (#8087) Signed-off-by: Julien Pivotto --- cmd/prometheus/main.go | 5 +- cmd/prometheus/main_test.go | 45 +- cmd/prometheus/query_log_test.go | 90 +- cmd/promtool/main.go | 3 +- cmd/promtool/main_test.go | 26 +- cmd/promtool/tsdb.go | 1 + cmd/promtool/unittest.go | 2 +- config/config_test.go | 66 +- discovery/azure/azure_test.go | 22 +- discovery/consul/consul_test.go | 30 +- discovery/digitalocean/digitalocean_test.go | 18 +- discovery/discovery.go | 2 +- discovery/dns/dns_test.go | 10 +- discovery/dockerswarm/mock_test.go | 7 +- discovery/dockerswarm/network.go | 1 + discovery/dockerswarm/nodes_test.go | 18 +- discovery/dockerswarm/services_test.go | 18 +- discovery/dockerswarm/tasks_test.go | 18 +- discovery/eureka/client_test.go | 26 +- discovery/eureka/eureka_test.go | 26 +- discovery/file/file_test.go | 32 +- discovery/hetzner/hcloud.go | 1 + discovery/hetzner/hcloud_test.go | 19 +- discovery/hetzner/hetzner.go | 3 +- discovery/hetzner/robot.go | 1 + discovery/hetzner/robot_test.go | 27 +- discovery/kubernetes/endpointslice_test.go | 3 +- discovery/kubernetes/kubernetes_test.go | 3 +- discovery/manager_test.go | 23 +- discovery/marathon/marathon_test.go | 1 + discovery/openstack/hypervisor_test.go | 20 +- discovery/openstack/instance.go | 1 + discovery/openstack/instance_test.go | 20 +- discovery/refresh/refresh_test.go | 6 +- discovery/registry.go | 3 +- discovery/targetgroup/targetgroup_test.go | 21 +- discovery/triton/triton_test.go | 107 +- .../examples/custom-sd/adapter-usage/main.go | 3 +- .../examples/custom-sd/adapter/adapter.go | 1 + .../custom-sd/adapter/adapter_test.go | 7 +- .../influxdb/client_test.go | 1 - .../remote_storage_adapter/main.go | 6 +- go.mod | 5 +- go.sum | 46 +- notifier/notifier.go | 4 +- notifier/notifier_test.go | 50 +- pkg/labels/labels_test.go | 52 +- pkg/labels/matcher_test.go | 10 +- pkg/labels/regexp_test.go | 14 +- pkg/logging/dedupe_test.go | 10 +- pkg/logging/file_test.go | 56 +- pkg/pool/pool_test.go | 4 +- pkg/relabel/relabel_test.go | 6 +- pkg/rulefmt/rulefmt_test.go | 6 +- pkg/textparse/openmetricsparse_test.go | 41 +- pkg/textparse/promparse_test.go | 53 +- promql/engine_test.go | 99 +- promql/functions_test.go | 17 +- promql/parser/generated_parser.y.go | 7 +- promql/parser/lex_test.go | 6 +- promql/parser/parse_test.go | 40 +- promql/parser/printer.go | 1 + promql/parser/printer_test.go | 6 +- promql/promql_test.go | 8 +- promql/query_logger_test.go | 8 +- promql/test.go | 1 + promql/test_test.go | 20 +- promql/value.go | 3 +- rules/alerting.go | 6 +- rules/alerting_test.go | 55 +- rules/manager_test.go | 164 +- rules/recording_test.go | 17 +- scrape/helpers_test.go | 1 + scrape/manager.go | 2 +- scrape/manager_test.go | 23 +- scrape/scrape_test.go | 301 +-- scrape/target_test.go | 16 +- storage/buffer_test.go | 32 +- storage/fanout.go | 1 + storage/fanout_test.go | 50 +- storage/merge.go | 1 + storage/merge_test.go | 72 +- storage/remote/chunked_test.go | 42 +- storage/remote/client.go | 2 +- storage/remote/client_test.go | 16 +- storage/remote/codec.go | 1 + storage/remote/codec_test.go | 45 +- storage/remote/intern.go | 3 +- storage/remote/intern_test.go | 28 +- storage/remote/queue_manager.go | 2 +- storage/remote/queue_manager_test.go | 77 +- storage/remote/read.go | 1 + storage/remote/read_test.go | 38 +- storage/remote/storage.go | 4 +- storage/remote/storage_test.go | 25 +- storage/remote/write.go | 1 + storage/remote/write_test.go | 125 +- template/template.go | 7 +- template/template_test.go | 9 +- tsdb/block.go | 1 + tsdb/block_test.go | 155 +- tsdb/blockwriter_test.go | 26 +- tsdb/chunkenc/bstream_test.go | 14 +- tsdb/chunkenc/chunk_test.go | 28 +- tsdb/chunks/chunks.go | 1 + tsdb/chunks/chunks_test.go | 4 +- tsdb/chunks/head_chunks.go | 3 +- tsdb/chunks/head_chunks_test.go | 177 +- tsdb/compact.go | 1 + tsdb/compact_test.go | 149 +- tsdb/db.go | 7 +- tsdb/db_test.go | 1148 +++++------ tsdb/head.go | 3 +- tsdb/head_bench_test.go | 14 +- tsdb/head_test.go | 879 ++++----- tsdb/index/index.go | 1 + tsdb/index/index_test.go | 168 +- tsdb/index/postings_test.go | 87 +- tsdb/index/postingsstats_test.go | 10 +- tsdb/mocks_test.go | 1 + tsdb/querier.go | 1 + tsdb/querier_bench_test.go | 47 +- tsdb/querier_test.go | 213 +-- tsdb/record/record.go | 1 + tsdb/record/record_test.go | 31 +- tsdb/repair.go | 1 + tsdb/repair_test.go | 39 +- tsdb/tombstones/tombstones.go | 1 + tsdb/tombstones/tombstones_test.go | 14 +- tsdb/tsdbblockutil.go | 1 + tsdb/tsdbutil/buffer_test.go | 24 +- tsdb/wal.go | 1 + tsdb/wal/checkpoint.go | 1 + tsdb/wal/checkpoint_test.go | 161 +- tsdb/wal/reader_test.go | 124 +- tsdb/wal/wal.go | 1 + tsdb/wal/wal_test.go | 201 +- tsdb/wal/watcher.go | 1 + tsdb/wal/watcher_test.go | 136 +- tsdb/wal_test.go | 233 +-- util/stats/stats_test.go | 1 + util/testutil/directory.go | 18 +- util/testutil/testing.go | 129 -- vendor/github.com/stretchr/testify/LICENSE | 21 + .../testify/assert/assertion_compare.go | 274 +++ .../testify/assert/assertion_format.go | 644 +++++++ .../testify/assert/assertion_format.go.tmpl | 5 + .../testify/assert/assertion_forward.go | 1276 +++++++++++++ .../testify/assert/assertion_forward.go.tmpl | 5 + .../stretchr/testify/assert/assertions.go | 1695 +++++++++++++++++ .../github.com/stretchr/testify/assert/doc.go | 45 + .../stretchr/testify/assert/errors.go | 10 + .../testify/assert/forward_assertions.go | 16 + .../testify/assert/http_assertions.go | 162 ++ .../x/tools/internal/gocommand/invoke.go | 36 +- .../x/tools/internal/imports/fix.go | 34 +- .../x/tools/internal/imports/mod.go | 26 +- vendor/modules.txt | 7 +- web/api/v1/api.go | 1 + web/api/v1/api_test.go | 118 +- web/federate.go | 2 +- web/federate_test.go | 11 +- web/web_test.go | 127 +- 163 files changed, 7645 insertions(+), 3610 deletions(-) create mode 100644 vendor/github.com/stretchr/testify/LICENSE create mode 100644 vendor/github.com/stretchr/testify/assert/assertion_compare.go create mode 100644 vendor/github.com/stretchr/testify/assert/assertion_format.go create mode 100644 vendor/github.com/stretchr/testify/assert/assertion_format.go.tmpl create mode 100644 vendor/github.com/stretchr/testify/assert/assertion_forward.go create mode 100644 vendor/github.com/stretchr/testify/assert/assertion_forward.go.tmpl create mode 100644 vendor/github.com/stretchr/testify/assert/assertions.go create mode 100644 vendor/github.com/stretchr/testify/assert/doc.go create mode 100644 vendor/github.com/stretchr/testify/assert/errors.go create mode 100644 vendor/github.com/stretchr/testify/assert/forward_assertions.go create mode 100644 vendor/github.com/stretchr/testify/assert/http_assertions.go diff --git a/cmd/prometheus/main.go b/cmd/prometheus/main.go index 52b646d181..3eadc36dc9 100644 --- a/cmd/prometheus/main.go +++ b/cmd/prometheus/main.go @@ -44,6 +44,7 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/common/model" "github.com/prometheus/common/promlog" + promlogflag "github.com/prometheus/common/promlog/flag" "github.com/prometheus/common/version" jcfg "github.com/uber/jaeger-client-go/config" jprom "github.com/uber/jaeger-lib/metrics/prometheus" @@ -52,9 +53,9 @@ import ( klog "k8s.io/klog" klogv2 "k8s.io/klog/v2" - promlogflag "github.com/prometheus/common/promlog/flag" "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/discovery" + _ "github.com/prometheus/prometheus/discovery/install" // Register service discovery implementations. "github.com/prometheus/prometheus/notifier" "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/logging" @@ -68,8 +69,6 @@ import ( "github.com/prometheus/prometheus/tsdb" "github.com/prometheus/prometheus/util/strutil" "github.com/prometheus/prometheus/web" - - _ "github.com/prometheus/prometheus/discovery/install" // Register service discovery implementations. ) var ( diff --git a/cmd/prometheus/main_test.go b/cmd/prometheus/main_test.go index a8c907f03f..28a76bd818 100644 --- a/cmd/prometheus/main_test.go +++ b/cmd/prometheus/main_test.go @@ -28,10 +28,11 @@ import ( "github.com/go-kit/kit/log" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/common/model" + "github.com/stretchr/testify/assert" + "github.com/prometheus/prometheus/notifier" "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/rules" - "github.com/prometheus/prometheus/util/testutil" ) var promPath = os.Args[0] @@ -97,9 +98,9 @@ func TestComputeExternalURL(t *testing.T) { for _, test := range tests { _, err := computeExternalURL(test.input, "0.0.0.0:9090") if test.valid { - testutil.Ok(t, err) + assert.NoError(t, err) } else { - testutil.NotOk(t, err, "input=%q", test.input) + assert.Error(t, err, "input=%q", test.input) } } } @@ -115,11 +116,11 @@ func TestFailedStartupExitCode(t *testing.T) { prom := exec.Command(promPath, "-test.main", "--config.file="+fakeInputFile) err := prom.Run() - testutil.NotOk(t, err) + assert.Error(t, err) if exitError, ok := err.(*exec.ExitError); ok { status := exitError.Sys().(syscall.WaitStatus) - testutil.Equals(t, expectedExitStatus, status.ExitStatus()) + assert.Equal(t, expectedExitStatus, status.ExitStatus()) } else { t.Errorf("unable to retrieve the exit status for prometheus: %v", err) } @@ -188,7 +189,7 @@ func TestSendAlerts(t *testing.T) { if len(tc.in) == 0 { t.Fatalf("sender called with 0 alert") } - testutil.Equals(t, tc.exp, alerts) + assert.Equal(t, tc.exp, alerts) }) sendAlerts(senderFunc, "http://localhost:9090")(context.TODO(), "up", tc.in...) }) @@ -205,14 +206,14 @@ func TestWALSegmentSizeBounds(t *testing.T) { // Log stderr in case of failure. stderr, err := prom.StderrPipe() - testutil.Ok(t, err) + assert.NoError(t, err) go func() { slurp, _ := ioutil.ReadAll(stderr) t.Log(string(slurp)) }() err = prom.Start() - testutil.Ok(t, err) + assert.NoError(t, err) if expectedExitStatus == 0 { done := make(chan error, 1) @@ -227,10 +228,10 @@ func TestWALSegmentSizeBounds(t *testing.T) { } err = prom.Wait() - testutil.NotOk(t, err) + assert.Error(t, err) if exitError, ok := err.(*exec.ExitError); ok { status := exitError.Sys().(syscall.WaitStatus) - testutil.Equals(t, expectedExitStatus, status.ExitStatus()) + assert.Equal(t, expectedExitStatus, status.ExitStatus()) } else { t.Errorf("unable to retrieve the exit status for prometheus: %v", err) } @@ -239,21 +240,21 @@ func TestWALSegmentSizeBounds(t *testing.T) { func TestTimeMetrics(t *testing.T) { tmpDir, err := ioutil.TempDir("", "time_metrics_e2e") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(tmpDir)) + assert.NoError(t, os.RemoveAll(tmpDir)) }() reg := prometheus.NewRegistry() db, err := openDBWithMetrics(tmpDir, log.NewNopLogger(), reg, nil) - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }() // Check initial values. - testutil.Equals(t, map[string]float64{ + assert.Equal(t, map[string]float64{ "prometheus_tsdb_lowest_timestamp_seconds": float64(math.MaxInt64) / 1000, "prometheus_tsdb_head_min_time_seconds": float64(math.MaxInt64) / 1000, "prometheus_tsdb_head_max_time_seconds": float64(math.MinInt64) / 1000, @@ -265,14 +266,14 @@ func TestTimeMetrics(t *testing.T) { app := db.Appender(context.Background()) _, err = app.Add(labels.FromStrings(model.MetricNameLabel, "a"), 1000, 1) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = app.Add(labels.FromStrings(model.MetricNameLabel, "a"), 2000, 1) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = app.Add(labels.FromStrings(model.MetricNameLabel, "a"), 3000, 1) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) - testutil.Equals(t, map[string]float64{ + assert.Equal(t, map[string]float64{ "prometheus_tsdb_lowest_timestamp_seconds": 1.0, "prometheus_tsdb_head_min_time_seconds": 1.0, "prometheus_tsdb_head_max_time_seconds": 3.0, @@ -285,7 +286,7 @@ func TestTimeMetrics(t *testing.T) { func getCurrentGaugeValuesFor(t *testing.T, reg prometheus.Gatherer, metricNames ...string) map[string]float64 { f, err := reg.Gather() - testutil.Ok(t, err) + assert.NoError(t, err) res := make(map[string]float64, len(metricNames)) for _, g := range f { @@ -294,7 +295,7 @@ func getCurrentGaugeValuesFor(t *testing.T, reg prometheus.Gatherer, metricNames continue } - testutil.Equals(t, 1, len(g.GetMetric())) + assert.Equal(t, 1, len(g.GetMetric())) if _, ok := res[m]; ok { t.Error("expected only one metric family for", m) t.FailNow() diff --git a/cmd/prometheus/query_log_test.go b/cmd/prometheus/query_log_test.go index eac6f6ec2a..f7e49ff148 100644 --- a/cmd/prometheus/query_log_test.go +++ b/cmd/prometheus/query_log_test.go @@ -30,7 +30,7 @@ import ( "testing" "time" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" ) type origin int @@ -82,22 +82,22 @@ func (p *queryLogTest) waitForPrometheus() error { // then reloads the configuration if needed. func (p *queryLogTest) setQueryLog(t *testing.T, queryLogFile string) { err := p.configFile.Truncate(0) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = p.configFile.Seek(0, 0) - testutil.Ok(t, err) + assert.NoError(t, err) if queryLogFile != "" { _, err = p.configFile.Write([]byte(fmt.Sprintf("global:\n query_log_file: %s\n", queryLogFile))) - testutil.Ok(t, err) + assert.NoError(t, err) } _, err = p.configFile.Write([]byte(p.configuration())) - testutil.Ok(t, err) + assert.NoError(t, err) } // reloadConfig reloads the configuration using POST. func (p *queryLogTest) reloadConfig(t *testing.T) { r, err := http.Post(fmt.Sprintf("http://%s:%d%s/-/reload", p.host, p.port, p.prefix), "text/plain", nil) - testutil.Ok(t, err) - testutil.Equals(t, 200, r.StatusCode) + assert.NoError(t, err) + assert.Equal(t, 200, r.StatusCode) } // query runs a query according to the test origin. @@ -111,8 +111,8 @@ func (p *queryLogTest) query(t *testing.T) { p.prefix, url.QueryEscape("query_with_api"), )) - testutil.Ok(t, err) - testutil.Equals(t, 200, r.StatusCode) + assert.NoError(t, err) + assert.Equal(t, 200, r.StatusCode) case consoleOrigin: r, err := http.Get(fmt.Sprintf( "http://%s:%d%s/consoles/test.html", @@ -120,8 +120,8 @@ func (p *queryLogTest) query(t *testing.T) { p.port, p.prefix, )) - testutil.Ok(t, err) - testutil.Equals(t, 200, r.StatusCode) + assert.NoError(t, err) + assert.Equal(t, 200, r.StatusCode) case ruleOrigin: time.Sleep(2 * time.Second) default: @@ -147,15 +147,15 @@ func (p *queryLogTest) queryString() string { // test parameters. func (p *queryLogTest) validateLastQuery(t *testing.T, ql []queryLogLine) { q := ql[len(ql)-1] - testutil.Equals(t, p.queryString(), q.Params.Query) + assert.Equal(t, p.queryString(), q.Params.Query) switch p.origin { case apiOrigin: - testutil.Equals(t, 5, q.Params.Step) - testutil.Equals(t, "1970-01-01T00:00:00.000Z", q.Params.Start) - testutil.Equals(t, "1970-01-01T01:00:00.000Z", q.Params.End) + assert.Equal(t, 5, q.Params.Step) + assert.Equal(t, "1970-01-01T00:00:00.000Z", q.Params.Start) + assert.Equal(t, "1970-01-01T01:00:00.000Z", q.Params.End) default: - testutil.Equals(t, 0, q.Params.Step) + assert.Equal(t, 0, q.Params.Step) } if p.origin != ruleOrigin { @@ -163,17 +163,17 @@ func (p *queryLogTest) validateLastQuery(t *testing.T, ql []queryLogLine) { if host == "[::1]" { host = "::1" } - testutil.Equals(t, host, q.Request.ClientIP) + assert.Equal(t, host, q.Request.ClientIP) } switch p.origin { case apiOrigin: - testutil.Equals(t, p.prefix+"/api/v1/query_range", q.Request.Path) + assert.Equal(t, p.prefix+"/api/v1/query_range", q.Request.Path) case consoleOrigin: - testutil.Equals(t, p.prefix+"/consoles/test.html", q.Request.Path) + assert.Equal(t, p.prefix+"/consoles/test.html", q.Request.Path) case ruleOrigin: - testutil.Equals(t, "querylogtest", q.RuleGroup.Name) - testutil.Equals(t, filepath.Join(p.cwd, "testdata", "rules", "test.yml"), q.RuleGroup.File) + assert.Equal(t, "querylogtest", q.RuleGroup.Name) + assert.Equal(t, filepath.Join(p.cwd, "testdata", "rules", "test.yml"), q.RuleGroup.File) default: panic("unknown origin") } @@ -234,10 +234,10 @@ func (p *queryLogTest) run(t *testing.T) { // Setup temporary files for this test. queryLogFile, err := ioutil.TempFile("", "query") - testutil.Ok(t, err) + assert.NoError(t, err) defer os.Remove(queryLogFile.Name()) p.configFile, err = ioutil.TempFile("", "config") - testutil.Ok(t, err) + assert.NoError(t, err) defer os.Remove(p.configFile.Name()) if p.enabledAtStart { @@ -247,9 +247,9 @@ func (p *queryLogTest) run(t *testing.T) { } dir, err := ioutil.TempDir("", "query_log_test") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() params := append([]string{ @@ -264,7 +264,7 @@ func (p *queryLogTest) run(t *testing.T) { // Log stderr in case of failure. stderr, err := prom.StderrPipe() - testutil.Ok(t, err) + assert.NoError(t, err) // We use a WaitGroup to avoid calling t.Log after the test is done. var wg sync.WaitGroup @@ -276,17 +276,17 @@ func (p *queryLogTest) run(t *testing.T) { wg.Done() }() - testutil.Ok(t, prom.Start()) + assert.NoError(t, prom.Start()) defer func() { prom.Process.Kill() prom.Wait() }() - testutil.Ok(t, p.waitForPrometheus()) + assert.NoError(t, p.waitForPrometheus()) if !p.enabledAtStart { p.query(t) - testutil.Equals(t, 0, len(readQueryLog(t, queryLogFile.Name()))) + assert.Equal(t, 0, len(readQueryLog(t, queryLogFile.Name()))) p.setQueryLog(t, queryLogFile.Name()) p.reloadConfig(t) } @@ -296,9 +296,9 @@ func (p *queryLogTest) run(t *testing.T) { ql := readQueryLog(t, queryLogFile.Name()) qc := len(ql) if p.exactQueryCount() { - testutil.Equals(t, 1, qc) + assert.Equal(t, 1, qc) } else { - testutil.Assert(t, qc > 0, "no queries logged") + assert.True(t, qc > 0, "no queries logged") } p.validateLastQuery(t, ql) @@ -311,7 +311,7 @@ func (p *queryLogTest) run(t *testing.T) { p.query(t) ql = readQueryLog(t, queryLogFile.Name()) - testutil.Equals(t, qc, len(ql)) + assert.Equal(t, qc, len(ql)) qc = len(ql) p.setQueryLog(t, queryLogFile.Name()) @@ -322,9 +322,9 @@ func (p *queryLogTest) run(t *testing.T) { ql = readQueryLog(t, queryLogFile.Name()) if p.exactQueryCount() { - testutil.Equals(t, qc, len(ql)) + assert.Equal(t, qc, len(ql)) } else { - testutil.Assert(t, len(ql) > qc, "no queries logged") + assert.True(t, len(ql) > qc, "no queries logged") } p.validateLastQuery(t, ql) qc = len(ql) @@ -336,13 +336,13 @@ func (p *queryLogTest) run(t *testing.T) { } // Move the file, Prometheus should still write to the old file. newFile, err := ioutil.TempFile("", "newLoc") - testutil.Ok(t, err) - testutil.Ok(t, newFile.Close()) + assert.NoError(t, err) + assert.NoError(t, newFile.Close()) defer os.Remove(newFile.Name()) - testutil.Ok(t, os.Rename(queryLogFile.Name(), newFile.Name())) + assert.NoError(t, os.Rename(queryLogFile.Name(), newFile.Name())) ql = readQueryLog(t, newFile.Name()) if p.exactQueryCount() { - testutil.Equals(t, qc, len(ql)) + assert.Equal(t, qc, len(ql)) } p.validateLastQuery(t, ql) qc = len(ql) @@ -353,9 +353,9 @@ func (p *queryLogTest) run(t *testing.T) { ql = readQueryLog(t, newFile.Name()) if p.exactQueryCount() { - testutil.Equals(t, qc, len(ql)) + assert.Equal(t, qc, len(ql)) } else { - testutil.Assert(t, len(ql) > qc, "no queries logged") + assert.True(t, len(ql) > qc, "no queries logged") } p.validateLastQuery(t, ql) @@ -366,9 +366,9 @@ func (p *queryLogTest) run(t *testing.T) { ql = readQueryLog(t, queryLogFile.Name()) qc = len(ql) if p.exactQueryCount() { - testutil.Equals(t, 1, qc) + assert.Equal(t, 1, qc) } else { - testutil.Assert(t, qc > 0, "no queries logged") + assert.True(t, qc > 0, "no queries logged") } } @@ -393,12 +393,12 @@ type queryLogLine struct { func readQueryLog(t *testing.T, path string) []queryLogLine { ql := []queryLogLine{} file, err := os.Open(path) - testutil.Ok(t, err) + assert.NoError(t, err) defer file.Close() scanner := bufio.NewScanner(file) for scanner.Scan() { var q queryLogLine - testutil.Ok(t, json.Unmarshal(scanner.Bytes(), &q)) + assert.NoError(t, json.Unmarshal(scanner.Bytes(), &q)) ql = append(ql, q) } return ql @@ -410,7 +410,7 @@ func TestQueryLog(t *testing.T) { } cwd, err := os.Getwd() - testutil.Ok(t, err) + assert.NoError(t, err) port := 15000 for _, host := range []string{"127.0.0.1", "[::1]"} { diff --git a/cmd/promtool/main.go b/cmd/promtool/main.go index 2d2c98fbb7..8dcb02d887 100644 --- a/cmd/promtool/main.go +++ b/cmd/promtool/main.go @@ -41,10 +41,9 @@ import ( "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/discovery/file" + _ "github.com/prometheus/prometheus/discovery/install" // Register service discovery implementations. "github.com/prometheus/prometheus/discovery/kubernetes" "github.com/prometheus/prometheus/pkg/rulefmt" - - _ "github.com/prometheus/prometheus/discovery/install" // Register service discovery implementations. ) func main() { diff --git a/cmd/promtool/main_test.go b/cmd/promtool/main_test.go index a40ff0a860..340b291033 100644 --- a/cmd/promtool/main_test.go +++ b/cmd/promtool/main_test.go @@ -20,7 +20,7 @@ import ( "testing" "time" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" ) func TestQueryRange(t *testing.T) { @@ -29,18 +29,18 @@ func TestQueryRange(t *testing.T) { p := &promqlPrinter{} exitCode := QueryRange(s.URL, map[string]string{}, "up", "0", "300", 0, p) - testutil.Equals(t, "/api/v1/query_range", getRequest().URL.Path) + assert.Equal(t, "/api/v1/query_range", getRequest().URL.Path) form := getRequest().Form - testutil.Equals(t, "up", form.Get("query")) - testutil.Equals(t, "1", form.Get("step")) - testutil.Equals(t, 0, exitCode) + assert.Equal(t, "up", form.Get("query")) + assert.Equal(t, "1", form.Get("step")) + assert.Equal(t, 0, exitCode) exitCode = QueryRange(s.URL, map[string]string{}, "up", "0", "300", 10*time.Millisecond, p) - testutil.Equals(t, "/api/v1/query_range", getRequest().URL.Path) + assert.Equal(t, "/api/v1/query_range", getRequest().URL.Path) form = getRequest().Form - testutil.Equals(t, "up", form.Get("query")) - testutil.Equals(t, "0.01", form.Get("step")) - testutil.Equals(t, 0, exitCode) + assert.Equal(t, "up", form.Get("query")) + assert.Equal(t, "0.01", form.Get("step")) + assert.Equal(t, 0, exitCode) } func TestQueryInstant(t *testing.T) { @@ -49,11 +49,11 @@ func TestQueryInstant(t *testing.T) { p := &promqlPrinter{} exitCode := QueryInstant(s.URL, "up", "300", p) - testutil.Equals(t, "/api/v1/query", getRequest().URL.Path) + assert.Equal(t, "/api/v1/query", getRequest().URL.Path) form := getRequest().Form - testutil.Equals(t, "up", form.Get("query")) - testutil.Equals(t, "300", form.Get("time")) - testutil.Equals(t, 0, exitCode) + assert.Equal(t, "up", form.Get("query")) + assert.Equal(t, "300", form.Get("time")) + assert.Equal(t, 0, exitCode) } func mockServer(code int, body string) (*httptest.Server, func() *http.Request) { diff --git a/cmd/promtool/tsdb.go b/cmd/promtool/tsdb.go index cb692e9ef8..369d26a4f5 100644 --- a/cmd/promtool/tsdb.go +++ b/cmd/promtool/tsdb.go @@ -33,6 +33,7 @@ import ( "github.com/alecthomas/units" "github.com/go-kit/kit/log" "github.com/pkg/errors" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/storage" "github.com/prometheus/prometheus/tsdb" diff --git a/cmd/promtool/unittest.go b/cmd/promtool/unittest.go index cbdb8931ef..a11150d010 100644 --- a/cmd/promtool/unittest.go +++ b/cmd/promtool/unittest.go @@ -27,9 +27,9 @@ import ( "github.com/go-kit/kit/log" "github.com/pkg/errors" + "github.com/prometheus/common/model" yaml "gopkg.in/yaml.v2" - "github.com/prometheus/common/model" "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/promql" "github.com/prometheus/prometheus/promql/parser" diff --git a/config/config_test.go b/config/config_test.go index 85234dd273..f2e1acac78 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -25,6 +25,7 @@ import ( "github.com/prometheus/common/config" "github.com/prometheus/common/model" + "github.com/stretchr/testify/assert" "gopkg.in/yaml.v2" "github.com/prometheus/prometheus/discovery" @@ -45,7 +46,6 @@ import ( "github.com/prometheus/prometheus/discovery/zookeeper" "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/relabel" - "github.com/prometheus/prometheus/util/testutil" ) func mustParseURL(u string) *config.URL { @@ -720,77 +720,77 @@ var expectedConf = &Config{ func TestYAMLRoundtrip(t *testing.T) { want, err := LoadFile("testdata/roundtrip.good.yml") - testutil.Ok(t, err) + assert.NoError(t, err) out, err := yaml.Marshal(want) - testutil.Ok(t, err) + assert.NoError(t, err) got := &Config{} - testutil.Ok(t, yaml.UnmarshalStrict(out, got)) + assert.NoError(t, yaml.UnmarshalStrict(out, got)) - testutil.Equals(t, want, got) + assert.Equal(t, want, got) } func TestLoadConfig(t *testing.T) { // Parse a valid file that sets a global scrape timeout. This tests whether parsing // an overwritten default field in the global config permanently changes the default. _, err := LoadFile("testdata/global_timeout.good.yml") - testutil.Ok(t, err) + assert.NoError(t, err) c, err := LoadFile("testdata/conf.good.yml") - testutil.Ok(t, err) - testutil.Equals(t, expectedConf, c) + assert.NoError(t, err) + assert.Equal(t, expectedConf, c) } func TestScrapeIntervalLarger(t *testing.T) { c, err := LoadFile("testdata/scrape_interval_larger.good.yml") - testutil.Ok(t, err) - testutil.Equals(t, 1, len(c.ScrapeConfigs)) + assert.NoError(t, err) + assert.Equal(t, 1, len(c.ScrapeConfigs)) for _, sc := range c.ScrapeConfigs { - testutil.Equals(t, true, sc.ScrapeInterval >= sc.ScrapeTimeout) + assert.Equal(t, true, sc.ScrapeInterval >= sc.ScrapeTimeout) } } // YAML marshaling must not reveal authentication credentials. func TestElideSecrets(t *testing.T) { c, err := LoadFile("testdata/conf.good.yml") - testutil.Ok(t, err) + assert.NoError(t, err) secretRe := regexp.MustCompile(`\\u003csecret\\u003e|`) config, err := yaml.Marshal(c) - testutil.Ok(t, err) + assert.NoError(t, err) yamlConfig := string(config) matches := secretRe.FindAllStringIndex(yamlConfig, -1) - testutil.Assert(t, len(matches) == 10, "wrong number of secret matches found") - testutil.Assert(t, !strings.Contains(yamlConfig, "mysecret"), + assert.True(t, len(matches) == 10, "wrong number of secret matches found") + assert.True(t, !strings.Contains(yamlConfig, "mysecret"), "yaml marshal reveals authentication credentials.") } func TestLoadConfigRuleFilesAbsolutePath(t *testing.T) { // Parse a valid file that sets a rule files with an absolute path c, err := LoadFile(ruleFilesConfigFile) - testutil.Ok(t, err) - testutil.Equals(t, ruleFilesExpectedConf, c) + assert.NoError(t, err) + assert.Equal(t, ruleFilesExpectedConf, c) } func TestKubernetesEmptyAPIServer(t *testing.T) { _, err := LoadFile("testdata/kubernetes_empty_apiserver.good.yml") - testutil.Ok(t, err) + assert.NoError(t, err) } func TestKubernetesSelectors(t *testing.T) { _, err := LoadFile("testdata/kubernetes_selectors_endpoints.good.yml") - testutil.Ok(t, err) + assert.NoError(t, err) _, err = LoadFile("testdata/kubernetes_selectors_node.good.yml") - testutil.Ok(t, err) + assert.NoError(t, err) _, err = LoadFile("testdata/kubernetes_selectors_ingress.good.yml") - testutil.Ok(t, err) + assert.NoError(t, err) _, err = LoadFile("testdata/kubernetes_selectors_pod.good.yml") - testutil.Ok(t, err) + assert.NoError(t, err) _, err = LoadFile("testdata/kubernetes_selectors_service.good.yml") - testutil.Ok(t, err) + assert.NoError(t, err) } var expectedErrors = []struct { @@ -1026,40 +1026,40 @@ var expectedErrors = []struct { func TestBadConfigs(t *testing.T) { for _, ee := range expectedErrors { _, err := LoadFile("testdata/" + ee.filename) - testutil.NotOk(t, err, "%s", ee.filename) - testutil.Assert(t, strings.Contains(err.Error(), ee.errMsg), + assert.Error(t, err, "%s", ee.filename) + assert.True(t, strings.Contains(err.Error(), ee.errMsg), "Expected error for %s to contain %q but got: %s", ee.filename, ee.errMsg, err) } } func TestBadStaticConfigsJSON(t *testing.T) { content, err := ioutil.ReadFile("testdata/static_config.bad.json") - testutil.Ok(t, err) + assert.NoError(t, err) var tg targetgroup.Group err = json.Unmarshal(content, &tg) - testutil.NotOk(t, err) + assert.Error(t, err) } func TestBadStaticConfigsYML(t *testing.T) { content, err := ioutil.ReadFile("testdata/static_config.bad.yml") - testutil.Ok(t, err) + assert.NoError(t, err) var tg targetgroup.Group err = yaml.UnmarshalStrict(content, &tg) - testutil.NotOk(t, err) + assert.Error(t, err) } func TestEmptyConfig(t *testing.T) { c, err := Load("") - testutil.Ok(t, err) + assert.NoError(t, err) exp := DefaultConfig - testutil.Equals(t, exp, *c) + assert.Equal(t, exp, *c) } func TestEmptyGlobalBlock(t *testing.T) { c, err := Load("global:\n") - testutil.Ok(t, err) + assert.NoError(t, err) exp := DefaultConfig - testutil.Equals(t, exp, *c) + assert.Equal(t, exp, *c) } func kubernetesSDHostURL() config.URL { diff --git a/discovery/azure/azure_test.go b/discovery/azure/azure_test.go index e5e6f42014..24e9e530fd 100644 --- a/discovery/azure/azure_test.go +++ b/discovery/azure/azure_test.go @@ -14,10 +14,10 @@ package azure import ( - "reflect" "testing" "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2018-10-01/compute" + "github.com/stretchr/testify/assert" "go.uber.org/goleak" ) @@ -63,9 +63,7 @@ func TestMapFromVMWithEmptyTags(t *testing.T) { actualVM := mapFromVM(testVM) - if !reflect.DeepEqual(expectedVM, actualVM) { - t.Errorf("Expected %v got %v", expectedVM, actualVM) - } + assert.Equal(t, expectedVM, actualVM) } func TestMapFromVMWithTags(t *testing.T) { @@ -109,9 +107,7 @@ func TestMapFromVMWithTags(t *testing.T) { actualVM := mapFromVM(testVM) - if !reflect.DeepEqual(expectedVM, actualVM) { - t.Errorf("Expected %v got %v", expectedVM, actualVM) - } + assert.Equal(t, expectedVM, actualVM) } func TestMapFromVMScaleSetVMWithEmptyTags(t *testing.T) { @@ -154,9 +150,7 @@ func TestMapFromVMScaleSetVMWithEmptyTags(t *testing.T) { actualVM := mapFromVMScaleSetVM(testVM, scaleSet) - if !reflect.DeepEqual(expectedVM, actualVM) { - t.Errorf("Expected %v got %v", expectedVM, actualVM) - } + assert.Equal(t, expectedVM, actualVM) } func TestMapFromVMScaleSetVMWithTags(t *testing.T) { @@ -202,9 +196,7 @@ func TestMapFromVMScaleSetVMWithTags(t *testing.T) { actualVM := mapFromVMScaleSetVM(testVM, scaleSet) - if !reflect.DeepEqual(expectedVM, actualVM) { - t.Errorf("Expected %v got %v", expectedVM, actualVM) - } + assert.Equal(t, expectedVM, actualVM) } func TestNewAzureResourceFromID(t *testing.T) { @@ -222,8 +214,6 @@ func TestNewAzureResourceFromID(t *testing.T) { }, } { actual, _ := newAzureResourceFromID(tc.id, nil) - if !reflect.DeepEqual(tc.expected, actual) { - t.Errorf("Expected %v got %v", tc.expected, actual) - } + assert.Equal(t, tc.expected, actual) } } diff --git a/discovery/consul/consul_test.go b/discovery/consul/consul_test.go index 92ae51b11c..eb4058d331 100644 --- a/discovery/consul/consul_test.go +++ b/discovery/consul/consul_test.go @@ -15,18 +15,18 @@ package consul import ( "context" - "testing" - "time" - "net/http" "net/http/httptest" "net/url" + "testing" + "time" "github.com/go-kit/kit/log" "github.com/prometheus/common/model" - "github.com/prometheus/prometheus/discovery/targetgroup" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" "go.uber.org/goleak" + + "github.com/prometheus/prometheus/discovery/targetgroup" ) func TestMain(m *testing.M) { @@ -251,7 +251,7 @@ func newServer(t *testing.T) (*httptest.Server, *SDConfig) { w.Write([]byte(response)) })) stuburl, err := url.Parse(stub.URL) - testutil.Ok(t, err) + assert.NoError(t, err) config := &SDConfig{ Server: stuburl.Host, @@ -264,18 +264,18 @@ func newServer(t *testing.T) (*httptest.Server, *SDConfig) { func newDiscovery(t *testing.T, config *SDConfig) *Discovery { logger := log.NewNopLogger() d, err := NewDiscovery(config, logger) - testutil.Ok(t, err) + assert.NoError(t, err) return d } func checkOneTarget(t *testing.T, tg []*targetgroup.Group) { - testutil.Equals(t, 1, len(tg)) + assert.Equal(t, 1, len(tg)) target := tg[0] - testutil.Equals(t, "test-dc", string(target.Labels["__meta_consul_dc"])) - testutil.Equals(t, target.Source, string(target.Labels["__meta_consul_service"])) + assert.Equal(t, "test-dc", string(target.Labels["__meta_consul_dc"])) + assert.Equal(t, target.Source, string(target.Labels["__meta_consul_service"])) if target.Source == "test" { // test service should have one node. - testutil.Assert(t, len(target.Targets) > 0, "Test service should have one node") + assert.True(t, len(target.Targets) > 0, "Test service should have one node") } } @@ -359,7 +359,7 @@ func TestGetDatacenterShouldReturnError(t *testing.T) { } { stub := httptest.NewServer(http.HandlerFunc(tc.handler)) stuburl, err := url.Parse(stub.URL) - testutil.Ok(t, err) + assert.NoError(t, err) config := &SDConfig{ Server: stuburl.Host, @@ -370,13 +370,13 @@ func TestGetDatacenterShouldReturnError(t *testing.T) { d := newDiscovery(t, config) // Should be empty if not initialized. - testutil.Equals(t, "", d.clientDatacenter) + assert.Equal(t, "", d.clientDatacenter) err = d.getDatacenter() // An error should be returned. - testutil.Equals(t, tc.errMessage, err.Error()) + assert.Equal(t, tc.errMessage, err.Error()) // Should still be empty. - testutil.Equals(t, "", d.clientDatacenter) + assert.Equal(t, "", d.clientDatacenter) } } diff --git a/discovery/digitalocean/digitalocean_test.go b/discovery/digitalocean/digitalocean_test.go index 86024a5dfe..3eff2d68bc 100644 --- a/discovery/digitalocean/digitalocean_test.go +++ b/discovery/digitalocean/digitalocean_test.go @@ -21,7 +21,7 @@ import ( "github.com/go-kit/kit/log" "github.com/prometheus/common/model" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" ) type DigitalOceanSDTestSuite struct { @@ -47,21 +47,21 @@ func TestDigitalOceanSDRefresh(t *testing.T) { cfg := DefaultSDConfig cfg.HTTPClientConfig.BearerToken = tokenID d, err := NewDiscovery(&cfg, log.NewNopLogger()) - testutil.Ok(t, err) + assert.NoError(t, err) endpoint, err := url.Parse(sdmock.Mock.Endpoint()) - testutil.Ok(t, err) + assert.NoError(t, err) d.client.BaseURL = endpoint ctx := context.Background() tgs, err := d.refresh(ctx) - testutil.Ok(t, err) + assert.NoError(t, err) - testutil.Equals(t, 1, len(tgs)) + assert.Equal(t, 1, len(tgs)) tg := tgs[0] - testutil.Assert(t, tg != nil, "tg should not be nil") - testutil.Assert(t, tg.Targets != nil, "tg.targets should not be nil") - testutil.Equals(t, 4, len(tg.Targets)) + assert.NotNil(t, tg) + assert.NotNil(t, tg.Targets) + assert.Equal(t, 4, len(tg.Targets)) for i, lbls := range []model.LabelSet{ { @@ -119,7 +119,7 @@ func TestDigitalOceanSDRefresh(t *testing.T) { }, } { t.Run(fmt.Sprintf("item %d", i), func(t *testing.T) { - testutil.Equals(t, lbls, tg.Targets[i]) + assert.Equal(t, lbls, tg.Targets[i]) }) } } diff --git a/discovery/discovery.go b/discovery/discovery.go index d77b193b35..5b0402bdb0 100644 --- a/discovery/discovery.go +++ b/discovery/discovery.go @@ -18,8 +18,8 @@ import ( "reflect" "github.com/go-kit/kit/log" - "github.com/prometheus/common/config" + "github.com/prometheus/prometheus/discovery/targetgroup" ) diff --git a/discovery/dns/dns_test.go b/discovery/dns/dns_test.go index 0ae70638de..a5ae2eccc8 100644 --- a/discovery/dns/dns_test.go +++ b/discovery/dns/dns_test.go @@ -22,12 +22,12 @@ import ( "github.com/go-kit/kit/log" "github.com/miekg/dns" + "github.com/prometheus/common/model" + "github.com/stretchr/testify/assert" "go.uber.org/goleak" "gopkg.in/yaml.v2" - "github.com/prometheus/common/model" "github.com/prometheus/prometheus/discovery/targetgroup" - "github.com/prometheus/prometheus/util/testutil" ) func TestMain(m *testing.M) { @@ -205,8 +205,8 @@ func TestDNS(t *testing.T) { sd.lookupFn = tc.lookup tgs, err := sd.refresh(context.Background()) - testutil.Ok(t, err) - testutil.Equals(t, tc.expected, tgs) + assert.NoError(t, err) + assert.Equal(t, tc.expected, tgs) }) } } @@ -296,7 +296,7 @@ func TestSDConfigUnmarshalYAML(t *testing.T) { var config SDConfig d := marshal(c.input) err := config.UnmarshalYAML(unmarshal(d)) - testutil.Equals(t, c.expectErr, err != nil) + assert.Equal(t, c.expectErr, err != nil) }) } } diff --git a/discovery/dockerswarm/mock_test.go b/discovery/dockerswarm/mock_test.go index 27f77f637f..9b2d54fd84 100644 --- a/discovery/dockerswarm/mock_test.go +++ b/discovery/dockerswarm/mock_test.go @@ -23,9 +23,10 @@ import ( "strings" "testing" - "github.com/prometheus/prometheus/util/strutil" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" "gopkg.in/yaml.v2" + + "github.com/prometheus/prometheus/util/strutil" ) // SDMock is the interface for the DigitalOcean mock @@ -63,7 +64,7 @@ func (m *SDMock) Setup() { func (m *SDMock) SetupHandlers() { headers := make(map[string]string) rawHeaders, err := ioutil.ReadFile(filepath.Join("testdata", m.directory, "headers.yml")) - testutil.Ok(m.t, err) + assert.NoError(m.t, err) yaml.Unmarshal(rawHeaders, &headers) prefix := "/" diff --git a/discovery/dockerswarm/network.go b/discovery/dockerswarm/network.go index 92e5ad8882..7d70169a67 100644 --- a/discovery/dockerswarm/network.go +++ b/discovery/dockerswarm/network.go @@ -18,6 +18,7 @@ import ( "fmt" "github.com/docker/docker/api/types" + "github.com/prometheus/prometheus/util/strutil" ) diff --git a/discovery/dockerswarm/nodes_test.go b/discovery/dockerswarm/nodes_test.go index cf557b1afd..dee8fa35ee 100644 --- a/discovery/dockerswarm/nodes_test.go +++ b/discovery/dockerswarm/nodes_test.go @@ -20,7 +20,7 @@ import ( "github.com/go-kit/kit/log" "github.com/prometheus/common/model" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" "gopkg.in/yaml.v2" ) @@ -36,21 +36,21 @@ role: nodes host: %s `, url) var cfg SDConfig - testutil.Ok(t, yaml.Unmarshal([]byte(cfgString), &cfg)) + assert.NoError(t, yaml.Unmarshal([]byte(cfgString), &cfg)) d, err := NewDiscovery(&cfg, log.NewNopLogger()) - testutil.Ok(t, err) + assert.NoError(t, err) ctx := context.Background() tgs, err := d.refresh(ctx) - testutil.Ok(t, err) + assert.NoError(t, err) - testutil.Equals(t, 1, len(tgs)) + assert.Equal(t, 1, len(tgs)) tg := tgs[0] - testutil.Assert(t, tg != nil, "tg should not be nil") - testutil.Assert(t, tg.Targets != nil, "tg.targets should not be nil") - testutil.Equals(t, 5, len(tg.Targets)) + assert.NotNil(t, tg) + assert.NotNil(t, tg.Targets) + assert.Equal(t, 5, len(tg.Targets)) for i, lbls := range []model.LabelSet{ { @@ -124,7 +124,7 @@ host: %s }, } { t.Run(fmt.Sprintf("item %d", i), func(t *testing.T) { - testutil.Equals(t, lbls, tg.Targets[i]) + assert.Equal(t, lbls, tg.Targets[i]) }) } } diff --git a/discovery/dockerswarm/services_test.go b/discovery/dockerswarm/services_test.go index 8240def0be..cdad9224a9 100644 --- a/discovery/dockerswarm/services_test.go +++ b/discovery/dockerswarm/services_test.go @@ -20,7 +20,7 @@ import ( "github.com/go-kit/kit/log" "github.com/prometheus/common/model" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" "gopkg.in/yaml.v2" ) @@ -36,21 +36,21 @@ role: services host: %s `, url) var cfg SDConfig - testutil.Ok(t, yaml.Unmarshal([]byte(cfgString), &cfg)) + assert.NoError(t, yaml.Unmarshal([]byte(cfgString), &cfg)) d, err := NewDiscovery(&cfg, log.NewNopLogger()) - testutil.Ok(t, err) + assert.NoError(t, err) ctx := context.Background() tgs, err := d.refresh(ctx) - testutil.Ok(t, err) + assert.NoError(t, err) - testutil.Equals(t, 1, len(tgs)) + assert.Equal(t, 1, len(tgs)) tg := tgs[0] - testutil.Assert(t, tg != nil, "tg should not be nil") - testutil.Assert(t, tg.Targets != nil, "tg.targets should not be nil") - testutil.Equals(t, 15, len(tg.Targets)) + assert.NotNil(t, tg) + assert.NotNil(t, tg.Targets) + assert.Equal(t, 15, len(tg.Targets)) for i, lbls := range []model.LabelSet{ { @@ -310,7 +310,7 @@ host: %s }, } { t.Run(fmt.Sprintf("item %d", i), func(t *testing.T) { - testutil.Equals(t, lbls, tg.Targets[i]) + assert.Equal(t, lbls, tg.Targets[i]) }) } } diff --git a/discovery/dockerswarm/tasks_test.go b/discovery/dockerswarm/tasks_test.go index dfd3716fcf..9db8ddaacc 100644 --- a/discovery/dockerswarm/tasks_test.go +++ b/discovery/dockerswarm/tasks_test.go @@ -20,7 +20,7 @@ import ( "github.com/go-kit/kit/log" "github.com/prometheus/common/model" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" "gopkg.in/yaml.v2" ) @@ -36,21 +36,21 @@ role: tasks host: %s `, url) var cfg SDConfig - testutil.Ok(t, yaml.Unmarshal([]byte(cfgString), &cfg)) + assert.NoError(t, yaml.Unmarshal([]byte(cfgString), &cfg)) d, err := NewDiscovery(&cfg, log.NewNopLogger()) - testutil.Ok(t, err) + assert.NoError(t, err) ctx := context.Background() tgs, err := d.refresh(ctx) - testutil.Ok(t, err) + assert.NoError(t, err) - testutil.Equals(t, 1, len(tgs)) + assert.Equal(t, 1, len(tgs)) tg := tgs[0] - testutil.Assert(t, tg != nil, "tg should not be nil") - testutil.Assert(t, tg.Targets != nil, "tg.targets should not be nil") - testutil.Equals(t, 27, len(tg.Targets)) + assert.NotNil(t, tg) + assert.NotNil(t, tg.Targets) + assert.Equal(t, 27, len(tg.Targets)) for i, lbls := range []model.LabelSet{ { @@ -787,7 +787,7 @@ host: %s }, } { t.Run(fmt.Sprintf("item %d", i), func(t *testing.T) { - testutil.Equals(t, lbls, tg.Targets[i]) + assert.Equal(t, lbls, tg.Targets[i]) }) } } diff --git a/discovery/eureka/client_test.go b/discovery/eureka/client_test.go index 9b7305a826..727d038aef 100644 --- a/discovery/eureka/client_test.go +++ b/discovery/eureka/client_test.go @@ -20,7 +20,7 @@ import ( "net/http/httptest" "testing" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" ) func TestFetchApps(t *testing.T) { @@ -182,19 +182,19 @@ func TestFetchApps(t *testing.T) { defer ts.Close() apps, err := fetchApps(context.TODO(), ts.URL, &http.Client{}) - testutil.Ok(t, err) + assert.NoError(t, err) - testutil.Equals(t, len(apps.Applications), 2) - testutil.Equals(t, apps.Applications[0].Name, "CONFIG-SERVICE") - testutil.Equals(t, apps.Applications[1].Name, "META-SERVICE") + assert.Equal(t, len(apps.Applications), 2) + assert.Equal(t, apps.Applications[0].Name, "CONFIG-SERVICE") + assert.Equal(t, apps.Applications[1].Name, "META-SERVICE") - testutil.Equals(t, len(apps.Applications[1].Instances), 2) - testutil.Equals(t, apps.Applications[1].Instances[0].InstanceID, "meta-service002.test.com:meta-service:8080") - testutil.Equals(t, apps.Applications[1].Instances[0].Metadata.Items[0].XMLName.Local, "project") - testutil.Equals(t, apps.Applications[1].Instances[0].Metadata.Items[0].Content, "meta-service") - testutil.Equals(t, apps.Applications[1].Instances[0].Metadata.Items[1].XMLName.Local, "management.port") - testutil.Equals(t, apps.Applications[1].Instances[0].Metadata.Items[1].Content, "8090") - testutil.Equals(t, apps.Applications[1].Instances[1].InstanceID, "meta-service001.test.com:meta-service:8080") + assert.Equal(t, len(apps.Applications[1].Instances), 2) + assert.Equal(t, apps.Applications[1].Instances[0].InstanceID, "meta-service002.test.com:meta-service:8080") + assert.Equal(t, apps.Applications[1].Instances[0].Metadata.Items[0].XMLName.Local, "project") + assert.Equal(t, apps.Applications[1].Instances[0].Metadata.Items[0].Content, "meta-service") + assert.Equal(t, apps.Applications[1].Instances[0].Metadata.Items[1].XMLName.Local, "management.port") + assert.Equal(t, apps.Applications[1].Instances[0].Metadata.Items[1].Content, "8090") + assert.Equal(t, apps.Applications[1].Instances[1].InstanceID, "meta-service001.test.com:meta-service:8080") } func Test500ErrorHttpResponse(t *testing.T) { @@ -209,5 +209,5 @@ func Test500ErrorHttpResponse(t *testing.T) { defer ts.Close() _, err := fetchApps(context.TODO(), ts.URL, &http.Client{}) - testutil.NotOk(t, err, "5xx HTTP response") + assert.Error(t, err, "5xx HTTP response") } diff --git a/discovery/eureka/eureka_test.go b/discovery/eureka/eureka_test.go index c2c67158ac..034cbd4254 100644 --- a/discovery/eureka/eureka_test.go +++ b/discovery/eureka/eureka_test.go @@ -15,14 +15,14 @@ package eureka import ( "context" - "github.com/prometheus/prometheus/util/testutil" "io" "net/http" "net/http/httptest" "testing" - "github.com/pkg/errors" "github.com/prometheus/common/model" + "github.com/stretchr/testify/assert" + "github.com/prometheus/prometheus/discovery/targetgroup" ) @@ -45,7 +45,7 @@ func testUpdateServices(respHandler http.HandlerFunc) ([]*targetgroup.Group, err func TestEurekaSDHandleError(t *testing.T) { var ( - errTesting = errors.Errorf("non 2xx status '%d' response during eureka service discovery", http.StatusInternalServerError) + errTesting = "non 2xx status '500' response during eureka service discovery" respHandler = func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusInternalServerError) w.Header().Set("Content-Type", "application/xml") @@ -54,8 +54,8 @@ func TestEurekaSDHandleError(t *testing.T) { ) tgs, err := testUpdateServices(respHandler) - testutil.ErrorEqual(t, err, errTesting) - testutil.Equals(t, len(tgs), 0) + assert.EqualError(t, err, errTesting) + assert.Equal(t, len(tgs), 0) } func TestEurekaSDEmptyList(t *testing.T) { @@ -71,8 +71,8 @@ func TestEurekaSDEmptyList(t *testing.T) { } ) tgs, err := testUpdateServices(respHandler) - testutil.Ok(t, err) - testutil.Equals(t, len(tgs), 1) + assert.NoError(t, err) + assert.Equal(t, len(tgs), 1) } func TestEurekaSDSendGroup(t *testing.T) { @@ -231,16 +231,16 @@ func TestEurekaSDSendGroup(t *testing.T) { ) tgs, err := testUpdateServices(respHandler) - testutil.Ok(t, err) - testutil.Equals(t, len(tgs), 1) + assert.NoError(t, err) + assert.Equal(t, len(tgs), 1) tg := tgs[0] - testutil.Equals(t, tg.Source, "eureka") - testutil.Equals(t, len(tg.Targets), 4) + assert.Equal(t, tg.Source, "eureka") + assert.Equal(t, len(tg.Targets), 4) tgt := tg.Targets[0] - testutil.Equals(t, tgt[model.AddressLabel], model.LabelValue("config-service001.test.com:8080")) + assert.Equal(t, tgt[model.AddressLabel], model.LabelValue("config-service001.test.com:8080")) tgt = tg.Targets[2] - testutil.Equals(t, tgt[model.AddressLabel], model.LabelValue("meta-service002.test.com:8080")) + assert.Equal(t, tgt[model.AddressLabel], model.LabelValue("meta-service002.test.com:8080")) } diff --git a/discovery/file/file_test.go b/discovery/file/file_test.go index b8fead21b8..b26459af4e 100644 --- a/discovery/file/file_test.go +++ b/discovery/file/file_test.go @@ -26,10 +26,10 @@ import ( "time" "github.com/prometheus/common/model" + "github.com/stretchr/testify/assert" "go.uber.org/goleak" "github.com/prometheus/prometheus/discovery/targetgroup" - "github.com/prometheus/prometheus/util/testutil" ) func TestMain(m *testing.M) { @@ -54,7 +54,7 @@ func newTestRunner(t *testing.T) *testRunner { t.Helper() tmpDir, err := ioutil.TempDir("", "prometheus-file-sd") - testutil.Ok(t, err) + assert.NoError(t, err) return &testRunner{ T: t, @@ -77,19 +77,19 @@ func (t *testRunner) copyFileTo(src string, name string) string { t.Helper() newf, err := ioutil.TempFile(t.dir, "") - testutil.Ok(t, err) + assert.NoError(t, err) f, err := os.Open(src) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = io.Copy(newf, f) - testutil.Ok(t, err) - testutil.Ok(t, f.Close()) - testutil.Ok(t, newf.Close()) + assert.NoError(t, err) + assert.NoError(t, f.Close()) + assert.NoError(t, newf.Close()) dst := filepath.Join(t.dir, name) err = os.Rename(newf.Name(), dst) - testutil.Ok(t, err) + assert.NoError(t, err) return dst } @@ -99,14 +99,14 @@ func (t *testRunner) writeString(file string, data string) { t.Helper() newf, err := ioutil.TempFile(t.dir, "") - testutil.Ok(t, err) + assert.NoError(t, err) _, err = newf.WriteString(data) - testutil.Ok(t, err) - testutil.Ok(t, newf.Close()) + assert.NoError(t, err) + assert.NoError(t, newf.Close()) err = os.Rename(newf.Name(), file) - testutil.Ok(t, err) + assert.NoError(t, err) } // appendString appends a string to a file. @@ -114,11 +114,11 @@ func (t *testRunner) appendString(file, data string) { t.Helper() f, err := os.OpenFile(file, os.O_WRONLY|os.O_APPEND, 0) - testutil.Ok(t, err) + assert.NoError(t, err) defer f.Close() _, err = f.WriteString(data) - testutil.Ok(t, err) + assert.NoError(t, err) } // run starts the file SD and the loop receiving target groups updates. @@ -230,7 +230,7 @@ func (t *testRunner) requireTargetGroups(expected, got []*targetgroup.Group) { panic(err) } - testutil.Equals(t, string(b1), string(b2)) + assert.Equal(t, string(b1), string(b2)) } // validTg() maps to fixtures/valid.{json,yml}. @@ -468,7 +468,7 @@ func TestRemoveFile(t *testing.T) { // Verify that we receive the update about the target groups being removed. ref := runner.lastReceive() - testutil.Ok(t, os.Remove(sdFile)) + assert.NoError(t, os.Remove(sdFile)) runner.requireUpdate( ref, []*targetgroup.Group{ diff --git a/discovery/hetzner/hcloud.go b/discovery/hetzner/hcloud.go index 66b96e63d5..20cfd1e582 100644 --- a/discovery/hetzner/hcloud.go +++ b/discovery/hetzner/hcloud.go @@ -26,6 +26,7 @@ import ( "github.com/prometheus/common/config" "github.com/prometheus/common/model" "github.com/prometheus/common/version" + "github.com/prometheus/prometheus/discovery/refresh" "github.com/prometheus/prometheus/discovery/targetgroup" "github.com/prometheus/prometheus/util/strutil" diff --git a/discovery/hetzner/hcloud_test.go b/discovery/hetzner/hcloud_test.go index 96fdc0262f..9cc41e8ba4 100644 --- a/discovery/hetzner/hcloud_test.go +++ b/discovery/hetzner/hcloud_test.go @@ -16,10 +16,11 @@ package hetzner import ( "context" "fmt" + "testing" + "github.com/go-kit/kit/log" "github.com/prometheus/common/model" - "github.com/prometheus/prometheus/util/testutil" - "testing" + "github.com/stretchr/testify/assert" ) type hcloudSDTestSuite struct { @@ -43,16 +44,16 @@ func TestHCloudSDRefresh(t *testing.T) { cfg.hcloudEndpoint = suite.Mock.Endpoint() d, err := newHcloudDiscovery(&cfg, log.NewNopLogger()) - testutil.Ok(t, err) + assert.NoError(t, err) targetGroups, err := d.refresh(context.Background()) - testutil.Ok(t, err) - testutil.Equals(t, 1, len(targetGroups)) + assert.NoError(t, err) + assert.Equal(t, 1, len(targetGroups)) targetGroup := targetGroups[0] - testutil.Assert(t, targetGroup != nil, "targetGroup should not be nil") - testutil.Assert(t, targetGroup.Targets != nil, "targetGroup.targets should not be nil") - testutil.Equals(t, 3, len(targetGroup.Targets)) + assert.NotNil(t, targetGroup, "targetGroup should not be nil") + assert.NotNil(t, targetGroup.Targets, "targetGroup.targets should not be nil") + assert.Equal(t, 3, len(targetGroup.Targets)) for i, labelSet := range []model.LabelSet{ { @@ -118,7 +119,7 @@ func TestHCloudSDRefresh(t *testing.T) { }, } { t.Run(fmt.Sprintf("item %d", i), func(t *testing.T) { - testutil.Equals(t, labelSet, targetGroup.Targets[i]) + assert.Equal(t, labelSet, targetGroup.Targets[i]) }) } } diff --git a/discovery/hetzner/hetzner.go b/discovery/hetzner/hetzner.go index 5b727464c6..9d1e3b264d 100644 --- a/discovery/hetzner/hetzner.go +++ b/discovery/hetzner/hetzner.go @@ -15,13 +15,14 @@ package hetzner import ( "context" - "github.com/pkg/errors" "time" "github.com/go-kit/kit/log" "github.com/hetznercloud/hcloud-go/hcloud" + "github.com/pkg/errors" "github.com/prometheus/common/config" "github.com/prometheus/common/model" + "github.com/prometheus/prometheus/discovery" "github.com/prometheus/prometheus/discovery/refresh" "github.com/prometheus/prometheus/discovery/targetgroup" diff --git a/discovery/hetzner/robot.go b/discovery/hetzner/robot.go index 4035d5fcfa..f3041d2c66 100644 --- a/discovery/hetzner/robot.go +++ b/discovery/hetzner/robot.go @@ -30,6 +30,7 @@ import ( "github.com/prometheus/common/config" "github.com/prometheus/common/model" "github.com/prometheus/common/version" + "github.com/prometheus/prometheus/discovery/refresh" "github.com/prometheus/prometheus/discovery/targetgroup" ) diff --git a/discovery/hetzner/robot_test.go b/discovery/hetzner/robot_test.go index b3271f92dd..8dc68d9369 100644 --- a/discovery/hetzner/robot_test.go +++ b/discovery/hetzner/robot_test.go @@ -16,11 +16,12 @@ package hetzner import ( "context" "fmt" + "testing" + "github.com/go-kit/kit/log" "github.com/prometheus/common/config" "github.com/prometheus/common/model" - "github.com/prometheus/prometheus/util/testutil" - "testing" + "github.com/stretchr/testify/assert" ) type robotSDTestSuite struct { @@ -42,16 +43,16 @@ func TestRobotSDRefresh(t *testing.T) { cfg.robotEndpoint = suite.Mock.Endpoint() d, err := newRobotDiscovery(&cfg, log.NewNopLogger()) - testutil.Ok(t, err) + assert.NoError(t, err) targetGroups, err := d.refresh(context.Background()) - testutil.Ok(t, err) - testutil.Equals(t, 1, len(targetGroups)) + assert.NoError(t, err) + assert.Equal(t, 1, len(targetGroups)) targetGroup := targetGroups[0] - testutil.Assert(t, targetGroup != nil, "targetGroup should not be nil") - testutil.Assert(t, targetGroup.Targets != nil, "targetGroup.targets should not be nil") - testutil.Equals(t, 2, len(targetGroup.Targets)) + assert.NotNil(t, targetGroup, "targetGroup should not be nil") + assert.NotNil(t, targetGroup.Targets, "targetGroup.targets should not be nil") + assert.Equal(t, 2, len(targetGroup.Targets)) for i, labelSet := range []model.LabelSet{ { @@ -79,7 +80,7 @@ func TestRobotSDRefresh(t *testing.T) { }, } { t.Run(fmt.Sprintf("item %d", i), func(t *testing.T) { - testutil.Equals(t, labelSet, targetGroup.Targets[i]) + assert.Equal(t, labelSet, targetGroup.Targets[i]) }) } } @@ -91,11 +92,11 @@ func TestRobotSDRefreshHandleError(t *testing.T) { cfg.robotEndpoint = suite.Mock.Endpoint() d, err := newRobotDiscovery(&cfg, log.NewNopLogger()) - testutil.Ok(t, err) + assert.NoError(t, err) targetGroups, err := d.refresh(context.Background()) - testutil.NotOk(t, err) - testutil.Equals(t, "non 2xx status '401' response during hetzner service discovery with role robot", err.Error()) + assert.Error(t, err) + assert.Equal(t, "non 2xx status '401' response during hetzner service discovery with role robot", err.Error()) - testutil.Equals(t, 0, len(targetGroups)) + assert.Equal(t, 0, len(targetGroups)) } diff --git a/discovery/kubernetes/endpointslice_test.go b/discovery/kubernetes/endpointslice_test.go index b6cb570810..b8ad505547 100644 --- a/discovery/kubernetes/endpointslice_test.go +++ b/discovery/kubernetes/endpointslice_test.go @@ -18,12 +18,13 @@ import ( "testing" "github.com/prometheus/common/model" - "github.com/prometheus/prometheus/discovery/targetgroup" v1 "k8s.io/api/core/v1" disv1beta1 "k8s.io/api/discovery/v1beta1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" + + "github.com/prometheus/prometheus/discovery/targetgroup" ) func strptr(str string) *string { diff --git a/discovery/kubernetes/kubernetes_test.go b/discovery/kubernetes/kubernetes_test.go index 7602647889..c095686fa9 100644 --- a/discovery/kubernetes/kubernetes_test.go +++ b/discovery/kubernetes/kubernetes_test.go @@ -20,6 +20,7 @@ import ( "time" "github.com/go-kit/kit/log" + "github.com/stretchr/testify/assert" "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes/fake" @@ -150,7 +151,7 @@ func requireTargetGroups(t *testing.T, expected, res map[string]*targetgroup.Gro panic(err) } - testutil.Equals(t, string(b1), string(b2)) + assert.Equal(t, string(b1), string(b2)) } type hasSynced interface { diff --git a/discovery/manager_test.go b/discovery/manager_test.go index 41d21cc736..603d98f5df 100644 --- a/discovery/manager_test.go +++ b/discovery/manager_test.go @@ -16,7 +16,6 @@ package discovery import ( "context" "fmt" - "reflect" "sort" "strconv" "testing" @@ -25,6 +24,8 @@ import ( "github.com/go-kit/kit/log" client_testutil "github.com/prometheus/client_golang/prometheus/testutil" "github.com/prometheus/common/model" + "github.com/stretchr/testify/assert" + "github.com/prometheus/prometheus/discovery/targetgroup" "github.com/prometheus/prometheus/util/testutil" ) @@ -696,25 +697,12 @@ func TestTargetUpdatesOrder(t *testing.T) { func assertEqualGroups(t *testing.T, got, expected []*targetgroup.Group, msg func(got, expected string) string) { t.Helper() - format := func(groups []*targetgroup.Group) string { - var s string - for i, group := range groups { - if i > 0 { - s += "," - } - s += group.Source + ":" + fmt.Sprint(group.Targets) - } - return s - } // Need to sort by the groups's source as the received order is not guaranteed. sort.Sort(byGroupSource(got)) sort.Sort(byGroupSource(expected)) - if !reflect.DeepEqual(got, expected) { - t.Errorf(msg(format(got), format(expected))) - } - + assert.Equal(t, expected, got) } func staticConfig(addrs ...string) StaticConfig { @@ -894,10 +882,7 @@ func TestApplyConfigDoesNotModifyStaticTargets(t *testing.T) { <-discoveryManager.SyncCh() for _, cfg := range cfgs { - if !reflect.DeepEqual(originalConfig, cfg) { - t.Fatalf("discovery manager modified static config \n expected: %v\n got: %v\n", - originalConfig, cfg) - } + assert.Equal(t, originalConfig, cfg) } } diff --git a/discovery/marathon/marathon_test.go b/discovery/marathon/marathon_test.go index b80636a1b3..8ac9713a23 100644 --- a/discovery/marathon/marathon_test.go +++ b/discovery/marathon/marathon_test.go @@ -22,6 +22,7 @@ import ( "testing" "github.com/prometheus/common/model" + "github.com/prometheus/prometheus/discovery/targetgroup" ) diff --git a/discovery/openstack/hypervisor_test.go b/discovery/openstack/hypervisor_test.go index 82c6315fcd..8184a1be40 100644 --- a/discovery/openstack/hypervisor_test.go +++ b/discovery/openstack/hypervisor_test.go @@ -19,7 +19,7 @@ import ( "testing" "github.com/prometheus/common/model" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" ) type OpenstackSDHypervisorTestSuite struct { @@ -55,12 +55,12 @@ func TestOpenstackSDHypervisorRefresh(t *testing.T) { hypervisor, _ := mock.openstackAuthSuccess() ctx := context.Background() tgs, err := hypervisor.refresh(ctx) - testutil.Equals(t, 1, len(tgs)) + assert.Equal(t, 1, len(tgs)) tg := tgs[0] - testutil.Ok(t, err) - testutil.Assert(t, tg != nil, "") - testutil.Assert(t, tg.Targets != nil, "") - testutil.Equals(t, 2, len(tg.Targets)) + assert.NoError(t, err) + assert.NotNil(t, tg) + assert.NotNil(t, tg.Targets) + assert.Equal(t, 2, len(tg.Targets)) for l, v := range map[string]string{ "__address__": "172.16.70.14:0", @@ -71,7 +71,7 @@ func TestOpenstackSDHypervisorRefresh(t *testing.T) { "__meta_openstack_hypervisor_status": "enabled", "__meta_openstack_hypervisor_id": "1", } { - testutil.Equals(t, model.LabelValue(v), tg.Targets[0][model.LabelName(l)]) + assert.Equal(t, model.LabelValue(v), tg.Targets[0][model.LabelName(l)]) } for l, v := range map[string]string{ @@ -83,7 +83,7 @@ func TestOpenstackSDHypervisorRefresh(t *testing.T) { "__meta_openstack_hypervisor_status": "enabled", "__meta_openstack_hypervisor_id": "721", } { - testutil.Equals(t, model.LabelValue(v), tg.Targets[1][model.LabelName(l)]) + assert.Equal(t, model.LabelValue(v), tg.Targets[1][model.LabelName(l)]) } } @@ -95,6 +95,6 @@ func TestOpenstackSDHypervisorRefreshWithDoneContext(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) cancel() _, err := hypervisor.refresh(ctx) - testutil.NotOk(t, err) - testutil.Assert(t, strings.Contains(err.Error(), context.Canceled.Error()), "%q doesn't contain %q", err, context.Canceled) + assert.Error(t, err) + assert.True(t, strings.Contains(err.Error(), context.Canceled.Error()), "%q doesn't contain %q", err, context.Canceled) } diff --git a/discovery/openstack/instance.go b/discovery/openstack/instance.go index 8ae4a05b70..a53d6274ef 100644 --- a/discovery/openstack/instance.go +++ b/discovery/openstack/instance.go @@ -27,6 +27,7 @@ import ( "github.com/gophercloud/gophercloud/pagination" "github.com/pkg/errors" "github.com/prometheus/common/model" + "github.com/prometheus/prometheus/discovery/targetgroup" "github.com/prometheus/prometheus/util/strutil" ) diff --git a/discovery/openstack/instance_test.go b/discovery/openstack/instance_test.go index da9232da4f..210048e29c 100644 --- a/discovery/openstack/instance_test.go +++ b/discovery/openstack/instance_test.go @@ -20,7 +20,7 @@ import ( "testing" "github.com/prometheus/common/model" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" ) type OpenstackSDInstanceTestSuite struct { @@ -57,18 +57,18 @@ func TestOpenstackSDInstanceRefresh(t *testing.T) { mock.SetupTest(t) instance, err := mock.openstackAuthSuccess() - testutil.Ok(t, err) + assert.NoError(t, err) ctx := context.Background() tgs, err := instance.refresh(ctx) - testutil.Ok(t, err) - testutil.Equals(t, 1, len(tgs)) + assert.NoError(t, err) + assert.Equal(t, 1, len(tgs)) tg := tgs[0] - testutil.Assert(t, tg != nil, "") - testutil.Assert(t, tg.Targets != nil, "") - testutil.Equals(t, 4, len(tg.Targets)) + assert.NotNil(t, tg) + assert.NotNil(t, tg.Targets) + assert.Equal(t, 4, len(tg.Targets)) for i, lbls := range []model.LabelSet{ { @@ -121,7 +121,7 @@ func TestOpenstackSDInstanceRefresh(t *testing.T) { }, } { t.Run(fmt.Sprintf("item %d", i), func(t *testing.T) { - testutil.Equals(t, lbls, tg.Targets[i]) + assert.Equal(t, lbls, tg.Targets[i]) }) } } @@ -134,6 +134,6 @@ func TestOpenstackSDInstanceRefreshWithDoneContext(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) cancel() _, err := hypervisor.refresh(ctx) - testutil.NotOk(t, err) - testutil.Assert(t, strings.Contains(err.Error(), context.Canceled.Error()), "%q doesn't contain %q", err, context.Canceled) + assert.Error(t, err) + assert.True(t, strings.Contains(err.Error(), context.Canceled.Error()), "%q doesn't contain %q", err, context.Canceled) } diff --git a/discovery/refresh/refresh_test.go b/discovery/refresh/refresh_test.go index 6463164ae8..60e47ad9b3 100644 --- a/discovery/refresh/refresh_test.go +++ b/discovery/refresh/refresh_test.go @@ -20,10 +20,10 @@ import ( "time" "github.com/prometheus/common/model" + "github.com/stretchr/testify/assert" "go.uber.org/goleak" "github.com/prometheus/prometheus/discovery/targetgroup" - "github.com/prometheus/prometheus/util/testutil" ) func TestMain(m *testing.M) { @@ -73,10 +73,10 @@ func TestRefresh(t *testing.T) { go d.Run(ctx, ch) tg := <-ch - testutil.Equals(t, tg1, tg) + assert.Equal(t, tg1, tg) tg = <-ch - testutil.Equals(t, tg2, tg) + assert.Equal(t, tg2, tg) tick := time.NewTicker(2 * interval) defer tick.Stop() diff --git a/discovery/registry.go b/discovery/registry.go index 5f9a40546a..2ebb36cb29 100644 --- a/discovery/registry.go +++ b/discovery/registry.go @@ -21,8 +21,9 @@ import ( "strings" "sync" - "github.com/prometheus/prometheus/discovery/targetgroup" "gopkg.in/yaml.v2" + + "github.com/prometheus/prometheus/discovery/targetgroup" ) const ( diff --git a/discovery/targetgroup/targetgroup_test.go b/discovery/targetgroup/targetgroup_test.go index 2676e1abe8..d496fbcada 100644 --- a/discovery/targetgroup/targetgroup_test.go +++ b/discovery/targetgroup/targetgroup_test.go @@ -18,9 +18,8 @@ import ( "testing" "github.com/prometheus/common/model" + "github.com/stretchr/testify/assert" "gopkg.in/yaml.v2" - - "github.com/prometheus/prometheus/util/testutil" ) func TestTargetGroupStrictJsonUnmarshal(t *testing.T) { @@ -54,8 +53,8 @@ func TestTargetGroupStrictJsonUnmarshal(t *testing.T) { for _, test := range tests { tg := Group{} actual := tg.UnmarshalJSON([]byte(test.json)) - testutil.Equals(t, test.expectedReply, actual) - testutil.Equals(t, test.expectedGroup, tg) + assert.Equal(t, test.expectedReply, actual) + assert.Equal(t, test.expectedGroup, tg) } } @@ -93,8 +92,8 @@ func TestTargetGroupYamlMarshal(t *testing.T) { for _, test := range tests { actual, err := test.group.MarshalYAML() - testutil.Equals(t, test.expectedErr, err) - testutil.Equals(t, test.expectedYaml, string(marshal(actual))) + assert.Equal(t, test.expectedErr, err) + assert.Equal(t, test.expectedYaml, string(marshal(actual))) } } @@ -133,8 +132,8 @@ func TestTargetGroupYamlUnmarshal(t *testing.T) { for _, test := range tests { tg := Group{} actual := tg.UnmarshalYAML(unmarshal([]byte(test.yaml))) - testutil.Equals(t, test.expectedReply, actual) - testutil.Equals(t, test.expectedGroup, tg) + assert.Equal(t, test.expectedReply, actual) + assert.Equal(t, test.expectedGroup, tg) } } @@ -151,7 +150,7 @@ func TestString(t *testing.T) { Group{Targets: []model.LabelSet{}, Source: "", Labels: model.LabelSet{}} - testutil.Equals(t, "", group1.String()) - testutil.Equals(t, "", group2.String()) - testutil.Equals(t, group1.String(), group2.String()) + assert.Equal(t, "", group1.String()) + assert.Equal(t, "", group2.String()) + assert.Equal(t, group1.String(), group2.String()) } diff --git a/discovery/triton/triton_test.go b/discovery/triton/triton_test.go index 48f129baba..46ba2fbc1a 100644 --- a/discovery/triton/triton_test.go +++ b/discovery/triton/triton_test.go @@ -26,8 +26,7 @@ import ( "github.com/prometheus/common/config" "github.com/prometheus/common/model" - - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" ) var ( @@ -85,54 +84,54 @@ func newTritonDiscovery(c SDConfig) (*Discovery, error) { func TestTritonSDNew(t *testing.T) { td, err := newTritonDiscovery(conf) - testutil.Ok(t, err) - testutil.Assert(t, td != nil, "") - testutil.Assert(t, td.client != nil, "") - testutil.Assert(t, td.interval != 0, "") - testutil.Assert(t, td.sdConfig != nil, "") - testutil.Equals(t, conf.Account, td.sdConfig.Account) - testutil.Equals(t, conf.DNSSuffix, td.sdConfig.DNSSuffix) - testutil.Equals(t, conf.Endpoint, td.sdConfig.Endpoint) - testutil.Equals(t, conf.Port, td.sdConfig.Port) + assert.NoError(t, err) + assert.NotNil(t, td) + assert.NotNil(t, td.client) + assert.True(t, td.interval != 0, "") + assert.NotNil(t, td.sdConfig) + assert.Equal(t, conf.Account, td.sdConfig.Account) + assert.Equal(t, conf.DNSSuffix, td.sdConfig.DNSSuffix) + assert.Equal(t, conf.Endpoint, td.sdConfig.Endpoint) + assert.Equal(t, conf.Port, td.sdConfig.Port) } func TestTritonSDNewBadConfig(t *testing.T) { td, err := newTritonDiscovery(badconf) - testutil.NotOk(t, err) - testutil.Assert(t, td == nil, "") + assert.Error(t, err) + assert.True(t, td == nil, "") } func TestTritonSDNewGroupsConfig(t *testing.T) { td, err := newTritonDiscovery(groupsconf) - testutil.Ok(t, err) - testutil.Assert(t, td != nil, "") - testutil.Assert(t, td.client != nil, "") - testutil.Assert(t, td.interval != 0, "") - testutil.Assert(t, td.sdConfig != nil, "") - testutil.Equals(t, groupsconf.Account, td.sdConfig.Account) - testutil.Equals(t, groupsconf.DNSSuffix, td.sdConfig.DNSSuffix) - testutil.Equals(t, groupsconf.Endpoint, td.sdConfig.Endpoint) - testutil.Equals(t, groupsconf.Groups, td.sdConfig.Groups) - testutil.Equals(t, groupsconf.Port, td.sdConfig.Port) + assert.NoError(t, err) + assert.NotNil(t, td) + assert.NotNil(t, td.client) + assert.True(t, td.interval != 0, "") + assert.NotNil(t, td.sdConfig) + assert.Equal(t, groupsconf.Account, td.sdConfig.Account) + assert.Equal(t, groupsconf.DNSSuffix, td.sdConfig.DNSSuffix) + assert.Equal(t, groupsconf.Endpoint, td.sdConfig.Endpoint) + assert.Equal(t, groupsconf.Groups, td.sdConfig.Groups) + assert.Equal(t, groupsconf.Port, td.sdConfig.Port) } func TestTritonSDNewCNConfig(t *testing.T) { td, err := newTritonDiscovery(cnconf) - testutil.Ok(t, err) - testutil.Assert(t, td != nil, "") - testutil.Assert(t, td.client != nil, "") - testutil.Assert(t, td.interval != 0, "") - testutil.Assert(t, td.sdConfig != nil, "") - testutil.Equals(t, cnconf.Role, td.sdConfig.Role) - testutil.Equals(t, cnconf.Account, td.sdConfig.Account) - testutil.Equals(t, cnconf.DNSSuffix, td.sdConfig.DNSSuffix) - testutil.Equals(t, cnconf.Endpoint, td.sdConfig.Endpoint) - testutil.Equals(t, cnconf.Port, td.sdConfig.Port) + assert.NoError(t, err) + assert.NotNil(t, td) + assert.NotNil(t, td.client) + assert.True(t, td.interval != 0, "") + assert.NotNil(t, td.sdConfig) + assert.Equal(t, cnconf.Role, td.sdConfig.Role) + assert.Equal(t, cnconf.Account, td.sdConfig.Account) + assert.Equal(t, cnconf.DNSSuffix, td.sdConfig.DNSSuffix) + assert.Equal(t, cnconf.Endpoint, td.sdConfig.Endpoint) + assert.Equal(t, cnconf.Port, td.sdConfig.Port) } func TestTritonSDRefreshNoTargets(t *testing.T) { tgts := testTritonSDRefresh(t, conf, "{\"containers\":[]}") - testutil.Assert(t, tgts == nil, "") + assert.True(t, tgts == nil, "") } func TestTritonSDRefreshMultipleTargets(t *testing.T) { @@ -157,8 +156,8 @@ func TestTritonSDRefreshMultipleTargets(t *testing.T) { ) tgts := testTritonSDRefresh(t, conf, dstr) - testutil.Assert(t, tgts != nil, "") - testutil.Equals(t, 2, len(tgts)) + assert.NotNil(t, tgts) + assert.Equal(t, 2, len(tgts)) } func TestTritonSDRefreshNoServer(t *testing.T) { @@ -167,8 +166,8 @@ func TestTritonSDRefreshNoServer(t *testing.T) { ) _, err := td.refresh(context.Background()) - testutil.NotOk(t, err) - testutil.Equals(t, strings.Contains(err.Error(), "an error occurred when requesting targets from the discovery endpoint"), true) + assert.Error(t, err) + assert.Equal(t, strings.Contains(err.Error(), "an error occurred when requesting targets from the discovery endpoint"), true) } func TestTritonSDRefreshCancelled(t *testing.T) { @@ -179,8 +178,8 @@ func TestTritonSDRefreshCancelled(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) cancel() _, err := td.refresh(ctx) - testutil.NotOk(t, err) - testutil.Equals(t, strings.Contains(err.Error(), context.Canceled.Error()), true) + assert.Error(t, err) + assert.Equal(t, strings.Contains(err.Error(), context.Canceled.Error()), true) } func TestTritonSDRefreshCNsUUIDOnly(t *testing.T) { @@ -196,8 +195,8 @@ func TestTritonSDRefreshCNsUUIDOnly(t *testing.T) { ) tgts := testTritonSDRefresh(t, cnconf, dstr) - testutil.Assert(t, tgts != nil, "") - testutil.Equals(t, 2, len(tgts)) + assert.NotNil(t, tgts) + assert.Equal(t, 2, len(tgts)) } func TestTritonSDRefreshCNsWithHostname(t *testing.T) { @@ -215,8 +214,8 @@ func TestTritonSDRefreshCNsWithHostname(t *testing.T) { ) tgts := testTritonSDRefresh(t, cnconf, dstr) - testutil.Assert(t, tgts != nil, "") - testutil.Equals(t, 2, len(tgts)) + assert.NotNil(t, tgts) + assert.Equal(t, 2, len(tgts)) } func testTritonSDRefresh(t *testing.T, c SDConfig, dstr string) []model.LabelSet { @@ -230,25 +229,25 @@ func testTritonSDRefresh(t *testing.T, c SDConfig, dstr string) []model.LabelSet defer s.Close() u, err := url.Parse(s.URL) - testutil.Ok(t, err) - testutil.Assert(t, u != nil, "") + assert.NoError(t, err) + assert.NotNil(t, u) host, strport, err := net.SplitHostPort(u.Host) - testutil.Ok(t, err) - testutil.Assert(t, host != "", "") - testutil.Assert(t, strport != "", "") + assert.NoError(t, err) + assert.True(t, host != "", "") + assert.True(t, strport != "", "") port, err := strconv.Atoi(strport) - testutil.Ok(t, err) - testutil.Assert(t, port != 0, "") + assert.NoError(t, err) + assert.True(t, port != 0, "") td.sdConfig.Port = port tgs, err := td.refresh(context.Background()) - testutil.Ok(t, err) - testutil.Equals(t, 1, len(tgs)) + assert.NoError(t, err) + assert.Equal(t, 1, len(tgs)) tg := tgs[0] - testutil.Assert(t, tg != nil, "") + assert.NotNil(t, tg) return tg.Targets } diff --git a/documentation/examples/custom-sd/adapter-usage/main.go b/documentation/examples/custom-sd/adapter-usage/main.go index aff9d76e08..13a1a90eed 100644 --- a/documentation/examples/custom-sd/adapter-usage/main.go +++ b/documentation/examples/custom-sd/adapter-usage/main.go @@ -29,10 +29,11 @@ import ( "github.com/go-kit/kit/log" "github.com/go-kit/kit/log/level" "github.com/prometheus/common/model" + "gopkg.in/alecthomas/kingpin.v2" + "github.com/prometheus/prometheus/discovery/targetgroup" "github.com/prometheus/prometheus/documentation/examples/custom-sd/adapter" "github.com/prometheus/prometheus/util/strutil" - "gopkg.in/alecthomas/kingpin.v2" ) var ( diff --git a/documentation/examples/custom-sd/adapter/adapter.go b/documentation/examples/custom-sd/adapter/adapter.go index 806eff9abb..5365d798c2 100644 --- a/documentation/examples/custom-sd/adapter/adapter.go +++ b/documentation/examples/custom-sd/adapter/adapter.go @@ -27,6 +27,7 @@ import ( "github.com/go-kit/kit/log" "github.com/go-kit/kit/log/level" "github.com/prometheus/common/model" + "github.com/prometheus/prometheus/discovery" "github.com/prometheus/prometheus/discovery/targetgroup" ) diff --git a/documentation/examples/custom-sd/adapter/adapter_test.go b/documentation/examples/custom-sd/adapter/adapter_test.go index 40cbbbacf2..ee0a463a0f 100644 --- a/documentation/examples/custom-sd/adapter/adapter_test.go +++ b/documentation/examples/custom-sd/adapter/adapter_test.go @@ -15,13 +15,14 @@ package adapter import ( "context" - "github.com/prometheus/prometheus/util/testutil" "io/ioutil" "os" "reflect" "testing" "github.com/prometheus/common/model" + "github.com/stretchr/testify/assert" + "github.com/prometheus/prometheus/discovery/targetgroup" ) @@ -231,9 +232,9 @@ func TestGenerateTargetGroups(t *testing.T) { func TestWriteOutput(t *testing.T) { ctx := context.Background() tmpfile, err := ioutil.TempFile("", "sd_adapter_test") - testutil.Ok(t, err) + assert.NoError(t, err) defer os.Remove(tmpfile.Name()) tmpfile.Close() adapter := NewAdapter(ctx, tmpfile.Name(), "test_sd", nil, nil) - testutil.Ok(t, adapter.writeOutput()) + assert.NoError(t, adapter.writeOutput()) } diff --git a/documentation/examples/remote_storage/remote_storage_adapter/influxdb/client_test.go b/documentation/examples/remote_storage/remote_storage_adapter/influxdb/client_test.go index 3903ca9c56..15bab1d9c9 100644 --- a/documentation/examples/remote_storage/remote_storage_adapter/influxdb/client_test.go +++ b/documentation/examples/remote_storage/remote_storage_adapter/influxdb/client_test.go @@ -23,7 +23,6 @@ import ( "time" influx "github.com/influxdata/influxdb/client/v2" - "github.com/prometheus/common/model" ) diff --git a/documentation/examples/remote_storage/remote_storage_adapter/main.go b/documentation/examples/remote_storage/remote_storage_adapter/main.go index a21d9f1b38..f445fb0240 100644 --- a/documentation/examples/remote_storage/remote_storage_adapter/main.go +++ b/documentation/examples/remote_storage/remote_storage_adapter/main.go @@ -29,16 +29,14 @@ import ( "github.com/go-kit/kit/log/level" "github.com/gogo/protobuf/proto" "github.com/golang/snappy" + influx "github.com/influxdata/influxdb/client/v2" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "github.com/prometheus/common/model" - "gopkg.in/alecthomas/kingpin.v2" - - influx "github.com/influxdata/influxdb/client/v2" - "github.com/prometheus/common/promlog" "github.com/prometheus/common/promlog/flag" + "gopkg.in/alecthomas/kingpin.v2" "github.com/prometheus/prometheus/documentation/examples/remote_storage/remote_storage_adapter/graphite" "github.com/prometheus/prometheus/documentation/examples/remote_storage/remote_storage_adapter/influxdb" diff --git a/go.mod b/go.mod index 358cd35b56..39c8967157 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,6 @@ require ( github.com/aws/aws-sdk-go v1.35.5 github.com/cespare/xxhash/v2 v2.1.1 github.com/containerd/containerd v1.3.4 // indirect - github.com/davecgh/go-spew v1.1.1 github.com/dgryski/go-sip13 v0.0.0-20200911182023-62edffca9245 github.com/digitalocean/godo v1.46.0 github.com/docker/distribution v2.7.1+incompatible // indirect @@ -49,7 +48,6 @@ require ( github.com/opentracing-contrib/go-stdlib v1.0.0 github.com/opentracing/opentracing-go v1.2.0 github.com/pkg/errors v0.9.1 - github.com/pmezard/go-difflib v1.0.0 github.com/prometheus/alertmanager v0.21.0 github.com/prometheus/client_golang v1.7.1 github.com/prometheus/client_model v0.2.0 @@ -57,6 +55,7 @@ require ( github.com/samuel/go-zookeeper v0.0.0-20200724154423-2164a8ac840e github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546 + github.com/stretchr/testify v1.6.1 github.com/uber/jaeger-client-go v2.25.0+incompatible github.com/uber/jaeger-lib v2.4.0+incompatible go.mongodb.org/mongo-driver v1.3.2 // indirect @@ -67,7 +66,7 @@ require ( golang.org/x/sync v0.0.0-20200930132711-30421366ff76 golang.org/x/sys v0.0.0-20201008064518-c1f3e3309c71 golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e - golang.org/x/tools v0.0.0-20201008025239-9df69603baec + golang.org/x/tools v0.0.0-20201020161133-226fd2f889ca google.golang.org/api v0.32.0 gopkg.in/alecthomas/kingpin.v2 v2.2.6 gopkg.in/fsnotify/fsnotify.v1 v1.4.7 diff --git a/go.sum b/go.sum index 9a16a18380..54f5823d14 100644 --- a/go.sum +++ b/go.sum @@ -11,7 +11,6 @@ cloud.google.com/go v0.51.0/go.mod h1:hWtGJ6gnXH+KgDv+V0zFGDvpi07n3z8ZNj3T1RW0Gc cloud.google.com/go v0.52.0/go.mod h1:pXajvRH/6o3+F9jDHZWQ5PbGhn+o8w9qiu/CffaVdO4= cloud.google.com/go v0.53.0/go.mod h1:fp/UouUEsRkN6ryDKNW/Upv/JBKnv6WDthjR6+vze6M= cloud.google.com/go v0.54.0/go.mod h1:1rq2OEkV3YMf6n/9ZvGWI3GWw0VoqH/1x2nd8Is/bPc= -cloud.google.com/go v0.56.0 h1:WRz29PgAsVEyPSDHyk+0fpEkwEFyfhHn+JbksT6gIL4= cloud.google.com/go v0.56.0/go.mod h1:jr7tqZxxKOVYizybht9+26Z/gUq7tiRzu+ACVAMbKVk= cloud.google.com/go v0.57.0/go.mod h1:oXiQ6Rzq3RAkkY7N6t3TcE6jE+CIBBbA36lwQ1JyzZs= cloud.google.com/go v0.65.0 h1:Dg9iHVQfrhq82rUNu9ZxUDrJLaxFUe/HlCVaLyRruq8= @@ -63,11 +62,9 @@ github.com/Azure/go-autorest/autorest/to v0.3.0 h1:zebkZaadz7+wIQYgC7GXaz3Wb28yK github.com/Azure/go-autorest/autorest/to v0.3.0/go.mod h1:MgwOyqaIuKdG4TL/2ywSsIWKAfJfgHDo8ObuUk3t5sA= github.com/Azure/go-autorest/autorest/validation v0.2.0 h1:15vMO4y76dehZSq7pAaOLQxC6dZYsSrj2GQpflyM/L4= github.com/Azure/go-autorest/autorest/validation v0.2.0/go.mod h1:3EEqHnBxQGHXRYq3HT1WyXAvT7LLY3tl70hw6tQIbjI= -github.com/Azure/go-autorest/logger v0.1.0 h1:ruG4BSDXONFRrZZJ2GUXDiUyVpayPmb1GnWeHDdaNKY= github.com/Azure/go-autorest/logger v0.1.0/go.mod h1:oExouG+K6PryycPJfVSxi/koC6LSNgds39diKLz7Vrc= github.com/Azure/go-autorest/logger v0.2.0 h1:e4RVHVZKC5p6UANLJHkM4OfR1UKZPj8Wt8Pcx+3oqrE= github.com/Azure/go-autorest/logger v0.2.0/go.mod h1:T9E3cAhj2VqvPOtCYAvby9aBXkZmbF5NWuPV8+WeEW8= -github.com/Azure/go-autorest/tracing v0.5.0 h1:TRn4WjSnkcSy5AEG3pnbtFSwNtwzjr4VYyQflFE619k= github.com/Azure/go-autorest/tracing v0.5.0/go.mod h1:r/s2XiOKccPW3HrqB+W0TQzfbtp2fGCgRFtBroKn4Dk= github.com/Azure/go-autorest/tracing v0.6.0 h1:TYi4+3m5t6K48TGI9AUdb+IzbnSxvnvUMfuitfgcfuo= github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBpUA79WCAKPPZVC2DeU= @@ -81,7 +78,6 @@ github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go github.com/Microsoft/go-winio v0.4.14 h1:+hMXMk01us9KgxGb7ftKQt2Xpf5hH/yky+TDA+qxleU= github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA= github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ= -github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= github.com/PuerkitoBio/purell v1.0.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= github.com/PuerkitoBio/purell v1.1.0/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0= @@ -141,7 +137,6 @@ github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghf github.com/cespare/xxhash/v2 v2.1.1 h1:6MnRN8NT7+YBpUIWxHtefFZOKTAPgGjpQSxqLNn0+qY= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= -github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e h1:fY5BOSpyZCqRo5OhCuC+XN+r/bBCmeuuJtjz+bCNIf8= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= github.com/circonus-labs/circonus-gometrics v2.3.1+incompatible/go.mod h1:nmEj6Dob7S7YxXgwXpfOuvO54S+tGdZdw9fuRZt25Ag= @@ -163,7 +158,6 @@ github.com/dave/jennifer v1.2.0/go.mod h1:fIb+770HOpJ2fmN9EPPKOqm1vMGhB+TwXKMZhr github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-bitstream v0.0.0-20180413035011-3522498ce2c8/go.mod h1:VMaSuZ+SZcx/wljOQKvp5srsbCiKDEb6K2wC4+PiBmQ= github.com/dgryski/go-sip13 v0.0.0-20200911182023-62edffca9245 h1:9cOfvEwjQxdwKuNDTQSaMKNRvwKwgZG+U4HrjeRKHso= @@ -205,7 +199,6 @@ github.com/form3tech-oss/jwt-go v3.2.2+incompatible h1:TcekIExNqud5crz4xD2pavyTg github.com/form3tech-oss/jwt-go v3.2.2+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k= github.com/franela/goblin v0.0.0-20200105215937-c9ffbefa60db/go.mod h1:7dvUGVsVBjqR7JHJk0brhHOZYGmfBYOrK0ZhYMEtBr4= github.com/franela/goreq v0.0.0-20171204163338-bcd34c9993f8/go.mod h1:ZhphrRTfi2rbfLwlschooIH4+wKKDR4Pdxhh+TRoA20= -github.com/fsnotify/fsnotify v1.4.7 h1:IXs+QLmnXW2CcXuY+8Mzv/fWEsPGWxqefPtCP5CnV9I= github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= github.com/fsnotify/fsnotify v1.4.9 h1:hsms1Qyu0jgnwNXIxa+/V/PDsU6CfLf6CNO8H7IWoS4= github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ= @@ -361,7 +354,6 @@ github.com/golang/protobuf v1.4.1/go.mod h1:U8fpvMrcmy5pZrNK1lt4xCsGvpyWQ/VVv6QD github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0= github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= -github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4= github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golang/snappy v0.0.2 h1:aeE13tS0IiQgFjYdoL8qN3K1N2bXXtI6Vi51/y7BpMw= github.com/golang/snappy v0.0.2/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= @@ -372,10 +364,8 @@ github.com/google/flatbuffers v1.11.0/go.mod h1:1AeVuKshWv4vARoZatz6mlQ0JxURH0Kv github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= -github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.4.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.0 h1:/QaMHBdZ26BB3SSst0Iwl10Epc+xhTquomWX0oZEB6w= github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.2 h1:X2ev0eStA3AbceY54o37/0PQ/UWqKEiiO2dKL5OPaFM= @@ -393,7 +383,6 @@ github.com/google/pprof v0.0.0-20191218002539-d4f498aebedc/go.mod h1:ZgVRPoUq/hf github.com/google/pprof v0.0.0-20200212024743-f11f1df84d12/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= github.com/google/pprof v0.0.0-20200229191704-1ebb73c60ed3/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= github.com/google/pprof v0.0.0-20200430221834-fc25d7d30c6d/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= -github.com/google/pprof v0.0.0-20200708004538-1a94d8640e99 h1:Ak8CrdlwwXwAZxzS66vgPt4U8yUZX7JwLvVR58FN5jM= github.com/google/pprof v0.0.0-20200708004538-1a94d8640e99/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= github.com/google/pprof v0.0.0-20201007051231-1066cbb265c7 h1:qYWTuM6SUNWgtvkhV8oH6GFHCpU+rKQOxPcepM3xKi0= github.com/google/pprof v0.0.0-20201007051231-1066cbb265c7/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM= @@ -414,7 +403,6 @@ github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2z github.com/gorilla/mux v1.7.3 h1:gnP5JzjVOuiZD07fKKToCAOjS0yOpj/qPETTXCCS6hw= github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs= github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= -github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7 h1:pdN6V1QBWetyv/0+wjACpqVH+eVULgEjkurDLq3goeM= github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA= github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk= @@ -440,7 +428,6 @@ github.com/hashicorp/go-immutable-radix v1.2.0 h1:l6UW37iCXwZkZoAbEYnptSHVE/cQ5b github.com/hashicorp/go-immutable-radix v1.2.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60= github.com/hashicorp/go-msgpack v0.5.3 h1:zKjpN5BK/P5lMYrLmBHdBULWbJ0XpYR+7NGzqkZzoD4= github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM= -github.com/hashicorp/go-multierror v1.0.0 h1:iVjPR7a6H0tWELX5NxNe7bYopibicUzc7uPribsnS6o= github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk= github.com/hashicorp/go-multierror v1.1.0 h1:B9UzwGQJehnUY1yNrnwREHc3fGbC2xefo8g4TbElacI= github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA= @@ -475,7 +462,6 @@ github.com/hetznercloud/hcloud-go v1.22.0/go.mod h1:xng8lbDUg+xM1dgc0yGHX5EeqbwI github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/hudl/fargo v1.3.0/go.mod h1:y3CKSmjA+wD2gak7sUSXTAoopbhU08POFhmITJgmKTg= -github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6 h1:UDMh68UUwekSh5iP2OMhRRZJiiBccgV7axzUG8vi56c= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/imdario/mergo v0.3.5/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= @@ -525,13 +511,11 @@ github.com/klauspost/cpuid v0.0.0-20170728055534-ae7887de9fa5/go.mod h1:Pj4uuM52 github.com/klauspost/crc32 v0.0.0-20161016154125-cb6bfca970f6/go.mod h1:+ZoRqAPRLkC4NPOvfYeR5KNOrY6TD+/sAC3HXPZgDYg= github.com/klauspost/pgzip v1.0.2-0.20170402124221-0bf5dcad4ada/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs= github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= -github.com/konsorten/go-windows-terminal-sequences v1.0.2 h1:DB17ag19krx9CFsz4o3enTrPXyIXCl+2iCXH/aMAp9s= github.com/konsorten/go-windows-terminal-sequences v1.0.2/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8= github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= -github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs= github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= @@ -638,7 +622,6 @@ github.com/opentracing-contrib/go-stdlib v1.0.0/go.mod h1:qtI1ogk+2JhVPIXVc6q+NH github.com/opentracing/basictracer-go v1.0.0/go.mod h1:QfBfYuafItcjQuMwinw9GhYKwFXS9KnPs5lxoYwgW74= github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/opentracing/opentracing-go v1.0.3-0.20180606204148-bd9c31933947/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= -github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU= github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o= github.com/opentracing/opentracing-go v1.2.0 h1:uEJPy/1a5RIPAJ0Ov+OIO8OxWu77jEv+1B0VhjKrZUs= github.com/opentracing/opentracing-go v1.2.0/go.mod h1:GxEUsuufX4nBwe+T+Wl9TAgYrxe9dPLANfrWvHYVTgc= @@ -654,7 +637,6 @@ github.com/paulbellamy/ratecounter v0.2.0/go.mod h1:Hfx1hDpSGoqxkVVpBi/IlYD7kChl github.com/pborman/uuid v1.2.0/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k= github.com/pelletier/go-toml v1.4.0/go.mod h1:PN7xzY2wHTK0K9p34ErDQMlFxa51Fk0OUruD3k1mMwo= github.com/performancecopilot/speed v3.0.0+incompatible/go.mod h1:/CLtqpZ5gBg1M9iaPbIdPPGyKcA8hKdoy6hAWba7Yac= -github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI= github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU= github.com/peterh/liner v1.0.1-0.20180619022028-8c1271fcf47f/go.mod h1:xIteQHvHuaLYG9IFj6mSxM0fCKrs34IrEQUhOYuGPHc= github.com/philhofer/fwd v1.0.0/go.mod h1:gk3iGcWd9+svBvR0sR+KPcfE+RNWozjowpeBVG3ZVNU= @@ -692,7 +674,6 @@ github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y8 github.com/prometheus/common v0.6.0/go.mod h1:eBmuwkDJBwy6iBfxCBob6t6dR6ENT/y+J+Zk0j9GMYc= github.com/prometheus/common v0.7.0/go.mod h1:DjGbpBbp5NYNiECxcL/VnbXCCaQpKd3tt26CguLLsqA= github.com/prometheus/common v0.9.1/go.mod h1:yhUN8i9wzaXS3w1O07YhxHEBxD+W35wd8bs7vj7HSQ4= -github.com/prometheus/common v0.10.0 h1:RyRA7RzGXQZiW+tGMr7sxa85G1z0yOpM1qq5c8lNawc= github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo= github.com/prometheus/common v0.14.0 h1:RHRyE8UocrbjU+6UvRzwi6HjiDfxrrBU91TtbKzkGp4= github.com/prometheus/common v0.14.0/go.mod h1:U+gB1OBLb1lF3O42bTCL+FK18tX9Oar16Clt/msog/s= @@ -714,7 +695,6 @@ github.com/rs/cors v1.7.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU= github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts= -github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da h1:p3Vo3i64TCLY7gIfzeQaUJ+kppEO5WQG3cL8iE8tGHU= github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E= github.com/samuel/go-zookeeper v0.0.0-20200724154423-2164a8ac840e h1:CGjiMQ0wMH4wtNWrlj6kiTbkPt2F3rbYnhGX6TWLfco= github.com/samuel/go-zookeeper v0.0.0-20200724154423-2164a8ac840e/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E= @@ -727,7 +707,6 @@ github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAm github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 h1:bUGsEnyNbVPw06Bs80sCeARAlK8lhwqGyi6UT8ymuGk= github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= -github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd h1:ug7PpSOB5RBPK1Kg6qskGBoP3Vnj/aNYFTznWvlkGo0= github.com/shurcooL/vfsgen v0.0.0-20181202132449-6a9ea43bcacd/go.mod h1:TrYk7fJVaAttu97ZZKrO9UbRa8izdowaMIZcxYMbVaw= github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546 h1:pXY9qYc/MP5zdvqWEUH6SjNiu7VhSjuVFTFiTcphaLU= github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546/go.mod h1:TrYk7fJVaAttu97ZZKrO9UbRa8izdowaMIZcxYMbVaw= @@ -738,16 +717,13 @@ github.com/simonpasquier/klog-gokit/v2 v2.0.1/go.mod h1:VgeTFrwzYYcMH8edEfh3/ai2 github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= -github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I= github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA= -github.com/soheilhy/cmux v0.1.4 h1:0HKaf1o97UwFjHH9o5XsHUOF+tqmdA7KEzXLpiyaw0E= github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM= github.com/sony/gobreaker v0.4.1/go.mod h1:ZKptC7FHNvhBz7dN2LGjPVBz2sZJmc0/PkyDJOjmxWY= -github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 h1:qLC7fQah7D6K1B0ujays3HV9gkFtllcxhzImRR7ArPQ= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= @@ -768,8 +744,9 @@ github.com/stretchr/testify v1.2.0/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= -github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= github.com/tinylib/msgp v1.0.2/go.mod h1:+d+yLhGm8mzTaHzB+wgMYrodPfmZrzkirds8fDWklFE= @@ -805,7 +782,6 @@ go.opencensus.io v0.20.2/go.mod h1:6WKK9ahsWS3RSO+PY9ZHZUfv2irvY6gN279GOPZjmmk= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= -go.opencensus.io v0.22.3 h1:8sGtKOrtQqkN1bp2AtX+misvLIlOmsEsNd+9NIcPEm8= go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= go.opencensus.io v0.22.4 h1:LYy1Hy3MJdrCdMwwzxA/dRok4ejH+RwNGbuoD9fCjto= go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= @@ -836,7 +812,6 @@ golang.org/x/crypto v0.0.0-20190923035154-9ee001bba392/go.mod h1:/lpIB1dKB+9EgE3 golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20191202143827-86a70503ff7e/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0 h1:hb9wdF1z5waM+dSIICn1l0DkLVDT3hqhhQsDNUmHPRE= golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= @@ -873,7 +848,6 @@ golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKG golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY= golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= golang.org/x/mod v0.1.1-0.20191107180719-034126e5016b/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= -golang.org/x/mod v0.2.0 h1:KU7oHjnv3XNWfa5COkzUifxZmxp1TyI7ImMXqFxLwvQ= golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4= golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= @@ -915,7 +889,6 @@ golang.org/x/net v0.0.0-20200513185701-a91f0712d120/go.mod h1:qpuaurCH72eLCgpAm/ golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= -golang.org/x/net v0.0.0-20200822124328-c89045814202 h1:VvcQYSHwXgi7W+TpUR6A9g6Up98WAHf3f/ulnJ62IyA= golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20201006153459-a7d1128ccaa0 h1:wBouT66WTYFXdxfVdz9sVWARVd/2vfGcmI45D2gj45M= golang.org/x/net v0.0.0-20201006153459-a7d1128ccaa0/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= @@ -923,7 +896,6 @@ golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAG golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20191202225959-858c2ad4c8b6/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= -golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d h1:TzXSXBo42m9gQenoE3b9BGiEpg5IG2JkU5FkPIawgtw= golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw= golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43 h1:ld7aEMNHoBnnDAX15v1T6z31v8HwR2A9FYOuAhWqkwc= golang.org/x/oauth2 v0.0.0-20200902213428-5d25da1a8d43/go.mod h1:KelEdhl1UZF7XfJ4dDtk6s++YSgaE7mD/BuKKDLBl4A= @@ -934,9 +906,7 @@ golang.org/x/sync v0.0.0-20190227155943-e225da77a7e6/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20190412183630-56d357773e84/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a h1:WXEvlFVvvGxCJLG6REjsT03iWnKLEWinaScsxF2Vm2o= golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208 h1:qwRHBd0NqMbJxfbotnDhm2ByMI1Shq4Y6oRJo21SGJA= golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20200930132711-30421366ff76 h1:JnxiSYT3Nm0BT2a8CyvYyM6cnrWpidecD1UuSYbhKm0= golang.org/x/sync v0.0.0-20200930132711-30421366ff76/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= @@ -991,7 +961,6 @@ golang.org/x/sys v0.0.0-20200501052902-10377860bb8e/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200511232937-7e40ca221e25/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200515095857-1151b9dac4a9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1 h1:ogLJMz+qpzav7lGMh10LMvAkM/fAoGlaiiHYiFYdm80= golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200622214017-ed371f2e16b4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -1003,7 +972,6 @@ golang.org/x/sys v0.0.0-20201008064518-c1f3e3309c71/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= @@ -1069,18 +1037,16 @@ golang.org/x/tools v0.0.0-20200312045724-11d5b4c81c7d/go.mod h1:o4KQGtdN14AW+yjs golang.org/x/tools v0.0.0-20200331025713-a30bf2db82d4/go.mod h1:Sl4aGygMT6LrqrWclx+PTx3U+LnKx/seiNR+3G19Ar8= golang.org/x/tools v0.0.0-20200501065659-ab2804fb9c9d/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20200512131952-2bc93b1c0c88/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= -golang.org/x/tools v0.0.0-20200513201620-d5fe73897c97 h1:DAuln/hGp+aJiHpID1Y1hYzMEPP5WLwtZHPb50mN0OE= golang.org/x/tools v0.0.0-20200513201620-d5fe73897c97/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20200515010526-7d3b6ebf133d/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20200618134242-20370b0cb4b2/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE= golang.org/x/tools v0.0.0-20200825202427-b303f430e36d/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= golang.org/x/tools v0.0.0-20200828161849-5deb26317202/go.mod h1:njjCfa9FT2d7l9Bc6FUM5FLjQPp3cFF28FI3qnDFljA= golang.org/x/tools v0.0.0-20200904185747-39188db58858/go.mod h1:Cj7w3i3Rnn0Xh82ur9kSqwfTHTeVxaDqrfMjpcNT6bE= -golang.org/x/tools v0.0.0-20201008025239-9df69603baec h1:RY2OghEV/7X1MLaecgm1mwFd3sGvUddm5pGVSxQvX0c= -golang.org/x/tools v0.0.0-20201008025239-9df69603baec/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU= +golang.org/x/tools v0.0.0-20201020161133-226fd2f889ca h1:pvScuB+UnCGDas2naNKUOXruM08MjwVcEdaweeynIqQ= +golang.org/x/tools v0.0.0-20201020161133-226fd2f889ca/go.mod h1:z6u4i615ZeAfBE4XtMziQW1fSVJXACjjbWkB/mvPzlU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= @@ -1142,7 +1108,6 @@ google.golang.org/genproto v0.0.0-20200312145019-da6875a35672/go.mod h1:55QSHmfG google.golang.org/genproto v0.0.0-20200331122359-1ee6d9798940/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= google.golang.org/genproto v0.0.0-20200430143042-b979b6f78d84/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= google.golang.org/genproto v0.0.0-20200511104702-f5ebc3bea380/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= -google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884 h1:fiNLklpBwWK1mth30Hlwk+fcdBmIALlgF5iy77O37Ig= google.golang.org/genproto v0.0.0-20200513103714-09dca8ec2884/go.mod h1:55QSHmfGQM9UVYDPBsyGGes0y52j32PQ3BqQfXhyH3c= google.golang.org/genproto v0.0.0-20200515170657-fc4c6c6a6587/go.mod h1:YsZOwe1myG/8QRHRsmBRE1LrgQY60beZKjly0O1fX9U= google.golang.org/genproto v0.0.0-20200526211855-cb27e3aa2013/go.mod h1:NbSheEEYHJ7i3ixzK3sjbqSGDJWnxyFXZblF3eUsNvo= @@ -1165,7 +1130,6 @@ google.golang.org/grpc v1.26.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8 google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk= google.golang.org/grpc v1.28.0/go.mod h1:rpkK4SK4GF4Ach/+MFLZUBavHOvF2JJB5uozKKal+60= -google.golang.org/grpc v1.29.1 h1:EC2SB8S04d2r73uptxphDSUG+kTKVgjRPF+N3xpxRB4= google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3IjizoKk= google.golang.org/grpc v1.31.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= google.golang.org/grpc v1.31.1/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak= @@ -1177,7 +1141,6 @@ google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQ google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE= google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo= google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= -google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM= google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU= google.golang.org/protobuf v1.24.0 h1:UhZDfRO8JRQru4/+LlLE0BRKGF8L+PICnvYZmx/fEGA= @@ -1210,6 +1173,7 @@ gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 h1:tQIYjPdBoyREyB9XMu+nnTclpTYkz2zFM+lzLJFO4gQ= gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo= diff --git a/notifier/notifier.go b/notifier/notifier.go index 29545bbee0..5856613503 100644 --- a/notifier/notifier.go +++ b/notifier/notifier.go @@ -32,13 +32,13 @@ import ( "github.com/go-kit/kit/log/level" "github.com/go-openapi/strfmt" "github.com/pkg/errors" - "go.uber.org/atomic" - "github.com/prometheus/alertmanager/api/v2/models" "github.com/prometheus/client_golang/prometheus" config_util "github.com/prometheus/common/config" "github.com/prometheus/common/model" "github.com/prometheus/common/version" + "go.uber.org/atomic" + "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/discovery/targetgroup" "github.com/prometheus/prometheus/pkg/labels" diff --git a/notifier/notifier_test.go b/notifier/notifier_test.go index 46c44f381d..f0e0b0ff81 100644 --- a/notifier/notifier_test.go +++ b/notifier/notifier_test.go @@ -29,6 +29,7 @@ import ( "github.com/prometheus/alertmanager/api/v2/models" config_util "github.com/prometheus/common/config" "github.com/prometheus/common/model" + "github.com/stretchr/testify/assert" "go.uber.org/atomic" yaml "gopkg.in/yaml.v2" @@ -36,7 +37,6 @@ import ( "github.com/prometheus/prometheus/discovery/targetgroup" "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/relabel" - "github.com/prometheus/prometheus/util/testutil" ) func TestPostPath(t *testing.T) { @@ -65,7 +65,7 @@ func TestPostPath(t *testing.T) { }, } for _, c := range cases { - testutil.Equals(t, c.out, postPath(c.in, config.AlertmanagerAPIVersionV1)) + assert.Equal(t, c.out, postPath(c.in, config.AlertmanagerAPIVersionV1)) } } @@ -80,10 +80,10 @@ func TestHandlerNextBatch(t *testing.T) { expected := append([]*Alert{}, h.queue...) - testutil.Ok(t, alertsEqual(expected[0:maxBatchSize], h.nextBatch())) - testutil.Ok(t, alertsEqual(expected[maxBatchSize:2*maxBatchSize], h.nextBatch())) - testutil.Ok(t, alertsEqual(expected[2*maxBatchSize:], h.nextBatch())) - testutil.Assert(t, len(h.queue) == 0, "Expected queue to be empty but got %d alerts", len(h.queue)) + assert.NoError(t, alertsEqual(expected[0:maxBatchSize], h.nextBatch())) + assert.NoError(t, alertsEqual(expected[maxBatchSize:2*maxBatchSize], h.nextBatch())) + assert.NoError(t, alertsEqual(expected[2*maxBatchSize:], h.nextBatch())) + assert.True(t, len(h.queue) == 0, "Expected queue to be empty but got %d alerts", len(h.queue)) } func alertsEqual(a, b []*Alert) error { @@ -188,20 +188,20 @@ func TestHandlerSendAll(t *testing.T) { t.Helper() select { case err := <-errc: - testutil.Ok(t, err) + assert.NoError(t, err) default: } } - testutil.Assert(t, h.sendAll(h.queue...), "all sends failed unexpectedly") + assert.True(t, h.sendAll(h.queue...), "all sends failed unexpectedly") checkNoErr() status1.Store(int32(http.StatusNotFound)) - testutil.Assert(t, h.sendAll(h.queue...), "all sends failed unexpectedly") + assert.True(t, h.sendAll(h.queue...), "all sends failed unexpectedly") checkNoErr() status2.Store(int32(http.StatusInternalServerError)) - testutil.Assert(t, !h.sendAll(h.queue...), "all sends succeeded unexpectedly") + assert.True(t, !h.sendAll(h.queue...), "all sends succeeded unexpectedly") checkNoErr() } @@ -215,11 +215,11 @@ func TestCustomDo(t *testing.T) { received = true body, err := ioutil.ReadAll(req.Body) - testutil.Ok(t, err) + assert.NoError(t, err) - testutil.Equals(t, testBody, string(body)) + assert.Equal(t, testBody, string(body)) - testutil.Equals(t, testURL, req.URL.String()) + assert.Equal(t, testURL, req.URL.String()) return &http.Response{ Body: ioutil.NopCloser(bytes.NewBuffer(nil)), @@ -229,7 +229,7 @@ func TestCustomDo(t *testing.T) { h.sendOne(context.Background(), nil, testURL, []byte(testBody)) - testutil.Assert(t, received, "Expected to receive an alert, but didn't") + assert.True(t, received, "Expected to receive an alert, but didn't") } func TestExternalLabels(t *testing.T) { @@ -263,7 +263,7 @@ func TestExternalLabels(t *testing.T) { {Labels: labels.FromStrings("alertname", "externalrelabelthis", "a", "c")}, } - testutil.Ok(t, alertsEqual(expected, h.queue)) + assert.NoError(t, alertsEqual(expected, h.queue)) } func TestHandlerRelabel(t *testing.T) { @@ -299,7 +299,7 @@ func TestHandlerRelabel(t *testing.T) { {Labels: labels.FromStrings("alertname", "renamed")}, } - testutil.Ok(t, alertsEqual(expected, h.queue)) + assert.NoError(t, alertsEqual(expected, h.queue)) } func TestHandlerQueuing(t *testing.T) { @@ -375,7 +375,7 @@ func TestHandlerQueuing(t *testing.T) { case <-called: expectedc <- expected case err := <-errc: - testutil.Ok(t, err) + assert.NoError(t, err) return case <-time.After(5 * time.Second): t.Fatalf("Alerts were not pushed") @@ -408,7 +408,7 @@ func TestHandlerQueuing(t *testing.T) { expectedc <- alerts[:maxBatchSize] select { case err := <-errc: - testutil.Ok(t, err) + assert.NoError(t, err) case <-time.After(5 * time.Second): t.Fatalf("Alerts were not pushed") } @@ -435,10 +435,10 @@ func TestLabelSetNotReused(t *testing.T) { tg := makeInputTargetGroup() _, _, err := alertmanagerFromGroup(tg, &config.AlertmanagerConfig{}) - testutil.Ok(t, err) + assert.NoError(t, err) // Target modified during alertmanager extraction - testutil.Equals(t, tg, makeInputTargetGroup()) + assert.Equal(t, tg, makeInputTargetGroup()) } func TestReload(t *testing.T) { @@ -469,7 +469,7 @@ alerting: if err := yaml.UnmarshalStrict([]byte(s), cfg); err != nil { t.Fatalf("Unable to load YAML config: %s", err) } - testutil.Equals(t, 1, len(cfg.AlertingConfig.AlertmanagerConfigs)) + assert.Equal(t, 1, len(cfg.AlertingConfig.AlertmanagerConfigs)) if err := n.ApplyConfig(cfg); err != nil { t.Fatalf("Error Applying the config:%v", err) @@ -486,7 +486,7 @@ alerting: n.reload(tgs) res := n.Alertmanagers()[0].String() - testutil.Equals(t, tt.out, res) + assert.Equal(t, tt.out, res) } } @@ -523,7 +523,7 @@ alerting: if err := yaml.UnmarshalStrict([]byte(s), cfg); err != nil { t.Fatalf("Unable to load YAML config: %s", err) } - testutil.Equals(t, 1, len(cfg.AlertingConfig.AlertmanagerConfigs)) + assert.Equal(t, 1, len(cfg.AlertingConfig.AlertmanagerConfigs)) if err := n.ApplyConfig(cfg); err != nil { t.Fatalf("Error Applying the config:%v", err) @@ -541,7 +541,7 @@ alerting: n.reload(tgs) res := n.DroppedAlertmanagers()[0].String() - testutil.Equals(t, res, tt.out) + assert.Equal(t, res, tt.out) } } @@ -561,5 +561,5 @@ func makeInputTargetGroup() *targetgroup.Group { } func TestLabelsToOpenAPILabelSet(t *testing.T) { - testutil.Equals(t, models.LabelSet{"aaa": "111", "bbb": "222"}, labelsToOpenAPILabelSet(labels.Labels{{Name: "aaa", Value: "111"}, {Name: "bbb", Value: "222"}})) + assert.Equal(t, models.LabelSet{"aaa": "111", "bbb": "222"}, labelsToOpenAPILabelSet(labels.Labels{{Name: "aaa", Value: "111"}, {Name: "bbb", Value: "222"}})) } diff --git a/pkg/labels/labels_test.go b/pkg/labels/labels_test.go index 4b1c01bbdb..9dfb61cfd6 100644 --- a/pkg/labels/labels_test.go +++ b/pkg/labels/labels_test.go @@ -18,7 +18,7 @@ import ( "strings" "testing" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" ) func TestLabels_String(t *testing.T) { @@ -50,7 +50,7 @@ func TestLabels_String(t *testing.T) { } for _, c := range cases { str := c.lables.String() - testutil.Equals(t, c.expected, str) + assert.Equal(t, c.expected, str) } } @@ -181,7 +181,7 @@ func TestLabels_MatchLabels(t *testing.T) { for i, test := range tests { got := labels.MatchLabels(test.on, test.providedNames...) - testutil.Equals(t, test.expected, got, "unexpected labelset for test case %d", i) + assert.Equal(t, test.expected, got, "unexpected labelset for test case %d", i) } } @@ -206,8 +206,8 @@ func TestLabels_HasDuplicateLabelNames(t *testing.T) { for i, c := range cases { l, d := c.Input.HasDuplicateLabelNames() - testutil.Equals(t, c.Duplicate, d, "test %d: incorrect duplicate bool", i) - testutil.Equals(t, c.LabelName, l, "test %d: incorrect label name", i) + assert.Equal(t, c.Duplicate, d, "test %d: incorrect duplicate bool", i) + assert.Equal(t, c.LabelName, l, "test %d: incorrect label name", i) } } @@ -287,7 +287,7 @@ func TestLabels_WithoutEmpty(t *testing.T) { }, } { t.Run("", func(t *testing.T) { - testutil.Equals(t, test.expected, test.input.WithoutEmpty()) + assert.Equal(t, test.expected, test.input.WithoutEmpty()) }) } } @@ -368,7 +368,7 @@ func TestLabels_Equal(t *testing.T) { for i, test := range tests { got := Equal(labels, test.compared) - testutil.Equals(t, test.expected, got, "unexpected comparison result for test case %d", i) + assert.Equal(t, test.expected, got, "unexpected comparison result for test case %d", i) } } @@ -385,12 +385,12 @@ func TestLabels_FromStrings(t *testing.T) { }, } - testutil.Equals(t, expected, labels, "unexpected labelset") + assert.Equal(t, expected, labels, "unexpected labelset") defer func() { recover() }() FromStrings("aaa", "111", "bbb") - testutil.Assert(t, false, "did not panic as expected") + assert.True(t, false, "did not panic as expected") } func TestLabels_Compare(t *testing.T) { @@ -508,7 +508,7 @@ func TestLabels_Compare(t *testing.T) { for i, test := range tests { got := Compare(labels, test.compared) - testutil.Equals(t, test.expected, got, "unexpected comparison result for test case %d", i) + assert.Equal(t, test.expected, got, "unexpected comparison result for test case %d", i) } } @@ -540,34 +540,34 @@ func TestLabels_Has(t *testing.T) { for i, test := range tests { got := labelsSet.Has(test.input) - testutil.Equals(t, test.expected, got, "unexpected comparison result for test case %d", i) + assert.Equal(t, test.expected, got, "unexpected comparison result for test case %d", i) } } func TestLabels_Get(t *testing.T) { - testutil.Equals(t, "", Labels{{"aaa", "111"}, {"bbb", "222"}}.Get("foo")) - testutil.Equals(t, "111", Labels{{"aaa", "111"}, {"bbb", "222"}}.Get("aaa")) + assert.Equal(t, "", Labels{{"aaa", "111"}, {"bbb", "222"}}.Get("foo")) + assert.Equal(t, "111", Labels{{"aaa", "111"}, {"bbb", "222"}}.Get("aaa")) } func TestLabels_Copy(t *testing.T) { - testutil.Equals(t, Labels{{"aaa", "111"}, {"bbb", "222"}}, Labels{{"aaa", "111"}, {"bbb", "222"}}.Copy()) + assert.Equal(t, Labels{{"aaa", "111"}, {"bbb", "222"}}, Labels{{"aaa", "111"}, {"bbb", "222"}}.Copy()) } func TestLabels_Map(t *testing.T) { - testutil.Equals(t, map[string]string{"aaa": "111", "bbb": "222"}, Labels{{"aaa", "111"}, {"bbb", "222"}}.Map()) + assert.Equal(t, map[string]string{"aaa": "111", "bbb": "222"}, Labels{{"aaa", "111"}, {"bbb", "222"}}.Map()) } func TestLabels_WithLabels(t *testing.T) { - testutil.Equals(t, Labels{{"aaa", "111"}, {"bbb", "222"}}, Labels{{"aaa", "111"}, {"bbb", "222"}, {"ccc", "333"}}.WithLabels("aaa", "bbb")) + assert.Equal(t, Labels{{"aaa", "111"}, {"bbb", "222"}}, Labels{{"aaa", "111"}, {"bbb", "222"}, {"ccc", "333"}}.WithLabels("aaa", "bbb")) } func TestLabels_WithoutLabels(t *testing.T) { - testutil.Equals(t, Labels{{"aaa", "111"}}, Labels{{"aaa", "111"}, {"bbb", "222"}, {"ccc", "333"}}.WithoutLabels("bbb", "ccc")) - testutil.Equals(t, Labels{{"aaa", "111"}}, Labels{{"aaa", "111"}, {"bbb", "222"}, {MetricName, "333"}}.WithoutLabels("bbb")) + assert.Equal(t, Labels{{"aaa", "111"}}, Labels{{"aaa", "111"}, {"bbb", "222"}, {"ccc", "333"}}.WithoutLabels("bbb", "ccc")) + assert.Equal(t, Labels{{"aaa", "111"}}, Labels{{"aaa", "111"}, {"bbb", "222"}, {MetricName, "333"}}.WithoutLabels("bbb")) } func TestBulider_NewBulider(t *testing.T) { - testutil.Equals( + assert.Equal( t, &Builder{ base: Labels{{"aaa", "111"}}, @@ -579,7 +579,7 @@ func TestBulider_NewBulider(t *testing.T) { } func TestBuilder_Del(t *testing.T) { - testutil.Equals( + assert.Equal( t, &Builder{ del: []string{"bbb"}, @@ -593,7 +593,7 @@ func TestBuilder_Del(t *testing.T) { } func TestBuilder_Set(t *testing.T) { - testutil.Equals( + assert.Equal( t, &Builder{ base: Labels{{"aaa", "111"}}, @@ -607,7 +607,7 @@ func TestBuilder_Set(t *testing.T) { }).Set("bbb", "222"), ) - testutil.Equals( + assert.Equal( t, &Builder{ base: Labels{{"aaa", "111"}}, @@ -623,7 +623,7 @@ func TestBuilder_Set(t *testing.T) { } func TestBuilder_Labels(t *testing.T) { - testutil.Equals( + assert.Equal( t, Labels{{"aaa", "111"}, {"ccc", "333"}, {"ddd", "444"}}, (&Builder{ @@ -639,9 +639,9 @@ func TestLabels_Hash(t *testing.T) { {Name: "foo", Value: "bar"}, {Name: "baz", Value: "qux"}, } - testutil.Equals(t, lbls.Hash(), lbls.Hash()) - testutil.Assert(t, lbls.Hash() != Labels{lbls[1], lbls[0]}.Hash(), "unordered labels match.") - testutil.Assert(t, lbls.Hash() != Labels{lbls[0]}.Hash(), "different labels match.") + assert.Equal(t, lbls.Hash(), lbls.Hash()) + assert.True(t, lbls.Hash() != Labels{lbls[1], lbls[0]}.Hash(), "unordered labels match.") + assert.True(t, lbls.Hash() != Labels{lbls[0]}.Hash(), "different labels match.") } var benchmarkLabelsResult uint64 diff --git a/pkg/labels/matcher_test.go b/pkg/labels/matcher_test.go index 9479320643..fa7338a760 100644 --- a/pkg/labels/matcher_test.go +++ b/pkg/labels/matcher_test.go @@ -16,12 +16,12 @@ package labels import ( "testing" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" ) func mustNewMatcher(t *testing.T, mType MatchType, value string) *Matcher { m, err := NewMatcher(mType, "", value) - testutil.Ok(t, err) + assert.NoError(t, err) return m } @@ -84,7 +84,7 @@ func TestMatcher(t *testing.T) { } for _, test := range tests { - testutil.Equals(t, test.matcher.Matches(test.value), test.match) + assert.Equal(t, test.matcher.Matches(test.value), test.match) } } @@ -113,7 +113,7 @@ func TestInverse(t *testing.T) { for _, test := range tests { result, err := test.matcher.Inverse() - testutil.Ok(t, err) - testutil.Equals(t, test.expected.Type, result.Type) + assert.NoError(t, err) + assert.Equal(t, test.expected.Type, result.Type) } } diff --git a/pkg/labels/regexp_test.go b/pkg/labels/regexp_test.go index 71704102a0..fb57a7ef80 100644 --- a/pkg/labels/regexp_test.go +++ b/pkg/labels/regexp_test.go @@ -17,7 +17,7 @@ import ( "regexp/syntax" "testing" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" ) func TestNewFastRegexMatcher(t *testing.T) { @@ -54,8 +54,8 @@ func TestNewFastRegexMatcher(t *testing.T) { for _, c := range cases { m, err := NewFastRegexMatcher(c.regex) - testutil.Ok(t, err) - testutil.Equals(t, c.expected, m.MatchString(c.value)) + assert.NoError(t, err) + assert.Equal(t, c.expected, m.MatchString(c.value)) } } @@ -88,11 +88,11 @@ func TestOptimizeConcatRegex(t *testing.T) { for _, c := range cases { parsed, err := syntax.Parse(c.regex, syntax.Perl) - testutil.Ok(t, err) + assert.NoError(t, err) prefix, suffix, contains := optimizeConcatRegex(parsed) - testutil.Equals(t, c.prefix, prefix) - testutil.Equals(t, c.suffix, suffix) - testutil.Equals(t, c.contains, contains) + assert.Equal(t, c.prefix, prefix) + assert.Equal(t, c.suffix, suffix) + assert.Equal(t, c.contains, contains) } } diff --git a/pkg/logging/dedupe_test.go b/pkg/logging/dedupe_test.go index da05ff4233..4278218cb3 100644 --- a/pkg/logging/dedupe_test.go +++ b/pkg/logging/dedupe_test.go @@ -17,7 +17,7 @@ import ( "testing" "time" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" ) type counter int @@ -35,13 +35,13 @@ func TestDedupe(t *testing.T) { // Log 10 times quickly, ensure they are deduped. for i := 0; i < 10; i++ { err := d.Log("msg", "hello") - testutil.Ok(t, err) + assert.NoError(t, err) } - testutil.Equals(t, 1, int(c)) + assert.Equal(t, 1, int(c)) // Wait, then log again, make sure it is logged. time.Sleep(200 * time.Millisecond) err := d.Log("msg", "hello") - testutil.Ok(t, err) - testutil.Equals(t, 2, int(c)) + assert.NoError(t, err) + assert.Equal(t, 2, int(c)) } diff --git a/pkg/logging/file_test.go b/pkg/logging/file_test.go index a2eb209614..f025bf9fda 100644 --- a/pkg/logging/file_test.go +++ b/pkg/logging/file_test.go @@ -20,71 +20,71 @@ import ( "strings" "testing" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" ) func TestJSONFileLogger_basic(t *testing.T) { f, err := ioutil.TempFile("", "logging") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, f.Close()) - testutil.Ok(t, os.Remove(f.Name())) + assert.NoError(t, f.Close()) + assert.NoError(t, os.Remove(f.Name())) }() l, err := NewJSONFileLogger(f.Name()) - testutil.Ok(t, err) - testutil.Assert(t, l != nil, "logger can't be nil") + assert.NoError(t, err) + assert.NotNil(t, l, "logger can't be nil") err = l.Log("test", "yes") - testutil.Ok(t, err) + assert.NoError(t, err) r := make([]byte, 1024) _, err = f.Read(r) - testutil.Ok(t, err) + assert.NoError(t, err) result, err := regexp.Match(`^{"test":"yes","ts":"[^"]+"}\n`, r) - testutil.Ok(t, err) - testutil.Assert(t, result, "unexpected content: %s", r) + assert.NoError(t, err) + assert.True(t, result, "unexpected content: %s", r) err = l.Close() - testutil.Ok(t, err) + assert.NoError(t, err) err = l.file.Close() - testutil.NotOk(t, err) - testutil.Assert(t, strings.HasSuffix(err.Error(), os.ErrClosed.Error()), "file not closed") + assert.Error(t, err) + assert.True(t, strings.HasSuffix(err.Error(), os.ErrClosed.Error()), "file not closed") } func TestJSONFileLogger_parallel(t *testing.T) { f, err := ioutil.TempFile("", "logging") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, f.Close()) - testutil.Ok(t, os.Remove(f.Name())) + assert.NoError(t, f.Close()) + assert.NoError(t, os.Remove(f.Name())) }() l, err := NewJSONFileLogger(f.Name()) - testutil.Ok(t, err) - testutil.Assert(t, l != nil, "logger can't be nil") + assert.NoError(t, err) + assert.NotNil(t, l, "logger can't be nil") err = l.Log("test", "yes") - testutil.Ok(t, err) + assert.NoError(t, err) l2, err := NewJSONFileLogger(f.Name()) - testutil.Ok(t, err) - testutil.Assert(t, l != nil, "logger can't be nil") + assert.NoError(t, err) + assert.NotNil(t, l, "logger can't be nil") err = l2.Log("test", "yes") - testutil.Ok(t, err) + assert.NoError(t, err) err = l.Close() - testutil.Ok(t, err) + assert.NoError(t, err) err = l.file.Close() - testutil.NotOk(t, err) - testutil.Assert(t, strings.HasSuffix(err.Error(), os.ErrClosed.Error()), "file not closed") + assert.Error(t, err) + assert.True(t, strings.HasSuffix(err.Error(), os.ErrClosed.Error()), "file not closed") err = l2.Close() - testutil.Ok(t, err) + assert.NoError(t, err) err = l2.file.Close() - testutil.NotOk(t, err) - testutil.Assert(t, strings.HasSuffix(err.Error(), os.ErrClosed.Error()), "file not closed") + assert.Error(t, err) + assert.True(t, strings.HasSuffix(err.Error(), os.ErrClosed.Error()), "file not closed") } diff --git a/pkg/pool/pool_test.go b/pkg/pool/pool_test.go index caa0bdac58..d091c4c8e6 100644 --- a/pkg/pool/pool_test.go +++ b/pkg/pool/pool_test.go @@ -16,7 +16,7 @@ package pool import ( "testing" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" ) func makeFunc(size int) interface{} { @@ -44,7 +44,7 @@ func TestPool(t *testing.T) { } for _, c := range cases { ret := testPool.Get(c.size) - testutil.Equals(t, c.expectedCap, cap(ret.([]int))) + assert.Equal(t, c.expectedCap, cap(ret.([]int))) testPool.Put(ret) } } diff --git a/pkg/relabel/relabel_test.go b/pkg/relabel/relabel_test.go index 538ac6593d..f002c8c8c7 100644 --- a/pkg/relabel/relabel_test.go +++ b/pkg/relabel/relabel_test.go @@ -17,9 +17,9 @@ import ( "testing" "github.com/prometheus/common/model" + "github.com/stretchr/testify/assert" "github.com/prometheus/prometheus/pkg/labels" - "github.com/prometheus/prometheus/util/testutil" ) func TestRelabel(t *testing.T) { @@ -414,7 +414,7 @@ func TestRelabel(t *testing.T) { for _, test := range tests { res := Process(test.input, test.relabel...) - testutil.Equals(t, test.output, res) + assert.Equal(t, test.output, res) } } @@ -440,7 +440,7 @@ func TestTargetLabelValidity(t *testing.T) { {"foo${bar}foo", true}, } for _, test := range tests { - testutil.Assert(t, relabelTarget.Match([]byte(test.str)) == test.valid, + assert.True(t, relabelTarget.Match([]byte(test.str)) == test.valid, "Expected %q to be %v", test.str, test.valid) } } diff --git a/pkg/rulefmt/rulefmt_test.go b/pkg/rulefmt/rulefmt_test.go index 92967cf7d1..1c710a2449 100644 --- a/pkg/rulefmt/rulefmt_test.go +++ b/pkg/rulefmt/rulefmt_test.go @@ -18,7 +18,7 @@ import ( "strings" "testing" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" ) func TestParseFileSuccess(t *testing.T) { @@ -163,9 +163,9 @@ groups: for _, tst := range tests { rgs, errs := Parse([]byte(tst.ruleString)) - testutil.Assert(t, rgs != nil, "Rule parsing, rule=\n"+tst.ruleString) + assert.NotNil(t, rgs, "Rule parsing, rule=\n"+tst.ruleString) passed := (tst.shouldPass && len(errs) == 0) || (!tst.shouldPass && len(errs) > 0) - testutil.Assert(t, passed, "Rule validation failed, rule=\n"+tst.ruleString) + assert.True(t, passed, "Rule validation failed, rule=\n"+tst.ruleString) } } diff --git a/pkg/textparse/openmetricsparse_test.go b/pkg/textparse/openmetricsparse_test.go index de672e3c28..dbce4bae06 100644 --- a/pkg/textparse/openmetricsparse_test.go +++ b/pkg/textparse/openmetricsparse_test.go @@ -17,9 +17,10 @@ import ( "io" "testing" + "github.com/stretchr/testify/assert" + "github.com/prometheus/prometheus/pkg/exemplar" "github.com/prometheus/prometheus/pkg/labels" - "github.com/prometheus/prometheus/util/testutil" ) func TestOpenMetricsParse(t *testing.T) { @@ -220,7 +221,7 @@ foo_total 17.0 1520879607.789 # {xx="yy"} 5` if err == io.EOF { break } - testutil.Ok(t, err) + assert.NoError(t, err) switch et { case EntrySeries: @@ -230,40 +231,40 @@ foo_total 17.0 1520879607.789 # {xx="yy"} 5` p.Metric(&res) found := p.Exemplar(&e) - testutil.Equals(t, exp[i].m, string(m)) - testutil.Equals(t, exp[i].t, ts) - testutil.Equals(t, exp[i].v, v) - testutil.Equals(t, exp[i].lset, res) + assert.Equal(t, exp[i].m, string(m)) + assert.Equal(t, exp[i].t, ts) + assert.Equal(t, exp[i].v, v) + assert.Equal(t, exp[i].lset, res) if exp[i].e == nil { - testutil.Equals(t, false, found) + assert.Equal(t, false, found) } else { - testutil.Equals(t, true, found) - testutil.Equals(t, *exp[i].e, e) + assert.Equal(t, true, found) + assert.Equal(t, *exp[i].e, e) } res = res[:0] case EntryType: m, typ := p.Type() - testutil.Equals(t, exp[i].m, string(m)) - testutil.Equals(t, exp[i].typ, typ) + assert.Equal(t, exp[i].m, string(m)) + assert.Equal(t, exp[i].typ, typ) case EntryHelp: m, h := p.Help() - testutil.Equals(t, exp[i].m, string(m)) - testutil.Equals(t, exp[i].help, string(h)) + assert.Equal(t, exp[i].m, string(m)) + assert.Equal(t, exp[i].help, string(h)) case EntryUnit: m, u := p.Unit() - testutil.Equals(t, exp[i].m, string(m)) - testutil.Equals(t, exp[i].unit, string(u)) + assert.Equal(t, exp[i].m, string(m)) + assert.Equal(t, exp[i].unit, string(u)) case EntryComment: - testutil.Equals(t, exp[i].comment, string(p.Comment())) + assert.Equal(t, exp[i].comment, string(p.Comment())) } i++ } - testutil.Equals(t, len(exp), i) + assert.Equal(t, len(exp), i) } func TestOpenMetricsParseErrors(t *testing.T) { @@ -510,7 +511,7 @@ func TestOpenMetricsParseErrors(t *testing.T) { for err == nil { _, err = p.Next() } - testutil.Equals(t, c.err, err.Error(), "test %d: %s", i, c.input) + assert.Equal(t, c.err, err.Error(), "test %d: %s", i, c.input) } } @@ -577,10 +578,10 @@ func TestOMNullByteHandling(t *testing.T) { } if c.err == "" { - testutil.Equals(t, io.EOF, err, "test %d", i) + assert.Equal(t, io.EOF, err, "test %d", i) continue } - testutil.Equals(t, c.err, err.Error(), "test %d", i) + assert.Equal(t, c.err, err.Error(), "test %d", i) } } diff --git a/pkg/textparse/promparse_test.go b/pkg/textparse/promparse_test.go index 28761ed13e..e7713c1092 100644 --- a/pkg/textparse/promparse_test.go +++ b/pkg/textparse/promparse_test.go @@ -23,8 +23,9 @@ import ( "github.com/prometheus/common/expfmt" "github.com/prometheus/common/model" + "github.com/stretchr/testify/assert" + "github.com/prometheus/prometheus/pkg/labels" - "github.com/prometheus/prometheus/util/testutil" ) func TestPromParse(t *testing.T) { @@ -179,7 +180,7 @@ testmetric{label="\"bar\""} 1` if err == io.EOF { break } - testutil.Ok(t, err) + assert.NoError(t, err) switch et { case EntrySeries: @@ -187,29 +188,29 @@ testmetric{label="\"bar\""} 1` p.Metric(&res) - testutil.Equals(t, exp[i].m, string(m)) - testutil.Equals(t, exp[i].t, ts) - testutil.Equals(t, exp[i].v, v) - testutil.Equals(t, exp[i].lset, res) + assert.Equal(t, exp[i].m, string(m)) + assert.Equal(t, exp[i].t, ts) + assert.Equal(t, exp[i].v, v) + assert.Equal(t, exp[i].lset, res) res = res[:0] case EntryType: m, typ := p.Type() - testutil.Equals(t, exp[i].m, string(m)) - testutil.Equals(t, exp[i].typ, typ) + assert.Equal(t, exp[i].m, string(m)) + assert.Equal(t, exp[i].typ, typ) case EntryHelp: m, h := p.Help() - testutil.Equals(t, exp[i].m, string(m)) - testutil.Equals(t, exp[i].help, string(h)) + assert.Equal(t, exp[i].m, string(m)) + assert.Equal(t, exp[i].help, string(h)) case EntryComment: - testutil.Equals(t, exp[i].comment, string(p.Comment())) + assert.Equal(t, exp[i].comment, string(p.Comment())) } i++ } - testutil.Equals(t, len(exp), i) + assert.Equal(t, len(exp), i) } func TestPromParseErrors(t *testing.T) { @@ -277,8 +278,8 @@ func TestPromParseErrors(t *testing.T) { for err == nil { _, err = p.Next() } - testutil.NotOk(t, err) - testutil.Equals(t, c.err, err.Error(), "test %d", i) + assert.Error(t, err) + assert.Equal(t, c.err, err.Error(), "test %d", i) } } @@ -329,12 +330,12 @@ func TestPromNullByteHandling(t *testing.T) { } if c.err == "" { - testutil.Equals(t, io.EOF, err, "test %d", i) + assert.Equal(t, io.EOF, err, "test %d", i) continue } - testutil.NotOk(t, err) - testutil.Equals(t, c.err, err.Error(), "test %d", i) + assert.Error(t, err) + assert.Equal(t, c.err, err.Error(), "test %d", i) } } @@ -349,11 +350,11 @@ func BenchmarkParse(b *testing.B) { } { for _, fn := range []string{"promtestdata.txt", "promtestdata.nometa.txt"} { f, err := os.Open(fn) - testutil.Ok(b, err) + assert.NoError(b, err) defer f.Close() buf, err := ioutil.ReadAll(f) - testutil.Ok(b, err) + assert.NoError(b, err) b.Run(parserName+"/no-decode-metric/"+fn, func(b *testing.B) { total := 0 @@ -483,18 +484,18 @@ func BenchmarkGzip(b *testing.B) { for _, fn := range []string{"promtestdata.txt", "promtestdata.nometa.txt"} { b.Run(fn, func(b *testing.B) { f, err := os.Open(fn) - testutil.Ok(b, err) + assert.NoError(b, err) defer f.Close() var buf bytes.Buffer gw := gzip.NewWriter(&buf) n, err := io.Copy(gw, f) - testutil.Ok(b, err) - testutil.Ok(b, gw.Close()) + assert.NoError(b, err) + assert.NoError(b, gw.Close()) gbuf, err := ioutil.ReadAll(&buf) - testutil.Ok(b, err) + assert.NoError(b, err) k := b.N / promtestdataSampleCount @@ -506,11 +507,11 @@ func BenchmarkGzip(b *testing.B) { for i := 0; i < k; i++ { gr, err := gzip.NewReader(bytes.NewReader(gbuf)) - testutil.Ok(b, err) + assert.NoError(b, err) d, err := ioutil.ReadAll(gr) - testutil.Ok(b, err) - testutil.Ok(b, gr.Close()) + assert.NoError(b, err) + assert.NoError(b, gr.Close()) total += len(d) } diff --git a/promql/engine_test.go b/promql/engine_test.go index 0157f15184..ccd6b61acd 100644 --- a/promql/engine_test.go +++ b/promql/engine_test.go @@ -23,12 +23,13 @@ import ( "time" "github.com/go-kit/kit/log" + "github.com/stretchr/testify/assert" + "go.uber.org/goleak" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/timestamp" "github.com/prometheus/prometheus/promql/parser" "github.com/prometheus/prometheus/storage" - "github.com/prometheus/prometheus/util/testutil" - "go.uber.org/goleak" ) func TestMain(m *testing.M) { @@ -39,7 +40,7 @@ func TestQueryConcurrency(t *testing.T) { maxConcurrency := 10 dir, err := ioutil.TempDir("", "test_concurrency") - testutil.Ok(t, err) + assert.NoError(t, err) defer os.RemoveAll(dir) queryTracker := NewActiveQueryTracker(dir, maxConcurrency, nil) @@ -118,10 +119,10 @@ func TestQueryTimeout(t *testing.T) { }) res := query.Exec(ctx) - testutil.NotOk(t, res.Err, "expected timeout error but got none") + assert.Error(t, res.Err, "expected timeout error but got none") var e ErrQueryTimeout - testutil.Assert(t, errors.As(res.Err, &e), "expected timeout error but got: %s", res.Err) + assert.True(t, errors.As(res.Err, &e), "expected timeout error but got: %s", res.Err) } const errQueryCanceled = ErrQueryCanceled("test statement execution") @@ -159,8 +160,8 @@ func TestQueryCancel(t *testing.T) { block <- struct{}{} <-processing - testutil.NotOk(t, res.Err, "expected cancellation error for query1 but got none") - testutil.Equals(t, errQueryCanceled, res.Err) + assert.Error(t, res.Err, "expected cancellation error for query1 but got none") + assert.Equal(t, errQueryCanceled, res.Err) // Canceling a query before starting it must have no effect. query2 := engine.newTestQuery(func(ctx context.Context) error { @@ -169,7 +170,7 @@ func TestQueryCancel(t *testing.T) { query2.Cancel() res = query2.Exec(ctx) - testutil.Ok(t, res.Err) + assert.NoError(t, res.Err) } // errQuerier implements storage.Querier which always returns error. @@ -210,18 +211,18 @@ func TestQueryError(t *testing.T) { defer cancelCtx() vectorQuery, err := engine.NewInstantQuery(queryable, "foo", time.Unix(1, 0)) - testutil.Ok(t, err) + assert.NoError(t, err) res := vectorQuery.Exec(ctx) - testutil.NotOk(t, res.Err, "expected error on failed select but got none") - testutil.Assert(t, errors.Is(res.Err, errStorage), "expected error doesn't match") + assert.Error(t, res.Err, "expected error on failed select but got none") + assert.True(t, errors.Is(res.Err, errStorage), "expected error doesn't match") matrixQuery, err := engine.NewInstantQuery(queryable, "foo[1m]", time.Unix(1, 0)) - testutil.Ok(t, err) + assert.NoError(t, err) res = matrixQuery.Exec(ctx) - testutil.NotOk(t, res.Err, "expected error on failed select but got none") - testutil.Assert(t, errors.Is(res.Err, errStorage), "expected error doesn't match") + assert.Error(t, res.Err, "expected error on failed select but got none") + assert.True(t, errors.Is(res.Err, errStorage), "expected error doesn't match") } type noopHintRecordingQueryable struct { @@ -377,12 +378,12 @@ func TestSelectHintsSetCorrectly(t *testing.T) { } else { query, err = engine.NewRangeQuery(hintsRecorder, tc.query, timestamp.Time(tc.start), timestamp.Time(tc.end), time.Second) } - testutil.Ok(t, err) + assert.NoError(t, err) res := query.Exec(context.Background()) - testutil.Ok(t, res.Err) + assert.NoError(t, res.Err) - testutil.Equals(t, tc.expected, hintsRecorder.hints) + assert.Equal(t, tc.expected, hintsRecorder.hints) }) } @@ -425,8 +426,8 @@ func TestEngineShutdown(t *testing.T) { block <- struct{}{} <-processing - testutil.NotOk(t, res.Err, "expected error on shutdown during query but got none") - testutil.Equals(t, errQueryCanceled, res.Err) + assert.Error(t, res.Err, "expected error on shutdown during query but got none") + assert.Equal(t, errQueryCanceled, res.Err) query2 := engine.newTestQuery(func(context.Context) error { t.Fatalf("reached query execution unexpectedly") @@ -436,10 +437,10 @@ func TestEngineShutdown(t *testing.T) { // The second query is started after the engine shut down. It must // be canceled immediately. res2 := query2.Exec(ctx) - testutil.NotOk(t, res2.Err, "expected error on querying with canceled context but got none") + assert.Error(t, res2.Err, "expected error on querying with canceled context but got none") var e ErrQueryCanceled - testutil.Assert(t, errors.As(res2.Err, &e), "expected cancellation error but got: %s", res2.Err) + assert.True(t, errors.As(res2.Err, &e), "expected cancellation error but got: %s", res2.Err) } func TestEngineEvalStmtTimestamps(t *testing.T) { @@ -447,11 +448,11 @@ func TestEngineEvalStmtTimestamps(t *testing.T) { load 10s metric 1 2 `) - testutil.Ok(t, err) + assert.NoError(t, err) defer test.Close() err = test.Run() - testutil.Ok(t, err) + assert.NoError(t, err) cases := []struct { Query string @@ -528,16 +529,16 @@ load 10s } else { qry, err = test.QueryEngine().NewRangeQuery(test.Queryable(), c.Query, c.Start, c.End, c.Interval) } - testutil.Ok(t, err) + assert.NoError(t, err) res := qry.Exec(test.Context()) if c.ShouldError { - testutil.NotOk(t, res.Err, "expected error for the query %q", c.Query) + assert.Error(t, res.Err, "expected error for the query %q", c.Query) continue } - testutil.Ok(t, res.Err) - testutil.Equals(t, c.Result, res.Value, "query %q failed", c.Query) + assert.NoError(t, res.Err) + assert.Equal(t, c.Result, res.Value, "query %q failed", c.Query) } } @@ -548,11 +549,11 @@ load 10s bigmetric{a="1"} 1 2 bigmetric{a="2"} 1 2 `) - testutil.Ok(t, err) + assert.NoError(t, err) defer test.Close() err = test.Run() - testutil.Ok(t, err) + assert.NoError(t, err) cases := []struct { Query string @@ -764,11 +765,11 @@ load 10s } else { qry, err = engine.NewRangeQuery(test.Queryable(), c.Query, c.Start, c.End, c.Interval) } - testutil.Ok(t, err) + assert.NoError(t, err) res := qry.Exec(test.Context()) - testutil.Equals(t, c.Result.Err, res.Err) - testutil.Equals(t, c.Result.Value, res.Value, "query %q failed", c.Query) + assert.Equal(t, c.Result.Err, res.Err) + assert.Equal(t, c.Result.Value, res.Value, "query %q failed", c.Query) } } @@ -1051,21 +1052,21 @@ func TestSubquerySelector(t *testing.T) { } { t.Run("", func(t *testing.T) { test, err := NewTest(t, tst.loadString) - testutil.Ok(t, err) + assert.NoError(t, err) defer test.Close() - testutil.Ok(t, test.Run()) + assert.NoError(t, test.Run()) engine := test.QueryEngine() for _, c := range tst.cases { t.Run(c.Query, func(t *testing.T) { qry, err := engine.NewInstantQuery(test.Queryable(), c.Query, c.Start) - testutil.Ok(t, err) + assert.NoError(t, err) res := qry.Exec(test.Context()) - testutil.Equals(t, c.Result.Err, res.Err) + assert.Equal(t, c.Result.Err, res.Err) mat := res.Value.(Matrix) sort.Sort(mat) - testutil.Equals(t, c.Result.Value, mat) + assert.Equal(t, c.Result.Value, mat) }) } }) @@ -1110,7 +1111,7 @@ func TestQueryLogger_basic(t *testing.T) { return contextDone(ctx, "test statement execution") }) res := query.Exec(ctx) - testutil.Ok(t, res.Err) + assert.NoError(t, res.Err) } // Query works without query log initialized. @@ -1120,28 +1121,28 @@ func TestQueryLogger_basic(t *testing.T) { engine.SetQueryLogger(f1) queryExec() for i, field := range []interface{}{"params", map[string]interface{}{"query": "test statement"}} { - testutil.Equals(t, field, f1.logs[i]) + assert.Equal(t, field, f1.logs[i]) } l := len(f1.logs) queryExec() - testutil.Equals(t, 2*l, len(f1.logs)) + assert.Equal(t, 2*l, len(f1.logs)) // Test that we close the query logger when unsetting it. - testutil.Assert(t, !f1.closed, "expected f1 to be open, got closed") + assert.True(t, !f1.closed, "expected f1 to be open, got closed") engine.SetQueryLogger(nil) - testutil.Assert(t, f1.closed, "expected f1 to be closed, got open") + assert.True(t, f1.closed, "expected f1 to be closed, got open") queryExec() // Test that we close the query logger when swapping. f2 := NewFakeQueryLogger() f3 := NewFakeQueryLogger() engine.SetQueryLogger(f2) - testutil.Assert(t, !f2.closed, "expected f2 to be open, got closed") + assert.True(t, !f2.closed, "expected f2 to be open, got closed") queryExec() engine.SetQueryLogger(f3) - testutil.Assert(t, f2.closed, "expected f2 to be closed, got open") - testutil.Assert(t, !f3.closed, "expected f3 to be open, got closed") + assert.True(t, f2.closed, "expected f2 to be closed, got open") + assert.True(t, !f3.closed, "expected f3 to be open, got closed") queryExec() } @@ -1165,12 +1166,12 @@ func TestQueryLogger_fields(t *testing.T) { }) res := query.Exec(ctx) - testutil.Ok(t, res.Err) + assert.NoError(t, res.Err) expected := []string{"foo", "bar"} for i, field := range expected { v := f1.logs[len(f1.logs)-len(expected)+i].(string) - testutil.Equals(t, field, v) + assert.Equal(t, field, v) } } @@ -1195,9 +1196,9 @@ func TestQueryLogger_error(t *testing.T) { }) res := query.Exec(ctx) - testutil.NotOk(t, res.Err, "query should have failed") + assert.Error(t, res.Err, "query should have failed") for i, field := range []interface{}{"params", map[string]interface{}{"query": "test statement"}, "error", testErr} { - testutil.Equals(t, f1.logs[i], field) + assert.Equal(t, f1.logs[i], field) } } diff --git a/promql/functions_test.go b/promql/functions_test.go index 00e028b987..99816ee54e 100644 --- a/promql/functions_test.go +++ b/promql/functions_test.go @@ -19,11 +19,12 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/timestamp" "github.com/prometheus/prometheus/promql/parser" "github.com/prometheus/prometheus/util/teststorage" - "github.com/prometheus/prometheus/util/testutil" ) func TestDeriv(t *testing.T) { @@ -46,28 +47,28 @@ func TestDeriv(t *testing.T) { a.Add(metric, 1493712816939, 1.0) a.Add(metric, 1493712846939, 1.0) - testutil.Ok(t, a.Commit()) + assert.NoError(t, a.Commit()) query, err := engine.NewInstantQuery(storage, "deriv(foo[30m])", timestamp.Time(1493712846939)) - testutil.Ok(t, err) + assert.NoError(t, err) result := query.Exec(context.Background()) - testutil.Ok(t, result.Err) + assert.NoError(t, result.Err) vec, _ := result.Vector() - testutil.Assert(t, len(vec) == 1, "Expected 1 result, got %d", len(vec)) - testutil.Assert(t, vec[0].V == 0.0, "Expected 0.0 as value, got %f", vec[0].V) + assert.True(t, len(vec) == 1, "Expected 1 result, got %d", len(vec)) + assert.True(t, vec[0].V == 0.0, "Expected 0.0 as value, got %f", vec[0].V) } func TestFunctionList(t *testing.T) { // Test that Functions and parser.Functions list the same functions. for i := range FunctionCalls { _, ok := parser.Functions[i] - testutil.Assert(t, ok, fmt.Sprintf("function %s exists in promql package, but not in parser package", i)) + assert.True(t, ok, fmt.Sprintf("function %s exists in promql package, but not in parser package", i)) } for i := range parser.Functions { _, ok := FunctionCalls[i] - testutil.Assert(t, ok, (fmt.Sprintf("function %s exists in parser package, but not in promql package", i))) + assert.True(t, ok, (fmt.Sprintf("function %s exists in parser package, but not in promql package", i))) } } diff --git a/promql/parser/generated_parser.y.go b/promql/parser/generated_parser.y.go index cf26fcc472..4117f0e715 100644 --- a/promql/parser/generated_parser.y.go +++ b/promql/parser/generated_parser.y.go @@ -3,11 +3,8 @@ //line generated_parser.y:15 package parser -import __yyfmt__ "fmt" - -//line generated_parser.y:15 - import ( + __yyfmt__ "fmt" "math" "sort" "strconv" @@ -15,7 +12,7 @@ import ( "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/value" -) +) //line generated_parser.y:15 //line generated_parser.y:28 type yySymType struct { diff --git a/promql/parser/lex_test.go b/promql/parser/lex_test.go index 9e95f7ae63..e499391ec0 100644 --- a/promql/parser/lex_test.go +++ b/promql/parser/lex_test.go @@ -16,7 +16,7 @@ package parser import ( "testing" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" ) type testCase struct { @@ -731,10 +731,10 @@ func TestLexer(t *testing.T) { } eofItem := Item{EOF, Pos(len(test.input)), ""} - testutil.Equals(t, lastItem, eofItem, "%d: input %q", i, test.input) + assert.Equal(t, lastItem, eofItem, "%d: input %q", i, test.input) out = out[:len(out)-1] - testutil.Equals(t, test.expected, out, "%d: input %q", i, test.input) + assert.Equal(t, test.expected, out, "%d: input %q", i, test.input) } }) } diff --git a/promql/parser/parse_test.go b/promql/parser/parse_test.go index 717ecdc003..f9340e375d 100644 --- a/promql/parser/parse_test.go +++ b/promql/parser/parse_test.go @@ -21,9 +21,9 @@ import ( "github.com/pkg/errors" "github.com/prometheus/common/model" + "github.com/stretchr/testify/assert" "github.com/prometheus/prometheus/pkg/labels" - "github.com/prometheus/prometheus/util/testutil" ) var testExpr = []struct { @@ -2659,23 +2659,23 @@ func TestParseExpressions(t *testing.T) { expr, err := ParseExpr(test.input) // Unexpected errors are always caused by a bug. - testutil.Assert(t, err != errUnexpected, "unexpected error occurred") + assert.True(t, err != errUnexpected, "unexpected error occurred") if !test.fail { - testutil.Ok(t, err) - testutil.Equals(t, test.expected, expr, "error on input '%s'", test.input) + assert.NoError(t, err) + assert.Equal(t, test.expected, expr, "error on input '%s'", test.input) } else { - testutil.NotOk(t, err) - testutil.Assert(t, strings.Contains(err.Error(), test.errMsg), "unexpected error on input '%s', expected '%s', got '%s'", test.input, test.errMsg, err.Error()) + assert.Error(t, err) + assert.True(t, strings.Contains(err.Error(), test.errMsg), "unexpected error on input '%s', expected '%s', got '%s'", test.input, test.errMsg, err.Error()) errorList, ok := err.(ParseErrors) - testutil.Assert(t, ok, "unexpected error type") + assert.True(t, ok, "unexpected error type") for _, e := range errorList { - testutil.Assert(t, 0 <= e.PositionRange.Start, "parse error has negative position\nExpression '%s'\nError: %v", test.input, e) - testutil.Assert(t, e.PositionRange.Start <= e.PositionRange.End, "parse error has negative length\nExpression '%s'\nError: %v", test.input, e) - testutil.Assert(t, e.PositionRange.End <= Pos(len(test.input)), "parse error is not contained in input\nExpression '%s'\nError: %v", test.input, e) + assert.True(t, 0 <= e.PositionRange.Start, "parse error has negative position\nExpression '%s'\nError: %v", test.input, e) + assert.True(t, e.PositionRange.Start <= e.PositionRange.End, "parse error has negative length\nExpression '%s'\nError: %v", test.input, e) + assert.True(t, e.PositionRange.End <= Pos(len(test.input)), "parse error is not contained in input\nExpression '%s'\nError: %v", test.input, e) } } }) @@ -2685,11 +2685,11 @@ func TestParseExpressions(t *testing.T) { // NaN has no equality. Thus, we need a separate test for it. func TestNaNExpression(t *testing.T) { expr, err := ParseExpr("NaN") - testutil.Ok(t, err) + assert.NoError(t, err) nl, ok := expr.(*NumberLiteral) - testutil.Assert(t, ok, "expected number literal but got %T", expr) - testutil.Assert(t, math.IsNaN(float64(nl.Val)), "expected 'NaN' in number literal but got %v", nl.Val) + assert.True(t, ok, "expected number literal but got %T", expr) + assert.True(t, math.IsNaN(float64(nl.Val)), "expected 'NaN' in number literal but got %v", nl.Val) } func mustLabelMatcher(mt labels.MatchType, name, val string) *labels.Matcher { @@ -2804,14 +2804,14 @@ func TestParseSeries(t *testing.T) { metric, vals, err := ParseSeriesDesc(test.input) // Unexpected errors are always caused by a bug. - testutil.Assert(t, err != errUnexpected, "unexpected error occurred") + assert.True(t, err != errUnexpected, "unexpected error occurred") if !test.fail { - testutil.Ok(t, err) - testutil.Equals(t, test.expectedMetric, metric, "error on input '%s'", test.input) - testutil.Equals(t, test.expectedValues, vals, "error in input '%s'", test.input) + assert.NoError(t, err) + assert.Equal(t, test.expectedMetric, metric, "error on input '%s'", test.input) + assert.Equal(t, test.expectedValues, vals, "error in input '%s'", test.input) } else { - testutil.NotOk(t, err) + assert.Error(t, err) } } } @@ -2821,7 +2821,7 @@ func TestRecoverParserRuntime(t *testing.T) { var err error defer func() { - testutil.Equals(t, errUnexpected, err) + assert.Equal(t, errUnexpected, err) }() defer p.recover(&err) // Cause a runtime panic. @@ -2837,7 +2837,7 @@ func TestRecoverParserError(t *testing.T) { e := errors.New("custom error") defer func() { - testutil.Equals(t, e.Error(), err.Error()) + assert.Equal(t, e.Error(), err.Error()) }() defer p.recover(&err) diff --git a/promql/parser/printer.go b/promql/parser/printer.go index e187000af7..eef4aa8e41 100644 --- a/promql/parser/printer.go +++ b/promql/parser/printer.go @@ -20,6 +20,7 @@ import ( "time" "github.com/prometheus/common/model" + "github.com/prometheus/prometheus/pkg/labels" ) diff --git a/promql/parser/printer_test.go b/promql/parser/printer_test.go index 4765704665..ce446e609c 100644 --- a/promql/parser/printer_test.go +++ b/promql/parser/printer_test.go @@ -16,7 +16,7 @@ package parser import ( "testing" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" ) func TestExprString(t *testing.T) { @@ -102,13 +102,13 @@ func TestExprString(t *testing.T) { for _, test := range inputs { expr, err := ParseExpr(test.in) - testutil.Ok(t, err) + assert.NoError(t, err) exp := test.in if test.out != "" { exp = test.out } - testutil.Equals(t, exp, expr.String()) + assert.Equal(t, exp, expr.String()) } } diff --git a/promql/promql_test.go b/promql/promql_test.go index a194742802..c5dbb5c9d4 100644 --- a/promql/promql_test.go +++ b/promql/promql_test.go @@ -17,18 +17,18 @@ import ( "path/filepath" "testing" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" ) func TestEvaluations(t *testing.T) { files, err := filepath.Glob("testdata/*.test") - testutil.Ok(t, err) + assert.NoError(t, err) for _, fn := range files { t.Run(fn, func(t *testing.T) { test, err := newTestFromFile(t, fn) - testutil.Ok(t, err) - testutil.Ok(t, test.Run()) + assert.NoError(t, err) + assert.NoError(t, test.Run()) test.Close() }) diff --git a/promql/query_logger_test.go b/promql/query_logger_test.go index 57d9b00218..5e10588836 100644 --- a/promql/query_logger_test.go +++ b/promql/query_logger_test.go @@ -20,7 +20,7 @@ import ( "regexp" "testing" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" ) func TestQueryLogging(t *testing.T) { @@ -109,18 +109,18 @@ func TestIndexReuse(t *testing.T) { func TestMMapFile(t *testing.T) { file, err := ioutil.TempFile("", "mmapedFile") - testutil.Ok(t, err) + assert.NoError(t, err) filename := file.Name() defer os.Remove(filename) fileAsBytes, err := getMMapedFile(filename, 2, nil) - testutil.Ok(t, err) + assert.NoError(t, err) copy(fileAsBytes, "ab") f, err := os.Open(filename) - testutil.Ok(t, err) + assert.NoError(t, err) bytes := make([]byte, 4) n, err := f.Read(bytes) diff --git a/promql/test.go b/promql/test.go index bcd90200f8..7992c5b23a 100644 --- a/promql/test.go +++ b/promql/test.go @@ -25,6 +25,7 @@ import ( "github.com/pkg/errors" "github.com/prometheus/common/model" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/promql/parser" "github.com/prometheus/prometheus/storage" diff --git a/promql/test_test.go b/promql/test_test.go index f18271ad14..158f161d08 100644 --- a/promql/test_test.go +++ b/promql/test_test.go @@ -18,9 +18,9 @@ import ( "testing" "time" - "github.com/prometheus/prometheus/pkg/labels" + "github.com/stretchr/testify/assert" - "github.com/prometheus/prometheus/util/testutil" + "github.com/prometheus/prometheus/pkg/labels" ) func TestLazyLoader_WithSamplesTill(t *testing.T) { @@ -110,12 +110,12 @@ func TestLazyLoader_WithSamplesTill(t *testing.T) { for _, c := range cases { suite, err := NewLazyLoader(t, c.loadString) - testutil.Ok(t, err) + assert.NoError(t, err) defer suite.Close() for _, tc := range c.testCases { suite.WithSamplesTill(tc.ts, func(err error) { - testutil.Ok(t, err) + assert.NoError(t, err) if tc.checkOnlyError { return } @@ -123,20 +123,20 @@ func TestLazyLoader_WithSamplesTill(t *testing.T) { // Check the series. queryable := suite.Queryable() querier, err := queryable.Querier(suite.Context(), math.MinInt64, math.MaxInt64) - testutil.Ok(t, err) + assert.NoError(t, err) for _, s := range tc.series { var matchers []*labels.Matcher for _, label := range s.Metric { m, err := labels.NewMatcher(labels.MatchEqual, label.Name, label.Value) - testutil.Ok(t, err) + assert.NoError(t, err) matchers = append(matchers, m) } // Get the series for the matcher. ss := querier.Select(false, nil, matchers...) - testutil.Assert(t, ss.Next(), "") + assert.True(t, ss.Next(), "") storageSeries := ss.At() - testutil.Assert(t, !ss.Next(), "Expecting only 1 series") + assert.True(t, !ss.Next(), "Expecting only 1 series") // Convert `storage.Series` to `promql.Series`. got := Series{ @@ -147,9 +147,9 @@ func TestLazyLoader_WithSamplesTill(t *testing.T) { t, v := it.At() got.Points = append(got.Points, Point{T: t, V: v}) } - testutil.Ok(t, it.Err()) + assert.NoError(t, it.Err()) - testutil.Equals(t, s, got) + assert.Equal(t, s, got) } }) } diff --git a/promql/value.go b/promql/value.go index a28f06bbae..fa3a71d352 100644 --- a/promql/value.go +++ b/promql/value.go @@ -21,11 +21,10 @@ import ( "github.com/pkg/errors" - "github.com/prometheus/prometheus/tsdb/chunkenc" - "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/promql/parser" "github.com/prometheus/prometheus/storage" + "github.com/prometheus/prometheus/tsdb/chunkenc" ) func (Matrix) Type() parser.ValueType { return parser.ValueTypeMatrix } diff --git a/rules/alerting.go b/rules/alerting.go index c82bc9640a..a4074221f9 100644 --- a/rules/alerting.go +++ b/rules/alerting.go @@ -16,19 +16,17 @@ package rules import ( "context" "fmt" + html_template "html/template" "net/url" "strings" "sync" "time" - html_template "html/template" - - yaml "gopkg.in/yaml.v2" - "github.com/go-kit/kit/log" "github.com/go-kit/kit/log/level" "github.com/pkg/errors" "github.com/prometheus/common/model" + yaml "gopkg.in/yaml.v2" "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/rulefmt" diff --git a/rules/alerting_test.go b/rules/alerting_test.go index 2cca9ee7a4..dce0302fa1 100644 --- a/rules/alerting_test.go +++ b/rules/alerting_test.go @@ -15,22 +15,22 @@ package rules import ( "context" - "fmt" "testing" "time" "github.com/go-kit/kit/log" + "github.com/stretchr/testify/assert" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/timestamp" "github.com/prometheus/prometheus/promql" "github.com/prometheus/prometheus/promql/parser" "github.com/prometheus/prometheus/util/teststorage" - "github.com/prometheus/prometheus/util/testutil" ) func TestAlertingRuleHTMLSnippet(t *testing.T) { expr, err := parser.ParseExpr(`foo{html="BOLD"}`) - testutil.Ok(t, err) + assert.NoError(t, err) rule := NewAlertingRule("testrule", expr, 0, labels.FromStrings("html", "BOLD"), labels.FromStrings("html", "BOLD"), nil, false, nil) const want = `alert: testrule @@ -42,7 +42,7 @@ annotations: ` got := rule.HTMLSnippet("/test/prefix") - testutil.Assert(t, want == got, "incorrect HTML snippet; want:\n\n|%v|\n\ngot:\n\n|%v|", want, got) + assert.True(t, want == got, "incorrect HTML snippet; want:\n\n|%v|\n\ngot:\n\n|%v|", want, got) } func TestAlertingRuleState(t *testing.T) { @@ -81,7 +81,7 @@ func TestAlertingRuleState(t *testing.T) { rule := NewAlertingRule(test.name, nil, 0, nil, nil, nil, true, nil) rule.active = test.active got := rule.State() - testutil.Assert(t, test.want == got, "test case %d unexpected AlertState, want:%d got:%d", i, test.want, got) + assert.True(t, test.want == got, "test case %d unexpected AlertState, want:%d got:%d", i, test.want, got) } } @@ -90,13 +90,13 @@ func TestAlertingRuleLabelsUpdate(t *testing.T) { load 1m http_requests{job="app-server", instance="0"} 75 85 70 70 `) - testutil.Ok(t, err) + assert.NoError(t, err) defer suite.Close() - testutil.Ok(t, suite.Run()) + assert.NoError(t, suite.Run()) expr, err := parser.ParseExpr(`http_requests < 100`) - testutil.Ok(t, err) + assert.NoError(t, err) rule := NewAlertingRule( "HTTPRequestRateLow", @@ -170,7 +170,7 @@ func TestAlertingRuleLabelsUpdate(t *testing.T) { evalTime := baseTime.Add(time.Duration(i) * time.Minute) result[0].Point.T = timestamp.FromTime(evalTime) res, err := rule.Eval(suite.Context(), evalTime, EngineQueryFunc(suite.QueryEngine(), suite.Storage()), nil) - testutil.Ok(t, err) + assert.NoError(t, err) var filteredRes promql.Vector // After removing 'ALERTS_FOR_STATE' samples. for _, smpl := range res { @@ -179,11 +179,11 @@ func TestAlertingRuleLabelsUpdate(t *testing.T) { filteredRes = append(filteredRes, smpl) } else { // If not 'ALERTS', it has to be 'ALERTS_FOR_STATE'. - testutil.Equals(t, "ALERTS_FOR_STATE", smplName) + assert.Equal(t, "ALERTS_FOR_STATE", smplName) } } - testutil.Equals(t, result, filteredRes) + assert.Equal(t, result, filteredRes) } } @@ -192,13 +192,13 @@ func TestAlertingRuleExternalLabelsInTemplate(t *testing.T) { load 1m http_requests{job="app-server", instance="0"} 75 85 70 70 `) - testutil.Ok(t, err) + assert.NoError(t, err) defer suite.Close() - testutil.Ok(t, suite.Run()) + assert.NoError(t, suite.Run()) expr, err := parser.ParseExpr(`http_requests < 100`) - testutil.Ok(t, err) + assert.NoError(t, err) ruleWithoutExternalLabels := NewAlertingRule( "ExternalLabelDoesNotExist", @@ -251,32 +251,32 @@ func TestAlertingRuleExternalLabelsInTemplate(t *testing.T) { res, err := ruleWithoutExternalLabels.Eval( suite.Context(), evalTime, EngineQueryFunc(suite.QueryEngine(), suite.Storage()), nil, ) - testutil.Ok(t, err) + assert.NoError(t, err) for _, smpl := range res { smplName := smpl.Metric.Get("__name__") if smplName == "ALERTS" { filteredRes = append(filteredRes, smpl) } else { // If not 'ALERTS', it has to be 'ALERTS_FOR_STATE'. - testutil.Equals(t, "ALERTS_FOR_STATE", smplName) + assert.Equal(t, "ALERTS_FOR_STATE", smplName) } } res, err = ruleWithExternalLabels.Eval( suite.Context(), evalTime, EngineQueryFunc(suite.QueryEngine(), suite.Storage()), nil, ) - testutil.Ok(t, err) + assert.NoError(t, err) for _, smpl := range res { smplName := smpl.Metric.Get("__name__") if smplName == "ALERTS" { filteredRes = append(filteredRes, smpl) } else { // If not 'ALERTS', it has to be 'ALERTS_FOR_STATE'. - testutil.Equals(t, "ALERTS_FOR_STATE", smplName) + assert.Equal(t, "ALERTS_FOR_STATE", smplName) } } - testutil.Equals(t, result, filteredRes) + assert.Equal(t, result, filteredRes) } func TestAlertingRuleEmptyLabelFromTemplate(t *testing.T) { @@ -284,13 +284,13 @@ func TestAlertingRuleEmptyLabelFromTemplate(t *testing.T) { load 1m http_requests{job="app-server", instance="0"} 75 85 70 70 `) - testutil.Ok(t, err) + assert.NoError(t, err) defer suite.Close() - testutil.Ok(t, suite.Run()) + assert.NoError(t, suite.Run()) expr, err := parser.ParseExpr(`http_requests < 100`) - testutil.Ok(t, err) + assert.NoError(t, err) rule := NewAlertingRule( "EmptyLabel", @@ -321,17 +321,17 @@ func TestAlertingRuleEmptyLabelFromTemplate(t *testing.T) { res, err := rule.Eval( suite.Context(), evalTime, EngineQueryFunc(suite.QueryEngine(), suite.Storage()), nil, ) - testutil.Ok(t, err) + assert.NoError(t, err) for _, smpl := range res { smplName := smpl.Metric.Get("__name__") if smplName == "ALERTS" { filteredRes = append(filteredRes, smpl) } else { // If not 'ALERTS', it has to be 'ALERTS_FOR_STATE'. - testutil.Equals(t, "ALERTS_FOR_STATE", smplName) + assert.Equal(t, "ALERTS_FOR_STATE", smplName) } } - testutil.Equals(t, result, filteredRes) + assert.Equal(t, result, filteredRes) } func TestAlertingRuleDuplicate(t *testing.T) { @@ -362,7 +362,6 @@ func TestAlertingRuleDuplicate(t *testing.T) { true, log.NewNopLogger(), ) _, err := rule.Eval(ctx, now, EngineQueryFunc(engine, storage), nil) - testutil.NotOk(t, err) - e := fmt.Errorf("vector contains metrics with the same labelset after applying alert labels") - testutil.ErrorEqual(t, e, err) + assert.Error(t, err) + assert.EqualError(t, err, "vector contains metrics with the same labelset after applying alert labels") } diff --git a/rules/manager_test.go b/rules/manager_test.go index 93e9cc8ba2..753fbaf876 100644 --- a/rules/manager_test.go +++ b/rules/manager_test.go @@ -26,6 +26,7 @@ import ( "github.com/go-kit/kit/log" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/common/model" + "github.com/stretchr/testify/assert" "go.uber.org/goleak" yaml "gopkg.in/yaml.v2" @@ -37,7 +38,6 @@ import ( "github.com/prometheus/prometheus/promql/parser" "github.com/prometheus/prometheus/storage" "github.com/prometheus/prometheus/util/teststorage" - "github.com/prometheus/prometheus/util/testutil" ) func TestMain(m *testing.M) { @@ -50,14 +50,14 @@ func TestAlertingRule(t *testing.T) { http_requests{job="app-server", instance="0", group="canary", severity="overwrite-me"} 75 85 95 105 105 95 85 http_requests{job="app-server", instance="1", group="canary", severity="overwrite-me"} 80 90 100 110 120 130 140 `) - testutil.Ok(t, err) + assert.NoError(t, err) defer suite.Close() err = suite.Run() - testutil.Ok(t, err) + assert.NoError(t, err) expr, err := parser.ParseExpr(`http_requests{group="canary", job="app-server"} < 100`) - testutil.Ok(t, err) + assert.NoError(t, err) rule := NewAlertingRule( "HTTPRequestRateLow", @@ -157,7 +157,7 @@ func TestAlertingRule(t *testing.T) { evalTime := baseTime.Add(test.time) res, err := rule.Eval(suite.Context(), evalTime, EngineQueryFunc(suite.QueryEngine(), suite.Storage()), nil) - testutil.Ok(t, err) + assert.NoError(t, err) var filteredRes promql.Vector // After removing 'ALERTS_FOR_STATE' samples. for _, smpl := range res { @@ -166,21 +166,21 @@ func TestAlertingRule(t *testing.T) { filteredRes = append(filteredRes, smpl) } else { // If not 'ALERTS', it has to be 'ALERTS_FOR_STATE'. - testutil.Equals(t, smplName, "ALERTS_FOR_STATE") + assert.Equal(t, smplName, "ALERTS_FOR_STATE") } } for i := range test.result { test.result[i].T = timestamp.FromTime(evalTime) } - testutil.Assert(t, len(test.result) == len(filteredRes), "%d. Number of samples in expected and actual output don't match (%d vs. %d)", i, len(test.result), len(res)) + assert.True(t, len(test.result) == len(filteredRes), "%d. Number of samples in expected and actual output don't match (%d vs. %d)", i, len(test.result), len(res)) sort.Slice(filteredRes, func(i, j int) bool { return labels.Compare(filteredRes[i].Metric, filteredRes[j].Metric) < 0 }) - testutil.Equals(t, test.result, filteredRes) + assert.Equal(t, test.result, filteredRes) for _, aa := range rule.ActiveAlerts() { - testutil.Assert(t, aa.Labels.Get(model.MetricNameLabel) == "", "%s label set on active alert: %s", model.MetricNameLabel, aa.Labels) + assert.True(t, aa.Labels.Get(model.MetricNameLabel) == "", "%s label set on active alert: %s", model.MetricNameLabel, aa.Labels) } } } @@ -191,14 +191,14 @@ func TestForStateAddSamples(t *testing.T) { http_requests{job="app-server", instance="0", group="canary", severity="overwrite-me"} 75 85 95 105 105 95 85 http_requests{job="app-server", instance="1", group="canary", severity="overwrite-me"} 80 90 100 110 120 130 140 `) - testutil.Ok(t, err) + assert.NoError(t, err) defer suite.Close() err = suite.Run() - testutil.Ok(t, err) + assert.NoError(t, err) expr, err := parser.ParseExpr(`http_requests{group="canary", job="app-server"} < 100`) - testutil.Ok(t, err) + assert.NoError(t, err) rule := NewAlertingRule( "HTTPRequestRateLow", @@ -306,7 +306,7 @@ func TestForStateAddSamples(t *testing.T) { } res, err := rule.Eval(suite.Context(), evalTime, EngineQueryFunc(suite.QueryEngine(), suite.Storage()), nil) - testutil.Ok(t, err) + assert.NoError(t, err) var filteredRes promql.Vector // After removing 'ALERTS' samples. for _, smpl := range res { @@ -315,7 +315,7 @@ func TestForStateAddSamples(t *testing.T) { filteredRes = append(filteredRes, smpl) } else { // If not 'ALERTS_FOR_STATE', it has to be 'ALERTS'. - testutil.Equals(t, smplName, "ALERTS") + assert.Equal(t, smplName, "ALERTS") } } for i := range test.result { @@ -325,15 +325,15 @@ func TestForStateAddSamples(t *testing.T) { test.result[i].V = forState } } - testutil.Assert(t, len(test.result) == len(filteredRes), "%d. Number of samples in expected and actual output don't match (%d vs. %d)", i, len(test.result), len(res)) + assert.True(t, len(test.result) == len(filteredRes), "%d. Number of samples in expected and actual output don't match (%d vs. %d)", i, len(test.result), len(res)) sort.Slice(filteredRes, func(i, j int) bool { return labels.Compare(filteredRes[i].Metric, filteredRes[j].Metric) < 0 }) - testutil.Equals(t, test.result, filteredRes) + assert.Equal(t, test.result, filteredRes) for _, aa := range rule.ActiveAlerts() { - testutil.Assert(t, aa.Labels.Get(model.MetricNameLabel) == "", "%s label set on active alert: %s", model.MetricNameLabel, aa.Labels) + assert.True(t, aa.Labels.Get(model.MetricNameLabel) == "", "%s label set on active alert: %s", model.MetricNameLabel, aa.Labels) } } @@ -352,14 +352,14 @@ func TestForStateRestore(t *testing.T) { http_requests{job="app-server", instance="0", group="canary", severity="overwrite-me"} 75 85 50 0 0 25 0 0 40 0 120 http_requests{job="app-server", instance="1", group="canary", severity="overwrite-me"} 125 90 60 0 0 25 0 0 40 0 130 `) - testutil.Ok(t, err) + assert.NoError(t, err) defer suite.Close() err = suite.Run() - testutil.Ok(t, err) + assert.NoError(t, err) expr, err := parser.ParseExpr(`http_requests{group="canary", job="app-server"} < 100`) - testutil.Ok(t, err) + assert.NoError(t, err) opts := &ManagerOptions{ QueryFunc: EngineQueryFunc(suite.QueryEngine(), suite.Storage()), @@ -402,7 +402,7 @@ func TestForStateRestore(t *testing.T) { exp := rule.ActiveAlerts() for _, aa := range exp { - testutil.Assert(t, aa.Labels.Get(model.MetricNameLabel) == "", "%s label set on active alert: %s", model.MetricNameLabel, aa.Labels) + assert.True(t, aa.Labels.Get(model.MetricNameLabel) == "", "%s label set on active alert: %s", model.MetricNameLabel, aa.Labels) } sort.Slice(exp, func(i, j int) bool { return labels.Compare(exp[i].Labels, exp[j].Labels) < 0 @@ -466,7 +466,7 @@ func TestForStateRestore(t *testing.T) { got := newRule.ActiveAlerts() for _, aa := range got { - testutil.Assert(t, aa.Labels.Get(model.MetricNameLabel) == "", "%s label set on active alert: %s", model.MetricNameLabel, aa.Labels) + assert.True(t, aa.Labels.Get(model.MetricNameLabel) == "", "%s label set on active alert: %s", model.MetricNameLabel, aa.Labels) } sort.Slice(got, func(i, j int) bool { return labels.Compare(got[i].Labels, got[j].Labels) < 0 @@ -474,27 +474,27 @@ func TestForStateRestore(t *testing.T) { // Checking if we have restored it correctly. if tst.noRestore { - testutil.Equals(t, tst.num, len(got)) + assert.Equal(t, tst.num, len(got)) for _, e := range got { - testutil.Equals(t, e.ActiveAt, restoreTime) + assert.Equal(t, e.ActiveAt, restoreTime) } } else if tst.gracePeriod { - testutil.Equals(t, tst.num, len(got)) + assert.Equal(t, tst.num, len(got)) for _, e := range got { - testutil.Equals(t, opts.ForGracePeriod, e.ActiveAt.Add(alertForDuration).Sub(restoreTime)) + assert.Equal(t, opts.ForGracePeriod, e.ActiveAt.Add(alertForDuration).Sub(restoreTime)) } } else { exp := tst.alerts - testutil.Equals(t, len(exp), len(got)) + assert.Equal(t, len(exp), len(got)) sortAlerts(exp) sortAlerts(got) for i, e := range exp { - testutil.Equals(t, e.Labels, got[i].Labels) + assert.Equal(t, e.Labels, got[i].Labels) // Difference in time should be within 1e6 ns, i.e. 1ms // (due to conversion between ns & ms, float64 & int64). activeAtDiff := float64(e.ActiveAt.Unix() + int64(tst.downDuration/time.Second) - got[i].ActiveAt.Unix()) - testutil.Assert(t, math.Abs(activeAtDiff) == 0, "'for' state restored time is wrong") + assert.True(t, math.Abs(activeAtDiff) == 0, "'for' state restored time is wrong") } } } @@ -535,7 +535,7 @@ func TestStaleness(t *testing.T) { } expr, err := parser.ParseExpr("a + 1") - testutil.Ok(t, err) + assert.NoError(t, err) rule := NewRecordingRule("a_plus_one", expr, labels.Labels{}) group := NewGroup(GroupOptions{ Name: "default", @@ -552,7 +552,7 @@ func TestStaleness(t *testing.T) { app.Add(labels.FromStrings(model.MetricNameLabel, "a"), 2000, math.Float64frombits(value.StaleNaN)) err = app.Commit() - testutil.Ok(t, err) + assert.NoError(t, err) ctx := context.Background() @@ -562,31 +562,31 @@ func TestStaleness(t *testing.T) { group.Eval(ctx, time.Unix(2, 0)) querier, err := st.Querier(context.Background(), 0, 2000) - testutil.Ok(t, err) + assert.NoError(t, err) defer querier.Close() matcher, err := labels.NewMatcher(labels.MatchEqual, model.MetricNameLabel, "a_plus_one") - testutil.Ok(t, err) + assert.NoError(t, err) set := querier.Select(false, nil, matcher) samples, err := readSeriesSet(set) - testutil.Ok(t, err) + assert.NoError(t, err) metric := labels.FromStrings(model.MetricNameLabel, "a_plus_one").String() metricSample, ok := samples[metric] - testutil.Assert(t, ok, "Series %s not returned.", metric) - testutil.Assert(t, value.IsStaleNaN(metricSample[2].V), "Appended second sample not as expected. Wanted: stale NaN Got: %x", math.Float64bits(metricSample[2].V)) - metricSample[2].V = 42 // reflect.DeepEqual cannot handle NaN. + assert.True(t, ok, "Series %s not returned.", metric) + assert.True(t, value.IsStaleNaN(metricSample[2].V), "Appended second sample not as expected. Wanted: stale NaN Got: %x", math.Float64bits(metricSample[2].V)) + metricSample[2].V = 42 // assert.Equal cannot handle NaN. want := map[string][]promql.Point{ metric: {{T: 0, V: 2}, {T: 1000, V: 3}, {T: 2000, V: 42}}, } - testutil.Equals(t, want, samples) + assert.Equal(t, want, samples) } -// Convert a SeriesSet into a form usable with reflect.DeepEqual. +// Convert a SeriesSet into a form usable with assert.Equal. func readSeriesSet(ss storage.SeriesSet) (map[string][]promql.Point, error) { result := map[string][]promql.Point{} @@ -654,11 +654,11 @@ func TestCopyState(t *testing.T) { {"a2": labels.Labels{{Name: "l2", Value: "v1"}}}, nil, } - testutil.Equals(t, want, newGroup.seriesInPreviousEval) - testutil.Equals(t, oldGroup.rules[0], newGroup.rules[3]) - testutil.Equals(t, oldGroup.evaluationTime, newGroup.evaluationTime) - testutil.Equals(t, oldGroup.lastEvaluation, newGroup.lastEvaluation) - testutil.Equals(t, []labels.Labels{{{Name: "l1", Value: "v3"}}}, newGroup.staleSeries) + assert.Equal(t, want, newGroup.seriesInPreviousEval) + assert.Equal(t, oldGroup.rules[0], newGroup.rules[3]) + assert.Equal(t, oldGroup.evaluationTime, newGroup.evaluationTime) + assert.Equal(t, oldGroup.lastEvaluation, newGroup.lastEvaluation) + assert.Equal(t, []labels.Labels{{{Name: "l1", Value: "v3"}}}, newGroup.staleSeries) } func TestDeletedRuleMarkedStale(t *testing.T) { @@ -684,21 +684,21 @@ func TestDeletedRuleMarkedStale(t *testing.T) { newGroup.Eval(context.Background(), time.Unix(0, 0)) querier, err := st.Querier(context.Background(), 0, 2000) - testutil.Ok(t, err) + assert.NoError(t, err) defer querier.Close() matcher, err := labels.NewMatcher(labels.MatchEqual, "l1", "v1") - testutil.Ok(t, err) + assert.NoError(t, err) set := querier.Select(false, nil, matcher) samples, err := readSeriesSet(set) - testutil.Ok(t, err) + assert.NoError(t, err) metric := labels.FromStrings("l1", "v1").String() metricSample, ok := samples[metric] - testutil.Assert(t, ok, "Series %s not returned.", metric) - testutil.Assert(t, value.IsStaleNaN(metricSample[0].V), "Appended sample not as expected. Wanted: stale NaN Got: %x", math.Float64bits(metricSample[0].V)) + assert.True(t, ok, "Series %s not returned.", metric) + assert.True(t, value.IsStaleNaN(metricSample[0].V), "Appended sample not as expected. Wanted: stale NaN Got: %x", math.Float64bits(metricSample[0].V)) } func TestUpdate(t *testing.T) { @@ -726,8 +726,8 @@ func TestUpdate(t *testing.T) { defer ruleManager.Stop() err := ruleManager.Update(10*time.Second, files, nil) - testutil.Ok(t, err) - testutil.Assert(t, len(ruleManager.groups) > 0, "expected non-empty rule groups") + assert.NoError(t, err) + assert.True(t, len(ruleManager.groups) > 0, "expected non-empty rule groups") ogs := map[string]*Group{} for h, g := range ruleManager.groups { g.seriesInPreviousEval = []map[string]labels.Labels{ @@ -737,26 +737,26 @@ func TestUpdate(t *testing.T) { } err = ruleManager.Update(10*time.Second, files, nil) - testutil.Ok(t, err) + assert.NoError(t, err) for h, g := range ruleManager.groups { for _, actual := range g.seriesInPreviousEval { - testutil.Equals(t, expected, actual) + assert.Equal(t, expected, actual) } // Groups are the same because of no updates. - testutil.Equals(t, ogs[h], g) + assert.Equal(t, ogs[h], g) } // Groups will be recreated if updated. rgs, errs := rulefmt.ParseFile("fixtures/rules.yaml") - testutil.Assert(t, len(errs) == 0, "file parsing failures") + assert.True(t, len(errs) == 0, "file parsing failures") tmpFile, err := ioutil.TempFile("", "rules.test.*.yaml") - testutil.Ok(t, err) + assert.NoError(t, err) defer os.Remove(tmpFile.Name()) defer tmpFile.Close() err = ruleManager.Update(10*time.Second, []string{tmpFile.Name()}, nil) - testutil.Ok(t, err) + assert.NoError(t, err) for h, g := range ruleManager.groups { ogs[h] = g @@ -822,12 +822,12 @@ func formatRules(r *rulefmt.RuleGroups) ruleGroupsTest { func reloadAndValidate(rgs *rulefmt.RuleGroups, t *testing.T, tmpFile *os.File, ruleManager *Manager, expected map[string]labels.Labels, ogs map[string]*Group) { bs, err := yaml.Marshal(formatRules(rgs)) - testutil.Ok(t, err) + assert.NoError(t, err) tmpFile.Seek(0, 0) _, err = tmpFile.Write(bs) - testutil.Ok(t, err) + assert.NoError(t, err) err = ruleManager.Update(10*time.Second, []string{tmpFile.Name()}, nil) - testutil.Ok(t, err) + assert.NoError(t, err) for h, g := range ruleManager.groups { if ogs[h] == g { t.Fail() @@ -861,7 +861,7 @@ func TestNotify(t *testing.T) { } expr, err := parser.ParseExpr("a > 1") - testutil.Ok(t, err) + assert.NoError(t, err) rule := NewAlertingRule("aTooHigh", expr, 0, labels.Labels{}, labels.Labels{}, nil, true, log.NewNopLogger()) group := NewGroup(GroupOptions{ Name: "alert", @@ -878,26 +878,26 @@ func TestNotify(t *testing.T) { app.Add(labels.FromStrings(model.MetricNameLabel, "a"), 6000, 0) err = app.Commit() - testutil.Ok(t, err) + assert.NoError(t, err) ctx := context.Background() // Alert sent right away group.Eval(ctx, time.Unix(1, 0)) - testutil.Equals(t, 1, len(lastNotified)) - testutil.Assert(t, !lastNotified[0].ValidUntil.IsZero(), "ValidUntil should not be zero") + assert.Equal(t, 1, len(lastNotified)) + assert.True(t, !lastNotified[0].ValidUntil.IsZero(), "ValidUntil should not be zero") // Alert is not sent 1s later group.Eval(ctx, time.Unix(2, 0)) - testutil.Equals(t, 0, len(lastNotified)) + assert.Equal(t, 0, len(lastNotified)) // Alert is resent at t=5s group.Eval(ctx, time.Unix(5, 0)) - testutil.Equals(t, 1, len(lastNotified)) + assert.Equal(t, 1, len(lastNotified)) // Resolution alert sent right away group.Eval(ctx, time.Unix(6, 0)) - testutil.Equals(t, 1, len(lastNotified)) + assert.Equal(t, 1, len(lastNotified)) } func TestMetricsUpdate(t *testing.T) { @@ -934,7 +934,7 @@ func TestMetricsUpdate(t *testing.T) { countMetrics := func() int { ms, err := registry.Gather() - testutil.Ok(t, err) + assert.NoError(t, err) var metrics int for _, m := range ms { s := m.GetName() @@ -972,9 +972,9 @@ func TestMetricsUpdate(t *testing.T) { for i, c := range cases { err := ruleManager.Update(time.Second, c.files, nil) - testutil.Ok(t, err) + assert.NoError(t, err) time.Sleep(2 * time.Second) - testutil.Equals(t, c.metrics, countMetrics(), "test %d: invalid count of metrics", i) + assert.Equal(t, c.metrics, countMetrics(), "test %d: invalid count of metrics", i) } } @@ -1046,14 +1046,14 @@ func TestGroupStalenessOnRemoval(t *testing.T) { var totalStaleNaN int for i, c := range cases { err := ruleManager.Update(time.Second, c.files, nil) - testutil.Ok(t, err) + assert.NoError(t, err) time.Sleep(3 * time.Second) totalStaleNaN += c.staleNaN - testutil.Equals(t, totalStaleNaN, countStaleNaN(t, storage), "test %d/%q: invalid count of staleness markers", i, c.files) + assert.Equal(t, totalStaleNaN, countStaleNaN(t, storage), "test %d/%q: invalid count of staleness markers", i, c.files) } ruleManager.Stop() stopped = true - testutil.Equals(t, totalStaleNaN, countStaleNaN(t, storage), "invalid count of staleness markers after stopping the engine") + assert.Equal(t, totalStaleNaN, countStaleNaN(t, storage), "invalid count of staleness markers after stopping the engine") } func TestMetricsStalenessOnManagerShutdown(t *testing.T) { @@ -1089,34 +1089,34 @@ func TestMetricsStalenessOnManagerShutdown(t *testing.T) { err := ruleManager.Update(2*time.Second, files, nil) time.Sleep(4 * time.Second) - testutil.Ok(t, err) + assert.NoError(t, err) start := time.Now() err = ruleManager.Update(3*time.Second, files[:0], nil) - testutil.Ok(t, err) + assert.NoError(t, err) ruleManager.Stop() stopped = true - testutil.Assert(t, time.Since(start) < 1*time.Second, "rule manager does not stop early") + assert.True(t, time.Since(start) < 1*time.Second, "rule manager does not stop early") time.Sleep(5 * time.Second) - testutil.Equals(t, 0, countStaleNaN(t, storage), "invalid count of staleness markers after stopping the engine") + assert.Equal(t, 0, countStaleNaN(t, storage), "invalid count of staleness markers after stopping the engine") } func countStaleNaN(t *testing.T, st storage.Storage) int { var c int querier, err := st.Querier(context.Background(), 0, time.Now().Unix()*1000) - testutil.Ok(t, err) + assert.NoError(t, err) defer querier.Close() matcher, err := labels.NewMatcher(labels.MatchEqual, model.MetricNameLabel, "test_2") - testutil.Ok(t, err) + assert.NoError(t, err) set := querier.Select(false, nil, matcher) samples, err := readSeriesSet(set) - testutil.Ok(t, err) + assert.NoError(t, err) metric := labels.FromStrings(model.MetricNameLabel, "test_2").String() metricSample, ok := samples[metric] - testutil.Assert(t, ok, "Series %s not returned.", metric) + assert.True(t, ok, "Series %s not returned.", metric) for _, s := range metricSample { if value.IsStaleNaN(s.V) { c++ @@ -1160,6 +1160,6 @@ func TestGroupHasAlertingRules(t *testing.T) { for i, test := range tests { got := test.group.HasAlertingRules() - testutil.Assert(t, test.want == got, "test case %d failed, expected:%t got:%t", i, test.want, got) + assert.True(t, test.want == got, "test case %d failed, expected:%t got:%t", i, test.want, got) } } diff --git a/rules/recording_test.go b/rules/recording_test.go index 30f818cfb0..274944cfaf 100644 --- a/rules/recording_test.go +++ b/rules/recording_test.go @@ -15,16 +15,16 @@ package rules import ( "context" - "fmt" "testing" "time" + "github.com/stretchr/testify/assert" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/timestamp" "github.com/prometheus/prometheus/promql" "github.com/prometheus/prometheus/promql/parser" "github.com/prometheus/prometheus/util/teststorage" - "github.com/prometheus/prometheus/util/testutil" ) func TestRuleEval(t *testing.T) { @@ -73,14 +73,14 @@ func TestRuleEval(t *testing.T) { for _, test := range suite { rule := NewRecordingRule(test.name, test.expr, test.labels) result, err := rule.Eval(ctx, now, EngineQueryFunc(engine, storage), nil) - testutil.Ok(t, err) - testutil.Equals(t, test.result, result) + assert.NoError(t, err) + assert.Equal(t, test.result, result) } } func TestRecordingRuleHTMLSnippet(t *testing.T) { expr, err := parser.ParseExpr(`foo{html="BOLD"}`) - testutil.Ok(t, err) + assert.NoError(t, err) rule := NewRecordingRule("testrule", expr, labels.FromStrings("html", "BOLD")) const want = `record: testrule @@ -90,7 +90,7 @@ labels: ` got := rule.HTMLSnippet("/test/prefix") - testutil.Assert(t, want == got, "incorrect HTML snippet; want:\n\n%s\n\ngot:\n\n%s", want, got) + assert.True(t, want == got, "incorrect HTML snippet; want:\n\n%s\n\ngot:\n\n%s", want, got) } // TestRuleEvalDuplicate tests for duplicate labels in recorded metrics, see #5529. @@ -114,7 +114,6 @@ func TestRuleEvalDuplicate(t *testing.T) { expr, _ := parser.ParseExpr(`vector(0) or label_replace(vector(0),"test","x","","")`) rule := NewRecordingRule("foo", expr, labels.FromStrings("test", "test")) _, err := rule.Eval(ctx, now, EngineQueryFunc(engine, storage), nil) - testutil.NotOk(t, err) - e := fmt.Errorf("vector contains metrics with the same labelset after applying rule labels") - testutil.ErrorEqual(t, e, err) + assert.Error(t, err) + assert.EqualError(t, err, "vector contains metrics with the same labelset after applying rule labels") } diff --git a/scrape/helpers_test.go b/scrape/helpers_test.go index 095ea7cac3..b42d144d20 100644 --- a/scrape/helpers_test.go +++ b/scrape/helpers_test.go @@ -15,6 +15,7 @@ package scrape import ( "context" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/storage" ) diff --git a/scrape/manager.go b/scrape/manager.go index 26bb0d1a57..bf73561a12 100644 --- a/scrape/manager.go +++ b/scrape/manager.go @@ -26,8 +26,8 @@ import ( "github.com/go-kit/kit/log" "github.com/go-kit/kit/log/level" "github.com/pkg/errors" - "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/discovery/targetgroup" "github.com/prometheus/prometheus/pkg/labels" diff --git a/scrape/manager_test.go b/scrape/manager_test.go index 9443903198..cf47990530 100644 --- a/scrape/manager_test.go +++ b/scrape/manager_test.go @@ -19,15 +19,14 @@ import ( "testing" "time" - "github.com/pkg/errors" "github.com/prometheus/common/model" + "github.com/stretchr/testify/assert" yaml "gopkg.in/yaml.v2" "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/discovery/targetgroup" "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/relabel" - "github.com/prometheus/prometheus/util/testutil" ) func TestPopulateLabels(t *testing.T) { @@ -36,7 +35,7 @@ func TestPopulateLabels(t *testing.T) { cfg *config.ScrapeConfig res labels.Labels resOrig labels.Labels - err error + err string }{ // Regular population of scrape config options. { @@ -129,7 +128,7 @@ func TestPopulateLabels(t *testing.T) { }, res: nil, resOrig: nil, - err: errors.New("no address"), + err: "no address", }, // Address label missing, but added in relabelling. { @@ -208,17 +207,21 @@ func TestPopulateLabels(t *testing.T) { }, res: nil, resOrig: nil, - err: errors.New("invalid label value for \"custom\": \"\\xbd\""), + err: "invalid label value for \"custom\": \"\\xbd\"", }, } for _, c := range cases { in := c.in.Copy() res, orig, err := populateLabels(c.in, c.cfg) - testutil.ErrorEqual(t, c.err, err) - testutil.Equals(t, c.in, in) - testutil.Equals(t, c.res, res) - testutil.Equals(t, c.resOrig, orig) + if c.err != "" { + assert.EqualError(t, err, c.err) + } else { + assert.NoError(t, err) + } + assert.Equal(t, c.in, in) + assert.Equal(t, c.res, res) + assert.Equal(t, c.resOrig, orig) } } @@ -362,7 +365,7 @@ func TestManagerTargetsUpdates(t *testing.T) { m.mtxScrape.Unlock() // Make sure all updates have been received. - testutil.Equals(t, tgSent, tsetActual) + assert.Equal(t, tgSent, tsetActual) select { case <-m.triggerReload: diff --git a/scrape/scrape_test.go b/scrape/scrape_test.go index af6a0719e3..6845d3b7b8 100644 --- a/scrape/scrape_test.go +++ b/scrape/scrape_test.go @@ -33,6 +33,7 @@ import ( dto "github.com/prometheus/client_model/go" config_util "github.com/prometheus/common/config" "github.com/prometheus/common/model" + "github.com/stretchr/testify/assert" "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/discovery/targetgroup" @@ -134,7 +135,7 @@ func TestDiscoveredLabelsUpdate(t *testing.T) { } sp.sync([]*Target{t2}) - testutil.Equals(t, t2.DiscoveredLabels(), sp.activeTargets[t1.hash()].DiscoveredLabels()) + assert.Equal(t, t2.DiscoveredLabels(), sp.activeTargets[t1.hash()].DiscoveredLabels()) } type testLoop struct { @@ -227,11 +228,11 @@ func TestScrapePoolStop(t *testing.T) { } mtx.Lock() - testutil.Equals(t, numTargets, len(stopped), "Unexpected number of stopped loops") + assert.Equal(t, numTargets, len(stopped), "Unexpected number of stopped loops") mtx.Unlock() - testutil.Assert(t, len(sp.activeTargets) == 0, "Targets were not cleared on stopping: %d left", len(sp.activeTargets)) - testutil.Assert(t, len(sp.loops) == 0, "Loops were not cleared on stopping: %d left", len(sp.loops)) + assert.True(t, len(sp.activeTargets) == 0, "Targets were not cleared on stopping: %d left", len(sp.activeTargets)) + assert.True(t, len(sp.loops) == 0, "Loops were not cleared on stopping: %d left", len(sp.loops)) } func TestScrapePoolReload(t *testing.T) { @@ -249,12 +250,12 @@ func TestScrapePoolReload(t *testing.T) { newLoop := func(opts scrapeLoopOptions) loop { l := &testLoop{} l.startFunc = func(interval, timeout time.Duration, errc chan<- error) { - testutil.Equals(t, 3*time.Second, interval, "Unexpected scrape interval") - testutil.Equals(t, 2*time.Second, timeout, "Unexpected scrape timeout") + assert.Equal(t, 3*time.Second, interval, "Unexpected scrape interval") + assert.Equal(t, 2*time.Second, timeout, "Unexpected scrape timeout") mtx.Lock() targetScraper := opts.scraper.(*targetScraper) - testutil.Assert(t, stopped[targetScraper.hash()], "Scrape loop for %v not stopped yet", targetScraper) + assert.True(t, stopped[targetScraper.hash()], "Scrape loop for %v not stopped yet", targetScraper) mtx.Unlock() } return l @@ -313,11 +314,11 @@ func TestScrapePoolReload(t *testing.T) { } mtx.Lock() - testutil.Equals(t, numTargets, len(stopped), "Unexpected number of stopped loops") + assert.Equal(t, numTargets, len(stopped), "Unexpected number of stopped loops") mtx.Unlock() - testutil.Equals(t, sp.activeTargets, beforeTargets, "Reloading affected target states unexpectedly") - testutil.Equals(t, numTargets, len(sp.loops), "Unexpected number of stopped loops after reload") + assert.Equal(t, sp.activeTargets, beforeTargets, "Reloading affected target states unexpectedly") + assert.Equal(t, numTargets, len(sp.loops), "Unexpected number of stopped loops after reload") } func TestScrapePoolTargetLimit(t *testing.T) { @@ -357,7 +358,7 @@ func TestScrapePoolTargetLimit(t *testing.T) { var limit uint reloadWithLimit := func(l uint) { limit = l - testutil.Ok(t, sp.reload(&config.ScrapeConfig{ + assert.NoError(t, sp.reload(&config.ScrapeConfig{ ScrapeInterval: model.Duration(3 * time.Second), ScrapeTimeout: model.Duration(2 * time.Second), TargetLimit: l, @@ -373,7 +374,7 @@ func TestScrapePoolTargetLimit(t *testing.T) { validateIsRunning := func() { wg.Wait() for _, l := range sp.loops { - testutil.Assert(t, l.(*testLoop).runOnce, "loop should be running") + assert.True(t, l.(*testLoop).runOnce, "loop should be running") } } @@ -381,10 +382,10 @@ func TestScrapePoolTargetLimit(t *testing.T) { for _, l := range sp.loops { lerr := l.(*testLoop).getForcedError() if shouldErr { - testutil.Assert(t, lerr != nil, "error was expected for %d targets with a limit of %d", targets, limit) - testutil.Equals(t, fmt.Sprintf("target_limit exceeded (number of targets: %d, limit: %d)", targets, limit), lerr.Error()) + assert.NotNil(t, lerr, "error was expected for %d targets with a limit of %d", targets, limit) + assert.Equal(t, fmt.Sprintf("target_limit exceeded (number of targets: %d, limit: %d)", targets, limit), lerr.Error()) } else { - testutil.Equals(t, nil, lerr) + assert.Equal(t, nil, lerr) } } } @@ -451,33 +452,33 @@ func TestScrapePoolAppender(t *testing.T) { target: &Target{}, }) appl, ok := loop.(*scrapeLoop) - testutil.Assert(t, ok, "Expected scrapeLoop but got %T", loop) + assert.True(t, ok, "Expected scrapeLoop but got %T", loop) wrapped := appl.appender(context.Background()) tl, ok := wrapped.(*timeLimitAppender) - testutil.Assert(t, ok, "Expected timeLimitAppender but got %T", wrapped) + assert.True(t, ok, "Expected timeLimitAppender but got %T", wrapped) _, ok = tl.Appender.(nopAppender) - testutil.Assert(t, ok, "Expected base appender but got %T", tl.Appender) + assert.True(t, ok, "Expected base appender but got %T", tl.Appender) loop = sp.newLoop(scrapeLoopOptions{ target: &Target{}, limit: 100, }) appl, ok = loop.(*scrapeLoop) - testutil.Assert(t, ok, "Expected scrapeLoop but got %T", loop) + assert.True(t, ok, "Expected scrapeLoop but got %T", loop) wrapped = appl.appender(context.Background()) sl, ok := wrapped.(*limitAppender) - testutil.Assert(t, ok, "Expected limitAppender but got %T", wrapped) + assert.True(t, ok, "Expected limitAppender but got %T", wrapped) tl, ok = sl.Appender.(*timeLimitAppender) - testutil.Assert(t, ok, "Expected limitAppender but got %T", sl.Appender) + assert.True(t, ok, "Expected limitAppender but got %T", sl.Appender) _, ok = tl.Appender.(nopAppender) - testutil.Assert(t, ok, "Expected base appender but got %T", tl.Appender) + assert.True(t, ok, "Expected base appender but got %T", tl.Appender) } func TestScrapePoolRaces(t *testing.T) { @@ -507,8 +508,8 @@ func TestScrapePoolRaces(t *testing.T) { dropped := sp.DroppedTargets() expectedActive, expectedDropped := len(tgts[0].Targets), 0 - testutil.Equals(t, expectedActive, len(active), "Invalid number of active targets") - testutil.Equals(t, expectedDropped, len(dropped), "Invalid number of dropped targets") + assert.Equal(t, expectedActive, len(active), "Invalid number of active targets") + assert.Equal(t, expectedDropped, len(dropped), "Invalid number of dropped targets") for i := 0; i < 20; i++ { time.Sleep(time.Duration(10 * time.Millisecond)) @@ -551,17 +552,17 @@ func TestScrapePoolScrapeLoopsStarted(t *testing.T) { }, } - testutil.Ok(t, sp.reload(&config.ScrapeConfig{ + assert.NoError(t, sp.reload(&config.ScrapeConfig{ ScrapeInterval: model.Duration(3 * time.Second), ScrapeTimeout: model.Duration(2 * time.Second), })) sp.Sync(tgs) - testutil.Equals(t, 1, len(sp.loops)) + assert.Equal(t, 1, len(sp.loops)) wg.Wait() for _, l := range sp.loops { - testutil.Assert(t, l.(*testLoop).runOnce, "loop should be running") + assert.True(t, l.(*testLoop).runOnce, "loop should be running") } } @@ -865,27 +866,27 @@ test_metric 1 # TYPE test_metric_no_help gauge # HELP test_metric_no_type other help text # EOF`), "application/openmetrics-text", time.Now()) - testutil.Ok(t, err) - testutil.Ok(t, slApp.Commit()) - testutil.Equals(t, 1, total) + assert.NoError(t, err) + assert.NoError(t, slApp.Commit()) + assert.Equal(t, 1, total) md, ok := cache.GetMetadata("test_metric") - testutil.Assert(t, ok, "expected metadata to be present") - testutil.Assert(t, textparse.MetricTypeCounter == md.Type, "unexpected metric type") - testutil.Equals(t, "some help text", md.Help) - testutil.Equals(t, "metric", md.Unit) + assert.True(t, ok, "expected metadata to be present") + assert.True(t, textparse.MetricTypeCounter == md.Type, "unexpected metric type") + assert.Equal(t, "some help text", md.Help) + assert.Equal(t, "metric", md.Unit) md, ok = cache.GetMetadata("test_metric_no_help") - testutil.Assert(t, ok, "expected metadata to be present") - testutil.Assert(t, textparse.MetricTypeGauge == md.Type, "unexpected metric type") - testutil.Equals(t, "", md.Help) - testutil.Equals(t, "", md.Unit) + assert.True(t, ok, "expected metadata to be present") + assert.True(t, textparse.MetricTypeGauge == md.Type, "unexpected metric type") + assert.Equal(t, "", md.Help) + assert.Equal(t, "", md.Unit) md, ok = cache.GetMetadata("test_metric_no_type") - testutil.Assert(t, ok, "expected metadata to be present") - testutil.Assert(t, textparse.MetricTypeUnknown == md.Type, "unexpected metric type") - testutil.Equals(t, "other help text", md.Help) - testutil.Equals(t, "", md.Unit) + assert.True(t, ok, "expected metadata to be present") + assert.True(t, textparse.MetricTypeUnknown == md.Type, "unexpected metric type") + assert.Equal(t, "other help text", md.Help) + assert.Equal(t, "", md.Unit) } func TestScrapeLoopSeriesAdded(t *testing.T) { @@ -908,19 +909,19 @@ func TestScrapeLoopSeriesAdded(t *testing.T) { slApp := sl.appender(ctx) total, added, seriesAdded, err := sl.append(slApp, []byte("test_metric 1\n"), "", time.Time{}) - testutil.Ok(t, err) - testutil.Ok(t, slApp.Commit()) - testutil.Equals(t, 1, total) - testutil.Equals(t, 1, added) - testutil.Equals(t, 1, seriesAdded) + assert.NoError(t, err) + assert.NoError(t, slApp.Commit()) + assert.Equal(t, 1, total) + assert.Equal(t, 1, added) + assert.Equal(t, 1, seriesAdded) slApp = sl.appender(ctx) total, added, seriesAdded, err = sl.append(slApp, []byte("test_metric 1\n"), "", time.Time{}) - testutil.Ok(t, slApp.Commit()) - testutil.Ok(t, err) - testutil.Equals(t, 1, total) - testutil.Equals(t, 1, added) - testutil.Equals(t, 0, seriesAdded) + assert.NoError(t, slApp.Commit()) + assert.NoError(t, err) + assert.Equal(t, 1, total) + assert.Equal(t, 1, added) + assert.Equal(t, 0, seriesAdded) } func TestScrapeLoopRunCreatesStaleMarkersOnFailedScrape(t *testing.T) { @@ -970,9 +971,9 @@ func TestScrapeLoopRunCreatesStaleMarkersOnFailedScrape(t *testing.T) { // 1 successfully scraped sample, 1 stale marker after first fail, 5 report samples for // each scrape successful or not. - testutil.Equals(t, 27, len(appender.result), "Appended samples not as expected") - testutil.Equals(t, 42.0, appender.result[0].v, "Appended first sample not as expected") - testutil.Assert(t, value.IsStaleNaN(appender.result[6].v), + assert.Equal(t, 27, len(appender.result), "Appended samples not as expected") + assert.Equal(t, 42.0, appender.result[0].v, "Appended first sample not as expected") + assert.True(t, value.IsStaleNaN(appender.result[6].v), "Appended second sample not as expected. Wanted: stale NaN Got: %x", math.Float64bits(appender.result[6].v)) } @@ -1026,9 +1027,9 @@ func TestScrapeLoopRunCreatesStaleMarkersOnParseFailure(t *testing.T) { // 1 successfully scraped sample, 1 stale marker after first fail, 5 report samples for // each scrape successful or not. - testutil.Equals(t, 17, len(appender.result), "Appended samples not as expected") - testutil.Equals(t, 42.0, appender.result[0].v, "Appended first sample not as expected") - testutil.Assert(t, value.IsStaleNaN(appender.result[6].v), + assert.Equal(t, 17, len(appender.result), "Appended samples not as expected") + assert.Equal(t, 42.0, appender.result[0].v, "Appended first sample not as expected") + assert.True(t, value.IsStaleNaN(appender.result[6].v), "Appended second sample not as expected. Wanted: stale NaN Got: %x", math.Float64bits(appender.result[6].v)) } @@ -1101,7 +1102,7 @@ func TestScrapeLoopCache(t *testing.T) { // 1 successfully scraped sample, 1 stale marker after first fail, 5 report samples for // each scrape successful or not. - testutil.Equals(t, 26, len(appender.result), "Appended samples not as expected") + assert.Equal(t, 26, len(appender.result), "Appended samples not as expected") } func TestScrapeLoopCacheMemoryExhaustionProtection(t *testing.T) { @@ -1239,8 +1240,8 @@ func TestScrapeLoopAppend(t *testing.T) { slApp := sl.appender(context.Background()) _, _, _, err := sl.append(slApp, []byte(test.scrapeLabels), "", now) - testutil.Ok(t, err) - testutil.Ok(t, slApp.Commit()) + assert.NoError(t, err) + assert.NoError(t, slApp.Commit()) expected := []sample{ { @@ -1258,7 +1259,7 @@ func TestScrapeLoopAppend(t *testing.T) { } t.Logf("Test:%s", test.title) - testutil.Equals(t, expected, app.result) + assert.Equal(t, expected, app.result) } } @@ -1292,8 +1293,8 @@ func TestScrapeLoopAppendCacheEntryButErrNotFound(t *testing.T) { slApp := sl.appender(context.Background()) _, _, _, err := sl.append(slApp, []byte(metric), "", now) - testutil.Ok(t, err) - testutil.Ok(t, slApp.Commit()) + assert.NoError(t, err) + assert.NoError(t, slApp.Commit()) expected := []sample{ { @@ -1303,7 +1304,7 @@ func TestScrapeLoopAppendCacheEntryButErrNotFound(t *testing.T) { }, } - testutil.Equals(t, expected, app.result) + assert.Equal(t, expected, app.result) } func TestScrapeLoopAppendSampleLimit(t *testing.T) { @@ -1328,7 +1329,7 @@ func TestScrapeLoopAppendSampleLimit(t *testing.T) { // Get the value of the Counter before performing the append. beforeMetric := dto.Metric{} err := targetScrapeSampleLimit.Write(&beforeMetric) - testutil.Ok(t, err) + assert.NoError(t, err) beforeMetricValue := beforeMetric.GetCounter().GetValue() @@ -1338,20 +1339,20 @@ func TestScrapeLoopAppendSampleLimit(t *testing.T) { if err != errSampleLimit { t.Fatalf("Did not see expected sample limit error: %s", err) } - testutil.Ok(t, slApp.Rollback()) - testutil.Equals(t, 3, total) - testutil.Equals(t, 3, added) - testutil.Equals(t, 1, seriesAdded) + assert.NoError(t, slApp.Rollback()) + assert.Equal(t, 3, total) + assert.Equal(t, 3, added) + assert.Equal(t, 1, seriesAdded) // Check that the Counter has been incremented a single time for the scrape, // not multiple times for each sample. metric := dto.Metric{} err = targetScrapeSampleLimit.Write(&metric) - testutil.Ok(t, err) + assert.NoError(t, err) value := metric.GetCounter().GetValue() change := value - beforeMetricValue - testutil.Assert(t, change == 1, "Unexpected change of sample limit metric: %f", change) + assert.True(t, change == 1, "Unexpected change of sample limit metric: %f", change) // And verify that we got the samples that fit under the limit. want := []sample{ @@ -1361,7 +1362,7 @@ func TestScrapeLoopAppendSampleLimit(t *testing.T) { v: 1, }, } - testutil.Equals(t, want, resApp.rolledbackResult, "Appended samples not as expected") + assert.Equal(t, want, resApp.rolledbackResult, "Appended samples not as expected") now = time.Now() slApp = sl.appender(context.Background()) @@ -1369,10 +1370,10 @@ func TestScrapeLoopAppendSampleLimit(t *testing.T) { if err != errSampleLimit { t.Fatalf("Did not see expected sample limit error: %s", err) } - testutil.Ok(t, slApp.Rollback()) - testutil.Equals(t, 9, total) - testutil.Equals(t, 6, added) - testutil.Equals(t, 0, seriesAdded) + assert.NoError(t, slApp.Rollback()) + assert.Equal(t, 9, total) + assert.Equal(t, 6, added) + assert.Equal(t, 0, seriesAdded) } func TestScrapeLoop_ChangingMetricString(t *testing.T) { @@ -1397,13 +1398,13 @@ func TestScrapeLoop_ChangingMetricString(t *testing.T) { now := time.Now() slApp := sl.appender(context.Background()) _, _, _, err := sl.append(slApp, []byte(`metric_a{a="1",b="1"} 1`), "", now) - testutil.Ok(t, err) - testutil.Ok(t, slApp.Commit()) + assert.NoError(t, err) + assert.NoError(t, slApp.Commit()) slApp = sl.appender(context.Background()) _, _, _, err = sl.append(slApp, []byte(`metric_a{b="1",a="1"} 2`), "", now.Add(time.Minute)) - testutil.Ok(t, err) - testutil.Ok(t, slApp.Commit()) + assert.NoError(t, err) + assert.NoError(t, slApp.Commit()) // DeepEqual will report NaNs as being different, so replace with a different value. want := []sample{ @@ -1418,7 +1419,7 @@ func TestScrapeLoop_ChangingMetricString(t *testing.T) { v: 2, }, } - testutil.Equals(t, want, capp.result, "Appended samples not as expected") + assert.Equal(t, want, capp.result, "Appended samples not as expected") } func TestScrapeLoopAppendStaleness(t *testing.T) { @@ -1437,16 +1438,16 @@ func TestScrapeLoopAppendStaleness(t *testing.T) { now := time.Now() slApp := sl.appender(context.Background()) _, _, _, err := sl.append(slApp, []byte("metric_a 1\n"), "", now) - testutil.Ok(t, err) - testutil.Ok(t, slApp.Commit()) + assert.NoError(t, err) + assert.NoError(t, slApp.Commit()) slApp = sl.appender(context.Background()) _, _, _, err = sl.append(slApp, []byte(""), "", now.Add(time.Second)) - testutil.Ok(t, err) - testutil.Ok(t, slApp.Commit()) + assert.NoError(t, err) + assert.NoError(t, slApp.Commit()) ingestedNaN := math.Float64bits(app.result[1].v) - testutil.Equals(t, value.StaleNaN, ingestedNaN, "Appended stale sample wasn't as expected") + assert.Equal(t, value.StaleNaN, ingestedNaN, "Appended stale sample wasn't as expected") // DeepEqual will report NaNs as being different, so replace with a different value. app.result[1].v = 42 @@ -1462,7 +1463,7 @@ func TestScrapeLoopAppendStaleness(t *testing.T) { v: 42, }, } - testutil.Equals(t, want, app.result, "Appended samples not as expected") + assert.Equal(t, want, app.result, "Appended samples not as expected") } func TestScrapeLoopAppendNoStalenessIfTimestamp(t *testing.T) { @@ -1480,13 +1481,13 @@ func TestScrapeLoopAppendNoStalenessIfTimestamp(t *testing.T) { now := time.Now() slApp := sl.appender(context.Background()) _, _, _, err := sl.append(slApp, []byte("metric_a 1 1000\n"), "", now) - testutil.Ok(t, err) - testutil.Ok(t, slApp.Commit()) + assert.NoError(t, err) + assert.NoError(t, slApp.Commit()) slApp = sl.appender(context.Background()) _, _, _, err = sl.append(slApp, []byte(""), "", now.Add(time.Second)) - testutil.Ok(t, err) - testutil.Ok(t, slApp.Commit()) + assert.NoError(t, err) + assert.NoError(t, slApp.Commit()) want := []sample{ { @@ -1495,7 +1496,7 @@ func TestScrapeLoopAppendNoStalenessIfTimestamp(t *testing.T) { v: 1, }, } - testutil.Equals(t, want, app.result, "Appended samples not as expected") + assert.Equal(t, want, app.result, "Appended samples not as expected") } func TestScrapeLoopRunReportsTargetDownOnScrapeError(t *testing.T) { @@ -1523,7 +1524,7 @@ func TestScrapeLoopRunReportsTargetDownOnScrapeError(t *testing.T) { } sl.run(10*time.Millisecond, time.Hour, nil) - testutil.Equals(t, 0.0, appender.result[0].v, "bad 'up' value") + assert.Equal(t, 0.0, appender.result[0].v, "bad 'up' value") } func TestScrapeLoopRunReportsTargetDownOnInvalidUTF8(t *testing.T) { @@ -1552,7 +1553,7 @@ func TestScrapeLoopRunReportsTargetDownOnInvalidUTF8(t *testing.T) { } sl.run(10*time.Millisecond, time.Hour, nil) - testutil.Equals(t, 0.0, appender.result[0].v, "bad 'up' value") + assert.Equal(t, 0.0, appender.result[0].v, "bad 'up' value") } type errorAppender struct { @@ -1593,8 +1594,8 @@ func TestScrapeLoopAppendGracefullyIfAmendOrOutOfOrderOrOutOfBounds(t *testing.T now := time.Unix(1, 0) slApp := sl.appender(context.Background()) total, added, seriesAdded, err := sl.append(slApp, []byte("out_of_order 1\namend 1\nnormal 1\nout_of_bounds 1\n"), "", now) - testutil.Ok(t, err) - testutil.Ok(t, slApp.Commit()) + assert.NoError(t, err) + assert.NoError(t, slApp.Commit()) want := []sample{ { @@ -1603,10 +1604,10 @@ func TestScrapeLoopAppendGracefullyIfAmendOrOutOfOrderOrOutOfBounds(t *testing.T v: 1, }, } - testutil.Equals(t, want, app.result, "Appended samples not as expected") - testutil.Equals(t, 4, total) - testutil.Equals(t, 4, added) - testutil.Equals(t, 1, seriesAdded) + assert.Equal(t, want, app.result, "Appended samples not as expected") + assert.Equal(t, 4, total) + assert.Equal(t, 4, added) + assert.Equal(t, 1, seriesAdded) } func TestScrapeLoopOutOfBoundsTimeError(t *testing.T) { @@ -1630,11 +1631,11 @@ func TestScrapeLoopOutOfBoundsTimeError(t *testing.T) { now := time.Now().Add(20 * time.Minute) slApp := sl.appender(context.Background()) total, added, seriesAdded, err := sl.append(slApp, []byte("normal 1\n"), "", now) - testutil.Ok(t, err) - testutil.Ok(t, slApp.Commit()) - testutil.Equals(t, 1, total) - testutil.Equals(t, 1, added) - testutil.Equals(t, 0, seriesAdded) + assert.NoError(t, err) + assert.NoError(t, slApp.Commit()) + assert.Equal(t, 1, total) + assert.Equal(t, 1, added) + assert.Equal(t, 0, seriesAdded) } @@ -1680,9 +1681,9 @@ func TestTargetScraperScrapeOK(t *testing.T) { var buf bytes.Buffer contentType, err := ts.scrape(context.Background(), &buf) - testutil.Ok(t, err) - testutil.Equals(t, "text/plain; version=0.0.4", contentType) - testutil.Equals(t, "metric_a 1\nmetric_b 2\n", buf.String()) + assert.NoError(t, err) + assert.Equal(t, "text/plain; version=0.0.4", contentType) + assert.Equal(t, "metric_a 1\nmetric_b 2\n", buf.String()) } func TestTargetScrapeScrapeCancel(t *testing.T) { @@ -1733,7 +1734,7 @@ func TestTargetScrapeScrapeCancel(t *testing.T) { case <-time.After(5 * time.Second): t.Fatalf("Scrape function did not return unexpectedly") case err := <-errc: - testutil.Ok(t, err) + assert.NoError(t, err) } // If this is closed in a defer above the function the test server // doesn't terminate and the test doesn't complete. @@ -1764,7 +1765,7 @@ func TestTargetScrapeScrapeNotFound(t *testing.T) { } _, err = ts.scrape(context.Background(), ioutil.Discard) - testutil.Assert(t, strings.Contains(err.Error(), "404"), "Expected \"404 NotFound\" error but got: %s", err) + assert.True(t, strings.Contains(err.Error(), "404"), "Expected \"404 NotFound\" error but got: %s", err) } // testScraper implements the scraper interface and allows setting values @@ -1817,8 +1818,8 @@ func TestScrapeLoop_RespectTimestamps(t *testing.T) { now := time.Now() slApp := sl.appender(context.Background()) _, _, _, err := sl.append(slApp, []byte(`metric_a{a="1",b="1"} 1 0`), "", now) - testutil.Ok(t, err) - testutil.Ok(t, slApp.Commit()) + assert.NoError(t, err) + assert.NoError(t, slApp.Commit()) want := []sample{ { @@ -1827,7 +1828,7 @@ func TestScrapeLoop_RespectTimestamps(t *testing.T) { v: 1, }, } - testutil.Equals(t, want, capp.result, "Appended samples not as expected") + assert.Equal(t, want, capp.result, "Appended samples not as expected") } func TestScrapeLoop_DiscardTimestamps(t *testing.T) { @@ -1850,8 +1851,8 @@ func TestScrapeLoop_DiscardTimestamps(t *testing.T) { now := time.Now() slApp := sl.appender(context.Background()) _, _, _, err := sl.append(slApp, []byte(`metric_a{a="1",b="1"} 1 0`), "", now) - testutil.Ok(t, err) - testutil.Ok(t, slApp.Commit()) + assert.NoError(t, err) + assert.NoError(t, slApp.Commit()) want := []sample{ { @@ -1860,7 +1861,7 @@ func TestScrapeLoop_DiscardTimestamps(t *testing.T) { v: 1, }, } - testutil.Equals(t, want, capp.result, "Appended samples not as expected") + assert.Equal(t, want, capp.result, "Appended samples not as expected") } func TestScrapeLoopDiscardDuplicateLabels(t *testing.T) { @@ -1883,27 +1884,27 @@ func TestScrapeLoopDiscardDuplicateLabels(t *testing.T) { // We add a good and a bad metric to check that both are discarded. slApp := sl.appender(ctx) _, _, _, err := sl.append(slApp, []byte("test_metric{le=\"500\"} 1\ntest_metric{le=\"600\",le=\"700\"} 1\n"), "", time.Time{}) - testutil.NotOk(t, err) - testutil.Ok(t, slApp.Rollback()) + assert.Error(t, err) + assert.NoError(t, slApp.Rollback()) q, err := s.Querier(ctx, time.Time{}.UnixNano(), 0) - testutil.Ok(t, err) + assert.NoError(t, err) series := q.Select(false, nil, labels.MustNewMatcher(labels.MatchRegexp, "__name__", ".*")) - testutil.Equals(t, false, series.Next(), "series found in tsdb") - testutil.Ok(t, series.Err()) + assert.Equal(t, false, series.Next(), "series found in tsdb") + assert.NoError(t, series.Err()) // We add a good metric to check that it is recorded. slApp = sl.appender(ctx) _, _, _, err = sl.append(slApp, []byte("test_metric{le=\"500\"} 1\n"), "", time.Time{}) - testutil.Ok(t, err) - testutil.Ok(t, slApp.Commit()) + assert.NoError(t, err) + assert.NoError(t, slApp.Commit()) q, err = s.Querier(ctx, time.Time{}.UnixNano(), 0) - testutil.Ok(t, err) + assert.NoError(t, err) series = q.Select(false, nil, labels.MustNewMatcher(labels.MatchEqual, "le", "500")) - testutil.Equals(t, true, series.Next(), "series not found in tsdb") - testutil.Ok(t, series.Err()) - testutil.Equals(t, false, series.Next(), "more than one series found in tsdb") + assert.Equal(t, true, series.Next(), "series not found in tsdb") + assert.NoError(t, series.Err()) + assert.Equal(t, false, series.Next(), "more than one series found in tsdb") } func TestScrapeLoopDiscardUnnamedMetrics(t *testing.T) { @@ -1932,15 +1933,15 @@ func TestScrapeLoopDiscardUnnamedMetrics(t *testing.T) { slApp := sl.appender(context.Background()) _, _, _, err := sl.append(slApp, []byte("nok 1\nnok2{drop=\"drop\"} 1\n"), "", time.Time{}) - testutil.NotOk(t, err) - testutil.Ok(t, slApp.Rollback()) - testutil.Equals(t, errNameLabelMandatory, err) + assert.Error(t, err) + assert.NoError(t, slApp.Rollback()) + assert.Equal(t, errNameLabelMandatory, err) q, err := s.Querier(ctx, time.Time{}.UnixNano(), 0) - testutil.Ok(t, err) + assert.NoError(t, err) series := q.Select(false, nil, labels.MustNewMatcher(labels.MatchRegexp, "__name__", ".*")) - testutil.Equals(t, false, series.Next(), "series found in tsdb") - testutil.Ok(t, series.Err()) + assert.Equal(t, false, series.Next(), "series found in tsdb") + assert.NoError(t, series.Err()) } func TestReusableConfig(t *testing.T) { @@ -1999,14 +2000,14 @@ func TestReusableConfig(t *testing.T) { } for i, m := range match { - testutil.Equals(t, true, reusableCache(variants[m[0]], variants[m[1]]), "match test %d", i) - testutil.Equals(t, true, reusableCache(variants[m[1]], variants[m[0]]), "match test %d", i) - testutil.Equals(t, true, reusableCache(variants[m[1]], variants[m[1]]), "match test %d", i) - testutil.Equals(t, true, reusableCache(variants[m[0]], variants[m[0]]), "match test %d", i) + assert.Equal(t, true, reusableCache(variants[m[0]], variants[m[1]]), "match test %d", i) + assert.Equal(t, true, reusableCache(variants[m[1]], variants[m[0]]), "match test %d", i) + assert.Equal(t, true, reusableCache(variants[m[1]], variants[m[1]]), "match test %d", i) + assert.Equal(t, true, reusableCache(variants[m[0]], variants[m[0]]), "match test %d", i) } for i, m := range noMatch { - testutil.Equals(t, false, reusableCache(variants[m[0]], variants[m[1]]), "not match test %d", i) - testutil.Equals(t, false, reusableCache(variants[m[1]], variants[m[0]]), "not match test %d", i) + assert.Equal(t, false, reusableCache(variants[m[0]], variants[m[1]]), "not match test %d", i) + assert.Equal(t, false, reusableCache(variants[m[1]], variants[m[0]]), "not match test %d", i) } } @@ -2117,15 +2118,15 @@ func TestReuseScrapeCache(t *testing.T) { sp.reload(s.newConfig) for fp, newCacheAddr := range cacheAddr(sp) { if s.keep { - testutil.Assert(t, initCacheAddr[fp] == newCacheAddr, "step %d: old cache and new cache are not the same", i) + assert.True(t, initCacheAddr[fp] == newCacheAddr, "step %d: old cache and new cache are not the same", i) } else { - testutil.Assert(t, initCacheAddr[fp] != newCacheAddr, "step %d: old cache and new cache are the same", i) + assert.True(t, initCacheAddr[fp] != newCacheAddr, "step %d: old cache and new cache are the same", i) } } initCacheAddr = cacheAddr(sp) sp.reload(s.newConfig) for fp, newCacheAddr := range cacheAddr(sp) { - testutil.Assert(t, initCacheAddr[fp] == newCacheAddr, "step %d: reloading the exact config invalidates the cache", i) + assert.True(t, initCacheAddr[fp] == newCacheAddr, "step %d: reloading the exact config invalidates the cache", i) } } } @@ -2149,8 +2150,8 @@ func TestScrapeAddFast(t *testing.T) { slApp := sl.appender(ctx) _, _, _, err := sl.append(slApp, []byte("up 1\n"), "", time.Time{}) - testutil.Ok(t, err) - testutil.Ok(t, slApp.Commit()) + assert.NoError(t, err) + assert.NoError(t, slApp.Commit()) // Poison the cache. There is just one entry, and one series in the // storage. Changing the ref will create a 'not found' error. @@ -2160,8 +2161,8 @@ func TestScrapeAddFast(t *testing.T) { slApp = sl.appender(ctx) _, _, _, err = sl.append(slApp, []byte("up 1\n"), "", time.Time{}.Add(time.Second)) - testutil.Ok(t, err) - testutil.Ok(t, slApp.Commit()) + assert.NoError(t, err) + assert.NoError(t, slApp.Commit()) } func TestReuseCacheRace(t *testing.T) { @@ -2205,7 +2206,7 @@ func TestCheckAddError(t *testing.T) { var appErrs appendErrors sl := scrapeLoop{l: log.NewNopLogger()} sl.checkAddError(nil, nil, nil, storage.ErrOutOfOrderSample, nil, &appErrs) - testutil.Equals(t, 1, appErrs.numOutOfOrder) + assert.Equal(t, 1, appErrs.numOutOfOrder) } func TestScrapeReportSingleAppender(t *testing.T) { @@ -2248,7 +2249,7 @@ func TestScrapeReportSingleAppender(t *testing.T) { start := time.Now() for time.Since(start) < 3*time.Second { q, err := s.Querier(ctx, time.Time{}.UnixNano(), time.Now().UnixNano()) - testutil.Ok(t, err) + assert.NoError(t, err) series := q.Select(false, nil, labels.MustNewMatcher(labels.MatchRegexp, "__name__", ".+")) c := 0 @@ -2259,7 +2260,7 @@ func TestScrapeReportSingleAppender(t *testing.T) { } } - testutil.Equals(t, 0, c%9, "Appended samples not as expected: %d", c) + assert.Equal(t, 0, c%9, "Appended samples not as expected: %d", c) q.Close() } cancel() diff --git a/scrape/target_test.go b/scrape/target_test.go index fe5f5871bd..08350f94c0 100644 --- a/scrape/target_test.go +++ b/scrape/target_test.go @@ -21,14 +21,14 @@ import ( "net/http" "net/http/httptest" "net/url" - "reflect" "strings" "testing" "time" - "github.com/prometheus/common/model" - config_util "github.com/prometheus/common/config" + "github.com/prometheus/common/model" + "github.com/stretchr/testify/assert" + "github.com/prometheus/prometheus/pkg/labels" ) @@ -40,9 +40,7 @@ func TestTargetLabels(t *testing.T) { target := newTestTarget("example.com:80", 0, labels.FromStrings("job", "some_job", "foo", "bar")) want := labels.FromStrings(model.JobLabel, "some_job", "foo", "bar") got := target.Labels() - if !reflect.DeepEqual(want, got) { - t.Errorf("want base labels %v, got %v", want, got) - } + assert.Equal(t, want, got) } func TestTargetOffset(t *testing.T) { @@ -113,16 +111,14 @@ func TestTargetURL(t *testing.T) { "cde": []string{"huu"}, "xyz": []string{"hoo"}, } - expectedURL := url.URL{ + expectedURL := &url.URL{ Scheme: "https", Host: "example.com:1234", Path: "/metricz", RawQuery: expectedParams.Encode(), } - if u := target.URL(); !reflect.DeepEqual(u.String(), expectedURL.String()) { - t.Fatalf("Expected URL %q, but got %q", expectedURL.String(), u.String()) - } + assert.Equal(t, expectedURL, target.URL()) } func newTestTarget(targetURL string, deadline time.Duration, lbls labels.Labels) *Target { diff --git a/storage/buffer_test.go b/storage/buffer_test.go index 58e71acd03..76501052aa 100644 --- a/storage/buffer_test.go +++ b/storage/buffer_test.go @@ -17,7 +17,7 @@ import ( "math/rand" "testing" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" ) func TestSampleRing(t *testing.T) { @@ -77,9 +77,9 @@ func TestSampleRing(t *testing.T) { } if found { - testutil.Assert(t, sold.t >= s.t-c.delta, "%d: unexpected sample %d in buffer; buffer %v", i, sold.t, buffered) + assert.True(t, sold.t >= s.t-c.delta, "%d: unexpected sample %d in buffer; buffer %v", i, sold.t, buffered) } else { - testutil.Assert(t, sold.t < s.t-c.delta, "%d: expected sample %d to be in buffer but was not; buffer %v", i, sold.t, buffered) + assert.True(t, sold.t < s.t-c.delta, "%d: expected sample %d to be in buffer but was not; buffer %v", i, sold.t, buffered) } } } @@ -96,12 +96,12 @@ func TestBufferedSeriesIterator(t *testing.T) { t, v := bit.At() b = append(b, sample{t: t, v: v}) } - testutil.Equals(t, exp, b, "buffer mismatch") + assert.Equal(t, exp, b, "buffer mismatch") } sampleEq := func(ets int64, ev float64) { ts, v := it.Values() - testutil.Equals(t, ets, ts, "timestamp mismatch") - testutil.Equals(t, ev, v, "value mismatch") + assert.Equal(t, ets, ts, "timestamp mismatch") + assert.Equal(t, ev, v, "value mismatch") } it = NewBufferIterator(NewListSeriesIterator(samples{ @@ -115,29 +115,29 @@ func TestBufferedSeriesIterator(t *testing.T) { sample{t: 101, v: 10}, }), 2) - testutil.Assert(t, it.Seek(-123), "seek failed") + assert.True(t, it.Seek(-123), "seek failed") sampleEq(1, 2) bufferEq(nil) - testutil.Assert(t, it.Next(), "next failed") + assert.True(t, it.Next(), "next failed") sampleEq(2, 3) bufferEq([]sample{{t: 1, v: 2}}) - testutil.Assert(t, it.Next(), "next failed") - testutil.Assert(t, it.Next(), "next failed") - testutil.Assert(t, it.Next(), "next failed") + assert.True(t, it.Next(), "next failed") + assert.True(t, it.Next(), "next failed") + assert.True(t, it.Next(), "next failed") sampleEq(5, 6) bufferEq([]sample{{t: 2, v: 3}, {t: 3, v: 4}, {t: 4, v: 5}}) - testutil.Assert(t, it.Seek(5), "seek failed") + assert.True(t, it.Seek(5), "seek failed") sampleEq(5, 6) bufferEq([]sample{{t: 2, v: 3}, {t: 3, v: 4}, {t: 4, v: 5}}) - testutil.Assert(t, it.Seek(101), "seek failed") + assert.True(t, it.Seek(101), "seek failed") sampleEq(101, 10) bufferEq([]sample{{t: 99, v: 8}, {t: 100, v: 9}}) - testutil.Assert(t, !it.Next(), "next succeeded unexpectedly") + assert.True(t, !it.Next(), "next succeeded unexpectedly") } // At() should not be called once Next() returns false. @@ -147,7 +147,7 @@ func TestBufferedSeriesIteratorNoBadAt(t *testing.T) { m := &mockSeriesIterator{ seek: func(int64) bool { return false }, at: func() (int64, float64) { - testutil.Assert(t, !done, "unexpectedly done") + assert.True(t, !done, "unexpectedly done") done = true return 0, 0 }, @@ -171,7 +171,7 @@ func BenchmarkBufferedSeriesIterator(b *testing.B) { for it.Next() { // scan everything } - testutil.Ok(b, it.Err()) + assert.NoError(b, it.Err()) } type mockSeriesIterator struct { diff --git a/storage/fanout.go b/storage/fanout.go index 62b2ef54ae..82e65e91b3 100644 --- a/storage/fanout.go +++ b/storage/fanout.go @@ -19,6 +19,7 @@ import ( "github.com/go-kit/kit/log" "github.com/go-kit/kit/log/level" "github.com/prometheus/common/model" + "github.com/prometheus/prometheus/pkg/labels" tsdb_errors "github.com/prometheus/prometheus/tsdb/errors" ) diff --git a/storage/fanout_test.go b/storage/fanout_test.go index fcd63db9a3..14519aef37 100644 --- a/storage/fanout_test.go +++ b/storage/fanout_test.go @@ -19,11 +19,11 @@ import ( "github.com/pkg/errors" "github.com/prometheus/common/model" + "github.com/stretchr/testify/assert" "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/storage" "github.com/prometheus/prometheus/util/teststorage" - "github.com/prometheus/prometheus/util/testutil" ) func TestFanout_SelectSorted(t *testing.T) { @@ -43,7 +43,7 @@ func TestFanout_SelectSorted(t *testing.T) { app1.Add(inputLabel, 2000, 2) inputTotalSize++ err := app1.Commit() - testutil.Ok(t, err) + assert.NoError(t, err) remoteStorage1 := teststorage.New(t) defer remoteStorage1.Close() @@ -55,7 +55,7 @@ func TestFanout_SelectSorted(t *testing.T) { app2.Add(inputLabel, 5000, 5) inputTotalSize++ err = app2.Commit() - testutil.Ok(t, err) + assert.NoError(t, err) remoteStorage2 := teststorage.New(t) defer remoteStorage2.Close() @@ -69,17 +69,17 @@ func TestFanout_SelectSorted(t *testing.T) { inputTotalSize++ err = app3.Commit() - testutil.Ok(t, err) + assert.NoError(t, err) fanoutStorage := storage.NewFanout(nil, priStorage, remoteStorage1, remoteStorage2) t.Run("querier", func(t *testing.T) { querier, err := fanoutStorage.Querier(context.Background(), 0, 8000) - testutil.Ok(t, err) + assert.NoError(t, err) defer querier.Close() matcher, err := labels.NewMatcher(labels.MatchEqual, model.MetricNameLabel, "a") - testutil.Ok(t, err) + assert.NoError(t, err) seriesSet := querier.Select(true, nil, matcher) @@ -96,16 +96,16 @@ func TestFanout_SelectSorted(t *testing.T) { } } - testutil.Equals(t, labelsResult, outputLabel) - testutil.Equals(t, inputTotalSize, len(result)) + assert.Equal(t, labelsResult, outputLabel) + assert.Equal(t, inputTotalSize, len(result)) }) t.Run("chunk querier", func(t *testing.T) { querier, err := fanoutStorage.ChunkQuerier(ctx, 0, 8000) - testutil.Ok(t, err) + assert.NoError(t, err) defer querier.Close() matcher, err := labels.NewMatcher(labels.MatchEqual, model.MetricNameLabel, "a") - testutil.Ok(t, err) + assert.NoError(t, err) seriesSet := storage.NewSeriesSetFromChunkSeriesSet(querier.Select(true, nil, matcher)) @@ -122,9 +122,9 @@ func TestFanout_SelectSorted(t *testing.T) { } } - testutil.Ok(t, seriesSet.Err()) - testutil.Equals(t, labelsResult, outputLabel) - testutil.Equals(t, inputTotalSize, len(result)) + assert.NoError(t, seriesSet.Err()) + assert.Equal(t, labelsResult, outputLabel) + assert.Equal(t, inputTotalSize, len(result)) }) } @@ -157,7 +157,7 @@ func TestFanoutErrors(t *testing.T) { t.Run("samples", func(t *testing.T) { querier, err := fanoutStorage.Querier(context.Background(), 0, 8000) - testutil.Ok(t, err) + assert.NoError(t, err) defer querier.Close() matcher := labels.MustNewMatcher(labels.MatchEqual, "a", "b") @@ -169,20 +169,20 @@ func TestFanoutErrors(t *testing.T) { } if tc.err != nil { - testutil.NotOk(t, ss.Err()) - testutil.Equals(t, tc.err.Error(), ss.Err().Error()) + assert.Error(t, ss.Err()) + assert.Equal(t, tc.err.Error(), ss.Err().Error()) } if tc.warning != nil { - testutil.Assert(t, len(ss.Warnings()) > 0, "warnings expected") - testutil.NotOk(t, ss.Warnings()[0]) - testutil.Equals(t, tc.warning.Error(), ss.Warnings()[0].Error()) + assert.True(t, len(ss.Warnings()) > 0, "warnings expected") + assert.Error(t, ss.Warnings()[0]) + assert.Equal(t, tc.warning.Error(), ss.Warnings()[0].Error()) } }) t.Run("chunks", func(t *testing.T) { t.Skip("enable once TestStorage and TSDB implements ChunkQuerier") querier, err := fanoutStorage.ChunkQuerier(context.Background(), 0, 8000) - testutil.Ok(t, err) + assert.NoError(t, err) defer querier.Close() matcher := labels.MustNewMatcher(labels.MatchEqual, "a", "b") @@ -194,14 +194,14 @@ func TestFanoutErrors(t *testing.T) { } if tc.err != nil { - testutil.NotOk(t, ss.Err()) - testutil.Equals(t, tc.err.Error(), ss.Err().Error()) + assert.Error(t, ss.Err()) + assert.Equal(t, tc.err.Error(), ss.Err().Error()) } if tc.warning != nil { - testutil.Assert(t, len(ss.Warnings()) > 0, "warnings expected") - testutil.NotOk(t, ss.Warnings()[0]) - testutil.Equals(t, tc.warning.Error(), ss.Warnings()[0].Error()) + assert.True(t, len(ss.Warnings()) > 0, "warnings expected") + assert.Error(t, ss.Warnings()[0]) + assert.Equal(t, tc.warning.Error(), ss.Warnings()[0].Error()) } }) } diff --git a/storage/merge.go b/storage/merge.go index e3026cf5ee..1670a119a1 100644 --- a/storage/merge.go +++ b/storage/merge.go @@ -22,6 +22,7 @@ import ( "sync" "github.com/pkg/errors" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/tsdb/chunkenc" "github.com/prometheus/prometheus/tsdb/chunks" diff --git a/storage/merge_test.go b/storage/merge_test.go index 2cad88781f..55d1ae6236 100644 --- a/storage/merge_test.go +++ b/storage/merge_test.go @@ -21,11 +21,11 @@ import ( "testing" "github.com/pkg/errors" + "github.com/stretchr/testify/assert" "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/tsdb/chunkenc" "github.com/prometheus/prometheus/tsdb/tsdbutil" - "github.com/prometheus/prometheus/util/testutil" ) func TestMergeQuerierWithChainMerger(t *testing.T) { @@ -194,19 +194,19 @@ func TestMergeQuerierWithChainMerger(t *testing.T) { for mergedQuerier.Next() { mergedSeries = append(mergedSeries, mergedQuerier.At()) } - testutil.Ok(t, mergedQuerier.Err()) + assert.NoError(t, mergedQuerier.Err()) for _, actualSeries := range mergedSeries { - testutil.Assert(t, tc.expected.Next(), "Expected Next() to be true") + assert.True(t, tc.expected.Next(), "Expected Next() to be true") expectedSeries := tc.expected.At() - testutil.Equals(t, expectedSeries.Labels(), actualSeries.Labels()) + assert.Equal(t, expectedSeries.Labels(), actualSeries.Labels()) expSmpl, expErr := ExpandSamples(expectedSeries.Iterator(), nil) actSmpl, actErr := ExpandSamples(actualSeries.Iterator(), nil) - testutil.Equals(t, expErr, actErr) - testutil.Equals(t, expSmpl, actSmpl) + assert.Equal(t, expErr, actErr) + assert.Equal(t, expSmpl, actSmpl) } - testutil.Assert(t, !tc.expected.Next(), "Expected Next() to be false") + assert.True(t, !tc.expected.Next(), "Expected Next() to be false") }) } } @@ -364,19 +364,19 @@ func TestMergeChunkQuerierWithNoVerticalChunkSeriesMerger(t *testing.T) { merged := NewMergeChunkQuerier([]ChunkQuerier{p}, qs, NewCompactingChunkSeriesMerger(nil)).Select(false, nil) for merged.Next() { - testutil.Assert(t, tc.expected.Next(), "Expected Next() to be true") + assert.True(t, tc.expected.Next(), "Expected Next() to be true") actualSeries := merged.At() expectedSeries := tc.expected.At() - testutil.Equals(t, expectedSeries.Labels(), actualSeries.Labels()) + assert.Equal(t, expectedSeries.Labels(), actualSeries.Labels()) expChks, expErr := ExpandChunks(expectedSeries.Iterator()) actChks, actErr := ExpandChunks(actualSeries.Iterator()) - testutil.Equals(t, expErr, actErr) - testutil.Equals(t, expChks, actChks) + assert.Equal(t, expErr, actErr) + assert.Equal(t, expChks, actChks) } - testutil.Ok(t, merged.Err()) - testutil.Assert(t, !tc.expected.Next(), "Expected Next() to be false") + assert.NoError(t, merged.Err()) + assert.True(t, !tc.expected.Next(), "Expected Next() to be false") }) } } @@ -468,12 +468,12 @@ func TestCompactingChunkSeriesMerger(t *testing.T) { } { t.Run(tc.name, func(t *testing.T) { merged := m(tc.input...) - testutil.Equals(t, tc.expected.Labels(), merged.Labels()) + assert.Equal(t, tc.expected.Labels(), merged.Labels()) actChks, actErr := ExpandChunks(merged.Iterator()) expChks, expErr := ExpandChunks(tc.expected.Iterator()) - testutil.Equals(t, expErr, actErr) - testutil.Equals(t, expChks, actChks) + assert.Equal(t, expErr, actErr) + assert.Equal(t, expChks, actChks) }) } } @@ -612,8 +612,8 @@ func TestChainSampleIterator(t *testing.T) { } { merged := newChainSampleIterator(tc.input) actual, err := ExpandSamples(merged, nil) - testutil.Ok(t, err) - testutil.Equals(t, tc.expected, actual) + assert.NoError(t, err) + assert.Equal(t, tc.expected, actual) } } @@ -655,9 +655,9 @@ func TestChainSampleIteratorSeek(t *testing.T) { actual = append(actual, sample{t, v}) } s, err := ExpandSamples(merged, nil) - testutil.Ok(t, err) + assert.NoError(t, err) actual = append(actual, s...) - testutil.Equals(t, tc.expected, actual) + assert.Equal(t, tc.expected, actual) } } @@ -689,7 +689,7 @@ func benchmarkDrain(seriesSet SeriesSet, b *testing.B) { for n := 0; n < b.N; n++ { for seriesSet.Next() { result, err = ExpandSamples(seriesSet.At().Iterator(), nil) - testutil.Ok(b, err) + assert.NoError(b, err) } } } @@ -789,9 +789,9 @@ func unwrapMockGenericQuerier(t *testing.T, qr genericQuerier) *mockGenericQueri m, ok := qr.(*mockGenericQuerier) if !ok { s, ok := qr.(*secondaryQuerier) - testutil.Assert(t, ok, "expected secondaryQuerier got something else") + assert.True(t, ok, "expected secondaryQuerier got something else") m, ok = s.genericQuerier.(*mockGenericQuerier) - testutil.Assert(t, ok, "expected mockGenericQuerier got something else") + assert.True(t, ok, "expected mockGenericQuerier got something else") } return m } @@ -922,10 +922,10 @@ func TestMergeGenericQuerierWithSecondaries_ErrorHandling(t *testing.T) { for res.Next() { lbls = append(lbls, res.At().Labels()) } - testutil.Equals(t, tcase.expectedWarnings[0], res.Warnings()) - testutil.Equals(t, tcase.expectedErrs[0], res.Err()) - testutil.Assert(t, errors.Is(res.Err(), tcase.expectedErrs[0]), "expected error doesn't match") - testutil.Equals(t, tcase.expectedSelectsSeries, lbls) + assert.Equal(t, tcase.expectedWarnings[0], res.Warnings()) + assert.Equal(t, tcase.expectedErrs[0], res.Err()) + assert.True(t, errors.Is(res.Err(), tcase.expectedErrs[0]), "expected error doesn't match") + assert.Equal(t, tcase.expectedSelectsSeries, lbls) for _, qr := range q.queriers { m := unwrapMockGenericQuerier(t, qr) @@ -934,14 +934,14 @@ func TestMergeGenericQuerierWithSecondaries_ErrorHandling(t *testing.T) { if len(q.queriers) == 1 { exp[0] = false } - testutil.Equals(t, exp, m.sortedSeriesRequested) + assert.Equal(t, exp, m.sortedSeriesRequested) } }) t.Run("LabelNames", func(t *testing.T) { res, w, err := q.LabelNames() - testutil.Equals(t, tcase.expectedWarnings[1], w) - testutil.Assert(t, errors.Is(err, tcase.expectedErrs[1]), "expected error doesn't match") - testutil.Equals(t, tcase.expectedLabels, res) + assert.Equal(t, tcase.expectedWarnings[1], w) + assert.True(t, errors.Is(err, tcase.expectedErrs[1]), "expected error doesn't match") + assert.Equal(t, tcase.expectedLabels, res) if err != nil { return @@ -949,14 +949,14 @@ func TestMergeGenericQuerierWithSecondaries_ErrorHandling(t *testing.T) { for _, qr := range q.queriers { m := unwrapMockGenericQuerier(t, qr) - testutil.Equals(t, 1, m.labelNamesCalls) + assert.Equal(t, 1, m.labelNamesCalls) } }) t.Run("LabelValues", func(t *testing.T) { res, w, err := q.LabelValues("test") - testutil.Equals(t, tcase.expectedWarnings[2], w) - testutil.Assert(t, errors.Is(err, tcase.expectedErrs[2]), "expected error doesn't match") - testutil.Equals(t, tcase.expectedLabels, res) + assert.Equal(t, tcase.expectedWarnings[2], w) + assert.True(t, errors.Is(err, tcase.expectedErrs[2]), "expected error doesn't match") + assert.Equal(t, tcase.expectedLabels, res) if err != nil { return @@ -964,7 +964,7 @@ func TestMergeGenericQuerierWithSecondaries_ErrorHandling(t *testing.T) { for _, qr := range q.queriers { m := unwrapMockGenericQuerier(t, qr) - testutil.Equals(t, []string{"test"}, m.labelNamesRequested) + assert.Equal(t, []string{"test"}, m.labelNamesRequested) } }) }) diff --git a/storage/remote/chunked_test.go b/storage/remote/chunked_test.go index df2c5b27f6..cf56ca8f73 100644 --- a/storage/remote/chunked_test.go +++ b/storage/remote/chunked_test.go @@ -17,7 +17,7 @@ import ( "io" "testing" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" ) type mockedFlusher struct { @@ -45,48 +45,48 @@ func TestChunkedReaderCanReadFromChunkedWriter(t *testing.T) { for _, msg := range msgs { n, err := w.Write(msg) - testutil.Ok(t, err) - testutil.Equals(t, len(msg), n) + assert.NoError(t, err) + assert.Equal(t, len(msg), n) } i := 0 for ; i < 4; i++ { msg, err := r.Next() - testutil.Ok(t, err) - testutil.Assert(t, i < len(msgs), "more messages then expected") - testutil.Equals(t, msgs[i], msg) + assert.NoError(t, err) + assert.True(t, i < len(msgs), "more messages then expected") + assert.Equal(t, msgs[i], msg) } // Empty byte slice is skipped. i++ msg, err := r.Next() - testutil.Ok(t, err) - testutil.Assert(t, i < len(msgs), "more messages then expected") - testutil.Equals(t, msgs[i], msg) + assert.NoError(t, err) + assert.True(t, i < len(msgs), "more messages then expected") + assert.Equal(t, msgs[i], msg) _, err = r.Next() - testutil.NotOk(t, err, "expected io.EOF") - testutil.Equals(t, io.EOF, err) + assert.Error(t, err, "expected io.EOF") + assert.Equal(t, io.EOF, err) - testutil.Equals(t, 5, f.flushed) + assert.Equal(t, 5, f.flushed) } func TestChunkedReader_Overflow(t *testing.T) { b := &bytes.Buffer{} _, err := NewChunkedWriter(b, &mockedFlusher{}).Write([]byte("twelve bytes")) - testutil.Ok(t, err) + assert.NoError(t, err) b2 := make([]byte, 12) copy(b2, b.Bytes()) ret, err := NewChunkedReader(b, 12, nil).Next() - testutil.Ok(t, err) - testutil.Equals(t, "twelve bytes", string(ret)) + assert.NoError(t, err) + assert.Equal(t, "twelve bytes", string(ret)) _, err = NewChunkedReader(bytes.NewReader(b2), 11, nil).Next() - testutil.NotOk(t, err, "expect exceed limit error") - testutil.Equals(t, "chunkedReader: message size exceeded the limit 11 bytes; got: 12 bytes", err.Error()) + assert.Error(t, err, "expect exceed limit error") + assert.Equal(t, "chunkedReader: message size exceeded the limit 11 bytes; got: 12 bytes", err.Error()) } func TestChunkedReader_CorruptedFrame(t *testing.T) { @@ -94,13 +94,13 @@ func TestChunkedReader_CorruptedFrame(t *testing.T) { w := NewChunkedWriter(b, &mockedFlusher{}) n, err := w.Write([]byte("test1")) - testutil.Ok(t, err) - testutil.Equals(t, 5, n) + assert.NoError(t, err) + assert.Equal(t, 5, n) bs := b.Bytes() bs[9] = 1 // Malform the frame by changing one byte. _, err = NewChunkedReader(bytes.NewReader(bs), 20, nil).Next() - testutil.NotOk(t, err, "expected malformed frame") - testutil.Equals(t, "chunkedReader: corrupted frame; checksum mismatch", err.Error()) + assert.Error(t, err, "expected malformed frame") + assert.Equal(t, "chunkedReader: corrupted frame; checksum mismatch", err.Error()) } diff --git a/storage/remote/client.go b/storage/remote/client.go index b7965a2c98..4e2b9e5b8f 100644 --- a/storage/remote/client.go +++ b/storage/remote/client.go @@ -27,6 +27,7 @@ import ( "github.com/gogo/protobuf/proto" "github.com/golang/snappy" + "github.com/opentracing-contrib/go-stdlib/nethttp" "github.com/opentracing/opentracing-go" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" @@ -34,7 +35,6 @@ import ( "github.com/prometheus/common/model" "github.com/prometheus/common/version" - "github.com/opentracing-contrib/go-stdlib/nethttp" "github.com/prometheus/prometheus/prompb" ) diff --git a/storage/remote/client_test.go b/storage/remote/client_test.go index 22942e3f39..904123fa55 100644 --- a/storage/remote/client_test.go +++ b/storage/remote/client_test.go @@ -25,7 +25,7 @@ import ( "github.com/pkg/errors" config_util "github.com/prometheus/common/config" "github.com/prometheus/common/model" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" ) var longErrMessage = strings.Repeat("error message", maxErrMsgLen) @@ -53,7 +53,7 @@ func TestStoreHTTPErrorHandling(t *testing.T) { }, } - for i, test := range tests { + for _, test := range tests { server := httptest.NewServer( http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { http.Error(w, longErrMessage, test.code) @@ -61,7 +61,7 @@ func TestStoreHTTPErrorHandling(t *testing.T) { ) serverURL, err := url.Parse(server.URL) - testutil.Ok(t, err) + assert.NoError(t, err) conf := &ClientConfig{ URL: &config_util.URL{URL: serverURL}, @@ -69,12 +69,16 @@ func TestStoreHTTPErrorHandling(t *testing.T) { } hash, err := toHash(conf) - testutil.Ok(t, err) + assert.NoError(t, err) c, err := NewWriteClient(hash, conf) - testutil.Ok(t, err) + assert.NoError(t, err) err = c.Store(context.Background(), []byte{}) - testutil.ErrorEqual(t, err, test.err, "unexpected error in test %d", i) + if test.err != nil { + assert.EqualError(t, err, test.err.Error()) + } else { + assert.NoError(t, err) + } server.Close() } diff --git a/storage/remote/codec.go b/storage/remote/codec.go index 47360dd57b..474e4e4007 100644 --- a/storage/remote/codec.go +++ b/storage/remote/codec.go @@ -24,6 +24,7 @@ import ( "github.com/golang/snappy" "github.com/pkg/errors" "github.com/prometheus/common/model" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/prompb" "github.com/prometheus/prometheus/storage" diff --git a/storage/remote/codec_test.go b/storage/remote/codec_test.go index 64736906e4..8d7af46ea4 100644 --- a/storage/remote/codec_test.go +++ b/storage/remote/codec_test.go @@ -17,10 +17,11 @@ import ( "fmt" "testing" + "github.com/stretchr/testify/assert" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/prompb" "github.com/prometheus/prometheus/storage" - "github.com/prometheus/prometheus/util/testutil" ) func TestValidateLabelsAndMetricName(t *testing.T) { @@ -114,10 +115,10 @@ func TestValidateLabelsAndMetricName(t *testing.T) { t.Run(test.description, func(t *testing.T) { err := validateLabelsAndMetricName(test.input) if test.expectedErr != "" { - testutil.NotOk(t, err) - testutil.Equals(t, test.expectedErr, err.Error()) + assert.Error(t, err) + assert.Equal(t, test.expectedErr, err.Error()) } else { - testutil.Ok(t, err) + assert.NoError(t, err) } }) } @@ -135,11 +136,11 @@ func TestConcreteSeriesSet(t *testing.T) { c := &concreteSeriesSet{ series: []storage.Series{series1, series2}, } - testutil.Assert(t, c.Next(), "Expected Next() to be true.") - testutil.Equals(t, series1, c.At(), "Unexpected series returned.") - testutil.Assert(t, c.Next(), "Expected Next() to be true.") - testutil.Equals(t, series2, c.At(), "Unexpected series returned.") - testutil.Assert(t, !c.Next(), "Expected Next() to be false.") + assert.True(t, c.Next(), "Expected Next() to be true.") + assert.Equal(t, series1, c.At(), "Unexpected series returned.") + assert.True(t, c.Next(), "Expected Next() to be true.") + assert.Equal(t, series2, c.At(), "Unexpected series returned.") + assert.True(t, !c.Next(), "Expected Next() to be false.") } func TestConcreteSeriesClonesLabels(t *testing.T) { @@ -152,13 +153,13 @@ func TestConcreteSeriesClonesLabels(t *testing.T) { } gotLabels := cs.Labels() - testutil.Equals(t, lbls, gotLabels) + assert.Equal(t, lbls, gotLabels) gotLabels[0].Value = "foo" gotLabels[1].Value = "bar" gotLabels = cs.Labels() - testutil.Equals(t, lbls, gotLabels) + assert.Equal(t, lbls, gotLabels) } func TestFromQueryResultWithDuplicates(t *testing.T) { @@ -182,9 +183,9 @@ func TestFromQueryResultWithDuplicates(t *testing.T) { errSeries, isErrSeriesSet := series.(errSeriesSet) - testutil.Assert(t, isErrSeriesSet, "Expected resulting series to be an errSeriesSet") + assert.True(t, isErrSeriesSet, "Expected resulting series to be an errSeriesSet") errMessage := errSeries.Err().Error() - testutil.Assert(t, errMessage == "duplicate label with name: foo", fmt.Sprintf("Expected error to be from duplicate label, but got: %s", errMessage)) + assert.True(t, errMessage == "duplicate label with name: foo", fmt.Sprintf("Expected error to be from duplicate label, but got: %s", errMessage)) } func TestNegotiateResponseType(t *testing.T) { @@ -192,23 +193,23 @@ func TestNegotiateResponseType(t *testing.T) { prompb.ReadRequest_STREAMED_XOR_CHUNKS, prompb.ReadRequest_SAMPLES, }) - testutil.Ok(t, err) - testutil.Equals(t, prompb.ReadRequest_STREAMED_XOR_CHUNKS, r) + assert.NoError(t, err) + assert.Equal(t, prompb.ReadRequest_STREAMED_XOR_CHUNKS, r) r2, err := NegotiateResponseType([]prompb.ReadRequest_ResponseType{ prompb.ReadRequest_SAMPLES, prompb.ReadRequest_STREAMED_XOR_CHUNKS, }) - testutil.Ok(t, err) - testutil.Equals(t, prompb.ReadRequest_SAMPLES, r2) + assert.NoError(t, err) + assert.Equal(t, prompb.ReadRequest_SAMPLES, r2) r3, err := NegotiateResponseType([]prompb.ReadRequest_ResponseType{}) - testutil.Ok(t, err) - testutil.Equals(t, prompb.ReadRequest_SAMPLES, r3) + assert.NoError(t, err) + assert.Equal(t, prompb.ReadRequest_SAMPLES, r3) _, err = NegotiateResponseType([]prompb.ReadRequest_ResponseType{20}) - testutil.NotOk(t, err, "expected error due to not supported requested response types") - testutil.Equals(t, "server does not support any of the requested response types: [20]; supported: map[SAMPLES:{} STREAMED_XOR_CHUNKS:{}]", err.Error()) + assert.Error(t, err, "expected error due to not supported requested response types") + assert.Equal(t, "server does not support any of the requested response types: [20]; supported: map[SAMPLES:{} STREAMED_XOR_CHUNKS:{}]", err.Error()) } func TestMergeLabels(t *testing.T) { @@ -226,6 +227,6 @@ func TestMergeLabels(t *testing.T) { expected: []prompb.Label{{Name: "aaa", Value: "foo"}, {Name: "bbb", Value: "bar"}, {Name: "ccc", Value: "bar"}, {Name: "ddd", Value: "foo"}}, }, } { - testutil.Equals(t, tc.expected, MergeLabels(tc.primary, tc.secondary)) + assert.Equal(t, tc.expected, MergeLabels(tc.primary, tc.secondary)) } } diff --git a/storage/remote/intern.go b/storage/remote/intern.go index 98eec34141..23047acd9b 100644 --- a/storage/remote/intern.go +++ b/storage/remote/intern.go @@ -21,10 +21,9 @@ package remote import ( "sync" - "go.uber.org/atomic" - "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" + "go.uber.org/atomic" ) var noReferenceReleases = promauto.NewCounter(prometheus.CounterOpts{ diff --git a/storage/remote/intern_test.go b/storage/remote/intern_test.go index 7f03275e57..bffcc42600 100644 --- a/storage/remote/intern_test.go +++ b/storage/remote/intern_test.go @@ -23,7 +23,7 @@ import ( "testing" "time" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" ) func TestIntern(t *testing.T) { @@ -32,8 +32,8 @@ func TestIntern(t *testing.T) { interner.intern(testString) interned, ok := interner.pool[testString] - testutil.Equals(t, true, ok) - testutil.Assert(t, interned.refs.Load() == 1, fmt.Sprintf("expected refs to be 1 but it was %d", interned.refs.Load())) + assert.Equal(t, true, ok) + assert.True(t, interned.refs.Load() == 1, fmt.Sprintf("expected refs to be 1 but it was %d", interned.refs.Load())) } func TestIntern_MultiRef(t *testing.T) { @@ -43,14 +43,14 @@ func TestIntern_MultiRef(t *testing.T) { interner.intern(testString) interned, ok := interner.pool[testString] - testutil.Equals(t, true, ok) - testutil.Assert(t, interned.refs.Load() == 1, fmt.Sprintf("expected refs to be 1 but it was %d", interned.refs.Load())) + assert.Equal(t, true, ok) + assert.True(t, interned.refs.Load() == 1, fmt.Sprintf("expected refs to be 1 but it was %d", interned.refs.Load())) interner.intern(testString) interned, ok = interner.pool[testString] - testutil.Equals(t, true, ok) - testutil.Assert(t, interned.refs.Load() == 2, fmt.Sprintf("expected refs to be 2 but it was %d", interned.refs.Load())) + assert.Equal(t, true, ok) + assert.True(t, interned.refs.Load() == 2, fmt.Sprintf("expected refs to be 2 but it was %d", interned.refs.Load())) } func TestIntern_DeleteRef(t *testing.T) { @@ -60,12 +60,12 @@ func TestIntern_DeleteRef(t *testing.T) { interner.intern(testString) interned, ok := interner.pool[testString] - testutil.Equals(t, true, ok) - testutil.Assert(t, interned.refs.Load() == 1, fmt.Sprintf("expected refs to be 1 but it was %d", interned.refs.Load())) + assert.Equal(t, true, ok) + assert.True(t, interned.refs.Load() == 1, fmt.Sprintf("expected refs to be 1 but it was %d", interned.refs.Load())) interner.release(testString) _, ok = interner.pool[testString] - testutil.Equals(t, false, ok) + assert.Equal(t, false, ok) } func TestIntern_MultiRef_Concurrent(t *testing.T) { @@ -74,8 +74,8 @@ func TestIntern_MultiRef_Concurrent(t *testing.T) { interner.intern(testString) interned, ok := interner.pool[testString] - testutil.Equals(t, true, ok) - testutil.Assert(t, interned.refs.Load() == 1, fmt.Sprintf("expected refs to be 1 but it was %d", interned.refs.Load())) + assert.Equal(t, true, ok) + assert.True(t, interned.refs.Load() == 1, fmt.Sprintf("expected refs to be 1 but it was %d", interned.refs.Load())) go interner.release(testString) @@ -86,6 +86,6 @@ func TestIntern_MultiRef_Concurrent(t *testing.T) { interner.mtx.RLock() interned, ok = interner.pool[testString] interner.mtx.RUnlock() - testutil.Equals(t, true, ok) - testutil.Assert(t, interned.refs.Load() == 1, fmt.Sprintf("expected refs to be 1 but it was %d", interned.refs.Load())) + assert.Equal(t, true, ok) + assert.True(t, interned.refs.Load() == 1, fmt.Sprintf("expected refs to be 1 but it was %d", interned.refs.Load())) } diff --git a/storage/remote/queue_manager.go b/storage/remote/queue_manager.go index 47b5726bdc..6e04d65159 100644 --- a/storage/remote/queue_manager.go +++ b/storage/remote/queue_manager.go @@ -26,9 +26,9 @@ import ( "github.com/golang/snappy" "github.com/opentracing/opentracing-go" "github.com/opentracing/opentracing-go/ext" + "github.com/prometheus/client_golang/prometheus" "go.uber.org/atomic" - "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/relabel" diff --git a/storage/remote/queue_manager_test.go b/storage/remote/queue_manager_test.go index d8236f8dee..2a9d55fa31 100644 --- a/storage/remote/queue_manager_test.go +++ b/storage/remote/queue_manager_test.go @@ -18,8 +18,8 @@ import ( "fmt" "io/ioutil" "math" + "net/url" "os" - "reflect" "sort" "strconv" "strings" @@ -30,19 +30,18 @@ import ( "github.com/go-kit/kit/log" "github.com/gogo/protobuf/proto" "github.com/golang/snappy" - "go.uber.org/atomic" - "github.com/prometheus/client_golang/prometheus" client_testutil "github.com/prometheus/client_golang/prometheus/testutil" common_config "github.com/prometheus/common/config" "github.com/prometheus/common/model" + "github.com/stretchr/testify/assert" + "go.uber.org/atomic" + "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/timestamp" "github.com/prometheus/prometheus/prompb" "github.com/prometheus/prometheus/tsdb/record" - "github.com/prometheus/prometheus/util/testutil" - "net/url" ) const defaultFlushDeadline = 1 * time.Minute @@ -74,9 +73,9 @@ func TestSampleDelivery(t *testing.T) { queueConfig.MaxSamplesPerSend = len(samples) / 2 dir, err := ioutil.TempDir("", "TestSampleDeliver") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() s := NewStorage(nil, nil, nil, dir, defaultFlushDeadline) @@ -96,9 +95,9 @@ func TestSampleDelivery(t *testing.T) { }, } writeConfig.QueueConfig = queueConfig - testutil.Ok(t, s.ApplyConfig(conf)) + assert.NoError(t, s.ApplyConfig(conf)) hash, err := toHash(writeConfig) - testutil.Ok(t, err) + assert.NoError(t, err) qm := s.rws.queues[hash] qm.SetClient(c) @@ -122,9 +121,9 @@ func TestSampleDeliveryTimeout(t *testing.T) { cfg.BatchSendDeadline = model.Duration(100 * time.Millisecond) dir, err := ioutil.TempDir("", "TestSampleDeliveryTimeout") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() metrics := newQueueManagerMetrics(nil, "", "") @@ -165,9 +164,9 @@ func TestSampleDeliveryOrder(t *testing.T) { c.expectSamples(samples, series) dir, err := ioutil.TempDir("", "TestSampleDeliveryOrder") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() metrics := newQueueManagerMetrics(nil, "", "") @@ -186,9 +185,9 @@ func TestShutdown(t *testing.T) { c := NewTestBlockedWriteClient() dir, err := ioutil.TempDir("", "TestShutdown") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() metrics := newQueueManagerMetrics(nil, "", "") @@ -227,9 +226,9 @@ func TestSeriesReset(t *testing.T) { numSeries := 25 dir, err := ioutil.TempDir("", "TestSeriesReset") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() metrics := newQueueManagerMetrics(nil, "", "") @@ -241,9 +240,9 @@ func TestSeriesReset(t *testing.T) { } m.StoreSeries(series, i) } - testutil.Equals(t, numSegments*numSeries, len(m.seriesLabels)) + assert.Equal(t, numSegments*numSeries, len(m.seriesLabels)) m.SeriesReset(2) - testutil.Equals(t, numSegments*numSeries/2, len(m.seriesLabels)) + assert.Equal(t, numSegments*numSeries/2, len(m.seriesLabels)) } func TestReshard(t *testing.T) { @@ -259,9 +258,9 @@ func TestReshard(t *testing.T) { cfg.MaxShards = 1 dir, err := ioutil.TempDir("", "TestReshard") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() metrics := newQueueManagerMetrics(nil, "", "") @@ -274,7 +273,7 @@ func TestReshard(t *testing.T) { go func() { for i := 0; i < len(samples); i += config.DefaultQueueConfig.Capacity { sent := m.Append(samples[i : i+config.DefaultQueueConfig.Capacity]) - testutil.Assert(t, sent, "samples not sent") + assert.True(t, sent, "samples not sent") time.Sleep(100 * time.Millisecond) } }() @@ -335,7 +334,7 @@ func TestReleaseNoninternedString(t *testing.T) { } metric := client_testutil.ToFloat64(noReferenceReleases) - testutil.Assert(t, metric == 0, "expected there to be no calls to release for strings that were not already interned: %d", int(metric)) + assert.True(t, metric == 0, "expected there to be no calls to release for strings that were not already interned: %d", int(metric)) } func TestShouldReshard(t *testing.T) { @@ -377,7 +376,7 @@ func TestShouldReshard(t *testing.T) { m.Stop() - testutil.Equals(t, c.expectedToReshard, shouldReshard) + assert.Equal(t, c.expectedToReshard, shouldReshard) } } @@ -456,9 +455,7 @@ func (c *TestWriteClient) waitForExpectedSamples(tb testing.TB) { c.mtx.Lock() defer c.mtx.Unlock() for ts, expectedSamples := range c.expectedSamples { - if !reflect.DeepEqual(expectedSamples, c.receivedSamples[ts]) { - tb.Fatalf("%s: Expected %v, got %v", ts, expectedSamples, c.receivedSamples[ts]) - } + assert.Equal(tb, expectedSamples, c.receivedSamples[ts], ts) } } @@ -567,7 +564,7 @@ func BenchmarkSampleDelivery(b *testing.B) { cfg.MaxShards = 1 dir, err := ioutil.TempDir("", "BenchmarkSampleDelivery") - testutil.Ok(b, err) + assert.NoError(b, err) defer os.RemoveAll(dir) metrics := newQueueManagerMetrics(nil, "", "") @@ -597,7 +594,7 @@ func BenchmarkStartup(b *testing.B) { // Find the second largest segment; we will replay up to this. // (Second largest as WALWatcher will start tailing the largest). dirents, err := ioutil.ReadDir(dir) - testutil.Ok(b, err) + assert.NoError(b, err) var segments []int for _, dirent := range dirents { @@ -619,7 +616,7 @@ func BenchmarkStartup(b *testing.B) { m.watcher.SetStartTime(timestamp.Time(math.MaxInt64)) m.watcher.MaxSegment = segments[len(segments)-2] err := m.watcher.Run() - testutil.Ok(b, err) + assert.NoError(b, err) } } @@ -650,7 +647,7 @@ func TestProcessExternalLabels(t *testing.T) { expected: labels.Labels{{Name: "a", Value: "b"}}, }, } { - testutil.Equals(t, tc.expected, processExternalLabels(tc.labels, tc.externalLabels)) + assert.Equal(t, tc.expected, processExternalLabels(tc.labels, tc.externalLabels)) } } @@ -659,9 +656,9 @@ func TestCalculateDesiredShards(t *testing.T) { cfg := config.DefaultQueueConfig dir, err := ioutil.TempDir("", "TestCalculateDesiredShards") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() metrics := newQueueManagerMetrics(nil, "", "") @@ -706,7 +703,7 @@ func TestCalculateDesiredShards(t *testing.T) { for ; ts < 120*time.Second; ts += shardUpdateDuration { addSamples(inputRate*int64(shardUpdateDuration/time.Second), ts) m.numShards = m.calculateDesiredShards() - testutil.Equals(t, 1, m.numShards) + assert.Equal(t, 1, m.numShards) } // Assume 100ms per request, or 10 requests per second per shard. @@ -728,10 +725,10 @@ func TestCalculateDesiredShards(t *testing.T) { t.Log("desiredShards", m.numShards, "pendingSamples", pendingSamples) m.numShards = m.calculateDesiredShards() - testutil.Assert(t, m.numShards >= minShards, "Shards are too low. desiredShards=%d, minShards=%d, t_seconds=%d", m.numShards, minShards, ts/time.Second) - testutil.Assert(t, m.numShards <= maxShards, "Shards are too high. desiredShards=%d, maxShards=%d, t_seconds=%d", m.numShards, maxShards, ts/time.Second) + assert.True(t, m.numShards >= minShards, "Shards are too low. desiredShards=%d, minShards=%d, t_seconds=%d", m.numShards, minShards, ts/time.Second) + assert.True(t, m.numShards <= maxShards, "Shards are too high. desiredShards=%d, maxShards=%d, t_seconds=%d", m.numShards, maxShards, ts/time.Second) } - testutil.Assert(t, pendingSamples == 0, "Remote write never caught up, there are still %d pending samples.", pendingSamples) + assert.True(t, pendingSamples == 0, "Remote write never caught up, there are still %d pending samples.", pendingSamples) } func TestQueueManagerMetrics(t *testing.T) { @@ -740,12 +737,12 @@ func TestQueueManagerMetrics(t *testing.T) { // Make sure metrics pass linting. problems, err := client_testutil.GatherAndLint(reg) - testutil.Ok(t, err) - testutil.Equals(t, 0, len(problems), "Metric linting problems detected: %v", problems) + assert.NoError(t, err) + assert.Equal(t, 0, len(problems), "Metric linting problems detected: %v", problems) // Make sure all metrics were unregistered. A failure here means you need // unregister a metric in `queueManagerMetrics.unregister()`. metrics.unregister() err = client_testutil.GatherAndCompare(reg, strings.NewReader("")) - testutil.Ok(t, err) + assert.NoError(t, err) } diff --git a/storage/remote/read.go b/storage/remote/read.go index 39822a6763..4718b47978 100644 --- a/storage/remote/read.go +++ b/storage/remote/read.go @@ -17,6 +17,7 @@ import ( "context" "github.com/pkg/errors" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/storage" ) diff --git a/storage/remote/read_test.go b/storage/remote/read_test.go index cafb579e74..76e436bc4e 100644 --- a/storage/remote/read_test.go +++ b/storage/remote/read_test.go @@ -18,23 +18,23 @@ import ( "io/ioutil" "net/url" "os" - "reflect" "sort" "testing" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" config_util "github.com/prometheus/common/config" + "github.com/stretchr/testify/assert" + "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/prompb" "github.com/prometheus/prometheus/storage" - "github.com/prometheus/prometheus/util/testutil" ) func TestNoDuplicateReadConfigs(t *testing.T) { dir, err := ioutil.TempDir("", "TestNoDuplicateReadConfigs") - testutil.Ok(t, err) + assert.NoError(t, err) defer os.RemoveAll(dir) cfg1 := config.RemoteReadConfig{ @@ -103,8 +103,8 @@ func TestNoDuplicateReadConfigs(t *testing.T) { err := s.ApplyConfig(conf) prometheus.Unregister(s.rws.highestTimestamp) gotError := err != nil - testutil.Equals(t, tc.err, gotError) - testutil.Ok(t, s.Close()) + assert.Equal(t, tc.err, gotError) + assert.NoError(t, s.Close()) }) } } @@ -170,12 +170,8 @@ func TestExternalLabelsQuerierAddExternalLabels(t *testing.T) { sort.Slice(test.outMatchers, func(i, j int) bool { return test.outMatchers[i].Name < test.outMatchers[j].Name }) sort.Slice(matchers, func(i, j int) bool { return matchers[i].Name < matchers[j].Name }) - if !reflect.DeepEqual(matchers, test.outMatchers) { - t.Fatalf("%d. unexpected matchers; want %v, got %v", i, test.outMatchers, matchers) - } - if !reflect.DeepEqual(added, test.added) { - t.Fatalf("%d. unexpected added labels; want %v, got %v", i, test.added, added) - } + assert.Equal(t, test.outMatchers, matchers, "%d", i) + assert.Equal(t, test.added, added, "%d", i) } } @@ -204,9 +200,9 @@ func TestSeriesSetFilter(t *testing.T) { for _, tc := range tests { filtered := newSeriesSetFilter(FromQueryResult(true, tc.in), tc.toRemove) act, ws, err := ToQueryResult(filtered, 1e6) - testutil.Ok(t, err) - testutil.Equals(t, 0, len(ws)) - testutil.Equals(t, tc.expected, act) + assert.NoError(t, err) + assert.Equal(t, 0, len(ws)) + assert.Equal(t, tc.expected, act) } } @@ -495,21 +491,21 @@ func TestSampleAndChunkQueryableClient(t *testing.T) { tc.callback, ) q, err := c.Querier(context.TODO(), tc.mint, tc.maxt) - testutil.Ok(t, err) - defer testutil.Ok(t, q.Close()) + assert.NoError(t, err) + defer assert.NoError(t, q.Close()) ss := q.Select(true, nil, tc.matchers...) - testutil.Ok(t, err) - testutil.Equals(t, storage.Warnings(nil), ss.Warnings()) + assert.NoError(t, err) + assert.Equal(t, storage.Warnings(nil), ss.Warnings()) - testutil.Equals(t, tc.expectedQuery, m.got) + assert.Equal(t, tc.expectedQuery, m.got) var got []labels.Labels for ss.Next() { got = append(got, ss.At().Labels()) } - testutil.Ok(t, ss.Err()) - testutil.Equals(t, tc.expectedSeries, got) + assert.NoError(t, ss.Err()) + assert.Equal(t, tc.expectedSeries, got) }) } diff --git a/storage/remote/storage.go b/storage/remote/storage.go index 36d7c01145..5fb4bfbecb 100644 --- a/storage/remote/storage.go +++ b/storage/remote/storage.go @@ -22,10 +22,10 @@ import ( "time" "github.com/go-kit/kit/log" - "gopkg.in/yaml.v2" - "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/common/model" + "gopkg.in/yaml.v2" + "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/logging" diff --git a/storage/remote/storage_test.go b/storage/remote/storage_test.go index 2c114339f2..122520bce1 100644 --- a/storage/remote/storage_test.go +++ b/storage/remote/storage_test.go @@ -20,13 +20,14 @@ import ( "testing" common_config "github.com/prometheus/common/config" + "github.com/stretchr/testify/assert" + "github.com/prometheus/prometheus/config" - "github.com/prometheus/prometheus/util/testutil" ) func TestStorageLifecycle(t *testing.T) { dir, err := ioutil.TempDir("", "TestStorageLifecycle") - testutil.Ok(t, err) + assert.NoError(t, err) defer os.RemoveAll(dir) s := NewStorage(nil, nil, nil, dir, defaultFlushDeadline) @@ -51,21 +52,21 @@ func TestStorageLifecycle(t *testing.T) { }, } - testutil.Ok(t, s.ApplyConfig(conf)) + assert.NoError(t, s.ApplyConfig(conf)) // make sure remote write has a queue. - testutil.Equals(t, 1, len(s.rws.queues)) + assert.Equal(t, 1, len(s.rws.queues)) // make sure remote write has a queue. - testutil.Equals(t, 1, len(s.queryables)) + assert.Equal(t, 1, len(s.queryables)) err = s.Close() - testutil.Ok(t, err) + assert.NoError(t, err) } func TestUpdateRemoteReadConfigs(t *testing.T) { dir, err := ioutil.TempDir("", "TestUpdateRemoteReadConfigs") - testutil.Ok(t, err) + assert.NoError(t, err) defer os.RemoveAll(dir) s := NewStorage(nil, nil, nil, dir, defaultFlushDeadline) @@ -73,15 +74,15 @@ func TestUpdateRemoteReadConfigs(t *testing.T) { conf := &config.Config{ GlobalConfig: config.GlobalConfig{}, } - testutil.Ok(t, s.ApplyConfig(conf)) - testutil.Equals(t, 0, len(s.queryables)) + assert.NoError(t, s.ApplyConfig(conf)) + assert.Equal(t, 0, len(s.queryables)) conf.RemoteReadConfigs = []*config.RemoteReadConfig{ &config.DefaultRemoteReadConfig, } - testutil.Ok(t, s.ApplyConfig(conf)) - testutil.Equals(t, 1, len(s.queryables)) + assert.NoError(t, s.ApplyConfig(conf)) + assert.Equal(t, 1, len(s.queryables)) err = s.Close() - testutil.Ok(t, err) + assert.NoError(t, err) } diff --git a/storage/remote/write.go b/storage/remote/write.go index 9e93e0c18f..64d3be326d 100644 --- a/storage/remote/write.go +++ b/storage/remote/write.go @@ -22,6 +22,7 @@ import ( "github.com/go-kit/kit/log" "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promauto" + "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/storage" diff --git a/storage/remote/write_test.go b/storage/remote/write_test.go index c2b2afc475..abf88fab80 100644 --- a/storage/remote/write_test.go +++ b/storage/remote/write_test.go @@ -23,10 +23,11 @@ import ( "github.com/prometheus/client_golang/prometheus" common_config "github.com/prometheus/common/config" "github.com/prometheus/common/model" + "github.com/stretchr/testify/assert" + "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/relabel" - "github.com/prometheus/prometheus/util/testutil" ) var cfg = config.RemoteWriteConfig{ @@ -42,9 +43,9 @@ var cfg = config.RemoteWriteConfig{ func TestNoDuplicateWriteConfigs(t *testing.T) { dir, err := ioutil.TempDir("", "TestNoDuplicateWriteConfigs") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() cfg1 := config.RemoteWriteConfig{ @@ -121,22 +122,22 @@ func TestNoDuplicateWriteConfigs(t *testing.T) { } err := s.ApplyConfig(conf) gotError := err != nil - testutil.Equals(t, tc.err, gotError) + assert.Equal(t, tc.err, gotError) err = s.Close() - testutil.Ok(t, err) + assert.NoError(t, err) } } func TestRestartOnNameChange(t *testing.T) { dir, err := ioutil.TempDir("", "TestRestartOnNameChange") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() hash, err := toHash(cfg) - testutil.Ok(t, err) + assert.NoError(t, err) s := NewWriteStorage(nil, nil, dir, time.Millisecond) conf := &config.Config{ @@ -145,25 +146,25 @@ func TestRestartOnNameChange(t *testing.T) { &cfg, }, } - testutil.Ok(t, s.ApplyConfig(conf)) - testutil.Equals(t, s.queues[hash].client().Name(), cfg.Name) + assert.NoError(t, s.ApplyConfig(conf)) + assert.Equal(t, s.queues[hash].client().Name(), cfg.Name) // Change the queues name, ensure the queue has been restarted. conf.RemoteWriteConfigs[0].Name = "dev-2" - testutil.Ok(t, s.ApplyConfig(conf)) + assert.NoError(t, s.ApplyConfig(conf)) hash, err = toHash(cfg) - testutil.Ok(t, err) - testutil.Equals(t, s.queues[hash].client().Name(), conf.RemoteWriteConfigs[0].Name) + assert.NoError(t, err) + assert.Equal(t, s.queues[hash].client().Name(), conf.RemoteWriteConfigs[0].Name) err = s.Close() - testutil.Ok(t, err) + assert.NoError(t, err) } func TestUpdateWithRegisterer(t *testing.T) { dir, err := ioutil.TempDir("", "TestRestartWithRegisterer") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() s := NewWriteStorage(nil, prometheus.NewRegistry(), dir, time.Millisecond) @@ -190,24 +191,24 @@ func TestUpdateWithRegisterer(t *testing.T) { GlobalConfig: config.DefaultGlobalConfig, RemoteWriteConfigs: []*config.RemoteWriteConfig{c1, c2}, } - testutil.Ok(t, s.ApplyConfig(conf)) + assert.NoError(t, s.ApplyConfig(conf)) c1.QueueConfig.MaxShards = 10 c2.QueueConfig.MaxShards = 10 - testutil.Ok(t, s.ApplyConfig(conf)) + assert.NoError(t, s.ApplyConfig(conf)) for _, queue := range s.queues { - testutil.Equals(t, 10, queue.cfg.MaxShards) + assert.Equal(t, 10, queue.cfg.MaxShards) } err = s.Close() - testutil.Ok(t, err) + assert.NoError(t, err) } func TestWriteStorageLifecycle(t *testing.T) { dir, err := ioutil.TempDir("", "TestWriteStorageLifecycle") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() s := NewWriteStorage(nil, nil, dir, defaultFlushDeadline) @@ -218,17 +219,17 @@ func TestWriteStorageLifecycle(t *testing.T) { }, } s.ApplyConfig(conf) - testutil.Equals(t, 1, len(s.queues)) + assert.Equal(t, 1, len(s.queues)) err = s.Close() - testutil.Ok(t, err) + assert.NoError(t, err) } func TestUpdateExternalLabels(t *testing.T) { dir, err := ioutil.TempDir("", "TestUpdateExternalLabels") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() s := NewWriteStorage(nil, prometheus.NewRegistry(), dir, time.Second) @@ -241,27 +242,27 @@ func TestUpdateExternalLabels(t *testing.T) { }, } hash, err := toHash(conf.RemoteWriteConfigs[0]) - testutil.Ok(t, err) + assert.NoError(t, err) s.ApplyConfig(conf) - testutil.Equals(t, 1, len(s.queues)) - testutil.Equals(t, labels.Labels(nil), s.queues[hash].externalLabels) + assert.Equal(t, 1, len(s.queues)) + assert.Equal(t, labels.Labels(nil), s.queues[hash].externalLabels) conf.GlobalConfig.ExternalLabels = externalLabels hash, err = toHash(conf.RemoteWriteConfigs[0]) - testutil.Ok(t, err) + assert.NoError(t, err) s.ApplyConfig(conf) - testutil.Equals(t, 1, len(s.queues)) - testutil.Equals(t, externalLabels, s.queues[hash].externalLabels) + assert.Equal(t, 1, len(s.queues)) + assert.Equal(t, externalLabels, s.queues[hash].externalLabels) err = s.Close() - testutil.Ok(t, err) + assert.NoError(t, err) } func TestWriteStorageApplyConfigsIdempotent(t *testing.T) { dir, err := ioutil.TempDir("", "TestWriteStorageApplyConfigsIdempotent") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() s := NewWriteStorage(nil, nil, dir, defaultFlushDeadline) @@ -279,25 +280,25 @@ func TestWriteStorageApplyConfigsIdempotent(t *testing.T) { }, } hash, err := toHash(conf.RemoteWriteConfigs[0]) - testutil.Ok(t, err) + assert.NoError(t, err) s.ApplyConfig(conf) - testutil.Equals(t, 1, len(s.queues)) + assert.Equal(t, 1, len(s.queues)) s.ApplyConfig(conf) - testutil.Equals(t, 1, len(s.queues)) + assert.Equal(t, 1, len(s.queues)) _, hashExists := s.queues[hash] - testutil.Assert(t, hashExists, "Queue pointer should have remained the same") + assert.True(t, hashExists, "Queue pointer should have remained the same") err = s.Close() - testutil.Ok(t, err) + assert.NoError(t, err) } func TestWriteStorageApplyConfigsPartialUpdate(t *testing.T) { dir, err := ioutil.TempDir("", "TestWriteStorageApplyConfigsPartialUpdate") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() s := NewWriteStorage(nil, nil, dir, defaultFlushDeadline) @@ -335,15 +336,15 @@ func TestWriteStorageApplyConfigsPartialUpdate(t *testing.T) { }, } } - testutil.Ok(t, s.ApplyConfig(conf)) - testutil.Equals(t, 3, len(s.queues)) + assert.NoError(t, s.ApplyConfig(conf)) + assert.Equal(t, 3, len(s.queues)) hashes := make([]string, len(conf.RemoteWriteConfigs)) queues := make([]*QueueManager, len(conf.RemoteWriteConfigs)) storeHashes := func() { for i := range conf.RemoteWriteConfigs { hash, err := toHash(conf.RemoteWriteConfigs[i]) - testutil.Ok(t, err) + assert.NoError(t, err) hashes[i] = hash queues[i] = s.queues[hash] } @@ -357,32 +358,32 @@ func TestWriteStorageApplyConfigsPartialUpdate(t *testing.T) { GlobalConfig: config.GlobalConfig{}, RemoteWriteConfigs: []*config.RemoteWriteConfig{c0, c1, c2}, } - testutil.Ok(t, s.ApplyConfig(conf)) - testutil.Equals(t, 3, len(s.queues)) + assert.NoError(t, s.ApplyConfig(conf)) + assert.Equal(t, 3, len(s.queues)) _, hashExists := s.queues[hashes[0]] - testutil.Assert(t, !hashExists, "The queue for the first remote write configuration should have been restarted because the relabel configuration has changed.") + assert.True(t, !hashExists, "The queue for the first remote write configuration should have been restarted because the relabel configuration has changed.") q, hashExists := s.queues[hashes[1]] - testutil.Assert(t, hashExists, "Hash of unchanged queue should have remained the same") - testutil.Assert(t, q == queues[1], "Pointer of unchanged queue should have remained the same") + assert.True(t, hashExists, "Hash of unchanged queue should have remained the same") + assert.True(t, q == queues[1], "Pointer of unchanged queue should have remained the same") _, hashExists = s.queues[hashes[2]] - testutil.Assert(t, !hashExists, "The queue for the third remote write configuration should have been restarted because the timeout has changed.") + assert.True(t, !hashExists, "The queue for the third remote write configuration should have been restarted because the timeout has changed.") storeHashes() secondClient := s.queues[hashes[1]].client() // Update c1. c1.HTTPClientConfig.BearerToken = "bar" err = s.ApplyConfig(conf) - testutil.Ok(t, err) - testutil.Equals(t, 3, len(s.queues)) + assert.NoError(t, err) + assert.Equal(t, 3, len(s.queues)) _, hashExists = s.queues[hashes[0]] - testutil.Assert(t, hashExists, "Pointer of unchanged queue should have remained the same") + assert.True(t, hashExists, "Pointer of unchanged queue should have remained the same") q, hashExists = s.queues[hashes[1]] - testutil.Assert(t, hashExists, "Hash of queue with secret change should have remained the same") - testutil.Assert(t, secondClient != q.client(), "Pointer of a client with a secret change should not be the same") + assert.True(t, hashExists, "Hash of queue with secret change should have remained the same") + assert.True(t, secondClient != q.client(), "Pointer of a client with a secret change should not be the same") _, hashExists = s.queues[hashes[2]] - testutil.Assert(t, hashExists, "Pointer of unchanged queue should have remained the same") + assert.True(t, hashExists, "Pointer of unchanged queue should have remained the same") storeHashes() // Delete c0. @@ -391,15 +392,15 @@ func TestWriteStorageApplyConfigsPartialUpdate(t *testing.T) { RemoteWriteConfigs: []*config.RemoteWriteConfig{c1, c2}, } s.ApplyConfig(conf) - testutil.Equals(t, 2, len(s.queues)) + assert.Equal(t, 2, len(s.queues)) _, hashExists = s.queues[hashes[0]] - testutil.Assert(t, !hashExists, "If a config is removed, the queue should be stopped and recreated.") + assert.True(t, !hashExists, "If a config is removed, the queue should be stopped and recreated.") _, hashExists = s.queues[hashes[1]] - testutil.Assert(t, hashExists, "Pointer of unchanged queue should have remained the same") + assert.True(t, hashExists, "Pointer of unchanged queue should have remained the same") _, hashExists = s.queues[hashes[2]] - testutil.Assert(t, hashExists, "Pointer of unchanged queue should have remained the same") + assert.True(t, hashExists, "Pointer of unchanged queue should have remained the same") err = s.Close() - testutil.Ok(t, err) + assert.NoError(t, err) } diff --git a/template/template.go b/template/template.go index 0a31c4e54a..10a9241c79 100644 --- a/template/template.go +++ b/template/template.go @@ -17,20 +17,19 @@ import ( "bytes" "context" "fmt" + html_template "html/template" "math" "net/url" "regexp" "sort" "strings" + text_template "text/template" "time" - html_template "html/template" - text_template "text/template" - "github.com/pkg/errors" - "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/common/model" + "github.com/prometheus/prometheus/promql" "github.com/prometheus/prometheus/util/strutil" ) diff --git a/template/template_test.go b/template/template_test.go index 654f0de24a..315a8945aa 100644 --- a/template/template_test.go +++ b/template/template_test.go @@ -20,9 +20,10 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/promql" - "github.com/prometheus/prometheus/util/testutil" ) func TestTemplateExpansion(t *testing.T) { @@ -283,14 +284,14 @@ func TestTemplateExpansion(t *testing.T) { result, err = expander.Expand() } if s.shouldFail { - testutil.NotOk(t, err, "%v", s.text) + assert.Error(t, err, "%v", s.text) continue } - testutil.Ok(t, err) + assert.NoError(t, err) if err == nil { - testutil.Equals(t, result, s.output) + assert.Equal(t, result, s.output) } } } diff --git a/tsdb/block.go b/tsdb/block.go index c6de088d33..e177c112a5 100644 --- a/tsdb/block.go +++ b/tsdb/block.go @@ -26,6 +26,7 @@ import ( "github.com/go-kit/kit/log/level" "github.com/oklog/ulid" "github.com/pkg/errors" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/tsdb/chunkenc" "github.com/prometheus/prometheus/tsdb/chunks" diff --git a/tsdb/block_test.go b/tsdb/block_test.go index 6c095925dc..d59a47b71a 100644 --- a/tsdb/block_test.go +++ b/tsdb/block_test.go @@ -26,13 +26,14 @@ import ( "testing" "github.com/go-kit/kit/log" + "github.com/stretchr/testify/assert" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/storage" "github.com/prometheus/prometheus/tsdb/chunks" "github.com/prometheus/prometheus/tsdb/fileutil" "github.com/prometheus/prometheus/tsdb/tsdbutil" "github.com/prometheus/prometheus/tsdb/wal" - "github.com/prometheus/prometheus/util/testutil" ) // In Prometheus 2.1.0 we had a bug where the meta.json version was falsely bumped @@ -40,51 +41,51 @@ import ( // version 3 next time to avoid confusion and issues. func TestBlockMetaMustNeverBeVersion2(t *testing.T) { dir, err := ioutil.TempDir("", "metaversion") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() _, err = writeMetaFile(log.NewNopLogger(), dir, &BlockMeta{}) - testutil.Ok(t, err) + assert.NoError(t, err) meta, _, err := readMetaFile(dir) - testutil.Ok(t, err) - testutil.Assert(t, meta.Version != 2, "meta.json version must never be 2") + assert.NoError(t, err) + assert.True(t, meta.Version != 2, "meta.json version must never be 2") } func TestSetCompactionFailed(t *testing.T) { tmpdir, err := ioutil.TempDir("", "test") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(tmpdir)) + assert.NoError(t, os.RemoveAll(tmpdir)) }() blockDir := createBlock(t, tmpdir, genSeries(1, 1, 0, 1)) b, err := OpenBlock(nil, blockDir, nil) - testutil.Ok(t, err) - testutil.Equals(t, false, b.meta.Compaction.Failed) - testutil.Ok(t, b.setCompactionFailed()) - testutil.Equals(t, true, b.meta.Compaction.Failed) - testutil.Ok(t, b.Close()) + assert.NoError(t, err) + assert.Equal(t, false, b.meta.Compaction.Failed) + assert.NoError(t, b.setCompactionFailed()) + assert.Equal(t, true, b.meta.Compaction.Failed) + assert.NoError(t, b.Close()) b, err = OpenBlock(nil, blockDir, nil) - testutil.Ok(t, err) - testutil.Equals(t, true, b.meta.Compaction.Failed) - testutil.Ok(t, b.Close()) + assert.NoError(t, err) + assert.Equal(t, true, b.meta.Compaction.Failed) + assert.NoError(t, b.Close()) } func TestCreateBlock(t *testing.T) { tmpdir, err := ioutil.TempDir("", "test") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(tmpdir)) + assert.NoError(t, os.RemoveAll(tmpdir)) }() b, err := OpenBlock(nil, createBlock(t, tmpdir, genSeries(1, 1, 0, 10)), nil) if err == nil { - testutil.Ok(t, b.Close()) + assert.NoError(t, b.Close()) } - testutil.Ok(t, err) + assert.NoError(t, err) } func TestCorruptedChunk(t *testing.T) { @@ -97,7 +98,7 @@ func TestCorruptedChunk(t *testing.T) { { name: "invalid header size", corrFunc: func(f *os.File) { - testutil.Ok(t, f.Truncate(1)) + assert.NoError(t, f.Truncate(1)) }, openErr: errors.New("invalid segment header in segment 0: invalid size"), }, @@ -106,14 +107,14 @@ func TestCorruptedChunk(t *testing.T) { corrFunc: func(f *os.File) { magicChunksOffset := int64(0) _, err := f.Seek(magicChunksOffset, 0) - testutil.Ok(t, err) + assert.NoError(t, err) // Set invalid magic number. b := make([]byte, chunks.MagicChunksSize) binary.BigEndian.PutUint32(b[:chunks.MagicChunksSize], 0x00000000) n, err := f.Write(b) - testutil.Ok(t, err) - testutil.Equals(t, chunks.MagicChunksSize, n) + assert.NoError(t, err) + assert.Equal(t, chunks.MagicChunksSize, n) }, openErr: errors.New("invalid magic number 0"), }, @@ -122,14 +123,14 @@ func TestCorruptedChunk(t *testing.T) { corrFunc: func(f *os.File) { chunksFormatVersionOffset := int64(4) _, err := f.Seek(chunksFormatVersionOffset, 0) - testutil.Ok(t, err) + assert.NoError(t, err) // Set invalid chunk format version. b := make([]byte, chunks.ChunksFormatVersionSize) b[0] = 0 n, err := f.Write(b) - testutil.Ok(t, err) - testutil.Equals(t, chunks.ChunksFormatVersionSize, n) + assert.NoError(t, err) + assert.Equal(t, chunks.ChunksFormatVersionSize, n) }, openErr: errors.New("invalid chunk format version 0"), }, @@ -137,7 +138,7 @@ func TestCorruptedChunk(t *testing.T) { name: "chunk not enough bytes to read the chunk length", corrFunc: func(f *os.File) { // Truncate one byte after the segment header. - testutil.Ok(t, f.Truncate(chunks.SegmentHeaderSize+1)) + assert.NoError(t, f.Truncate(chunks.SegmentHeaderSize+1)) }, iterErr: errors.New("cannot populate chunk 8: segment doesn't include enough bytes to read the chunk size data field - required:13, available:9"), }, @@ -145,8 +146,8 @@ func TestCorruptedChunk(t *testing.T) { name: "chunk not enough bytes to read the data", corrFunc: func(f *os.File) { fi, err := f.Stat() - testutil.Ok(t, err) - testutil.Ok(t, f.Truncate(fi.Size()-1)) + assert.NoError(t, err) + assert.NoError(t, f.Truncate(fi.Size()-1)) }, iterErr: errors.New("cannot populate chunk 8: segment doesn't include enough bytes to read the chunk - required:26, available:25"), }, @@ -154,59 +155,59 @@ func TestCorruptedChunk(t *testing.T) { name: "checksum mismatch", corrFunc: func(f *os.File) { fi, err := f.Stat() - testutil.Ok(t, err) + assert.NoError(t, err) // Get the chunk data end offset. chkEndOffset := int(fi.Size()) - crc32.Size // Seek to the last byte of chunk data and modify it. _, err = f.Seek(int64(chkEndOffset-1), 0) - testutil.Ok(t, err) + assert.NoError(t, err) n, err := f.Write([]byte("x")) - testutil.Ok(t, err) - testutil.Equals(t, n, 1) + assert.NoError(t, err) + assert.Equal(t, n, 1) }, iterErr: errors.New("cannot populate chunk 8: checksum mismatch expected:cfc0526c, actual:34815eae"), }, } { t.Run(tc.name, func(t *testing.T) { tmpdir, err := ioutil.TempDir("", "test_open_block_chunk_corrupted") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(tmpdir)) + assert.NoError(t, os.RemoveAll(tmpdir)) }() series := storage.NewListSeries(labels.FromStrings("a", "b"), []tsdbutil.Sample{sample{1, 1}}) blockDir := createBlock(t, tmpdir, []storage.Series{series}) files, err := sequenceFiles(chunkDir(blockDir)) - testutil.Ok(t, err) - testutil.Assert(t, len(files) > 0, "No chunk created.") + assert.NoError(t, err) + assert.True(t, len(files) > 0, "No chunk created.") f, err := os.OpenFile(files[0], os.O_RDWR, 0666) - testutil.Ok(t, err) + assert.NoError(t, err) // Apply corruption function. tc.corrFunc(f) - testutil.Ok(t, f.Close()) + assert.NoError(t, f.Close()) // Check open err. b, err := OpenBlock(nil, blockDir, nil) if tc.openErr != nil { - testutil.Equals(t, tc.openErr.Error(), err.Error()) + assert.Equal(t, tc.openErr.Error(), err.Error()) return } - defer func() { testutil.Ok(t, b.Close()) }() + defer func() { assert.NoError(t, b.Close()) }() querier, err := NewBlockQuerier(b, 0, 1) - testutil.Ok(t, err) - defer func() { testutil.Ok(t, querier.Close()) }() + assert.NoError(t, err) + defer func() { assert.NoError(t, querier.Close()) }() set := querier.Select(false, nil, labels.MustNewMatcher(labels.MatchEqual, "a", "b")) // Check chunk errors during iter time. - testutil.Assert(t, set.Next(), "") + assert.True(t, set.Next(), "") it := set.At().Iterator() - testutil.Equals(t, false, it.Next()) - testutil.Equals(t, tc.iterErr.Error(), it.Err().Error()) + assert.Equal(t, false, it.Next()) + assert.Equal(t, tc.iterErr.Error(), it.Err().Error()) }) } } @@ -214,9 +215,9 @@ func TestCorruptedChunk(t *testing.T) { // TestBlockSize ensures that the block size is calculated correctly. func TestBlockSize(t *testing.T) { tmpdir, err := ioutil.TempDir("", "test_blockSize") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(tmpdir)) + assert.NoError(t, os.RemoveAll(tmpdir)) }() var ( @@ -229,39 +230,39 @@ func TestBlockSize(t *testing.T) { { blockDirInit = createBlock(t, tmpdir, genSeries(10, 1, 1, 100)) blockInit, err = OpenBlock(nil, blockDirInit, nil) - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, blockInit.Close()) + assert.NoError(t, blockInit.Close()) }() expSizeInit = blockInit.Size() actSizeInit, err := fileutil.DirSize(blockInit.Dir()) - testutil.Ok(t, err) - testutil.Equals(t, expSizeInit, actSizeInit) + assert.NoError(t, err) + assert.Equal(t, expSizeInit, actSizeInit) } // Delete some series and check the sizes again. { - testutil.Ok(t, blockInit.Delete(1, 10, labels.MustNewMatcher(labels.MatchRegexp, "", ".*"))) + assert.NoError(t, blockInit.Delete(1, 10, labels.MustNewMatcher(labels.MatchRegexp, "", ".*"))) expAfterDelete := blockInit.Size() - testutil.Assert(t, expAfterDelete > expSizeInit, "after a delete the block size should be bigger as the tombstone file should grow %v > %v", expAfterDelete, expSizeInit) + assert.True(t, expAfterDelete > expSizeInit, "after a delete the block size should be bigger as the tombstone file should grow %v > %v", expAfterDelete, expSizeInit) actAfterDelete, err := fileutil.DirSize(blockDirInit) - testutil.Ok(t, err) - testutil.Equals(t, expAfterDelete, actAfterDelete, "after a delete reported block size doesn't match actual disk size") + assert.NoError(t, err) + assert.Equal(t, expAfterDelete, actAfterDelete, "after a delete reported block size doesn't match actual disk size") c, err := NewLeveledCompactor(context.Background(), nil, log.NewNopLogger(), []int64{0}, nil) - testutil.Ok(t, err) + assert.NoError(t, err) blockDirAfterCompact, err := c.Compact(tmpdir, []string{blockInit.Dir()}, nil) - testutil.Ok(t, err) + assert.NoError(t, err) blockAfterCompact, err := OpenBlock(nil, filepath.Join(tmpdir, blockDirAfterCompact.String()), nil) - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, blockAfterCompact.Close()) + assert.NoError(t, blockAfterCompact.Close()) }() expAfterCompact := blockAfterCompact.Size() actAfterCompact, err := fileutil.DirSize(blockAfterCompact.Dir()) - testutil.Ok(t, err) - testutil.Assert(t, actAfterDelete > actAfterCompact, "after a delete and compaction the block size should be smaller %v,%v", actAfterDelete, actAfterCompact) - testutil.Equals(t, expAfterCompact, actAfterCompact, "after a delete and compaction reported block size doesn't match actual disk size") + assert.NoError(t, err) + assert.True(t, actAfterDelete > actAfterCompact, "after a delete and compaction the block size should be smaller %v,%v", actAfterDelete, actAfterCompact) + assert.Equal(t, expAfterCompact, actAfterCompact, "after a delete and compaction reported block size doesn't match actual disk size") } } @@ -284,16 +285,16 @@ func TestReadIndexFormatV1(t *testing.T) { blockDir := filepath.Join("testdata", "index_format_v1") block, err := OpenBlock(nil, blockDir, nil) - testutil.Ok(t, err) + assert.NoError(t, err) q, err := NewBlockQuerier(block, 0, 1000) - testutil.Ok(t, err) - testutil.Equals(t, query(t, q, labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")), + assert.NoError(t, err) + assert.Equal(t, query(t, q, labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")), map[string][]tsdbutil.Sample{`{foo="bar"}`: {sample{t: 1, v: 2}}}) q, err = NewBlockQuerier(block, 0, 1000) - testutil.Ok(t, err) - testutil.Equals(t, query(t, q, labels.MustNewMatcher(labels.MatchNotRegexp, "foo", "^.?$")), + assert.NoError(t, err) + assert.Equal(t, query(t, q, labels.MustNewMatcher(labels.MatchNotRegexp, "foo", "^.?$")), map[string][]tsdbutil.Sample{ `{foo="bar"}`: {sample{t: 1, v: 2}}, `{foo="baz"}`: {sample{t: 3, v: 4}}, @@ -303,26 +304,26 @@ func TestReadIndexFormatV1(t *testing.T) { // createBlock creates a block with given set of series and returns its dir. func createBlock(tb testing.TB, dir string, series []storage.Series) string { blockDir, err := CreateBlock(series, dir, 0, log.NewNopLogger()) - testutil.Ok(tb, err) + assert.NoError(tb, err) return blockDir } func createBlockFromHead(tb testing.TB, dir string, head *Head) string { compactor, err := NewLeveledCompactor(context.Background(), nil, log.NewNopLogger(), []int64{1000000}, nil) - testutil.Ok(tb, err) + assert.NoError(tb, err) - testutil.Ok(tb, os.MkdirAll(dir, 0777)) + assert.NoError(tb, os.MkdirAll(dir, 0777)) // Add +1 millisecond to block maxt because block intervals are half-open: [b.MinTime, b.MaxTime). // Because of this block intervals are always +1 than the total samples it includes. ulid, err := compactor.Write(dir, head, head.MinTime(), head.MaxTime()+1, nil) - testutil.Ok(tb, err) + assert.NoError(tb, err) return filepath.Join(dir, ulid.String()) } func createHead(tb testing.TB, w *wal.WAL, series []storage.Series, chunkDir string) *Head { head, err := NewHead(nil, nil, w, DefaultBlockDuration, chunkDir, nil, DefaultStripeSize, nil) - testutil.Ok(tb, err) + assert.NoError(tb, err) app := head.Appender(context.Background()) for _, s := range series { @@ -337,11 +338,11 @@ func createHead(tb testing.TB, w *wal.WAL, series []storage.Series, chunkDir str } } ref, err = app.Add(s.Labels(), t, v) - testutil.Ok(tb, err) + assert.NoError(tb, err) } - testutil.Ok(tb, it.Err()) + assert.NoError(tb, it.Err()) } - testutil.Ok(tb, app.Commit()) + assert.NoError(tb, app.Commit()) return head } diff --git a/tsdb/blockwriter_test.go b/tsdb/blockwriter_test.go index eaeed677f2..223d9acd39 100644 --- a/tsdb/blockwriter_test.go +++ b/tsdb/blockwriter_test.go @@ -22,46 +22,46 @@ import ( "testing" "github.com/go-kit/kit/log" - "github.com/pkg/errors" + "github.com/stretchr/testify/assert" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/tsdb/tsdbutil" - "github.com/prometheus/prometheus/util/testutil" ) func TestBlockWriter(t *testing.T) { ctx := context.Background() outputDir, err := ioutil.TempDir(os.TempDir(), "output") - testutil.Ok(t, err) + assert.NoError(t, err) w, err := NewBlockWriter(log.NewNopLogger(), outputDir, DefaultBlockDuration) - testutil.Ok(t, err) + assert.NoError(t, err) // Flush with no series results in error. _, err = w.Flush(ctx) - testutil.ErrorEqual(t, err, errors.New("no series appended, aborting")) + assert.EqualError(t, err, "no series appended, aborting") // Add some series. app := w.Appender(ctx) ts1, v1 := int64(44), float64(7) _, err = app.Add(labels.Labels{{Name: "a", Value: "b"}}, ts1, v1) - testutil.Ok(t, err) + assert.NoError(t, err) ts2, v2 := int64(55), float64(12) _, err = app.Add(labels.Labels{{Name: "c", Value: "d"}}, ts2, v2) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) id, err := w.Flush(ctx) - testutil.Ok(t, err) + assert.NoError(t, err) // Confirm the block has the correct data. blockpath := filepath.Join(outputDir, id.String()) b, err := OpenBlock(nil, blockpath, nil) - testutil.Ok(t, err) + assert.NoError(t, err) q, err := NewBlockQuerier(b, math.MinInt64, math.MaxInt64) - testutil.Ok(t, err) + assert.NoError(t, err) series := query(t, q, labels.MustNewMatcher(labels.MatchRegexp, "", ".*")) sample1 := []tsdbutil.Sample{sample{t: ts1, v: v1}} sample2 := []tsdbutil.Sample{sample{t: ts2, v: v2}} expectedSeries := map[string][]tsdbutil.Sample{"{a=\"b\"}": sample1, "{c=\"d\"}": sample2} - testutil.Equals(t, expectedSeries, series) + assert.Equal(t, expectedSeries, series) - testutil.Ok(t, w.Close()) + assert.NoError(t, w.Close()) } diff --git a/tsdb/chunkenc/bstream_test.go b/tsdb/chunkenc/bstream_test.go index 40425b1caf..47604f2a1f 100644 --- a/tsdb/chunkenc/bstream_test.go +++ b/tsdb/chunkenc/bstream_test.go @@ -16,7 +16,7 @@ package chunkenc import ( "testing" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" ) func TestBstreamReader(t *testing.T) { @@ -39,23 +39,23 @@ func TestBstreamReader(t *testing.T) { if err != nil { v, err = r.readBit() } - testutil.Ok(t, err) - testutil.Equals(t, bit, v) + assert.NoError(t, err) + assert.Equal(t, bit, v) } for nbits := uint8(1); nbits <= 64; nbits++ { v, err := r.readBitsFast(nbits) if err != nil { v, err = r.readBits(nbits) } - testutil.Ok(t, err) - testutil.Equals(t, uint64(nbits), v, "nbits=%d", nbits) + assert.NoError(t, err) + assert.Equal(t, uint64(nbits), v, "nbits=%d", nbits) } for v := 1; v < 10000; v += 123 { actual, err := r.readBitsFast(29) if err != nil { actual, err = r.readBits(29) } - testutil.Ok(t, err) - testutil.Equals(t, uint64(v), actual, "v=%d", v) + assert.NoError(t, err) + assert.Equal(t, uint64(v), actual, "v=%d", v) } } diff --git a/tsdb/chunkenc/chunk_test.go b/tsdb/chunkenc/chunk_test.go index 2534a1cdcb..6e81d53a8b 100644 --- a/tsdb/chunkenc/chunk_test.go +++ b/tsdb/chunkenc/chunk_test.go @@ -19,7 +19,7 @@ import ( "math/rand" "testing" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" ) type pair struct { @@ -42,7 +42,7 @@ func TestChunk(t *testing.T) { func testChunk(t *testing.T, c Chunk) { app, err := c.Appender() - testutil.Ok(t, err) + assert.NoError(t, err) var exp []pair var ( @@ -61,7 +61,7 @@ func testChunk(t *testing.T, c Chunk) { // appending to a partially filled chunk. if i%10 == 0 { app, err = c.Appender() - testutil.Ok(t, err) + assert.NoError(t, err) } app.Append(ts, v) @@ -75,8 +75,8 @@ func testChunk(t *testing.T, c Chunk) { ts, v := it1.At() res1 = append(res1, pair{t: ts, v: v}) } - testutil.Ok(t, it1.Err()) - testutil.Equals(t, exp, res1) + assert.NoError(t, it1.Err()) + assert.Equal(t, exp, res1) // 2. Expand second iterator while reusing first one. it2 := c.Iterator(it1) @@ -85,18 +85,18 @@ func testChunk(t *testing.T, c Chunk) { ts, v := it2.At() res2 = append(res2, pair{t: ts, v: v}) } - testutil.Ok(t, it2.Err()) - testutil.Equals(t, exp, res2) + assert.NoError(t, it2.Err()) + assert.Equal(t, exp, res2) // 3. Test iterator Seek. mid := len(exp) / 2 it3 := c.Iterator(nil) var res3 []pair - testutil.Equals(t, true, it3.Seek(exp[mid].t)) + assert.Equal(t, true, it3.Seek(exp[mid].t)) // Below ones should not matter. - testutil.Equals(t, true, it3.Seek(exp[mid].t)) - testutil.Equals(t, true, it3.Seek(exp[mid].t)) + assert.Equal(t, true, it3.Seek(exp[mid].t)) + assert.Equal(t, true, it3.Seek(exp[mid].t)) ts, v = it3.At() res3 = append(res3, pair{t: ts, v: v}) @@ -104,9 +104,9 @@ func testChunk(t *testing.T, c Chunk) { ts, v := it3.At() res3 = append(res3, pair{t: ts, v: v}) } - testutil.Ok(t, it3.Err()) - testutil.Equals(t, exp[mid:], res3) - testutil.Equals(t, false, it3.Seek(exp[len(exp)-1].t+1)) + assert.NoError(t, it3.Err()) + assert.Equal(t, exp[mid:], res3) + assert.Equal(t, false, it3.Seek(exp[len(exp)-1].t+1)) } func benchmarkIterator(b *testing.B, newChunk func() Chunk) { @@ -160,7 +160,7 @@ func benchmarkIterator(b *testing.B, newChunk func() Chunk) { res = append(res, v) } if it.Err() != io.EOF { - testutil.Ok(b, it.Err()) + assert.NoError(b, it.Err()) } res = res[:0] } diff --git a/tsdb/chunks/chunks.go b/tsdb/chunks/chunks.go index 6f7ea09b44..af781e2597 100644 --- a/tsdb/chunks/chunks.go +++ b/tsdb/chunks/chunks.go @@ -27,6 +27,7 @@ import ( "strconv" "github.com/pkg/errors" + "github.com/prometheus/prometheus/tsdb/chunkenc" tsdb_errors "github.com/prometheus/prometheus/tsdb/errors" "github.com/prometheus/prometheus/tsdb/fileutil" diff --git a/tsdb/chunks/chunks_test.go b/tsdb/chunks/chunks_test.go index d9d9524570..847e8a8924 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/util/testutil" + "github.com/stretchr/testify/assert" ) func TestReaderWithInvalidBuffer(t *testing.T) { @@ -24,5 +24,5 @@ func TestReaderWithInvalidBuffer(t *testing.T) { r := &Reader{bs: []ByteSlice{b}} _, err := r.Chunk(0) - testutil.NotOk(t, err) + assert.Error(t, err) } diff --git a/tsdb/chunks/head_chunks.go b/tsdb/chunks/head_chunks.go index 154149d0cc..6f67b8318a 100644 --- a/tsdb/chunks/head_chunks.go +++ b/tsdb/chunks/head_chunks.go @@ -27,10 +27,11 @@ import ( "sync" "github.com/pkg/errors" + "go.uber.org/atomic" + "github.com/prometheus/prometheus/tsdb/chunkenc" tsdb_errors "github.com/prometheus/prometheus/tsdb/errors" "github.com/prometheus/prometheus/tsdb/fileutil" - "go.uber.org/atomic" ) // Head chunk file header fields constants. diff --git a/tsdb/chunks/head_chunks_test.go b/tsdb/chunks/head_chunks_test.go index 3bc47f7bf6..cc8881722f 100644 --- a/tsdb/chunks/head_chunks_test.go +++ b/tsdb/chunks/head_chunks_test.go @@ -22,14 +22,15 @@ import ( "strconv" "testing" + "github.com/stretchr/testify/assert" + "github.com/prometheus/prometheus/tsdb/chunkenc" - "github.com/prometheus/prometheus/util/testutil" ) func TestChunkDiskMapper_WriteChunk_Chunk_IterateChunks(t *testing.T) { hrw := testChunkDiskMapper(t) defer func() { - testutil.Ok(t, hrw.Close()) + assert.NoError(t, hrw.Close()) }() expectedBytes := []byte{} @@ -68,7 +69,7 @@ func TestChunkDiskMapper_WriteChunk_Chunk_IterateChunks(t *testing.T) { // Calculating expected bytes written on disk for first file. firstFileName = hrw.curFile.Name() - testutil.Equals(t, chunkRef(1, nextChunkOffset), chkRef) + assert.Equal(t, chunkRef(1, nextChunkOffset), chkRef) bytesWritten := 0 chkCRC32.Reset() @@ -86,10 +87,10 @@ func TestChunkDiskMapper_WriteChunk_Chunk_IterateChunks(t *testing.T) { expectedBytes = append(expectedBytes, buf[:bytesWritten]...) _, err := chkCRC32.Write(buf[:bytesWritten]) - testutil.Ok(t, err) + assert.NoError(t, err) expectedBytes = append(expectedBytes, chunk.Bytes()...) _, err = chkCRC32.Write(chunk.Bytes()) - testutil.Ok(t, err) + assert.NoError(t, err) expectedBytes = append(expectedBytes, chkCRC32.Sum(nil)...) @@ -103,57 +104,57 @@ func TestChunkDiskMapper_WriteChunk_Chunk_IterateChunks(t *testing.T) { } // Checking on-disk bytes for the first file. - testutil.Assert(t, len(hrw.mmappedChunkFiles) == 3 && len(hrw.closers) == 3, "expected 3 mmapped files, got %d", len(hrw.mmappedChunkFiles)) + assert.True(t, len(hrw.mmappedChunkFiles) == 3 && len(hrw.closers) == 3, "expected 3 mmapped files, got %d", len(hrw.mmappedChunkFiles)) actualBytes, err := ioutil.ReadFile(firstFileName) - testutil.Ok(t, err) + assert.NoError(t, err) // Check header of the segment file. - testutil.Equals(t, MagicHeadChunks, int(binary.BigEndian.Uint32(actualBytes[0:MagicChunksSize]))) - testutil.Equals(t, chunksFormatV1, int(actualBytes[MagicChunksSize])) + assert.Equal(t, MagicHeadChunks, int(binary.BigEndian.Uint32(actualBytes[0:MagicChunksSize]))) + assert.Equal(t, chunksFormatV1, int(actualBytes[MagicChunksSize])) // Remaining chunk data. fileEnd := HeadChunkFileHeaderSize + len(expectedBytes) - testutil.Equals(t, expectedBytes, actualBytes[HeadChunkFileHeaderSize:fileEnd]) + assert.Equal(t, expectedBytes, actualBytes[HeadChunkFileHeaderSize:fileEnd]) // Test for the next chunk header to be all 0s. That marks the end of the file. for _, b := range actualBytes[fileEnd : fileEnd+MaxHeadChunkMetaSize] { - testutil.Equals(t, byte(0), b) + assert.Equal(t, byte(0), b) } // Testing reading of chunks. for _, exp := range expectedData { actChunk, err := hrw.Chunk(exp.chunkRef) - testutil.Ok(t, err) - testutil.Equals(t, exp.chunk.Bytes(), actChunk.Bytes()) + assert.NoError(t, err) + assert.Equal(t, exp.chunk.Bytes(), actChunk.Bytes()) } // Testing IterateAllChunks method. dir := hrw.dir.Name() - testutil.Ok(t, hrw.Close()) + assert.NoError(t, hrw.Close()) hrw, err = NewChunkDiskMapper(dir, chunkenc.NewPool()) - testutil.Ok(t, err) + assert.NoError(t, err) idx := 0 err = hrw.IterateAllChunks(func(seriesRef, chunkRef uint64, mint, maxt int64, numSamples uint16) error { t.Helper() expData := expectedData[idx] - testutil.Equals(t, expData.seriesRef, seriesRef) - testutil.Equals(t, expData.chunkRef, chunkRef) - testutil.Equals(t, expData.maxt, maxt) - testutil.Equals(t, expData.maxt, maxt) - testutil.Equals(t, expData.numSamples, numSamples) + assert.Equal(t, expData.seriesRef, seriesRef) + assert.Equal(t, expData.chunkRef, chunkRef) + assert.Equal(t, expData.maxt, maxt) + assert.Equal(t, expData.maxt, maxt) + assert.Equal(t, expData.numSamples, numSamples) actChunk, err := hrw.Chunk(expData.chunkRef) - testutil.Ok(t, err) - testutil.Equals(t, expData.chunk.Bytes(), actChunk.Bytes()) + assert.NoError(t, err) + assert.Equal(t, expData.chunk.Bytes(), actChunk.Bytes()) idx++ return nil }) - testutil.Ok(t, err) - testutil.Equals(t, len(expectedData), idx) + assert.NoError(t, err) + assert.Equal(t, len(expectedData), idx) } @@ -165,7 +166,7 @@ func TestChunkDiskMapper_WriteChunk_Chunk_IterateChunks(t *testing.T) { func TestChunkDiskMapper_Truncate(t *testing.T) { hrw := testChunkDiskMapper(t) defer func() { - testutil.Ok(t, hrw.Close()) + assert.NoError(t, hrw.Close()) }() timeRange := 0 @@ -178,7 +179,7 @@ func TestChunkDiskMapper_Truncate(t *testing.T) { // Write a chunks to set maxt for the segment. _, err := hrw.WriteChunk(1, int64(mint), int64(maxt), randomChunk(t)) - testutil.Ok(t, err) + assert.NoError(t, err) timeRange += fileTimeStep @@ -189,20 +190,20 @@ func TestChunkDiskMapper_Truncate(t *testing.T) { t.Helper() files, err := ioutil.ReadDir(hrw.dir.Name()) - testutil.Ok(t, err) - testutil.Equals(t, len(remainingFiles), len(files), "files on disk") - testutil.Equals(t, len(remainingFiles), len(hrw.mmappedChunkFiles), "hrw.mmappedChunkFiles") - testutil.Equals(t, len(remainingFiles), len(hrw.closers), "closers") + assert.NoError(t, err) + assert.Equal(t, len(remainingFiles), len(files), "files on disk") + assert.Equal(t, len(remainingFiles), len(hrw.mmappedChunkFiles), "hrw.mmappedChunkFiles") + assert.Equal(t, len(remainingFiles), len(hrw.closers), "closers") for _, i := range remainingFiles { _, ok := hrw.mmappedChunkFiles[i] - testutil.Equals(t, true, ok) + assert.Equal(t, true, ok) } } // Create segments 1 to 7. for i := 1; i <= 7; i++ { - testutil.Ok(t, hrw.CutNewFile()) + assert.NoError(t, hrw.CutNewFile()) mint := int64(addChunk()) if i == 3 { thirdFileMinT = mint @@ -213,20 +214,20 @@ func TestChunkDiskMapper_Truncate(t *testing.T) { verifyFiles([]int{1, 2, 3, 4, 5, 6, 7}) // Truncating files. - testutil.Ok(t, hrw.Truncate(thirdFileMinT)) + assert.NoError(t, hrw.Truncate(thirdFileMinT)) verifyFiles([]int{3, 4, 5, 6, 7, 8}) dir := hrw.dir.Name() - testutil.Ok(t, hrw.Close()) + assert.NoError(t, hrw.Close()) // Restarted. var err error hrw, err = NewChunkDiskMapper(dir, chunkenc.NewPool()) - testutil.Ok(t, err) + assert.NoError(t, err) - testutil.Assert(t, !hrw.fileMaxtSet, "") - testutil.Ok(t, hrw.IterateAllChunks(func(_, _ uint64, _, _ int64, _ uint16) error { return nil })) - testutil.Assert(t, hrw.fileMaxtSet, "") + assert.True(t, !hrw.fileMaxtSet, "") + assert.NoError(t, hrw.IterateAllChunks(func(_, _ uint64, _, _ int64, _ uint16) error { return nil })) + assert.True(t, hrw.fileMaxtSet, "") verifyFiles([]int{3, 4, 5, 6, 7, 8}) // New file is created after restart even if last file was empty. @@ -234,16 +235,16 @@ func TestChunkDiskMapper_Truncate(t *testing.T) { verifyFiles([]int{3, 4, 5, 6, 7, 8, 9}) // Truncating files after restart. - testutil.Ok(t, hrw.Truncate(sixthFileMinT)) + assert.NoError(t, hrw.Truncate(sixthFileMinT)) verifyFiles([]int{6, 7, 8, 9, 10}) // As the last file was empty, this creates no new files. - testutil.Ok(t, hrw.Truncate(sixthFileMinT+1)) + assert.NoError(t, hrw.Truncate(sixthFileMinT+1)) verifyFiles([]int{6, 7, 8, 9, 10}) addChunk() // Truncating till current time should not delete the current active file. - testutil.Ok(t, hrw.Truncate(int64(timeRange+(2*fileTimeStep)))) + assert.NoError(t, hrw.Truncate(int64(timeRange+(2*fileTimeStep)))) verifyFiles([]int{10, 11}) // One file is the previously active file and one currently created. } @@ -254,7 +255,7 @@ func TestChunkDiskMapper_Truncate(t *testing.T) { func TestChunkDiskMapper_Truncate_PreservesFileSequence(t *testing.T) { hrw := testChunkDiskMapper(t) defer func() { - testutil.Ok(t, hrw.Close()) + assert.NoError(t, hrw.Close()) }() timeRange := 0 @@ -262,11 +263,11 @@ func TestChunkDiskMapper_Truncate_PreservesFileSequence(t *testing.T) { step := 100 mint, maxt := timeRange+1, timeRange+step-1 _, err := hrw.WriteChunk(1, int64(mint), int64(maxt), randomChunk(t)) - testutil.Ok(t, err) + assert.NoError(t, err) timeRange += step } emptyFile := func() { - testutil.Ok(t, hrw.CutNewFile()) + assert.NoError(t, hrw.CutNewFile()) } nonEmptyFile := func() { emptyFile() @@ -284,14 +285,14 @@ func TestChunkDiskMapper_Truncate_PreservesFileSequence(t *testing.T) { t.Helper() files, err := ioutil.ReadDir(hrw.dir.Name()) - testutil.Ok(t, err) - testutil.Equals(t, len(remainingFiles), len(files), "files on disk") - testutil.Equals(t, len(remainingFiles), len(hrw.mmappedChunkFiles), "hrw.mmappedChunkFiles") - testutil.Equals(t, len(remainingFiles), len(hrw.closers), "closers") + assert.NoError(t, err) + assert.Equal(t, len(remainingFiles), len(files), "files on disk") + assert.Equal(t, len(remainingFiles), len(hrw.mmappedChunkFiles), "hrw.mmappedChunkFiles") + assert.Equal(t, len(remainingFiles), len(hrw.closers), "closers") for _, i := range remainingFiles { _, ok := hrw.mmappedChunkFiles[i] - testutil.Assert(t, ok, "remaining file %d not in hrw.mmappedChunkFiles", i) + assert.True(t, ok, "remaining file %d not in hrw.mmappedChunkFiles", i) } } @@ -300,22 +301,22 @@ func TestChunkDiskMapper_Truncate_PreservesFileSequence(t *testing.T) { // Truncating files till 2. It should not delete anything after 3 (inclusive) // though files 4 and 6 are empty. file2Maxt := hrw.mmappedChunkFiles[2].maxt - testutil.Ok(t, hrw.Truncate(file2Maxt+1)) + assert.NoError(t, hrw.Truncate(file2Maxt+1)) // As 6 was empty, it should not create another file. verifyFiles([]int{3, 4, 5, 6}) addChunk() // Truncate creates another file as 6 is not empty now. - testutil.Ok(t, hrw.Truncate(file2Maxt+1)) + assert.NoError(t, hrw.Truncate(file2Maxt+1)) verifyFiles([]int{3, 4, 5, 6, 7}) dir := hrw.dir.Name() - testutil.Ok(t, hrw.Close()) + assert.NoError(t, hrw.Close()) // Restarting checks for unsequential files. var err error hrw, err = NewChunkDiskMapper(dir, chunkenc.NewPool()) - testutil.Ok(t, err) + assert.NoError(t, err) verifyFiles([]int{3, 4, 5, 6, 7}) } @@ -324,33 +325,33 @@ func TestChunkDiskMapper_Truncate_PreservesFileSequence(t *testing.T) { func TestHeadReadWriter_TruncateAfterFailedIterateChunks(t *testing.T) { hrw := testChunkDiskMapper(t) defer func() { - testutil.Ok(t, hrw.Close()) + assert.NoError(t, hrw.Close()) }() // Write a chunks to iterate on it later. _, err := hrw.WriteChunk(1, 0, 1000, randomChunk(t)) - testutil.Ok(t, err) + assert.NoError(t, err) dir := hrw.dir.Name() - testutil.Ok(t, hrw.Close()) + assert.NoError(t, hrw.Close()) // Restarting to recreate https://github.com/prometheus/prometheus/issues/7753. hrw, err = NewChunkDiskMapper(dir, chunkenc.NewPool()) - testutil.Ok(t, err) + assert.NoError(t, err) // Forcefully failing IterateAllChunks. - testutil.NotOk(t, hrw.IterateAllChunks(func(_, _ uint64, _, _ int64, _ uint16) error { + assert.Error(t, hrw.IterateAllChunks(func(_, _ uint64, _, _ int64, _ uint16) error { return errors.New("random error") })) // Truncation call should not return error after IterateAllChunks fails. - testutil.Ok(t, hrw.Truncate(2000)) + assert.NoError(t, hrw.Truncate(2000)) } func TestHeadReadWriter_ReadRepairOnEmptyLastFile(t *testing.T) { hrw := testChunkDiskMapper(t) defer func() { - testutil.Ok(t, hrw.Close()) + assert.NoError(t, hrw.Close()) }() timeRange := 0 @@ -358,11 +359,11 @@ func TestHeadReadWriter_ReadRepairOnEmptyLastFile(t *testing.T) { step := 100 mint, maxt := timeRange+1, timeRange+step-1 _, err := hrw.WriteChunk(1, int64(mint), int64(maxt), randomChunk(t)) - testutil.Ok(t, err) + assert.NoError(t, err) timeRange += step } nonEmptyFile := func() { - testutil.Ok(t, hrw.CutNewFile()) + assert.NoError(t, hrw.CutNewFile()) addChunk() } @@ -370,64 +371,64 @@ func TestHeadReadWriter_ReadRepairOnEmptyLastFile(t *testing.T) { nonEmptyFile() // 2. nonEmptyFile() // 3. - testutil.Equals(t, 3, len(hrw.mmappedChunkFiles)) + assert.Equal(t, 3, len(hrw.mmappedChunkFiles)) lastFile := 0 for idx := range hrw.mmappedChunkFiles { if idx > lastFile { lastFile = idx } } - testutil.Equals(t, 3, lastFile) + assert.Equal(t, 3, lastFile) dir := hrw.dir.Name() - testutil.Ok(t, hrw.Close()) + assert.NoError(t, hrw.Close()) // Write an empty last file mimicking an abrupt shutdown on file creation. emptyFileName := segmentFile(dir, lastFile+1) f, err := os.OpenFile(emptyFileName, os.O_WRONLY|os.O_CREATE, 0666) - testutil.Ok(t, err) - testutil.Ok(t, f.Sync()) + assert.NoError(t, err) + assert.NoError(t, f.Sync()) stat, err := f.Stat() - testutil.Ok(t, err) - testutil.Equals(t, int64(0), stat.Size()) - testutil.Ok(t, f.Close()) + assert.NoError(t, err) + assert.Equal(t, int64(0), stat.Size()) + assert.NoError(t, f.Close()) // Open chunk disk mapper again, corrupt file should be removed. hrw, err = NewChunkDiskMapper(dir, chunkenc.NewPool()) - testutil.Ok(t, err) - testutil.Assert(t, !hrw.fileMaxtSet, "") - testutil.Ok(t, hrw.IterateAllChunks(func(_, _ uint64, _, _ int64, _ uint16) error { return nil })) - testutil.Assert(t, hrw.fileMaxtSet, "") + assert.NoError(t, err) + assert.True(t, !hrw.fileMaxtSet, "") + assert.NoError(t, hrw.IterateAllChunks(func(_, _ uint64, _, _ int64, _ uint16) error { return nil })) + assert.True(t, hrw.fileMaxtSet, "") // Removed from memory. - testutil.Equals(t, 3, len(hrw.mmappedChunkFiles)) + assert.Equal(t, 3, len(hrw.mmappedChunkFiles)) for idx := range hrw.mmappedChunkFiles { - testutil.Assert(t, idx <= lastFile, "file index is bigger than previous last file") + assert.True(t, idx <= lastFile, "file index is bigger than previous last file") } // Removed even from disk. files, err := ioutil.ReadDir(dir) - testutil.Ok(t, err) - testutil.Equals(t, 3, len(files)) + assert.NoError(t, err) + assert.Equal(t, 3, len(files)) for _, fi := range files { seq, err := strconv.ParseUint(fi.Name(), 10, 64) - testutil.Ok(t, err) - testutil.Assert(t, seq <= uint64(lastFile), "file index on disk is bigger than previous last file") + assert.NoError(t, err) + assert.True(t, seq <= uint64(lastFile), "file index on disk is bigger than previous last file") } } func testChunkDiskMapper(t *testing.T) *ChunkDiskMapper { tmpdir, err := ioutil.TempDir("", "data") - testutil.Ok(t, err) + assert.NoError(t, err) t.Cleanup(func() { - testutil.Ok(t, os.RemoveAll(tmpdir)) + assert.NoError(t, os.RemoveAll(tmpdir)) }) hrw, err := NewChunkDiskMapper(tmpdir, chunkenc.NewPool()) - testutil.Ok(t, err) - testutil.Assert(t, !hrw.fileMaxtSet, "") - testutil.Ok(t, hrw.IterateAllChunks(func(_, _ uint64, _, _ int64, _ uint16) error { return nil })) - testutil.Assert(t, hrw.fileMaxtSet, "") + assert.NoError(t, err) + assert.True(t, !hrw.fileMaxtSet, "") + assert.NoError(t, hrw.IterateAllChunks(func(_, _ uint64, _, _ int64, _ uint16) error { return nil })) + assert.True(t, hrw.fileMaxtSet, "") return hrw } @@ -435,7 +436,7 @@ func randomChunk(t *testing.T) chunkenc.Chunk { chunk := chunkenc.NewXORChunk() len := rand.Int() % 120 app, err := chunk.Appender() - testutil.Ok(t, err) + assert.NoError(t, err) for i := 0; i < len; i++ { app.Append(rand.Int63(), rand.Float64()) } @@ -449,6 +450,6 @@ func createChunk(t *testing.T, idx int, hrw *ChunkDiskMapper) (seriesRef uint64, maxt = int64((idx + 1) * 1000) chunk = randomChunk(t) chunkRef, err = hrw.WriteChunk(seriesRef, mint, maxt, chunk) - testutil.Ok(t, err) + assert.NoError(t, err) return } diff --git a/tsdb/compact.go b/tsdb/compact.go index fbc5b35833..cd70ee52db 100644 --- a/tsdb/compact.go +++ b/tsdb/compact.go @@ -29,6 +29,7 @@ import ( "github.com/oklog/ulid" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/prometheus/storage" "github.com/prometheus/prometheus/tsdb/chunkenc" "github.com/prometheus/prometheus/tsdb/chunks" diff --git a/tsdb/compact_test.go b/tsdb/compact_test.go index 9ba0c64499..25d492b01e 100644 --- a/tsdb/compact_test.go +++ b/tsdb/compact_test.go @@ -27,12 +27,13 @@ import ( "github.com/go-kit/kit/log" "github.com/pkg/errors" prom_testutil "github.com/prometheus/client_golang/prometheus/testutil" + "github.com/stretchr/testify/assert" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/tsdb/chunkenc" "github.com/prometheus/prometheus/tsdb/chunks" "github.com/prometheus/prometheus/tsdb/fileutil" "github.com/prometheus/prometheus/tsdb/tombstones" - "github.com/prometheus/prometheus/util/testutil" ) func TestSplitByRange(t *testing.T) { @@ -134,7 +135,7 @@ func TestSplitByRange(t *testing.T) { } } - testutil.Equals(t, exp, splitByRange(blocks, c.trange)) + assert.Equal(t, exp, splitByRange(blocks, c.trange)) } } @@ -158,7 +159,7 @@ func TestNoPanicFor0Tombstones(t *testing.T) { } c, err := NewLeveledCompactor(context.Background(), nil, nil, []int64{50}, nil) - testutil.Ok(t, err) + assert.NoError(t, err) c.plan(metas) } @@ -172,7 +173,7 @@ func TestLeveledCompactor_plan(t *testing.T) { 540, 1620, }, nil) - testutil.Ok(t, err) + assert.NoError(t, err) cases := map[string]struct { metas []dirMeta @@ -365,8 +366,8 @@ func TestLeveledCompactor_plan(t *testing.T) { for title, c := range cases { if !t.Run(title, func(t *testing.T) { res, err := compactor.plan(c.metas) - testutil.Ok(t, err) - testutil.Equals(t, c.expected, res) + assert.NoError(t, err) + assert.Equal(t, c.expected, res) }) { return } @@ -381,7 +382,7 @@ func TestRangeWithFailedCompactionWontGetSelected(t *testing.T) { 720, 2160, }, nil) - testutil.Ok(t, err) + assert.NoError(t, err) cases := []struct { metas []dirMeta @@ -417,9 +418,9 @@ func TestRangeWithFailedCompactionWontGetSelected(t *testing.T) { for _, c := range cases { c.metas[1].meta.Compaction.Failed = true res, err := compactor.plan(c.metas) - testutil.Ok(t, err) + assert.NoError(t, err) - testutil.Equals(t, []string(nil), res) + assert.Equal(t, []string(nil), res) } } @@ -431,17 +432,17 @@ func TestCompactionFailWillCleanUpTempDir(t *testing.T) { 720, 2160, }, nil) - testutil.Ok(t, err) + assert.NoError(t, err) tmpdir, err := ioutil.TempDir("", "test") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(tmpdir)) + assert.NoError(t, os.RemoveAll(tmpdir)) }() - testutil.NotOk(t, compactor.write(tmpdir, &BlockMeta{}, erringBReader{})) + assert.Error(t, compactor.write(tmpdir, &BlockMeta{}, erringBReader{})) _, err = os.Stat(filepath.Join(tmpdir, BlockMeta{}.ULID.String()) + tmpForCreationBlockDirSuffix) - testutil.Assert(t, os.IsNotExist(err), "directory is not cleaned up") + assert.True(t, os.IsNotExist(err), "directory is not cleaned up") } func metaRange(name string, mint, maxt int64, stats *BlockStats) dirMeta { @@ -940,7 +941,7 @@ func TestCompaction_populateBlock(t *testing.T) { } c, err := NewLeveledCompactor(context.Background(), nil, nil, []int64{0}, nil) - testutil.Ok(t, err) + assert.NoError(t, err) meta := &BlockMeta{ MinTime: tc.compactMinTime, @@ -953,11 +954,11 @@ func TestCompaction_populateBlock(t *testing.T) { iw := &mockIndexWriter{} err = c.populateBlock(blocks, meta, iw, nopChunkWriter{}) if tc.expErr != nil { - testutil.NotOk(t, err) - testutil.Equals(t, tc.expErr.Error(), err.Error()) + assert.Error(t, err) + assert.Equal(t, tc.expErr.Error(), err.Error()) return } - testutil.Ok(t, err) + assert.NoError(t, err) // Check if response is expected and chunk is valid. var raw []seriesSamples @@ -980,15 +981,15 @@ func TestCompaction_populateBlock(t *testing.T) { } // Check if chunk has correct min, max times. - testutil.Equals(t, firstTs, chk.MinTime, "chunk Meta %v does not match the first encoded sample timestamp: %v", chk, firstTs) - testutil.Equals(t, s.t, chk.MaxTime, "chunk Meta %v does not match the last encoded sample timestamp %v", chk, s.t) + assert.Equal(t, firstTs, chk.MinTime, "chunk Meta %v does not match the first encoded sample timestamp: %v", chk, firstTs) + assert.Equal(t, s.t, chk.MaxTime, "chunk Meta %v does not match the last encoded sample timestamp %v", chk, s.t) - testutil.Ok(t, iter.Err()) + assert.NoError(t, iter.Err()) ss.chunks = append(ss.chunks, samples) } raw = append(raw, ss) } - testutil.Equals(t, tc.expSeriesSamples, raw) + assert.Equal(t, tc.expSeriesSamples, raw) // Check if stats are calculated properly. s := BlockStats{NumSeries: uint64(len(tc.expSeriesSamples))} @@ -998,7 +999,7 @@ func TestCompaction_populateBlock(t *testing.T) { s.NumSamples += uint64(len(chk)) } } - testutil.Equals(t, s, meta.Stats) + assert.Equal(t, s, meta.Stats) }) } } @@ -1048,30 +1049,30 @@ func BenchmarkCompaction(b *testing.B) { nBlocks := len(c.ranges) b.Run(fmt.Sprintf("type=%s,blocks=%d,series=%d,samplesPerSeriesPerBlock=%d", c.compactionType, nBlocks, nSeries, c.ranges[0][1]-c.ranges[0][0]+1), func(b *testing.B) { dir, err := ioutil.TempDir("", "bench_compaction") - testutil.Ok(b, err) + assert.NoError(b, err) defer func() { - testutil.Ok(b, os.RemoveAll(dir)) + assert.NoError(b, os.RemoveAll(dir)) }() blockDirs := make([]string, 0, len(c.ranges)) var blocks []*Block for _, r := range c.ranges { block, err := OpenBlock(nil, createBlock(b, dir, genSeries(nSeries, 10, r[0], r[1])), nil) - testutil.Ok(b, err) + assert.NoError(b, err) blocks = append(blocks, block) defer func() { - testutil.Ok(b, block.Close()) + assert.NoError(b, block.Close()) }() blockDirs = append(blockDirs, block.Dir()) } c, err := NewLeveledCompactor(context.Background(), nil, log.NewNopLogger(), []int64{0}, nil) - testutil.Ok(b, err) + assert.NoError(b, err) b.ResetTimer() b.ReportAllocs() for i := 0; i < b.N; i++ { _, err = c.Compact(dir, blockDirs, blocks) - testutil.Ok(b, err) + assert.NoError(b, err) } }) } @@ -1079,27 +1080,27 @@ func BenchmarkCompaction(b *testing.B) { func BenchmarkCompactionFromHead(b *testing.B) { dir, err := ioutil.TempDir("", "bench_compaction_from_head") - testutil.Ok(b, err) + assert.NoError(b, err) defer func() { - testutil.Ok(b, os.RemoveAll(dir)) + assert.NoError(b, os.RemoveAll(dir)) }() totalSeries := 100000 for labelNames := 1; labelNames < totalSeries; labelNames *= 10 { labelValues := totalSeries / labelNames b.Run(fmt.Sprintf("labelnames=%d,labelvalues=%d", labelNames, labelValues), func(b *testing.B) { chunkDir, err := ioutil.TempDir("", "chunk_dir") - testutil.Ok(b, err) + assert.NoError(b, err) defer func() { - testutil.Ok(b, os.RemoveAll(chunkDir)) + assert.NoError(b, os.RemoveAll(chunkDir)) }() h, err := NewHead(nil, nil, nil, 1000, chunkDir, nil, DefaultStripeSize, nil) - testutil.Ok(b, err) + assert.NoError(b, err) for ln := 0; ln < labelNames; ln++ { app := h.Appender(context.Background()) for lv := 0; lv < labelValues; lv++ { app.Add(labels.FromStrings(fmt.Sprintf("%d", ln), fmt.Sprintf("%d%s%d", lv, postingsBenchSuffix, ln)), 0, 0) } - testutil.Ok(b, app.Commit()) + assert.NoError(b, app.Commit()) } b.ResetTimer() @@ -1119,7 +1120,7 @@ func BenchmarkCompactionFromHead(b *testing.B) { func TestDisableAutoCompactions(t *testing.T) { db := openTestDB(t, nil, nil) defer func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }() blockRange := db.compactor.(*LeveledCompactor).ranges[0] @@ -1131,11 +1132,11 @@ func TestDisableAutoCompactions(t *testing.T) { app := db.Appender(context.Background()) for i := int64(0); i < 3; i++ { _, err := app.Add(label, i*blockRange, 0) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = app.Add(label, i*blockRange+1000, 0) - testutil.Ok(t, err) + assert.NoError(t, err) } - testutil.Ok(t, app.Commit()) + assert.NoError(t, app.Commit()) select { case db.compactc <- struct{}{}: @@ -1149,8 +1150,8 @@ func TestDisableAutoCompactions(t *testing.T) { time.Sleep(10 * time.Millisecond) } - testutil.Assert(t, prom_testutil.ToFloat64(db.metrics.compactionsSkipped) > 0.0, "No compaction was skipped after the set timeout.") - testutil.Equals(t, 0, len(db.blocks)) + assert.True(t, prom_testutil.ToFloat64(db.metrics.compactionsSkipped) > 0.0, "No compaction was skipped after the set timeout.") + assert.Equal(t, 0, len(db.blocks)) // Enable the compaction, trigger it and check that the block is persisted. db.EnableCompactions() @@ -1164,16 +1165,16 @@ func TestDisableAutoCompactions(t *testing.T) { } time.Sleep(100 * time.Millisecond) } - testutil.Assert(t, len(db.Blocks()) > 0, "No block was persisted after the set timeout.") + assert.True(t, len(db.Blocks()) > 0, "No block was persisted after the set timeout.") } // TestCancelCompactions ensures that when the db is closed // any running compaction is cancelled to unblock closing the db. func TestCancelCompactions(t *testing.T) { tmpdir, err := ioutil.TempDir("", "testCancelCompaction") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(tmpdir)) + assert.NoError(t, os.RemoveAll(tmpdir)) }() // Create some blocks to fall within the compaction range. @@ -1184,18 +1185,18 @@ func TestCancelCompactions(t *testing.T) { // Copy the db so we have an exact copy to compare compaction times. tmpdirCopy := tmpdir + "Copy" err = fileutil.CopyDirs(tmpdir, tmpdirCopy) - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(tmpdirCopy)) + assert.NoError(t, os.RemoveAll(tmpdirCopy)) }() // Measure the compaction time without interrupting it. var timeCompactionUninterrupted time.Duration { db, err := open(tmpdir, log.NewNopLogger(), nil, DefaultOptions(), []int64{1, 2000}) - testutil.Ok(t, err) - testutil.Equals(t, 3, len(db.Blocks()), "initial block count mismatch") - testutil.Equals(t, 0.0, prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.ran), "initial compaction counter mismatch") + assert.NoError(t, err) + assert.Equal(t, 3, len(db.Blocks()), "initial block count mismatch") + assert.Equal(t, 0.0, prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.ran), "initial compaction counter mismatch") db.compactc <- struct{}{} // Trigger a compaction. var start time.Time for prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.populatingBlocks) <= 0 { @@ -1208,14 +1209,14 @@ func TestCancelCompactions(t *testing.T) { } timeCompactionUninterrupted = time.Since(start) - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) } // Measure the compaction time when closing the db in the middle of compaction. { db, err := open(tmpdirCopy, log.NewNopLogger(), nil, DefaultOptions(), []int64{1, 2000}) - testutil.Ok(t, err) - testutil.Equals(t, 3, len(db.Blocks()), "initial block count mismatch") - testutil.Equals(t, 0.0, prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.ran), "initial compaction counter mismatch") + assert.NoError(t, err) + assert.Equal(t, 3, len(db.Blocks()), "initial block count mismatch") + assert.Equal(t, 0.0, prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.ran), "initial compaction counter mismatch") db.compactc <- struct{}{} // Trigger a compaction. dbClosed := make(chan struct{}) @@ -1223,7 +1224,7 @@ func TestCancelCompactions(t *testing.T) { time.Sleep(3 * time.Millisecond) } go func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) close(dbClosed) }() @@ -1231,7 +1232,7 @@ func TestCancelCompactions(t *testing.T) { <-dbClosed actT := time.Since(start) expT := time.Duration(timeCompactionUninterrupted / 2) // Closing the db in the middle of compaction should less than half the time. - testutil.Assert(t, actT < expT, "closing the db took more than expected. exp: <%v, act: %v", expT, actT) + assert.True(t, actT < expT, "closing the db took more than expected. exp: <%v, act: %v", expT, actT) } } @@ -1246,12 +1247,12 @@ func TestDeleteCompactionBlockAfterFailedReload(t *testing.T) { // Add some data to the head that is enough to trigger a compaction. app := db.Appender(context.Background()) _, err := app.Add(defaultLabel, 1, 0) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = app.Add(defaultLabel, 2, 0) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = app.Add(defaultLabel, 3+rangeToTriggerCompaction, 0) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) return 0 }, @@ -1264,8 +1265,8 @@ func TestDeleteCompactionBlockAfterFailedReload(t *testing.T) { for _, m := range blocks { createBlock(t, db.Dir(), genSeries(1, 1, m.MinTime, m.MaxTime)) } - testutil.Ok(t, db.reload()) - testutil.Equals(t, len(blocks), len(db.Blocks()), "unexpected block count after a reloadBlocks") + assert.NoError(t, db.reload()) + assert.Equal(t, len(blocks), len(db.Blocks()), "unexpected block count after a reloadBlocks") return len(blocks) }, @@ -1275,7 +1276,7 @@ func TestDeleteCompactionBlockAfterFailedReload(t *testing.T) { t.Run(title, func(t *testing.T) { db := openTestDB(t, nil, []int64{1, 100}) defer func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }() db.DisableCompactions() @@ -1285,25 +1286,25 @@ func TestDeleteCompactionBlockAfterFailedReload(t *testing.T) { blockPath := createBlock(t, db.Dir(), genSeries(1, 1, 200, 300)) lastBlockIndex := path.Join(blockPath, indexFilename) actBlocks, err := blockDirs(db.Dir()) - testutil.Ok(t, err) - testutil.Equals(t, expBlocks, len(actBlocks)-1) // -1 to exclude the corrupted block. - testutil.Ok(t, os.RemoveAll(lastBlockIndex)) // Corrupt the block by removing the index file. + assert.NoError(t, err) + assert.Equal(t, expBlocks, len(actBlocks)-1) // -1 to exclude the corrupted block. + assert.NoError(t, os.RemoveAll(lastBlockIndex)) // Corrupt the block by removing the index file. - testutil.Equals(t, 0.0, prom_testutil.ToFloat64(db.metrics.reloadsFailed), "initial 'failed db reloadBlocks' count metrics mismatch") - testutil.Equals(t, 0.0, prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.ran), "initial `compactions` count metric mismatch") - testutil.Equals(t, 0.0, prom_testutil.ToFloat64(db.metrics.compactionsFailed), "initial `compactions failed` count metric mismatch") + assert.Equal(t, 0.0, prom_testutil.ToFloat64(db.metrics.reloadsFailed), "initial 'failed db reloadBlocks' count metrics mismatch") + assert.Equal(t, 0.0, prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.ran), "initial `compactions` count metric mismatch") + assert.Equal(t, 0.0, prom_testutil.ToFloat64(db.metrics.compactionsFailed), "initial `compactions failed` count metric mismatch") // Do the compaction and check the metrics. // Compaction should succeed, but the reloadBlocks should fail and // the new block created from the compaction should be deleted. - testutil.NotOk(t, db.Compact()) - testutil.Equals(t, 1.0, prom_testutil.ToFloat64(db.metrics.reloadsFailed), "'failed db reloadBlocks' count metrics mismatch") - testutil.Equals(t, 1.0, prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.ran), "`compaction` count metric mismatch") - testutil.Equals(t, 1.0, prom_testutil.ToFloat64(db.metrics.compactionsFailed), "`compactions failed` count metric mismatch") + assert.Error(t, db.Compact()) + assert.Equal(t, 1.0, prom_testutil.ToFloat64(db.metrics.reloadsFailed), "'failed db reloadBlocks' count metrics mismatch") + assert.Equal(t, 1.0, prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.ran), "`compaction` count metric mismatch") + assert.Equal(t, 1.0, prom_testutil.ToFloat64(db.metrics.compactionsFailed), "`compactions failed` count metric mismatch") actBlocks, err = blockDirs(db.Dir()) - testutil.Ok(t, err) - testutil.Equals(t, expBlocks, len(actBlocks)-1, "block count should be the same as before the compaction") // -1 to exclude the corrupted block. + assert.NoError(t, err) + assert.Equal(t, expBlocks, len(actBlocks)-1, "block count should be the same as before the compaction") // -1 to exclude the corrupted block. }) } } diff --git a/tsdb/db.go b/tsdb/db.go index 5be60b670b..3263320fcb 100644 --- a/tsdb/db.go +++ b/tsdb/db.go @@ -34,16 +34,15 @@ import ( "github.com/oklog/ulid" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" + "golang.org/x/sync/errgroup" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/storage" "github.com/prometheus/prometheus/tsdb/chunkenc" tsdb_errors "github.com/prometheus/prometheus/tsdb/errors" "github.com/prometheus/prometheus/tsdb/fileutil" + _ "github.com/prometheus/prometheus/tsdb/goversion" // Load the package into main to make sure minium Go version is met. "github.com/prometheus/prometheus/tsdb/wal" - "golang.org/x/sync/errgroup" - - // Load the package into main to make sure minium Go version is met. - _ "github.com/prometheus/prometheus/tsdb/goversion" ) const ( diff --git a/tsdb/db_test.go b/tsdb/db_test.go index 023db63af0..7e6a93fded 100644 --- a/tsdb/db_test.go +++ b/tsdb/db_test.go @@ -36,6 +36,9 @@ import ( "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" prom_testutil "github.com/prometheus/client_golang/prometheus/testutil" + "github.com/stretchr/testify/assert" + "go.uber.org/goleak" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/storage" "github.com/prometheus/prometheus/tsdb/chunks" @@ -46,7 +49,6 @@ import ( "github.com/prometheus/prometheus/tsdb/tsdbutil" "github.com/prometheus/prometheus/tsdb/wal" "github.com/prometheus/prometheus/util/testutil" - "go.uber.org/goleak" ) func TestMain(m *testing.M) { @@ -55,7 +57,7 @@ func TestMain(m *testing.M) { func openTestDB(t testing.TB, opts *Options, rngs []int64) (db *DB) { tmpdir, err := ioutil.TempDir("", "test") - testutil.Ok(t, err) + assert.NoError(t, err) if len(rngs) == 0 { db, err = Open(tmpdir, nil, nil, opts) @@ -63,11 +65,11 @@ func openTestDB(t testing.TB, opts *Options, rngs []int64) (db *DB) { opts, rngs = validateOpts(opts, rngs) db, err = open(tmpdir, nil, nil, opts, rngs) } - testutil.Ok(t, err) + assert.NoError(t, err) // Do not Close() the test database by default as it will deadlock on test failures. t.Cleanup(func() { - testutil.Ok(t, os.RemoveAll(tmpdir)) + assert.NoError(t, os.RemoveAll(tmpdir)) }) return db } @@ -76,7 +78,7 @@ func openTestDB(t testing.TB, opts *Options, rngs []int64) (db *DB) { func query(t testing.TB, q storage.Querier, matchers ...*labels.Matcher) map[string][]tsdbutil.Sample { ss := q.Select(false, nil, matchers...) defer func() { - testutil.Ok(t, q.Close()) + assert.NoError(t, q.Close()) }() result := map[string][]tsdbutil.Sample{} @@ -89,7 +91,7 @@ func query(t testing.TB, q storage.Querier, matchers ...*labels.Matcher) map[str t, v := it.At() samples = append(samples, sample{t: t, v: v}) } - testutil.Ok(t, it.Err()) + assert.NoError(t, it.Err()) if len(samples) == 0 { continue @@ -98,8 +100,8 @@ func query(t testing.TB, q storage.Querier, matchers ...*labels.Matcher) map[str name := series.Labels().String() result[name] = samples } - testutil.Ok(t, ss.Err()) - testutil.Equals(t, 0, len(ss.Warnings())) + assert.NoError(t, ss.Err()) + assert.Equal(t, 0, len(ss.Warnings())) return result } @@ -108,7 +110,7 @@ func query(t testing.TB, q storage.Querier, matchers ...*labels.Matcher) map[str func queryChunks(t testing.TB, q storage.ChunkQuerier, matchers ...*labels.Matcher) map[string][]chunks.Meta { ss := q.Select(false, nil, matchers...) defer func() { - testutil.Ok(t, q.Close()) + assert.NoError(t, q.Close()) }() result := map[string][]chunks.Meta{} @@ -120,7 +122,7 @@ func queryChunks(t testing.TB, q storage.ChunkQuerier, matchers ...*labels.Match for it.Next() { chks = append(chks, it.At()) } - testutil.Ok(t, it.Err()) + assert.NoError(t, it.Err()) if len(chks) == 0 { continue @@ -129,8 +131,8 @@ func queryChunks(t testing.TB, q storage.ChunkQuerier, matchers ...*labels.Match name := series.Labels().String() result[name] = chks } - testutil.Ok(t, ss.Err()) - testutil.Equals(t, 0, len(ss.Warnings())) + assert.NoError(t, ss.Err()) + assert.Equal(t, 0, len(ss.Warnings())) return result } @@ -139,7 +141,7 @@ func queryChunks(t testing.TB, q storage.ChunkQuerier, matchers ...*labels.Match func TestDB_reloadOrder(t *testing.T) { db := openTestDB(t, nil, nil) defer func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }() metas := []BlockMeta{ @@ -151,44 +153,44 @@ func TestDB_reloadOrder(t *testing.T) { createBlock(t, db.Dir(), genSeries(1, 1, m.MinTime, m.MaxTime)) } - testutil.Ok(t, db.reloadBlocks()) + assert.NoError(t, db.reloadBlocks()) blocks := db.Blocks() - testutil.Equals(t, 3, len(blocks)) - testutil.Equals(t, metas[1].MinTime, blocks[0].Meta().MinTime) - testutil.Equals(t, metas[1].MaxTime, blocks[0].Meta().MaxTime) - testutil.Equals(t, metas[0].MinTime, blocks[1].Meta().MinTime) - testutil.Equals(t, metas[0].MaxTime, blocks[1].Meta().MaxTime) - testutil.Equals(t, metas[2].MinTime, blocks[2].Meta().MinTime) - testutil.Equals(t, metas[2].MaxTime, blocks[2].Meta().MaxTime) + assert.Equal(t, 3, len(blocks)) + assert.Equal(t, metas[1].MinTime, blocks[0].Meta().MinTime) + assert.Equal(t, metas[1].MaxTime, blocks[0].Meta().MaxTime) + assert.Equal(t, metas[0].MinTime, blocks[1].Meta().MinTime) + assert.Equal(t, metas[0].MaxTime, blocks[1].Meta().MaxTime) + assert.Equal(t, metas[2].MinTime, blocks[2].Meta().MinTime) + assert.Equal(t, metas[2].MaxTime, blocks[2].Meta().MaxTime) } func TestDataAvailableOnlyAfterCommit(t *testing.T) { db := openTestDB(t, nil, nil) defer func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }() ctx := context.Background() app := db.Appender(ctx) _, err := app.Add(labels.FromStrings("foo", "bar"), 0, 0) - testutil.Ok(t, err) + assert.NoError(t, err) querier, err := db.Querier(context.TODO(), 0, 1) - testutil.Ok(t, err) + assert.NoError(t, err) seriesSet := query(t, querier, labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")) - testutil.Equals(t, map[string][]tsdbutil.Sample{}, seriesSet) + assert.Equal(t, map[string][]tsdbutil.Sample{}, seriesSet) err = app.Commit() - testutil.Ok(t, err) + assert.NoError(t, err) querier, err = db.Querier(context.TODO(), 0, 1) - testutil.Ok(t, err) + assert.NoError(t, err) defer querier.Close() seriesSet = query(t, querier, labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")) - testutil.Equals(t, map[string][]tsdbutil.Sample{`{foo="bar"}`: {sample{t: 0, v: 0}}}, seriesSet) + assert.Equal(t, map[string][]tsdbutil.Sample{`{foo="bar"}`: {sample{t: 0, v: 0}}}, seriesSet) } // TestNoPanicAfterWALCorrutpion ensures that querying the db after a WAL corruption doesn't cause a panic. @@ -206,16 +208,16 @@ func TestNoPanicAfterWALCorrutpion(t *testing.T) { app := db.Appender(ctx) _, err := app.Add(labels.FromStrings("foo", "bar"), maxt, 0) expSamples = append(expSamples, sample{t: maxt, v: 0}) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) mmapedChunks, err := ioutil.ReadDir(mmappedChunksDir(db.Dir())) - testutil.Ok(t, err) + assert.NoError(t, err) if len(mmapedChunks) > 0 { break } maxt++ } - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) } // Corrupt the WAL after the first sample of the series so that it has at least one sample and @@ -223,102 +225,102 @@ func TestNoPanicAfterWALCorrutpion(t *testing.T) { // The repair deletes all WAL records after the corrupted record and these are read from the mmaped chunk. { walFiles, err := ioutil.ReadDir(path.Join(db.Dir(), "wal")) - testutil.Ok(t, err) + assert.NoError(t, err) f, err := os.OpenFile(path.Join(db.Dir(), "wal", walFiles[0].Name()), os.O_RDWR, 0666) - testutil.Ok(t, err) + assert.NoError(t, err) r := wal.NewReader(bufio.NewReader(f)) - testutil.Assert(t, r.Next(), "reading the series record") - testutil.Assert(t, r.Next(), "reading the first sample record") + assert.True(t, r.Next(), "reading the series record") + assert.True(t, r.Next(), "reading the first sample record") // Write an invalid record header to corrupt everything after the first wal sample. _, err = f.WriteAt([]byte{99}, r.Offset()) - testutil.Ok(t, err) + assert.NoError(t, err) f.Close() } // Query the data. { db, err := Open(db.Dir(), nil, nil, nil) - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }() - testutil.Equals(t, 1.0, prom_testutil.ToFloat64(db.head.metrics.walCorruptionsTotal), "WAL corruption count mismatch") + assert.Equal(t, 1.0, prom_testutil.ToFloat64(db.head.metrics.walCorruptionsTotal), "WAL corruption count mismatch") querier, err := db.Querier(context.TODO(), 0, maxt) - testutil.Ok(t, err) + assert.NoError(t, err) seriesSet := query(t, querier, labels.MustNewMatcher(labels.MatchEqual, "", "")) // The last sample should be missing as it was after the WAL segment corruption. - testutil.Equals(t, map[string][]tsdbutil.Sample{`{foo="bar"}`: expSamples[0 : len(expSamples)-1]}, seriesSet) + assert.Equal(t, map[string][]tsdbutil.Sample{`{foo="bar"}`: expSamples[0 : len(expSamples)-1]}, seriesSet) } } func TestDataNotAvailableAfterRollback(t *testing.T) { db := openTestDB(t, nil, nil) defer func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }() app := db.Appender(context.Background()) _, err := app.Add(labels.FromStrings("foo", "bar"), 0, 0) - testutil.Ok(t, err) + assert.NoError(t, err) err = app.Rollback() - testutil.Ok(t, err) + assert.NoError(t, err) querier, err := db.Querier(context.TODO(), 0, 1) - testutil.Ok(t, err) + assert.NoError(t, err) defer querier.Close() seriesSet := query(t, querier, labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")) - testutil.Equals(t, map[string][]tsdbutil.Sample{}, seriesSet) + assert.Equal(t, map[string][]tsdbutil.Sample{}, seriesSet) } func TestDBAppenderAddRef(t *testing.T) { db := openTestDB(t, nil, nil) defer func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }() ctx := context.Background() app1 := db.Appender(ctx) ref1, err := app1.Add(labels.FromStrings("a", "b"), 123, 0) - testutil.Ok(t, err) + assert.NoError(t, err) // Reference should already work before commit. err = app1.AddFast(ref1, 124, 1) - testutil.Ok(t, err) + assert.NoError(t, err) err = app1.Commit() - testutil.Ok(t, err) + assert.NoError(t, err) app2 := db.Appender(ctx) // first ref should already work in next transaction. err = app2.AddFast(ref1, 125, 0) - testutil.Ok(t, err) + assert.NoError(t, err) ref2, err := app2.Add(labels.FromStrings("a", "b"), 133, 1) - testutil.Ok(t, err) + assert.NoError(t, err) - testutil.Assert(t, ref1 == ref2, "") + assert.True(t, ref1 == ref2, "") // Reference must be valid to add another sample. err = app2.AddFast(ref2, 143, 2) - testutil.Ok(t, err) + assert.NoError(t, err) err = app2.AddFast(9999999, 1, 1) - testutil.Equals(t, storage.ErrNotFound, errors.Cause(err)) + assert.Equal(t, storage.ErrNotFound, errors.Cause(err)) - testutil.Ok(t, app2.Commit()) + assert.NoError(t, app2.Commit()) q, err := db.Querier(context.TODO(), 0, 200) - testutil.Ok(t, err) + assert.NoError(t, err) res := query(t, q, labels.MustNewMatcher(labels.MatchEqual, "a", "b")) - testutil.Equals(t, map[string][]tsdbutil.Sample{ + assert.Equal(t, map[string][]tsdbutil.Sample{ labels.FromStrings("a", "b").String(): { sample{t: 123, v: 0}, sample{t: 124, v: 1}, @@ -332,24 +334,24 @@ func TestDBAppenderAddRef(t *testing.T) { func TestAppendEmptyLabelsIgnored(t *testing.T) { db := openTestDB(t, nil, nil) defer func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }() ctx := context.Background() app1 := db.Appender(ctx) ref1, err := app1.Add(labels.FromStrings("a", "b"), 123, 0) - testutil.Ok(t, err) + assert.NoError(t, err) // Construct labels manually so there is an empty label. ref2, err := app1.Add(labels.Labels{labels.Label{Name: "a", Value: "b"}, labels.Label{Name: "c", Value: ""}}, 124, 0) - testutil.Ok(t, err) + assert.NoError(t, err) // Should be the same series. - testutil.Equals(t, ref1, ref2) + assert.Equal(t, ref1, ref2) err = app1.Commit() - testutil.Ok(t, err) + assert.NoError(t, err) } func TestDeleteSimple(t *testing.T) { @@ -385,7 +387,7 @@ Outer: for _, c := range cases { db := openTestDB(t, nil, nil) defer func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }() ctx := context.Background() @@ -397,17 +399,17 @@ Outer: app.Add(labels.Labels{{Name: "a", Value: "b"}}, i, smpls[i]) } - testutil.Ok(t, app.Commit()) + assert.NoError(t, app.Commit()) // TODO(gouthamve): Reset the tombstones somehow. // Delete the ranges. for _, r := range c.Intervals { - testutil.Ok(t, db.Delete(r.Mint, r.Maxt, labels.MustNewMatcher(labels.MatchEqual, "a", "b"))) + assert.NoError(t, db.Delete(r.Mint, r.Maxt, labels.MustNewMatcher(labels.MatchEqual, "a", "b"))) } // Compare the result. q, err := db.Querier(context.TODO(), 0, numSamples) - testutil.Ok(t, err) + assert.NoError(t, err) res := q.Select(false, nil, labels.MustNewMatcher(labels.MatchEqual, "a", "b")) @@ -422,22 +424,22 @@ Outer: for { eok, rok := expss.Next(), res.Next() - testutil.Equals(t, eok, rok) + assert.Equal(t, eok, rok) if !eok { - testutil.Equals(t, 0, len(res.Warnings())) + assert.Equal(t, 0, len(res.Warnings())) continue Outer } sexp := expss.At() sres := res.At() - testutil.Equals(t, sexp.Labels(), sres.Labels()) + assert.Equal(t, sexp.Labels(), sres.Labels()) smplExp, errExp := storage.ExpandSamples(sexp.Iterator(), nil) smplRes, errRes := storage.ExpandSamples(sres.Iterator(), nil) - testutil.Equals(t, errExp, errRes) - testutil.Equals(t, smplExp, smplRes) + assert.Equal(t, errExp, errRes) + assert.Equal(t, smplExp, smplRes) } } } @@ -445,107 +447,107 @@ Outer: func TestAmendDatapointCausesError(t *testing.T) { db := openTestDB(t, nil, nil) defer func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }() ctx := context.Background() app := db.Appender(ctx) _, err := app.Add(labels.Labels{{Name: "a", Value: "b"}}, 0, 0) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) app = db.Appender(ctx) _, err = app.Add(labels.Labels{{Name: "a", Value: "b"}}, 0, 1) - testutil.Equals(t, storage.ErrDuplicateSampleForTimestamp, err) - testutil.Ok(t, app.Rollback()) + assert.Equal(t, storage.ErrDuplicateSampleForTimestamp, err) + assert.NoError(t, app.Rollback()) } func TestDuplicateNaNDatapointNoAmendError(t *testing.T) { db := openTestDB(t, nil, nil) defer func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }() ctx := context.Background() app := db.Appender(ctx) _, err := app.Add(labels.Labels{{Name: "a", Value: "b"}}, 0, math.NaN()) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) app = db.Appender(ctx) _, err = app.Add(labels.Labels{{Name: "a", Value: "b"}}, 0, math.NaN()) - testutil.Ok(t, err) + assert.NoError(t, err) } func TestNonDuplicateNaNDatapointsCausesAmendError(t *testing.T) { db := openTestDB(t, nil, nil) defer func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }() ctx := context.Background() app := db.Appender(ctx) _, err := app.Add(labels.Labels{{Name: "a", Value: "b"}}, 0, math.Float64frombits(0x7ff0000000000001)) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) app = db.Appender(ctx) _, err = app.Add(labels.Labels{{Name: "a", Value: "b"}}, 0, math.Float64frombits(0x7ff0000000000002)) - testutil.Equals(t, storage.ErrDuplicateSampleForTimestamp, err) + assert.Equal(t, storage.ErrDuplicateSampleForTimestamp, err) } func TestEmptyLabelsetCausesError(t *testing.T) { db := openTestDB(t, nil, nil) defer func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }() ctx := context.Background() app := db.Appender(ctx) _, err := app.Add(labels.Labels{}, 0, 0) - testutil.NotOk(t, err) - testutil.Equals(t, "empty labelset: invalid sample", err.Error()) + assert.Error(t, err) + assert.Equal(t, "empty labelset: invalid sample", err.Error()) } func TestSkippingInvalidValuesInSameTxn(t *testing.T) { db := openTestDB(t, nil, nil) defer func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }() // Append AmendedValue. ctx := context.Background() app := db.Appender(ctx) _, err := app.Add(labels.Labels{{Name: "a", Value: "b"}}, 0, 1) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = app.Add(labels.Labels{{Name: "a", Value: "b"}}, 0, 2) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) // Make sure the right value is stored. q, err := db.Querier(context.TODO(), 0, 10) - testutil.Ok(t, err) + assert.NoError(t, err) ssMap := query(t, q, labels.MustNewMatcher(labels.MatchEqual, "a", "b")) - testutil.Equals(t, map[string][]tsdbutil.Sample{ + assert.Equal(t, map[string][]tsdbutil.Sample{ labels.New(labels.Label{Name: "a", Value: "b"}).String(): {sample{0, 1}}, }, ssMap) // Append Out of Order Value. app = db.Appender(ctx) _, err = app.Add(labels.Labels{{Name: "a", Value: "b"}}, 10, 3) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = app.Add(labels.Labels{{Name: "a", Value: "b"}}, 7, 5) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) q, err = db.Querier(context.TODO(), 0, 10) - testutil.Ok(t, err) + assert.NoError(t, err) ssMap = query(t, q, labels.MustNewMatcher(labels.MatchEqual, "a", "b")) - testutil.Equals(t, map[string][]tsdbutil.Sample{ + assert.Equal(t, map[string][]tsdbutil.Sample{ labels.New(labels.Label{Name: "a", Value: "b"}).String(): {sample{0, 1}, sample{10, 3}}, }, ssMap) } @@ -559,28 +561,28 @@ func TestDB_Snapshot(t *testing.T) { mint := int64(1414141414000) for i := 0; i < 1000; i++ { _, err := app.Add(labels.FromStrings("foo", "bar"), mint+int64(i), 1.0) - testutil.Ok(t, err) + assert.NoError(t, err) } - testutil.Ok(t, app.Commit()) + assert.NoError(t, app.Commit()) // create snapshot snap, err := ioutil.TempDir("", "snap") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(snap)) + assert.NoError(t, os.RemoveAll(snap)) }() - testutil.Ok(t, db.Snapshot(snap, true)) - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Snapshot(snap, true)) + assert.NoError(t, db.Close()) // reopen DB from snapshot db, err = Open(snap, nil, nil, nil) - testutil.Ok(t, err) - defer func() { testutil.Ok(t, db.Close()) }() + assert.NoError(t, err) + defer func() { assert.NoError(t, db.Close()) }() querier, err := db.Querier(context.TODO(), mint, mint+1000) - testutil.Ok(t, err) - defer func() { testutil.Ok(t, querier.Close()) }() + assert.NoError(t, err) + defer func() { assert.NoError(t, querier.Close()) }() // sum values seriesSet := querier.Select(false, nil, labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")) @@ -591,11 +593,11 @@ func TestDB_Snapshot(t *testing.T) { _, v := series.At() sum += v } - testutil.Ok(t, series.Err()) + assert.NoError(t, series.Err()) } - testutil.Ok(t, seriesSet.Err()) - testutil.Equals(t, 0, len(seriesSet.Warnings())) - testutil.Equals(t, 1000.0, sum) + assert.NoError(t, seriesSet.Err()) + assert.Equal(t, 0, len(seriesSet.Warnings())) + assert.Equal(t, 1000.0, sum) } // TestDB_Snapshot_ChunksOutsideOfCompactedRange ensures that a snapshot removes chunks samples @@ -609,30 +611,30 @@ func TestDB_Snapshot_ChunksOutsideOfCompactedRange(t *testing.T) { mint := int64(1414141414000) for i := 0; i < 1000; i++ { _, err := app.Add(labels.FromStrings("foo", "bar"), mint+int64(i), 1.0) - testutil.Ok(t, err) + assert.NoError(t, err) } - testutil.Ok(t, app.Commit()) + assert.NoError(t, app.Commit()) snap, err := ioutil.TempDir("", "snap") - testutil.Ok(t, err) + assert.NoError(t, err) // Hackingly introduce "race", by having lower max time then maxTime in last chunk. db.head.maxTime.Sub(10) defer func() { - testutil.Ok(t, os.RemoveAll(snap)) + assert.NoError(t, os.RemoveAll(snap)) }() - testutil.Ok(t, db.Snapshot(snap, true)) - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Snapshot(snap, true)) + assert.NoError(t, db.Close()) // Reopen DB from snapshot. db, err = Open(snap, nil, nil, nil) - testutil.Ok(t, err) - defer func() { testutil.Ok(t, db.Close()) }() + assert.NoError(t, err) + defer func() { assert.NoError(t, db.Close()) }() querier, err := db.Querier(context.TODO(), mint, mint+1000) - testutil.Ok(t, err) - defer func() { testutil.Ok(t, querier.Close()) }() + assert.NoError(t, err) + defer func() { assert.NoError(t, querier.Close()) }() // Sum values. seriesSet := querier.Select(false, nil, labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")) @@ -643,13 +645,13 @@ func TestDB_Snapshot_ChunksOutsideOfCompactedRange(t *testing.T) { _, v := series.At() sum += v } - testutil.Ok(t, series.Err()) + assert.NoError(t, series.Err()) } - testutil.Ok(t, seriesSet.Err()) - testutil.Equals(t, 0, len(seriesSet.Warnings())) + assert.NoError(t, seriesSet.Err()) + assert.Equal(t, 0, len(seriesSet.Warnings())) // Since we snapshotted with MaxTime - 10, so expect 10 less samples. - testutil.Equals(t, 1000.0-10, sum) + assert.Equal(t, 1000.0-10, sum) } func TestDB_SnapshotWithDelete(t *testing.T) { @@ -666,7 +668,7 @@ func TestDB_SnapshotWithDelete(t *testing.T) { app.Add(labels.Labels{{Name: "a", Value: "b"}}, i, smpls[i]) } - testutil.Ok(t, app.Commit()) + assert.NoError(t, app.Commit()) cases := []struct { intervals tombstones.Intervals remaint []int64 @@ -682,28 +684,28 @@ Outer: // TODO(gouthamve): Reset the tombstones somehow. // Delete the ranges. for _, r := range c.intervals { - testutil.Ok(t, db.Delete(r.Mint, r.Maxt, labels.MustNewMatcher(labels.MatchEqual, "a", "b"))) + assert.NoError(t, db.Delete(r.Mint, r.Maxt, labels.MustNewMatcher(labels.MatchEqual, "a", "b"))) } // create snapshot snap, err := ioutil.TempDir("", "snap") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(snap)) + assert.NoError(t, os.RemoveAll(snap)) }() - testutil.Ok(t, db.Snapshot(snap, true)) - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Snapshot(snap, true)) + assert.NoError(t, db.Close()) // reopen DB from snapshot db, err = Open(snap, nil, nil, nil) - testutil.Ok(t, err) - defer func() { testutil.Ok(t, db.Close()) }() + assert.NoError(t, err) + defer func() { assert.NoError(t, db.Close()) }() // Compare the result. q, err := db.Querier(context.TODO(), 0, numSamples) - testutil.Ok(t, err) - defer func() { testutil.Ok(t, q.Close()) }() + assert.NoError(t, err) + defer func() { assert.NoError(t, q.Close()) }() res := q.Select(false, nil, labels.MustNewMatcher(labels.MatchEqual, "a", "b")) @@ -717,28 +719,28 @@ Outer: }) if len(expSamples) == 0 { - testutil.Assert(t, res.Next() == false, "") + assert.True(t, res.Next() == false, "") continue } for { eok, rok := expss.Next(), res.Next() - testutil.Equals(t, eok, rok) + assert.Equal(t, eok, rok) if !eok { - testutil.Equals(t, 0, len(res.Warnings())) + assert.Equal(t, 0, len(res.Warnings())) continue Outer } sexp := expss.At() sres := res.At() - testutil.Equals(t, sexp.Labels(), sres.Labels()) + assert.Equal(t, sexp.Labels(), sres.Labels()) smplExp, errExp := storage.ExpandSamples(sexp.Iterator(), nil) smplRes, errRes := storage.ExpandSamples(sres.Iterator(), nil) - testutil.Equals(t, errExp, errRes) - testutil.Equals(t, smplExp, smplRes) + assert.Equal(t, errExp, errRes) + assert.Equal(t, smplExp, smplRes) } } } @@ -800,7 +802,7 @@ func TestDB_e2e(t *testing.T) { db := openTestDB(t, nil, nil) defer func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }() ctx := context.Background() @@ -817,7 +819,7 @@ func TestDB_e2e(t *testing.T) { series = append(series, sample{ts, v}) _, err := app.Add(lset, ts, v) - testutil.Ok(t, err) + assert.NoError(t, err) ts += rand.Int63n(timeInterval) + 1 } @@ -825,7 +827,7 @@ func TestDB_e2e(t *testing.T) { seriesMap[lset.String()] = series } - testutil.Ok(t, app.Commit()) + assert.NoError(t, app.Commit()) // Query each selector on 1000 random time-ranges. queries := []struct { @@ -876,7 +878,7 @@ func TestDB_e2e(t *testing.T) { } q, err := db.Querier(context.TODO(), mint, maxt) - testutil.Ok(t, err) + assert.NoError(t, err) ss := q.Select(false, nil, qry.ms...) result := map[string][]tsdbutil.Sample{} @@ -885,16 +887,16 @@ func TestDB_e2e(t *testing.T) { x := ss.At() smpls, err := storage.ExpandSamples(x.Iterator(), newSample) - testutil.Ok(t, err) + assert.NoError(t, err) if len(smpls) > 0 { result[x.Labels().String()] = smpls } } - testutil.Ok(t, ss.Err()) - testutil.Equals(t, 0, len(ss.Warnings())) - testutil.Equals(t, expected, result) + assert.NoError(t, ss.Err()) + assert.Equal(t, 0, len(ss.Warnings())) + assert.Equal(t, expected, result) q.Close() } @@ -911,22 +913,22 @@ func TestWALFlushedOnDBClose(t *testing.T) { ctx := context.Background() app := db.Appender(ctx) _, err := app.Add(lbls, 0, 1) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) db, err = Open(dirDb, nil, nil, nil) - testutil.Ok(t, err) - defer func() { testutil.Ok(t, db.Close()) }() + assert.NoError(t, err) + defer func() { assert.NoError(t, db.Close()) }() q, err := db.Querier(context.TODO(), 0, 1) - testutil.Ok(t, err) + assert.NoError(t, err) values, ws, err := q.LabelValues("labelname") - testutil.Ok(t, err) - testutil.Equals(t, 0, len(ws)) - testutil.Equals(t, []string{"labelvalue"}, values) + assert.NoError(t, err) + assert.Equal(t, 0, len(ws)) + assert.Equal(t, []string{"labelvalue"}, values) } func TestWALSegmentSizeOptions(t *testing.T) { @@ -934,7 +936,7 @@ func TestWALSegmentSizeOptions(t *testing.T) { // Default Wal Size. 0: func(dbDir string, segmentSize int) { filesAndDir, err := ioutil.ReadDir(filepath.Join(dbDir, "wal")) - testutil.Ok(t, err) + assert.NoError(t, err) files := []os.FileInfo{} for _, f := range filesAndDir { if !f.IsDir() { @@ -943,37 +945,37 @@ func TestWALSegmentSizeOptions(t *testing.T) { } // All the full segment files (all but the last) should match the segment size option. for _, f := range files[:len(files)-1] { - testutil.Equals(t, int64(DefaultOptions().WALSegmentSize), f.Size(), "WAL file size doesn't match WALSegmentSize option, filename: %v", f.Name()) + assert.Equal(t, int64(DefaultOptions().WALSegmentSize), f.Size(), "WAL file size doesn't match WALSegmentSize option, filename: %v", f.Name()) } lastFile := files[len(files)-1] - testutil.Assert(t, int64(DefaultOptions().WALSegmentSize) > lastFile.Size(), "last WAL file size is not smaller than the WALSegmentSize option, filename: %v", lastFile.Name()) + assert.True(t, int64(DefaultOptions().WALSegmentSize) > lastFile.Size(), "last WAL file size is not smaller than the WALSegmentSize option, filename: %v", lastFile.Name()) }, // Custom Wal Size. 2 * 32 * 1024: func(dbDir string, segmentSize int) { filesAndDir, err := ioutil.ReadDir(filepath.Join(dbDir, "wal")) - testutil.Ok(t, err) + assert.NoError(t, err) files := []os.FileInfo{} for _, f := range filesAndDir { if !f.IsDir() { files = append(files, f) } } - testutil.Assert(t, len(files) > 1, "current WALSegmentSize should result in more than a single WAL file.") + assert.True(t, len(files) > 1, "current WALSegmentSize should result in more than a single WAL file.") // All the full segment files (all but the last) should match the segment size option. for _, f := range files[:len(files)-1] { - testutil.Equals(t, int64(segmentSize), f.Size(), "WAL file size doesn't match WALSegmentSize option, filename: %v", f.Name()) + assert.Equal(t, int64(segmentSize), f.Size(), "WAL file size doesn't match WALSegmentSize option, filename: %v", f.Name()) } lastFile := files[len(files)-1] - testutil.Assert(t, int64(segmentSize) > lastFile.Size(), "last WAL file size is not smaller than the WALSegmentSize option, filename: %v", lastFile.Name()) + assert.True(t, int64(segmentSize) > lastFile.Size(), "last WAL file size is not smaller than the WALSegmentSize option, filename: %v", lastFile.Name()) }, // Wal disabled. -1: func(dbDir string, segmentSize int) { // Check that WAL dir is not there. _, err := os.Stat(filepath.Join(dbDir, "wal")) - testutil.NotOk(t, err) + assert.Error(t, err) // Check that there is chunks dir. _, err = os.Stat(mmappedChunksDir(dbDir)) - testutil.Ok(t, err) + assert.NoError(t, err) }, } for segmentSize, testFunc := range tests { @@ -985,16 +987,16 @@ func TestWALSegmentSizeOptions(t *testing.T) { for i := int64(0); i < 155; i++ { app := db.Appender(context.Background()) ref, err := app.Add(labels.Labels{labels.Label{Name: "wal" + fmt.Sprintf("%d", i), Value: "size"}}, i, rand.Float64()) - testutil.Ok(t, err) + assert.NoError(t, err) for j := int64(1); j <= 78; j++ { err := app.AddFast(ref, i+j, rand.Float64()) - testutil.Ok(t, err) + assert.NoError(t, err) } - testutil.Ok(t, app.Commit()) + assert.NoError(t, app.Commit()) } dbDir := db.Dir() - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) testFunc(dbDir, int(opts.WALSegmentSize)) }) } @@ -1014,7 +1016,7 @@ func TestTombstoneClean(t *testing.T) { app.Add(labels.Labels{{Name: "a", Value: "b"}}, i, smpls[i]) } - testutil.Ok(t, app.Commit()) + assert.NoError(t, app.Commit()) cases := []struct { intervals tombstones.Intervals remaint []int64 @@ -1030,29 +1032,29 @@ func TestTombstoneClean(t *testing.T) { // create snapshot snap, err := ioutil.TempDir("", "snap") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(snap)) + assert.NoError(t, os.RemoveAll(snap)) }() - testutil.Ok(t, db.Snapshot(snap, true)) - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Snapshot(snap, true)) + assert.NoError(t, db.Close()) // reopen DB from snapshot db, err = Open(snap, nil, nil, nil) - testutil.Ok(t, err) + assert.NoError(t, err) defer db.Close() for _, r := range c.intervals { - testutil.Ok(t, db.Delete(r.Mint, r.Maxt, labels.MustNewMatcher(labels.MatchEqual, "a", "b"))) + assert.NoError(t, db.Delete(r.Mint, r.Maxt, labels.MustNewMatcher(labels.MatchEqual, "a", "b"))) } // All of the setup for THIS line. - testutil.Ok(t, db.CleanTombstones()) + assert.NoError(t, db.CleanTombstones()) // Compare the result. q, err := db.Querier(context.TODO(), 0, numSamples) - testutil.Ok(t, err) + assert.NoError(t, err) defer q.Close() res := q.Select(false, nil, labels.MustNewMatcher(labels.MatchEqual, "a", "b")) @@ -1067,13 +1069,13 @@ func TestTombstoneClean(t *testing.T) { }) if len(expSamples) == 0 { - testutil.Assert(t, res.Next() == false, "") + assert.True(t, res.Next() == false, "") continue } for { eok, rok := expss.Next(), res.Next() - testutil.Equals(t, eok, rok) + assert.Equal(t, eok, rok) if !eok { break @@ -1081,18 +1083,18 @@ func TestTombstoneClean(t *testing.T) { sexp := expss.At() sres := res.At() - testutil.Equals(t, sexp.Labels(), sres.Labels()) + assert.Equal(t, sexp.Labels(), sres.Labels()) smplExp, errExp := storage.ExpandSamples(sexp.Iterator(), nil) smplRes, errRes := storage.ExpandSamples(sres.Iterator(), nil) - testutil.Equals(t, errExp, errRes) - testutil.Equals(t, smplExp, smplRes) + assert.Equal(t, errExp, errRes) + assert.Equal(t, smplExp, smplRes) } - testutil.Equals(t, 0, len(res.Warnings())) + assert.Equal(t, 0, len(res.Warnings())) for _, b := range db.Blocks() { - testutil.Equals(t, tombstones.NewMemTombstones(), b.tombstones) + assert.Equal(t, tombstones.NewMemTombstones(), b.tombstones) } } } @@ -1103,7 +1105,7 @@ func TestTombstoneClean(t *testing.T) { func TestTombstoneCleanFail(t *testing.T) { db := openTestDB(t, nil, nil) defer func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }() var expectedBlockDirs []string @@ -1114,7 +1116,7 @@ func TestTombstoneCleanFail(t *testing.T) { for i := 0; i < totalBlocks; i++ { blockDir := createBlock(t, db.Dir(), genSeries(1, 1, 0, 1)) block, err := OpenBlock(nil, blockDir, nil) - testutil.Ok(t, err) + assert.NoError(t, err) // Add some fake tombstones to trigger the compaction. tomb := tombstones.NewMemTombstones() tomb.AddInterval(0, tombstones.Interval{Mint: 0, Maxt: 1}) @@ -1133,12 +1135,12 @@ func TestTombstoneCleanFail(t *testing.T) { } // The compactor should trigger a failure here. - testutil.NotOk(t, db.CleanTombstones()) + assert.Error(t, db.CleanTombstones()) // Now check that the CleanTombstones didn't leave any blocks behind after a failure. actualBlockDirs, err := blockDirs(db.dir) - testutil.Ok(t, err) - testutil.Equals(t, expectedBlockDirs, actualBlockDirs) + assert.NoError(t, err) + assert.Equal(t, expectedBlockDirs, actualBlockDirs) } // mockCompactorFailing creates a new empty block on every write and fails when reached the max allowed total. @@ -1157,8 +1159,8 @@ func (c *mockCompactorFailing) Write(dest string, b BlockReader, mint, maxt int6 } block, err := OpenBlock(nil, createBlock(c.t, dest, genSeries(1, 1, 0, 1)), nil) - testutil.Ok(c.t, err) - testutil.Ok(c.t, block.Close()) // Close block as we won't be using anywhere. + assert.NoError(c.t, err) + assert.NoError(c.t, block.Close()) // Close block as we won't be using anywhere. c.blocks = append(c.blocks, block) // Now check that all expected blocks are actually persisted on disk. @@ -1168,9 +1170,9 @@ func (c *mockCompactorFailing) Write(dest string, b BlockReader, mint, maxt int6 expectedBlocks = append(expectedBlocks, filepath.Join(dest, b.Meta().ULID.String())) } actualBlockDirs, err := blockDirs(dest) - testutil.Ok(c.t, err) + assert.NoError(c.t, err) - testutil.Equals(c.t, expectedBlocks, actualBlockDirs) + assert.Equal(c.t, expectedBlocks, actualBlockDirs) return block.Meta().ULID, nil } @@ -1182,7 +1184,7 @@ func (*mockCompactorFailing) Compact(string, []string, []*Block) (ulid.ULID, err func TestTimeRetention(t *testing.T) { db := openTestDB(t, nil, []int64{1000}) defer func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }() blocks := []*BlockMeta{ @@ -1195,25 +1197,25 @@ func TestTimeRetention(t *testing.T) { createBlock(t, db.Dir(), genSeries(10, 10, m.MinTime, m.MaxTime)) } - testutil.Ok(t, db.reloadBlocks()) // Reload the db to register the new blocks. - testutil.Equals(t, len(blocks), len(db.Blocks())) // Ensure all blocks are registered. + assert.NoError(t, db.reloadBlocks()) // Reload the db to register the new blocks. + assert.Equal(t, len(blocks), len(db.Blocks())) // Ensure all blocks are registered. db.opts.RetentionDuration = blocks[2].MaxTime - blocks[1].MinTime - testutil.Ok(t, db.reloadBlocks()) + assert.NoError(t, db.reloadBlocks()) expBlocks := blocks[1:] actBlocks := db.Blocks() - testutil.Equals(t, 1, int(prom_testutil.ToFloat64(db.metrics.timeRetentionCount)), "metric retention count mismatch") - testutil.Equals(t, len(expBlocks), len(actBlocks)) - testutil.Equals(t, expBlocks[0].MaxTime, actBlocks[0].meta.MaxTime) - testutil.Equals(t, expBlocks[len(expBlocks)-1].MaxTime, actBlocks[len(actBlocks)-1].meta.MaxTime) + assert.Equal(t, 1, int(prom_testutil.ToFloat64(db.metrics.timeRetentionCount)), "metric retention count mismatch") + assert.Equal(t, len(expBlocks), len(actBlocks)) + assert.Equal(t, expBlocks[0].MaxTime, actBlocks[0].meta.MaxTime) + assert.Equal(t, expBlocks[len(expBlocks)-1].MaxTime, actBlocks[len(actBlocks)-1].meta.MaxTime) } func TestSizeRetention(t *testing.T) { db := openTestDB(t, nil, []int64{100}) defer func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }() blocks := []*BlockMeta{ @@ -1241,62 +1243,62 @@ func TestSizeRetention(t *testing.T) { for it.Next() { tim, v := it.At() _, err := headApp.Add(s.Labels(), tim, v) - testutil.Ok(t, err) + assert.NoError(t, err) } - testutil.Ok(t, it.Err()) + assert.NoError(t, it.Err()) } } - testutil.Ok(t, headApp.Commit()) + assert.NoError(t, headApp.Commit()) // Test that registered size matches the actual disk size. - testutil.Ok(t, db.reloadBlocks()) // Reload the db to register the new db size. - testutil.Equals(t, len(blocks), len(db.Blocks())) // Ensure all blocks are registered. + assert.NoError(t, db.reloadBlocks()) // Reload the db to register the new db size. + assert.Equal(t, len(blocks), len(db.Blocks())) // Ensure all blocks are registered. blockSize := int64(prom_testutil.ToFloat64(db.metrics.blocksBytes)) // Use the actual internal metrics. walSize, err := db.Head().wal.Size() - testutil.Ok(t, err) + assert.NoError(t, err) // Expected size should take into account block size + WAL size expSize := blockSize + walSize actSize, err := fileutil.DirSize(db.Dir()) - testutil.Ok(t, err) - testutil.Equals(t, expSize, actSize, "registered size doesn't match actual disk size") + assert.NoError(t, err) + assert.Equal(t, expSize, actSize, "registered size doesn't match actual disk size") // Create a WAL checkpoint, and compare sizes. first, last, err := wal.Segments(db.Head().wal.Dir()) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = wal.Checkpoint(log.NewNopLogger(), db.Head().wal, first, last-1, func(x uint64) bool { return false }, 0) - testutil.Ok(t, err) + assert.NoError(t, err) blockSize = int64(prom_testutil.ToFloat64(db.metrics.blocksBytes)) // Use the actual internal metrics. walSize, err = db.Head().wal.Size() - testutil.Ok(t, err) + assert.NoError(t, err) expSize = blockSize + walSize actSize, err = fileutil.DirSize(db.Dir()) - testutil.Ok(t, err) - testutil.Equals(t, expSize, actSize, "registered size doesn't match actual disk size") + assert.NoError(t, err) + assert.Equal(t, expSize, actSize, "registered size doesn't match actual disk size") // Decrease the max bytes limit so that a delete is triggered. // Check total size, total count and check that the oldest block was deleted. firstBlockSize := db.Blocks()[0].Size() sizeLimit := actSize - firstBlockSize - db.opts.MaxBytes = sizeLimit // Set the new db size limit one block smaller that the actual size. - testutil.Ok(t, db.reloadBlocks()) // Reload the db to register the new db size. + db.opts.MaxBytes = sizeLimit // Set the new db size limit one block smaller that the actual size. + assert.NoError(t, db.reloadBlocks()) // Reload the db to register the new db size. expBlocks := blocks[1:] actBlocks := db.Blocks() blockSize = int64(prom_testutil.ToFloat64(db.metrics.blocksBytes)) walSize, err = db.Head().wal.Size() - testutil.Ok(t, err) + assert.NoError(t, err) // Expected size should take into account block size + WAL size expSize = blockSize + walSize actRetentionCount := int(prom_testutil.ToFloat64(db.metrics.sizeRetentionCount)) actSize, err = fileutil.DirSize(db.Dir()) - testutil.Ok(t, err) + assert.NoError(t, err) - testutil.Equals(t, 1, actRetentionCount, "metric retention count mismatch") - testutil.Equals(t, actSize, expSize, "metric db size doesn't match actual disk size") - testutil.Assert(t, expSize <= sizeLimit, "actual size (%v) is expected to be less than or equal to limit (%v)", expSize, sizeLimit) - testutil.Equals(t, len(blocks)-1, len(actBlocks), "new block count should be decreased from:%v to:%v", len(blocks), len(blocks)-1) - testutil.Equals(t, expBlocks[0].MaxTime, actBlocks[0].meta.MaxTime, "maxT mismatch of the first block") - testutil.Equals(t, expBlocks[len(expBlocks)-1].MaxTime, actBlocks[len(actBlocks)-1].meta.MaxTime, "maxT mismatch of the last block") + assert.Equal(t, 1, actRetentionCount, "metric retention count mismatch") + assert.Equal(t, actSize, expSize, "metric db size doesn't match actual disk size") + assert.True(t, expSize <= sizeLimit, "actual size (%v) is expected to be less than or equal to limit (%v)", expSize, sizeLimit) + assert.Equal(t, len(blocks)-1, len(actBlocks), "new block count should be decreased from:%v to:%v", len(blocks), len(blocks)-1) + assert.Equal(t, expBlocks[0].MaxTime, actBlocks[0].meta.MaxTime, "maxT mismatch of the first block") + assert.Equal(t, expBlocks[len(expBlocks)-1].MaxTime, actBlocks[len(actBlocks)-1].meta.MaxTime, "maxT mismatch of the last block") } func TestSizeRetentionMetric(t *testing.T) { @@ -1314,18 +1316,18 @@ func TestSizeRetentionMetric(t *testing.T) { MaxBytes: c.maxBytes, }, []int64{100}) defer func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }() actMaxBytes := int64(prom_testutil.ToFloat64(db.metrics.maxBytes)) - testutil.Equals(t, actMaxBytes, c.expMaxBytes, "metric retention limit bytes mismatch") + assert.Equal(t, actMaxBytes, c.expMaxBytes, "metric retention limit bytes mismatch") } } func TestNotMatcherSelectsLabelsUnsetSeries(t *testing.T) { db := openTestDB(t, nil, nil) defer func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }() labelpairs := []labels.Labels{ @@ -1337,9 +1339,9 @@ func TestNotMatcherSelectsLabelsUnsetSeries(t *testing.T) { app := db.Appender(ctx) for _, lbls := range labelpairs { _, err := app.Add(lbls, 0, 1) - testutil.Ok(t, err) + assert.NoError(t, err) } - testutil.Ok(t, app.Commit()) + assert.NoError(t, app.Commit()) cases := []struct { selector labels.Selector @@ -1384,15 +1386,15 @@ func TestNotMatcherSelectsLabelsUnsetSeries(t *testing.T) { }} q, err := db.Querier(context.TODO(), 0, 10) - testutil.Ok(t, err) - defer func() { testutil.Ok(t, q.Close()) }() + assert.NoError(t, err) + defer func() { assert.NoError(t, q.Close()) }() for _, c := range cases { ss := q.Select(false, nil, c.selector...) lres, _, ws, err := expandSeriesSet(ss) - testutil.Ok(t, err) - testutil.Equals(t, 0, len(ws)) - testutil.Equals(t, c.series, lres) + assert.NoError(t, err) + assert.Equal(t, 0, len(ws)) + assert.Equal(t, c.series, lres) } } @@ -1423,7 +1425,7 @@ func TestOverlappingBlocksDetectsAllOverlaps(t *testing.T) { metas[i] = BlockMeta{MinTime: int64(i * 10), MaxTime: int64((i + 1) * 10)} } - testutil.Assert(t, len(OverlappingBlocks(metas)) == 0, "we found unexpected overlaps") + assert.True(t, len(OverlappingBlocks(metas)) == 0, "we found unexpected overlaps") // Add overlapping blocks. We've to establish order again since we aren't interested // in trivial overlaps caused by unorderedness. @@ -1437,13 +1439,13 @@ func TestOverlappingBlocksDetectsAllOverlaps(t *testing.T) { // o1 overlaps with 10-20. o1 := BlockMeta{MinTime: 15, MaxTime: 17} - testutil.Equals(t, Overlaps{ + assert.Equal(t, Overlaps{ {Min: 15, Max: 17}: {metas[1], o1}, }, OverlappingBlocks(add(o1))) // o2 overlaps with 20-30 and 30-40. o2 := BlockMeta{MinTime: 21, MaxTime: 31} - testutil.Equals(t, Overlaps{ + assert.Equal(t, Overlaps{ {Min: 21, Max: 30}: {metas[2], o2}, {Min: 30, Max: 31}: {o2, metas[3]}, }, OverlappingBlocks(add(o2))) @@ -1451,19 +1453,19 @@ func TestOverlappingBlocksDetectsAllOverlaps(t *testing.T) { // o3a and o3b overlaps with 30-40 and each other. o3a := BlockMeta{MinTime: 33, MaxTime: 39} o3b := BlockMeta{MinTime: 34, MaxTime: 36} - testutil.Equals(t, Overlaps{ + assert.Equal(t, Overlaps{ {Min: 34, Max: 36}: {metas[3], o3a, o3b}, }, OverlappingBlocks(add(o3a, o3b))) // o4 is 1:1 overlap with 50-60. o4 := BlockMeta{MinTime: 50, MaxTime: 60} - testutil.Equals(t, Overlaps{ + assert.Equal(t, Overlaps{ {Min: 50, Max: 60}: {metas[5], o4}, }, OverlappingBlocks(add(o4))) // o5 overlaps with 60-70, 70-80 and 80-90. o5 := BlockMeta{MinTime: 61, MaxTime: 85} - testutil.Equals(t, Overlaps{ + assert.Equal(t, Overlaps{ {Min: 61, Max: 70}: {metas[6], o5}, {Min: 70, Max: 80}: {o5, metas[7]}, {Min: 80, Max: 85}: {o5, metas[8]}, @@ -1472,13 +1474,13 @@ func TestOverlappingBlocksDetectsAllOverlaps(t *testing.T) { // o6a overlaps with 90-100, 100-110 and o6b, o6b overlaps with 90-100 and o6a. o6a := BlockMeta{MinTime: 92, MaxTime: 105} o6b := BlockMeta{MinTime: 94, MaxTime: 99} - testutil.Equals(t, Overlaps{ + assert.Equal(t, Overlaps{ {Min: 94, Max: 99}: {metas[9], o6a, o6b}, {Min: 100, Max: 105}: {o6a, metas[10]}, }, OverlappingBlocks(add(o6a, o6b))) // All together. - testutil.Equals(t, Overlaps{ + assert.Equal(t, Overlaps{ {Min: 15, Max: 17}: {metas[1], o1}, {Min: 21, Max: 30}: {metas[2], o2}, {Min: 30, Max: 31}: {o2, metas[3]}, {Min: 34, Max: 36}: {metas[3], o3a, o3b}, @@ -1499,7 +1501,7 @@ func TestOverlappingBlocksDetectsAllOverlaps(t *testing.T) { nc1 = append(nc1, BlockMeta{MinTime: 5, MaxTime: 7}) nc1 = append(nc1, BlockMeta{MinTime: 7, MaxTime: 10}) nc1 = append(nc1, BlockMeta{MinTime: 8, MaxTime: 9}) - testutil.Equals(t, Overlaps{ + assert.Equal(t, Overlaps{ {Min: 2, Max: 3}: {nc1[0], nc1[1], nc1[2], nc1[3], nc1[4], nc1[5]}, // 1-5, 2-3, 2-3, 2-3, 2-3, 2,6 {Min: 3, Max: 5}: {nc1[0], nc1[5], nc1[6]}, // 1-5, 2-6, 3-5 {Min: 5, Max: 6}: {nc1[5], nc1[7]}, // 2-6, 5-7 @@ -1511,7 +1513,7 @@ func TestOverlappingBlocksDetectsAllOverlaps(t *testing.T) { func TestChunkAtBlockBoundary(t *testing.T) { db := openTestDB(t, nil, nil) defer func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }() ctx := context.Background() @@ -1522,27 +1524,27 @@ func TestChunkAtBlockBoundary(t *testing.T) { for i := int64(0); i < 3; i++ { _, err := app.Add(label, i*blockRange, 0) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = app.Add(label, i*blockRange+1000, 0) - testutil.Ok(t, err) + assert.NoError(t, err) } err := app.Commit() - testutil.Ok(t, err) + assert.NoError(t, err) err = db.Compact() - testutil.Ok(t, err) + assert.NoError(t, err) for _, block := range db.Blocks() { r, err := block.Index() - testutil.Ok(t, err) + assert.NoError(t, err) defer r.Close() meta := block.Meta() k, v := index.AllPostingsKey() p, err := r.Postings(k, v) - testutil.Ok(t, err) + assert.NoError(t, err) var ( lset labels.Labels @@ -1553,22 +1555,22 @@ func TestChunkAtBlockBoundary(t *testing.T) { for p.Next() { err = r.Series(p.At(), &lset, &chks) - testutil.Ok(t, err) + assert.NoError(t, err) for _, c := range chks { - testutil.Assert(t, meta.MinTime <= c.MinTime && c.MaxTime <= meta.MaxTime, + assert.True(t, meta.MinTime <= c.MinTime && c.MaxTime <= meta.MaxTime, "chunk spans beyond block boundaries: [block.MinTime=%d, block.MaxTime=%d]; [chunk.MinTime=%d, chunk.MaxTime=%d]", meta.MinTime, meta.MaxTime, c.MinTime, c.MaxTime) chunkCount++ } } - testutil.Assert(t, chunkCount == 1, "expected 1 chunk in block %s, got %d", meta.ULID, chunkCount) + assert.True(t, chunkCount == 1, "expected 1 chunk in block %s, got %d", meta.ULID, chunkCount) } } func TestQuerierWithBoundaryChunks(t *testing.T) { db := openTestDB(t, nil, nil) defer func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }() ctx := context.Background() @@ -1579,28 +1581,28 @@ func TestQuerierWithBoundaryChunks(t *testing.T) { for i := int64(0); i < 5; i++ { _, err := app.Add(label, i*blockRange, 0) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = app.Add(labels.FromStrings("blockID", strconv.FormatInt(i, 10)), i*blockRange, 0) - testutil.Ok(t, err) + assert.NoError(t, err) } err := app.Commit() - testutil.Ok(t, err) + assert.NoError(t, err) err = db.Compact() - testutil.Ok(t, err) + assert.NoError(t, err) - testutil.Assert(t, len(db.blocks) >= 3, "invalid test, less than three blocks in DB") + assert.True(t, len(db.blocks) >= 3, "invalid test, less than three blocks in DB") q, err := db.Querier(context.TODO(), blockRange, 2*blockRange) - testutil.Ok(t, err) + assert.NoError(t, err) defer q.Close() // The requested interval covers 2 blocks, so the querier's label values for blockID should give us 2 values, one from each block. b, ws, err := q.LabelValues("blockID") - testutil.Ok(t, err) - testutil.Equals(t, storage.Warnings(nil), ws) - testutil.Equals(t, []string{"1", "2"}, b) + assert.NoError(t, err) + assert.Equal(t, storage.Warnings(nil), ws) + assert.Equal(t, []string{"1", "2"}, b) } // TestInitializeHeadTimestamp ensures that the h.minTime is set properly. @@ -1611,38 +1613,38 @@ func TestQuerierWithBoundaryChunks(t *testing.T) { func TestInitializeHeadTimestamp(t *testing.T) { t.Run("clean", func(t *testing.T) { dir, err := ioutil.TempDir("", "test_head_init") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() db, err := Open(dir, nil, nil, nil) - testutil.Ok(t, err) + assert.NoError(t, err) defer db.Close() // Should be set to init values if no WAL or blocks exist so far. - testutil.Equals(t, int64(math.MaxInt64), db.head.MinTime()) - testutil.Equals(t, int64(math.MinInt64), db.head.MaxTime()) + assert.Equal(t, int64(math.MaxInt64), db.head.MinTime()) + assert.Equal(t, int64(math.MinInt64), db.head.MaxTime()) // First added sample initializes the writable range. ctx := context.Background() app := db.Appender(ctx) _, err = app.Add(labels.FromStrings("a", "b"), 1000, 1) - testutil.Ok(t, err) + assert.NoError(t, err) - testutil.Equals(t, int64(1000), db.head.MinTime()) - testutil.Equals(t, int64(1000), db.head.MaxTime()) + assert.Equal(t, int64(1000), db.head.MinTime()) + assert.Equal(t, int64(1000), db.head.MaxTime()) }) t.Run("wal-only", func(t *testing.T) { dir, err := ioutil.TempDir("", "test_head_init") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() - testutil.Ok(t, os.MkdirAll(path.Join(dir, "wal"), 0777)) + assert.NoError(t, os.MkdirAll(path.Join(dir, "wal"), 0777)) w, err := wal.New(nil, nil, path.Join(dir, "wal"), false) - testutil.Ok(t, err) + assert.NoError(t, err) var enc record.Encoder err = w.Log( @@ -1655,44 +1657,44 @@ func TestInitializeHeadTimestamp(t *testing.T) { {Ref: 124, T: 15000, V: 1}, }, nil), ) - testutil.Ok(t, err) - testutil.Ok(t, w.Close()) + assert.NoError(t, err) + assert.NoError(t, w.Close()) db, err := Open(dir, nil, nil, nil) - testutil.Ok(t, err) + assert.NoError(t, err) defer db.Close() - testutil.Equals(t, int64(5000), db.head.MinTime()) - testutil.Equals(t, int64(15000), db.head.MaxTime()) + assert.Equal(t, int64(5000), db.head.MinTime()) + assert.Equal(t, int64(15000), db.head.MaxTime()) }) t.Run("existing-block", func(t *testing.T) { dir, err := ioutil.TempDir("", "test_head_init") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() createBlock(t, dir, genSeries(1, 1, 1000, 2000)) db, err := Open(dir, nil, nil, nil) - testutil.Ok(t, err) + assert.NoError(t, err) defer db.Close() - testutil.Equals(t, int64(2000), db.head.MinTime()) - testutil.Equals(t, int64(2000), db.head.MaxTime()) + assert.Equal(t, int64(2000), db.head.MinTime()) + assert.Equal(t, int64(2000), db.head.MaxTime()) }) t.Run("existing-block-and-wal", func(t *testing.T) { dir, err := ioutil.TempDir("", "test_head_init") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() createBlock(t, dir, genSeries(1, 1, 1000, 6000)) - testutil.Ok(t, os.MkdirAll(path.Join(dir, "wal"), 0777)) + assert.NoError(t, os.MkdirAll(path.Join(dir, "wal"), 0777)) w, err := wal.New(nil, nil, path.Join(dir, "wal"), false) - testutil.Ok(t, err) + assert.NoError(t, err) var enc record.Encoder err = w.Log( @@ -1705,19 +1707,19 @@ func TestInitializeHeadTimestamp(t *testing.T) { {Ref: 124, T: 15000, V: 1}, }, nil), ) - testutil.Ok(t, err) - testutil.Ok(t, w.Close()) + assert.NoError(t, err) + assert.NoError(t, w.Close()) r := prometheus.NewRegistry() db, err := Open(dir, nil, r, nil) - testutil.Ok(t, err) + assert.NoError(t, err) defer db.Close() - testutil.Equals(t, int64(6000), db.head.MinTime()) - testutil.Equals(t, int64(15000), db.head.MaxTime()) + assert.Equal(t, int64(6000), db.head.MinTime()) + assert.Equal(t, int64(15000), db.head.MaxTime()) // Check that old series has been GCed. - testutil.Equals(t, 1.0, prom_testutil.ToFloat64(db.head.metrics.series)) + assert.Equal(t, 1.0, prom_testutil.ToFloat64(db.head.metrics.series)) }) } @@ -1725,7 +1727,7 @@ func TestNoEmptyBlocks(t *testing.T) { db := openTestDB(t, nil, []int64{100}) ctx := context.Background() defer func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }() db.DisableCompactions() @@ -1734,52 +1736,52 @@ func TestNoEmptyBlocks(t *testing.T) { defaultMatcher := labels.MustNewMatcher(labels.MatchRegexp, "", ".*") t.Run("Test no blocks after compact with empty head.", func(t *testing.T) { - testutil.Ok(t, db.Compact()) + assert.NoError(t, db.Compact()) actBlocks, err := blockDirs(db.Dir()) - testutil.Ok(t, err) - testutil.Equals(t, len(db.Blocks()), len(actBlocks)) - testutil.Equals(t, 0, len(actBlocks)) - testutil.Equals(t, 0, int(prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.ran)), "no compaction should be triggered here") + assert.NoError(t, err) + assert.Equal(t, len(db.Blocks()), len(actBlocks)) + assert.Equal(t, 0, len(actBlocks)) + assert.Equal(t, 0, int(prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.ran)), "no compaction should be triggered here") }) t.Run("Test no blocks after deleting all samples from head.", func(t *testing.T) { app := db.Appender(ctx) _, err := app.Add(defaultLabel, 1, 0) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = app.Add(defaultLabel, 2, 0) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = app.Add(defaultLabel, 3+rangeToTriggerCompaction, 0) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) - testutil.Ok(t, db.Delete(math.MinInt64, math.MaxInt64, defaultMatcher)) - testutil.Ok(t, db.Compact()) - testutil.Equals(t, 1, int(prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.ran)), "compaction should have been triggered here") + assert.NoError(t, err) + assert.NoError(t, app.Commit()) + assert.NoError(t, db.Delete(math.MinInt64, math.MaxInt64, defaultMatcher)) + assert.NoError(t, db.Compact()) + assert.Equal(t, 1, int(prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.ran)), "compaction should have been triggered here") actBlocks, err := blockDirs(db.Dir()) - testutil.Ok(t, err) - testutil.Equals(t, len(db.Blocks()), len(actBlocks)) - testutil.Equals(t, 0, len(actBlocks)) + assert.NoError(t, err) + assert.Equal(t, len(db.Blocks()), len(actBlocks)) + assert.Equal(t, 0, len(actBlocks)) app = db.Appender(ctx) _, err = app.Add(defaultLabel, 1, 0) - testutil.Assert(t, err == storage.ErrOutOfBounds, "the head should be truncated so no samples in the past should be allowed") + assert.True(t, err == storage.ErrOutOfBounds, "the head should be truncated so no samples in the past should be allowed") // Adding new blocks. currentTime := db.Head().MaxTime() _, err = app.Add(defaultLabel, currentTime, 0) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = app.Add(defaultLabel, currentTime+1, 0) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = app.Add(defaultLabel, currentTime+rangeToTriggerCompaction, 0) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) - testutil.Ok(t, db.Compact()) - testutil.Equals(t, 2, int(prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.ran)), "compaction should have been triggered here") + assert.NoError(t, db.Compact()) + assert.Equal(t, 2, int(prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.ran)), "compaction should have been triggered here") actBlocks, err = blockDirs(db.Dir()) - testutil.Ok(t, err) - testutil.Equals(t, len(db.Blocks()), len(actBlocks)) - testutil.Assert(t, len(actBlocks) == 1, "No blocks created when compacting with >0 samples") + assert.NoError(t, err) + assert.Equal(t, len(db.Blocks()), len(actBlocks)) + assert.True(t, len(actBlocks) == 1, "No blocks created when compacting with >0 samples") }) t.Run(`When no new block is created from head, and there are some blocks on disk @@ -1788,16 +1790,16 @@ func TestNoEmptyBlocks(t *testing.T) { app := db.Appender(ctx) currentTime := db.Head().MaxTime() _, err := app.Add(defaultLabel, currentTime, 0) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = app.Add(defaultLabel, currentTime+1, 0) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = app.Add(defaultLabel, currentTime+rangeToTriggerCompaction, 0) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) - testutil.Ok(t, db.head.Delete(math.MinInt64, math.MaxInt64, defaultMatcher)) - testutil.Ok(t, db.Compact()) - testutil.Equals(t, 3, int(prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.ran)), "compaction should have been triggered here") - testutil.Equals(t, oldBlocks, db.Blocks()) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) + assert.NoError(t, db.head.Delete(math.MinInt64, math.MaxInt64, defaultMatcher)) + assert.NoError(t, db.Compact()) + assert.Equal(t, 3, int(prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.ran)), "compaction should have been triggered here") + assert.Equal(t, oldBlocks, db.Blocks()) }) t.Run("Test no blocks remaining after deleting all samples from disk.", func(t *testing.T) { @@ -1811,16 +1813,16 @@ func TestNoEmptyBlocks(t *testing.T) { } oldBlocks := db.Blocks() - testutil.Ok(t, db.reloadBlocks()) // Reload the db to register the new blocks. - testutil.Equals(t, len(blocks)+len(oldBlocks), len(db.Blocks())) // Ensure all blocks are registered. - testutil.Ok(t, db.Delete(math.MinInt64, math.MaxInt64, defaultMatcher)) - testutil.Ok(t, db.Compact()) - testutil.Equals(t, 5, int(prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.ran)), "compaction should have been triggered here once for each block that have tombstones") + assert.NoError(t, db.reloadBlocks()) // Reload the db to register the new blocks. + assert.Equal(t, len(blocks)+len(oldBlocks), len(db.Blocks())) // Ensure all blocks are registered. + assert.NoError(t, db.Delete(math.MinInt64, math.MaxInt64, defaultMatcher)) + assert.NoError(t, db.Compact()) + assert.Equal(t, 5, int(prom_testutil.ToFloat64(db.compactor.(*LeveledCompactor).metrics.ran)), "compaction should have been triggered here once for each block that have tombstones") actBlocks, err := blockDirs(db.Dir()) - testutil.Ok(t, err) - testutil.Equals(t, len(db.Blocks()), len(actBlocks)) - testutil.Equals(t, 1, len(actBlocks), "All samples are deleted. Only the most recent block should remain after compaction.") + assert.NoError(t, err) + assert.Equal(t, len(db.Blocks()), len(actBlocks)) + assert.Equal(t, 1, len(actBlocks), "All samples are deleted. Only the most recent block should remain after compaction.") }) } @@ -1874,40 +1876,40 @@ func TestDB_LabelNames(t *testing.T) { for _, tuple := range sampleLabels { label := labels.FromStrings(tuple[0], tuple[1]) _, err := app.Add(label, i*blockRange, 0) - testutil.Ok(t, err) + assert.NoError(t, err) } } err := app.Commit() - testutil.Ok(t, err) + assert.NoError(t, err) } for _, tst := range tests { db := openTestDB(t, nil, nil) defer func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }() appendSamples(db, 0, 4, tst.sampleLabels1) // Testing head. headIndexr, err := db.head.Index() - testutil.Ok(t, err) + assert.NoError(t, err) labelNames, err := headIndexr.LabelNames() - testutil.Ok(t, err) - testutil.Equals(t, tst.exp1, labelNames) - testutil.Ok(t, headIndexr.Close()) + assert.NoError(t, err) + assert.Equal(t, tst.exp1, labelNames) + assert.NoError(t, headIndexr.Close()) // Testing disk. err = db.Compact() - testutil.Ok(t, err) + assert.NoError(t, err) // All blocks have same label names, hence check them individually. // No need to aggregate and check. for _, b := range db.Blocks() { blockIndexr, err := b.Index() - testutil.Ok(t, err) + assert.NoError(t, err) labelNames, err = blockIndexr.LabelNames() - testutil.Ok(t, err) - testutil.Equals(t, tst.exp1, labelNames) - testutil.Ok(t, blockIndexr.Close()) + assert.NoError(t, err) + assert.Equal(t, tst.exp1, labelNames) + assert.NoError(t, blockIndexr.Close()) } // Adding more samples to head with new label names @@ -1916,20 +1918,20 @@ func TestDB_LabelNames(t *testing.T) { // Testing DB (union). q, err := db.Querier(context.TODO(), math.MinInt64, math.MaxInt64) - testutil.Ok(t, err) + assert.NoError(t, err) var ws storage.Warnings labelNames, ws, err = q.LabelNames() - testutil.Ok(t, err) - testutil.Equals(t, 0, len(ws)) - testutil.Ok(t, q.Close()) - testutil.Equals(t, tst.exp2, labelNames) + assert.NoError(t, err) + assert.Equal(t, 0, len(ws)) + assert.NoError(t, q.Close()) + assert.Equal(t, tst.exp2, labelNames) } } func TestCorrectNumTombstones(t *testing.T) { db := openTestDB(t, nil, nil) defer func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }() blockRange := db.compactor.(*LeveledCompactor).ranges[0] @@ -1941,27 +1943,27 @@ func TestCorrectNumTombstones(t *testing.T) { for i := int64(0); i < 3; i++ { for j := int64(0); j < 15; j++ { _, err := app.Add(defaultLabel, i*blockRange+j, 0) - testutil.Ok(t, err) + assert.NoError(t, err) } } - testutil.Ok(t, app.Commit()) + assert.NoError(t, app.Commit()) err := db.Compact() - testutil.Ok(t, err) - testutil.Equals(t, 1, len(db.blocks)) + assert.NoError(t, err) + assert.Equal(t, 1, len(db.blocks)) - testutil.Ok(t, db.Delete(0, 1, defaultMatcher)) - testutil.Equals(t, uint64(1), db.blocks[0].meta.Stats.NumTombstones) + assert.NoError(t, db.Delete(0, 1, defaultMatcher)) + assert.Equal(t, uint64(1), db.blocks[0].meta.Stats.NumTombstones) // {0, 1} and {2, 3} are merged to form 1 tombstone. - testutil.Ok(t, db.Delete(2, 3, defaultMatcher)) - testutil.Equals(t, uint64(1), db.blocks[0].meta.Stats.NumTombstones) + assert.NoError(t, db.Delete(2, 3, defaultMatcher)) + assert.Equal(t, uint64(1), db.blocks[0].meta.Stats.NumTombstones) - testutil.Ok(t, db.Delete(5, 6, defaultMatcher)) - testutil.Equals(t, uint64(2), db.blocks[0].meta.Stats.NumTombstones) + assert.NoError(t, db.Delete(5, 6, defaultMatcher)) + assert.Equal(t, uint64(2), db.blocks[0].meta.Stats.NumTombstones) - testutil.Ok(t, db.Delete(9, 11, defaultMatcher)) - testutil.Equals(t, uint64(3), db.blocks[0].meta.Stats.NumTombstones) + assert.NoError(t, db.Delete(9, 11, defaultMatcher)) + assert.Equal(t, uint64(3), db.blocks[0].meta.Stats.NumTombstones) } // TestBlockRanges checks the following use cases: @@ -1978,14 +1980,14 @@ func TestBlockRanges(t *testing.T) { ctx := context.Background() dir, err := ioutil.TempDir("", "test_storage") - testutil.Ok(t, err) + assert.NoError(t, err) // Test that the compactor doesn't create overlapping blocks // when a non standard block already exists. firstBlockMaxT := int64(3) createBlock(t, dir, genSeries(1, 1, 0, firstBlockMaxT)) db, err := open(dir, logger, nil, DefaultOptions(), []int64{10000}) - testutil.Ok(t, err) + assert.NoError(t, err) rangeToTriggerCompaction := db.compactor.(*LeveledCompactor).ranges[0]/2*3 + 1 defer func() { @@ -1998,21 +2000,21 @@ func TestBlockRanges(t *testing.T) { t.Fatalf("appending a sample with a timestamp covered by a previous block shouldn't be possible") } _, err = app.Add(lbl, firstBlockMaxT+1, rand.Float64()) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = app.Add(lbl, firstBlockMaxT+2, rand.Float64()) - testutil.Ok(t, err) + assert.NoError(t, err) secondBlockMaxt := firstBlockMaxT + rangeToTriggerCompaction _, err = app.Add(lbl, secondBlockMaxt, rand.Float64()) // Add samples to trigger a new compaction - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) for x := 0; x < 100; x++ { if len(db.Blocks()) == 2 { break } time.Sleep(100 * time.Millisecond) } - testutil.Equals(t, 2, len(db.Blocks()), "no new block created after the set timeout") + assert.Equal(t, 2, len(db.Blocks()), "no new block created after the set timeout") if db.Blocks()[0].Meta().MaxTime > db.Blocks()[1].Meta().MinTime { t.Fatalf("new block overlaps old:%v,new:%v", db.Blocks()[0].Meta(), db.Blocks()[1].Meta()) @@ -2023,30 +2025,30 @@ func TestBlockRanges(t *testing.T) { app = db.Appender(ctx) db.DisableCompactions() _, err = app.Add(lbl, secondBlockMaxt+1, rand.Float64()) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = app.Add(lbl, secondBlockMaxt+2, rand.Float64()) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = app.Add(lbl, secondBlockMaxt+3, rand.Float64()) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = app.Add(lbl, secondBlockMaxt+4, rand.Float64()) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) - testutil.Ok(t, db.Close()) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) + assert.NoError(t, db.Close()) thirdBlockMaxt := secondBlockMaxt + 2 createBlock(t, dir, genSeries(1, 1, secondBlockMaxt+1, thirdBlockMaxt)) db, err = open(dir, logger, nil, DefaultOptions(), []int64{10000}) - testutil.Ok(t, err) + assert.NoError(t, err) defer db.Close() - testutil.Equals(t, 3, len(db.Blocks()), "db doesn't include expected number of blocks") - testutil.Equals(t, db.Blocks()[2].Meta().MaxTime, thirdBlockMaxt, "unexpected maxt of the last block") + assert.Equal(t, 3, len(db.Blocks()), "db doesn't include expected number of blocks") + assert.Equal(t, db.Blocks()[2].Meta().MaxTime, thirdBlockMaxt, "unexpected maxt of the last block") app = db.Appender(ctx) _, err = app.Add(lbl, thirdBlockMaxt+rangeToTriggerCompaction, rand.Float64()) // Trigger a compaction - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) for x := 0; x < 100; x++ { if len(db.Blocks()) == 4 { break @@ -2054,7 +2056,7 @@ func TestBlockRanges(t *testing.T) { time.Sleep(100 * time.Millisecond) } - testutil.Equals(t, 4, len(db.Blocks()), "no new block created after the set timeout") + assert.Equal(t, 4, len(db.Blocks()), "no new block created after the set timeout") if db.Blocks()[2].Meta().MaxTime > db.Blocks()[3].Meta().MinTime { t.Fatalf("new block overlaps old:%v,new:%v", db.Blocks()[2].Meta(), db.Blocks()[3].Meta()) @@ -2078,10 +2080,10 @@ func TestDBReadOnly(t *testing.T) { // Bootstrap the db. { dbDir, err = ioutil.TempDir("", "test") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dbDir)) + assert.NoError(t, os.RemoveAll(dbDir)) }() dbBlocks := []*BlockMeta{ @@ -2097,74 +2099,74 @@ func TestDBReadOnly(t *testing.T) { // Add head to test DBReadOnly WAL reading capabilities. w, err := wal.New(logger, nil, filepath.Join(dbDir, "wal"), true) - testutil.Ok(t, err) + assert.NoError(t, err) h := createHead(t, w, genSeries(1, 1, 16, 18), dbDir) - testutil.Ok(t, h.Close()) + assert.NoError(t, h.Close()) } // Open a normal db to use for a comparison. { dbWritable, err := Open(dbDir, logger, nil, nil) - testutil.Ok(t, err) + assert.NoError(t, err) dbWritable.DisableCompactions() dbSizeBeforeAppend, err := fileutil.DirSize(dbWritable.Dir()) - testutil.Ok(t, err) + assert.NoError(t, err) app := dbWritable.Appender(context.Background()) _, err = app.Add(labels.FromStrings("foo", "bar"), dbWritable.Head().MaxTime()+1, 0) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) expBlocks = dbWritable.Blocks() expDbSize, err := fileutil.DirSize(dbWritable.Dir()) - testutil.Ok(t, err) - testutil.Assert(t, expDbSize > dbSizeBeforeAppend, "db size didn't increase after an append") + assert.NoError(t, err) + assert.True(t, expDbSize > dbSizeBeforeAppend, "db size didn't increase after an append") q, err := dbWritable.Querier(context.TODO(), math.MinInt64, math.MaxInt64) - testutil.Ok(t, err) + assert.NoError(t, err) expSeries = query(t, q, matchAll) cq, err := dbWritable.ChunkQuerier(context.TODO(), math.MinInt64, math.MaxInt64) - testutil.Ok(t, err) + assert.NoError(t, err) expChunks = queryChunks(t, cq, matchAll) - testutil.Ok(t, dbWritable.Close()) // Close here to allow getting the dir hash for windows. + assert.NoError(t, dbWritable.Close()) // Close here to allow getting the dir hash for windows. expDBHash = testutil.DirHash(t, dbWritable.Dir()) } // Open a read only db and ensure that the API returns the same result as the normal DB. dbReadOnly, err := OpenDBReadOnly(dbDir, logger) - testutil.Ok(t, err) - defer func() { testutil.Ok(t, dbReadOnly.Close()) }() + assert.NoError(t, err) + defer func() { assert.NoError(t, dbReadOnly.Close()) }() t.Run("blocks", func(t *testing.T) { blocks, err := dbReadOnly.Blocks() - testutil.Ok(t, err) - testutil.Equals(t, len(expBlocks), len(blocks)) + assert.NoError(t, err) + assert.Equal(t, len(expBlocks), len(blocks)) for i, expBlock := range expBlocks { - testutil.Equals(t, expBlock.Meta(), blocks[i].Meta(), "block meta mismatch") + assert.Equal(t, expBlock.Meta(), blocks[i].Meta(), "block meta mismatch") } }) t.Run("querier", func(t *testing.T) { // Open a read only db and ensure that the API returns the same result as the normal DB. q, err := dbReadOnly.Querier(context.TODO(), math.MinInt64, math.MaxInt64) - testutil.Ok(t, err) + assert.NoError(t, err) readOnlySeries := query(t, q, matchAll) readOnlyDBHash := testutil.DirHash(t, dbDir) - testutil.Equals(t, len(expSeries), len(readOnlySeries), "total series mismatch") - testutil.Equals(t, expSeries, readOnlySeries, "series mismatch") - testutil.Equals(t, expDBHash, readOnlyDBHash, "after all read operations the db hash should remain the same") + assert.Equal(t, len(expSeries), len(readOnlySeries), "total series mismatch") + assert.Equal(t, expSeries, readOnlySeries, "series mismatch") + assert.Equal(t, expDBHash, readOnlyDBHash, "after all read operations the db hash should remain the same") }) t.Run("chunk querier", func(t *testing.T) { cq, err := dbReadOnly.ChunkQuerier(context.TODO(), math.MinInt64, math.MaxInt64) - testutil.Ok(t, err) + assert.NoError(t, err) readOnlySeries := queryChunks(t, cq, matchAll) readOnlyDBHash := testutil.DirHash(t, dbDir) - testutil.Equals(t, len(expChunks), len(readOnlySeries), "total series mismatch") - testutil.Equals(t, expChunks, readOnlySeries, "series chunks mismatch") - testutil.Equals(t, expDBHash, readOnlyDBHash, "after all read operations the db hash should remain the same") + assert.Equal(t, len(expChunks), len(readOnlySeries), "total series mismatch") + assert.Equal(t, expChunks, readOnlySeries, "series chunks mismatch") + assert.Equal(t, expDBHash, readOnlyDBHash, "after all read operations the db hash should remain the same") }) } @@ -2172,19 +2174,19 @@ func TestDBReadOnly(t *testing.T) { // all api methods return an ErrClosed. func TestDBReadOnlyClosing(t *testing.T) { dbDir, err := ioutil.TempDir("", "test") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dbDir)) + assert.NoError(t, os.RemoveAll(dbDir)) }() db, err := OpenDBReadOnly(dbDir, log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr))) - testutil.Ok(t, err) - testutil.Ok(t, db.Close()) - testutil.Equals(t, db.Close(), ErrClosed) + assert.NoError(t, err) + assert.NoError(t, db.Close()) + assert.Equal(t, db.Close(), ErrClosed) _, err = db.Blocks() - testutil.Equals(t, err, ErrClosed) + assert.Equal(t, err, ErrClosed) _, err = db.Querier(context.TODO(), 0, 1) - testutil.Equals(t, err, ErrClosed) + assert.Equal(t, err, ErrClosed) } func TestDBReadOnly_FlushWAL(t *testing.T) { @@ -2199,50 +2201,50 @@ func TestDBReadOnly_FlushWAL(t *testing.T) { // Bootstrap the db. { dbDir, err = ioutil.TempDir("", "test") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dbDir)) + assert.NoError(t, os.RemoveAll(dbDir)) }() // Append data to the WAL. db, err := Open(dbDir, logger, nil, nil) - testutil.Ok(t, err) + assert.NoError(t, err) db.DisableCompactions() app := db.Appender(ctx) maxt = 1000 for i := 0; i < maxt; i++ { _, err := app.Add(labels.FromStrings(defaultLabelName, "flush"), int64(i), 1.0) - testutil.Ok(t, err) + assert.NoError(t, err) } - testutil.Ok(t, app.Commit()) - defer func() { testutil.Ok(t, db.Close()) }() + assert.NoError(t, app.Commit()) + defer func() { assert.NoError(t, db.Close()) }() } // Flush WAL. db, err := OpenDBReadOnly(dbDir, logger) - testutil.Ok(t, err) + assert.NoError(t, err) flush, err := ioutil.TempDir("", "flush") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(flush)) + assert.NoError(t, os.RemoveAll(flush)) }() - testutil.Ok(t, db.FlushWAL(flush)) - testutil.Ok(t, db.Close()) + assert.NoError(t, db.FlushWAL(flush)) + assert.NoError(t, db.Close()) // Reopen the DB from the flushed WAL block. db, err = OpenDBReadOnly(flush, logger) - testutil.Ok(t, err) - defer func() { testutil.Ok(t, db.Close()) }() + assert.NoError(t, err) + defer func() { assert.NoError(t, db.Close()) }() blocks, err := db.Blocks() - testutil.Ok(t, err) - testutil.Equals(t, len(blocks), 1) + assert.NoError(t, err) + assert.Equal(t, len(blocks), 1) querier, err := db.Querier(context.TODO(), 0, int64(maxt)-1) - testutil.Ok(t, err) - defer func() { testutil.Ok(t, querier.Close()) }() + assert.NoError(t, err) + defer func() { assert.NoError(t, querier.Close()) }() // Sum the values. seriesSet := querier.Select(false, nil, labels.MustNewMatcher(labels.MatchEqual, defaultLabelName, "flush")) @@ -2254,21 +2256,21 @@ func TestDBReadOnly_FlushWAL(t *testing.T) { _, v := series.At() sum += v } - testutil.Ok(t, series.Err()) + assert.NoError(t, series.Err()) } - testutil.Ok(t, seriesSet.Err()) - testutil.Equals(t, 0, len(seriesSet.Warnings())) - testutil.Equals(t, 1000.0, sum) + assert.NoError(t, seriesSet.Err()) + assert.Equal(t, 0, len(seriesSet.Warnings())) + assert.Equal(t, 1000.0, sum) } func TestDBCannotSeePartialCommits(t *testing.T) { tmpdir, _ := ioutil.TempDir("", "test") defer func() { - testutil.Ok(t, os.RemoveAll(tmpdir)) + assert.NoError(t, os.RemoveAll(tmpdir)) }() db, err := Open(tmpdir, nil, nil, nil) - testutil.Ok(t, err) + assert.NoError(t, err) defer db.Close() stop := make(chan struct{}) @@ -2283,10 +2285,10 @@ func TestDBCannotSeePartialCommits(t *testing.T) { for j := 0; j < 100; j++ { _, err := app.Add(labels.FromStrings("foo", "bar", "a", strconv.Itoa(j)), int64(iter), float64(iter)) - testutil.Ok(t, err) + assert.NoError(t, err) } err = app.Commit() - testutil.Ok(t, err) + assert.NoError(t, err) if iter == 0 { close(firstInsert) @@ -2309,13 +2311,13 @@ func TestDBCannotSeePartialCommits(t *testing.T) { for i := 0; i < 10; i++ { func() { querier, err := db.Querier(context.Background(), 0, 1000000) - testutil.Ok(t, err) + assert.NoError(t, err) defer querier.Close() ss := querier.Select(false, nil, labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")) _, seriesSet, ws, err := expandSeriesSet(ss) - testutil.Ok(t, err) - testutil.Equals(t, 0, len(ws)) + assert.NoError(t, err) + assert.Equal(t, 0, len(ws)) values := map[float64]struct{}{} for _, series := range seriesSet { @@ -2328,73 +2330,73 @@ func TestDBCannotSeePartialCommits(t *testing.T) { } stop <- struct{}{} - testutil.Equals(t, 0, inconsistencies, "Some queries saw inconsistent results.") + assert.Equal(t, 0, inconsistencies, "Some queries saw inconsistent results.") } func TestDBQueryDoesntSeeAppendsAfterCreation(t *testing.T) { tmpdir, _ := ioutil.TempDir("", "test") defer func() { - testutil.Ok(t, os.RemoveAll(tmpdir)) + assert.NoError(t, os.RemoveAll(tmpdir)) }() db, err := Open(tmpdir, nil, nil, nil) - testutil.Ok(t, err) + assert.NoError(t, err) defer db.Close() querierBeforeAdd, err := db.Querier(context.Background(), 0, 1000000) - testutil.Ok(t, err) + assert.NoError(t, err) defer querierBeforeAdd.Close() ctx := context.Background() app := db.Appender(ctx) _, err = app.Add(labels.FromStrings("foo", "bar"), 0, 0) - testutil.Ok(t, err) + assert.NoError(t, err) querierAfterAddButBeforeCommit, err := db.Querier(context.Background(), 0, 1000000) - testutil.Ok(t, err) + assert.NoError(t, err) defer querierAfterAddButBeforeCommit.Close() // None of the queriers should return anything after the Add but before the commit. ss := querierBeforeAdd.Select(false, nil, labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")) _, seriesSet, ws, err := expandSeriesSet(ss) - testutil.Ok(t, err) - testutil.Equals(t, 0, len(ws)) - testutil.Equals(t, map[string][]sample{}, seriesSet) + assert.NoError(t, err) + assert.Equal(t, 0, len(ws)) + assert.Equal(t, map[string][]sample{}, seriesSet) ss = querierAfterAddButBeforeCommit.Select(false, nil, labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")) _, seriesSet, ws, err = expandSeriesSet(ss) - testutil.Ok(t, err) - testutil.Equals(t, 0, len(ws)) - testutil.Equals(t, map[string][]sample{}, seriesSet) + assert.NoError(t, err) + assert.Equal(t, 0, len(ws)) + assert.Equal(t, map[string][]sample{}, seriesSet) // This commit is after the queriers are created, so should not be returned. err = app.Commit() - testutil.Ok(t, err) + assert.NoError(t, err) // Nothing returned for querier created before the Add. ss = querierBeforeAdd.Select(false, nil, labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")) _, seriesSet, ws, err = expandSeriesSet(ss) - testutil.Ok(t, err) - testutil.Equals(t, 0, len(ws)) - testutil.Equals(t, map[string][]sample{}, seriesSet) + assert.NoError(t, err) + assert.Equal(t, 0, len(ws)) + assert.Equal(t, map[string][]sample{}, seriesSet) // Series exists but has no samples for querier created after Add. ss = querierAfterAddButBeforeCommit.Select(false, nil, labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")) _, seriesSet, ws, err = expandSeriesSet(ss) - testutil.Ok(t, err) - testutil.Equals(t, 0, len(ws)) - testutil.Equals(t, map[string][]sample{`{foo="bar"}`: {}}, seriesSet) + assert.NoError(t, err) + assert.Equal(t, 0, len(ws)) + assert.Equal(t, map[string][]sample{`{foo="bar"}`: {}}, seriesSet) querierAfterCommit, err := db.Querier(context.Background(), 0, 1000000) - testutil.Ok(t, err) + assert.NoError(t, err) defer querierAfterCommit.Close() // Samples are returned for querier created after Commit. ss = querierAfterCommit.Select(false, nil, labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")) _, seriesSet, ws, err = expandSeriesSet(ss) - testutil.Ok(t, err) - testutil.Equals(t, 0, len(ws)) - testutil.Equals(t, map[string][]sample{`{foo="bar"}`: {{t: 0, v: 0}}}, seriesSet) + assert.NoError(t, err) + assert.Equal(t, 0, len(ws)) + assert.Equal(t, map[string][]sample{`{foo="bar"}`: {{t: 0, v: 0}}}, seriesSet) } // TestChunkWriter_ReadAfterWrite ensures that chunk segment are cut at the set segment size and @@ -2524,20 +2526,20 @@ func TestChunkWriter_ReadAfterWrite(t *testing.T) { t.Run(strconv.Itoa(i), func(t *testing.T) { tempDir, err := ioutil.TempDir("", "test_chunk_writer") - testutil.Ok(t, err) - defer func() { testutil.Ok(t, os.RemoveAll(tempDir)) }() + assert.NoError(t, err) + defer func() { assert.NoError(t, os.RemoveAll(tempDir)) }() chunkw, err := chunks.NewWriterWithSegSize(tempDir, chunks.SegmentHeaderSize+int64(test.segmentSize)) - testutil.Ok(t, err) + assert.NoError(t, err) for _, chks := range test.chks { - testutil.Ok(t, chunkw.WriteChunks(chks...)) + assert.NoError(t, chunkw.WriteChunks(chks...)) } - testutil.Ok(t, chunkw.Close()) + assert.NoError(t, chunkw.Close()) files, err := ioutil.ReadDir(tempDir) - testutil.Ok(t, err) - testutil.Equals(t, test.expSegmentsCount, len(files), "expected segments count mismatch") + assert.NoError(t, err) + assert.Equal(t, test.expSegmentsCount, len(files), "expected segments count mismatch") // Verify that all data is written to the segments. sizeExp := 0 @@ -2557,22 +2559,22 @@ func TestChunkWriter_ReadAfterWrite(t *testing.T) { for i, f := range files { size := int(f.Size()) // Verify that the segment is the same or smaller than the expected size. - testutil.Assert(t, chunks.SegmentHeaderSize+test.expSegmentSizes[i] >= size, "Segment:%v should NOT be bigger than:%v actual:%v", i, chunks.SegmentHeaderSize+test.expSegmentSizes[i], size) + assert.True(t, chunks.SegmentHeaderSize+test.expSegmentSizes[i] >= size, "Segment:%v should NOT be bigger than:%v actual:%v", i, chunks.SegmentHeaderSize+test.expSegmentSizes[i], size) sizeAct += size } - testutil.Equals(t, sizeExp, sizeAct) + assert.Equal(t, sizeExp, sizeAct) // Check the content of the chunks. r, err := chunks.NewDirReader(tempDir, nil) - testutil.Ok(t, err) - defer func() { testutil.Ok(t, r.Close()) }() + assert.NoError(t, err) + defer func() { assert.NoError(t, r.Close()) }() for _, chks := range test.chks { for _, chkExp := range chks { chkAct, err := r.Chunk(chkExp.Ref) - testutil.Ok(t, err) - testutil.Equals(t, chkExp.Chunk.Bytes(), chkAct.Bytes()) + assert.NoError(t, err) + assert.Equal(t, chkExp.Chunk.Bytes(), chkAct.Bytes()) } } }) @@ -2597,7 +2599,7 @@ func TestRangeForTimestamp(t *testing.T) { } for _, tt := range tests { got := rangeForTimestamp(tt.args.t, tt.args.width) - testutil.Equals(t, tt.expected, got) + assert.Equal(t, tt.expected, got) } } @@ -2613,17 +2615,17 @@ func TestChunkReader_ConcurrentReads(t *testing.T) { } tempDir, err := ioutil.TempDir("", "test_chunk_writer") - testutil.Ok(t, err) - defer func() { testutil.Ok(t, os.RemoveAll(tempDir)) }() + assert.NoError(t, err) + defer func() { assert.NoError(t, os.RemoveAll(tempDir)) }() chunkw, err := chunks.NewWriter(tempDir) - testutil.Ok(t, err) + assert.NoError(t, err) - testutil.Ok(t, chunkw.WriteChunks(chks...)) - testutil.Ok(t, chunkw.Close()) + assert.NoError(t, chunkw.WriteChunks(chks...)) + assert.NoError(t, chunkw.Close()) r, err := chunks.NewDirReader(tempDir, nil) - testutil.Ok(t, err) + assert.NoError(t, err) var wg sync.WaitGroup for _, chk := range chks { @@ -2633,13 +2635,13 @@ func TestChunkReader_ConcurrentReads(t *testing.T) { defer wg.Done() chkAct, err := r.Chunk(chunk.Ref) - testutil.Ok(t, err) - testutil.Equals(t, chunk.Chunk.Bytes(), chkAct.Bytes()) + assert.NoError(t, err) + assert.Equal(t, chunk.Chunk.Bytes(), chkAct.Bytes()) }(chk) } wg.Wait() } - testutil.Ok(t, r.Close()) + assert.NoError(t, r.Close()) } // TestCompactHead ensures that the head compaction @@ -2652,8 +2654,8 @@ func TestChunkReader_ConcurrentReads(t *testing.T) { // * queries the db to ensure the samples are present from the compacted head. func TestCompactHead(t *testing.T) { dbDir, err := ioutil.TempDir("", "testFlush") - testutil.Ok(t, err) - defer func() { testutil.Ok(t, os.RemoveAll(dbDir)) }() + assert.NoError(t, err) + defer func() { assert.NoError(t, os.RemoveAll(dbDir)) }() // Open a DB and append data to the WAL. tsdbCfg := &Options{ @@ -2665,7 +2667,7 @@ func TestCompactHead(t *testing.T) { } db, err := Open(dbDir, log.NewNopLogger(), prometheus.NewRegistry(), tsdbCfg) - testutil.Ok(t, err) + assert.NoError(t, err) ctx := context.Background() app := db.Appender(ctx) var expSamples []sample @@ -2673,26 +2675,26 @@ func TestCompactHead(t *testing.T) { for i := 0; i < maxt; i++ { val := rand.Float64() _, err := app.Add(labels.FromStrings("a", "b"), int64(i), val) - testutil.Ok(t, err) + assert.NoError(t, err) expSamples = append(expSamples, sample{int64(i), val}) } - testutil.Ok(t, app.Commit()) + assert.NoError(t, app.Commit()) // Compact the Head to create a new block. - testutil.Ok(t, db.CompactHead(NewRangeHead(db.Head(), 0, int64(maxt)-1))) - testutil.Ok(t, db.Close()) + assert.NoError(t, db.CompactHead(NewRangeHead(db.Head(), 0, int64(maxt)-1))) + assert.NoError(t, db.Close()) // Delete everything but the new block and // reopen the db to query it to ensure it includes the head data. - testutil.Ok(t, deleteNonBlocks(db.Dir())) + assert.NoError(t, deleteNonBlocks(db.Dir())) db, err = Open(dbDir, log.NewNopLogger(), prometheus.NewRegistry(), tsdbCfg) - testutil.Ok(t, err) - testutil.Equals(t, 1, len(db.Blocks())) - testutil.Equals(t, int64(maxt), db.Head().MinTime()) - defer func() { testutil.Ok(t, db.Close()) }() + assert.NoError(t, err) + assert.Equal(t, 1, len(db.Blocks())) + assert.Equal(t, int64(maxt), db.Head().MinTime()) + defer func() { assert.NoError(t, db.Close()) }() querier, err := db.Querier(context.Background(), 0, int64(maxt)-1) - testutil.Ok(t, err) - defer func() { testutil.Ok(t, querier.Close()) }() + assert.NoError(t, err) + defer func() { assert.NoError(t, querier.Close()) }() seriesSet := querier.Select(false, nil, &labels.Matcher{Type: labels.MatchEqual, Name: "a", Value: "b"}) var actSamples []sample @@ -2703,10 +2705,10 @@ func TestCompactHead(t *testing.T) { time, val := series.At() actSamples = append(actSamples, sample{int64(time), val}) } - testutil.Ok(t, series.Err()) + assert.NoError(t, series.Err()) } - testutil.Equals(t, expSamples, actSamples) - testutil.Ok(t, seriesSet.Err()) + assert.Equal(t, expSamples, actSamples) + assert.NoError(t, seriesSet.Err()) } func deleteNonBlocks(dbDir string) error { @@ -2735,9 +2737,9 @@ func deleteNonBlocks(dbDir string) error { func TestOpen_VariousBlockStates(t *testing.T) { tmpDir, err := ioutil.TempDir("", "test") - testutil.Ok(t, err) + assert.NoError(t, err) t.Cleanup(func() { - testutil.Ok(t, os.RemoveAll(tmpDir)) + assert.NoError(t, os.RemoveAll(tmpDir)) }) var ( @@ -2758,29 +2760,29 @@ func TestOpen_VariousBlockStates(t *testing.T) { expectedLoadedDirs[outDir] = struct{}{} // Touch chunks dir in block. - testutil.Ok(t, os.MkdirAll(filepath.Join(dbDir, "chunks"), 0777)) + assert.NoError(t, os.MkdirAll(filepath.Join(dbDir, "chunks"), 0777)) defer func() { - testutil.Ok(t, os.RemoveAll(filepath.Join(dbDir, "chunks"))) + assert.NoError(t, os.RemoveAll(filepath.Join(dbDir, "chunks"))) }() - testutil.Ok(t, os.Mkdir(outDir, os.ModePerm)) - testutil.Ok(t, fileutil.CopyDirs(dbDir, outDir)) + assert.NoError(t, os.Mkdir(outDir, os.ModePerm)) + assert.NoError(t, fileutil.CopyDirs(dbDir, outDir)) } { // Missing meta.json; should be ignored and only logged. // TODO(bwplotka): Probably add metric. dir := createBlock(t, tmpDir, genSeries(10, 2, 20, 30)) expectedIgnoredDirs[dir] = struct{}{} - testutil.Ok(t, os.Remove(filepath.Join(dir, metaFilename))) + assert.NoError(t, os.Remove(filepath.Join(dir, metaFilename))) } { // Tmp blocks during creation & deletion; those should be removed on start. dir := createBlock(t, tmpDir, genSeries(10, 2, 30, 40)) - testutil.Ok(t, fileutil.Replace(dir, dir+tmpForCreationBlockDirSuffix)) + assert.NoError(t, fileutil.Replace(dir, dir+tmpForCreationBlockDirSuffix)) expectedRemovedDirs[dir+tmpForCreationBlockDirSuffix] = struct{}{} // Tmp blocks during creation & deletion; those should be removed on start. dir = createBlock(t, tmpDir, genSeries(10, 2, 40, 50)) - testutil.Ok(t, fileutil.Replace(dir, dir+tmpForDeletionBlockDirSuffix)) + assert.NoError(t, fileutil.Replace(dir, dir+tmpForDeletionBlockDirSuffix)) expectedRemovedDirs[dir+tmpForDeletionBlockDirSuffix] = struct{}{} } { @@ -2789,7 +2791,7 @@ func TestOpen_VariousBlockStates(t *testing.T) { expectedLoadedDirs[dir] = struct{}{} m, _, err := readMetaFile(dir) - testutil.Ok(t, err) + assert.NoError(t, err) compacted := createBlock(t, tmpDir, genSeries(10, 2, 50, 55)) expectedRemovedDirs[compacted] = struct{}{} @@ -2804,13 +2806,13 @@ func TestOpen_VariousBlockStates(t *testing.T) { m.Compaction.Parents = append(m.Compaction.Parents, BlockDesc{ULID: ulid.MustParse(filepath.Base(compacted))}) m.Compaction.Parents = append(m.Compaction.Parents, BlockDesc{ULID: ulid.MustParse(filepath.Base(compacted))}) _, err = writeMetaFile(log.NewLogfmtLogger(os.Stderr), dir, m) - testutil.Ok(t, err) + assert.NoError(t, err) } opts := DefaultOptions() opts.RetentionDuration = 0 db, err := Open(tmpDir, log.NewLogfmtLogger(os.Stderr), nil, opts) - testutil.Ok(t, err) + assert.NoError(t, err) loadedBlocks := db.Blocks() @@ -2821,11 +2823,11 @@ func TestOpen_VariousBlockStates(t *testing.T) { } loaded++ } - testutil.Equals(t, len(expectedLoadedDirs), loaded) - testutil.Ok(t, db.Close()) + assert.Equal(t, len(expectedLoadedDirs), loaded) + assert.NoError(t, db.Close()) files, err := ioutil.ReadDir(tmpDir) - testutil.Ok(t, err) + assert.NoError(t, err) var ignored int for _, f := range files { @@ -2836,7 +2838,7 @@ func TestOpen_VariousBlockStates(t *testing.T) { ignored++ } } - testutil.Equals(t, len(expectedIgnoredDirs), ignored) + assert.Equal(t, len(expectedIgnoredDirs), ignored) } func TestOneCheckpointPerCompactCall(t *testing.T) { @@ -2849,15 +2851,15 @@ func TestOneCheckpointPerCompactCall(t *testing.T) { } tmpDir, err := ioutil.TempDir("", "test") - testutil.Ok(t, err) + assert.NoError(t, err) t.Cleanup(func() { - testutil.Ok(t, os.RemoveAll(tmpDir)) + assert.NoError(t, os.RemoveAll(tmpDir)) }) db, err := Open(tmpDir, log.NewNopLogger(), prometheus.NewRegistry(), tsdbCfg) - testutil.Ok(t, err) + assert.NoError(t, err) t.Cleanup(func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }) db.DisableCompactions() @@ -2868,40 +2870,40 @@ func TestOneCheckpointPerCompactCall(t *testing.T) { app := db.Appender(context.Background()) for i := int64(0); i < 60; i++ { _, err := app.Add(lbls, blockRange*i, rand.Float64()) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = app.Add(lbls, (blockRange*i)+blockRange/2, rand.Float64()) - testutil.Ok(t, err) + assert.NoError(t, err) // Rotate the WAL file so that there is >3 files for checkpoint to happen. - testutil.Ok(t, db.head.wal.NextSegment()) + assert.NoError(t, db.head.wal.NextSegment()) } - testutil.Ok(t, app.Commit()) + assert.NoError(t, app.Commit()) // Check the existing WAL files. first, last, err := wal.Segments(db.head.wal.Dir()) - testutil.Ok(t, err) - testutil.Equals(t, 0, first) - testutil.Equals(t, 60, last) + assert.NoError(t, err) + assert.Equal(t, 0, first) + assert.Equal(t, 60, last) - testutil.Equals(t, 0.0, prom_testutil.ToFloat64(db.head.metrics.checkpointCreationTotal)) - testutil.Ok(t, db.Compact()) - testutil.Equals(t, 1.0, prom_testutil.ToFloat64(db.head.metrics.checkpointCreationTotal)) + assert.Equal(t, 0.0, prom_testutil.ToFloat64(db.head.metrics.checkpointCreationTotal)) + assert.NoError(t, db.Compact()) + assert.Equal(t, 1.0, prom_testutil.ToFloat64(db.head.metrics.checkpointCreationTotal)) // As the data spans for 59 blocks, 58 go to disk and 1 remains in Head. - testutil.Equals(t, 58, len(db.Blocks())) + assert.Equal(t, 58, len(db.Blocks())) // Though WAL was truncated only once, head should be truncated after each compaction. - testutil.Equals(t, 58.0, prom_testutil.ToFloat64(db.head.metrics.headTruncateTotal)) + assert.Equal(t, 58.0, prom_testutil.ToFloat64(db.head.metrics.headTruncateTotal)) // The compaction should have only truncated first 2/3 of WAL (while also rotating the files). first, last, err = wal.Segments(db.head.wal.Dir()) - testutil.Ok(t, err) - testutil.Equals(t, 40, first) - testutil.Equals(t, 61, last) + assert.NoError(t, err) + assert.Equal(t, 40, first) + assert.Equal(t, 61, last) // The first checkpoint would be for first 2/3rd of WAL, hence till 39. // That should be the last checkpoint. _, cno, err := wal.LastCheckpoint(db.head.wal.Dir()) - testutil.Ok(t, err) - testutil.Equals(t, 39, cno) + assert.NoError(t, err) + assert.Equal(t, 39, cno) // Case 2: Old blocks on disk. // The above blocks will act as old blocks. @@ -2911,51 +2913,51 @@ func TestOneCheckpointPerCompactCall(t *testing.T) { blocks := db.Blocks() newBlockMint := blocks[len(blocks)-1].Meta().MaxTime newBlockMaxt := db.Head().MaxTime() + 1 - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) createBlock(t, db.dir, genSeries(1, 1, newBlockMint, newBlockMaxt)) db, err = Open(db.dir, log.NewNopLogger(), prometheus.NewRegistry(), tsdbCfg) - testutil.Ok(t, err) + assert.NoError(t, err) db.DisableCompactions() // 1 block more. - testutil.Equals(t, 59, len(db.Blocks())) + assert.Equal(t, 59, len(db.Blocks())) // No series in Head because of this new block. - testutil.Equals(t, 0, int(db.head.NumSeries())) + assert.Equal(t, 0, int(db.head.NumSeries())) // Adding sample way into the future. app = db.Appender(context.Background()) _, err = app.Add(lbls, blockRange*120, rand.Float64()) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) // The mint of head is the last block maxt, that means the gap between mint and maxt // of Head is too large. This will trigger many compactions. - testutil.Equals(t, newBlockMaxt, db.head.MinTime()) + assert.Equal(t, newBlockMaxt, db.head.MinTime()) // Another WAL file was rotated. first, last, err = wal.Segments(db.head.wal.Dir()) - testutil.Ok(t, err) - testutil.Equals(t, 40, first) - testutil.Equals(t, 62, last) + assert.NoError(t, err) + assert.Equal(t, 40, first) + assert.Equal(t, 62, last) - testutil.Equals(t, 0.0, prom_testutil.ToFloat64(db.head.metrics.checkpointCreationTotal)) - testutil.Ok(t, db.Compact()) - testutil.Equals(t, 1.0, prom_testutil.ToFloat64(db.head.metrics.checkpointCreationTotal)) + assert.Equal(t, 0.0, prom_testutil.ToFloat64(db.head.metrics.checkpointCreationTotal)) + assert.NoError(t, db.Compact()) + assert.Equal(t, 1.0, prom_testutil.ToFloat64(db.head.metrics.checkpointCreationTotal)) // No new blocks should be created as there was not data in between the new samples and the blocks. - testutil.Equals(t, 59, len(db.Blocks())) + assert.Equal(t, 59, len(db.Blocks())) // The compaction should have only truncated first 2/3 of WAL (while also rotating the files). first, last, err = wal.Segments(db.head.wal.Dir()) - testutil.Ok(t, err) - testutil.Equals(t, 55, first) - testutil.Equals(t, 63, last) + assert.NoError(t, err) + assert.Equal(t, 55, first) + assert.Equal(t, 63, last) // The first checkpoint would be for first 2/3rd of WAL, hence till 54. // That should be the last checkpoint. _, cno, err = wal.LastCheckpoint(db.head.wal.Dir()) - testutil.Ok(t, err) - testutil.Equals(t, 54, cno) + assert.NoError(t, err) + assert.Equal(t, 54, cno) } diff --git a/tsdb/head.go b/tsdb/head.go index 56b597f3df..e4dfef20ae 100644 --- a/tsdb/head.go +++ b/tsdb/head.go @@ -28,6 +28,8 @@ import ( "github.com/oklog/ulid" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" + "go.uber.org/atomic" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/storage" "github.com/prometheus/prometheus/tsdb/chunkenc" @@ -38,7 +40,6 @@ import ( "github.com/prometheus/prometheus/tsdb/tombstones" "github.com/prometheus/prometheus/tsdb/tsdbutil" "github.com/prometheus/prometheus/tsdb/wal" - "go.uber.org/atomic" ) var ( diff --git a/tsdb/head_bench_test.go b/tsdb/head_bench_test.go index 82e8de48dd..22837fb6e7 100644 --- a/tsdb/head_bench_test.go +++ b/tsdb/head_bench_test.go @@ -19,21 +19,21 @@ import ( "strconv" "testing" + "github.com/stretchr/testify/assert" "go.uber.org/atomic" "github.com/prometheus/prometheus/pkg/labels" - "github.com/prometheus/prometheus/util/testutil" ) func BenchmarkHeadStripeSeriesCreate(b *testing.B) { chunkDir, err := ioutil.TempDir("", "chunk_dir") - testutil.Ok(b, err) + assert.NoError(b, err) defer func() { - testutil.Ok(b, os.RemoveAll(chunkDir)) + assert.NoError(b, os.RemoveAll(chunkDir)) }() // Put a series, select it. GC it and then access it. h, err := NewHead(nil, nil, nil, 1000, chunkDir, nil, DefaultStripeSize, nil) - testutil.Ok(b, err) + assert.NoError(b, err) defer h.Close() for i := 0; i < b.N; i++ { @@ -43,13 +43,13 @@ func BenchmarkHeadStripeSeriesCreate(b *testing.B) { func BenchmarkHeadStripeSeriesCreateParallel(b *testing.B) { chunkDir, err := ioutil.TempDir("", "chunk_dir") - testutil.Ok(b, err) + assert.NoError(b, err) defer func() { - testutil.Ok(b, os.RemoveAll(chunkDir)) + assert.NoError(b, os.RemoveAll(chunkDir)) }() // Put a series, select it. GC it and then access it. h, err := NewHead(nil, nil, nil, 1000, chunkDir, nil, DefaultStripeSize, nil) - testutil.Ok(b, err) + assert.NoError(b, err) defer h.Close() var count atomic.Int64 diff --git a/tsdb/head_test.go b/tsdb/head_test.go index 62ab33baa3..42908f46f9 100644 --- a/tsdb/head_test.go +++ b/tsdb/head_test.go @@ -28,6 +28,8 @@ import ( "github.com/pkg/errors" prom_testutil "github.com/prometheus/client_golang/prometheus/testutil" + "github.com/stretchr/testify/assert" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/storage" "github.com/prometheus/prometheus/tsdb/chunkenc" @@ -37,22 +39,21 @@ import ( "github.com/prometheus/prometheus/tsdb/tombstones" "github.com/prometheus/prometheus/tsdb/tsdbutil" "github.com/prometheus/prometheus/tsdb/wal" - "github.com/prometheus/prometheus/util/testutil" ) func newTestHead(t testing.TB, chunkRange int64, compressWAL bool) (*Head, *wal.WAL) { dir, err := ioutil.TempDir("", "test") - testutil.Ok(t, err) + assert.NoError(t, err) wlog, err := wal.NewSize(nil, nil, filepath.Join(dir, "wal"), 32768, compressWAL) - testutil.Ok(t, err) + assert.NoError(t, err) h, err := NewHead(nil, nil, wlog, chunkRange, dir, nil, DefaultStripeSize, nil) - testutil.Ok(t, err) + assert.NoError(t, err) - testutil.Ok(t, h.chunkDiskMapper.IterateAllChunks(func(_, _ uint64, _, _ int64, _ uint16) error { return nil })) + assert.NoError(t, h.chunkDiskMapper.IterateAllChunks(func(_, _ uint64, _, _ int64, _ uint16) error { return nil })) t.Cleanup(func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }) return h, wlog } @@ -61,7 +62,7 @@ func BenchmarkCreateSeries(b *testing.B) { series := genSeries(b.N, 10, 0, 0) h, _ := newTestHead(b, 10000, false) defer func() { - testutil.Ok(b, h.Close()) + assert.NoError(b, h.Close()) }() b.ReportAllocs() @@ -77,18 +78,18 @@ func populateTestWAL(t testing.TB, w *wal.WAL, recs []interface{}) { for _, r := range recs { switch v := r.(type) { case []record.RefSeries: - testutil.Ok(t, w.Log(enc.Series(v, nil))) + assert.NoError(t, w.Log(enc.Series(v, nil))) case []record.RefSample: - testutil.Ok(t, w.Log(enc.Samples(v, nil))) + assert.NoError(t, w.Log(enc.Samples(v, nil))) case []tombstones.Stone: - testutil.Ok(t, w.Log(enc.Tombstones(v, nil))) + assert.NoError(t, w.Log(enc.Tombstones(v, nil))) } } } func readTestWAL(t testing.TB, dir string) (recs []interface{}) { sr, err := wal.NewSegmentsReader(dir) - testutil.Ok(t, err) + assert.NoError(t, err) defer sr.Close() var dec record.Decoder @@ -100,21 +101,21 @@ func readTestWAL(t testing.TB, dir string) (recs []interface{}) { switch dec.Type(rec) { case record.Series: series, err := dec.Series(rec, nil) - testutil.Ok(t, err) + assert.NoError(t, err) recs = append(recs, series) case record.Samples: samples, err := dec.Samples(rec, nil) - testutil.Ok(t, err) + assert.NoError(t, err) recs = append(recs, samples) case record.Tombstones: tstones, err := dec.Tombstones(rec, nil) - testutil.Ok(t, err) + assert.NoError(t, err) recs = append(recs, tstones) default: t.Fatalf("unknown record type") } } - testutil.Ok(t, r.Err()) + assert.NoError(t, r.Err()) return recs } @@ -147,13 +148,13 @@ func BenchmarkLoadWAL(b *testing.B) { b.Run(fmt.Sprintf("batches=%d,seriesPerBatch=%d,samplesPerSeries=%d", c.batches, c.seriesPerBatch, c.samplesPerSeries), func(b *testing.B) { dir, err := ioutil.TempDir("", "test_load_wal") - testutil.Ok(b, err) + assert.NoError(b, err) defer func() { - testutil.Ok(b, os.RemoveAll(dir)) + assert.NoError(b, os.RemoveAll(dir)) }() w, err := wal.New(nil, nil, dir, false) - testutil.Ok(b, err) + assert.NoError(b, err) // Write series. refSeries := make([]record.RefSeries, 0, c.seriesPerBatch) @@ -191,7 +192,7 @@ func BenchmarkLoadWAL(b *testing.B) { // Load the WAL. for i := 0; i < b.N; i++ { h, err := NewHead(nil, nil, w, 1000, w.Dir(), nil, DefaultStripeSize, nil) - testutil.Ok(b, err) + assert.NoError(b, err) h.Init(0) } }) @@ -229,35 +230,35 @@ func TestHead_ReadWAL(t *testing.T) { head, w := newTestHead(t, 1000, compress) defer func() { - testutil.Ok(t, head.Close()) + assert.NoError(t, head.Close()) }() populateTestWAL(t, w, entries) - testutil.Ok(t, head.Init(math.MinInt64)) - testutil.Equals(t, uint64(101), head.lastSeriesID.Load()) + assert.NoError(t, head.Init(math.MinInt64)) + assert.Equal(t, uint64(101), head.lastSeriesID.Load()) s10 := head.series.getByID(10) s11 := head.series.getByID(11) s50 := head.series.getByID(50) s100 := head.series.getByID(100) - testutil.Equals(t, labels.FromStrings("a", "1"), s10.lset) - testutil.Equals(t, (*memSeries)(nil), s11) // Series without samples should be garbage collected at head.Init(). - testutil.Equals(t, labels.FromStrings("a", "4"), s50.lset) - testutil.Equals(t, labels.FromStrings("a", "3"), s100.lset) + assert.Equal(t, labels.FromStrings("a", "1"), s10.lset) + assert.Equal(t, (*memSeries)(nil), s11) // Series without samples should be garbage collected at head.Init(). + assert.Equal(t, labels.FromStrings("a", "4"), s50.lset) + assert.Equal(t, labels.FromStrings("a", "3"), s100.lset) expandChunk := func(c chunkenc.Iterator) (x []sample) { for c.Next() { t, v := c.At() x = append(x, sample{t: t, v: v}) } - testutil.Ok(t, c.Err()) + assert.NoError(t, c.Err()) return x } - testutil.Equals(t, []sample{{100, 2}, {101, 5}}, expandChunk(s10.iterator(0, nil, head.chunkDiskMapper, nil))) - testutil.Equals(t, []sample{{101, 6}}, expandChunk(s50.iterator(0, nil, head.chunkDiskMapper, nil))) - testutil.Equals(t, []sample{{100, 3}, {101, 7}}, expandChunk(s100.iterator(0, nil, head.chunkDiskMapper, nil))) + assert.Equal(t, []sample{{100, 2}, {101, 5}}, expandChunk(s10.iterator(0, nil, head.chunkDiskMapper, nil))) + assert.Equal(t, []sample{{101, 6}}, expandChunk(s50.iterator(0, nil, head.chunkDiskMapper, nil))) + assert.Equal(t, []sample{{100, 3}, {101, 7}}, expandChunk(s100.iterator(0, nil, head.chunkDiskMapper, nil))) }) } } @@ -265,53 +266,53 @@ func TestHead_ReadWAL(t *testing.T) { func TestHead_WALMultiRef(t *testing.T) { head, w := newTestHead(t, 1000, false) - testutil.Ok(t, head.Init(0)) + assert.NoError(t, head.Init(0)) app := head.Appender(context.Background()) ref1, err := app.Add(labels.FromStrings("foo", "bar"), 100, 1) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) - testutil.Equals(t, 1.0, prom_testutil.ToFloat64(head.metrics.chunksCreated)) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) + assert.Equal(t, 1.0, prom_testutil.ToFloat64(head.metrics.chunksCreated)) // Add another sample outside chunk range to mmap a chunk. app = head.Appender(context.Background()) _, err = app.Add(labels.FromStrings("foo", "bar"), 1500, 2) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) - testutil.Equals(t, 2.0, prom_testutil.ToFloat64(head.metrics.chunksCreated)) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) + assert.Equal(t, 2.0, prom_testutil.ToFloat64(head.metrics.chunksCreated)) - testutil.Ok(t, head.Truncate(1600)) + assert.NoError(t, head.Truncate(1600)) app = head.Appender(context.Background()) ref2, err := app.Add(labels.FromStrings("foo", "bar"), 1700, 3) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) - testutil.Equals(t, 3.0, prom_testutil.ToFloat64(head.metrics.chunksCreated)) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) + assert.Equal(t, 3.0, prom_testutil.ToFloat64(head.metrics.chunksCreated)) // Add another sample outside chunk range to mmap a chunk. app = head.Appender(context.Background()) _, err = app.Add(labels.FromStrings("foo", "bar"), 2000, 4) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) - testutil.Equals(t, 4.0, prom_testutil.ToFloat64(head.metrics.chunksCreated)) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) + assert.Equal(t, 4.0, prom_testutil.ToFloat64(head.metrics.chunksCreated)) - testutil.Assert(t, ref1 != ref2, "Refs are the same") - testutil.Ok(t, head.Close()) + assert.True(t, ref1 != ref2, "Refs are the same") + assert.NoError(t, head.Close()) w, err = wal.New(nil, nil, w.Dir(), false) - testutil.Ok(t, err) + assert.NoError(t, err) head, err = NewHead(nil, nil, w, 1000, w.Dir(), nil, DefaultStripeSize, nil) - testutil.Ok(t, err) - testutil.Ok(t, head.Init(0)) + assert.NoError(t, err) + assert.NoError(t, head.Init(0)) defer func() { - testutil.Ok(t, head.Close()) + assert.NoError(t, head.Close()) }() q, err := NewBlockQuerier(head, 0, 2100) - testutil.Ok(t, err) + assert.NoError(t, err) series := query(t, q, labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")) - testutil.Equals(t, map[string][]tsdbutil.Sample{`{foo="bar"}`: { + assert.Equal(t, map[string][]tsdbutil.Sample{`{foo="bar"}`: { sample{100, 1}, sample{1500, 2}, sample{1700, 3}, @@ -322,14 +323,14 @@ func TestHead_WALMultiRef(t *testing.T) { func TestHead_UnknownWALRecord(t *testing.T) { head, w := newTestHead(t, 1000, false) w.Log([]byte{255, 42}) - testutil.Ok(t, head.Init(0)) - testutil.Ok(t, head.Close()) + assert.NoError(t, head.Init(0)) + assert.NoError(t, head.Close()) } func TestHead_Truncate(t *testing.T) { h, _ := newTestHead(t, 1000, false) defer func() { - testutil.Ok(t, h.Close()) + assert.NoError(t, h.Close()) }() h.initTime(0) @@ -356,21 +357,21 @@ func TestHead_Truncate(t *testing.T) { s4.mmappedChunks = []*mmappedChunk{} // Truncation need not be aligned. - testutil.Ok(t, h.Truncate(1)) + assert.NoError(t, h.Truncate(1)) - testutil.Ok(t, h.Truncate(2000)) + assert.NoError(t, h.Truncate(2000)) - testutil.Equals(t, []*mmappedChunk{ + assert.Equal(t, []*mmappedChunk{ {minTime: 2000, maxTime: 2999}, }, h.series.getByID(s1.ref).mmappedChunks) - testutil.Equals(t, []*mmappedChunk{ + assert.Equal(t, []*mmappedChunk{ {minTime: 2000, maxTime: 2999}, {minTime: 3000, maxTime: 3999}, }, h.series.getByID(s2.ref).mmappedChunks) - testutil.Assert(t, h.series.getByID(s3.ref) == nil, "") - testutil.Assert(t, h.series.getByID(s4.ref) == nil, "") + assert.True(t, h.series.getByID(s3.ref) == nil, "") + assert.True(t, h.series.getByID(s4.ref) == nil, "") postingsA1, _ := index.ExpandPostings(h.postings.Get("a", "1")) postingsA2, _ := index.ExpandPostings(h.postings.Get("a", "2")) @@ -379,14 +380,14 @@ func TestHead_Truncate(t *testing.T) { postingsC1, _ := index.ExpandPostings(h.postings.Get("c", "1")) postingsAll, _ := index.ExpandPostings(h.postings.Get("", "")) - testutil.Equals(t, []uint64{s1.ref}, postingsA1) - testutil.Equals(t, []uint64{s2.ref}, postingsA2) - testutil.Equals(t, []uint64{s1.ref, s2.ref}, postingsB1) - testutil.Equals(t, []uint64{s1.ref, s2.ref}, postingsAll) - testutil.Assert(t, postingsB2 == nil, "") - testutil.Assert(t, postingsC1 == nil, "") + assert.Equal(t, []uint64{s1.ref}, postingsA1) + assert.Equal(t, []uint64{s2.ref}, postingsA2) + assert.Equal(t, []uint64{s1.ref, s2.ref}, postingsB1) + assert.Equal(t, []uint64{s1.ref, s2.ref}, postingsAll) + assert.True(t, postingsB2 == nil, "") + assert.True(t, postingsC1 == nil, "") - testutil.Equals(t, map[string]struct{}{ + assert.Equal(t, map[string]struct{}{ "": {}, // from 'all' postings list "a": {}, "b": {}, @@ -405,7 +406,7 @@ func TestHead_Truncate(t *testing.T) { ss[value] = struct{}{} } } - testutil.Equals(t, map[string]map[string]struct{}{ + assert.Equal(t, map[string]map[string]struct{}{ "a": {"1": struct{}{}, "2": struct{}{}}, "b": {"1": struct{}{}}, }, values) @@ -415,15 +416,15 @@ func TestHead_Truncate(t *testing.T) { // garbage collected chunks. func TestMemSeries_truncateChunks(t *testing.T) { dir, err := ioutil.TempDir("", "truncate_chunks") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() // This is usually taken from the Head, but passing manually here. chunkDiskMapper, err := chunks.NewChunkDiskMapper(dir, chunkenc.NewPool()) - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, chunkDiskMapper.Close()) + assert.NoError(t, chunkDiskMapper.Close()) }() memChunkPool := sync.Pool{ @@ -436,7 +437,7 @@ func TestMemSeries_truncateChunks(t *testing.T) { for i := 0; i < 4000; i += 5 { ok, _ := s.append(int64(i), float64(i), 0, chunkDiskMapper) - testutil.Assert(t, ok == true, "sample append failed") + assert.True(t, ok == true, "sample append failed") } // Check that truncate removes half of the chunks and afterwards @@ -444,32 +445,32 @@ func TestMemSeries_truncateChunks(t *testing.T) { countBefore := len(s.mmappedChunks) + 1 // +1 for the head chunk. lastID := s.chunkID(countBefore - 1) lastChunk, _, err := s.chunk(lastID, chunkDiskMapper) - testutil.Ok(t, err) - testutil.Assert(t, lastChunk != nil, "") + assert.NoError(t, err) + assert.NotNil(t, lastChunk) chk, _, err := s.chunk(0, chunkDiskMapper) - testutil.Assert(t, chk != nil, "") - testutil.Ok(t, err) + assert.NotNil(t, chk) + assert.NoError(t, err) s.truncateChunksBefore(2000) - testutil.Equals(t, int64(2000), s.mmappedChunks[0].minTime) + assert.Equal(t, int64(2000), s.mmappedChunks[0].minTime) _, _, err = s.chunk(0, chunkDiskMapper) - testutil.Assert(t, err == storage.ErrNotFound, "first chunks not gone") - testutil.Equals(t, countBefore/2, len(s.mmappedChunks)+1) // +1 for the head chunk. + assert.True(t, err == storage.ErrNotFound, "first chunks not gone") + assert.Equal(t, countBefore/2, len(s.mmappedChunks)+1) // +1 for the head chunk. chk, _, err = s.chunk(lastID, chunkDiskMapper) - testutil.Ok(t, err) - testutil.Equals(t, lastChunk, chk) + assert.NoError(t, err) + assert.Equal(t, lastChunk, chk) // Validate that the series' sample buffer is applied correctly to the last chunk // after truncation. it1 := s.iterator(s.chunkID(len(s.mmappedChunks)), nil, chunkDiskMapper, nil) _, ok := it1.(*memSafeIterator) - testutil.Assert(t, ok == true, "") + assert.True(t, ok == true, "") it2 := s.iterator(s.chunkID(len(s.mmappedChunks)-1), nil, chunkDiskMapper, nil) _, ok = it2.(*memSafeIterator) - testutil.Assert(t, ok == false, "non-last chunk incorrectly wrapped with sample buffer") + assert.True(t, ok == false, "non-last chunk incorrectly wrapped with sample buffer") } func TestHeadDeleteSeriesWithoutSamples(t *testing.T) { @@ -490,14 +491,14 @@ func TestHeadDeleteSeriesWithoutSamples(t *testing.T) { } head, w := newTestHead(t, 1000, compress) defer func() { - testutil.Ok(t, head.Close()) + assert.NoError(t, head.Close()) }() populateTestWAL(t, w, entries) - testutil.Ok(t, head.Init(math.MinInt64)) + assert.NoError(t, head.Init(math.MinInt64)) - testutil.Ok(t, head.Delete(0, 100, labels.MustNewMatcher(labels.MatchEqual, "a", "1"))) + assert.NoError(t, head.Delete(0, 100, labels.MustNewMatcher(labels.MatchEqual, "a", "1"))) }) } } @@ -560,39 +561,39 @@ func TestHeadDeleteSimple(t *testing.T) { app := head.Appender(context.Background()) for _, smpl := range smplsAll { _, err := app.Add(labels.Labels{lblDefault}, smpl.t, smpl.v) - testutil.Ok(t, err) + assert.NoError(t, err) } - testutil.Ok(t, app.Commit()) + assert.NoError(t, app.Commit()) // Delete the ranges. for _, r := range c.dranges { - testutil.Ok(t, head.Delete(r.Mint, r.Maxt, labels.MustNewMatcher(labels.MatchEqual, lblDefault.Name, lblDefault.Value))) + assert.NoError(t, head.Delete(r.Mint, r.Maxt, labels.MustNewMatcher(labels.MatchEqual, lblDefault.Name, lblDefault.Value))) } // Add more samples. app = head.Appender(context.Background()) for _, smpl := range c.addSamples { _, err := app.Add(labels.Labels{lblDefault}, smpl.t, smpl.v) - testutil.Ok(t, err) + assert.NoError(t, err) } - testutil.Ok(t, app.Commit()) + assert.NoError(t, app.Commit()) // Compare the samples for both heads - before and after the reloadBlocks. reloadedW, err := wal.New(nil, nil, w.Dir(), compress) // Use a new wal to ensure deleted samples are gone even after a reloadBlocks. - testutil.Ok(t, err) + assert.NoError(t, err) reloadedHead, err := NewHead(nil, nil, reloadedW, 1000, reloadedW.Dir(), nil, DefaultStripeSize, nil) - testutil.Ok(t, err) - testutil.Ok(t, reloadedHead.Init(0)) + assert.NoError(t, err) + assert.NoError(t, reloadedHead.Init(0)) // Compare the query results for both heads - before and after the reloadBlocks. Outer: for _, h := range []*Head{head, reloadedHead} { q, err := NewBlockQuerier(h, h.MinTime(), h.MaxTime()) - testutil.Ok(t, err) + assert.NoError(t, err) actSeriesSet := q.Select(false, nil, labels.MustNewMatcher(labels.MatchEqual, lblDefault.Name, lblDefault.Value)) - testutil.Ok(t, q.Close()) + assert.NoError(t, q.Close()) expSeriesSet := newMockSeriesSet([]storage.Series{ storage.NewListSeries(labels.Labels{lblDefault}, func() []tsdbutil.Sample { ss := make([]tsdbutil.Sample, 0, len(c.smplsExp)) @@ -606,24 +607,24 @@ func TestHeadDeleteSimple(t *testing.T) { for { eok, rok := expSeriesSet.Next(), actSeriesSet.Next() - testutil.Equals(t, eok, rok) + assert.Equal(t, eok, rok) if !eok { - testutil.Ok(t, h.Close()) - testutil.Ok(t, actSeriesSet.Err()) - testutil.Equals(t, 0, len(actSeriesSet.Warnings())) + assert.NoError(t, h.Close()) + assert.NoError(t, actSeriesSet.Err()) + assert.Equal(t, 0, len(actSeriesSet.Warnings())) continue Outer } expSeries := expSeriesSet.At() actSeries := actSeriesSet.At() - testutil.Equals(t, expSeries.Labels(), actSeries.Labels()) + assert.Equal(t, expSeries.Labels(), actSeries.Labels()) smplExp, errExp := storage.ExpandSamples(expSeries.Iterator(), nil) smplRes, errRes := storage.ExpandSamples(actSeries.Iterator(), nil) - testutil.Equals(t, errExp, errRes) - testutil.Equals(t, smplExp, smplRes) + assert.Equal(t, errExp, errRes) + assert.Equal(t, smplExp, smplRes) } } } @@ -634,7 +635,7 @@ func TestHeadDeleteSimple(t *testing.T) { func TestDeleteUntilCurMax(t *testing.T) { hb, _ := newTestHead(t, 1000000, false) defer func() { - testutil.Ok(t, hb.Close()) + assert.NoError(t, hb.Close()) }() numSamples := int64(10) @@ -643,42 +644,42 @@ func TestDeleteUntilCurMax(t *testing.T) { for i := int64(0); i < numSamples; i++ { smpls[i] = rand.Float64() _, err := app.Add(labels.Labels{{Name: "a", Value: "b"}}, i, smpls[i]) - testutil.Ok(t, err) + assert.NoError(t, err) } - testutil.Ok(t, app.Commit()) - testutil.Ok(t, hb.Delete(0, 10000, labels.MustNewMatcher(labels.MatchEqual, "a", "b"))) + assert.NoError(t, app.Commit()) + assert.NoError(t, hb.Delete(0, 10000, labels.MustNewMatcher(labels.MatchEqual, "a", "b"))) // Test the series returns no samples. The series is cleared only after compaction. q, err := NewBlockQuerier(hb, 0, 100000) - testutil.Ok(t, err) + assert.NoError(t, err) res := q.Select(false, nil, labels.MustNewMatcher(labels.MatchEqual, "a", "b")) - testutil.Assert(t, res.Next(), "series is not present") + assert.True(t, res.Next(), "series is not present") s := res.At() it := s.Iterator() - testutil.Assert(t, !it.Next(), "expected no samples") + assert.True(t, !it.Next(), "expected no samples") for res.Next() { } - testutil.Ok(t, res.Err()) - testutil.Equals(t, 0, len(res.Warnings())) + assert.NoError(t, res.Err()) + assert.Equal(t, 0, len(res.Warnings())) // Add again and test for presence. app = hb.Appender(context.Background()) _, err = app.Add(labels.Labels{{Name: "a", Value: "b"}}, 11, 1) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) q, err = NewBlockQuerier(hb, 0, 100000) - testutil.Ok(t, err) + assert.NoError(t, err) res = q.Select(false, nil, labels.MustNewMatcher(labels.MatchEqual, "a", "b")) - testutil.Assert(t, res.Next(), "series don't exist") + assert.True(t, res.Next(), "series don't exist") exps := res.At() it = exps.Iterator() resSamples, err := storage.ExpandSamples(it, newSample) - testutil.Ok(t, err) - testutil.Equals(t, []tsdbutil.Sample{sample{11, 1}}, resSamples) + assert.NoError(t, err) + assert.Equal(t, []tsdbutil.Sample{sample{11, 1}}, resSamples) for res.Next() { } - testutil.Ok(t, res.Err()) - testutil.Equals(t, 0, len(res.Warnings())) + assert.NoError(t, res.Err()) + assert.Equal(t, 0, len(res.Warnings())) } func TestDeletedSamplesAndSeriesStillInWALAfterCheckpoint(t *testing.T) { @@ -690,16 +691,16 @@ func TestDeletedSamplesAndSeriesStillInWALAfterCheckpoint(t *testing.T) { for i := 0; i < numSamples; i++ { app := hb.Appender(context.Background()) _, err := app.Add(labels.Labels{{Name: "a", Value: "b"}}, int64(i), 0) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) } - testutil.Ok(t, hb.Delete(0, int64(numSamples), labels.MustNewMatcher(labels.MatchEqual, "a", "b"))) - testutil.Ok(t, hb.Truncate(1)) - testutil.Ok(t, hb.Close()) + assert.NoError(t, hb.Delete(0, int64(numSamples), labels.MustNewMatcher(labels.MatchEqual, "a", "b"))) + assert.NoError(t, hb.Truncate(1)) + assert.NoError(t, hb.Close()) // Confirm there's been a checkpoint. cdir, _, err := wal.LastCheckpoint(w.Dir()) - testutil.Ok(t, err) + assert.NoError(t, err) // Read in checkpoint and WAL. recs := readTestWAL(t, cdir) recs = append(recs, readTestWAL(t, w.Dir())...) @@ -717,9 +718,9 @@ func TestDeletedSamplesAndSeriesStillInWALAfterCheckpoint(t *testing.T) { t.Fatalf("unknown record type") } } - testutil.Equals(t, 1, series) - testutil.Equals(t, 9999, samples) - testutil.Equals(t, 1, stones) + assert.Equal(t, 1, series) + assert.Equal(t, 9999, samples) + assert.Equal(t, 1, stones) } @@ -777,7 +778,7 @@ func TestDelete_e2e(t *testing.T) { hb, _ := newTestHead(t, 100000, false) defer func() { - testutil.Ok(t, hb.Close()) + assert.NoError(t, hb.Close()) }() app := hb.Appender(context.Background()) @@ -788,13 +789,13 @@ func TestDelete_e2e(t *testing.T) { for i := 0; i < numDatapoints; i++ { v := rand.Float64() _, err := app.Add(ls, ts, v) - testutil.Ok(t, err) + assert.NoError(t, err) series = append(series, sample{ts, v}) ts += rand.Int63n(timeInterval) + 1 } seriesMap[labels.New(l...).String()] = series } - testutil.Ok(t, app.Commit()) + assert.NoError(t, app.Commit()) // Delete a time-range from each-selector. dels := []struct { ms []*labels.Matcher @@ -823,7 +824,7 @@ func TestDelete_e2e(t *testing.T) { } for _, del := range dels { for _, r := range del.drange { - testutil.Ok(t, hb.Delete(r.Mint, r.Maxt, del.ms...)) + assert.NoError(t, hb.Delete(r.Mint, r.Maxt, del.ms...)) } matched := labels.Slice{} for _, ls := range lbls { @@ -835,7 +836,7 @@ func TestDelete_e2e(t *testing.T) { sort.Sort(matched) for i := 0; i < numRanges; i++ { q, err := NewBlockQuerier(hb, 0, 100000) - testutil.Ok(t, err) + assert.NoError(t, err) defer q.Close() ss := q.Select(true, nil, del.ms...) // Build the mockSeriesSet. @@ -863,20 +864,20 @@ func TestDelete_e2e(t *testing.T) { } } } - testutil.Equals(t, eok, rok) + assert.Equal(t, eok, rok) if !eok { break } sexp := expSs.At() sres := ss.At() - testutil.Equals(t, sexp.Labels(), sres.Labels()) + assert.Equal(t, sexp.Labels(), sres.Labels()) smplExp, errExp := storage.ExpandSamples(sexp.Iterator(), nil) smplRes, errRes := storage.ExpandSamples(sres.Iterator(), nil) - testutil.Equals(t, errExp, errRes) - testutil.Equals(t, smplExp, smplRes) + assert.Equal(t, errExp, errRes) + assert.Equal(t, smplExp, smplRes) } - testutil.Ok(t, ss.Err()) - testutil.Equals(t, 0, len(ss.Warnings())) + assert.NoError(t, ss.Err()) + assert.Equal(t, 0, len(ss.Warnings())) } } } @@ -957,15 +958,15 @@ func TestComputeChunkEndTime(t *testing.T) { func TestMemSeries_append(t *testing.T) { dir, err := ioutil.TempDir("", "append") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() // This is usually taken from the Head, but passing manually here. chunkDiskMapper, err := chunks.NewChunkDiskMapper(dir, chunkenc.NewPool()) - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, chunkDiskMapper.Close()) + assert.NoError(t, chunkDiskMapper.Close()) }() s := newMemSeries(labels.Labels{}, 1, 500, nil) @@ -974,39 +975,39 @@ func TestMemSeries_append(t *testing.T) { // on and after it. // New chunk must correctly be cut at 1000. ok, chunkCreated := s.append(998, 1, 0, chunkDiskMapper) - testutil.Assert(t, ok, "append failed") - testutil.Assert(t, chunkCreated, "first sample created chunk") + assert.True(t, ok, "append failed") + assert.True(t, chunkCreated, "first sample created chunk") ok, chunkCreated = s.append(999, 2, 0, chunkDiskMapper) - testutil.Assert(t, ok, "append failed") - testutil.Assert(t, !chunkCreated, "second sample should use same chunk") + assert.True(t, ok, "append failed") + assert.True(t, !chunkCreated, "second sample should use same chunk") ok, chunkCreated = s.append(1000, 3, 0, chunkDiskMapper) - testutil.Assert(t, ok, "append failed") - testutil.Assert(t, chunkCreated, "expected new chunk on boundary") + assert.True(t, ok, "append failed") + assert.True(t, chunkCreated, "expected new chunk on boundary") ok, chunkCreated = s.append(1001, 4, 0, chunkDiskMapper) - testutil.Assert(t, ok, "append failed") - testutil.Assert(t, !chunkCreated, "second sample should use same chunk") + assert.True(t, ok, "append failed") + assert.True(t, !chunkCreated, "second sample should use same chunk") - testutil.Assert(t, len(s.mmappedChunks) == 1, "there should be only 1 mmapped chunk") - testutil.Assert(t, s.mmappedChunks[0].minTime == 998 && s.mmappedChunks[0].maxTime == 999, "wrong chunk range") - testutil.Assert(t, s.headChunk.minTime == 1000 && s.headChunk.maxTime == 1001, "wrong chunk range") + assert.True(t, len(s.mmappedChunks) == 1, "there should be only 1 mmapped chunk") + assert.True(t, s.mmappedChunks[0].minTime == 998 && s.mmappedChunks[0].maxTime == 999, "wrong chunk range") + assert.True(t, s.headChunk.minTime == 1000 && s.headChunk.maxTime == 1001, "wrong chunk range") // Fill the range [1000,2000) with many samples. Intermediate chunks should be cut // at approximately 120 samples per chunk. for i := 1; i < 1000; i++ { ok, _ := s.append(1001+int64(i), float64(i), 0, chunkDiskMapper) - testutil.Assert(t, ok, "append failed") + assert.True(t, ok, "append failed") } - testutil.Assert(t, len(s.mmappedChunks)+1 > 7, "expected intermediate chunks") + assert.True(t, len(s.mmappedChunks)+1 > 7, "expected intermediate chunks") // All chunks but the first and last should now be moderately full. for i, c := range s.mmappedChunks[1:] { chk, err := chunkDiskMapper.Chunk(c.ref) - testutil.Ok(t, err) - testutil.Assert(t, chk.NumSamples() > 100, "unexpected small chunk %d of length %d", i, chk.NumSamples()) + assert.NoError(t, err) + assert.True(t, chk.NumSamples() > 100, "unexpected small chunk %d of length %d", i, chk.NumSamples()) } } @@ -1014,7 +1015,7 @@ func TestGCChunkAccess(t *testing.T) { // Put a chunk, select it. GC it and then access it. h, _ := newTestHead(t, 1000, false) defer func() { - testutil.Ok(t, h.Close()) + assert.NoError(t, h.Close()) }() h.initTime(0) @@ -1023,52 +1024,52 @@ func TestGCChunkAccess(t *testing.T) { // Appending 2 samples for the first chunk. ok, chunkCreated := s.append(0, 0, 0, h.chunkDiskMapper) - testutil.Assert(t, ok, "series append failed") - testutil.Assert(t, chunkCreated, "chunks was not created") + assert.True(t, ok, "series append failed") + assert.True(t, chunkCreated, "chunks was not created") ok, chunkCreated = s.append(999, 999, 0, h.chunkDiskMapper) - testutil.Assert(t, ok, "series append failed") - testutil.Assert(t, !chunkCreated, "chunks was created") + assert.True(t, ok, "series append failed") + assert.True(t, !chunkCreated, "chunks was created") // A new chunks should be created here as it's beyond the chunk range. ok, chunkCreated = s.append(1000, 1000, 0, h.chunkDiskMapper) - testutil.Assert(t, ok, "series append failed") - testutil.Assert(t, chunkCreated, "chunks was not created") + assert.True(t, ok, "series append failed") + assert.True(t, chunkCreated, "chunks was not created") ok, chunkCreated = s.append(1999, 1999, 0, h.chunkDiskMapper) - testutil.Assert(t, ok, "series append failed") - testutil.Assert(t, !chunkCreated, "chunks was created") + assert.True(t, ok, "series append failed") + assert.True(t, !chunkCreated, "chunks was created") idx := h.indexRange(0, 1500) var ( lset labels.Labels chunks []chunks.Meta ) - testutil.Ok(t, idx.Series(1, &lset, &chunks)) + assert.NoError(t, idx.Series(1, &lset, &chunks)) - testutil.Equals(t, labels.Labels{{ + assert.Equal(t, labels.Labels{{ Name: "a", Value: "1", }}, lset) - testutil.Equals(t, 2, len(chunks)) + assert.Equal(t, 2, len(chunks)) cr, err := h.chunksRange(0, 1500, nil) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = cr.Chunk(chunks[0].Ref) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = cr.Chunk(chunks[1].Ref) - testutil.Ok(t, err) + assert.NoError(t, err) - testutil.Ok(t, h.Truncate(1500)) // Remove a chunk. + assert.NoError(t, h.Truncate(1500)) // Remove a chunk. _, err = cr.Chunk(chunks[0].Ref) - testutil.Equals(t, storage.ErrNotFound, err) + assert.Equal(t, storage.ErrNotFound, err) _, err = cr.Chunk(chunks[1].Ref) - testutil.Ok(t, err) + assert.NoError(t, err) } func TestGCSeriesAccess(t *testing.T) { // Put a series, select it. GC it and then access it. h, _ := newTestHead(t, 1000, false) defer func() { - testutil.Ok(t, h.Close()) + assert.NoError(t, h.Close()) }() h.initTime(0) @@ -1077,53 +1078,53 @@ func TestGCSeriesAccess(t *testing.T) { // Appending 2 samples for the first chunk. ok, chunkCreated := s.append(0, 0, 0, h.chunkDiskMapper) - testutil.Assert(t, ok, "series append failed") - testutil.Assert(t, chunkCreated, "chunks was not created") + assert.True(t, ok, "series append failed") + assert.True(t, chunkCreated, "chunks was not created") ok, chunkCreated = s.append(999, 999, 0, h.chunkDiskMapper) - testutil.Assert(t, ok, "series append failed") - testutil.Assert(t, !chunkCreated, "chunks was created") + assert.True(t, ok, "series append failed") + assert.True(t, !chunkCreated, "chunks was created") // A new chunks should be created here as it's beyond the chunk range. ok, chunkCreated = s.append(1000, 1000, 0, h.chunkDiskMapper) - testutil.Assert(t, ok, "series append failed") - testutil.Assert(t, chunkCreated, "chunks was not created") + assert.True(t, ok, "series append failed") + assert.True(t, chunkCreated, "chunks was not created") ok, chunkCreated = s.append(1999, 1999, 0, h.chunkDiskMapper) - testutil.Assert(t, ok, "series append failed") - testutil.Assert(t, !chunkCreated, "chunks was created") + assert.True(t, ok, "series append failed") + assert.True(t, !chunkCreated, "chunks was created") idx := h.indexRange(0, 2000) var ( lset labels.Labels chunks []chunks.Meta ) - testutil.Ok(t, idx.Series(1, &lset, &chunks)) + assert.NoError(t, idx.Series(1, &lset, &chunks)) - testutil.Equals(t, labels.Labels{{ + assert.Equal(t, labels.Labels{{ Name: "a", Value: "1", }}, lset) - testutil.Equals(t, 2, len(chunks)) + assert.Equal(t, 2, len(chunks)) cr, err := h.chunksRange(0, 2000, nil) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = cr.Chunk(chunks[0].Ref) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = cr.Chunk(chunks[1].Ref) - testutil.Ok(t, err) + assert.NoError(t, err) - testutil.Ok(t, h.Truncate(2000)) // Remove the series. + assert.NoError(t, h.Truncate(2000)) // Remove the series. - testutil.Equals(t, (*memSeries)(nil), h.series.getByID(1)) + assert.Equal(t, (*memSeries)(nil), h.series.getByID(1)) _, err = cr.Chunk(chunks[0].Ref) - testutil.Equals(t, storage.ErrNotFound, err) + assert.Equal(t, storage.ErrNotFound, err) _, err = cr.Chunk(chunks[1].Ref) - testutil.Equals(t, storage.ErrNotFound, err) + assert.Equal(t, storage.ErrNotFound, err) } func TestUncommittedSamplesNotLostOnTruncate(t *testing.T) { h, _ := newTestHead(t, 1000, false) defer func() { - testutil.Ok(t, h.Close()) + assert.NoError(t, h.Close()) }() h.initTime(0) @@ -1131,29 +1132,29 @@ func TestUncommittedSamplesNotLostOnTruncate(t *testing.T) { app := h.appender() lset := labels.FromStrings("a", "1") _, err := app.Add(lset, 2100, 1) - testutil.Ok(t, err) + assert.NoError(t, err) - testutil.Ok(t, h.Truncate(2000)) - testutil.Assert(t, nil != h.series.getByHash(lset.Hash(), lset), "series should not have been garbage collected") + assert.NoError(t, h.Truncate(2000)) + assert.True(t, nil != h.series.getByHash(lset.Hash(), lset), "series should not have been garbage collected") - testutil.Ok(t, app.Commit()) + assert.NoError(t, app.Commit()) q, err := NewBlockQuerier(h, 1500, 2500) - testutil.Ok(t, err) + assert.NoError(t, err) defer q.Close() ss := q.Select(false, nil, labels.MustNewMatcher(labels.MatchEqual, "a", "1")) - testutil.Equals(t, true, ss.Next()) + assert.Equal(t, true, ss.Next()) for ss.Next() { } - testutil.Ok(t, ss.Err()) - testutil.Equals(t, 0, len(ss.Warnings())) + assert.NoError(t, ss.Err()) + assert.Equal(t, 0, len(ss.Warnings())) } func TestRemoveSeriesAfterRollbackAndTruncate(t *testing.T) { h, _ := newTestHead(t, 1000, false) defer func() { - testutil.Ok(t, h.Close()) + assert.NoError(t, h.Close()) }() h.initTime(0) @@ -1161,24 +1162,24 @@ func TestRemoveSeriesAfterRollbackAndTruncate(t *testing.T) { app := h.appender() lset := labels.FromStrings("a", "1") _, err := app.Add(lset, 2100, 1) - testutil.Ok(t, err) + assert.NoError(t, err) - testutil.Ok(t, h.Truncate(2000)) - testutil.Assert(t, nil != h.series.getByHash(lset.Hash(), lset), "series should not have been garbage collected") + assert.NoError(t, h.Truncate(2000)) + assert.True(t, nil != h.series.getByHash(lset.Hash(), lset), "series should not have been garbage collected") - testutil.Ok(t, app.Rollback()) + assert.NoError(t, app.Rollback()) q, err := NewBlockQuerier(h, 1500, 2500) - testutil.Ok(t, err) + assert.NoError(t, err) defer q.Close() ss := q.Select(false, nil, labels.MustNewMatcher(labels.MatchEqual, "a", "1")) - testutil.Equals(t, false, ss.Next()) - testutil.Equals(t, 0, len(ss.Warnings())) + assert.Equal(t, false, ss.Next()) + assert.Equal(t, 0, len(ss.Warnings())) // Truncate again, this time the series should be deleted - testutil.Ok(t, h.Truncate(2050)) - testutil.Equals(t, (*memSeries)(nil), h.series.getByHash(lset.Hash(), lset)) + assert.NoError(t, h.Truncate(2050)) + assert.Equal(t, (*memSeries)(nil), h.series.getByHash(lset.Hash(), lset)) } func TestHead_LogRollback(t *testing.T) { @@ -1186,21 +1187,21 @@ func TestHead_LogRollback(t *testing.T) { t.Run(fmt.Sprintf("compress=%t", compress), func(t *testing.T) { h, w := newTestHead(t, 1000, compress) defer func() { - testutil.Ok(t, h.Close()) + assert.NoError(t, h.Close()) }() app := h.Appender(context.Background()) _, err := app.Add(labels.FromStrings("a", "b"), 1, 2) - testutil.Ok(t, err) + assert.NoError(t, err) - testutil.Ok(t, app.Rollback()) + assert.NoError(t, app.Rollback()) recs := readTestWAL(t, w.Dir()) - testutil.Equals(t, 1, len(recs)) + assert.Equal(t, 1, len(recs)) series, ok := recs[0].([]record.RefSeries) - testutil.Assert(t, ok, "expected series record but got %+v", recs[0]) - testutil.Equals(t, []record.RefSeries{{Ref: 1, Labels: labels.FromStrings("a", "b")}}, series) + assert.True(t, ok, "expected series record but got %+v", recs[0]) + assert.Equal(t, []record.RefSeries{{Ref: 1, Labels: labels.FromStrings("a", "b")}}, series) }) } } @@ -1243,50 +1244,50 @@ func TestWalRepair_DecodingError(t *testing.T) { for _, compress := range []bool{false, true} { t.Run(fmt.Sprintf("%s,compress=%t", name, compress), func(t *testing.T) { dir, err := ioutil.TempDir("", "wal_repair") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() // Fill the wal and corrupt it. { w, err := wal.New(nil, nil, filepath.Join(dir, "wal"), compress) - testutil.Ok(t, err) + assert.NoError(t, err) for i := 1; i <= test.totalRecs; i++ { // At this point insert a corrupted record. if i-1 == test.expRecs { - testutil.Ok(t, w.Log(test.corrFunc(test.rec))) + assert.NoError(t, w.Log(test.corrFunc(test.rec))) continue } - testutil.Ok(t, w.Log(test.rec)) + assert.NoError(t, w.Log(test.rec)) } h, err := NewHead(nil, nil, w, 1, w.Dir(), nil, DefaultStripeSize, nil) - testutil.Ok(t, err) - testutil.Equals(t, 0.0, prom_testutil.ToFloat64(h.metrics.walCorruptionsTotal)) + assert.NoError(t, err) + assert.Equal(t, 0.0, prom_testutil.ToFloat64(h.metrics.walCorruptionsTotal)) initErr := h.Init(math.MinInt64) err = errors.Cause(initErr) // So that we can pick up errors even if wrapped. _, corrErr := err.(*wal.CorruptionErr) - testutil.Assert(t, corrErr, "reading the wal didn't return corruption error") - testutil.Ok(t, w.Close()) + assert.True(t, corrErr, "reading the wal didn't return corruption error") + assert.NoError(t, w.Close()) } // Open the db to trigger a repair. { db, err := Open(dir, nil, nil, DefaultOptions()) - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }() - testutil.Equals(t, 1.0, prom_testutil.ToFloat64(db.head.metrics.walCorruptionsTotal)) + assert.Equal(t, 1.0, prom_testutil.ToFloat64(db.head.metrics.walCorruptionsTotal)) } // Read the wal content after the repair. { sr, err := wal.NewSegmentsReader(filepath.Join(dir, "wal")) - testutil.Ok(t, err) + assert.NoError(t, err) defer sr.Close() r := wal.NewReader(sr) @@ -1294,8 +1295,8 @@ func TestWalRepair_DecodingError(t *testing.T) { for r.Next() { actRec++ } - testutil.Ok(t, r.Err()) - testutil.Equals(t, test.expRecs, actRec, "Wrong number of intact records") + assert.NoError(t, r.Err()) + assert.Equal(t, test.expRecs, actRec, "Wrong number of intact records") } }) } @@ -1304,9 +1305,9 @@ func TestWalRepair_DecodingError(t *testing.T) { func TestHeadReadWriterRepair(t *testing.T) { dir, err := ioutil.TempDir("", "head_read_writer_repair") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() const chunkRange = 1000 @@ -1315,101 +1316,101 @@ func TestHeadReadWriterRepair(t *testing.T) { // Fill the chunk segments and corrupt it. { w, err := wal.New(nil, nil, walDir, false) - testutil.Ok(t, err) + assert.NoError(t, err) h, err := NewHead(nil, nil, w, chunkRange, dir, nil, DefaultStripeSize, nil) - testutil.Ok(t, err) - testutil.Equals(t, 0.0, prom_testutil.ToFloat64(h.metrics.mmapChunkCorruptionTotal)) - testutil.Ok(t, h.Init(math.MinInt64)) + assert.NoError(t, err) + assert.Equal(t, 0.0, prom_testutil.ToFloat64(h.metrics.mmapChunkCorruptionTotal)) + assert.NoError(t, h.Init(math.MinInt64)) s, created, _ := h.getOrCreate(1, labels.FromStrings("a", "1")) - testutil.Assert(t, created, "series was not created") + assert.True(t, created, "series was not created") for i := 0; i < 7; i++ { ok, chunkCreated := s.append(int64(i*chunkRange), float64(i*chunkRange), 0, h.chunkDiskMapper) - testutil.Assert(t, ok, "series append failed") - testutil.Assert(t, chunkCreated, "chunk was not created") + assert.True(t, ok, "series append failed") + assert.True(t, chunkCreated, "chunk was not created") ok, chunkCreated = s.append(int64(i*chunkRange)+chunkRange-1, float64(i*chunkRange), 0, h.chunkDiskMapper) - testutil.Assert(t, ok, "series append failed") - testutil.Assert(t, !chunkCreated, "chunk was created") - testutil.Ok(t, h.chunkDiskMapper.CutNewFile()) + assert.True(t, ok, "series append failed") + assert.True(t, !chunkCreated, "chunk was created") + assert.NoError(t, h.chunkDiskMapper.CutNewFile()) } - testutil.Ok(t, h.Close()) + assert.NoError(t, h.Close()) // Verify that there are 7 segment files. files, err := ioutil.ReadDir(mmappedChunksDir(dir)) - testutil.Ok(t, err) - testutil.Equals(t, 7, len(files)) + assert.NoError(t, err) + assert.Equal(t, 7, len(files)) // Corrupt the 4th file by writing a random byte to series ref. f, err := os.OpenFile(filepath.Join(mmappedChunksDir(dir), files[3].Name()), os.O_WRONLY, 0666) - testutil.Ok(t, err) + assert.NoError(t, err) n, err := f.WriteAt([]byte{67, 88}, chunks.HeadChunkFileHeaderSize+2) - testutil.Ok(t, err) - testutil.Equals(t, 2, n) - testutil.Ok(t, f.Close()) + assert.NoError(t, err) + assert.Equal(t, 2, n) + assert.NoError(t, f.Close()) } // Open the db to trigger a repair. { db, err := Open(dir, nil, nil, DefaultOptions()) - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }() - testutil.Equals(t, 1.0, prom_testutil.ToFloat64(db.head.metrics.mmapChunkCorruptionTotal)) + assert.Equal(t, 1.0, prom_testutil.ToFloat64(db.head.metrics.mmapChunkCorruptionTotal)) } // Verify that there are 3 segment files after the repair. // The segments from the corrupt segment should be removed. { files, err := ioutil.ReadDir(mmappedChunksDir(dir)) - testutil.Ok(t, err) - testutil.Equals(t, 3, len(files)) + assert.NoError(t, err) + assert.Equal(t, 3, len(files)) } } func TestNewWalSegmentOnTruncate(t *testing.T) { h, wlog := newTestHead(t, 1000, false) defer func() { - testutil.Ok(t, h.Close()) + assert.NoError(t, h.Close()) }() add := func(ts int64) { app := h.Appender(context.Background()) _, err := app.Add(labels.Labels{{Name: "a", Value: "b"}}, ts, 0) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) } add(0) _, last, err := wal.Segments(wlog.Dir()) - testutil.Ok(t, err) - testutil.Equals(t, 0, last) + assert.NoError(t, err) + assert.Equal(t, 0, last) add(1) - testutil.Ok(t, h.Truncate(1)) + assert.NoError(t, h.Truncate(1)) _, last, err = wal.Segments(wlog.Dir()) - testutil.Ok(t, err) - testutil.Equals(t, 1, last) + assert.NoError(t, err) + assert.Equal(t, 1, last) add(2) - testutil.Ok(t, h.Truncate(2)) + assert.NoError(t, h.Truncate(2)) _, last, err = wal.Segments(wlog.Dir()) - testutil.Ok(t, err) - testutil.Equals(t, 2, last) + assert.NoError(t, err) + assert.Equal(t, 2, last) } func TestAddDuplicateLabelName(t *testing.T) { h, _ := newTestHead(t, 1000, false) defer func() { - testutil.Ok(t, h.Close()) + assert.NoError(t, h.Close()) }() add := func(labels labels.Labels, labelName string) { app := h.Appender(context.Background()) _, err := app.Add(labels, 0, 0) - testutil.NotOk(t, err) - testutil.Equals(t, fmt.Sprintf(`label name "%s" is not unique: invalid sample`, labelName), err.Error()) + assert.Error(t, err) + assert.Equal(t, fmt.Sprintf(`label name "%s" is not unique: invalid sample`, labelName), err.Error()) } add(labels.Labels{{Name: "a", Value: "c"}, {Name: "a", Value: "b"}}, "a") @@ -1422,13 +1423,13 @@ func TestMemSeriesIsolation(t *testing.T) { lastValue := func(h *Head, maxAppendID uint64) int { idx, err := h.Index() - testutil.Ok(t, err) + assert.NoError(t, err) iso := h.iso.State() iso.maxAppendID = maxAppendID chunks, err := h.chunksRange(math.MinInt64, math.MaxInt64, iso) - testutil.Ok(t, err) + assert.NoError(t, err) // Hm.. here direct block chunk querier might be required? querier := blockQuerier{ blockBaseQuerier: &blockBaseQuerier{ @@ -1441,13 +1442,13 @@ func TestMemSeriesIsolation(t *testing.T) { }, } - testutil.Ok(t, err) + assert.NoError(t, err) defer querier.Close() ss := querier.Select(false, nil, labels.MustNewMatcher(labels.MatchEqual, "foo", "bar")) _, seriesSet, ws, err := expandSeriesSet(ss) - testutil.Ok(t, err) - testutil.Equals(t, 0, len(ws)) + assert.NoError(t, err) + assert.Equal(t, 0, len(ws)) for _, series := range seriesSet { return int(series[len(series)-1].v) @@ -1469,8 +1470,8 @@ func TestMemSeriesIsolation(t *testing.T) { } _, err := app.Add(labels.FromStrings("foo", "bar"), int64(i), float64(i)) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) } return i } @@ -1484,172 +1485,172 @@ func TestMemSeriesIsolation(t *testing.T) { testIsolation(hb, i) // Test simple cases in different chunks when no appendID cleanup has been performed. - testutil.Equals(t, 10, lastValue(hb, 10)) - testutil.Equals(t, 130, lastValue(hb, 130)) - testutil.Equals(t, 160, lastValue(hb, 160)) - testutil.Equals(t, 240, lastValue(hb, 240)) - testutil.Equals(t, 500, lastValue(hb, 500)) - testutil.Equals(t, 750, lastValue(hb, 750)) - testutil.Equals(t, 995, lastValue(hb, 995)) - testutil.Equals(t, 999, lastValue(hb, 999)) + assert.Equal(t, 10, lastValue(hb, 10)) + assert.Equal(t, 130, lastValue(hb, 130)) + assert.Equal(t, 160, lastValue(hb, 160)) + assert.Equal(t, 240, lastValue(hb, 240)) + assert.Equal(t, 500, lastValue(hb, 500)) + assert.Equal(t, 750, lastValue(hb, 750)) + assert.Equal(t, 995, lastValue(hb, 995)) + assert.Equal(t, 999, lastValue(hb, 999)) // Cleanup appendIDs below 500. app := hb.appender() app.cleanupAppendIDsBelow = 500 _, err := app.Add(labels.FromStrings("foo", "bar"), int64(i), float64(i)) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) i++ // We should not get queries with a maxAppendID below 500 after the cleanup, // but they only take the remaining appendIDs into account. - testutil.Equals(t, 499, lastValue(hb, 10)) - testutil.Equals(t, 499, lastValue(hb, 130)) - testutil.Equals(t, 499, lastValue(hb, 160)) - testutil.Equals(t, 499, lastValue(hb, 240)) - testutil.Equals(t, 500, lastValue(hb, 500)) - testutil.Equals(t, 995, lastValue(hb, 995)) - testutil.Equals(t, 999, lastValue(hb, 999)) + assert.Equal(t, 499, lastValue(hb, 10)) + assert.Equal(t, 499, lastValue(hb, 130)) + assert.Equal(t, 499, lastValue(hb, 160)) + assert.Equal(t, 499, lastValue(hb, 240)) + assert.Equal(t, 500, lastValue(hb, 500)) + assert.Equal(t, 995, lastValue(hb, 995)) + assert.Equal(t, 999, lastValue(hb, 999)) // Cleanup appendIDs below 1000, which means the sample buffer is // the only thing with appendIDs. app = hb.appender() app.cleanupAppendIDsBelow = 1000 _, err = app.Add(labels.FromStrings("foo", "bar"), int64(i), float64(i)) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) - testutil.Equals(t, 999, lastValue(hb, 998)) - testutil.Equals(t, 999, lastValue(hb, 999)) - testutil.Equals(t, 1000, lastValue(hb, 1000)) - testutil.Equals(t, 1001, lastValue(hb, 1001)) - testutil.Equals(t, 1002, lastValue(hb, 1002)) - testutil.Equals(t, 1002, lastValue(hb, 1003)) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) + assert.Equal(t, 999, lastValue(hb, 998)) + assert.Equal(t, 999, lastValue(hb, 999)) + assert.Equal(t, 1000, lastValue(hb, 1000)) + assert.Equal(t, 1001, lastValue(hb, 1001)) + assert.Equal(t, 1002, lastValue(hb, 1002)) + assert.Equal(t, 1002, lastValue(hb, 1003)) i++ // Cleanup appendIDs below 1001, but with a rollback. app = hb.appender() app.cleanupAppendIDsBelow = 1001 _, err = app.Add(labels.FromStrings("foo", "bar"), int64(i), float64(i)) - testutil.Ok(t, err) - testutil.Ok(t, app.Rollback()) - testutil.Equals(t, 1000, lastValue(hb, 999)) - testutil.Equals(t, 1000, lastValue(hb, 1000)) - testutil.Equals(t, 1001, lastValue(hb, 1001)) - testutil.Equals(t, 1002, lastValue(hb, 1002)) - testutil.Equals(t, 1002, lastValue(hb, 1003)) + assert.NoError(t, err) + assert.NoError(t, app.Rollback()) + assert.Equal(t, 1000, lastValue(hb, 999)) + assert.Equal(t, 1000, lastValue(hb, 1000)) + assert.Equal(t, 1001, lastValue(hb, 1001)) + assert.Equal(t, 1002, lastValue(hb, 1002)) + assert.Equal(t, 1002, lastValue(hb, 1003)) - testutil.Ok(t, hb.Close()) + assert.NoError(t, hb.Close()) // Test isolation with restart of Head. This is to verify the num samples of chunks after m-map chunk replay. hb, w := newTestHead(t, 1000, false) i = addSamples(hb) - testutil.Ok(t, hb.Close()) + assert.NoError(t, hb.Close()) wlog, err := wal.NewSize(nil, nil, w.Dir(), 32768, false) - testutil.Ok(t, err) + assert.NoError(t, err) hb, err = NewHead(nil, nil, wlog, 1000, wlog.Dir(), nil, DefaultStripeSize, nil) - defer func() { testutil.Ok(t, hb.Close()) }() - testutil.Ok(t, err) - testutil.Ok(t, hb.Init(0)) + defer func() { assert.NoError(t, hb.Close()) }() + assert.NoError(t, err) + assert.NoError(t, hb.Init(0)) // No appends after restarting. Hence all should return the last value. - testutil.Equals(t, 1000, lastValue(hb, 10)) - testutil.Equals(t, 1000, lastValue(hb, 130)) - testutil.Equals(t, 1000, lastValue(hb, 160)) - testutil.Equals(t, 1000, lastValue(hb, 240)) - testutil.Equals(t, 1000, lastValue(hb, 500)) + assert.Equal(t, 1000, lastValue(hb, 10)) + assert.Equal(t, 1000, lastValue(hb, 130)) + assert.Equal(t, 1000, lastValue(hb, 160)) + assert.Equal(t, 1000, lastValue(hb, 240)) + assert.Equal(t, 1000, lastValue(hb, 500)) // Cleanup appendIDs below 1000, which means the sample buffer is // the only thing with appendIDs. app = hb.appender() _, err = app.Add(labels.FromStrings("foo", "bar"), int64(i), float64(i)) i++ - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) - testutil.Equals(t, 1001, lastValue(hb, 998)) - testutil.Equals(t, 1001, lastValue(hb, 999)) - testutil.Equals(t, 1001, lastValue(hb, 1000)) - testutil.Equals(t, 1001, lastValue(hb, 1001)) - testutil.Equals(t, 1001, lastValue(hb, 1002)) - testutil.Equals(t, 1001, lastValue(hb, 1003)) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) + assert.Equal(t, 1001, lastValue(hb, 998)) + assert.Equal(t, 1001, lastValue(hb, 999)) + assert.Equal(t, 1001, lastValue(hb, 1000)) + assert.Equal(t, 1001, lastValue(hb, 1001)) + assert.Equal(t, 1001, lastValue(hb, 1002)) + assert.Equal(t, 1001, lastValue(hb, 1003)) // Cleanup appendIDs below 1002, but with a rollback. app = hb.appender() _, err = app.Add(labels.FromStrings("foo", "bar"), int64(i), float64(i)) - testutil.Ok(t, err) - testutil.Ok(t, app.Rollback()) - testutil.Equals(t, 1001, lastValue(hb, 999)) - testutil.Equals(t, 1001, lastValue(hb, 1000)) - testutil.Equals(t, 1001, lastValue(hb, 1001)) - testutil.Equals(t, 1001, lastValue(hb, 1002)) - testutil.Equals(t, 1001, lastValue(hb, 1003)) + assert.NoError(t, err) + assert.NoError(t, app.Rollback()) + assert.Equal(t, 1001, lastValue(hb, 999)) + assert.Equal(t, 1001, lastValue(hb, 1000)) + assert.Equal(t, 1001, lastValue(hb, 1001)) + assert.Equal(t, 1001, lastValue(hb, 1002)) + assert.Equal(t, 1001, lastValue(hb, 1003)) } func TestIsolationRollback(t *testing.T) { // Rollback after a failed append and test if the low watermark has progressed anyway. hb, _ := newTestHead(t, 1000, false) defer func() { - testutil.Ok(t, hb.Close()) + assert.NoError(t, hb.Close()) }() app := hb.Appender(context.Background()) _, err := app.Add(labels.FromStrings("foo", "bar"), 0, 0) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) - testutil.Equals(t, uint64(1), hb.iso.lowWatermark()) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) + assert.Equal(t, uint64(1), hb.iso.lowWatermark()) app = hb.Appender(context.Background()) _, err = app.Add(labels.FromStrings("foo", "bar"), 1, 1) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = app.Add(labels.FromStrings("foo", "bar", "foo", "baz"), 2, 2) - testutil.NotOk(t, err) - testutil.Ok(t, app.Rollback()) - testutil.Equals(t, uint64(2), hb.iso.lowWatermark()) + assert.Error(t, err) + assert.NoError(t, app.Rollback()) + assert.Equal(t, uint64(2), hb.iso.lowWatermark()) app = hb.Appender(context.Background()) _, err = app.Add(labels.FromStrings("foo", "bar"), 3, 3) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) - testutil.Equals(t, uint64(3), hb.iso.lowWatermark(), "Low watermark should proceed to 3 even if append #2 was rolled back.") + assert.NoError(t, err) + assert.NoError(t, app.Commit()) + assert.Equal(t, uint64(3), hb.iso.lowWatermark(), "Low watermark should proceed to 3 even if append #2 was rolled back.") } func TestIsolationLowWatermarkMonotonous(t *testing.T) { hb, _ := newTestHead(t, 1000, false) defer func() { - testutil.Ok(t, hb.Close()) + assert.NoError(t, hb.Close()) }() app1 := hb.Appender(context.Background()) _, err := app1.Add(labels.FromStrings("foo", "bar"), 0, 0) - testutil.Ok(t, err) - testutil.Ok(t, app1.Commit()) - testutil.Equals(t, uint64(1), hb.iso.lowWatermark(), "Low watermark should by 1 after 1st append.") + assert.NoError(t, err) + assert.NoError(t, app1.Commit()) + assert.Equal(t, uint64(1), hb.iso.lowWatermark(), "Low watermark should by 1 after 1st append.") app1 = hb.Appender(context.Background()) _, err = app1.Add(labels.FromStrings("foo", "bar"), 1, 1) - testutil.Ok(t, err) - testutil.Equals(t, uint64(2), hb.iso.lowWatermark(), "Low watermark should be two, even if append is not committed yet.") + assert.NoError(t, err) + assert.Equal(t, uint64(2), hb.iso.lowWatermark(), "Low watermark should be two, even if append is not committed yet.") app2 := hb.Appender(context.Background()) _, err = app2.Add(labels.FromStrings("foo", "baz"), 1, 1) - testutil.Ok(t, err) - testutil.Ok(t, app2.Commit()) - testutil.Equals(t, uint64(2), hb.iso.lowWatermark(), "Low watermark should stay two because app1 is not committed yet.") + assert.NoError(t, err) + assert.NoError(t, app2.Commit()) + assert.Equal(t, uint64(2), hb.iso.lowWatermark(), "Low watermark should stay two because app1 is not committed yet.") is := hb.iso.State() - testutil.Equals(t, uint64(2), hb.iso.lowWatermark(), "After simulated read (iso state retrieved), low watermark should stay at 2.") + assert.Equal(t, uint64(2), hb.iso.lowWatermark(), "After simulated read (iso state retrieved), low watermark should stay at 2.") - testutil.Ok(t, app1.Commit()) - testutil.Equals(t, uint64(2), hb.iso.lowWatermark(), "Even after app1 is committed, low watermark should stay at 2 because read is still ongoing.") + assert.NoError(t, app1.Commit()) + assert.Equal(t, uint64(2), hb.iso.lowWatermark(), "Even after app1 is committed, low watermark should stay at 2 because read is still ongoing.") is.Close() - testutil.Equals(t, uint64(3), hb.iso.lowWatermark(), "After read has finished (iso state closed), low watermark should jump to three.") + assert.Equal(t, uint64(3), hb.iso.lowWatermark(), "After read has finished (iso state closed), low watermark should jump to three.") } func TestIsolationAppendIDZeroIsNoop(t *testing.T) { h, _ := newTestHead(t, 1000, false) defer func() { - testutil.Ok(t, h.Close()) + assert.NoError(t, h.Close()) }() h.initTime(0) @@ -1657,8 +1658,8 @@ func TestIsolationAppendIDZeroIsNoop(t *testing.T) { s, _, _ := h.getOrCreate(1, labels.FromStrings("a", "1")) ok, _ := s.append(0, 0, 0, h.chunkDiskMapper) - testutil.Assert(t, ok, "Series append failed.") - testutil.Equals(t, 0, s.txs.txIDCount, "Series should not have an appendID after append with appendID=0.") + assert.True(t, ok, "Series append failed.") + assert.Equal(t, 0, s.txs.txIDCount, "Series should not have an appendID after append with appendID=0.") } func TestHeadSeriesChunkRace(t *testing.T) { @@ -1670,31 +1671,31 @@ func TestHeadSeriesChunkRace(t *testing.T) { func TestIsolationWithoutAdd(t *testing.T) { hb, _ := newTestHead(t, 1000, false) defer func() { - testutil.Ok(t, hb.Close()) + assert.NoError(t, hb.Close()) }() app := hb.Appender(context.Background()) - testutil.Ok(t, app.Commit()) + assert.NoError(t, app.Commit()) app = hb.Appender(context.Background()) _, err := app.Add(labels.FromStrings("foo", "baz"), 1, 1) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) - testutil.Equals(t, hb.iso.lastAppendID(), hb.iso.lowWatermark(), "High watermark should be equal to the low watermark") + assert.Equal(t, hb.iso.lastAppendID(), hb.iso.lowWatermark(), "High watermark should be equal to the low watermark") } func TestOutOfOrderSamplesMetric(t *testing.T) { dir, err := ioutil.TempDir("", "test") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() db, err := Open(dir, nil, nil, DefaultOptions()) - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }() db.DisableCompactions() @@ -1702,90 +1703,90 @@ func TestOutOfOrderSamplesMetric(t *testing.T) { app := db.Appender(ctx) for i := 1; i <= 5; i++ { _, err = app.Add(labels.FromStrings("a", "b"), int64(i), 99) - testutil.Ok(t, err) + assert.NoError(t, err) } - testutil.Ok(t, app.Commit()) + assert.NoError(t, app.Commit()) // Test out of order metric. - testutil.Equals(t, 0.0, prom_testutil.ToFloat64(db.head.metrics.outOfOrderSamples)) + assert.Equal(t, 0.0, prom_testutil.ToFloat64(db.head.metrics.outOfOrderSamples)) app = db.Appender(ctx) _, err = app.Add(labels.FromStrings("a", "b"), 2, 99) - testutil.Equals(t, storage.ErrOutOfOrderSample, err) - testutil.Equals(t, 1.0, prom_testutil.ToFloat64(db.head.metrics.outOfOrderSamples)) + assert.Equal(t, storage.ErrOutOfOrderSample, err) + assert.Equal(t, 1.0, prom_testutil.ToFloat64(db.head.metrics.outOfOrderSamples)) _, err = app.Add(labels.FromStrings("a", "b"), 3, 99) - testutil.Equals(t, storage.ErrOutOfOrderSample, err) - testutil.Equals(t, 2.0, prom_testutil.ToFloat64(db.head.metrics.outOfOrderSamples)) + assert.Equal(t, storage.ErrOutOfOrderSample, err) + assert.Equal(t, 2.0, prom_testutil.ToFloat64(db.head.metrics.outOfOrderSamples)) _, err = app.Add(labels.FromStrings("a", "b"), 4, 99) - testutil.Equals(t, storage.ErrOutOfOrderSample, err) - testutil.Equals(t, 3.0, prom_testutil.ToFloat64(db.head.metrics.outOfOrderSamples)) - testutil.Ok(t, app.Commit()) + assert.Equal(t, storage.ErrOutOfOrderSample, err) + assert.Equal(t, 3.0, prom_testutil.ToFloat64(db.head.metrics.outOfOrderSamples)) + assert.NoError(t, app.Commit()) // Compact Head to test out of bound metric. app = db.Appender(ctx) _, err = app.Add(labels.FromStrings("a", "b"), DefaultBlockDuration*2, 99) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) - testutil.Equals(t, int64(math.MinInt64), db.head.minValidTime.Load()) - testutil.Ok(t, db.Compact()) - testutil.Assert(t, db.head.minValidTime.Load() > 0, "") + assert.Equal(t, int64(math.MinInt64), db.head.minValidTime.Load()) + assert.NoError(t, db.Compact()) + assert.True(t, db.head.minValidTime.Load() > 0, "") app = db.Appender(ctx) _, err = app.Add(labels.FromStrings("a", "b"), db.head.minValidTime.Load()-2, 99) - testutil.Equals(t, storage.ErrOutOfBounds, err) - testutil.Equals(t, 1.0, prom_testutil.ToFloat64(db.head.metrics.outOfBoundSamples)) + assert.Equal(t, storage.ErrOutOfBounds, err) + assert.Equal(t, 1.0, prom_testutil.ToFloat64(db.head.metrics.outOfBoundSamples)) _, err = app.Add(labels.FromStrings("a", "b"), db.head.minValidTime.Load()-1, 99) - testutil.Equals(t, storage.ErrOutOfBounds, err) - testutil.Equals(t, 2.0, prom_testutil.ToFloat64(db.head.metrics.outOfBoundSamples)) - testutil.Ok(t, app.Commit()) + assert.Equal(t, storage.ErrOutOfBounds, err) + assert.Equal(t, 2.0, prom_testutil.ToFloat64(db.head.metrics.outOfBoundSamples)) + assert.NoError(t, app.Commit()) // Some more valid samples for out of order. app = db.Appender(ctx) for i := 1; i <= 5; i++ { _, err = app.Add(labels.FromStrings("a", "b"), db.head.minValidTime.Load()+DefaultBlockDuration+int64(i), 99) - testutil.Ok(t, err) + assert.NoError(t, err) } - testutil.Ok(t, app.Commit()) + assert.NoError(t, app.Commit()) // Test out of order metric. app = db.Appender(ctx) _, err = app.Add(labels.FromStrings("a", "b"), db.head.minValidTime.Load()+DefaultBlockDuration+2, 99) - testutil.Equals(t, storage.ErrOutOfOrderSample, err) - testutil.Equals(t, 4.0, prom_testutil.ToFloat64(db.head.metrics.outOfOrderSamples)) + assert.Equal(t, storage.ErrOutOfOrderSample, err) + assert.Equal(t, 4.0, prom_testutil.ToFloat64(db.head.metrics.outOfOrderSamples)) _, err = app.Add(labels.FromStrings("a", "b"), db.head.minValidTime.Load()+DefaultBlockDuration+3, 99) - testutil.Equals(t, storage.ErrOutOfOrderSample, err) - testutil.Equals(t, 5.0, prom_testutil.ToFloat64(db.head.metrics.outOfOrderSamples)) + assert.Equal(t, storage.ErrOutOfOrderSample, err) + assert.Equal(t, 5.0, prom_testutil.ToFloat64(db.head.metrics.outOfOrderSamples)) _, err = app.Add(labels.FromStrings("a", "b"), db.head.minValidTime.Load()+DefaultBlockDuration+4, 99) - testutil.Equals(t, storage.ErrOutOfOrderSample, err) - testutil.Equals(t, 6.0, prom_testutil.ToFloat64(db.head.metrics.outOfOrderSamples)) - testutil.Ok(t, app.Commit()) + assert.Equal(t, storage.ErrOutOfOrderSample, err) + assert.Equal(t, 6.0, prom_testutil.ToFloat64(db.head.metrics.outOfOrderSamples)) + assert.NoError(t, app.Commit()) } func testHeadSeriesChunkRace(t *testing.T) { h, _ := newTestHead(t, 1000, false) defer func() { - testutil.Ok(t, h.Close()) + assert.NoError(t, h.Close()) }() - testutil.Ok(t, h.Init(0)) + assert.NoError(t, h.Init(0)) app := h.Appender(context.Background()) s2, err := app.Add(labels.FromStrings("foo2", "bar"), 5, 0) - testutil.Ok(t, err) + assert.NoError(t, err) for ts := int64(6); ts < 11; ts++ { err = app.AddFast(s2, ts, 0) - testutil.Ok(t, err) + assert.NoError(t, err) } - testutil.Ok(t, app.Commit()) + assert.NoError(t, app.Commit()) var wg sync.WaitGroup matcher := labels.MustNewMatcher(labels.MatchEqual, "", "") q, err := NewBlockQuerier(h, 18, 22) - testutil.Ok(t, err) + assert.NoError(t, err) defer q.Close() wg.Add(1) @@ -1797,14 +1798,14 @@ func testHeadSeriesChunkRace(t *testing.T) { ss := q.Select(false, nil, matcher) for ss.Next() { } - testutil.Ok(t, ss.Err()) + assert.NoError(t, ss.Err()) wg.Wait() } func TestHeadLabelNamesValuesWithMinMaxRange(t *testing.T) { head, _ := newTestHead(t, 1000, false) defer func() { - testutil.Ok(t, head.Close()) + assert.NoError(t, head.Close()) }() const ( @@ -1824,11 +1825,11 @@ func TestHeadLabelNamesValuesWithMinMaxRange(t *testing.T) { app := head.Appender(context.Background()) for i, name := range expectedLabelNames { _, err := app.Add(labels.Labels{{Name: name, Value: expectedLabelValues[i]}}, seriesTimestamps[i], 0) - testutil.Ok(t, err) + assert.NoError(t, err) } - testutil.Ok(t, app.Commit()) - testutil.Equals(t, head.MinTime(), firstSeriesTimestamp) - testutil.Equals(t, head.MaxTime(), lastSeriesTimestamp) + assert.NoError(t, app.Commit()) + assert.Equal(t, head.MinTime(), firstSeriesTimestamp) + assert.Equal(t, head.MaxTime(), lastSeriesTimestamp) var testCases = []struct { name string @@ -1847,13 +1848,13 @@ func TestHeadLabelNamesValuesWithMinMaxRange(t *testing.T) { t.Run(tt.name, func(t *testing.T) { headIdxReader := head.indexRange(tt.mint, tt.maxt) actualLabelNames, err := headIdxReader.LabelNames() - testutil.Ok(t, err) - testutil.Equals(t, tt.expectedNames, actualLabelNames) + assert.NoError(t, err) + assert.Equal(t, tt.expectedNames, actualLabelNames) if len(tt.expectedValues) > 0 { for i, name := range expectedLabelNames { actualLabelValue, err := headIdxReader.SortedLabelValues(name) - testutil.Ok(t, err) - testutil.Equals(t, []string{tt.expectedValues[i]}, actualLabelValue) + assert.NoError(t, err) + assert.Equal(t, []string{tt.expectedValues[i]}, actualLabelValue) } } }) @@ -1863,34 +1864,34 @@ func TestHeadLabelNamesValuesWithMinMaxRange(t *testing.T) { func TestErrReuseAppender(t *testing.T) { head, _ := newTestHead(t, 1000, false) defer func() { - testutil.Ok(t, head.Close()) + assert.NoError(t, head.Close()) }() app := head.Appender(context.Background()) _, err := app.Add(labels.Labels{{Name: "test", Value: "test"}}, 0, 0) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) - testutil.NotOk(t, app.Commit()) - testutil.NotOk(t, app.Rollback()) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) + assert.Error(t, app.Commit()) + assert.Error(t, app.Rollback()) app = head.Appender(context.Background()) _, err = app.Add(labels.Labels{{Name: "test", Value: "test"}}, 1, 0) - testutil.Ok(t, err) - testutil.Ok(t, app.Rollback()) - testutil.NotOk(t, app.Rollback()) - testutil.NotOk(t, app.Commit()) + assert.NoError(t, err) + assert.NoError(t, app.Rollback()) + assert.Error(t, app.Rollback()) + assert.Error(t, app.Commit()) app = head.Appender(context.Background()) _, err = app.Add(labels.Labels{{Name: "test", Value: "test"}}, 2, 0) - testutil.Ok(t, err) - testutil.Ok(t, app.Commit()) - testutil.NotOk(t, app.Rollback()) - testutil.NotOk(t, app.Commit()) + assert.NoError(t, err) + assert.NoError(t, app.Commit()) + assert.Error(t, app.Rollback()) + assert.Error(t, app.Commit()) app = head.Appender(context.Background()) _, err = app.Add(labels.Labels{{Name: "test", Value: "test"}}, 3, 0) - testutil.Ok(t, err) - testutil.Ok(t, app.Rollback()) - testutil.NotOk(t, app.Commit()) - testutil.NotOk(t, app.Rollback()) + assert.NoError(t, err) + assert.NoError(t, app.Rollback()) + assert.Error(t, app.Commit()) + assert.Error(t, app.Rollback()) } diff --git a/tsdb/index/index.go b/tsdb/index/index.go index d7559b346a..b3d8293f1b 100644 --- a/tsdb/index/index.go +++ b/tsdb/index/index.go @@ -29,6 +29,7 @@ import ( "unsafe" "github.com/pkg/errors" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/tsdb/chunks" "github.com/prometheus/prometheus/tsdb/encoding" diff --git a/tsdb/index/index_test.go b/tsdb/index/index_test.go index c628181c78..3cf187d3a8 100644 --- a/tsdb/index/index_test.go +++ b/tsdb/index/index_test.go @@ -25,12 +25,14 @@ import ( "testing" "github.com/pkg/errors" + "github.com/stretchr/testify/assert" + "go.uber.org/goleak" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/tsdb/chunkenc" "github.com/prometheus/prometheus/tsdb/chunks" "github.com/prometheus/prometheus/tsdb/encoding" "github.com/prometheus/prometheus/util/testutil" - "go.uber.org/goleak" ) func TestMain(m *testing.M) { @@ -135,44 +137,44 @@ func (m mockIndex) Series(ref uint64, lset *labels.Labels, chks *[]chunks.Meta) func TestIndexRW_Create_Open(t *testing.T) { dir, err := ioutil.TempDir("", "test_index_create") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() fn := filepath.Join(dir, indexFilename) // An empty index must still result in a readable file. iw, err := NewWriter(context.Background(), fn) - testutil.Ok(t, err) - testutil.Ok(t, iw.Close()) + assert.NoError(t, err) + assert.NoError(t, iw.Close()) ir, err := NewFileReader(fn) - testutil.Ok(t, err) - testutil.Ok(t, ir.Close()) + assert.NoError(t, err) + assert.NoError(t, ir.Close()) // Modify magic header must cause open to fail. f, err := os.OpenFile(fn, os.O_WRONLY, 0666) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = f.WriteAt([]byte{0, 0}, 0) - testutil.Ok(t, err) + assert.NoError(t, err) f.Close() _, err = NewFileReader(dir) - testutil.NotOk(t, err) + assert.Error(t, err) } func TestIndexRW_Postings(t *testing.T) { dir, err := ioutil.TempDir("", "test_index_postings") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() fn := filepath.Join(dir, indexFilename) iw, err := NewWriter(context.Background(), fn) - testutil.Ok(t, err) + assert.NoError(t, err) series := []labels.Labels{ labels.FromStrings("a", "1", "b", "1"), @@ -181,27 +183,27 @@ func TestIndexRW_Postings(t *testing.T) { labels.FromStrings("a", "1", "b", "4"), } - testutil.Ok(t, iw.AddSymbol("1")) - testutil.Ok(t, iw.AddSymbol("2")) - testutil.Ok(t, iw.AddSymbol("3")) - testutil.Ok(t, iw.AddSymbol("4")) - testutil.Ok(t, iw.AddSymbol("a")) - testutil.Ok(t, iw.AddSymbol("b")) + assert.NoError(t, iw.AddSymbol("1")) + assert.NoError(t, iw.AddSymbol("2")) + assert.NoError(t, iw.AddSymbol("3")) + assert.NoError(t, iw.AddSymbol("4")) + assert.NoError(t, iw.AddSymbol("a")) + assert.NoError(t, iw.AddSymbol("b")) // Postings lists are only written if a series with the respective // reference was added before. - testutil.Ok(t, iw.AddSeries(1, series[0])) - testutil.Ok(t, iw.AddSeries(2, series[1])) - testutil.Ok(t, iw.AddSeries(3, series[2])) - testutil.Ok(t, iw.AddSeries(4, series[3])) + assert.NoError(t, iw.AddSeries(1, series[0])) + assert.NoError(t, iw.AddSeries(2, series[1])) + assert.NoError(t, iw.AddSeries(3, series[2])) + assert.NoError(t, iw.AddSeries(4, series[3])) - testutil.Ok(t, iw.Close()) + assert.NoError(t, iw.Close()) ir, err := NewFileReader(fn) - testutil.Ok(t, err) + assert.NoError(t, err) p, err := ir.Postings("a", "1") - testutil.Ok(t, err) + assert.NoError(t, err) var l labels.Labels var c []chunks.Meta @@ -209,15 +211,15 @@ func TestIndexRW_Postings(t *testing.T) { for i := 0; p.Next(); i++ { err := ir.Series(p.At(), &l, &c) - testutil.Ok(t, err) - testutil.Equals(t, 0, len(c)) - testutil.Equals(t, series[i], l) + assert.NoError(t, err) + assert.Equal(t, 0, len(c)) + assert.Equal(t, series[i], l) } - testutil.Ok(t, p.Err()) + assert.NoError(t, p.Err()) // The label incides are no longer used, so test them by hand here. labelIndices := map[string][]string{} - testutil.Ok(t, ReadOffsetTable(ir.b, ir.toc.LabelIndicesTable, func(key []string, off uint64, _ int) error { + assert.NoError(t, ReadOffsetTable(ir.b, ir.toc.LabelIndicesTable, func(key []string, off uint64, _ int) error { if len(key) != 1 { return errors.Errorf("unexpected key length for label indices table %d", len(key)) } @@ -238,25 +240,25 @@ func TestIndexRW_Postings(t *testing.T) { labelIndices[key[0]] = vals return d.Err() })) - testutil.Equals(t, map[string][]string{ + assert.Equal(t, map[string][]string{ "a": {"1"}, "b": {"1", "2", "3", "4"}, }, labelIndices) - testutil.Ok(t, ir.Close()) + assert.NoError(t, ir.Close()) } func TestPostingsMany(t *testing.T) { dir, err := ioutil.TempDir("", "test_postings_many") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() fn := filepath.Join(dir, indexFilename) iw, err := NewWriter(context.Background(), fn) - testutil.Ok(t, err) + assert.NoError(t, err) // Create a label in the index which has 999 values. symbols := map[string]struct{}{} @@ -275,17 +277,17 @@ func TestPostingsMany(t *testing.T) { } sort.Strings(syms) for _, s := range syms { - testutil.Ok(t, iw.AddSymbol(s)) + assert.NoError(t, iw.AddSymbol(s)) } for i, s := range series { - testutil.Ok(t, iw.AddSeries(uint64(i), s)) + assert.NoError(t, iw.AddSeries(uint64(i), s)) } - testutil.Ok(t, iw.Close()) + assert.NoError(t, iw.Close()) ir, err := NewFileReader(fn) - testutil.Ok(t, err) - defer func() { testutil.Ok(t, ir.Close()) }() + assert.NoError(t, err) + defer func() { assert.NoError(t, ir.Close()) }() cases := []struct { in []string @@ -320,36 +322,36 @@ func TestPostingsMany(t *testing.T) { for _, c := range cases { it, err := ir.Postings("i", c.in...) - testutil.Ok(t, err) + assert.NoError(t, err) got := []string{} var lbls labels.Labels var metas []chunks.Meta for it.Next() { - testutil.Ok(t, ir.Series(it.At(), &lbls, &metas)) + assert.NoError(t, ir.Series(it.At(), &lbls, &metas)) got = append(got, lbls.Get("i")) } - testutil.Ok(t, it.Err()) + assert.NoError(t, it.Err()) exp := []string{} for _, e := range c.in { if _, ok := symbols[e]; ok && e != "l" { exp = append(exp, e) } } - testutil.Equals(t, exp, got, fmt.Sprintf("input: %v", c.in)) + assert.Equal(t, exp, got, fmt.Sprintf("input: %v", c.in)) } } func TestPersistence_index_e2e(t *testing.T) { dir, err := ioutil.TempDir("", "test_persistence_e2e") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() lbls, err := labels.ReadLabels(filepath.Join("..", "testdata", "20kseries.json"), 20000) - testutil.Ok(t, err) + assert.NoError(t, err) // Sort labels as the index writer expects series in sorted order. sort.Sort(labels.Slice(lbls)) @@ -383,7 +385,7 @@ func TestPersistence_index_e2e(t *testing.T) { } iw, err := NewWriter(context.Background(), filepath.Join(dir, indexFilename)) - testutil.Ok(t, err) + assert.NoError(t, err) syms := []string{} for s := range symbols { @@ -391,7 +393,7 @@ func TestPersistence_index_e2e(t *testing.T) { } sort.Strings(syms) for _, s := range syms { - testutil.Ok(t, iw.AddSymbol(s)) + assert.NoError(t, iw.AddSymbol(s)) } // Population procedure as done by compaction. @@ -404,8 +406,8 @@ func TestPersistence_index_e2e(t *testing.T) { for i, s := range input { err = iw.AddSeries(uint64(i), s.labels, s.chunks...) - testutil.Ok(t, err) - testutil.Ok(t, mi.AddSeries(uint64(i), s.labels, s.chunks...)) + assert.NoError(t, err) + assert.NoError(t, mi.AddSeries(uint64(i), s.labels, s.chunks...)) for _, l := range s.labels { valset, ok := values[l.Name] @@ -419,36 +421,36 @@ func TestPersistence_index_e2e(t *testing.T) { } err = iw.Close() - testutil.Ok(t, err) + assert.NoError(t, err) ir, err := NewFileReader(filepath.Join(dir, indexFilename)) - testutil.Ok(t, err) + assert.NoError(t, err) for p := range mi.postings { gotp, err := ir.Postings(p.Name, p.Value) - testutil.Ok(t, err) + assert.NoError(t, err) expp, err := mi.Postings(p.Name, p.Value) - testutil.Ok(t, err) + assert.NoError(t, err) var lset, explset labels.Labels var chks, expchks []chunks.Meta for gotp.Next() { - testutil.Assert(t, expp.Next() == true, "") + assert.True(t, expp.Next() == true, "") ref := gotp.At() err := ir.Series(ref, &lset, &chks) - testutil.Ok(t, err) + assert.NoError(t, err) err = mi.Series(expp.At(), &explset, &expchks) - testutil.Ok(t, err) - testutil.Equals(t, explset, lset) - testutil.Equals(t, expchks, chks) + assert.NoError(t, err) + assert.Equal(t, explset, lset) + assert.Equal(t, expchks, chks) } - testutil.Assert(t, expp.Next() == false, "Expected no more postings for %q=%q", p.Name, p.Value) - testutil.Ok(t, gotp.Err()) + assert.True(t, expp.Next() == false, "Expected no more postings for %q=%q", p.Name, p.Value) + assert.NoError(t, gotp.Err()) } labelPairs := map[string][]string{} @@ -459,11 +461,11 @@ func TestPersistence_index_e2e(t *testing.T) { sort.Strings(v) res, err := ir.SortedLabelValues(k) - testutil.Ok(t, err) + assert.NoError(t, err) - testutil.Equals(t, len(v), len(res)) + assert.Equal(t, len(v), len(res)) for i := 0; i < len(v); i++ { - testutil.Equals(t, v[i], res[i]) + assert.Equal(t, v[i], res[i]) } } @@ -472,29 +474,29 @@ func TestPersistence_index_e2e(t *testing.T) { for it.Next() { gotSymbols = append(gotSymbols, it.At()) } - testutil.Ok(t, it.Err()) + assert.NoError(t, it.Err()) expSymbols := []string{} for s := range mi.symbols { expSymbols = append(expSymbols, s) } sort.Strings(expSymbols) - testutil.Equals(t, expSymbols, gotSymbols) + assert.Equal(t, expSymbols, gotSymbols) - testutil.Ok(t, ir.Close()) + assert.NoError(t, ir.Close()) } func TestDecbufUvarintWithInvalidBuffer(t *testing.T) { b := realByteSlice([]byte{0x81, 0x81, 0x81, 0x81, 0x81, 0x81}) db := encoding.NewDecbufUvarintAt(b, 0, castagnoliTable) - testutil.NotOk(t, db.Err()) + assert.Error(t, db.Err()) } func TestReaderWithInvalidBuffer(t *testing.T) { b := realByteSlice([]byte{0x81, 0x81, 0x81, 0x81, 0x81, 0x81}) _, err := NewReader(b) - testutil.NotOk(t, err) + assert.Error(t, err) } // TestNewFileReaderErrorNoOpenFiles ensures that in case of an error no file remains open. @@ -503,10 +505,10 @@ func TestNewFileReaderErrorNoOpenFiles(t *testing.T) { idxName := filepath.Join(dir.Path(), "index") err := ioutil.WriteFile(idxName, []byte("corrupted contents"), 0666) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = NewFileReader(idxName) - testutil.NotOk(t, err) + assert.Error(t, err) // dir.Close will fail on Win if idxName fd is not closed on error path. dir.Close() @@ -529,32 +531,32 @@ func TestSymbols(t *testing.T) { buf.PutBE32(checksum) // Check sum at the end. s, err := NewSymbols(realByteSlice(buf.Get()), FormatV2, symbolsStart) - testutil.Ok(t, err) + assert.NoError(t, err) // We store only 4 offsets to symbols. - testutil.Equals(t, 32, s.Size()) + assert.Equal(t, 32, s.Size()) for i := 99; i >= 0; i-- { s, err := s.Lookup(uint32(i)) - testutil.Ok(t, err) - testutil.Equals(t, string(rune(i)), s) + assert.NoError(t, err) + assert.Equal(t, string(rune(i)), s) } _, err = s.Lookup(100) - testutil.NotOk(t, err) + assert.Error(t, err) for i := 99; i >= 0; i-- { r, err := s.ReverseLookup(string(rune(i))) - testutil.Ok(t, err) - testutil.Equals(t, uint32(i), r) + assert.NoError(t, err) + assert.Equal(t, uint32(i), r) } _, err = s.ReverseLookup(string(rune(100))) - testutil.NotOk(t, err) + assert.Error(t, err) iter := s.Iter() i := 0 for iter.Next() { - testutil.Equals(t, string(rune(i)), iter.At()) + assert.Equal(t, string(rune(i)), iter.At()) i++ } - testutil.Ok(t, iter.Err()) + assert.NoError(t, iter.Err()) } diff --git a/tsdb/index/postings_test.go b/tsdb/index/postings_test.go index f75d9ea6ef..efcb4a2558 100644 --- a/tsdb/index/postings_test.go +++ b/tsdb/index/postings_test.go @@ -20,8 +20,9 @@ import ( "sort" "testing" + "github.com/stretchr/testify/assert" + "github.com/prometheus/prometheus/pkg/labels" - "github.com/prometheus/prometheus/util/testutil" ) func TestMemPostings_addFor(t *testing.T) { @@ -31,7 +32,7 @@ func TestMemPostings_addFor(t *testing.T) { p.addFor(5, allPostingsKey) - testutil.Equals(t, []uint64{1, 2, 3, 4, 5, 6, 7, 8}, p.m[allPostingsKey.Name][allPostingsKey.Value]) + assert.Equal(t, []uint64{1, 2, 3, 4, 5, 6, 7, 8}, p.m[allPostingsKey.Name][allPostingsKey.Value]) } func TestMemPostings_ensureOrder(t *testing.T) { @@ -160,12 +161,12 @@ func TestIntersect(t *testing.T) { } expected, err := ExpandPostings(c.res) - testutil.Ok(t, err) + assert.NoError(t, err) i := Intersect(c.in...) if c.res == EmptyPostings() { - testutil.Equals(t, EmptyPostings(), i) + assert.Equal(t, EmptyPostings(), i) return } @@ -174,8 +175,8 @@ func TestIntersect(t *testing.T) { } res, err := ExpandPostings(i) - testutil.Ok(t, err) - testutil.Equals(t, expected, res) + assert.NoError(t, err) + assert.Equal(t, expected, res) }) } } @@ -216,8 +217,8 @@ func TestMultiIntersect(t *testing.T) { res, err := ExpandPostings(Intersect(ps...)) - testutil.Ok(t, err) - testutil.Equals(t, c.res, res) + assert.NoError(t, err) + assert.Equal(t, c.res, res) } } @@ -314,8 +315,8 @@ func TestMultiMerge(t *testing.T) { i3 := newListPostings(1, 2, 5, 6, 7, 8, 1001, 1200) res, err := ExpandPostings(Merge(i1, i2, i3)) - testutil.Ok(t, err) - testutil.Equals(t, []uint64{1, 2, 3, 4, 5, 6, 7, 8, 999, 1000, 1001, 1200}, res) + assert.NoError(t, err) + assert.Equal(t, []uint64{1, 2, 3, 4, 5, 6, 7, 8, 999, 1000, 1001, 1200}, res) } func TestMergedPostings(t *testing.T) { @@ -402,12 +403,12 @@ func TestMergedPostings(t *testing.T) { } expected, err := ExpandPostings(c.res) - testutil.Ok(t, err) + assert.NoError(t, err) m := Merge(c.in...) if c.res == EmptyPostings() { - testutil.Equals(t, EmptyPostings(), m) + assert.Equal(t, EmptyPostings(), m) return } @@ -416,8 +417,8 @@ func TestMergedPostings(t *testing.T) { } res, err := ExpandPostings(m) - testutil.Ok(t, err) - testutil.Equals(t, expected, res) + assert.NoError(t, err) + assert.Equal(t, expected, res) }) } } @@ -470,16 +471,16 @@ func TestMergedPostingsSeek(t *testing.T) { p := Merge(a, b) - testutil.Equals(t, c.success, p.Seek(c.seek)) + assert.Equal(t, c.success, p.Seek(c.seek)) // After Seek(), At() should be called. if c.success { start := p.At() lst, err := ExpandPostings(p) - testutil.Ok(t, err) + assert.NoError(t, err) lst = append([]uint64{start}, lst...) - testutil.Equals(t, c.res, lst) + assert.Equal(t, c.res, lst) } } } @@ -531,8 +532,8 @@ func TestRemovedPostings(t *testing.T) { b := newListPostings(c.b...) res, err := ExpandPostings(newRemovedPostings(a, b)) - testutil.Ok(t, err) - testutil.Equals(t, c.res, res) + assert.NoError(t, err) + assert.Equal(t, c.res, res) } } @@ -555,8 +556,8 @@ func TestRemovedNextStackoverflow(t *testing.T) { gotElem = true } - testutil.Ok(t, rp.Err()) - testutil.Assert(t, !gotElem, "") + assert.NoError(t, rp.Err()) + assert.True(t, !gotElem, "") } func TestRemovedPostingsSeek(t *testing.T) { @@ -631,16 +632,16 @@ func TestRemovedPostingsSeek(t *testing.T) { p := newRemovedPostings(a, b) - testutil.Equals(t, c.success, p.Seek(c.seek)) + assert.Equal(t, c.success, p.Seek(c.seek)) // After Seek(), At() should be called. if c.success { start := p.At() lst, err := ExpandPostings(p) - testutil.Ok(t, err) + assert.NoError(t, err) lst = append([]uint64{start}, lst...) - testutil.Equals(t, c.res, lst) + assert.Equal(t, c.res, lst) } } } @@ -663,12 +664,12 @@ func TestBigEndian(t *testing.T) { t.Run("Iteration", func(t *testing.T) { bep := newBigEndianPostings(beLst) for i := 0; i < num; i++ { - testutil.Assert(t, bep.Next() == true, "") - testutil.Equals(t, uint64(ls[i]), bep.At()) + assert.True(t, bep.Next() == true, "") + assert.Equal(t, uint64(ls[i]), bep.At()) } - testutil.Assert(t, bep.Next() == false, "") - testutil.Assert(t, bep.Err() == nil, "") + assert.True(t, bep.Next() == false, "") + assert.True(t, bep.Err() == nil, "") }) t.Run("Seek", func(t *testing.T) { @@ -712,9 +713,9 @@ func TestBigEndian(t *testing.T) { bep := newBigEndianPostings(beLst) for _, v := range table { - testutil.Equals(t, v.found, bep.Seek(uint64(v.seek))) - testutil.Equals(t, uint64(v.val), bep.At()) - testutil.Assert(t, bep.Err() == nil, "") + assert.Equal(t, v.found, bep.Seek(uint64(v.seek))) + assert.Equal(t, uint64(v.val), bep.At()) + assert.True(t, bep.Err() == nil, "") } }) } @@ -732,8 +733,8 @@ func TestIntersectWithMerge(t *testing.T) { p := Intersect(a, b) res, err := ExpandPostings(p) - testutil.Ok(t, err) - testutil.Equals(t, []uint64{30}, res) + assert.NoError(t, err) + assert.Equal(t, []uint64{30}, res) } func TestWithoutPostings(t *testing.T) { @@ -794,12 +795,12 @@ func TestWithoutPostings(t *testing.T) { } expected, err := ExpandPostings(c.res) - testutil.Ok(t, err) + assert.NoError(t, err) w := Without(c.base, c.drop) if c.res == EmptyPostings() { - testutil.Equals(t, EmptyPostings(), w) + assert.Equal(t, EmptyPostings(), w) return } @@ -808,8 +809,8 @@ func TestWithoutPostings(t *testing.T) { } res, err := ExpandPostings(w) - testutil.Ok(t, err) - testutil.Equals(t, expected, res) + assert.NoError(t, err) + assert.Equal(t, expected, res) }) } } @@ -859,17 +860,17 @@ func TestMemPostings_Delete(t *testing.T) { // Make sure postings gotten before the delete have the old data when // iterated over. expanded, err := ExpandPostings(before) - testutil.Ok(t, err) - testutil.Equals(t, []uint64{1, 2, 3}, expanded) + assert.NoError(t, err) + assert.Equal(t, []uint64{1, 2, 3}, expanded) // Make sure postings gotten after the delete have the new data when // iterated over. expanded, err = ExpandPostings(after) - testutil.Ok(t, err) - testutil.Equals(t, []uint64{1, 3}, expanded) + assert.NoError(t, err) + assert.Equal(t, []uint64{1, 3}, expanded) deleted := p.Get("lbl1", "b") expanded, err = ExpandPostings(deleted) - testutil.Ok(t, err) - testutil.Assert(t, 0 == len(expanded), "expected empty postings, got %v", expanded) + assert.NoError(t, err) + assert.True(t, 0 == len(expanded), "expected empty postings, got %v", expanded) } diff --git a/tsdb/index/postingsstats_test.go b/tsdb/index/postingsstats_test.go index 33e4f80c27..96b9bcda5a 100644 --- a/tsdb/index/postingsstats_test.go +++ b/tsdb/index/postingsstats_test.go @@ -15,7 +15,7 @@ package index import ( "testing" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" ) func TestPostingsStats(t *testing.T) { @@ -33,9 +33,9 @@ func TestPostingsStats(t *testing.T) { stats.push(Stat{Name: "Stuff", Count: 3000000}) data := stats.get() - testutil.Equals(t, 10, len(data)) + assert.Equal(t, 10, len(data)) for i := 0; i < heapLength; i++ { - testutil.Equals(t, uint64(max-i), data[i].Count) + assert.Equal(t, uint64(max-i), data[i].Count) } } @@ -52,8 +52,8 @@ func TestPostingsStats2(t *testing.T) { data := stats.get() - testutil.Equals(t, 4, len(data)) - testutil.Equals(t, uint64(11), data[0].Count) + assert.Equal(t, 4, len(data)) + assert.Equal(t, uint64(11), data[0].Count) } func BenchmarkPostingStatsMaxHep(b *testing.B) { stats := &maxHeap{} diff --git a/tsdb/mocks_test.go b/tsdb/mocks_test.go index c186762a11..ac368e7ba4 100644 --- a/tsdb/mocks_test.go +++ b/tsdb/mocks_test.go @@ -15,6 +15,7 @@ package tsdb import ( "github.com/pkg/errors" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/tsdb/chunkenc" "github.com/prometheus/prometheus/tsdb/chunks" diff --git a/tsdb/querier.go b/tsdb/querier.go index 6df8cba9b4..63c311ee23 100644 --- a/tsdb/querier.go +++ b/tsdb/querier.go @@ -20,6 +20,7 @@ import ( "unicode/utf8" "github.com/pkg/errors" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/storage" "github.com/prometheus/prometheus/tsdb/chunkenc" diff --git a/tsdb/querier_bench_test.go b/tsdb/querier_bench_test.go index 212b673723..189af4e967 100644 --- a/tsdb/querier_bench_test.go +++ b/tsdb/querier_bench_test.go @@ -21,8 +21,9 @@ import ( "strconv" "testing" + "github.com/stretchr/testify/assert" + "github.com/prometheus/prometheus/pkg/labels" - "github.com/prometheus/prometheus/util/testutil" ) // Make entries ~50B in size, to emulate real-world high cardinality. @@ -32,14 +33,14 @@ const ( func BenchmarkPostingsForMatchers(b *testing.B) { chunkDir, err := ioutil.TempDir("", "chunk_dir") - testutil.Ok(b, err) + assert.NoError(b, err) defer func() { - testutil.Ok(b, os.RemoveAll(chunkDir)) + assert.NoError(b, os.RemoveAll(chunkDir)) }() h, err := NewHead(nil, nil, nil, 1000, chunkDir, nil, DefaultStripeSize, nil) - testutil.Ok(b, err) + assert.NoError(b, err) defer func() { - testutil.Ok(b, h.Close()) + assert.NoError(b, h.Close()) }() app := h.Appender(context.Background()) @@ -57,28 +58,28 @@ func BenchmarkPostingsForMatchers(b *testing.B) { addSeries(labels.FromStrings("i", strconv.Itoa(i)+postingsBenchSuffix, "n", "2_"+strconv.Itoa(n)+postingsBenchSuffix, "j", "foo")) } } - testutil.Ok(b, app.Commit()) + assert.NoError(b, app.Commit()) ir, err := h.Index() - testutil.Ok(b, err) + assert.NoError(b, err) b.Run("Head", func(b *testing.B) { benchmarkPostingsForMatchers(b, ir) }) tmpdir, err := ioutil.TempDir("", "test_benchpostingsformatchers") - testutil.Ok(b, err) + assert.NoError(b, err) defer func() { - testutil.Ok(b, os.RemoveAll(tmpdir)) + assert.NoError(b, os.RemoveAll(tmpdir)) }() blockdir := createBlockFromHead(b, tmpdir, h) block, err := OpenBlock(nil, blockdir, nil) - testutil.Ok(b, err) + assert.NoError(b, err) defer func() { - testutil.Ok(b, block.Close()) + assert.NoError(b, block.Close()) }() ir, err = block.Index() - testutil.Ok(b, err) + assert.NoError(b, err) defer ir.Close() b.Run("Block", func(b *testing.B) { benchmarkPostingsForMatchers(b, ir) @@ -133,7 +134,7 @@ func benchmarkPostingsForMatchers(b *testing.B, ir IndexReader) { b.Run(c.name, func(b *testing.B) { for i := 0; i < b.N; i++ { _, err := PostingsForMatchers(ir, c.matchers...) - testutil.Ok(b, err) + assert.NoError(b, err) } }) } @@ -141,33 +142,33 @@ func benchmarkPostingsForMatchers(b *testing.B, ir IndexReader) { func BenchmarkQuerierSelect(b *testing.B) { chunkDir, err := ioutil.TempDir("", "chunk_dir") - testutil.Ok(b, err) + assert.NoError(b, err) defer func() { - testutil.Ok(b, os.RemoveAll(chunkDir)) + assert.NoError(b, os.RemoveAll(chunkDir)) }() h, err := NewHead(nil, nil, nil, 1000, chunkDir, nil, DefaultStripeSize, nil) - testutil.Ok(b, err) + assert.NoError(b, err) defer h.Close() app := h.Appender(context.Background()) numSeries := 1000000 for i := 0; i < numSeries; i++ { app.Add(labels.FromStrings("foo", "bar", "i", fmt.Sprintf("%d%s", i, postingsBenchSuffix)), int64(i), 0) } - testutil.Ok(b, app.Commit()) + assert.NoError(b, app.Commit()) bench := func(b *testing.B, br BlockReader, sorted bool) { matcher := labels.MustNewMatcher(labels.MatchEqual, "foo", "bar") for s := 1; s <= numSeries; s *= 10 { b.Run(fmt.Sprintf("%dof%d", s, numSeries), func(b *testing.B) { q, err := NewBlockQuerier(br, 0, int64(s-1)) - testutil.Ok(b, err) + assert.NoError(b, err) b.ResetTimer() for i := 0; i < b.N; i++ { ss := q.Select(sorted, nil, matcher) for ss.Next() { } - testutil.Ok(b, ss.Err()) + assert.NoError(b, ss.Err()) } q.Close() }) @@ -182,16 +183,16 @@ func BenchmarkQuerierSelect(b *testing.B) { }) tmpdir, err := ioutil.TempDir("", "test_benchquerierselect") - testutil.Ok(b, err) + assert.NoError(b, err) defer func() { - testutil.Ok(b, os.RemoveAll(tmpdir)) + assert.NoError(b, os.RemoveAll(tmpdir)) }() blockdir := createBlockFromHead(b, tmpdir, h) block, err := OpenBlock(nil, blockdir, nil) - testutil.Ok(b, err) + assert.NoError(b, err) defer func() { - testutil.Ok(b, block.Close()) + assert.NoError(b, block.Close()) }() b.Run("Block", func(b *testing.B) { diff --git a/tsdb/querier_test.go b/tsdb/querier_test.go index a028b6b8e1..71d8ec317d 100644 --- a/tsdb/querier_test.go +++ b/tsdb/querier_test.go @@ -27,6 +27,8 @@ import ( "time" "github.com/pkg/errors" + "github.com/stretchr/testify/assert" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/storage" "github.com/prometheus/prometheus/tsdb/chunkenc" @@ -34,7 +36,6 @@ import ( "github.com/prometheus/prometheus/tsdb/index" "github.com/prometheus/prometheus/tsdb/tombstones" "github.com/prometheus/prometheus/tsdb/tsdbutil" - "github.com/prometheus/prometheus/util/testutil" ) // TODO(bwplotka): Replace those mocks with remote.concreteSeriesSet. @@ -138,7 +139,7 @@ func createIdxChkReaders(t *testing.T, tc []seriesSamples) (IndexReader, ChunkRe chunkRef++ } ls := labels.FromMap(s.lset) - testutil.Ok(t, mi.AddSeries(uint64(i), ls, metas...)) + assert.NoError(t, mi.AddSeries(uint64(i), ls, metas...)) postings.Add(uint64(i), ls) @@ -152,7 +153,7 @@ func createIdxChkReaders(t *testing.T, tc []seriesSamples) (IndexReader, ChunkRe } } - testutil.Ok(t, postings.Iter(func(l labels.Label, p index.Postings) error { + assert.NoError(t, postings.Iter(func(l labels.Label, p index.Postings) error { return mi.WritePostings(l.Name, l.Value, p) })) return mi, chkReader, blockMint, blockMaxt @@ -179,27 +180,27 @@ func testBlockQuerier(t *testing.T, c blockQuerierTestCase, ir IndexReader, cr C } res := q.Select(false, nil, c.ms...) - defer func() { testutil.Ok(t, q.Close()) }() + defer func() { assert.NoError(t, q.Close()) }() for { eok, rok := c.exp.Next(), res.Next() - testutil.Equals(t, eok, rok) + assert.Equal(t, eok, rok) if !eok { - testutil.Equals(t, 0, len(res.Warnings())) + assert.Equal(t, 0, len(res.Warnings())) break } sexp := c.exp.At() sres := res.At() - testutil.Equals(t, sexp.Labels(), sres.Labels()) + assert.Equal(t, sexp.Labels(), sres.Labels()) smplExp, errExp := storage.ExpandSamples(sexp.Iterator(), nil) smplRes, errRes := storage.ExpandSamples(sres.Iterator(), nil) - testutil.Equals(t, errExp, errRes) - testutil.Equals(t, smplExp, smplRes) + assert.Equal(t, errExp, errRes) + assert.Equal(t, smplExp, smplRes) } - testutil.Ok(t, res.Err()) + assert.NoError(t, res.Err()) }) t.Run("chunk", func(t *testing.T) { @@ -214,29 +215,29 @@ func testBlockQuerier(t *testing.T, c blockQuerierTestCase, ir IndexReader, cr C }, } res := q.Select(false, nil, c.ms...) - defer func() { testutil.Ok(t, q.Close()) }() + defer func() { assert.NoError(t, q.Close()) }() for { eok, rok := c.expChks.Next(), res.Next() - testutil.Equals(t, eok, rok) + assert.Equal(t, eok, rok) if !eok { - testutil.Equals(t, 0, len(res.Warnings())) + assert.Equal(t, 0, len(res.Warnings())) break } sexpChks := c.expChks.At() sres := res.At() - testutil.Equals(t, sexpChks.Labels(), sres.Labels()) + assert.Equal(t, sexpChks.Labels(), sres.Labels()) chksExp, errExp := storage.ExpandChunks(sexpChks.Iterator()) rmChunkRefs(chksExp) chksRes, errRes := storage.ExpandChunks(sres.Iterator()) rmChunkRefs(chksRes) - testutil.Equals(t, errExp, errRes) - testutil.Equals(t, chksExp, chksRes) + assert.Equal(t, errExp, errRes) + assert.Equal(t, chksExp, chksRes) } - testutil.Ok(t, res.Err()) + assert.NoError(t, res.Err()) }) } @@ -407,7 +408,7 @@ func TestBlockQuerier_AgainstHeadWithOpenChunks(t *testing.T) { } { t.Run("", func(t *testing.T) { h, err := NewHead(nil, nil, nil, 2*time.Hour.Milliseconds(), "", nil, DefaultStripeSize, nil) - testutil.Ok(t, err) + assert.NoError(t, err) defer h.Close() app := h.Appender(context.Background()) @@ -415,19 +416,19 @@ func TestBlockQuerier_AgainstHeadWithOpenChunks(t *testing.T) { for _, chk := range s.chunks { for _, sample := range chk { _, err = app.Add(labels.FromMap(s.lset), sample.t, sample.v) - testutil.Ok(t, err) + assert.NoError(t, err) } } } - testutil.Ok(t, app.Commit()) + assert.NoError(t, app.Commit()) hr := NewRangeHead(h, c.mint, c.maxt) ir, err := hr.Index() - testutil.Ok(t, err) + assert.NoError(t, err) defer ir.Close() cr, err := hr.Chunks() - testutil.Ok(t, err) + assert.NoError(t, err) defer cr.Close() testBlockQuerier(t, c, ir, cr, tombstones.NewMemTombstones()) @@ -810,8 +811,8 @@ func TestPopulateWithTombSeriesIterators(t *testing.T) { var r []tsdbutil.Sample if tc.seek != 0 { - testutil.Equals(t, tc.seekSuccess, it.Seek(tc.seek)) - testutil.Equals(t, tc.seekSuccess, it.Seek(tc.seek)) // Next one should be noop. + assert.Equal(t, tc.seekSuccess, it.Seek(tc.seek)) + assert.Equal(t, tc.seekSuccess, it.Seek(tc.seek)) // Next one should be noop. if tc.seekSuccess { // After successful seek iterator is ready. Grab the value. @@ -820,9 +821,9 @@ func TestPopulateWithTombSeriesIterators(t *testing.T) { } } expandedResult, err := storage.ExpandSamples(it, newSample) - testutil.Ok(t, err) + assert.NoError(t, err) r = append(r, expandedResult...) - testutil.Equals(t, tc.expected, r) + assert.Equal(t, tc.expected, r) }) t.Run("chunk", func(t *testing.T) { f, chkMetas := createFakeReaderAndNotPopulatedChunks(tc.chks...) @@ -833,12 +834,12 @@ func TestPopulateWithTombSeriesIterators(t *testing.T) { return } expandedResult, err := storage.ExpandChunks(it) - testutil.Ok(t, err) + assert.NoError(t, err) // We don't care about ref IDs for comparison, only chunk's samples matters. rmChunkRefs(expandedResult) rmChunkRefs(tc.expectedChks) - testutil.Equals(t, tc.expectedChks, expandedResult) + assert.Equal(t, tc.expectedChks, expandedResult) }) }) } @@ -859,12 +860,12 @@ func TestPopulateWithDelSeriesIterator_DoubleSeek(t *testing.T) { ) it := newPopulateWithDelGenericSeriesIterator(f, chkMetas, nil).toSeriesIterator() - testutil.Assert(t, it.Seek(1), "") - testutil.Assert(t, it.Seek(2), "") - testutil.Assert(t, it.Seek(2), "") + assert.True(t, it.Seek(1), "") + assert.True(t, it.Seek(2), "") + assert.True(t, it.Seek(2), "") ts, v := it.At() - testutil.Equals(t, int64(2), ts) - testutil.Equals(t, float64(2), v) + assert.Equal(t, int64(2), ts) + assert.Equal(t, float64(2), v) } // Regression when seeked chunks were still found via binary search and we always @@ -877,15 +878,15 @@ func TestPopulateWithDelSeriesIterator_SeekInCurrentChunk(t *testing.T) { ) it := newPopulateWithDelGenericSeriesIterator(f, chkMetas, nil).toSeriesIterator() - testutil.Assert(t, it.Next(), "") + assert.True(t, it.Next(), "") ts, v := it.At() - testutil.Equals(t, int64(1), ts) - testutil.Equals(t, float64(2), v) + assert.Equal(t, int64(1), ts) + assert.Equal(t, float64(2), v) - testutil.Assert(t, it.Seek(4), "") + assert.True(t, it.Seek(4), "") ts, v = it.At() - testutil.Equals(t, int64(5), ts) - testutil.Equals(t, float64(6), v) + assert.Equal(t, int64(5), ts) + assert.Equal(t, float64(6), v) } func TestPopulateWithDelSeriesIterator_SeekWithMinTime(t *testing.T) { @@ -894,8 +895,8 @@ func TestPopulateWithDelSeriesIterator_SeekWithMinTime(t *testing.T) { ) it := newPopulateWithDelGenericSeriesIterator(f, chkMetas, nil).toSeriesIterator() - testutil.Equals(t, false, it.Seek(7)) - testutil.Equals(t, true, it.Seek(3)) + assert.Equal(t, false, it.Seek(7)) + assert.Equal(t, true, it.Seek(3)) } // Regression when calling Next() with a time bounded to fit within two samples. @@ -908,7 +909,7 @@ func TestPopulateWithDelSeriesIterator_NextWithMinTime(t *testing.T) { it := newPopulateWithDelGenericSeriesIterator( f, chkMetas, tombstones.Intervals{{Mint: math.MinInt64, Maxt: 2}}.Add(tombstones.Interval{Mint: 4, Maxt: math.MaxInt64}), ).toSeriesIterator() - testutil.Equals(t, false, it.Next()) + assert.Equal(t, false, it.Next()) } // Test the cost of merging series sets for different number of merged sets and their size. @@ -928,7 +929,7 @@ func BenchmarkMergedSeriesSet(b *testing.B) { for _, j := range []int{1, 2, 4, 8, 16, 32} { b.Run(fmt.Sprintf("series=%d,blocks=%d", k, j), func(b *testing.B) { lbls, err := labels.ReadLabels(filepath.Join("testdata", "20kseries.json"), k) - testutil.Ok(b, err) + assert.NoError(b, err) sort.Sort(labels.Slice(lbls)) @@ -954,8 +955,8 @@ func BenchmarkMergedSeriesSet(b *testing.B) { for ms.Next() { i++ } - testutil.Ok(b, ms.Err()) - testutil.Equals(b, len(lbls), i) + assert.NoError(b, ms.Err()) + assert.Equal(b, len(lbls), i) } }) } @@ -980,7 +981,7 @@ func (cr mockChunkReader) Close() error { func TestDeletedIterator(t *testing.T) { chk := chunkenc.NewXORChunk() app, err := chk.Appender() - testutil.Ok(t, err) + assert.NoError(t, err) // Insert random stuff from (0, 1000). act := make([]sample, 1000) for i := 0; i < 1000; i++ { @@ -1017,11 +1018,11 @@ func TestDeletedIterator(t *testing.T) { } } - testutil.Assert(t, i < 1000, "") + assert.True(t, i < 1000, "") ts, v := it.At() - testutil.Equals(t, act[i].t, ts) - testutil.Equals(t, act[i].v, v) + assert.Equal(t, act[i].t, ts) + assert.Equal(t, act[i].v, v) } // There has been an extra call to Next(). i++ @@ -1032,15 +1033,15 @@ func TestDeletedIterator(t *testing.T) { } } - testutil.Assert(t, i >= 1000, "") - testutil.Ok(t, it.Err()) + assert.True(t, i >= 1000, "") + assert.NoError(t, it.Err()) } } func TestDeletedIterator_WithSeek(t *testing.T) { chk := chunkenc.NewXORChunk() app, err := chk.Appender() - testutil.Ok(t, err) + assert.NoError(t, err) // Insert random stuff from (0, 1000). act := make([]sample, 1000) for i := 0; i < 1000; i++ { @@ -1070,10 +1071,10 @@ func TestDeletedIterator_WithSeek(t *testing.T) { for _, c := range cases { it := &deletedIterator{it: chk.Iterator(nil), intervals: c.r[:]} - testutil.Equals(t, c.ok, it.Seek(c.seek)) + assert.Equal(t, c.ok, it.Seek(c.seek)) if c.ok { ts, _ := it.At() - testutil.Equals(t, c.seekedTs, ts) + assert.Equal(t, c.seekedTs, ts) } } } @@ -1227,9 +1228,9 @@ func BenchmarkQueryIterator(b *testing.B) { b.Run(benchMsg, func(b *testing.B) { dir, err := ioutil.TempDir("", "bench_query_iterator") - testutil.Ok(b, err) + assert.NoError(b, err) defer func() { - testutil.Ok(b, os.RemoveAll(dir)) + assert.NoError(b, os.RemoveAll(dir)) }() var ( @@ -1251,7 +1252,7 @@ func BenchmarkQueryIterator(b *testing.B) { generatedSeries = populateSeries(prefilledLabels, mint, maxt) } block, err := OpenBlock(nil, createBlock(b, dir, generatedSeries), nil) - testutil.Ok(b, err) + assert.NoError(b, err) blocks = append(blocks, block) defer block.Close() } @@ -1259,7 +1260,7 @@ func BenchmarkQueryIterator(b *testing.B) { qblocks := make([]storage.Querier, 0, len(blocks)) for _, blk := range blocks { q, err := NewBlockQuerier(blk, math.MinInt64, math.MaxInt64) - testutil.Ok(b, err) + assert.NoError(b, err) qblocks = append(qblocks, q) } @@ -1294,9 +1295,9 @@ func BenchmarkQuerySeek(b *testing.B) { b.Run(benchMsg, func(b *testing.B) { dir, err := ioutil.TempDir("", "bench_query_iterator") - testutil.Ok(b, err) + assert.NoError(b, err) defer func() { - testutil.Ok(b, os.RemoveAll(dir)) + assert.NoError(b, os.RemoveAll(dir)) }() var ( @@ -1318,7 +1319,7 @@ func BenchmarkQuerySeek(b *testing.B) { generatedSeries = populateSeries(prefilledLabels, mint, maxt) } block, err := OpenBlock(nil, createBlock(b, dir, generatedSeries), nil) - testutil.Ok(b, err) + assert.NoError(b, err) blocks = append(blocks, block) defer block.Close() } @@ -1326,7 +1327,7 @@ func BenchmarkQuerySeek(b *testing.B) { qblocks := make([]storage.Querier, 0, len(blocks)) for _, blk := range blocks { q, err := NewBlockQuerier(blk, math.MinInt64, math.MaxInt64) - testutil.Ok(b, err) + assert.NoError(b, err) qblocks = append(qblocks, q) } @@ -1345,11 +1346,11 @@ func BenchmarkQuerySeek(b *testing.B) { for t := mint; t <= maxt; t++ { it.Seek(t) } - testutil.Ok(b, it.Err()) + assert.NoError(b, it.Err()) } - testutil.Ok(b, ss.Err()) - testutil.Ok(b, err) - testutil.Equals(b, 0, len(ss.Warnings())) + assert.NoError(b, ss.Err()) + assert.NoError(b, err) + assert.Equal(b, 0, len(ss.Warnings())) }) } } @@ -1435,9 +1436,9 @@ func BenchmarkSetMatcher(b *testing.B) { for _, c := range cases { dir, err := ioutil.TempDir("", "bench_postings_for_matchers") - testutil.Ok(b, err) + assert.NoError(b, err) defer func() { - testutil.Ok(b, os.RemoveAll(dir)) + assert.NoError(b, os.RemoveAll(dir)) }() var ( @@ -1457,7 +1458,7 @@ func BenchmarkSetMatcher(b *testing.B) { generatedSeries = populateSeries(prefilledLabels, mint, maxt) } block, err := OpenBlock(nil, createBlock(b, dir, generatedSeries), nil) - testutil.Ok(b, err) + assert.NoError(b, err) blocks = append(blocks, block) defer block.Close() } @@ -1465,7 +1466,7 @@ func BenchmarkSetMatcher(b *testing.B) { qblocks := make([]storage.Querier, 0, len(blocks)) for _, blk := range blocks { q, err := NewBlockQuerier(blk, math.MinInt64, math.MaxInt64) - testutil.Ok(b, err) + assert.NoError(b, err) qblocks = append(qblocks, q) } @@ -1480,8 +1481,8 @@ func BenchmarkSetMatcher(b *testing.B) { ss := sq.Select(false, nil, labels.MustNewMatcher(labels.MatchRegexp, "test", c.pattern)) for ss.Next() { } - testutil.Ok(b, ss.Err()) - testutil.Equals(b, 0, len(ss.Warnings())) + assert.NoError(b, ss.Err()) + assert.Equal(b, 0, len(ss.Warnings())) } }) } @@ -1545,14 +1546,14 @@ func TestFindSetMatches(t *testing.T) { func TestPostingsForMatchers(t *testing.T) { chunkDir, err := ioutil.TempDir("", "chunk_dir") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(chunkDir)) + assert.NoError(t, os.RemoveAll(chunkDir)) }() h, err := NewHead(nil, nil, nil, 1000, chunkDir, nil, DefaultStripeSize, nil) - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, h.Close()) + assert.NoError(t, h.Close()) }() app := h.Appender(context.Background()) @@ -1561,7 +1562,7 @@ func TestPostingsForMatchers(t *testing.T) { app.Add(labels.FromStrings("n", "1", "i", "b"), 0, 0) app.Add(labels.FromStrings("n", "2"), 0, 0) app.Add(labels.FromStrings("n", "2.5"), 0, 0) - testutil.Ok(t, app.Commit()) + assert.NoError(t, app.Commit()) cases := []struct { matchers []*labels.Matcher @@ -1774,7 +1775,7 @@ func TestPostingsForMatchers(t *testing.T) { } ir, err := h.Index() - testutil.Ok(t, err) + assert.NoError(t, err) for _, c := range cases { exp := map[string]struct{}{} @@ -1782,18 +1783,18 @@ func TestPostingsForMatchers(t *testing.T) { exp[l.String()] = struct{}{} } p, err := PostingsForMatchers(ir, c.matchers...) - testutil.Ok(t, err) + assert.NoError(t, err) for p.Next() { lbls := labels.Labels{} - testutil.Ok(t, ir.Series(p.At(), &lbls, &[]chunks.Meta{})) + assert.NoError(t, ir.Series(p.At(), &lbls, &[]chunks.Meta{})) if _, ok := exp[lbls.String()]; !ok { t.Errorf("Evaluating %v, unexpected result %s", c.matchers, lbls.String()) } else { delete(exp, lbls.String()) } } - testutil.Ok(t, p.Err()) + assert.NoError(t, p.Err()) if len(exp) != 0 { t.Errorf("Evaluating %v, missing results %+v", c.matchers, exp) } @@ -1808,7 +1809,7 @@ func TestClose(t *testing.T) { t.Fatalf("Opening test dir failed: %s", err) } defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() createBlock(t, dir, genSeries(1, 1, 0, 10)) @@ -1819,13 +1820,13 @@ func TestClose(t *testing.T) { t.Fatalf("Opening test storage failed: %s", err) } defer func() { - testutil.Ok(t, db.Close()) + assert.NoError(t, db.Close()) }() q, err := db.Querier(context.TODO(), 0, 20) - testutil.Ok(t, err) - testutil.Ok(t, q.Close()) - testutil.NotOk(t, q.Close()) + assert.NoError(t, err) + assert.NoError(t, q.Close()) + assert.Error(t, q.Close()) } func BenchmarkQueries(b *testing.B) { @@ -1871,9 +1872,9 @@ func BenchmarkQueries(b *testing.B) { for _, nSeries := range []int{10} { for _, nSamples := range []int64{1000, 10000, 100000} { dir, err := ioutil.TempDir("", "test_persisted_query") - testutil.Ok(b, err) + assert.NoError(b, err) defer func() { - testutil.Ok(b, os.RemoveAll(dir)) + assert.NoError(b, os.RemoveAll(dir)) }() series := genSeries(nSeries, 5, 1, nSamples) @@ -1902,9 +1903,9 @@ func BenchmarkQueries(b *testing.B) { qs := make([]storage.Querier, 0, 10) for x := 0; x <= 10; x++ { block, err := OpenBlock(nil, createBlock(b, dir, series), nil) - testutil.Ok(b, err) + assert.NoError(b, err) q, err := NewBlockQuerier(block, 1, int64(nSamples)) - testutil.Ok(b, err) + assert.NoError(b, err) qs = append(qs, q) } @@ -1913,23 +1914,23 @@ func BenchmarkQueries(b *testing.B) { queryTypes["_10-Blocks"] = storage.NewMergeQuerier(qs, nil, storage.ChainedSeriesMerge) chunkDir, err := ioutil.TempDir("", "chunk_dir") - testutil.Ok(b, err) + assert.NoError(b, err) defer func() { - testutil.Ok(b, os.RemoveAll(chunkDir)) + assert.NoError(b, os.RemoveAll(chunkDir)) }() head := createHead(b, nil, series, chunkDir) qHead, err := NewBlockQuerier(head, 1, nSamples) - testutil.Ok(b, err) + assert.NoError(b, err) queryTypes["_Head"] = qHead for qtype, querier := range queryTypes { b.Run(title+qtype+"_nSeries:"+strconv.Itoa(nSeries)+"_nSamples:"+strconv.Itoa(int(nSamples)), func(b *testing.B) { expExpansions, err := strconv.Atoi(string(title[len(title)-1])) - testutil.Ok(b, err) + assert.NoError(b, err) benchQuery(b, expExpansions, querier, selectors) }) } - testutil.Ok(b, head.Close()) + assert.NoError(b, head.Close()) } } } @@ -1949,10 +1950,10 @@ func benchQuery(b *testing.B, expExpansions int, q storage.Querier, selectors la } actualExpansions++ } - testutil.Ok(b, ss.Err()) - testutil.Equals(b, 0, len(ss.Warnings())) - testutil.Equals(b, expExpansions, actualExpansions) - testutil.Ok(b, ss.Err()) + assert.NoError(b, ss.Err()) + assert.Equal(b, 0, len(ss.Warnings())) + assert.Equal(b, expExpansions, actualExpansions) + assert.NoError(b, ss.Err()) } } @@ -2017,9 +2018,9 @@ func TestPostingsForMatcher(t *testing.T) { ir := &mockMatcherIndex{} _, err := postingsForMatcher(ir, tc.matcher) if tc.hasError { - testutil.NotOk(t, err) + assert.Error(t, err) } else { - testutil.Ok(t, err) + assert.NoError(t, err) } } } @@ -2095,7 +2096,7 @@ func TestBlockBaseSeriesSet(t *testing.T) { for _, tc := range cases { mi := newMockIndex() for _, s := range tc.series { - testutil.Ok(t, mi.AddSeries(s.ref, s.lset, s.chunks...)) + assert.NoError(t, mi.AddSeries(s.ref, s.lset, s.chunks...)) } bcs := &blockBaseSeriesSet{ @@ -2109,12 +2110,12 @@ func TestBlockBaseSeriesSet(t *testing.T) { chks := bcs.currIterFn().chks idx := tc.expIdxs[i] - testutil.Equals(t, tc.series[idx].lset, bcs.currLabels) - testutil.Equals(t, tc.series[idx].chunks, chks) + assert.Equal(t, tc.series[idx].lset, bcs.currLabels) + assert.Equal(t, tc.series[idx].chunks, chks) i++ } - testutil.Equals(t, len(tc.expIdxs), i) - testutil.Ok(t, bcs.Err()) + assert.Equal(t, len(tc.expIdxs), i) + assert.NoError(t, bcs.Err()) } } diff --git a/tsdb/record/record.go b/tsdb/record/record.go index e7df28766c..408882e832 100644 --- a/tsdb/record/record.go +++ b/tsdb/record/record.go @@ -19,6 +19,7 @@ import ( "sort" "github.com/pkg/errors" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/tsdb/encoding" "github.com/prometheus/prometheus/tsdb/tombstones" diff --git a/tsdb/record/record_test.go b/tsdb/record/record_test.go index 7d56ae208e..abfd1e0185 100644 --- a/tsdb/record/record_test.go +++ b/tsdb/record/record_test.go @@ -18,10 +18,11 @@ import ( "testing" "github.com/pkg/errors" + "github.com/stretchr/testify/assert" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/tsdb/encoding" "github.com/prometheus/prometheus/tsdb/tombstones" - "github.com/prometheus/prometheus/util/testutil" ) func TestRecord_EncodeDecode(t *testing.T) { @@ -41,8 +42,8 @@ func TestRecord_EncodeDecode(t *testing.T) { }, } decSeries, err := dec.Series(enc.Series(series, nil), nil) - testutil.Ok(t, err) - testutil.Equals(t, series, decSeries) + assert.NoError(t, err) + assert.Equal(t, series, decSeries) samples := []RefSample{ {Ref: 0, T: 12423423, V: 1.2345}, @@ -50,8 +51,8 @@ func TestRecord_EncodeDecode(t *testing.T) { {Ref: 2, T: 0, V: 99999}, } decSamples, err := dec.Samples(enc.Samples(samples, nil), nil) - testutil.Ok(t, err) - testutil.Equals(t, samples, decSamples) + assert.NoError(t, err) + assert.Equal(t, samples, decSamples) // Intervals get split up into single entries. So we don't get back exactly // what we put in. @@ -66,8 +67,8 @@ func TestRecord_EncodeDecode(t *testing.T) { }}, } decTstones, err := dec.Tombstones(enc.Tombstones(tstones, nil), nil) - testutil.Ok(t, err) - testutil.Equals(t, []tombstones.Stone{ + assert.NoError(t, err) + assert.Equal(t, []tombstones.Stone{ {Ref: 123, Intervals: tombstones.Intervals{{Mint: -1000, Maxt: 1231231}}}, {Ref: 123, Intervals: tombstones.Intervals{{Mint: 5000, Maxt: 0}}}, {Ref: 13, Intervals: tombstones.Intervals{{Mint: -1000, Maxt: -11}}}, @@ -91,7 +92,7 @@ func TestRecord_Corrupted(t *testing.T) { corrupted := enc.Series(series, nil)[:8] _, err := dec.Series(corrupted, nil) - testutil.Equals(t, err, encoding.ErrInvalidSize) + assert.Equal(t, err, encoding.ErrInvalidSize) }) t.Run("Test corrupted sample record", func(t *testing.T) { @@ -101,7 +102,7 @@ func TestRecord_Corrupted(t *testing.T) { corrupted := enc.Samples(samples, nil)[:8] _, err := dec.Samples(corrupted, nil) - testutil.Equals(t, errors.Cause(err), encoding.ErrInvalidSize) + assert.Equal(t, errors.Cause(err), encoding.ErrInvalidSize) }) t.Run("Test corrupted tombstone record", func(t *testing.T) { @@ -114,7 +115,7 @@ func TestRecord_Corrupted(t *testing.T) { corrupted := enc.Tombstones(tstones, nil)[:8] _, err := dec.Tombstones(corrupted, nil) - testutil.Equals(t, err, encoding.ErrInvalidSize) + assert.Equal(t, err, encoding.ErrInvalidSize) }) } @@ -124,19 +125,19 @@ func TestRecord_Type(t *testing.T) { series := []RefSeries{{Ref: 100, Labels: labels.FromStrings("abc", "123")}} recordType := dec.Type(enc.Series(series, nil)) - testutil.Equals(t, Series, recordType) + assert.Equal(t, Series, recordType) samples := []RefSample{{Ref: 123, T: 12345, V: 1.2345}} recordType = dec.Type(enc.Samples(samples, nil)) - testutil.Equals(t, Samples, recordType) + assert.Equal(t, Samples, recordType) tstones := []tombstones.Stone{{Ref: 1, Intervals: tombstones.Intervals{{Mint: 1, Maxt: 2}}}} recordType = dec.Type(enc.Tombstones(tstones, nil)) - testutil.Equals(t, Tombstones, recordType) + assert.Equal(t, Tombstones, recordType) recordType = dec.Type(nil) - testutil.Equals(t, Unknown, recordType) + assert.Equal(t, Unknown, recordType) recordType = dec.Type([]byte{0}) - testutil.Equals(t, Unknown, recordType) + assert.Equal(t, Unknown, recordType) } diff --git a/tsdb/repair.go b/tsdb/repair.go index 02114cd4f6..49f55d5a8a 100644 --- a/tsdb/repair.go +++ b/tsdb/repair.go @@ -23,6 +23,7 @@ import ( "github.com/go-kit/kit/log" "github.com/go-kit/kit/log/level" "github.com/pkg/errors" + tsdb_errors "github.com/prometheus/prometheus/tsdb/errors" "github.com/prometheus/prometheus/tsdb/fileutil" ) diff --git a/tsdb/repair_test.go b/tsdb/repair_test.go index d789d2a5b5..d73fe5007e 100644 --- a/tsdb/repair_test.go +++ b/tsdb/repair_test.go @@ -19,18 +19,19 @@ import ( "path/filepath" "testing" + "github.com/stretchr/testify/assert" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/tsdb/chunks" "github.com/prometheus/prometheus/tsdb/fileutil" "github.com/prometheus/prometheus/tsdb/index" - "github.com/prometheus/prometheus/util/testutil" ) func TestRepairBadIndexVersion(t *testing.T) { tmpDir, err := ioutil.TempDir("", "test") - testutil.Ok(t, err) + assert.NoError(t, err) t.Cleanup(func() { - testutil.Ok(t, os.RemoveAll(tmpDir)) + assert.NoError(t, os.RemoveAll(tmpDir)) }) // The broken index used in this test was written by the following script @@ -69,40 +70,40 @@ func TestRepairBadIndexVersion(t *testing.T) { tmpDbDir := filepath.Join(tmpDir, "01BZJ9WJQPWHGNC2W4J9TA62KC") // Create a copy DB to run test against. - testutil.Ok(t, fileutil.CopyDirs(filepath.Join("testdata", "repair_index_version", "01BZJ9WJQPWHGNC2W4J9TA62KC"), tmpDbDir)) + assert.NoError(t, fileutil.CopyDirs(filepath.Join("testdata", "repair_index_version", "01BZJ9WJQPWHGNC2W4J9TA62KC"), tmpDbDir)) // Check the current db. // In its current state, lookups should fail with the fixed code. _, _, err = readMetaFile(tmpDbDir) - testutil.NotOk(t, err) + assert.Error(t, err) // Touch chunks dir in block to imitate them. - testutil.Ok(t, os.MkdirAll(filepath.Join(tmpDbDir, "chunks"), 0777)) + assert.NoError(t, os.MkdirAll(filepath.Join(tmpDbDir, "chunks"), 0777)) // Read current index to check integrity. r, err := index.NewFileReader(filepath.Join(tmpDbDir, indexFilename)) - testutil.Ok(t, err) + assert.NoError(t, err) p, err := r.Postings("b", "1") - testutil.Ok(t, err) + assert.NoError(t, err) for p.Next() { t.Logf("next ID %d", p.At()) var lset labels.Labels - testutil.NotOk(t, r.Series(p.At(), &lset, nil)) + assert.Error(t, r.Series(p.At(), &lset, nil)) } - testutil.Ok(t, p.Err()) - testutil.Ok(t, r.Close()) + assert.NoError(t, p.Err()) + assert.NoError(t, r.Close()) // On DB opening all blocks in the base dir should be repaired. db, err := Open(tmpDir, nil, nil, nil) - testutil.Ok(t, err) + assert.NoError(t, err) db.Close() r, err = index.NewFileReader(filepath.Join(tmpDbDir, indexFilename)) - testutil.Ok(t, err) + assert.NoError(t, err) defer r.Close() p, err = r.Postings("b", "1") - testutil.Ok(t, err) + assert.NoError(t, err) res := []labels.Labels{} for p.Next() { @@ -110,17 +111,17 @@ func TestRepairBadIndexVersion(t *testing.T) { var lset labels.Labels var chks []chunks.Meta - testutil.Ok(t, r.Series(p.At(), &lset, &chks)) + assert.NoError(t, r.Series(p.At(), &lset, &chks)) res = append(res, lset) } - testutil.Ok(t, p.Err()) - testutil.Equals(t, []labels.Labels{ + assert.NoError(t, p.Err()) + assert.Equal(t, []labels.Labels{ {{Name: "a", Value: "1"}, {Name: "b", Value: "1"}}, {{Name: "a", Value: "2"}, {Name: "b", Value: "1"}}, }, res) meta, _, err := readMetaFile(tmpDbDir) - testutil.Ok(t, err) - testutil.Assert(t, meta.Version == metaVersion1, "unexpected meta version %d", meta.Version) + assert.NoError(t, err) + assert.True(t, meta.Version == metaVersion1, "unexpected meta version %d", meta.Version) } diff --git a/tsdb/tombstones/tombstones.go b/tsdb/tombstones/tombstones.go index d34a75af4d..79b68adfbb 100644 --- a/tsdb/tombstones/tombstones.go +++ b/tsdb/tombstones/tombstones.go @@ -27,6 +27,7 @@ import ( "github.com/go-kit/kit/log" "github.com/go-kit/kit/log/level" "github.com/pkg/errors" + "github.com/prometheus/prometheus/tsdb/encoding" tsdb_errors "github.com/prometheus/prometheus/tsdb/errors" "github.com/prometheus/prometheus/tsdb/fileutil" diff --git a/tsdb/tombstones/tombstones_test.go b/tsdb/tombstones/tombstones_test.go index 49d743a88c..0336ff2a14 100644 --- a/tsdb/tombstones/tombstones_test.go +++ b/tsdb/tombstones/tombstones_test.go @@ -23,7 +23,7 @@ import ( "time" "github.com/go-kit/kit/log" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" "go.uber.org/goleak" ) @@ -34,7 +34,7 @@ func TestMain(m *testing.M) { func TestWriteAndReadbackTombstones(t *testing.T) { tmpdir, _ := ioutil.TempDir("", "test") defer func() { - testutil.Ok(t, os.RemoveAll(tmpdir)) + assert.NoError(t, os.RemoveAll(tmpdir)) }() ref := uint64(0) @@ -54,13 +54,13 @@ func TestWriteAndReadbackTombstones(t *testing.T) { } _, err := WriteFile(log.NewNopLogger(), tmpdir, stones) - testutil.Ok(t, err) + assert.NoError(t, err) restr, _, err := ReadTombstones(tmpdir) - testutil.Ok(t, err) + assert.NoError(t, err) // Compare the two readers. - testutil.Equals(t, stones, restr) + assert.Equal(t, stones, restr) } func TestAddingNewIntervals(t *testing.T) { @@ -157,7 +157,7 @@ func TestAddingNewIntervals(t *testing.T) { for _, c := range cases { t.Run("", func(t *testing.T) { - testutil.Equals(t, c.exp, c.exist.Add(c.new)) + assert.Equal(t, c.exp, c.exist.Add(c.new)) }) } } @@ -178,7 +178,7 @@ func TestMemTombstonesConcurrency(t *testing.T) { go func() { for x := 0; x < totalRuns; x++ { _, err := tomb.Get(uint64(x)) - testutil.Ok(t, err) + assert.NoError(t, err) } wg.Done() }() diff --git a/tsdb/tsdbblockutil.go b/tsdb/tsdbblockutil.go index 15f129d318..be2c63f9f4 100644 --- a/tsdb/tsdbblockutil.go +++ b/tsdb/tsdbblockutil.go @@ -19,6 +19,7 @@ import ( "path/filepath" "github.com/go-kit/kit/log" + "github.com/prometheus/prometheus/storage" ) diff --git a/tsdb/tsdbutil/buffer_test.go b/tsdb/tsdbutil/buffer_test.go index b7954ea4e5..3c916e0126 100644 --- a/tsdb/tsdbutil/buffer_test.go +++ b/tsdb/tsdbutil/buffer_test.go @@ -18,7 +18,7 @@ import ( "sort" "testing" - "github.com/prometheus/prometheus/util/testutil" + "github.com/stretchr/testify/assert" ) func TestSampleRing(t *testing.T) { @@ -92,12 +92,12 @@ func TestBufferedSeriesIterator(t *testing.T) { t, v := bit.At() b = append(b, sample{t: t, v: v}) } - testutil.Equals(t, exp, b) + assert.Equal(t, exp, b) } sampleEq := func(ets int64, ev float64) { ts, v := it.At() - testutil.Equals(t, ets, ts) - testutil.Equals(t, ev, v) + assert.Equal(t, ets, ts) + assert.Equal(t, ev, v) } it = NewBuffer(newListSeriesIterator([]sample{ @@ -111,29 +111,29 @@ func TestBufferedSeriesIterator(t *testing.T) { {t: 101, v: 10}, }), 2) - testutil.Assert(t, it.Seek(-123) == true, "seek failed") + assert.True(t, it.Seek(-123) == true, "seek failed") sampleEq(1, 2) bufferEq(nil) - testutil.Assert(t, it.Next() == true, "next failed") + assert.True(t, it.Next() == true, "next failed") sampleEq(2, 3) bufferEq([]sample{{t: 1, v: 2}}) - testutil.Assert(t, it.Next() == true, "next failed") - testutil.Assert(t, it.Next() == true, "next failed") - testutil.Assert(t, it.Next() == true, "next failed") + assert.True(t, it.Next() == true, "next failed") + assert.True(t, it.Next() == true, "next failed") + assert.True(t, it.Next() == true, "next failed") sampleEq(5, 6) bufferEq([]sample{{t: 2, v: 3}, {t: 3, v: 4}, {t: 4, v: 5}}) - testutil.Assert(t, it.Seek(5) == true, "seek failed") + assert.True(t, it.Seek(5) == true, "seek failed") sampleEq(5, 6) bufferEq([]sample{{t: 2, v: 3}, {t: 3, v: 4}, {t: 4, v: 5}}) - testutil.Assert(t, it.Seek(101) == true, "seek failed") + assert.True(t, it.Seek(101) == true, "seek failed") sampleEq(101, 10) bufferEq([]sample{{t: 99, v: 8}, {t: 100, v: 9}}) - testutil.Assert(t, it.Next() == false, "next succeeded unexpectedly") + assert.True(t, it.Next() == false, "next succeeded unexpectedly") } type listSeriesIterator struct { diff --git a/tsdb/wal.go b/tsdb/wal.go index 1e503a1481..7856ac14c6 100644 --- a/tsdb/wal.go +++ b/tsdb/wal.go @@ -31,6 +31,7 @@ import ( "github.com/go-kit/kit/log/level" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/tsdb/encoding" "github.com/prometheus/prometheus/tsdb/fileutil" diff --git a/tsdb/wal/checkpoint.go b/tsdb/wal/checkpoint.go index 33e2e58ced..c27f6f5b9f 100644 --- a/tsdb/wal/checkpoint.go +++ b/tsdb/wal/checkpoint.go @@ -28,6 +28,7 @@ import ( "github.com/go-kit/kit/log" "github.com/go-kit/kit/log/level" "github.com/pkg/errors" + tsdb_errors "github.com/prometheus/prometheus/tsdb/errors" "github.com/prometheus/prometheus/tsdb/fileutil" "github.com/prometheus/prometheus/tsdb/record" diff --git a/tsdb/wal/checkpoint_test.go b/tsdb/wal/checkpoint_test.go index d32713bd9f..21c47a1023 100644 --- a/tsdb/wal/checkpoint_test.go +++ b/tsdb/wal/checkpoint_test.go @@ -16,142 +16,143 @@ package wal import ( "fmt" - "github.com/go-kit/kit/log" "io/ioutil" "os" "path/filepath" "strings" "testing" + "github.com/go-kit/kit/log" "github.com/pkg/errors" + "github.com/stretchr/testify/assert" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/tsdb/record" - "github.com/prometheus/prometheus/util/testutil" ) func TestLastCheckpoint(t *testing.T) { dir, err := ioutil.TempDir("", "test_checkpoint") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() _, _, err = LastCheckpoint(dir) - testutil.Equals(t, record.ErrNotFound, err) + assert.Equal(t, record.ErrNotFound, err) - testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.0000"), 0777)) + assert.NoError(t, os.MkdirAll(filepath.Join(dir, "checkpoint.0000"), 0777)) s, k, err := LastCheckpoint(dir) - testutil.Ok(t, err) - testutil.Equals(t, filepath.Join(dir, "checkpoint.0000"), s) - testutil.Equals(t, 0, k) + assert.NoError(t, err) + assert.Equal(t, filepath.Join(dir, "checkpoint.0000"), s) + assert.Equal(t, 0, k) - testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.xyz"), 0777)) + assert.NoError(t, os.MkdirAll(filepath.Join(dir, "checkpoint.xyz"), 0777)) s, k, err = LastCheckpoint(dir) - testutil.Ok(t, err) - testutil.Equals(t, filepath.Join(dir, "checkpoint.0000"), s) - testutil.Equals(t, 0, k) + assert.NoError(t, err) + assert.Equal(t, filepath.Join(dir, "checkpoint.0000"), s) + assert.Equal(t, 0, k) - testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.1"), 0777)) + assert.NoError(t, os.MkdirAll(filepath.Join(dir, "checkpoint.1"), 0777)) s, k, err = LastCheckpoint(dir) - testutil.Ok(t, err) - testutil.Equals(t, filepath.Join(dir, "checkpoint.1"), s) - testutil.Equals(t, 1, k) + assert.NoError(t, err) + assert.Equal(t, filepath.Join(dir, "checkpoint.1"), s) + assert.Equal(t, 1, k) - testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.1000"), 0777)) + assert.NoError(t, os.MkdirAll(filepath.Join(dir, "checkpoint.1000"), 0777)) s, k, err = LastCheckpoint(dir) - testutil.Ok(t, err) - testutil.Equals(t, filepath.Join(dir, "checkpoint.1000"), s) - testutil.Equals(t, 1000, k) + assert.NoError(t, err) + assert.Equal(t, filepath.Join(dir, "checkpoint.1000"), s) + assert.Equal(t, 1000, k) - testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.99999999"), 0777)) + assert.NoError(t, os.MkdirAll(filepath.Join(dir, "checkpoint.99999999"), 0777)) s, k, err = LastCheckpoint(dir) - testutil.Ok(t, err) - testutil.Equals(t, filepath.Join(dir, "checkpoint.99999999"), s) - testutil.Equals(t, 99999999, k) + assert.NoError(t, err) + assert.Equal(t, filepath.Join(dir, "checkpoint.99999999"), s) + assert.Equal(t, 99999999, k) - testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.100000000"), 0777)) + assert.NoError(t, os.MkdirAll(filepath.Join(dir, "checkpoint.100000000"), 0777)) s, k, err = LastCheckpoint(dir) - testutil.Ok(t, err) - testutil.Equals(t, filepath.Join(dir, "checkpoint.100000000"), s) - testutil.Equals(t, 100000000, k) + assert.NoError(t, err) + assert.Equal(t, filepath.Join(dir, "checkpoint.100000000"), s) + assert.Equal(t, 100000000, k) } func TestDeleteCheckpoints(t *testing.T) { dir, err := ioutil.TempDir("", "test_checkpoint") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() - testutil.Ok(t, DeleteCheckpoints(dir, 0)) + assert.NoError(t, DeleteCheckpoints(dir, 0)) - testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.00"), 0777)) - testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.01"), 0777)) - testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.02"), 0777)) - testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.03"), 0777)) + assert.NoError(t, os.MkdirAll(filepath.Join(dir, "checkpoint.00"), 0777)) + assert.NoError(t, os.MkdirAll(filepath.Join(dir, "checkpoint.01"), 0777)) + assert.NoError(t, os.MkdirAll(filepath.Join(dir, "checkpoint.02"), 0777)) + assert.NoError(t, os.MkdirAll(filepath.Join(dir, "checkpoint.03"), 0777)) - testutil.Ok(t, DeleteCheckpoints(dir, 2)) + assert.NoError(t, DeleteCheckpoints(dir, 2)) files, err := ioutil.ReadDir(dir) - testutil.Ok(t, err) + assert.NoError(t, err) fns := []string{} for _, f := range files { fns = append(fns, f.Name()) } - testutil.Equals(t, []string{"checkpoint.02", "checkpoint.03"}, fns) + assert.Equal(t, []string{"checkpoint.02", "checkpoint.03"}, fns) - testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.99999999"), 0777)) - testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.100000000"), 0777)) - testutil.Ok(t, os.MkdirAll(filepath.Join(dir, "checkpoint.100000001"), 0777)) + assert.NoError(t, os.MkdirAll(filepath.Join(dir, "checkpoint.99999999"), 0777)) + assert.NoError(t, os.MkdirAll(filepath.Join(dir, "checkpoint.100000000"), 0777)) + assert.NoError(t, os.MkdirAll(filepath.Join(dir, "checkpoint.100000001"), 0777)) - testutil.Ok(t, DeleteCheckpoints(dir, 100000000)) + assert.NoError(t, DeleteCheckpoints(dir, 100000000)) files, err = ioutil.ReadDir(dir) - testutil.Ok(t, err) + assert.NoError(t, err) fns = []string{} for _, f := range files { fns = append(fns, f.Name()) } - testutil.Equals(t, []string{"checkpoint.100000000", "checkpoint.100000001"}, fns) + assert.Equal(t, []string{"checkpoint.100000000", "checkpoint.100000001"}, fns) } func TestCheckpoint(t *testing.T) { for _, compress := range []bool{false, true} { t.Run(fmt.Sprintf("compress=%t", compress), func(t *testing.T) { dir, err := ioutil.TempDir("", "test_checkpoint") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() var enc record.Encoder // Create a dummy segment to bump the initial number. seg, err := CreateSegment(dir, 100) - testutil.Ok(t, err) - testutil.Ok(t, seg.Close()) + assert.NoError(t, err) + assert.NoError(t, seg.Close()) // Manually create checkpoint for 99 and earlier. w, err := New(nil, nil, filepath.Join(dir, "checkpoint.0099"), compress) - testutil.Ok(t, err) + assert.NoError(t, err) // Add some data we expect to be around later. err = w.Log(enc.Series([]record.RefSeries{ {Ref: 0, Labels: labels.FromStrings("a", "b", "c", "0")}, {Ref: 1, Labels: labels.FromStrings("a", "b", "c", "1")}, }, nil)) - testutil.Ok(t, err) + assert.NoError(t, err) // Log an unknown record, that might have come from a future Prometheus version. - testutil.Ok(t, w.Log([]byte{255})) - testutil.Ok(t, w.Close()) + assert.NoError(t, w.Log([]byte{255})) + assert.NoError(t, w.Close()) // Start a WAL and write records to it as usual. w, err = NewSize(nil, nil, dir, 64*1024, compress) - testutil.Ok(t, err) + assert.NoError(t, err) var last int64 for i := 0; ; i++ { _, n, err := Segments(w.Dir()) - testutil.Ok(t, err) + assert.NoError(t, err) if n >= 106 { break } @@ -163,7 +164,7 @@ func TestCheckpoint(t *testing.T) { {Ref: 4, Labels: labels.FromStrings("a", "b", "c", "4")}, {Ref: 5, Labels: labels.FromStrings("a", "b", "c", "5")}, }, nil) - testutil.Ok(t, w.Log(b)) + assert.NoError(t, w.Log(b)) } // Write samples until the WAL has enough segments. // Make them have drifting timestamps within a record to see that they @@ -174,27 +175,27 @@ func TestCheckpoint(t *testing.T) { {Ref: 2, T: last + 20000, V: float64(i)}, {Ref: 3, T: last + 30000, V: float64(i)}, }, nil) - testutil.Ok(t, w.Log(b)) + assert.NoError(t, w.Log(b)) last += 100 } - testutil.Ok(t, w.Close()) + assert.NoError(t, w.Close()) _, err = Checkpoint(log.NewNopLogger(), w, 100, 106, func(x uint64) bool { return x%2 == 0 }, last/2) - testutil.Ok(t, err) - testutil.Ok(t, w.Truncate(107)) - testutil.Ok(t, DeleteCheckpoints(w.Dir(), 106)) + assert.NoError(t, err) + assert.NoError(t, w.Truncate(107)) + assert.NoError(t, DeleteCheckpoints(w.Dir(), 106)) // Only the new checkpoint should be left. files, err := ioutil.ReadDir(dir) - testutil.Ok(t, err) - testutil.Equals(t, 1, len(files)) - testutil.Equals(t, "checkpoint.00000106", files[0].Name()) + assert.NoError(t, err) + assert.Equal(t, 1, len(files)) + assert.Equal(t, "checkpoint.00000106", files[0].Name()) sr, err := NewSegmentsReader(filepath.Join(dir, "checkpoint.00000106")) - testutil.Ok(t, err) + assert.NoError(t, err) defer sr.Close() var dec record.Decoder @@ -207,17 +208,17 @@ func TestCheckpoint(t *testing.T) { switch dec.Type(rec) { case record.Series: series, err = dec.Series(rec, series) - testutil.Ok(t, err) + assert.NoError(t, err) case record.Samples: samples, err := dec.Samples(rec, nil) - testutil.Ok(t, err) + assert.NoError(t, err) for _, s := range samples { - testutil.Assert(t, s.T >= last/2, "sample with wrong timestamp") + assert.True(t, s.T >= last/2, "sample with wrong timestamp") } } } - testutil.Ok(t, r.Err()) - testutil.Equals(t, []record.RefSeries{ + assert.NoError(t, r.Err()) + assert.Equal(t, []record.RefSeries{ {Ref: 0, Labels: labels.FromStrings("a", "b", "c", "0")}, {Ref: 2, Labels: labels.FromStrings("a", "b", "c", "2")}, {Ref: 4, Labels: labels.FromStrings("a", "b", "c", "4")}, @@ -229,27 +230,27 @@ func TestCheckpoint(t *testing.T) { func TestCheckpointNoTmpFolderAfterError(t *testing.T) { // Create a new wal with invalid data. dir, err := ioutil.TempDir("", "test_checkpoint") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() w, err := NewSize(nil, nil, dir, 64*1024, false) - testutil.Ok(t, err) + assert.NoError(t, err) var enc record.Encoder - testutil.Ok(t, w.Log(enc.Series([]record.RefSeries{ + assert.NoError(t, w.Log(enc.Series([]record.RefSeries{ {Ref: 0, Labels: labels.FromStrings("a", "b", "c", "2")}}, nil))) - testutil.Ok(t, w.Close()) + assert.NoError(t, w.Close()) // Corrupt data. f, err := os.OpenFile(filepath.Join(w.Dir(), "00000000"), os.O_WRONLY, 0666) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = f.WriteAt([]byte{42}, 1) - testutil.Ok(t, err) - testutil.Ok(t, f.Close()) + assert.NoError(t, err) + assert.NoError(t, f.Close()) // Run the checkpoint and since the wal contains corrupt data this should return an error. _, err = Checkpoint(log.NewNopLogger(), w, 0, 1, nil, 0) - testutil.NotOk(t, err) + assert.Error(t, err) // Walk the wal dir to make sure there are no tmp folder left behind after the error. err = filepath.Walk(w.Dir(), func(path string, info os.FileInfo, err error) error { @@ -261,5 +262,5 @@ func TestCheckpointNoTmpFolderAfterError(t *testing.T) { } return nil }) - testutil.Ok(t, err) + assert.NoError(t, err) } diff --git a/tsdb/wal/reader_test.go b/tsdb/wal/reader_test.go index bb9b9739f4..3f8d4eae74 100644 --- a/tsdb/wal/reader_test.go +++ b/tsdb/wal/reader_test.go @@ -30,6 +30,8 @@ import ( "time" "github.com/go-kit/kit/log" + "github.com/stretchr/testify/assert" + tsdb_errors "github.com/prometheus/prometheus/tsdb/errors" "github.com/prometheus/prometheus/util/testutil" ) @@ -181,7 +183,7 @@ func TestReader(t *testing.T) { if j >= len(c.exp) { t.Fatal("received more records than expected") } - testutil.Equals(t, c.exp[j], rec, "Bytes within record did not match expected Bytes") + assert.Equal(t, c.exp[j], rec, "Bytes within record did not match expected Bytes") } if !c.fail && r.Err() != nil { t.Fatalf("unexpected error: %s", r.Err()) @@ -200,14 +202,14 @@ func TestReader_Live(t *testing.T) { for i := range testReaderCases { t.Run(strconv.Itoa(i), func(t *testing.T) { writeFd, err := ioutil.TempFile("", "TestReader_Live") - testutil.Ok(t, err) + assert.NoError(t, err) defer os.Remove(writeFd.Name()) go func(i int) { for _, rec := range testReaderCases[i].t { rec := encodedRecord(rec.t, rec.b) _, err := writeFd.Write(rec) - testutil.Ok(t, err) + assert.NoError(t, err) runtime.Gosched() } writeFd.Close() @@ -215,21 +217,21 @@ func TestReader_Live(t *testing.T) { // Read from a second FD on the same file. readFd, err := os.Open(writeFd.Name()) - testutil.Ok(t, err) + assert.NoError(t, err) reader := NewLiveReader(logger, NewLiveReaderMetrics(nil), readFd) for _, exp := range testReaderCases[i].exp { for !reader.Next() { - testutil.Assert(t, reader.Err() == io.EOF, "expect EOF, got: %v", reader.Err()) + assert.True(t, reader.Err() == io.EOF, "expect EOF, got: %v", reader.Err()) runtime.Gosched() } actual := reader.Record() - testutil.Equals(t, exp, actual, "read wrong record") + assert.Equal(t, exp, actual, "read wrong record") } - testutil.Assert(t, !reader.Next(), "unexpected record") + assert.True(t, !reader.Next(), "unexpected record") if testReaderCases[i].fail { - testutil.Assert(t, reader.Err() != nil, "expected error") + assert.Error(t, reader.Err()) } }) } @@ -313,33 +315,33 @@ func TestReaderFuzz(t *testing.T) { for _, compress := range []bool{false, true} { t.Run(fmt.Sprintf("%s,compress=%t", name, compress), func(t *testing.T) { dir, err := ioutil.TempDir("", "wal_fuzz_live") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() w, err := NewSize(nil, nil, dir, 128*pageSize, compress) - testutil.Ok(t, err) + assert.NoError(t, err) // Buffering required as we're not reading concurrently. input := make(chan []byte, fuzzLen) err = generateRandomEntries(w, input) - testutil.Ok(t, err) + assert.NoError(t, err) close(input) err = w.Close() - testutil.Ok(t, err) + assert.NoError(t, err) sr, err := allSegments(w.Dir()) - testutil.Ok(t, err) + assert.NoError(t, err) defer sr.Close() reader := fn(sr) for expected := range input { - testutil.Assert(t, reader.Next(), "expected record: %v", reader.Err()) - testutil.Equals(t, expected, reader.Record(), "read wrong record") + assert.True(t, reader.Next(), "expected record: %v", reader.Err()) + assert.Equal(t, expected, reader.Record(), "read wrong record") } - testutil.Assert(t, !reader.Next(), "unexpected record") + assert.True(t, !reader.Next(), "unexpected record") }) } } @@ -350,13 +352,13 @@ func TestReaderFuzz_Live(t *testing.T) { for _, compress := range []bool{false, true} { t.Run(fmt.Sprintf("compress=%t", compress), func(t *testing.T) { dir, err := ioutil.TempDir("", "wal_fuzz_live") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() w, err := NewSize(nil, nil, dir, 128*pageSize, compress) - testutil.Ok(t, err) + assert.NoError(t, err) defer w.Close() // In the background, generate a stream of random records and write them @@ -365,17 +367,17 @@ func TestReaderFuzz_Live(t *testing.T) { done := make(chan struct{}) go func() { err := generateRandomEntries(w, input) - testutil.Ok(t, err) + assert.NoError(t, err) time.Sleep(100 * time.Millisecond) close(done) }() // Tail the WAL and compare the results. m, _, err := Segments(w.Dir()) - testutil.Ok(t, err) + assert.NoError(t, err) seg, err := OpenReadSegment(SegmentName(dir, m)) - testutil.Ok(t, err) + assert.NoError(t, err) defer seg.Close() r := NewLiveReader(logger, nil, seg) @@ -386,10 +388,10 @@ func TestReaderFuzz_Live(t *testing.T) { for r.Next() { rec := r.Record() expected, ok := <-input - testutil.Assert(t, ok, "unexpected record") - testutil.Equals(t, expected, rec, "record does not match expected") + assert.True(t, ok, "unexpected record") + assert.Equal(t, expected, rec, "record does not match expected") } - testutil.Assert(t, r.Err() == io.EOF, "expected EOF, got: %v", r.Err()) + assert.True(t, r.Err() == io.EOF, "expected EOF, got: %v", r.Err()) return true } @@ -399,7 +401,7 @@ func TestReaderFuzz_Live(t *testing.T) { case <-segmentTicker.C: // check if new segments exist _, last, err := Segments(w.Dir()) - testutil.Ok(t, err) + assert.NoError(t, err) if last <= seg.i { continue } @@ -408,11 +410,11 @@ func TestReaderFuzz_Live(t *testing.T) { readSegment(r) fi, err := os.Stat(SegmentName(dir, seg.i)) - testutil.Ok(t, err) - testutil.Assert(t, r.Offset() == fi.Size(), "expected to have read whole segment, but read %d of %d", r.Offset(), fi.Size()) + assert.NoError(t, err) + assert.True(t, r.Offset() == fi.Size(), "expected to have read whole segment, but read %d of %d", r.Offset(), fi.Size()) seg, err = OpenReadSegment(SegmentName(dir, seg.i+1)) - testutil.Ok(t, err) + assert.NoError(t, err) defer seg.Close() r = NewLiveReader(logger, nil, seg) @@ -425,7 +427,7 @@ func TestReaderFuzz_Live(t *testing.T) { } } - testutil.Assert(t, r.Err() == io.EOF, "expected EOF") + assert.True(t, r.Err() == io.EOF, "expected EOF") }) } } @@ -435,92 +437,92 @@ func TestLiveReaderCorrupt_ShortFile(t *testing.T) { // but the segment is only half written. logger := testutil.NewLogger(t) dir, err := ioutil.TempDir("", "wal_live_corrupt") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() w, err := NewSize(nil, nil, dir, pageSize, false) - testutil.Ok(t, err) + assert.NoError(t, err) rec := make([]byte, pageSize-recordHeaderSize) _, err = rand.Read(rec) - testutil.Ok(t, err) + assert.NoError(t, err) err = w.Log(rec) - testutil.Ok(t, err) + assert.NoError(t, err) err = w.Close() - testutil.Ok(t, err) + assert.NoError(t, err) segmentFile, err := os.OpenFile(filepath.Join(dir, "00000000"), os.O_RDWR, 0666) - testutil.Ok(t, err) + assert.NoError(t, err) err = segmentFile.Truncate(pageSize / 2) - testutil.Ok(t, err) + assert.NoError(t, err) err = segmentFile.Close() - testutil.Ok(t, err) + assert.NoError(t, err) // Try and LiveReader it. m, _, err := Segments(w.Dir()) - testutil.Ok(t, err) + assert.NoError(t, err) seg, err := OpenReadSegment(SegmentName(dir, m)) - testutil.Ok(t, err) + assert.NoError(t, err) defer seg.Close() r := NewLiveReader(logger, nil, seg) - testutil.Assert(t, r.Next() == false, "expected no records") - testutil.Assert(t, r.Err() == io.EOF, "expected error, got: %v", r.Err()) + assert.True(t, r.Next() == false, "expected no records") + assert.True(t, r.Err() == io.EOF, "expected error, got: %v", r.Err()) } func TestLiveReaderCorrupt_RecordTooLongAndShort(t *testing.T) { // Write a corrupt WAL segment, when record len > page size. logger := testutil.NewLogger(t) dir, err := ioutil.TempDir("", "wal_live_corrupt") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() w, err := NewSize(nil, nil, dir, pageSize*2, false) - testutil.Ok(t, err) + assert.NoError(t, err) rec := make([]byte, pageSize-recordHeaderSize) _, err = rand.Read(rec) - testutil.Ok(t, err) + assert.NoError(t, err) err = w.Log(rec) - testutil.Ok(t, err) + assert.NoError(t, err) err = w.Close() - testutil.Ok(t, err) + assert.NoError(t, err) segmentFile, err := os.OpenFile(filepath.Join(dir, "00000000"), os.O_RDWR, 0666) - testutil.Ok(t, err) + assert.NoError(t, err) // Override the record length buf := make([]byte, 3) buf[0] = byte(recFull) binary.BigEndian.PutUint16(buf[1:], 0xFFFF) _, err = segmentFile.WriteAt(buf, 0) - testutil.Ok(t, err) + assert.NoError(t, err) err = segmentFile.Close() - testutil.Ok(t, err) + assert.NoError(t, err) // Try and LiveReader it. m, _, err := Segments(w.Dir()) - testutil.Ok(t, err) + assert.NoError(t, err) seg, err := OpenReadSegment(SegmentName(dir, m)) - testutil.Ok(t, err) + assert.NoError(t, err) defer seg.Close() r := NewLiveReader(logger, NewLiveReaderMetrics(nil), seg) - testutil.Assert(t, r.Next() == false, "expected no records") - testutil.Assert(t, r.Err().Error() == "record length greater than a single page: 65542 > 32768", "expected error, got: %v", r.Err()) + assert.True(t, r.Next() == false, "expected no records") + assert.True(t, r.Err().Error() == "record length greater than a single page: 65542 > 32768", "expected error, got: %v", r.Err()) } func TestReaderData(t *testing.T) { @@ -532,18 +534,18 @@ func TestReaderData(t *testing.T) { for name, fn := range readerConstructors { t.Run(name, func(t *testing.T) { w, err := New(nil, nil, dir, true) - testutil.Ok(t, err) + assert.NoError(t, err) sr, err := allSegments(dir) - testutil.Ok(t, err) + assert.NoError(t, err) reader := fn(sr) for reader.Next() { } - testutil.Ok(t, reader.Err()) + assert.NoError(t, reader.Err()) err = w.Repair(reader.Err()) - testutil.Ok(t, err) + assert.NoError(t, err) }) } } diff --git a/tsdb/wal/wal.go b/tsdb/wal/wal.go index c5401ddf1e..36df9ad693 100644 --- a/tsdb/wal/wal.go +++ b/tsdb/wal/wal.go @@ -33,6 +33,7 @@ import ( "github.com/golang/snappy" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/prometheus/tsdb/fileutil" ) diff --git a/tsdb/wal/wal_test.go b/tsdb/wal/wal_test.go index e38a4d8060..c6d6408120 100644 --- a/tsdb/wal/wal_test.go +++ b/tsdb/wal/wal_test.go @@ -23,10 +23,11 @@ import ( "path/filepath" "testing" - "github.com/prometheus/prometheus/tsdb/fileutil" + client_testutil "github.com/prometheus/client_golang/prometheus/testutil" + "github.com/stretchr/testify/assert" "go.uber.org/goleak" - client_testutil "github.com/prometheus/client_golang/prometheus/testutil" + "github.com/prometheus/prometheus/tsdb/fileutil" "github.com/prometheus/prometheus/util/testutil" ) @@ -46,9 +47,9 @@ func TestWALRepair_ReadingError(t *testing.T) { 2, func(f *os.File) { _, err := f.Seek(pageSize*2, 0) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = f.Write([]byte{byte(recFirst)}) - testutil.Ok(t, err) + assert.NoError(t, err) }, 8, }, @@ -59,9 +60,9 @@ func TestWALRepair_ReadingError(t *testing.T) { 1, func(f *os.File) { _, err := f.Seek(pageSize, 0) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = f.Write([]byte{byte(recPageTerm)}) - testutil.Ok(t, err) + assert.NoError(t, err) }, 4, }, @@ -69,9 +70,9 @@ func TestWALRepair_ReadingError(t *testing.T) { 1, func(f *os.File) { _, err := f.Seek(pageSize, 0) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = f.Write([]byte{byte(recLast)}) - testutil.Ok(t, err) + assert.NoError(t, err) }, 4, }, @@ -79,9 +80,9 @@ func TestWALRepair_ReadingError(t *testing.T) { 1, func(f *os.File) { _, err := f.Seek(pageSize, 0) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = f.Write([]byte{123}) - testutil.Ok(t, err) + assert.NoError(t, err) }, 4, }, @@ -89,9 +90,9 @@ func TestWALRepair_ReadingError(t *testing.T) { 1, func(f *os.File) { _, err := f.Seek(pageSize+4, 0) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = f.Write([]byte{0}) - testutil.Ok(t, err) + assert.NoError(t, err) }, 4, }, @@ -99,9 +100,9 @@ func TestWALRepair_ReadingError(t *testing.T) { 1, func(f *os.File) { _, err := f.Seek(pageSize+2, 0) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = f.Write([]byte{0}) - testutil.Ok(t, err) + assert.NoError(t, err) }, 4, }, @@ -109,18 +110,18 @@ func TestWALRepair_ReadingError(t *testing.T) { 1, func(f *os.File) { _, err := f.Seek(pageSize+100, 0) - testutil.Ok(t, err) + assert.NoError(t, err) _, err = f.Write([]byte("beef")) - testutil.Ok(t, err) + assert.NoError(t, err) }, 4, }, } { t.Run(name, func(t *testing.T) { dir, err := ioutil.TempDir("", "wal_repair") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() // We create 3 segments with 3 records each and @@ -128,7 +129,7 @@ func TestWALRepair_ReadingError(t *testing.T) { // As a result we want a repaired WAL with given intact records. segSize := 3 * pageSize w, err := NewSize(nil, nil, dir, segSize, false) - testutil.Ok(t, err) + assert.NoError(t, err) var records [][]byte @@ -136,36 +137,36 @@ func TestWALRepair_ReadingError(t *testing.T) { b := make([]byte, pageSize-recordHeaderSize) b[0] = byte(i) records = append(records, b) - testutil.Ok(t, w.Log(b)) + assert.NoError(t, w.Log(b)) } first, last, err := Segments(w.Dir()) - testutil.Ok(t, err) - testutil.Equals(t, 3, 1+last-first, "wal creation didn't result in expected number of segments") + assert.NoError(t, err) + assert.Equal(t, 3, 1+last-first, "wal creation didn't result in expected number of segments") - testutil.Ok(t, w.Close()) + assert.NoError(t, w.Close()) f, err := os.OpenFile(SegmentName(dir, test.corrSgm), os.O_RDWR, 0666) - testutil.Ok(t, err) + assert.NoError(t, err) // Apply corruption function. test.corrFunc(f) - testutil.Ok(t, f.Close()) + assert.NoError(t, f.Close()) w, err = NewSize(nil, nil, dir, segSize, false) - testutil.Ok(t, err) + assert.NoError(t, err) defer w.Close() first, last, err = Segments(w.Dir()) - testutil.Ok(t, err) + assert.NoError(t, err) // Backfill segments from the most recent checkpoint onwards. for i := first; i <= last; i++ { s, err := OpenReadSegment(SegmentName(w.Dir(), i)) - testutil.Ok(t, err) + assert.NoError(t, err) sr := NewSegmentBufReader(s) - testutil.Ok(t, err) + assert.NoError(t, err) r := NewReader(sr) for r.Next() { } @@ -177,12 +178,12 @@ func TestWALRepair_ReadingError(t *testing.T) { if r.Err() == nil { continue } - testutil.Ok(t, w.Repair(r.Err())) + assert.NoError(t, w.Repair(r.Err())) break } sr, err := NewSegmentsReader(dir) - testutil.Ok(t, err) + assert.NoError(t, err) defer sr.Close() r := NewReader(sr) @@ -191,8 +192,8 @@ func TestWALRepair_ReadingError(t *testing.T) { var b []byte result = append(result, append(b, r.Record()...)) } - testutil.Ok(t, r.Err()) - testutil.Equals(t, test.intactRecs, len(result), "Wrong number of intact records") + assert.NoError(t, r.Err()) + assert.Equal(t, test.intactRecs, len(result), "Wrong number of intact records") for i, r := range result { if !bytes.Equal(records[i], r) { @@ -202,11 +203,11 @@ func TestWALRepair_ReadingError(t *testing.T) { // Make sure there is a new 0 size Segment after the corrupted Segment. _, last, err = Segments(w.Dir()) - testutil.Ok(t, err) - testutil.Equals(t, test.corrSgm+1, last) + assert.NoError(t, err) + assert.Equal(t, test.corrSgm+1, last) fi, err := os.Stat(SegmentName(dir, last)) - testutil.Ok(t, err) - testutil.Equals(t, int64(0), fi.Size()) + assert.NoError(t, err) + assert.Equal(t, int64(0), fi.Size()) }) } } @@ -216,9 +217,9 @@ func TestWALRepair_ReadingError(t *testing.T) { // moving to write more records to the WAL. func TestCorruptAndCarryOn(t *testing.T) { dir, err := ioutil.TempDir("", "wal_repair") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() var ( @@ -231,37 +232,37 @@ func TestCorruptAndCarryOn(t *testing.T) { // so when we truncate the file we're guaranteed to split a record. { w, err := NewSize(logger, nil, dir, segmentSize, false) - testutil.Ok(t, err) + assert.NoError(t, err) for i := 0; i < 18; i++ { buf := make([]byte, recordSize) _, err := rand.Read(buf) - testutil.Ok(t, err) + assert.NoError(t, err) err = w.Log(buf) - testutil.Ok(t, err) + assert.NoError(t, err) } err = w.Close() - testutil.Ok(t, err) + assert.NoError(t, err) } // Check all the segments are the correct size. { segments, err := listSegments(dir) - testutil.Ok(t, err) + assert.NoError(t, err) for _, segment := range segments { f, err := os.OpenFile(filepath.Join(dir, fmt.Sprintf("%08d", segment.index)), os.O_RDONLY, 0666) - testutil.Ok(t, err) + assert.NoError(t, err) fi, err := f.Stat() - testutil.Ok(t, err) + assert.NoError(t, err) t.Log("segment", segment.index, "size", fi.Size()) - testutil.Equals(t, int64(segmentSize), fi.Size()) + assert.Equal(t, int64(segmentSize), fi.Size()) err = f.Close() - testutil.Ok(t, err) + assert.NoError(t, err) } } @@ -269,74 +270,74 @@ func TestCorruptAndCarryOn(t *testing.T) { // page in half, leaving 4 valid records. { f, err := os.OpenFile(filepath.Join(dir, fmt.Sprintf("%08d", 0)), os.O_RDWR, 0666) - testutil.Ok(t, err) + assert.NoError(t, err) fi, err := f.Stat() - testutil.Ok(t, err) - testutil.Equals(t, int64(segmentSize), fi.Size()) + assert.NoError(t, err) + assert.Equal(t, int64(segmentSize), fi.Size()) err = f.Truncate(int64(segmentSize / 2)) - testutil.Ok(t, err) + assert.NoError(t, err) err = f.Close() - testutil.Ok(t, err) + assert.NoError(t, err) } // Now try and repair this WAL, and write 5 more records to it. { sr, err := NewSegmentsReader(dir) - testutil.Ok(t, err) + assert.NoError(t, err) reader := NewReader(sr) i := 0 for ; i < 4 && reader.Next(); i++ { - testutil.Equals(t, recordSize, len(reader.Record())) + assert.Equal(t, recordSize, len(reader.Record())) } - testutil.Equals(t, 4, i, "not enough records") - testutil.Assert(t, !reader.Next(), "unexpected record") + assert.Equal(t, 4, i, "not enough records") + assert.True(t, !reader.Next(), "unexpected record") corruptionErr := reader.Err() - testutil.Assert(t, corruptionErr != nil, "expected error") + assert.Error(t, corruptionErr) err = sr.Close() - testutil.Ok(t, err) + assert.NoError(t, err) w, err := NewSize(logger, nil, dir, segmentSize, false) - testutil.Ok(t, err) + assert.NoError(t, err) err = w.Repair(corruptionErr) - testutil.Ok(t, err) + assert.NoError(t, err) // Ensure that we have a completely clean slate after repairing. - testutil.Equals(t, w.segment.Index(), 1) // We corrupted segment 0. - testutil.Equals(t, w.donePages, 0) + assert.Equal(t, w.segment.Index(), 1) // We corrupted segment 0. + assert.Equal(t, w.donePages, 0) for i := 0; i < 5; i++ { buf := make([]byte, recordSize) _, err := rand.Read(buf) - testutil.Ok(t, err) + assert.NoError(t, err) err = w.Log(buf) - testutil.Ok(t, err) + assert.NoError(t, err) } err = w.Close() - testutil.Ok(t, err) + assert.NoError(t, err) } // Replay the WAL. Should get 9 records. { sr, err := NewSegmentsReader(dir) - testutil.Ok(t, err) + assert.NoError(t, err) reader := NewReader(sr) i := 0 for ; i < 9 && reader.Next(); i++ { - testutil.Equals(t, recordSize, len(reader.Record())) + assert.Equal(t, recordSize, len(reader.Record())) } - testutil.Equals(t, 9, i, "wrong number of records") - testutil.Assert(t, !reader.Next(), "unexpected record") - testutil.Equals(t, nil, reader.Err()) + assert.Equal(t, 9, i, "wrong number of records") + assert.True(t, !reader.Next(), "unexpected record") + assert.Equal(t, nil, reader.Err()) sr.Close() } } @@ -344,14 +345,14 @@ func TestCorruptAndCarryOn(t *testing.T) { // TestClose ensures that calling Close more than once doesn't panic and doesn't block. func TestClose(t *testing.T) { dir, err := ioutil.TempDir("", "wal_repair") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() w, err := NewSize(nil, nil, dir, pageSize, false) - testutil.Ok(t, err) - testutil.Ok(t, w.Close()) - testutil.NotOk(t, w.Close()) + assert.NoError(t, err) + assert.NoError(t, w.Close()) + assert.Error(t, w.Close()) } func TestSegmentMetric(t *testing.T) { @@ -361,12 +362,12 @@ func TestSegmentMetric(t *testing.T) { ) dir, err := ioutil.TempDir("", "segment_metric") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() w, err := NewSize(nil, nil, dir, segmentSize, false) - testutil.Ok(t, err) + assert.NoError(t, err) initialSegment := client_testutil.ToFloat64(w.metrics.currentSegment) @@ -374,13 +375,13 @@ func TestSegmentMetric(t *testing.T) { for i := 0; i < 3; i++ { buf := make([]byte, recordSize) _, err := rand.Read(buf) - testutil.Ok(t, err) + assert.NoError(t, err) err = w.Log(buf) - testutil.Ok(t, err) + assert.NoError(t, err) } - testutil.Assert(t, client_testutil.ToFloat64(w.metrics.currentSegment) == initialSegment+1, "segment metric did not increment after segment rotation") - testutil.Ok(t, w.Close()) + assert.True(t, client_testutil.ToFloat64(w.metrics.currentSegment) == initialSegment+1, "segment metric did not increment after segment rotation") + assert.NoError(t, w.Close()) } func TestCompression(t *testing.T) { @@ -392,48 +393,48 @@ func TestCompression(t *testing.T) { ) dirPath, err := ioutil.TempDir("", fmt.Sprintf("TestCompression_%t", compressed)) - testutil.Ok(t, err) + assert.NoError(t, err) w, err := NewSize(nil, nil, dirPath, segmentSize, compressed) - testutil.Ok(t, err) + assert.NoError(t, err) buf := make([]byte, recordSize) for i := 0; i < records; i++ { - testutil.Ok(t, w.Log(buf)) + assert.NoError(t, w.Log(buf)) } - testutil.Ok(t, w.Close()) + assert.NoError(t, w.Close()) return dirPath } dirCompressed := bootstrap(true) defer func() { - testutil.Ok(t, os.RemoveAll(dirCompressed)) + assert.NoError(t, os.RemoveAll(dirCompressed)) }() dirUnCompressed := bootstrap(false) defer func() { - testutil.Ok(t, os.RemoveAll(dirUnCompressed)) + assert.NoError(t, os.RemoveAll(dirUnCompressed)) }() uncompressedSize, err := fileutil.DirSize(dirUnCompressed) - testutil.Ok(t, err) + assert.NoError(t, err) compressedSize, err := fileutil.DirSize(dirCompressed) - testutil.Ok(t, err) + assert.NoError(t, err) - testutil.Assert(t, float64(uncompressedSize)*0.75 > float64(compressedSize), "Compressing zeroes should save at least 25%% space - uncompressedSize: %d, compressedSize: %d", uncompressedSize, compressedSize) + assert.True(t, float64(uncompressedSize)*0.75 > float64(compressedSize), "Compressing zeroes should save at least 25%% space - uncompressedSize: %d, compressedSize: %d", uncompressedSize, compressedSize) } func BenchmarkWAL_LogBatched(b *testing.B) { for _, compress := range []bool{true, false} { b.Run(fmt.Sprintf("compress=%t", compress), func(b *testing.B) { dir, err := ioutil.TempDir("", "bench_logbatch") - testutil.Ok(b, err) + assert.NoError(b, err) defer func() { - testutil.Ok(b, os.RemoveAll(dir)) + assert.NoError(b, os.RemoveAll(dir)) }() w, err := New(nil, nil, dir, compress) - testutil.Ok(b, err) + assert.NoError(b, err) defer w.Close() var buf [2048]byte @@ -446,7 +447,7 @@ func BenchmarkWAL_LogBatched(b *testing.B) { continue } err := w.Log(recs...) - testutil.Ok(b, err) + assert.NoError(b, err) recs = recs[:0] } // Stop timer to not count fsync time on close. @@ -461,13 +462,13 @@ func BenchmarkWAL_Log(b *testing.B) { for _, compress := range []bool{true, false} { b.Run(fmt.Sprintf("compress=%t", compress), func(b *testing.B) { dir, err := ioutil.TempDir("", "bench_logsingle") - testutil.Ok(b, err) + assert.NoError(b, err) defer func() { - testutil.Ok(b, os.RemoveAll(dir)) + assert.NoError(b, os.RemoveAll(dir)) }() w, err := New(nil, nil, dir, compress) - testutil.Ok(b, err) + assert.NoError(b, err) defer w.Close() var buf [2048]byte @@ -475,7 +476,7 @@ func BenchmarkWAL_Log(b *testing.B) { for i := 0; i < b.N; i++ { err := w.Log(buf[:]) - testutil.Ok(b, err) + assert.NoError(b, err) } // Stop timer to not count fsync time on close. // If it's counted batched vs. single benchmarks are very similar but diff --git a/tsdb/wal/watcher.go b/tsdb/wal/watcher.go index 1fb78005f8..8670567f13 100644 --- a/tsdb/wal/watcher.go +++ b/tsdb/wal/watcher.go @@ -29,6 +29,7 @@ import ( "github.com/go-kit/kit/log/level" "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/prometheus/pkg/timestamp" "github.com/prometheus/prometheus/tsdb/record" ) diff --git a/tsdb/wal/watcher_test.go b/tsdb/wal/watcher_test.go index 6893ad72a0..4028afd1a4 100644 --- a/tsdb/wal/watcher_test.go +++ b/tsdb/wal/watcher_test.go @@ -23,11 +23,11 @@ import ( "time" "github.com/go-kit/kit/log" - "github.com/prometheus/client_golang/prometheus" + "github.com/stretchr/testify/assert" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/tsdb/record" - "github.com/prometheus/prometheus/util/testutil" ) var defaultRetryInterval = 100 * time.Millisecond @@ -100,20 +100,20 @@ func TestTailSamples(t *testing.T) { now := time.Now() dir, err := ioutil.TempDir("", "readCheckpoint") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() wdir := path.Join(dir, "wal") err = os.Mkdir(wdir, 0777) - testutil.Ok(t, err) + assert.NoError(t, err) enc := record.Encoder{} w, err := NewSize(nil, nil, wdir, 128*pageSize, compress) - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, w.Close()) + assert.NoError(t, w.Close()) }() // Write to the initial segment then checkpoint. @@ -125,7 +125,7 @@ func TestTailSamples(t *testing.T) { Labels: labels.Labels{labels.Label{Name: "__name__", Value: fmt.Sprintf("metric_%d", i)}}, }, }, nil) - testutil.Ok(t, w.Log(series)) + assert.NoError(t, w.Log(series)) for j := 0; j < samplesCount; j++ { inner := rand.Intn(ref + 1) @@ -136,13 +136,13 @@ func TestTailSamples(t *testing.T) { V: float64(i), }, }, nil) - testutil.Ok(t, w.Log(sample)) + assert.NoError(t, w.Log(sample)) } } // Start read after checkpoint, no more data written. first, last, err := Segments(w.Dir()) - testutil.Ok(t, err) + assert.NoError(t, err) wt := newWriteToMock() watcher := NewWatcher(wMetrics, nil, nil, "", wt, dir) @@ -152,7 +152,7 @@ func TestTailSamples(t *testing.T) { watcher.setMetrics() for i := first; i <= last; i++ { segment, err := OpenReadSegment(SegmentName(watcher.walDir, i)) - testutil.Ok(t, err) + assert.NoError(t, err) defer segment.Close() reader := NewLiveReader(nil, NewLiveReaderMetrics(nil), segment) @@ -165,8 +165,8 @@ func TestTailSamples(t *testing.T) { retry(t, defaultRetryInterval, defaultRetries, func() bool { return wt.checkNumLabels() >= expectedSeries }) - testutil.Equals(t, expectedSeries, wt.checkNumLabels()) - testutil.Equals(t, expectedSamples, wt.samplesAppended) + assert.Equal(t, expectedSeries, wt.checkNumLabels()) + assert.Equal(t, expectedSamples, wt.samplesAppended) }) } } @@ -179,18 +179,18 @@ func TestReadToEndNoCheckpoint(t *testing.T) { for _, compress := range []bool{false, true} { t.Run(fmt.Sprintf("compress=%t", compress), func(t *testing.T) { dir, err := ioutil.TempDir("", "readToEnd_noCheckpoint") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() wdir := path.Join(dir, "wal") err = os.Mkdir(wdir, 0777) - testutil.Ok(t, err) + assert.NoError(t, err) w, err := NewSize(nil, nil, wdir, 128*pageSize, compress) - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, w.Close()) + assert.NoError(t, w.Close()) }() var recs [][]byte @@ -218,15 +218,15 @@ func TestReadToEndNoCheckpoint(t *testing.T) { // Randomly batch up records. if rand.Intn(4) < 3 { - testutil.Ok(t, w.Log(recs...)) + assert.NoError(t, w.Log(recs...)) recs = recs[:0] } } } - testutil.Ok(t, w.Log(recs...)) + assert.NoError(t, w.Log(recs...)) _, _, err = Segments(w.Dir()) - testutil.Ok(t, err) + assert.NoError(t, err) wt := newWriteToMock() watcher := NewWatcher(wMetrics, nil, nil, "", wt, dir) @@ -237,7 +237,7 @@ func TestReadToEndNoCheckpoint(t *testing.T) { return wt.checkNumLabels() >= expected }) watcher.Stop() - testutil.Equals(t, expected, wt.checkNumLabels()) + assert.Equal(t, expected, wt.checkNumLabels()) }) } } @@ -252,20 +252,20 @@ func TestReadToEndWithCheckpoint(t *testing.T) { for _, compress := range []bool{false, true} { t.Run(fmt.Sprintf("compress=%t", compress), func(t *testing.T) { dir, err := ioutil.TempDir("", "readToEnd_withCheckpoint") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() wdir := path.Join(dir, "wal") err = os.Mkdir(wdir, 0777) - testutil.Ok(t, err) + assert.NoError(t, err) enc := record.Encoder{} w, err := NewSize(nil, nil, wdir, segmentSize, compress) - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, w.Close()) + assert.NoError(t, w.Close()) }() // Write to the initial segment then checkpoint. @@ -277,9 +277,9 @@ func TestReadToEndWithCheckpoint(t *testing.T) { Labels: labels.Labels{labels.Label{Name: "__name__", Value: fmt.Sprintf("metric_%d", i)}}, }, }, nil) - testutil.Ok(t, w.Log(series)) + assert.NoError(t, w.Log(series)) // Add in an unknown record type, which should be ignored. - testutil.Ok(t, w.Log([]byte{255})) + assert.NoError(t, w.Log([]byte{255})) for j := 0; j < samplesCount; j++ { inner := rand.Intn(ref + 1) @@ -290,7 +290,7 @@ func TestReadToEndWithCheckpoint(t *testing.T) { V: float64(i), }, }, nil) - testutil.Ok(t, w.Log(sample)) + assert.NoError(t, w.Log(sample)) } } @@ -305,7 +305,7 @@ func TestReadToEndWithCheckpoint(t *testing.T) { Labels: labels.Labels{labels.Label{Name: "__name__", Value: fmt.Sprintf("metric_%d", i)}}, }, }, nil) - testutil.Ok(t, w.Log(series)) + assert.NoError(t, w.Log(series)) for j := 0; j < samplesCount; j++ { sample := enc.Samples([]record.RefSample{ @@ -315,12 +315,12 @@ func TestReadToEndWithCheckpoint(t *testing.T) { V: float64(i), }, }, nil) - testutil.Ok(t, w.Log(sample)) + assert.NoError(t, w.Log(sample)) } } _, _, err = Segments(w.Dir()) - testutil.Ok(t, err) + assert.NoError(t, err) wt := newWriteToMock() watcher := NewWatcher(wMetrics, nil, nil, "", wt, dir) go watcher.Start() @@ -330,7 +330,7 @@ func TestReadToEndWithCheckpoint(t *testing.T) { return wt.checkNumLabels() >= expected }) watcher.Stop() - testutil.Equals(t, expected, wt.checkNumLabels()) + assert.Equal(t, expected, wt.checkNumLabels()) }) } } @@ -343,22 +343,22 @@ func TestReadCheckpoint(t *testing.T) { for _, compress := range []bool{false, true} { t.Run(fmt.Sprintf("compress=%t", compress), func(t *testing.T) { dir, err := ioutil.TempDir("", "readCheckpoint") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() wdir := path.Join(dir, "wal") err = os.Mkdir(wdir, 0777) - testutil.Ok(t, err) + assert.NoError(t, err) os.Create(SegmentName(wdir, 30)) enc := record.Encoder{} w, err := NewSize(nil, nil, wdir, 128*pageSize, compress) - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, w.Close()) + assert.NoError(t, w.Close()) }() // Write to the initial segment then checkpoint. @@ -370,7 +370,7 @@ func TestReadCheckpoint(t *testing.T) { Labels: labels.Labels{labels.Label{Name: "__name__", Value: fmt.Sprintf("metric_%d", i)}}, }, }, nil) - testutil.Ok(t, w.Log(series)) + assert.NoError(t, w.Log(series)) for j := 0; j < samplesCount; j++ { inner := rand.Intn(ref + 1) @@ -381,7 +381,7 @@ func TestReadCheckpoint(t *testing.T) { V: float64(i), }, }, nil) - testutil.Ok(t, w.Log(sample)) + assert.NoError(t, w.Log(sample)) } } Checkpoint(log.NewNopLogger(), w, 30, 31, func(x uint64) bool { return true }, 0) @@ -389,7 +389,7 @@ func TestReadCheckpoint(t *testing.T) { // Start read after checkpoint, no more data written. _, _, err = Segments(w.Dir()) - testutil.Ok(t, err) + assert.NoError(t, err) wt := newWriteToMock() watcher := NewWatcher(wMetrics, nil, nil, "", wt, dir) @@ -400,7 +400,7 @@ func TestReadCheckpoint(t *testing.T) { return wt.checkNumLabels() >= expectedSeries }) watcher.Stop() - testutil.Equals(t, expectedSeries, wt.checkNumLabels()) + assert.Equal(t, expectedSeries, wt.checkNumLabels()) }) } } @@ -415,18 +415,18 @@ func TestReadCheckpointMultipleSegments(t *testing.T) { for _, compress := range []bool{false, true} { t.Run(fmt.Sprintf("compress=%t", compress), func(t *testing.T) { dir, err := ioutil.TempDir("", "readCheckpoint") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() wdir := path.Join(dir, "wal") err = os.Mkdir(wdir, 0777) - testutil.Ok(t, err) + assert.NoError(t, err) enc := record.Encoder{} w, err := NewSize(nil, nil, wdir, pageSize, compress) - testutil.Ok(t, err) + assert.NoError(t, err) // Write a bunch of data. for i := 0; i < segments; i++ { @@ -438,7 +438,7 @@ func TestReadCheckpointMultipleSegments(t *testing.T) { Labels: labels.Labels{labels.Label{Name: "__name__", Value: fmt.Sprintf("metric_%d", j)}}, }, }, nil) - testutil.Ok(t, w.Log(series)) + assert.NoError(t, w.Log(series)) for k := 0; k < samplesCount; k++ { inner := rand.Intn(ref + 1) @@ -449,19 +449,19 @@ func TestReadCheckpointMultipleSegments(t *testing.T) { V: float64(i), }, }, nil) - testutil.Ok(t, w.Log(sample)) + assert.NoError(t, w.Log(sample)) } } } - testutil.Ok(t, w.Close()) + assert.NoError(t, w.Close()) // At this point we should have at least 6 segments, lets create a checkpoint dir of the first 5. checkpointDir := dir + "/wal/checkpoint.000004" err = os.Mkdir(checkpointDir, 0777) - testutil.Ok(t, err) + assert.NoError(t, err) for i := 0; i <= 4; i++ { err := os.Rename(SegmentName(dir+"/wal", i), SegmentName(checkpointDir, i)) - testutil.Ok(t, err) + assert.NoError(t, err) } wt := newWriteToMock() @@ -472,10 +472,10 @@ func TestReadCheckpointMultipleSegments(t *testing.T) { watcher.setMetrics() lastCheckpoint, _, err := LastCheckpoint(watcher.walDir) - testutil.Ok(t, err) + assert.NoError(t, err) err = watcher.readCheckpoint(lastCheckpoint) - testutil.Ok(t, err) + assert.NoError(t, err) }) } } @@ -497,20 +497,20 @@ func TestCheckpointSeriesReset(t *testing.T) { for _, tc := range testCases { t.Run(fmt.Sprintf("compress=%t", tc.compress), func(t *testing.T) { dir, err := ioutil.TempDir("", "seriesReset") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() wdir := path.Join(dir, "wal") err = os.Mkdir(wdir, 0777) - testutil.Ok(t, err) + assert.NoError(t, err) enc := record.Encoder{} w, err := NewSize(nil, nil, wdir, segmentSize, tc.compress) - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, w.Close()) + assert.NoError(t, w.Close()) }() // Write to the initial segment, then checkpoint later. @@ -522,7 +522,7 @@ func TestCheckpointSeriesReset(t *testing.T) { Labels: labels.Labels{labels.Label{Name: "__name__", Value: fmt.Sprintf("metric_%d", i)}}, }, }, nil) - testutil.Ok(t, w.Log(series)) + assert.NoError(t, w.Log(series)) for j := 0; j < samplesCount; j++ { inner := rand.Intn(ref + 1) @@ -533,12 +533,12 @@ func TestCheckpointSeriesReset(t *testing.T) { V: float64(i), }, }, nil) - testutil.Ok(t, w.Log(sample)) + assert.NoError(t, w.Log(sample)) } } _, _, err = Segments(w.Dir()) - testutil.Ok(t, err) + assert.NoError(t, err) wt := newWriteToMock() watcher := NewWatcher(wMetrics, nil, nil, "", wt, dir) @@ -549,24 +549,24 @@ func TestCheckpointSeriesReset(t *testing.T) { retry(t, defaultRetryInterval, defaultRetries, func() bool { return wt.checkNumLabels() >= expected }) - testutil.Equals(t, seriesCount, wt.checkNumLabels()) + assert.Equal(t, seriesCount, wt.checkNumLabels()) _, err = Checkpoint(log.NewNopLogger(), w, 2, 4, func(x uint64) bool { return true }, 0) - testutil.Ok(t, err) + assert.NoError(t, err) err = w.Truncate(5) - testutil.Ok(t, err) + assert.NoError(t, err) _, cpi, err := LastCheckpoint(path.Join(dir, "wal")) - testutil.Ok(t, err) + assert.NoError(t, err) err = watcher.garbageCollectSeries(cpi + 1) - testutil.Ok(t, err) + assert.NoError(t, err) watcher.Stop() // If you modify the checkpoint and truncate segment #'s run the test to see how // many series records you end up with and change the last Equals check accordingly // or modify the Equals to Assert(len(wt.seriesLabels) < seriesCount*10) - testutil.Equals(t, tc.segments, wt.checkNumLabels()) + assert.Equal(t, tc.segments, wt.checkNumLabels()) }) } } diff --git a/tsdb/wal_test.go b/tsdb/wal_test.go index e1e4b9a1dd..69a148c0f0 100644 --- a/tsdb/wal_test.go +++ b/tsdb/wal_test.go @@ -27,53 +27,54 @@ import ( "time" "github.com/go-kit/kit/log" + "github.com/stretchr/testify/assert" + "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/tsdb/record" "github.com/prometheus/prometheus/tsdb/tombstones" "github.com/prometheus/prometheus/tsdb/wal" - "github.com/prometheus/prometheus/util/testutil" ) func TestSegmentWAL_cut(t *testing.T) { tmpdir, err := ioutil.TempDir("", "test_wal_cut") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(tmpdir)) + assert.NoError(t, os.RemoveAll(tmpdir)) }() // This calls cut() implicitly the first time without a previous tail. w, err := OpenSegmentWAL(tmpdir, nil, 0, nil) - testutil.Ok(t, err) + assert.NoError(t, err) - testutil.Ok(t, w.write(WALEntrySeries, 1, []byte("Hello World!!"))) + assert.NoError(t, w.write(WALEntrySeries, 1, []byte("Hello World!!"))) - testutil.Ok(t, w.cut()) + assert.NoError(t, w.cut()) // Cutting creates a new file. - testutil.Equals(t, 2, len(w.files)) + assert.Equal(t, 2, len(w.files)) - testutil.Ok(t, w.write(WALEntrySeries, 1, []byte("Hello World!!"))) + assert.NoError(t, w.write(WALEntrySeries, 1, []byte("Hello World!!"))) - testutil.Ok(t, w.Close()) + assert.NoError(t, w.Close()) for _, of := range w.files { f, err := os.Open(of.Name()) - testutil.Ok(t, err) + assert.NoError(t, err) // Verify header data. metab := make([]byte, 8) _, err = f.Read(metab) - testutil.Ok(t, err) - testutil.Equals(t, WALMagic, binary.BigEndian.Uint32(metab[:4])) - testutil.Equals(t, WALFormatDefault, metab[4]) + assert.NoError(t, err) + assert.Equal(t, WALMagic, binary.BigEndian.Uint32(metab[:4])) + assert.Equal(t, WALFormatDefault, metab[4]) // We cannot actually check for correct pre-allocation as it is // optional per filesystem and handled transparently. et, flag, b, err := newWALReader(nil, nil).entry(f) - testutil.Ok(t, err) - testutil.Equals(t, WALEntrySeries, et) - testutil.Equals(t, byte(walSeriesSimple), flag) - testutil.Equals(t, []byte("Hello World!!"), b) + assert.NoError(t, err) + assert.Equal(t, WALEntrySeries, et) + assert.Equal(t, byte(walSeriesSimple), flag) + assert.Equal(t, []byte("Hello World!!"), b) } } @@ -83,17 +84,17 @@ func TestSegmentWAL_Truncate(t *testing.T) { batch = 100 ) series, err := labels.ReadLabels(filepath.Join("testdata", "20kseries.json"), numMetrics) - testutil.Ok(t, err) + assert.NoError(t, err) dir, err := ioutil.TempDir("", "test_wal_log_truncate") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() w, err := OpenSegmentWAL(dir, nil, 0, nil) - testutil.Ok(t, err) - defer func(wal *SegmentWAL) { testutil.Ok(t, wal.Close()) }(w) + assert.NoError(t, err) + defer func(wal *SegmentWAL) { assert.NoError(t, wal.Close()) }(w) w.segmentSize = 10000 for i := 0; i < numMetrics; i += batch { @@ -103,7 +104,7 @@ func TestSegmentWAL_Truncate(t *testing.T) { rs = append(rs, record.RefSeries{Labels: s, Ref: uint64(i+j) + 1}) } err := w.LogSeries(rs) - testutil.Ok(t, err) + assert.NoError(t, err) } // We mark the 2nd half of the files with a min timestamp that should discard @@ -125,7 +126,7 @@ func TestSegmentWAL_Truncate(t *testing.T) { } err = w.Truncate(1000, keepf) - testutil.Ok(t, err) + assert.NoError(t, err) var expected []record.RefSeries @@ -138,22 +139,22 @@ func TestSegmentWAL_Truncate(t *testing.T) { // Call Truncate once again to see whether we can read the written file without // creating a new WAL. err = w.Truncate(1000, keepf) - testutil.Ok(t, err) - testutil.Ok(t, w.Close()) + assert.NoError(t, err) + assert.NoError(t, w.Close()) // The same again with a new WAL. w, err = OpenSegmentWAL(dir, nil, 0, nil) - testutil.Ok(t, err) - defer func(wal *SegmentWAL) { testutil.Ok(t, wal.Close()) }(w) + assert.NoError(t, err) + defer func(wal *SegmentWAL) { assert.NoError(t, wal.Close()) }(w) var readSeries []record.RefSeries r := w.Reader() - testutil.Ok(t, r.Read(func(s []record.RefSeries) { + assert.NoError(t, r.Read(func(s []record.RefSeries) { readSeries = append(readSeries, s...) }, nil, nil)) - testutil.Equals(t, expected, readSeries) + assert.Equal(t, expected, readSeries) } // Symmetrical test of reading and writing to the WAL via its main interface. @@ -166,12 +167,12 @@ func TestSegmentWAL_Log_Restore(t *testing.T) { // Generate testing data. It does not make semantic sense but // for the purpose of this test. series, err := labels.ReadLabels(filepath.Join("testdata", "20kseries.json"), numMetrics) - testutil.Ok(t, err) + assert.NoError(t, err) dir, err := ioutil.TempDir("", "test_wal_log_restore") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() var ( @@ -185,7 +186,7 @@ func TestSegmentWAL_Log_Restore(t *testing.T) { // write more data to it, close it. for k := 0; k < numMetrics; k += numMetrics / iterations { w, err := OpenSegmentWAL(dir, nil, 0, nil) - testutil.Ok(t, err) + assert.NoError(t, err) // Set smaller segment size so we can actually write several files. w.segmentSize = 1000 * 1000 @@ -221,11 +222,11 @@ func TestSegmentWAL_Log_Restore(t *testing.T) { } } - testutil.Ok(t, r.Read(serf, smplf, delf)) + assert.NoError(t, r.Read(serf, smplf, delf)) - testutil.Equals(t, recordedSamples, resultSamples) - testutil.Equals(t, recordedSeries, resultSeries) - testutil.Equals(t, recordedDeletes, resultDeletes) + assert.Equal(t, recordedSamples, resultSamples) + assert.Equal(t, recordedSeries, resultSeries) + assert.Equal(t, recordedDeletes, resultDeletes) series := series[k : k+(numMetrics/iterations)] @@ -256,9 +257,9 @@ func TestSegmentWAL_Log_Restore(t *testing.T) { }) } - testutil.Ok(t, w.LogSeries(series)) - testutil.Ok(t, w.LogSamples(samples)) - testutil.Ok(t, w.LogDeletes(stones)) + assert.NoError(t, w.LogSeries(series)) + assert.NoError(t, w.LogSamples(samples)) + assert.NoError(t, w.LogDeletes(stones)) if len(lbls) > 0 { recordedSeries = append(recordedSeries, series) @@ -272,47 +273,47 @@ func TestSegmentWAL_Log_Restore(t *testing.T) { } } - testutil.Ok(t, w.Close()) + assert.NoError(t, w.Close()) } } func TestWALRestoreCorrupted_invalidSegment(t *testing.T) { dir, err := ioutil.TempDir("", "test_wal_log_restore") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() wal, err := OpenSegmentWAL(dir, nil, 0, nil) - testutil.Ok(t, err) - defer func(wal *SegmentWAL) { testutil.Ok(t, wal.Close()) }(wal) + assert.NoError(t, err) + defer func(wal *SegmentWAL) { assert.NoError(t, wal.Close()) }(wal) _, err = wal.createSegmentFile(filepath.Join(dir, "000000")) - testutil.Ok(t, err) + assert.NoError(t, err) f, err := wal.createSegmentFile(filepath.Join(dir, "000001")) - testutil.Ok(t, err) + assert.NoError(t, err) f2, err := wal.createSegmentFile(filepath.Join(dir, "000002")) - testutil.Ok(t, err) - testutil.Ok(t, f2.Close()) + assert.NoError(t, err) + assert.NoError(t, f2.Close()) // Make header of second segment invalid. _, err = f.WriteAt([]byte{1, 2, 3, 4}, 0) - testutil.Ok(t, err) - testutil.Ok(t, f.Close()) + assert.NoError(t, err) + assert.NoError(t, f.Close()) - testutil.Ok(t, wal.Close()) + assert.NoError(t, wal.Close()) wal, err = OpenSegmentWAL(dir, log.NewLogfmtLogger(os.Stderr), 0, nil) - testutil.Ok(t, err) - defer func(wal *SegmentWAL) { testutil.Ok(t, wal.Close()) }(wal) + assert.NoError(t, err) + defer func(wal *SegmentWAL) { assert.NoError(t, wal.Close()) }(wal) files, err := ioutil.ReadDir(dir) - testutil.Ok(t, err) + assert.NoError(t, err) fns := []string{} for _, f := range files { fns = append(fns, f.Name()) } - testutil.Equals(t, []string{"000000"}, fns) + assert.Equal(t, []string{"000000"}, fns) } // Test reading from a WAL that has been corrupted through various means. @@ -325,56 +326,56 @@ func TestWALRestoreCorrupted(t *testing.T) { name: "truncate_checksum", f: func(t *testing.T, w *SegmentWAL) { f, err := os.OpenFile(w.files[0].Name(), os.O_WRONLY, 0666) - testutil.Ok(t, err) + assert.NoError(t, err) defer f.Close() off, err := f.Seek(0, io.SeekEnd) - testutil.Ok(t, err) + assert.NoError(t, err) - testutil.Ok(t, f.Truncate(off-1)) + assert.NoError(t, f.Truncate(off-1)) }, }, { name: "truncate_body", f: func(t *testing.T, w *SegmentWAL) { f, err := os.OpenFile(w.files[0].Name(), os.O_WRONLY, 0666) - testutil.Ok(t, err) + assert.NoError(t, err) defer f.Close() off, err := f.Seek(0, io.SeekEnd) - testutil.Ok(t, err) + assert.NoError(t, err) - testutil.Ok(t, f.Truncate(off-8)) + assert.NoError(t, f.Truncate(off-8)) }, }, { name: "body_content", f: func(t *testing.T, w *SegmentWAL) { f, err := os.OpenFile(w.files[0].Name(), os.O_WRONLY, 0666) - testutil.Ok(t, err) + assert.NoError(t, err) defer f.Close() off, err := f.Seek(0, io.SeekEnd) - testutil.Ok(t, err) + assert.NoError(t, err) // Write junk before checksum starts. _, err = f.WriteAt([]byte{1, 2, 3, 4}, off-8) - testutil.Ok(t, err) + assert.NoError(t, err) }, }, { name: "checksum", f: func(t *testing.T, w *SegmentWAL) { f, err := os.OpenFile(w.files[0].Name(), os.O_WRONLY, 0666) - testutil.Ok(t, err) + assert.NoError(t, err) defer f.Close() off, err := f.Seek(0, io.SeekEnd) - testutil.Ok(t, err) + assert.NoError(t, err) // Write junk into checksum _, err = f.WriteAt([]byte{1, 2, 3, 4}, off-4) - testutil.Ok(t, err) + assert.NoError(t, err) }, }, } @@ -383,29 +384,29 @@ func TestWALRestoreCorrupted(t *testing.T) { // Generate testing data. It does not make semantic sense but // for the purpose of this test. dir, err := ioutil.TempDir("", "test_corrupted") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() w, err := OpenSegmentWAL(dir, nil, 0, nil) - testutil.Ok(t, err) - defer func(wal *SegmentWAL) { testutil.Ok(t, wal.Close()) }(w) + assert.NoError(t, err) + defer func(wal *SegmentWAL) { assert.NoError(t, wal.Close()) }(w) - testutil.Ok(t, w.LogSamples([]record.RefSample{{T: 1, V: 2}})) - testutil.Ok(t, w.LogSamples([]record.RefSample{{T: 2, V: 3}})) + assert.NoError(t, w.LogSamples([]record.RefSample{{T: 1, V: 2}})) + assert.NoError(t, w.LogSamples([]record.RefSample{{T: 2, V: 3}})) - testutil.Ok(t, w.cut()) + assert.NoError(t, w.cut()) // Sleep 2 seconds to avoid error where cut and test "cases" function may write or // truncate the file out of orders as "cases" are not synchronized with cut. // Hopefully cut will complete by 2 seconds. time.Sleep(2 * time.Second) - testutil.Ok(t, w.LogSamples([]record.RefSample{{T: 3, V: 4}})) - testutil.Ok(t, w.LogSamples([]record.RefSample{{T: 5, V: 6}})) + assert.NoError(t, w.LogSamples([]record.RefSample{{T: 3, V: 4}})) + assert.NoError(t, w.LogSamples([]record.RefSample{{T: 5, V: 6}})) - testutil.Ok(t, w.Close()) + assert.NoError(t, w.Close()) // cut() truncates and fsyncs the first segment async. If it happens after // the corruption we apply below, the corruption will be overwritten again. @@ -420,41 +421,41 @@ func TestWALRestoreCorrupted(t *testing.T) { logger := log.NewLogfmtLogger(os.Stderr) w2, err := OpenSegmentWAL(dir, logger, 0, nil) - testutil.Ok(t, err) - defer func(wal *SegmentWAL) { testutil.Ok(t, wal.Close()) }(w2) + assert.NoError(t, err) + defer func(wal *SegmentWAL) { assert.NoError(t, wal.Close()) }(w2) r := w2.Reader() serf := func(l []record.RefSeries) { - testutil.Equals(t, 0, len(l)) + assert.Equal(t, 0, len(l)) } // Weird hack to check order of reads. i := 0 samplef := func(s []record.RefSample) { if i == 0 { - testutil.Equals(t, []record.RefSample{{T: 1, V: 2}}, s) + assert.Equal(t, []record.RefSample{{T: 1, V: 2}}, s) i++ } else { - testutil.Equals(t, []record.RefSample{{T: 99, V: 100}}, s) + assert.Equal(t, []record.RefSample{{T: 99, V: 100}}, s) } } - testutil.Ok(t, r.Read(serf, samplef, nil)) + assert.NoError(t, r.Read(serf, samplef, nil)) - testutil.Ok(t, w2.LogSamples([]record.RefSample{{T: 99, V: 100}})) - testutil.Ok(t, w2.Close()) + assert.NoError(t, w2.LogSamples([]record.RefSample{{T: 99, V: 100}})) + assert.NoError(t, w2.Close()) // We should see the first valid entry and the new one, everything after // is truncated. w3, err := OpenSegmentWAL(dir, logger, 0, nil) - testutil.Ok(t, err) - defer func(wal *SegmentWAL) { testutil.Ok(t, wal.Close()) }(w3) + assert.NoError(t, err) + defer func(wal *SegmentWAL) { assert.NoError(t, wal.Close()) }(w3) r = w3.Reader() i = 0 - testutil.Ok(t, r.Read(serf, samplef, nil)) + assert.NoError(t, r.Read(serf, samplef, nil)) }) } } @@ -463,75 +464,75 @@ func TestMigrateWAL_Empty(t *testing.T) { // The migration procedure must properly deal with a zero-length segment, // which is valid in the new format. dir, err := ioutil.TempDir("", "walmigrate") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() wdir := path.Join(dir, "wal") // Initialize empty WAL. w, err := wal.New(nil, nil, wdir, false) - testutil.Ok(t, err) - testutil.Ok(t, w.Close()) + assert.NoError(t, err) + assert.NoError(t, w.Close()) - testutil.Ok(t, MigrateWAL(nil, wdir)) + assert.NoError(t, MigrateWAL(nil, wdir)) } func TestMigrateWAL_Fuzz(t *testing.T) { dir, err := ioutil.TempDir("", "walmigrate") - testutil.Ok(t, err) + assert.NoError(t, err) defer func() { - testutil.Ok(t, os.RemoveAll(dir)) + assert.NoError(t, os.RemoveAll(dir)) }() wdir := path.Join(dir, "wal") // Should pass if no WAL exists yet. - testutil.Ok(t, MigrateWAL(nil, wdir)) + assert.NoError(t, MigrateWAL(nil, wdir)) oldWAL, err := OpenSegmentWAL(wdir, nil, time.Minute, nil) - testutil.Ok(t, err) + assert.NoError(t, err) // Write some data. - testutil.Ok(t, oldWAL.LogSeries([]record.RefSeries{ + assert.NoError(t, oldWAL.LogSeries([]record.RefSeries{ {Ref: 100, Labels: labels.FromStrings("abc", "def", "123", "456")}, {Ref: 1, Labels: labels.FromStrings("abc", "def2", "1234", "4567")}, })) - testutil.Ok(t, oldWAL.LogSamples([]record.RefSample{ + assert.NoError(t, oldWAL.LogSamples([]record.RefSample{ {Ref: 1, T: 100, V: 200}, {Ref: 2, T: 300, V: 400}, })) - testutil.Ok(t, oldWAL.LogSeries([]record.RefSeries{ + assert.NoError(t, oldWAL.LogSeries([]record.RefSeries{ {Ref: 200, Labels: labels.FromStrings("xyz", "def", "foo", "bar")}, })) - testutil.Ok(t, oldWAL.LogSamples([]record.RefSample{ + assert.NoError(t, oldWAL.LogSamples([]record.RefSample{ {Ref: 3, T: 100, V: 200}, {Ref: 4, T: 300, V: 400}, })) - testutil.Ok(t, oldWAL.LogDeletes([]tombstones.Stone{ + assert.NoError(t, oldWAL.LogDeletes([]tombstones.Stone{ {Ref: 1, Intervals: []tombstones.Interval{{Mint: 100, Maxt: 200}}}, })) - testutil.Ok(t, oldWAL.Close()) + assert.NoError(t, oldWAL.Close()) // Perform migration. - testutil.Ok(t, MigrateWAL(nil, wdir)) + assert.NoError(t, MigrateWAL(nil, wdir)) w, err := wal.New(nil, nil, wdir, false) - testutil.Ok(t, err) + assert.NoError(t, err) // We can properly write some new data after migration. var enc record.Encoder - testutil.Ok(t, w.Log(enc.Samples([]record.RefSample{ + assert.NoError(t, w.Log(enc.Samples([]record.RefSample{ {Ref: 500, T: 1, V: 1}, }, nil))) - testutil.Ok(t, w.Close()) + assert.NoError(t, w.Close()) // Read back all data. sr, err := wal.NewSegmentsReader(wdir) - testutil.Ok(t, err) + assert.NoError(t, err) r := wal.NewReader(sr) var res []interface{} @@ -543,23 +544,23 @@ func TestMigrateWAL_Fuzz(t *testing.T) { switch dec.Type(rec) { case record.Series: s, err := dec.Series(rec, nil) - testutil.Ok(t, err) + assert.NoError(t, err) res = append(res, s) case record.Samples: s, err := dec.Samples(rec, nil) - testutil.Ok(t, err) + assert.NoError(t, err) res = append(res, s) case record.Tombstones: s, err := dec.Tombstones(rec, nil) - testutil.Ok(t, err) + assert.NoError(t, err) res = append(res, s) default: t.Fatalf("unknown record type %d", dec.Type(rec)) } } - testutil.Ok(t, r.Err()) + assert.NoError(t, r.Err()) - testutil.Equals(t, []interface{}{ + assert.Equal(t, []interface{}{ []record.RefSeries{ {Ref: 100, Labels: labels.FromStrings("abc", "def", "123", "456")}, {Ref: 1, Labels: labels.FromStrings("abc", "def2", "1234", "4567")}, @@ -574,5 +575,5 @@ func TestMigrateWAL_Fuzz(t *testing.T) { }, res) // Migrating an already migrated WAL shouldn't do anything. - testutil.Ok(t, MigrateWAL(nil, wdir)) + assert.NoError(t, MigrateWAL(nil, wdir)) } diff --git a/util/stats/stats_test.go b/util/stats/stats_test.go index bf4fde4272..15d76e7911 100644 --- a/util/stats/stats_test.go +++ b/util/stats/stats_test.go @@ -20,6 +20,7 @@ import ( "time" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/prometheus/util/testutil" ) diff --git a/util/testutil/directory.go b/util/testutil/directory.go index 4f0f08fe8f..340d566c02 100644 --- a/util/testutil/directory.go +++ b/util/testutil/directory.go @@ -21,6 +21,8 @@ import ( "path/filepath" "strconv" "testing" + + "github.com/stretchr/testify/assert" ) const ( @@ -137,32 +139,32 @@ func NewTemporaryDirectory(name string, t T) (handler TemporaryDirectory) { 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) + assert.NoError(t, err) if info.IsDir() { return nil } f, err := os.Open(path) - Ok(t, err) + assert.NoError(t, err) defer f.Close() _, err = io.Copy(hash, f) - Ok(t, err) + assert.NoError(t, err) _, err = io.WriteString(hash, strconv.Itoa(int(info.Size()))) - Ok(t, err) + assert.NoError(t, err) _, err = io.WriteString(hash, info.Name()) - Ok(t, err) + assert.NoError(t, err) modTime, err := info.ModTime().GobEncode() - Ok(t, err) + assert.NoError(t, err) _, err = io.WriteString(hash, string(modTime)) - Ok(t, err) + assert.NoError(t, err) return nil }) - Ok(t, err) + assert.NoError(t, err) return hash.Sum(nil) } diff --git a/util/testutil/testing.go b/util/testutil/testing.go index 1645f80d5c..8ec50cb00f 100644 --- a/util/testutil/testing.go +++ b/util/testutil/testing.go @@ -23,140 +23,11 @@ package testutil import ( - "fmt" - "reflect" "testing" - "github.com/davecgh/go-spew/spew" - "github.com/pmezard/go-difflib/difflib" "go.uber.org/goleak" ) -// This package is imported by non-test code and therefore cannot import the -// testing package, which has side effects such as adding flags. Hence we use an -// interface to testing.{T,B}. -type TB interface { - Helper() - Fatalf(string, ...interface{}) -} - -// Assert fails the test if the condition is false. -func Assert(tb TB, condition bool, format string, a ...interface{}) { - tb.Helper() - if !condition { - tb.Fatalf("\033[31m"+format+"\033[39m\n", a...) - } -} - -// Ok fails the test if an err is not nil. -func Ok(tb TB, err error) { - tb.Helper() - if err != nil { - tb.Fatalf("\033[31munexpected error: %v\033[39m\n", err) - } -} - -// NotOk fails the test if an err is nil. -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[1:]...) - } - tb.Fatalf("\033[31mexpected error, got none\033[39m") - } -} - -// Equals fails the test if exp is not equal to act. -func Equals(tb TB, exp, act interface{}, msgAndArgs ...interface{}) { - tb.Helper() - if !reflect.DeepEqual(exp, act) { - tb.Fatalf("\033[31m%s\n\nexp: %#v\n\ngot: %#v%s\033[39m\n", formatMessage(msgAndArgs), exp, act, diff(exp, act)) - } -} - -func typeAndKind(v interface{}) (reflect.Type, reflect.Kind) { - t := reflect.TypeOf(v) - k := t.Kind() - - if k == reflect.Ptr { - t = t.Elem() - k = t.Kind() - } - return t, k -} - -// diff returns a diff of both values as long as both are of the same type and -// are a struct, map, slice, array or string. Otherwise it returns an empty string. -func diff(expected interface{}, actual interface{}) string { - if expected == nil || actual == nil { - return "" - } - - et, ek := typeAndKind(expected) - at, _ := typeAndKind(actual) - if et != at { - return "" - } - - if ek != reflect.Struct && ek != reflect.Map && ek != reflect.Slice && ek != reflect.Array && ek != reflect.String { - return "" - } - - var e, a string - c := spew.ConfigState{ - Indent: " ", - DisablePointerAddresses: true, - DisableCapacities: true, - SortKeys: true, - } - if et != reflect.TypeOf("") { - e = c.Sdump(expected) - a = c.Sdump(actual) - } else { - e = reflect.ValueOf(expected).String() - a = reflect.ValueOf(actual).String() - } - - diff, _ := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{ - A: difflib.SplitLines(e), - B: difflib.SplitLines(a), - FromFile: "Expected", - FromDate: "", - ToFile: "Actual", - ToDate: "", - Context: 1, - }) - return "\n\nDiff:\n" + diff -} - -// ErrorEqual compares Go errors for equality. -func ErrorEqual(tb TB, left, right error, msgAndArgs ...interface{}) { - tb.Helper() - if left == right { - return - } - - if left != nil && right != nil { - Equals(tb, left.Error(), right.Error(), msgAndArgs...) - return - } - - tb.Fatalf("\033[31m%s\n\nexp: %#v\n\ngot: %#v\033[39m\n", formatMessage(msgAndArgs), left, right) -} - -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 "" -} - // TolerantVerifyLeak verifies go leaks but excludes the go routines that are // launched as side effects of some of our dependencies. func TolerantVerifyLeak(m *testing.M) { diff --git a/vendor/github.com/stretchr/testify/LICENSE b/vendor/github.com/stretchr/testify/LICENSE new file mode 100644 index 0000000000..4b0421cf9e --- /dev/null +++ b/vendor/github.com/stretchr/testify/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2012-2020 Mat Ryer, Tyler Bunnell and contributors. + +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. diff --git a/vendor/github.com/stretchr/testify/assert/assertion_compare.go b/vendor/github.com/stretchr/testify/assert/assertion_compare.go new file mode 100644 index 0000000000..dc200395ce --- /dev/null +++ b/vendor/github.com/stretchr/testify/assert/assertion_compare.go @@ -0,0 +1,274 @@ +package assert + +import ( + "fmt" + "reflect" +) + +type CompareType int + +const ( + compareLess CompareType = iota - 1 + compareEqual + compareGreater +) + +func compare(obj1, obj2 interface{}, kind reflect.Kind) (CompareType, bool) { + switch kind { + case reflect.Int: + { + intobj1 := obj1.(int) + intobj2 := obj2.(int) + if intobj1 > intobj2 { + return compareGreater, true + } + if intobj1 == intobj2 { + return compareEqual, true + } + if intobj1 < intobj2 { + return compareLess, true + } + } + case reflect.Int8: + { + int8obj1 := obj1.(int8) + int8obj2 := obj2.(int8) + if int8obj1 > int8obj2 { + return compareGreater, true + } + if int8obj1 == int8obj2 { + return compareEqual, true + } + if int8obj1 < int8obj2 { + return compareLess, true + } + } + case reflect.Int16: + { + int16obj1 := obj1.(int16) + int16obj2 := obj2.(int16) + if int16obj1 > int16obj2 { + return compareGreater, true + } + if int16obj1 == int16obj2 { + return compareEqual, true + } + if int16obj1 < int16obj2 { + return compareLess, true + } + } + case reflect.Int32: + { + int32obj1 := obj1.(int32) + int32obj2 := obj2.(int32) + if int32obj1 > int32obj2 { + return compareGreater, true + } + if int32obj1 == int32obj2 { + return compareEqual, true + } + if int32obj1 < int32obj2 { + return compareLess, true + } + } + case reflect.Int64: + { + int64obj1 := obj1.(int64) + int64obj2 := obj2.(int64) + if int64obj1 > int64obj2 { + return compareGreater, true + } + if int64obj1 == int64obj2 { + return compareEqual, true + } + if int64obj1 < int64obj2 { + return compareLess, true + } + } + case reflect.Uint: + { + uintobj1 := obj1.(uint) + uintobj2 := obj2.(uint) + if uintobj1 > uintobj2 { + return compareGreater, true + } + if uintobj1 == uintobj2 { + return compareEqual, true + } + if uintobj1 < uintobj2 { + return compareLess, true + } + } + case reflect.Uint8: + { + uint8obj1 := obj1.(uint8) + uint8obj2 := obj2.(uint8) + if uint8obj1 > uint8obj2 { + return compareGreater, true + } + if uint8obj1 == uint8obj2 { + return compareEqual, true + } + if uint8obj1 < uint8obj2 { + return compareLess, true + } + } + case reflect.Uint16: + { + uint16obj1 := obj1.(uint16) + uint16obj2 := obj2.(uint16) + if uint16obj1 > uint16obj2 { + return compareGreater, true + } + if uint16obj1 == uint16obj2 { + return compareEqual, true + } + if uint16obj1 < uint16obj2 { + return compareLess, true + } + } + case reflect.Uint32: + { + uint32obj1 := obj1.(uint32) + uint32obj2 := obj2.(uint32) + if uint32obj1 > uint32obj2 { + return compareGreater, true + } + if uint32obj1 == uint32obj2 { + return compareEqual, true + } + if uint32obj1 < uint32obj2 { + return compareLess, true + } + } + case reflect.Uint64: + { + uint64obj1 := obj1.(uint64) + uint64obj2 := obj2.(uint64) + if uint64obj1 > uint64obj2 { + return compareGreater, true + } + if uint64obj1 == uint64obj2 { + return compareEqual, true + } + if uint64obj1 < uint64obj2 { + return compareLess, true + } + } + case reflect.Float32: + { + float32obj1 := obj1.(float32) + float32obj2 := obj2.(float32) + if float32obj1 > float32obj2 { + return compareGreater, true + } + if float32obj1 == float32obj2 { + return compareEqual, true + } + if float32obj1 < float32obj2 { + return compareLess, true + } + } + case reflect.Float64: + { + float64obj1 := obj1.(float64) + float64obj2 := obj2.(float64) + if float64obj1 > float64obj2 { + return compareGreater, true + } + if float64obj1 == float64obj2 { + return compareEqual, true + } + if float64obj1 < float64obj2 { + return compareLess, true + } + } + case reflect.String: + { + stringobj1 := obj1.(string) + stringobj2 := obj2.(string) + if stringobj1 > stringobj2 { + return compareGreater, true + } + if stringobj1 == stringobj2 { + return compareEqual, true + } + if stringobj1 < stringobj2 { + return compareLess, true + } + } + } + + return compareEqual, false +} + +// Greater asserts that the first element is greater than the second +// +// assert.Greater(t, 2, 1) +// assert.Greater(t, float64(2), float64(1)) +// assert.Greater(t, "b", "a") +func Greater(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool { + return compareTwoValues(t, e1, e2, []CompareType{compareGreater}, "\"%v\" is not greater than \"%v\"", msgAndArgs) +} + +// GreaterOrEqual asserts that the first element is greater than or equal to the second +// +// assert.GreaterOrEqual(t, 2, 1) +// assert.GreaterOrEqual(t, 2, 2) +// assert.GreaterOrEqual(t, "b", "a") +// assert.GreaterOrEqual(t, "b", "b") +func GreaterOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool { + return compareTwoValues(t, e1, e2, []CompareType{compareGreater, compareEqual}, "\"%v\" is not greater than or equal to \"%v\"", msgAndArgs) +} + +// Less asserts that the first element is less than the second +// +// assert.Less(t, 1, 2) +// assert.Less(t, float64(1), float64(2)) +// assert.Less(t, "a", "b") +func Less(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool { + return compareTwoValues(t, e1, e2, []CompareType{compareLess}, "\"%v\" is not less than \"%v\"", msgAndArgs) +} + +// LessOrEqual asserts that the first element is less than or equal to the second +// +// assert.LessOrEqual(t, 1, 2) +// assert.LessOrEqual(t, 2, 2) +// assert.LessOrEqual(t, "a", "b") +// assert.LessOrEqual(t, "b", "b") +func LessOrEqual(t TestingT, e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool { + return compareTwoValues(t, e1, e2, []CompareType{compareLess, compareEqual}, "\"%v\" is not less than or equal to \"%v\"", msgAndArgs) +} + +func compareTwoValues(t TestingT, e1 interface{}, e2 interface{}, allowedComparesResults []CompareType, failMessage string, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + e1Kind := reflect.ValueOf(e1).Kind() + e2Kind := reflect.ValueOf(e2).Kind() + if e1Kind != e2Kind { + return Fail(t, "Elements should be the same type", msgAndArgs...) + } + + compareResult, isComparable := compare(e1, e2, e1Kind) + if !isComparable { + return Fail(t, fmt.Sprintf("Can not compare type \"%s\"", reflect.TypeOf(e1)), msgAndArgs...) + } + + if !containsValue(allowedComparesResults, compareResult) { + return Fail(t, fmt.Sprintf(failMessage, e1, e2), msgAndArgs...) + } + + return true +} + +func containsValue(values []CompareType, value CompareType) bool { + for _, v := range values { + if v == value { + return true + } + } + + return false +} diff --git a/vendor/github.com/stretchr/testify/assert/assertion_format.go b/vendor/github.com/stretchr/testify/assert/assertion_format.go new file mode 100644 index 0000000000..49370eb167 --- /dev/null +++ b/vendor/github.com/stretchr/testify/assert/assertion_format.go @@ -0,0 +1,644 @@ +/* +* CODE GENERATED AUTOMATICALLY WITH github.com/stretchr/testify/_codegen +* THIS FILE MUST NOT BE EDITED BY HAND + */ + +package assert + +import ( + http "net/http" + url "net/url" + time "time" +) + +// Conditionf uses a Comparison to assert a complex condition. +func Conditionf(t TestingT, comp Comparison, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Condition(t, comp, append([]interface{}{msg}, args...)...) +} + +// Containsf asserts that the specified string, list(array, slice...) or map contains the +// specified substring or element. +// +// assert.Containsf(t, "Hello World", "World", "error message %s", "formatted") +// assert.Containsf(t, ["Hello", "World"], "World", "error message %s", "formatted") +// assert.Containsf(t, {"Hello": "World"}, "Hello", "error message %s", "formatted") +func Containsf(t TestingT, s interface{}, contains interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Contains(t, s, contains, append([]interface{}{msg}, args...)...) +} + +// DirExistsf checks whether a directory exists in the given path. It also fails +// if the path is a file rather a directory or there is an error checking whether it exists. +func DirExistsf(t TestingT, path string, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return DirExists(t, path, append([]interface{}{msg}, args...)...) +} + +// ElementsMatchf asserts that the specified listA(array, slice...) is equal to specified +// listB(array, slice...) ignoring the order of the elements. If there are duplicate elements, +// the number of appearances of each of them in both lists should match. +// +// assert.ElementsMatchf(t, [1, 3, 2, 3], [1, 3, 3, 2], "error message %s", "formatted") +func ElementsMatchf(t TestingT, listA interface{}, listB interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return ElementsMatch(t, listA, listB, append([]interface{}{msg}, args...)...) +} + +// Emptyf asserts that the specified object is empty. I.e. nil, "", false, 0 or either +// a slice or a channel with len == 0. +// +// assert.Emptyf(t, obj, "error message %s", "formatted") +func Emptyf(t TestingT, object interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Empty(t, object, append([]interface{}{msg}, args...)...) +} + +// Equalf asserts that two objects are equal. +// +// assert.Equalf(t, 123, 123, "error message %s", "formatted") +// +// Pointer variable equality is determined based on the equality of the +// referenced values (as opposed to the memory addresses). Function equality +// cannot be determined and will always fail. +func Equalf(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Equal(t, expected, actual, append([]interface{}{msg}, args...)...) +} + +// EqualErrorf asserts that a function returned an error (i.e. not `nil`) +// and that it is equal to the provided error. +// +// actualObj, err := SomeFunction() +// assert.EqualErrorf(t, err, expectedErrorString, "error message %s", "formatted") +func EqualErrorf(t TestingT, theError error, errString string, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return EqualError(t, theError, errString, append([]interface{}{msg}, args...)...) +} + +// EqualValuesf asserts that two objects are equal or convertable to the same types +// and equal. +// +// assert.EqualValuesf(t, uint32(123), int32(123), "error message %s", "formatted") +func EqualValuesf(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return EqualValues(t, expected, actual, append([]interface{}{msg}, args...)...) +} + +// Errorf asserts that a function returned an error (i.e. not `nil`). +// +// actualObj, err := SomeFunction() +// if assert.Errorf(t, err, "error message %s", "formatted") { +// assert.Equal(t, expectedErrorf, err) +// } +func Errorf(t TestingT, err error, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Error(t, err, append([]interface{}{msg}, args...)...) +} + +// Eventuallyf asserts that given condition will be met in waitFor time, +// periodically checking target function each tick. +// +// assert.Eventuallyf(t, func() bool { return true; }, time.Second, 10*time.Millisecond, "error message %s", "formatted") +func Eventuallyf(t TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Eventually(t, condition, waitFor, tick, append([]interface{}{msg}, args...)...) +} + +// Exactlyf asserts that two objects are equal in value and type. +// +// assert.Exactlyf(t, int32(123), int64(123), "error message %s", "formatted") +func Exactlyf(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Exactly(t, expected, actual, append([]interface{}{msg}, args...)...) +} + +// Failf reports a failure through +func Failf(t TestingT, failureMessage string, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Fail(t, failureMessage, append([]interface{}{msg}, args...)...) +} + +// FailNowf fails test +func FailNowf(t TestingT, failureMessage string, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return FailNow(t, failureMessage, append([]interface{}{msg}, args...)...) +} + +// Falsef asserts that the specified value is false. +// +// assert.Falsef(t, myBool, "error message %s", "formatted") +func Falsef(t TestingT, value bool, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return False(t, value, append([]interface{}{msg}, args...)...) +} + +// FileExistsf checks whether a file exists in the given path. It also fails if +// the path points to a directory or there is an error when trying to check the file. +func FileExistsf(t TestingT, path string, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return FileExists(t, path, append([]interface{}{msg}, args...)...) +} + +// Greaterf asserts that the first element is greater than the second +// +// assert.Greaterf(t, 2, 1, "error message %s", "formatted") +// assert.Greaterf(t, float64(2), float64(1), "error message %s", "formatted") +// assert.Greaterf(t, "b", "a", "error message %s", "formatted") +func Greaterf(t TestingT, e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Greater(t, e1, e2, append([]interface{}{msg}, args...)...) +} + +// GreaterOrEqualf asserts that the first element is greater than or equal to the second +// +// assert.GreaterOrEqualf(t, 2, 1, "error message %s", "formatted") +// assert.GreaterOrEqualf(t, 2, 2, "error message %s", "formatted") +// assert.GreaterOrEqualf(t, "b", "a", "error message %s", "formatted") +// assert.GreaterOrEqualf(t, "b", "b", "error message %s", "formatted") +func GreaterOrEqualf(t TestingT, e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return GreaterOrEqual(t, e1, e2, append([]interface{}{msg}, args...)...) +} + +// HTTPBodyContainsf asserts that a specified handler returns a +// body that contains a string. +// +// assert.HTTPBodyContainsf(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky", "error message %s", "formatted") +// +// Returns whether the assertion was successful (true) or not (false). +func HTTPBodyContainsf(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return HTTPBodyContains(t, handler, method, url, values, str, append([]interface{}{msg}, args...)...) +} + +// HTTPBodyNotContainsf asserts that a specified handler returns a +// body that does not contain a string. +// +// assert.HTTPBodyNotContainsf(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky", "error message %s", "formatted") +// +// Returns whether the assertion was successful (true) or not (false). +func HTTPBodyNotContainsf(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return HTTPBodyNotContains(t, handler, method, url, values, str, append([]interface{}{msg}, args...)...) +} + +// HTTPErrorf asserts that a specified handler returns an error status code. +// +// assert.HTTPErrorf(t, myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}} +// +// Returns whether the assertion was successful (true) or not (false). +func HTTPErrorf(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return HTTPError(t, handler, method, url, values, append([]interface{}{msg}, args...)...) +} + +// HTTPRedirectf asserts that a specified handler returns a redirect status code. +// +// assert.HTTPRedirectf(t, myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}} +// +// Returns whether the assertion was successful (true) or not (false). +func HTTPRedirectf(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return HTTPRedirect(t, handler, method, url, values, append([]interface{}{msg}, args...)...) +} + +// HTTPStatusCodef asserts that a specified handler returns a specified status code. +// +// assert.HTTPStatusCodef(t, myHandler, "GET", "/notImplemented", nil, 501, "error message %s", "formatted") +// +// Returns whether the assertion was successful (true) or not (false). +func HTTPStatusCodef(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, statuscode int, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return HTTPStatusCode(t, handler, method, url, values, statuscode, append([]interface{}{msg}, args...)...) +} + +// HTTPSuccessf asserts that a specified handler returns a success status code. +// +// assert.HTTPSuccessf(t, myHandler, "POST", "http://www.google.com", nil, "error message %s", "formatted") +// +// Returns whether the assertion was successful (true) or not (false). +func HTTPSuccessf(t TestingT, handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return HTTPSuccess(t, handler, method, url, values, append([]interface{}{msg}, args...)...) +} + +// Implementsf asserts that an object is implemented by the specified interface. +// +// assert.Implementsf(t, (*MyInterface)(nil), new(MyObject), "error message %s", "formatted") +func Implementsf(t TestingT, interfaceObject interface{}, object interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Implements(t, interfaceObject, object, append([]interface{}{msg}, args...)...) +} + +// InDeltaf asserts that the two numerals are within delta of each other. +// +// assert.InDeltaf(t, math.Pi, 22/7.0, 0.01, "error message %s", "formatted") +func InDeltaf(t TestingT, expected interface{}, actual interface{}, delta float64, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return InDelta(t, expected, actual, delta, append([]interface{}{msg}, args...)...) +} + +// InDeltaMapValuesf is the same as InDelta, but it compares all values between two maps. Both maps must have exactly the same keys. +func InDeltaMapValuesf(t TestingT, expected interface{}, actual interface{}, delta float64, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return InDeltaMapValues(t, expected, actual, delta, append([]interface{}{msg}, args...)...) +} + +// InDeltaSlicef is the same as InDelta, except it compares two slices. +func InDeltaSlicef(t TestingT, expected interface{}, actual interface{}, delta float64, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return InDeltaSlice(t, expected, actual, delta, append([]interface{}{msg}, args...)...) +} + +// InEpsilonf asserts that expected and actual have a relative error less than epsilon +func InEpsilonf(t TestingT, expected interface{}, actual interface{}, epsilon float64, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return InEpsilon(t, expected, actual, epsilon, append([]interface{}{msg}, args...)...) +} + +// InEpsilonSlicef is the same as InEpsilon, except it compares each value from two slices. +func InEpsilonSlicef(t TestingT, expected interface{}, actual interface{}, epsilon float64, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return InEpsilonSlice(t, expected, actual, epsilon, append([]interface{}{msg}, args...)...) +} + +// IsTypef asserts that the specified objects are of the same type. +func IsTypef(t TestingT, expectedType interface{}, object interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return IsType(t, expectedType, object, append([]interface{}{msg}, args...)...) +} + +// JSONEqf asserts that two JSON strings are equivalent. +// +// assert.JSONEqf(t, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`, "error message %s", "formatted") +func JSONEqf(t TestingT, expected string, actual string, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return JSONEq(t, expected, actual, append([]interface{}{msg}, args...)...) +} + +// Lenf asserts that the specified object has specific length. +// Lenf also fails if the object has a type that len() not accept. +// +// assert.Lenf(t, mySlice, 3, "error message %s", "formatted") +func Lenf(t TestingT, object interface{}, length int, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Len(t, object, length, append([]interface{}{msg}, args...)...) +} + +// Lessf asserts that the first element is less than the second +// +// assert.Lessf(t, 1, 2, "error message %s", "formatted") +// assert.Lessf(t, float64(1), float64(2), "error message %s", "formatted") +// assert.Lessf(t, "a", "b", "error message %s", "formatted") +func Lessf(t TestingT, e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Less(t, e1, e2, append([]interface{}{msg}, args...)...) +} + +// LessOrEqualf asserts that the first element is less than or equal to the second +// +// assert.LessOrEqualf(t, 1, 2, "error message %s", "formatted") +// assert.LessOrEqualf(t, 2, 2, "error message %s", "formatted") +// assert.LessOrEqualf(t, "a", "b", "error message %s", "formatted") +// assert.LessOrEqualf(t, "b", "b", "error message %s", "formatted") +func LessOrEqualf(t TestingT, e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return LessOrEqual(t, e1, e2, append([]interface{}{msg}, args...)...) +} + +// Neverf asserts that the given condition doesn't satisfy in waitFor time, +// periodically checking the target function each tick. +// +// assert.Neverf(t, func() bool { return false; }, time.Second, 10*time.Millisecond, "error message %s", "formatted") +func Neverf(t TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Never(t, condition, waitFor, tick, append([]interface{}{msg}, args...)...) +} + +// Nilf asserts that the specified object is nil. +// +// assert.Nilf(t, err, "error message %s", "formatted") +func Nilf(t TestingT, object interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Nil(t, object, append([]interface{}{msg}, args...)...) +} + +// NoDirExistsf checks whether a directory does not exist in the given path. +// It fails if the path points to an existing _directory_ only. +func NoDirExistsf(t TestingT, path string, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NoDirExists(t, path, append([]interface{}{msg}, args...)...) +} + +// NoErrorf asserts that a function returned no error (i.e. `nil`). +// +// actualObj, err := SomeFunction() +// if assert.NoErrorf(t, err, "error message %s", "formatted") { +// assert.Equal(t, expectedObj, actualObj) +// } +func NoErrorf(t TestingT, err error, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NoError(t, err, append([]interface{}{msg}, args...)...) +} + +// NoFileExistsf checks whether a file does not exist in a given path. It fails +// if the path points to an existing _file_ only. +func NoFileExistsf(t TestingT, path string, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NoFileExists(t, path, append([]interface{}{msg}, args...)...) +} + +// NotContainsf asserts that the specified string, list(array, slice...) or map does NOT contain the +// specified substring or element. +// +// assert.NotContainsf(t, "Hello World", "Earth", "error message %s", "formatted") +// assert.NotContainsf(t, ["Hello", "World"], "Earth", "error message %s", "formatted") +// assert.NotContainsf(t, {"Hello": "World"}, "Earth", "error message %s", "formatted") +func NotContainsf(t TestingT, s interface{}, contains interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NotContains(t, s, contains, append([]interface{}{msg}, args...)...) +} + +// NotEmptyf asserts that the specified object is NOT empty. I.e. not nil, "", false, 0 or either +// a slice or a channel with len == 0. +// +// if assert.NotEmptyf(t, obj, "error message %s", "formatted") { +// assert.Equal(t, "two", obj[1]) +// } +func NotEmptyf(t TestingT, object interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NotEmpty(t, object, append([]interface{}{msg}, args...)...) +} + +// NotEqualf asserts that the specified values are NOT equal. +// +// assert.NotEqualf(t, obj1, obj2, "error message %s", "formatted") +// +// Pointer variable equality is determined based on the equality of the +// referenced values (as opposed to the memory addresses). +func NotEqualf(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NotEqual(t, expected, actual, append([]interface{}{msg}, args...)...) +} + +// NotEqualValuesf asserts that two objects are not equal even when converted to the same type +// +// assert.NotEqualValuesf(t, obj1, obj2, "error message %s", "formatted") +func NotEqualValuesf(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NotEqualValues(t, expected, actual, append([]interface{}{msg}, args...)...) +} + +// NotNilf asserts that the specified object is not nil. +// +// assert.NotNilf(t, err, "error message %s", "formatted") +func NotNilf(t TestingT, object interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NotNil(t, object, append([]interface{}{msg}, args...)...) +} + +// NotPanicsf asserts that the code inside the specified PanicTestFunc does NOT panic. +// +// assert.NotPanicsf(t, func(){ RemainCalm() }, "error message %s", "formatted") +func NotPanicsf(t TestingT, f PanicTestFunc, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NotPanics(t, f, append([]interface{}{msg}, args...)...) +} + +// NotRegexpf asserts that a specified regexp does not match a string. +// +// assert.NotRegexpf(t, regexp.MustCompile("starts"), "it's starting", "error message %s", "formatted") +// assert.NotRegexpf(t, "^start", "it's not starting", "error message %s", "formatted") +func NotRegexpf(t TestingT, rx interface{}, str interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NotRegexp(t, rx, str, append([]interface{}{msg}, args...)...) +} + +// NotSamef asserts that two pointers do not reference the same object. +// +// assert.NotSamef(t, ptr1, ptr2, "error message %s", "formatted") +// +// Both arguments must be pointer variables. Pointer variable sameness is +// determined based on the equality of both type and value. +func NotSamef(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NotSame(t, expected, actual, append([]interface{}{msg}, args...)...) +} + +// NotSubsetf asserts that the specified list(array, slice...) contains not all +// elements given in the specified subset(array, slice...). +// +// assert.NotSubsetf(t, [1, 3, 4], [1, 2], "But [1, 3, 4] does not contain [1, 2]", "error message %s", "formatted") +func NotSubsetf(t TestingT, list interface{}, subset interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NotSubset(t, list, subset, append([]interface{}{msg}, args...)...) +} + +// NotZerof asserts that i is not the zero value for its type. +func NotZerof(t TestingT, i interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return NotZero(t, i, append([]interface{}{msg}, args...)...) +} + +// Panicsf asserts that the code inside the specified PanicTestFunc panics. +// +// assert.Panicsf(t, func(){ GoCrazy() }, "error message %s", "formatted") +func Panicsf(t TestingT, f PanicTestFunc, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Panics(t, f, append([]interface{}{msg}, args...)...) +} + +// PanicsWithErrorf asserts that the code inside the specified PanicTestFunc +// panics, and that the recovered panic value is an error that satisfies the +// EqualError comparison. +// +// assert.PanicsWithErrorf(t, "crazy error", func(){ GoCrazy() }, "error message %s", "formatted") +func PanicsWithErrorf(t TestingT, errString string, f PanicTestFunc, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return PanicsWithError(t, errString, f, append([]interface{}{msg}, args...)...) +} + +// PanicsWithValuef asserts that the code inside the specified PanicTestFunc panics, and that +// the recovered panic value equals the expected panic value. +// +// assert.PanicsWithValuef(t, "crazy error", func(){ GoCrazy() }, "error message %s", "formatted") +func PanicsWithValuef(t TestingT, expected interface{}, f PanicTestFunc, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return PanicsWithValue(t, expected, f, append([]interface{}{msg}, args...)...) +} + +// Regexpf asserts that a specified regexp matches a string. +// +// assert.Regexpf(t, regexp.MustCompile("start"), "it's starting", "error message %s", "formatted") +// assert.Regexpf(t, "start...$", "it's not starting", "error message %s", "formatted") +func Regexpf(t TestingT, rx interface{}, str interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Regexp(t, rx, str, append([]interface{}{msg}, args...)...) +} + +// Samef asserts that two pointers reference the same object. +// +// assert.Samef(t, ptr1, ptr2, "error message %s", "formatted") +// +// Both arguments must be pointer variables. Pointer variable sameness is +// determined based on the equality of both type and value. +func Samef(t TestingT, expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Same(t, expected, actual, append([]interface{}{msg}, args...)...) +} + +// Subsetf asserts that the specified list(array, slice...) contains all +// elements given in the specified subset(array, slice...). +// +// assert.Subsetf(t, [1, 2, 3], [1, 2], "But [1, 2, 3] does contain [1, 2]", "error message %s", "formatted") +func Subsetf(t TestingT, list interface{}, subset interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Subset(t, list, subset, append([]interface{}{msg}, args...)...) +} + +// Truef asserts that the specified value is true. +// +// assert.Truef(t, myBool, "error message %s", "formatted") +func Truef(t TestingT, value bool, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return True(t, value, append([]interface{}{msg}, args...)...) +} + +// WithinDurationf asserts that the two times are within duration delta of each other. +// +// assert.WithinDurationf(t, time.Now(), time.Now(), 10*time.Second, "error message %s", "formatted") +func WithinDurationf(t TestingT, expected time.Time, actual time.Time, delta time.Duration, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return WithinDuration(t, expected, actual, delta, append([]interface{}{msg}, args...)...) +} + +// YAMLEqf asserts that two YAML strings are equivalent. +func YAMLEqf(t TestingT, expected string, actual string, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return YAMLEq(t, expected, actual, append([]interface{}{msg}, args...)...) +} + +// Zerof asserts that i is the zero value for its type. +func Zerof(t TestingT, i interface{}, msg string, args ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Zero(t, i, append([]interface{}{msg}, args...)...) +} diff --git a/vendor/github.com/stretchr/testify/assert/assertion_format.go.tmpl b/vendor/github.com/stretchr/testify/assert/assertion_format.go.tmpl new file mode 100644 index 0000000000..d2bb0b8177 --- /dev/null +++ b/vendor/github.com/stretchr/testify/assert/assertion_format.go.tmpl @@ -0,0 +1,5 @@ +{{.CommentFormat}} +func {{.DocInfo.Name}}f(t TestingT, {{.ParamsFormat}}) bool { + if h, ok := t.(tHelper); ok { h.Helper() } + return {{.DocInfo.Name}}(t, {{.ForwardedParamsFormat}}) +} diff --git a/vendor/github.com/stretchr/testify/assert/assertion_forward.go b/vendor/github.com/stretchr/testify/assert/assertion_forward.go new file mode 100644 index 0000000000..9db889427a --- /dev/null +++ b/vendor/github.com/stretchr/testify/assert/assertion_forward.go @@ -0,0 +1,1276 @@ +/* +* CODE GENERATED AUTOMATICALLY WITH github.com/stretchr/testify/_codegen +* THIS FILE MUST NOT BE EDITED BY HAND + */ + +package assert + +import ( + http "net/http" + url "net/url" + time "time" +) + +// Condition uses a Comparison to assert a complex condition. +func (a *Assertions) Condition(comp Comparison, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Condition(a.t, comp, msgAndArgs...) +} + +// Conditionf uses a Comparison to assert a complex condition. +func (a *Assertions) Conditionf(comp Comparison, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Conditionf(a.t, comp, msg, args...) +} + +// Contains asserts that the specified string, list(array, slice...) or map contains the +// specified substring or element. +// +// a.Contains("Hello World", "World") +// a.Contains(["Hello", "World"], "World") +// a.Contains({"Hello": "World"}, "Hello") +func (a *Assertions) Contains(s interface{}, contains interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Contains(a.t, s, contains, msgAndArgs...) +} + +// Containsf asserts that the specified string, list(array, slice...) or map contains the +// specified substring or element. +// +// a.Containsf("Hello World", "World", "error message %s", "formatted") +// a.Containsf(["Hello", "World"], "World", "error message %s", "formatted") +// a.Containsf({"Hello": "World"}, "Hello", "error message %s", "formatted") +func (a *Assertions) Containsf(s interface{}, contains interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Containsf(a.t, s, contains, msg, args...) +} + +// DirExists checks whether a directory exists in the given path. It also fails +// if the path is a file rather a directory or there is an error checking whether it exists. +func (a *Assertions) DirExists(path string, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return DirExists(a.t, path, msgAndArgs...) +} + +// DirExistsf checks whether a directory exists in the given path. It also fails +// if the path is a file rather a directory or there is an error checking whether it exists. +func (a *Assertions) DirExistsf(path string, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return DirExistsf(a.t, path, msg, args...) +} + +// ElementsMatch asserts that the specified listA(array, slice...) is equal to specified +// listB(array, slice...) ignoring the order of the elements. If there are duplicate elements, +// the number of appearances of each of them in both lists should match. +// +// a.ElementsMatch([1, 3, 2, 3], [1, 3, 3, 2]) +func (a *Assertions) ElementsMatch(listA interface{}, listB interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return ElementsMatch(a.t, listA, listB, msgAndArgs...) +} + +// ElementsMatchf asserts that the specified listA(array, slice...) is equal to specified +// listB(array, slice...) ignoring the order of the elements. If there are duplicate elements, +// the number of appearances of each of them in both lists should match. +// +// a.ElementsMatchf([1, 3, 2, 3], [1, 3, 3, 2], "error message %s", "formatted") +func (a *Assertions) ElementsMatchf(listA interface{}, listB interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return ElementsMatchf(a.t, listA, listB, msg, args...) +} + +// Empty asserts that the specified object is empty. I.e. nil, "", false, 0 or either +// a slice or a channel with len == 0. +// +// a.Empty(obj) +func (a *Assertions) Empty(object interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Empty(a.t, object, msgAndArgs...) +} + +// Emptyf asserts that the specified object is empty. I.e. nil, "", false, 0 or either +// a slice or a channel with len == 0. +// +// a.Emptyf(obj, "error message %s", "formatted") +func (a *Assertions) Emptyf(object interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Emptyf(a.t, object, msg, args...) +} + +// Equal asserts that two objects are equal. +// +// a.Equal(123, 123) +// +// Pointer variable equality is determined based on the equality of the +// referenced values (as opposed to the memory addresses). Function equality +// cannot be determined and will always fail. +func (a *Assertions) Equal(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Equal(a.t, expected, actual, msgAndArgs...) +} + +// EqualError asserts that a function returned an error (i.e. not `nil`) +// and that it is equal to the provided error. +// +// actualObj, err := SomeFunction() +// a.EqualError(err, expectedErrorString) +func (a *Assertions) EqualError(theError error, errString string, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return EqualError(a.t, theError, errString, msgAndArgs...) +} + +// EqualErrorf asserts that a function returned an error (i.e. not `nil`) +// and that it is equal to the provided error. +// +// actualObj, err := SomeFunction() +// a.EqualErrorf(err, expectedErrorString, "error message %s", "formatted") +func (a *Assertions) EqualErrorf(theError error, errString string, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return EqualErrorf(a.t, theError, errString, msg, args...) +} + +// EqualValues asserts that two objects are equal or convertable to the same types +// and equal. +// +// a.EqualValues(uint32(123), int32(123)) +func (a *Assertions) EqualValues(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return EqualValues(a.t, expected, actual, msgAndArgs...) +} + +// EqualValuesf asserts that two objects are equal or convertable to the same types +// and equal. +// +// a.EqualValuesf(uint32(123), int32(123), "error message %s", "formatted") +func (a *Assertions) EqualValuesf(expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return EqualValuesf(a.t, expected, actual, msg, args...) +} + +// Equalf asserts that two objects are equal. +// +// a.Equalf(123, 123, "error message %s", "formatted") +// +// Pointer variable equality is determined based on the equality of the +// referenced values (as opposed to the memory addresses). Function equality +// cannot be determined and will always fail. +func (a *Assertions) Equalf(expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Equalf(a.t, expected, actual, msg, args...) +} + +// Error asserts that a function returned an error (i.e. not `nil`). +// +// actualObj, err := SomeFunction() +// if a.Error(err) { +// assert.Equal(t, expectedError, err) +// } +func (a *Assertions) Error(err error, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Error(a.t, err, msgAndArgs...) +} + +// Errorf asserts that a function returned an error (i.e. not `nil`). +// +// actualObj, err := SomeFunction() +// if a.Errorf(err, "error message %s", "formatted") { +// assert.Equal(t, expectedErrorf, err) +// } +func (a *Assertions) Errorf(err error, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Errorf(a.t, err, msg, args...) +} + +// Eventually asserts that given condition will be met in waitFor time, +// periodically checking target function each tick. +// +// a.Eventually(func() bool { return true; }, time.Second, 10*time.Millisecond) +func (a *Assertions) Eventually(condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Eventually(a.t, condition, waitFor, tick, msgAndArgs...) +} + +// Eventuallyf asserts that given condition will be met in waitFor time, +// periodically checking target function each tick. +// +// a.Eventuallyf(func() bool { return true; }, time.Second, 10*time.Millisecond, "error message %s", "formatted") +func (a *Assertions) Eventuallyf(condition func() bool, waitFor time.Duration, tick time.Duration, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Eventuallyf(a.t, condition, waitFor, tick, msg, args...) +} + +// Exactly asserts that two objects are equal in value and type. +// +// a.Exactly(int32(123), int64(123)) +func (a *Assertions) Exactly(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Exactly(a.t, expected, actual, msgAndArgs...) +} + +// Exactlyf asserts that two objects are equal in value and type. +// +// a.Exactlyf(int32(123), int64(123), "error message %s", "formatted") +func (a *Assertions) Exactlyf(expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Exactlyf(a.t, expected, actual, msg, args...) +} + +// Fail reports a failure through +func (a *Assertions) Fail(failureMessage string, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Fail(a.t, failureMessage, msgAndArgs...) +} + +// FailNow fails test +func (a *Assertions) FailNow(failureMessage string, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return FailNow(a.t, failureMessage, msgAndArgs...) +} + +// FailNowf fails test +func (a *Assertions) FailNowf(failureMessage string, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return FailNowf(a.t, failureMessage, msg, args...) +} + +// Failf reports a failure through +func (a *Assertions) Failf(failureMessage string, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Failf(a.t, failureMessage, msg, args...) +} + +// False asserts that the specified value is false. +// +// a.False(myBool) +func (a *Assertions) False(value bool, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return False(a.t, value, msgAndArgs...) +} + +// Falsef asserts that the specified value is false. +// +// a.Falsef(myBool, "error message %s", "formatted") +func (a *Assertions) Falsef(value bool, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Falsef(a.t, value, msg, args...) +} + +// FileExists checks whether a file exists in the given path. It also fails if +// the path points to a directory or there is an error when trying to check the file. +func (a *Assertions) FileExists(path string, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return FileExists(a.t, path, msgAndArgs...) +} + +// FileExistsf checks whether a file exists in the given path. It also fails if +// the path points to a directory or there is an error when trying to check the file. +func (a *Assertions) FileExistsf(path string, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return FileExistsf(a.t, path, msg, args...) +} + +// Greater asserts that the first element is greater than the second +// +// a.Greater(2, 1) +// a.Greater(float64(2), float64(1)) +// a.Greater("b", "a") +func (a *Assertions) Greater(e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Greater(a.t, e1, e2, msgAndArgs...) +} + +// GreaterOrEqual asserts that the first element is greater than or equal to the second +// +// a.GreaterOrEqual(2, 1) +// a.GreaterOrEqual(2, 2) +// a.GreaterOrEqual("b", "a") +// a.GreaterOrEqual("b", "b") +func (a *Assertions) GreaterOrEqual(e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return GreaterOrEqual(a.t, e1, e2, msgAndArgs...) +} + +// GreaterOrEqualf asserts that the first element is greater than or equal to the second +// +// a.GreaterOrEqualf(2, 1, "error message %s", "formatted") +// a.GreaterOrEqualf(2, 2, "error message %s", "formatted") +// a.GreaterOrEqualf("b", "a", "error message %s", "formatted") +// a.GreaterOrEqualf("b", "b", "error message %s", "formatted") +func (a *Assertions) GreaterOrEqualf(e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return GreaterOrEqualf(a.t, e1, e2, msg, args...) +} + +// Greaterf asserts that the first element is greater than the second +// +// a.Greaterf(2, 1, "error message %s", "formatted") +// a.Greaterf(float64(2), float64(1), "error message %s", "formatted") +// a.Greaterf("b", "a", "error message %s", "formatted") +func (a *Assertions) Greaterf(e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Greaterf(a.t, e1, e2, msg, args...) +} + +// HTTPBodyContains asserts that a specified handler returns a +// body that contains a string. +// +// a.HTTPBodyContains(myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky") +// +// Returns whether the assertion was successful (true) or not (false). +func (a *Assertions) HTTPBodyContains(handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return HTTPBodyContains(a.t, handler, method, url, values, str, msgAndArgs...) +} + +// HTTPBodyContainsf asserts that a specified handler returns a +// body that contains a string. +// +// a.HTTPBodyContainsf(myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky", "error message %s", "formatted") +// +// Returns whether the assertion was successful (true) or not (false). +func (a *Assertions) HTTPBodyContainsf(handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return HTTPBodyContainsf(a.t, handler, method, url, values, str, msg, args...) +} + +// HTTPBodyNotContains asserts that a specified handler returns a +// body that does not contain a string. +// +// a.HTTPBodyNotContains(myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky") +// +// Returns whether the assertion was successful (true) or not (false). +func (a *Assertions) HTTPBodyNotContains(handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return HTTPBodyNotContains(a.t, handler, method, url, values, str, msgAndArgs...) +} + +// HTTPBodyNotContainsf asserts that a specified handler returns a +// body that does not contain a string. +// +// a.HTTPBodyNotContainsf(myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky", "error message %s", "formatted") +// +// Returns whether the assertion was successful (true) or not (false). +func (a *Assertions) HTTPBodyNotContainsf(handler http.HandlerFunc, method string, url string, values url.Values, str interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return HTTPBodyNotContainsf(a.t, handler, method, url, values, str, msg, args...) +} + +// HTTPError asserts that a specified handler returns an error status code. +// +// a.HTTPError(myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}} +// +// Returns whether the assertion was successful (true) or not (false). +func (a *Assertions) HTTPError(handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return HTTPError(a.t, handler, method, url, values, msgAndArgs...) +} + +// HTTPErrorf asserts that a specified handler returns an error status code. +// +// a.HTTPErrorf(myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}} +// +// Returns whether the assertion was successful (true) or not (false). +func (a *Assertions) HTTPErrorf(handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return HTTPErrorf(a.t, handler, method, url, values, msg, args...) +} + +// HTTPRedirect asserts that a specified handler returns a redirect status code. +// +// a.HTTPRedirect(myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}} +// +// Returns whether the assertion was successful (true) or not (false). +func (a *Assertions) HTTPRedirect(handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return HTTPRedirect(a.t, handler, method, url, values, msgAndArgs...) +} + +// HTTPRedirectf asserts that a specified handler returns a redirect status code. +// +// a.HTTPRedirectf(myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}} +// +// Returns whether the assertion was successful (true) or not (false). +func (a *Assertions) HTTPRedirectf(handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return HTTPRedirectf(a.t, handler, method, url, values, msg, args...) +} + +// HTTPStatusCode asserts that a specified handler returns a specified status code. +// +// a.HTTPStatusCode(myHandler, "GET", "/notImplemented", nil, 501) +// +// Returns whether the assertion was successful (true) or not (false). +func (a *Assertions) HTTPStatusCode(handler http.HandlerFunc, method string, url string, values url.Values, statuscode int, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return HTTPStatusCode(a.t, handler, method, url, values, statuscode, msgAndArgs...) +} + +// HTTPStatusCodef asserts that a specified handler returns a specified status code. +// +// a.HTTPStatusCodef(myHandler, "GET", "/notImplemented", nil, 501, "error message %s", "formatted") +// +// Returns whether the assertion was successful (true) or not (false). +func (a *Assertions) HTTPStatusCodef(handler http.HandlerFunc, method string, url string, values url.Values, statuscode int, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return HTTPStatusCodef(a.t, handler, method, url, values, statuscode, msg, args...) +} + +// HTTPSuccess asserts that a specified handler returns a success status code. +// +// a.HTTPSuccess(myHandler, "POST", "http://www.google.com", nil) +// +// Returns whether the assertion was successful (true) or not (false). +func (a *Assertions) HTTPSuccess(handler http.HandlerFunc, method string, url string, values url.Values, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return HTTPSuccess(a.t, handler, method, url, values, msgAndArgs...) +} + +// HTTPSuccessf asserts that a specified handler returns a success status code. +// +// a.HTTPSuccessf(myHandler, "POST", "http://www.google.com", nil, "error message %s", "formatted") +// +// Returns whether the assertion was successful (true) or not (false). +func (a *Assertions) HTTPSuccessf(handler http.HandlerFunc, method string, url string, values url.Values, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return HTTPSuccessf(a.t, handler, method, url, values, msg, args...) +} + +// Implements asserts that an object is implemented by the specified interface. +// +// a.Implements((*MyInterface)(nil), new(MyObject)) +func (a *Assertions) Implements(interfaceObject interface{}, object interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Implements(a.t, interfaceObject, object, msgAndArgs...) +} + +// Implementsf asserts that an object is implemented by the specified interface. +// +// a.Implementsf((*MyInterface)(nil), new(MyObject), "error message %s", "formatted") +func (a *Assertions) Implementsf(interfaceObject interface{}, object interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Implementsf(a.t, interfaceObject, object, msg, args...) +} + +// InDelta asserts that the two numerals are within delta of each other. +// +// a.InDelta(math.Pi, 22/7.0, 0.01) +func (a *Assertions) InDelta(expected interface{}, actual interface{}, delta float64, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return InDelta(a.t, expected, actual, delta, msgAndArgs...) +} + +// InDeltaMapValues is the same as InDelta, but it compares all values between two maps. Both maps must have exactly the same keys. +func (a *Assertions) InDeltaMapValues(expected interface{}, actual interface{}, delta float64, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return InDeltaMapValues(a.t, expected, actual, delta, msgAndArgs...) +} + +// InDeltaMapValuesf is the same as InDelta, but it compares all values between two maps. Both maps must have exactly the same keys. +func (a *Assertions) InDeltaMapValuesf(expected interface{}, actual interface{}, delta float64, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return InDeltaMapValuesf(a.t, expected, actual, delta, msg, args...) +} + +// InDeltaSlice is the same as InDelta, except it compares two slices. +func (a *Assertions) InDeltaSlice(expected interface{}, actual interface{}, delta float64, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return InDeltaSlice(a.t, expected, actual, delta, msgAndArgs...) +} + +// InDeltaSlicef is the same as InDelta, except it compares two slices. +func (a *Assertions) InDeltaSlicef(expected interface{}, actual interface{}, delta float64, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return InDeltaSlicef(a.t, expected, actual, delta, msg, args...) +} + +// InDeltaf asserts that the two numerals are within delta of each other. +// +// a.InDeltaf(math.Pi, 22/7.0, 0.01, "error message %s", "formatted") +func (a *Assertions) InDeltaf(expected interface{}, actual interface{}, delta float64, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return InDeltaf(a.t, expected, actual, delta, msg, args...) +} + +// InEpsilon asserts that expected and actual have a relative error less than epsilon +func (a *Assertions) InEpsilon(expected interface{}, actual interface{}, epsilon float64, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return InEpsilon(a.t, expected, actual, epsilon, msgAndArgs...) +} + +// InEpsilonSlice is the same as InEpsilon, except it compares each value from two slices. +func (a *Assertions) InEpsilonSlice(expected interface{}, actual interface{}, epsilon float64, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return InEpsilonSlice(a.t, expected, actual, epsilon, msgAndArgs...) +} + +// InEpsilonSlicef is the same as InEpsilon, except it compares each value from two slices. +func (a *Assertions) InEpsilonSlicef(expected interface{}, actual interface{}, epsilon float64, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return InEpsilonSlicef(a.t, expected, actual, epsilon, msg, args...) +} + +// InEpsilonf asserts that expected and actual have a relative error less than epsilon +func (a *Assertions) InEpsilonf(expected interface{}, actual interface{}, epsilon float64, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return InEpsilonf(a.t, expected, actual, epsilon, msg, args...) +} + +// IsType asserts that the specified objects are of the same type. +func (a *Assertions) IsType(expectedType interface{}, object interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return IsType(a.t, expectedType, object, msgAndArgs...) +} + +// IsTypef asserts that the specified objects are of the same type. +func (a *Assertions) IsTypef(expectedType interface{}, object interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return IsTypef(a.t, expectedType, object, msg, args...) +} + +// JSONEq asserts that two JSON strings are equivalent. +// +// a.JSONEq(`{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`) +func (a *Assertions) JSONEq(expected string, actual string, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return JSONEq(a.t, expected, actual, msgAndArgs...) +} + +// JSONEqf asserts that two JSON strings are equivalent. +// +// a.JSONEqf(`{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`, "error message %s", "formatted") +func (a *Assertions) JSONEqf(expected string, actual string, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return JSONEqf(a.t, expected, actual, msg, args...) +} + +// Len asserts that the specified object has specific length. +// Len also fails if the object has a type that len() not accept. +// +// a.Len(mySlice, 3) +func (a *Assertions) Len(object interface{}, length int, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Len(a.t, object, length, msgAndArgs...) +} + +// Lenf asserts that the specified object has specific length. +// Lenf also fails if the object has a type that len() not accept. +// +// a.Lenf(mySlice, 3, "error message %s", "formatted") +func (a *Assertions) Lenf(object interface{}, length int, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Lenf(a.t, object, length, msg, args...) +} + +// Less asserts that the first element is less than the second +// +// a.Less(1, 2) +// a.Less(float64(1), float64(2)) +// a.Less("a", "b") +func (a *Assertions) Less(e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Less(a.t, e1, e2, msgAndArgs...) +} + +// LessOrEqual asserts that the first element is less than or equal to the second +// +// a.LessOrEqual(1, 2) +// a.LessOrEqual(2, 2) +// a.LessOrEqual("a", "b") +// a.LessOrEqual("b", "b") +func (a *Assertions) LessOrEqual(e1 interface{}, e2 interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return LessOrEqual(a.t, e1, e2, msgAndArgs...) +} + +// LessOrEqualf asserts that the first element is less than or equal to the second +// +// a.LessOrEqualf(1, 2, "error message %s", "formatted") +// a.LessOrEqualf(2, 2, "error message %s", "formatted") +// a.LessOrEqualf("a", "b", "error message %s", "formatted") +// a.LessOrEqualf("b", "b", "error message %s", "formatted") +func (a *Assertions) LessOrEqualf(e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return LessOrEqualf(a.t, e1, e2, msg, args...) +} + +// Lessf asserts that the first element is less than the second +// +// a.Lessf(1, 2, "error message %s", "formatted") +// a.Lessf(float64(1), float64(2), "error message %s", "formatted") +// a.Lessf("a", "b", "error message %s", "formatted") +func (a *Assertions) Lessf(e1 interface{}, e2 interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Lessf(a.t, e1, e2, msg, args...) +} + +// Never asserts that the given condition doesn't satisfy in waitFor time, +// periodically checking the target function each tick. +// +// a.Never(func() bool { return false; }, time.Second, 10*time.Millisecond) +func (a *Assertions) Never(condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Never(a.t, condition, waitFor, tick, msgAndArgs...) +} + +// Neverf asserts that the given condition doesn't satisfy in waitFor time, +// periodically checking the target function each tick. +// +// a.Neverf(func() bool { return false; }, time.Second, 10*time.Millisecond, "error message %s", "formatted") +func (a *Assertions) Neverf(condition func() bool, waitFor time.Duration, tick time.Duration, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Neverf(a.t, condition, waitFor, tick, msg, args...) +} + +// Nil asserts that the specified object is nil. +// +// a.Nil(err) +func (a *Assertions) Nil(object interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Nil(a.t, object, msgAndArgs...) +} + +// Nilf asserts that the specified object is nil. +// +// a.Nilf(err, "error message %s", "formatted") +func (a *Assertions) Nilf(object interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Nilf(a.t, object, msg, args...) +} + +// NoDirExists checks whether a directory does not exist in the given path. +// It fails if the path points to an existing _directory_ only. +func (a *Assertions) NoDirExists(path string, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NoDirExists(a.t, path, msgAndArgs...) +} + +// NoDirExistsf checks whether a directory does not exist in the given path. +// It fails if the path points to an existing _directory_ only. +func (a *Assertions) NoDirExistsf(path string, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NoDirExistsf(a.t, path, msg, args...) +} + +// NoError asserts that a function returned no error (i.e. `nil`). +// +// actualObj, err := SomeFunction() +// if a.NoError(err) { +// assert.Equal(t, expectedObj, actualObj) +// } +func (a *Assertions) NoError(err error, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NoError(a.t, err, msgAndArgs...) +} + +// NoErrorf asserts that a function returned no error (i.e. `nil`). +// +// actualObj, err := SomeFunction() +// if a.NoErrorf(err, "error message %s", "formatted") { +// assert.Equal(t, expectedObj, actualObj) +// } +func (a *Assertions) NoErrorf(err error, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NoErrorf(a.t, err, msg, args...) +} + +// NoFileExists checks whether a file does not exist in a given path. It fails +// if the path points to an existing _file_ only. +func (a *Assertions) NoFileExists(path string, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NoFileExists(a.t, path, msgAndArgs...) +} + +// NoFileExistsf checks whether a file does not exist in a given path. It fails +// if the path points to an existing _file_ only. +func (a *Assertions) NoFileExistsf(path string, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NoFileExistsf(a.t, path, msg, args...) +} + +// NotContains asserts that the specified string, list(array, slice...) or map does NOT contain the +// specified substring or element. +// +// a.NotContains("Hello World", "Earth") +// a.NotContains(["Hello", "World"], "Earth") +// a.NotContains({"Hello": "World"}, "Earth") +func (a *Assertions) NotContains(s interface{}, contains interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotContains(a.t, s, contains, msgAndArgs...) +} + +// NotContainsf asserts that the specified string, list(array, slice...) or map does NOT contain the +// specified substring or element. +// +// a.NotContainsf("Hello World", "Earth", "error message %s", "formatted") +// a.NotContainsf(["Hello", "World"], "Earth", "error message %s", "formatted") +// a.NotContainsf({"Hello": "World"}, "Earth", "error message %s", "formatted") +func (a *Assertions) NotContainsf(s interface{}, contains interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotContainsf(a.t, s, contains, msg, args...) +} + +// NotEmpty asserts that the specified object is NOT empty. I.e. not nil, "", false, 0 or either +// a slice or a channel with len == 0. +// +// if a.NotEmpty(obj) { +// assert.Equal(t, "two", obj[1]) +// } +func (a *Assertions) NotEmpty(object interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotEmpty(a.t, object, msgAndArgs...) +} + +// NotEmptyf asserts that the specified object is NOT empty. I.e. not nil, "", false, 0 or either +// a slice or a channel with len == 0. +// +// if a.NotEmptyf(obj, "error message %s", "formatted") { +// assert.Equal(t, "two", obj[1]) +// } +func (a *Assertions) NotEmptyf(object interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotEmptyf(a.t, object, msg, args...) +} + +// NotEqual asserts that the specified values are NOT equal. +// +// a.NotEqual(obj1, obj2) +// +// Pointer variable equality is determined based on the equality of the +// referenced values (as opposed to the memory addresses). +func (a *Assertions) NotEqual(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotEqual(a.t, expected, actual, msgAndArgs...) +} + +// NotEqualValues asserts that two objects are not equal even when converted to the same type +// +// a.NotEqualValues(obj1, obj2) +func (a *Assertions) NotEqualValues(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotEqualValues(a.t, expected, actual, msgAndArgs...) +} + +// NotEqualValuesf asserts that two objects are not equal even when converted to the same type +// +// a.NotEqualValuesf(obj1, obj2, "error message %s", "formatted") +func (a *Assertions) NotEqualValuesf(expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotEqualValuesf(a.t, expected, actual, msg, args...) +} + +// NotEqualf asserts that the specified values are NOT equal. +// +// a.NotEqualf(obj1, obj2, "error message %s", "formatted") +// +// Pointer variable equality is determined based on the equality of the +// referenced values (as opposed to the memory addresses). +func (a *Assertions) NotEqualf(expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotEqualf(a.t, expected, actual, msg, args...) +} + +// NotNil asserts that the specified object is not nil. +// +// a.NotNil(err) +func (a *Assertions) NotNil(object interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotNil(a.t, object, msgAndArgs...) +} + +// NotNilf asserts that the specified object is not nil. +// +// a.NotNilf(err, "error message %s", "formatted") +func (a *Assertions) NotNilf(object interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotNilf(a.t, object, msg, args...) +} + +// NotPanics asserts that the code inside the specified PanicTestFunc does NOT panic. +// +// a.NotPanics(func(){ RemainCalm() }) +func (a *Assertions) NotPanics(f PanicTestFunc, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotPanics(a.t, f, msgAndArgs...) +} + +// NotPanicsf asserts that the code inside the specified PanicTestFunc does NOT panic. +// +// a.NotPanicsf(func(){ RemainCalm() }, "error message %s", "formatted") +func (a *Assertions) NotPanicsf(f PanicTestFunc, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotPanicsf(a.t, f, msg, args...) +} + +// NotRegexp asserts that a specified regexp does not match a string. +// +// a.NotRegexp(regexp.MustCompile("starts"), "it's starting") +// a.NotRegexp("^start", "it's not starting") +func (a *Assertions) NotRegexp(rx interface{}, str interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotRegexp(a.t, rx, str, msgAndArgs...) +} + +// NotRegexpf asserts that a specified regexp does not match a string. +// +// a.NotRegexpf(regexp.MustCompile("starts"), "it's starting", "error message %s", "formatted") +// a.NotRegexpf("^start", "it's not starting", "error message %s", "formatted") +func (a *Assertions) NotRegexpf(rx interface{}, str interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotRegexpf(a.t, rx, str, msg, args...) +} + +// NotSame asserts that two pointers do not reference the same object. +// +// a.NotSame(ptr1, ptr2) +// +// Both arguments must be pointer variables. Pointer variable sameness is +// determined based on the equality of both type and value. +func (a *Assertions) NotSame(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotSame(a.t, expected, actual, msgAndArgs...) +} + +// NotSamef asserts that two pointers do not reference the same object. +// +// a.NotSamef(ptr1, ptr2, "error message %s", "formatted") +// +// Both arguments must be pointer variables. Pointer variable sameness is +// determined based on the equality of both type and value. +func (a *Assertions) NotSamef(expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotSamef(a.t, expected, actual, msg, args...) +} + +// NotSubset asserts that the specified list(array, slice...) contains not all +// elements given in the specified subset(array, slice...). +// +// a.NotSubset([1, 3, 4], [1, 2], "But [1, 3, 4] does not contain [1, 2]") +func (a *Assertions) NotSubset(list interface{}, subset interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotSubset(a.t, list, subset, msgAndArgs...) +} + +// NotSubsetf asserts that the specified list(array, slice...) contains not all +// elements given in the specified subset(array, slice...). +// +// a.NotSubsetf([1, 3, 4], [1, 2], "But [1, 3, 4] does not contain [1, 2]", "error message %s", "formatted") +func (a *Assertions) NotSubsetf(list interface{}, subset interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotSubsetf(a.t, list, subset, msg, args...) +} + +// NotZero asserts that i is not the zero value for its type. +func (a *Assertions) NotZero(i interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotZero(a.t, i, msgAndArgs...) +} + +// NotZerof asserts that i is not the zero value for its type. +func (a *Assertions) NotZerof(i interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return NotZerof(a.t, i, msg, args...) +} + +// Panics asserts that the code inside the specified PanicTestFunc panics. +// +// a.Panics(func(){ GoCrazy() }) +func (a *Assertions) Panics(f PanicTestFunc, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Panics(a.t, f, msgAndArgs...) +} + +// PanicsWithError asserts that the code inside the specified PanicTestFunc +// panics, and that the recovered panic value is an error that satisfies the +// EqualError comparison. +// +// a.PanicsWithError("crazy error", func(){ GoCrazy() }) +func (a *Assertions) PanicsWithError(errString string, f PanicTestFunc, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return PanicsWithError(a.t, errString, f, msgAndArgs...) +} + +// PanicsWithErrorf asserts that the code inside the specified PanicTestFunc +// panics, and that the recovered panic value is an error that satisfies the +// EqualError comparison. +// +// a.PanicsWithErrorf("crazy error", func(){ GoCrazy() }, "error message %s", "formatted") +func (a *Assertions) PanicsWithErrorf(errString string, f PanicTestFunc, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return PanicsWithErrorf(a.t, errString, f, msg, args...) +} + +// PanicsWithValue asserts that the code inside the specified PanicTestFunc panics, and that +// the recovered panic value equals the expected panic value. +// +// a.PanicsWithValue("crazy error", func(){ GoCrazy() }) +func (a *Assertions) PanicsWithValue(expected interface{}, f PanicTestFunc, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return PanicsWithValue(a.t, expected, f, msgAndArgs...) +} + +// PanicsWithValuef asserts that the code inside the specified PanicTestFunc panics, and that +// the recovered panic value equals the expected panic value. +// +// a.PanicsWithValuef("crazy error", func(){ GoCrazy() }, "error message %s", "formatted") +func (a *Assertions) PanicsWithValuef(expected interface{}, f PanicTestFunc, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return PanicsWithValuef(a.t, expected, f, msg, args...) +} + +// Panicsf asserts that the code inside the specified PanicTestFunc panics. +// +// a.Panicsf(func(){ GoCrazy() }, "error message %s", "formatted") +func (a *Assertions) Panicsf(f PanicTestFunc, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Panicsf(a.t, f, msg, args...) +} + +// Regexp asserts that a specified regexp matches a string. +// +// a.Regexp(regexp.MustCompile("start"), "it's starting") +// a.Regexp("start...$", "it's not starting") +func (a *Assertions) Regexp(rx interface{}, str interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Regexp(a.t, rx, str, msgAndArgs...) +} + +// Regexpf asserts that a specified regexp matches a string. +// +// a.Regexpf(regexp.MustCompile("start"), "it's starting", "error message %s", "formatted") +// a.Regexpf("start...$", "it's not starting", "error message %s", "formatted") +func (a *Assertions) Regexpf(rx interface{}, str interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Regexpf(a.t, rx, str, msg, args...) +} + +// Same asserts that two pointers reference the same object. +// +// a.Same(ptr1, ptr2) +// +// Both arguments must be pointer variables. Pointer variable sameness is +// determined based on the equality of both type and value. +func (a *Assertions) Same(expected interface{}, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Same(a.t, expected, actual, msgAndArgs...) +} + +// Samef asserts that two pointers reference the same object. +// +// a.Samef(ptr1, ptr2, "error message %s", "formatted") +// +// Both arguments must be pointer variables. Pointer variable sameness is +// determined based on the equality of both type and value. +func (a *Assertions) Samef(expected interface{}, actual interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Samef(a.t, expected, actual, msg, args...) +} + +// Subset asserts that the specified list(array, slice...) contains all +// elements given in the specified subset(array, slice...). +// +// a.Subset([1, 2, 3], [1, 2], "But [1, 2, 3] does contain [1, 2]") +func (a *Assertions) Subset(list interface{}, subset interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Subset(a.t, list, subset, msgAndArgs...) +} + +// Subsetf asserts that the specified list(array, slice...) contains all +// elements given in the specified subset(array, slice...). +// +// a.Subsetf([1, 2, 3], [1, 2], "But [1, 2, 3] does contain [1, 2]", "error message %s", "formatted") +func (a *Assertions) Subsetf(list interface{}, subset interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Subsetf(a.t, list, subset, msg, args...) +} + +// True asserts that the specified value is true. +// +// a.True(myBool) +func (a *Assertions) True(value bool, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return True(a.t, value, msgAndArgs...) +} + +// Truef asserts that the specified value is true. +// +// a.Truef(myBool, "error message %s", "formatted") +func (a *Assertions) Truef(value bool, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Truef(a.t, value, msg, args...) +} + +// WithinDuration asserts that the two times are within duration delta of each other. +// +// a.WithinDuration(time.Now(), time.Now(), 10*time.Second) +func (a *Assertions) WithinDuration(expected time.Time, actual time.Time, delta time.Duration, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return WithinDuration(a.t, expected, actual, delta, msgAndArgs...) +} + +// WithinDurationf asserts that the two times are within duration delta of each other. +// +// a.WithinDurationf(time.Now(), time.Now(), 10*time.Second, "error message %s", "formatted") +func (a *Assertions) WithinDurationf(expected time.Time, actual time.Time, delta time.Duration, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return WithinDurationf(a.t, expected, actual, delta, msg, args...) +} + +// YAMLEq asserts that two YAML strings are equivalent. +func (a *Assertions) YAMLEq(expected string, actual string, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return YAMLEq(a.t, expected, actual, msgAndArgs...) +} + +// YAMLEqf asserts that two YAML strings are equivalent. +func (a *Assertions) YAMLEqf(expected string, actual string, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return YAMLEqf(a.t, expected, actual, msg, args...) +} + +// Zero asserts that i is the zero value for its type. +func (a *Assertions) Zero(i interface{}, msgAndArgs ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Zero(a.t, i, msgAndArgs...) +} + +// Zerof asserts that i is the zero value for its type. +func (a *Assertions) Zerof(i interface{}, msg string, args ...interface{}) bool { + if h, ok := a.t.(tHelper); ok { + h.Helper() + } + return Zerof(a.t, i, msg, args...) +} diff --git a/vendor/github.com/stretchr/testify/assert/assertion_forward.go.tmpl b/vendor/github.com/stretchr/testify/assert/assertion_forward.go.tmpl new file mode 100644 index 0000000000..188bb9e174 --- /dev/null +++ b/vendor/github.com/stretchr/testify/assert/assertion_forward.go.tmpl @@ -0,0 +1,5 @@ +{{.CommentWithoutT "a"}} +func (a *Assertions) {{.DocInfo.Name}}({{.Params}}) bool { + if h, ok := a.t.(tHelper); ok { h.Helper() } + return {{.DocInfo.Name}}(a.t, {{.ForwardedParams}}) +} diff --git a/vendor/github.com/stretchr/testify/assert/assertions.go b/vendor/github.com/stretchr/testify/assert/assertions.go new file mode 100644 index 0000000000..914a10d83a --- /dev/null +++ b/vendor/github.com/stretchr/testify/assert/assertions.go @@ -0,0 +1,1695 @@ +package assert + +import ( + "bufio" + "bytes" + "encoding/json" + "errors" + "fmt" + "math" + "os" + "reflect" + "regexp" + "runtime" + "runtime/debug" + "strings" + "time" + "unicode" + "unicode/utf8" + + "github.com/davecgh/go-spew/spew" + "github.com/pmezard/go-difflib/difflib" + yaml "gopkg.in/yaml.v3" +) + +//go:generate sh -c "cd ../_codegen && go build && cd - && ../_codegen/_codegen -output-package=assert -template=assertion_format.go.tmpl" + +// TestingT is an interface wrapper around *testing.T +type TestingT interface { + Errorf(format string, args ...interface{}) +} + +// ComparisonAssertionFunc is a common function prototype when comparing two values. Can be useful +// for table driven tests. +type ComparisonAssertionFunc func(TestingT, interface{}, interface{}, ...interface{}) bool + +// ValueAssertionFunc is a common function prototype when validating a single value. Can be useful +// for table driven tests. +type ValueAssertionFunc func(TestingT, interface{}, ...interface{}) bool + +// BoolAssertionFunc is a common function prototype when validating a bool value. Can be useful +// for table driven tests. +type BoolAssertionFunc func(TestingT, bool, ...interface{}) bool + +// ErrorAssertionFunc is a common function prototype when validating an error value. Can be useful +// for table driven tests. +type ErrorAssertionFunc func(TestingT, error, ...interface{}) bool + +// Comparison is a custom function that returns true on success and false on failure +type Comparison func() (success bool) + +/* + Helper functions +*/ + +// ObjectsAreEqual determines if two objects are considered equal. +// +// This function does no assertion of any kind. +func ObjectsAreEqual(expected, actual interface{}) bool { + if expected == nil || actual == nil { + return expected == actual + } + + exp, ok := expected.([]byte) + if !ok { + return reflect.DeepEqual(expected, actual) + } + + act, ok := actual.([]byte) + if !ok { + return false + } + if exp == nil || act == nil { + return exp == nil && act == nil + } + return bytes.Equal(exp, act) +} + +// ObjectsAreEqualValues gets whether two objects are equal, or if their +// values are equal. +func ObjectsAreEqualValues(expected, actual interface{}) bool { + if ObjectsAreEqual(expected, actual) { + return true + } + + actualType := reflect.TypeOf(actual) + if actualType == nil { + return false + } + expectedValue := reflect.ValueOf(expected) + if expectedValue.IsValid() && expectedValue.Type().ConvertibleTo(actualType) { + // Attempt comparison after type conversion + return reflect.DeepEqual(expectedValue.Convert(actualType).Interface(), actual) + } + + return false +} + +/* CallerInfo is necessary because the assert functions use the testing object +internally, causing it to print the file:line of the assert method, rather than where +the problem actually occurred in calling code.*/ + +// CallerInfo returns an array of strings containing the file and line number +// of each stack frame leading from the current test to the assert call that +// failed. +func CallerInfo() []string { + + var pc uintptr + var ok bool + var file string + var line int + var name string + + callers := []string{} + for i := 0; ; i++ { + pc, file, line, ok = runtime.Caller(i) + if !ok { + // The breaks below failed to terminate the loop, and we ran off the + // end of the call stack. + break + } + + // This is a huge edge case, but it will panic if this is the case, see #180 + if file == "" { + break + } + + f := runtime.FuncForPC(pc) + if f == nil { + break + } + name = f.Name() + + // testing.tRunner is the standard library function that calls + // tests. Subtests are called directly by tRunner, without going through + // the Test/Benchmark/Example function that contains the t.Run calls, so + // with subtests we should break when we hit tRunner, without adding it + // to the list of callers. + if name == "testing.tRunner" { + break + } + + parts := strings.Split(file, "/") + file = parts[len(parts)-1] + if len(parts) > 1 { + dir := parts[len(parts)-2] + if (dir != "assert" && dir != "mock" && dir != "require") || file == "mock_test.go" { + callers = append(callers, fmt.Sprintf("%s:%d", file, line)) + } + } + + // Drop the package + segments := strings.Split(name, ".") + name = segments[len(segments)-1] + if isTest(name, "Test") || + isTest(name, "Benchmark") || + isTest(name, "Example") { + break + } + } + + return callers +} + +// Stolen from the `go test` tool. +// isTest tells whether name looks like a test (or benchmark, according to prefix). +// It is a Test (say) if there is a character after Test that is not a lower-case letter. +// We don't want TesticularCancer. +func isTest(name, prefix string) bool { + if !strings.HasPrefix(name, prefix) { + return false + } + if len(name) == len(prefix) { // "Test" is ok + return true + } + rune, _ := utf8.DecodeRuneInString(name[len(prefix):]) + return !unicode.IsLower(rune) +} + +func messageFromMsgAndArgs(msgAndArgs ...interface{}) string { + if len(msgAndArgs) == 0 || msgAndArgs == nil { + return "" + } + if len(msgAndArgs) == 1 { + msg := msgAndArgs[0] + if msgAsStr, ok := msg.(string); ok { + return msgAsStr + } + return fmt.Sprintf("%+v", msg) + } + if len(msgAndArgs) > 1 { + return fmt.Sprintf(msgAndArgs[0].(string), msgAndArgs[1:]...) + } + return "" +} + +// Aligns the provided message so that all lines after the first line start at the same location as the first line. +// Assumes that the first line starts at the correct location (after carriage return, tab, label, spacer and tab). +// The longestLabelLen parameter specifies the length of the longest label in the output (required becaues this is the +// basis on which the alignment occurs). +func indentMessageLines(message string, longestLabelLen int) string { + outBuf := new(bytes.Buffer) + + for i, scanner := 0, bufio.NewScanner(strings.NewReader(message)); scanner.Scan(); i++ { + // no need to align first line because it starts at the correct location (after the label) + if i != 0 { + // append alignLen+1 spaces to align with "{{longestLabel}}:" before adding tab + outBuf.WriteString("\n\t" + strings.Repeat(" ", longestLabelLen+1) + "\t") + } + outBuf.WriteString(scanner.Text()) + } + + return outBuf.String() +} + +type failNower interface { + FailNow() +} + +// FailNow fails test +func FailNow(t TestingT, failureMessage string, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + Fail(t, failureMessage, msgAndArgs...) + + // We cannot extend TestingT with FailNow() and + // maintain backwards compatibility, so we fallback + // to panicking when FailNow is not available in + // TestingT. + // See issue #263 + + if t, ok := t.(failNower); ok { + t.FailNow() + } else { + panic("test failed and t is missing `FailNow()`") + } + return false +} + +// Fail reports a failure through +func Fail(t TestingT, failureMessage string, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + content := []labeledContent{ + {"Error Trace", strings.Join(CallerInfo(), "\n\t\t\t")}, + {"Error", failureMessage}, + } + + // Add test name if the Go version supports it + if n, ok := t.(interface { + Name() string + }); ok { + content = append(content, labeledContent{"Test", n.Name()}) + } + + message := messageFromMsgAndArgs(msgAndArgs...) + if len(message) > 0 { + content = append(content, labeledContent{"Messages", message}) + } + + t.Errorf("\n%s", ""+labeledOutput(content...)) + + return false +} + +type labeledContent struct { + label string + content string +} + +// labeledOutput returns a string consisting of the provided labeledContent. Each labeled output is appended in the following manner: +// +// \t{{label}}:{{align_spaces}}\t{{content}}\n +// +// The initial carriage return is required to undo/erase any padding added by testing.T.Errorf. The "\t{{label}}:" is for the label. +// If a label is shorter than the longest label provided, padding spaces are added to make all the labels match in length. Once this +// alignment is achieved, "\t{{content}}\n" is added for the output. +// +// If the content of the labeledOutput contains line breaks, the subsequent lines are aligned so that they start at the same location as the first line. +func labeledOutput(content ...labeledContent) string { + longestLabel := 0 + for _, v := range content { + if len(v.label) > longestLabel { + longestLabel = len(v.label) + } + } + var output string + for _, v := range content { + output += "\t" + v.label + ":" + strings.Repeat(" ", longestLabel-len(v.label)) + "\t" + indentMessageLines(v.content, longestLabel) + "\n" + } + return output +} + +// Implements asserts that an object is implemented by the specified interface. +// +// assert.Implements(t, (*MyInterface)(nil), new(MyObject)) +func Implements(t TestingT, interfaceObject interface{}, object interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + interfaceType := reflect.TypeOf(interfaceObject).Elem() + + if object == nil { + return Fail(t, fmt.Sprintf("Cannot check if nil implements %v", interfaceType), msgAndArgs...) + } + if !reflect.TypeOf(object).Implements(interfaceType) { + return Fail(t, fmt.Sprintf("%T must implement %v", object, interfaceType), msgAndArgs...) + } + + return true +} + +// IsType asserts that the specified objects are of the same type. +func IsType(t TestingT, expectedType interface{}, object interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + if !ObjectsAreEqual(reflect.TypeOf(object), reflect.TypeOf(expectedType)) { + return Fail(t, fmt.Sprintf("Object expected to be of type %v, but was %v", reflect.TypeOf(expectedType), reflect.TypeOf(object)), msgAndArgs...) + } + + return true +} + +// Equal asserts that two objects are equal. +// +// assert.Equal(t, 123, 123) +// +// Pointer variable equality is determined based on the equality of the +// referenced values (as opposed to the memory addresses). Function equality +// cannot be determined and will always fail. +func Equal(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if err := validateEqualArgs(expected, actual); err != nil { + return Fail(t, fmt.Sprintf("Invalid operation: %#v == %#v (%s)", + expected, actual, err), msgAndArgs...) + } + + if !ObjectsAreEqual(expected, actual) { + diff := diff(expected, actual) + expected, actual = formatUnequalValues(expected, actual) + return Fail(t, fmt.Sprintf("Not equal: \n"+ + "expected: %s\n"+ + "actual : %s%s", expected, actual, diff), msgAndArgs...) + } + + return true + +} + +// validateEqualArgs checks whether provided arguments can be safely used in the +// Equal/NotEqual functions. +func validateEqualArgs(expected, actual interface{}) error { + if expected == nil && actual == nil { + return nil + } + + if isFunction(expected) || isFunction(actual) { + return errors.New("cannot take func type as argument") + } + return nil +} + +// Same asserts that two pointers reference the same object. +// +// assert.Same(t, ptr1, ptr2) +// +// Both arguments must be pointer variables. Pointer variable sameness is +// determined based on the equality of both type and value. +func Same(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + if !samePointers(expected, actual) { + return Fail(t, fmt.Sprintf("Not same: \n"+ + "expected: %p %#v\n"+ + "actual : %p %#v", expected, expected, actual, actual), msgAndArgs...) + } + + return true +} + +// NotSame asserts that two pointers do not reference the same object. +// +// assert.NotSame(t, ptr1, ptr2) +// +// Both arguments must be pointer variables. Pointer variable sameness is +// determined based on the equality of both type and value. +func NotSame(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + if samePointers(expected, actual) { + return Fail(t, fmt.Sprintf( + "Expected and actual point to the same object: %p %#v", + expected, expected), msgAndArgs...) + } + return true +} + +// samePointers compares two generic interface objects and returns whether +// they point to the same object +func samePointers(first, second interface{}) bool { + firstPtr, secondPtr := reflect.ValueOf(first), reflect.ValueOf(second) + if firstPtr.Kind() != reflect.Ptr || secondPtr.Kind() != reflect.Ptr { + return false + } + + firstType, secondType := reflect.TypeOf(first), reflect.TypeOf(second) + if firstType != secondType { + return false + } + + // compare pointer addresses + return first == second +} + +// formatUnequalValues takes two values of arbitrary types and returns string +// representations appropriate to be presented to the user. +// +// If the values are not of like type, the returned strings will be prefixed +// with the type name, and the value will be enclosed in parenthesis similar +// to a type conversion in the Go grammar. +func formatUnequalValues(expected, actual interface{}) (e string, a string) { + if reflect.TypeOf(expected) != reflect.TypeOf(actual) { + return fmt.Sprintf("%T(%s)", expected, truncatingFormat(expected)), + fmt.Sprintf("%T(%s)", actual, truncatingFormat(actual)) + } + switch expected.(type) { + case time.Duration: + return fmt.Sprintf("%v", expected), fmt.Sprintf("%v", actual) + } + return truncatingFormat(expected), truncatingFormat(actual) +} + +// truncatingFormat formats the data and truncates it if it's too long. +// +// This helps keep formatted error messages lines from exceeding the +// bufio.MaxScanTokenSize max line length that the go testing framework imposes. +func truncatingFormat(data interface{}) string { + value := fmt.Sprintf("%#v", data) + max := bufio.MaxScanTokenSize - 100 // Give us some space the type info too if needed. + if len(value) > max { + value = value[0:max] + "<... truncated>" + } + return value +} + +// EqualValues asserts that two objects are equal or convertable to the same types +// and equal. +// +// assert.EqualValues(t, uint32(123), int32(123)) +func EqualValues(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + if !ObjectsAreEqualValues(expected, actual) { + diff := diff(expected, actual) + expected, actual = formatUnequalValues(expected, actual) + return Fail(t, fmt.Sprintf("Not equal: \n"+ + "expected: %s\n"+ + "actual : %s%s", expected, actual, diff), msgAndArgs...) + } + + return true + +} + +// Exactly asserts that two objects are equal in value and type. +// +// assert.Exactly(t, int32(123), int64(123)) +func Exactly(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + aType := reflect.TypeOf(expected) + bType := reflect.TypeOf(actual) + + if aType != bType { + return Fail(t, fmt.Sprintf("Types expected to match exactly\n\t%v != %v", aType, bType), msgAndArgs...) + } + + return Equal(t, expected, actual, msgAndArgs...) + +} + +// NotNil asserts that the specified object is not nil. +// +// assert.NotNil(t, err) +func NotNil(t TestingT, object interface{}, msgAndArgs ...interface{}) bool { + if !isNil(object) { + return true + } + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Fail(t, "Expected value not to be nil.", msgAndArgs...) +} + +// containsKind checks if a specified kind in the slice of kinds. +func containsKind(kinds []reflect.Kind, kind reflect.Kind) bool { + for i := 0; i < len(kinds); i++ { + if kind == kinds[i] { + return true + } + } + + return false +} + +// isNil checks if a specified object is nil or not, without Failing. +func isNil(object interface{}) bool { + if object == nil { + return true + } + + value := reflect.ValueOf(object) + kind := value.Kind() + isNilableKind := containsKind( + []reflect.Kind{ + reflect.Chan, reflect.Func, + reflect.Interface, reflect.Map, + reflect.Ptr, reflect.Slice}, + kind) + + if isNilableKind && value.IsNil() { + return true + } + + return false +} + +// Nil asserts that the specified object is nil. +// +// assert.Nil(t, err) +func Nil(t TestingT, object interface{}, msgAndArgs ...interface{}) bool { + if isNil(object) { + return true + } + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Fail(t, fmt.Sprintf("Expected nil, but got: %#v", object), msgAndArgs...) +} + +// isEmpty gets whether the specified object is considered empty or not. +func isEmpty(object interface{}) bool { + + // get nil case out of the way + if object == nil { + return true + } + + objValue := reflect.ValueOf(object) + + switch objValue.Kind() { + // collection types are empty when they have no element + case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice: + return objValue.Len() == 0 + // pointers are empty if nil or if the value they point to is empty + case reflect.Ptr: + if objValue.IsNil() { + return true + } + deref := objValue.Elem().Interface() + return isEmpty(deref) + // for all other types, compare against the zero value + default: + zero := reflect.Zero(objValue.Type()) + return reflect.DeepEqual(object, zero.Interface()) + } +} + +// Empty asserts that the specified object is empty. I.e. nil, "", false, 0 or either +// a slice or a channel with len == 0. +// +// assert.Empty(t, obj) +func Empty(t TestingT, object interface{}, msgAndArgs ...interface{}) bool { + pass := isEmpty(object) + if !pass { + if h, ok := t.(tHelper); ok { + h.Helper() + } + Fail(t, fmt.Sprintf("Should be empty, but was %v", object), msgAndArgs...) + } + + return pass + +} + +// NotEmpty asserts that the specified object is NOT empty. I.e. not nil, "", false, 0 or either +// a slice or a channel with len == 0. +// +// if assert.NotEmpty(t, obj) { +// assert.Equal(t, "two", obj[1]) +// } +func NotEmpty(t TestingT, object interface{}, msgAndArgs ...interface{}) bool { + pass := !isEmpty(object) + if !pass { + if h, ok := t.(tHelper); ok { + h.Helper() + } + Fail(t, fmt.Sprintf("Should NOT be empty, but was %v", object), msgAndArgs...) + } + + return pass + +} + +// getLen try to get length of object. +// return (false, 0) if impossible. +func getLen(x interface{}) (ok bool, length int) { + v := reflect.ValueOf(x) + defer func() { + if e := recover(); e != nil { + ok = false + } + }() + return true, v.Len() +} + +// Len asserts that the specified object has specific length. +// Len also fails if the object has a type that len() not accept. +// +// assert.Len(t, mySlice, 3) +func Len(t TestingT, object interface{}, length int, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + ok, l := getLen(object) + if !ok { + return Fail(t, fmt.Sprintf("\"%s\" could not be applied builtin len()", object), msgAndArgs...) + } + + if l != length { + return Fail(t, fmt.Sprintf("\"%s\" should have %d item(s), but has %d", object, length, l), msgAndArgs...) + } + return true +} + +// True asserts that the specified value is true. +// +// assert.True(t, myBool) +func True(t TestingT, value bool, msgAndArgs ...interface{}) bool { + if !value { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Fail(t, "Should be true", msgAndArgs...) + } + + return true + +} + +// False asserts that the specified value is false. +// +// assert.False(t, myBool) +func False(t TestingT, value bool, msgAndArgs ...interface{}) bool { + if value { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Fail(t, "Should be false", msgAndArgs...) + } + + return true + +} + +// NotEqual asserts that the specified values are NOT equal. +// +// assert.NotEqual(t, obj1, obj2) +// +// Pointer variable equality is determined based on the equality of the +// referenced values (as opposed to the memory addresses). +func NotEqual(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if err := validateEqualArgs(expected, actual); err != nil { + return Fail(t, fmt.Sprintf("Invalid operation: %#v != %#v (%s)", + expected, actual, err), msgAndArgs...) + } + + if ObjectsAreEqual(expected, actual) { + return Fail(t, fmt.Sprintf("Should not be: %#v\n", actual), msgAndArgs...) + } + + return true + +} + +// NotEqualValues asserts that two objects are not equal even when converted to the same type +// +// assert.NotEqualValues(t, obj1, obj2) +func NotEqualValues(t TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + if ObjectsAreEqualValues(expected, actual) { + return Fail(t, fmt.Sprintf("Should not be: %#v\n", actual), msgAndArgs...) + } + + return true +} + +// containsElement try loop over the list check if the list includes the element. +// return (false, false) if impossible. +// return (true, false) if element was not found. +// return (true, true) if element was found. +func includeElement(list interface{}, element interface{}) (ok, found bool) { + + listValue := reflect.ValueOf(list) + listKind := reflect.TypeOf(list).Kind() + defer func() { + if e := recover(); e != nil { + ok = false + found = false + } + }() + + if listKind == reflect.String { + elementValue := reflect.ValueOf(element) + return true, strings.Contains(listValue.String(), elementValue.String()) + } + + if listKind == reflect.Map { + mapKeys := listValue.MapKeys() + for i := 0; i < len(mapKeys); i++ { + if ObjectsAreEqual(mapKeys[i].Interface(), element) { + return true, true + } + } + return true, false + } + + for i := 0; i < listValue.Len(); i++ { + if ObjectsAreEqual(listValue.Index(i).Interface(), element) { + return true, true + } + } + return true, false + +} + +// Contains asserts that the specified string, list(array, slice...) or map contains the +// specified substring or element. +// +// assert.Contains(t, "Hello World", "World") +// assert.Contains(t, ["Hello", "World"], "World") +// assert.Contains(t, {"Hello": "World"}, "Hello") +func Contains(t TestingT, s, contains interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + ok, found := includeElement(s, contains) + if !ok { + return Fail(t, fmt.Sprintf("%#v could not be applied builtin len()", s), msgAndArgs...) + } + if !found { + return Fail(t, fmt.Sprintf("%#v does not contain %#v", s, contains), msgAndArgs...) + } + + return true + +} + +// NotContains asserts that the specified string, list(array, slice...) or map does NOT contain the +// specified substring or element. +// +// assert.NotContains(t, "Hello World", "Earth") +// assert.NotContains(t, ["Hello", "World"], "Earth") +// assert.NotContains(t, {"Hello": "World"}, "Earth") +func NotContains(t TestingT, s, contains interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + ok, found := includeElement(s, contains) + if !ok { + return Fail(t, fmt.Sprintf("\"%s\" could not be applied builtin len()", s), msgAndArgs...) + } + if found { + return Fail(t, fmt.Sprintf("\"%s\" should not contain \"%s\"", s, contains), msgAndArgs...) + } + + return true + +} + +// Subset asserts that the specified list(array, slice...) contains all +// elements given in the specified subset(array, slice...). +// +// assert.Subset(t, [1, 2, 3], [1, 2], "But [1, 2, 3] does contain [1, 2]") +func Subset(t TestingT, list, subset interface{}, msgAndArgs ...interface{}) (ok bool) { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if subset == nil { + return true // we consider nil to be equal to the nil set + } + + subsetValue := reflect.ValueOf(subset) + defer func() { + if e := recover(); e != nil { + ok = false + } + }() + + listKind := reflect.TypeOf(list).Kind() + subsetKind := reflect.TypeOf(subset).Kind() + + if listKind != reflect.Array && listKind != reflect.Slice { + return Fail(t, fmt.Sprintf("%q has an unsupported type %s", list, listKind), msgAndArgs...) + } + + if subsetKind != reflect.Array && subsetKind != reflect.Slice { + return Fail(t, fmt.Sprintf("%q has an unsupported type %s", subset, subsetKind), msgAndArgs...) + } + + for i := 0; i < subsetValue.Len(); i++ { + element := subsetValue.Index(i).Interface() + ok, found := includeElement(list, element) + if !ok { + return Fail(t, fmt.Sprintf("\"%s\" could not be applied builtin len()", list), msgAndArgs...) + } + if !found { + return Fail(t, fmt.Sprintf("\"%s\" does not contain \"%s\"", list, element), msgAndArgs...) + } + } + + return true +} + +// NotSubset asserts that the specified list(array, slice...) contains not all +// elements given in the specified subset(array, slice...). +// +// assert.NotSubset(t, [1, 3, 4], [1, 2], "But [1, 3, 4] does not contain [1, 2]") +func NotSubset(t TestingT, list, subset interface{}, msgAndArgs ...interface{}) (ok bool) { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if subset == nil { + return Fail(t, fmt.Sprintf("nil is the empty set which is a subset of every set"), msgAndArgs...) + } + + subsetValue := reflect.ValueOf(subset) + defer func() { + if e := recover(); e != nil { + ok = false + } + }() + + listKind := reflect.TypeOf(list).Kind() + subsetKind := reflect.TypeOf(subset).Kind() + + if listKind != reflect.Array && listKind != reflect.Slice { + return Fail(t, fmt.Sprintf("%q has an unsupported type %s", list, listKind), msgAndArgs...) + } + + if subsetKind != reflect.Array && subsetKind != reflect.Slice { + return Fail(t, fmt.Sprintf("%q has an unsupported type %s", subset, subsetKind), msgAndArgs...) + } + + for i := 0; i < subsetValue.Len(); i++ { + element := subsetValue.Index(i).Interface() + ok, found := includeElement(list, element) + if !ok { + return Fail(t, fmt.Sprintf("\"%s\" could not be applied builtin len()", list), msgAndArgs...) + } + if !found { + return true + } + } + + return Fail(t, fmt.Sprintf("%q is a subset of %q", subset, list), msgAndArgs...) +} + +// ElementsMatch asserts that the specified listA(array, slice...) is equal to specified +// listB(array, slice...) ignoring the order of the elements. If there are duplicate elements, +// the number of appearances of each of them in both lists should match. +// +// assert.ElementsMatch(t, [1, 3, 2, 3], [1, 3, 3, 2]) +func ElementsMatch(t TestingT, listA, listB interface{}, msgAndArgs ...interface{}) (ok bool) { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if isEmpty(listA) && isEmpty(listB) { + return true + } + + if !isList(t, listA, msgAndArgs...) || !isList(t, listB, msgAndArgs...) { + return false + } + + extraA, extraB := diffLists(listA, listB) + + if len(extraA) == 0 && len(extraB) == 0 { + return true + } + + return Fail(t, formatListDiff(listA, listB, extraA, extraB), msgAndArgs...) +} + +// isList checks that the provided value is array or slice. +func isList(t TestingT, list interface{}, msgAndArgs ...interface{}) (ok bool) { + kind := reflect.TypeOf(list).Kind() + if kind != reflect.Array && kind != reflect.Slice { + return Fail(t, fmt.Sprintf("%q has an unsupported type %s, expecting array or slice", list, kind), + msgAndArgs...) + } + return true +} + +// diffLists diffs two arrays/slices and returns slices of elements that are only in A and only in B. +// If some element is present multiple times, each instance is counted separately (e.g. if something is 2x in A and +// 5x in B, it will be 0x in extraA and 3x in extraB). The order of items in both lists is ignored. +func diffLists(listA, listB interface{}) (extraA, extraB []interface{}) { + aValue := reflect.ValueOf(listA) + bValue := reflect.ValueOf(listB) + + aLen := aValue.Len() + bLen := bValue.Len() + + // Mark indexes in bValue that we already used + visited := make([]bool, bLen) + for i := 0; i < aLen; i++ { + element := aValue.Index(i).Interface() + found := false + for j := 0; j < bLen; j++ { + if visited[j] { + continue + } + if ObjectsAreEqual(bValue.Index(j).Interface(), element) { + visited[j] = true + found = true + break + } + } + if !found { + extraA = append(extraA, element) + } + } + + for j := 0; j < bLen; j++ { + if visited[j] { + continue + } + extraB = append(extraB, bValue.Index(j).Interface()) + } + + return +} + +func formatListDiff(listA, listB interface{}, extraA, extraB []interface{}) string { + var msg bytes.Buffer + + msg.WriteString("elements differ") + if len(extraA) > 0 { + msg.WriteString("\n\nextra elements in list A:\n") + msg.WriteString(spewConfig.Sdump(extraA)) + } + if len(extraB) > 0 { + msg.WriteString("\n\nextra elements in list B:\n") + msg.WriteString(spewConfig.Sdump(extraB)) + } + msg.WriteString("\n\nlistA:\n") + msg.WriteString(spewConfig.Sdump(listA)) + msg.WriteString("\n\nlistB:\n") + msg.WriteString(spewConfig.Sdump(listB)) + + return msg.String() +} + +// Condition uses a Comparison to assert a complex condition. +func Condition(t TestingT, comp Comparison, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + result := comp() + if !result { + Fail(t, "Condition failed!", msgAndArgs...) + } + return result +} + +// PanicTestFunc defines a func that should be passed to the assert.Panics and assert.NotPanics +// methods, and represents a simple func that takes no arguments, and returns nothing. +type PanicTestFunc func() + +// didPanic returns true if the function passed to it panics. Otherwise, it returns false. +func didPanic(f PanicTestFunc) (bool, interface{}, string) { + + didPanic := false + var message interface{} + var stack string + func() { + + defer func() { + if message = recover(); message != nil { + didPanic = true + stack = string(debug.Stack()) + } + }() + + // call the target function + f() + + }() + + return didPanic, message, stack + +} + +// Panics asserts that the code inside the specified PanicTestFunc panics. +// +// assert.Panics(t, func(){ GoCrazy() }) +func Panics(t TestingT, f PanicTestFunc, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + if funcDidPanic, panicValue, _ := didPanic(f); !funcDidPanic { + return Fail(t, fmt.Sprintf("func %#v should panic\n\tPanic value:\t%#v", f, panicValue), msgAndArgs...) + } + + return true +} + +// PanicsWithValue asserts that the code inside the specified PanicTestFunc panics, and that +// the recovered panic value equals the expected panic value. +// +// assert.PanicsWithValue(t, "crazy error", func(){ GoCrazy() }) +func PanicsWithValue(t TestingT, expected interface{}, f PanicTestFunc, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + funcDidPanic, panicValue, panickedStack := didPanic(f) + if !funcDidPanic { + return Fail(t, fmt.Sprintf("func %#v should panic\n\tPanic value:\t%#v", f, panicValue), msgAndArgs...) + } + if panicValue != expected { + return Fail(t, fmt.Sprintf("func %#v should panic with value:\t%#v\n\tPanic value:\t%#v\n\tPanic stack:\t%s", f, expected, panicValue, panickedStack), msgAndArgs...) + } + + return true +} + +// PanicsWithError asserts that the code inside the specified PanicTestFunc +// panics, and that the recovered panic value is an error that satisfies the +// EqualError comparison. +// +// assert.PanicsWithError(t, "crazy error", func(){ GoCrazy() }) +func PanicsWithError(t TestingT, errString string, f PanicTestFunc, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + funcDidPanic, panicValue, panickedStack := didPanic(f) + if !funcDidPanic { + return Fail(t, fmt.Sprintf("func %#v should panic\n\tPanic value:\t%#v", f, panicValue), msgAndArgs...) + } + panicErr, ok := panicValue.(error) + if !ok || panicErr.Error() != errString { + return Fail(t, fmt.Sprintf("func %#v should panic with error message:\t%#v\n\tPanic value:\t%#v\n\tPanic stack:\t%s", f, errString, panicValue, panickedStack), msgAndArgs...) + } + + return true +} + +// NotPanics asserts that the code inside the specified PanicTestFunc does NOT panic. +// +// assert.NotPanics(t, func(){ RemainCalm() }) +func NotPanics(t TestingT, f PanicTestFunc, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + if funcDidPanic, panicValue, panickedStack := didPanic(f); funcDidPanic { + return Fail(t, fmt.Sprintf("func %#v should not panic\n\tPanic value:\t%v\n\tPanic stack:\t%s", f, panicValue, panickedStack), msgAndArgs...) + } + + return true +} + +// WithinDuration asserts that the two times are within duration delta of each other. +// +// assert.WithinDuration(t, time.Now(), time.Now(), 10*time.Second) +func WithinDuration(t TestingT, expected, actual time.Time, delta time.Duration, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + dt := expected.Sub(actual) + if dt < -delta || dt > delta { + return Fail(t, fmt.Sprintf("Max difference between %v and %v allowed is %v, but difference was %v", expected, actual, delta, dt), msgAndArgs...) + } + + return true +} + +func toFloat(x interface{}) (float64, bool) { + var xf float64 + xok := true + + switch xn := x.(type) { + case uint: + xf = float64(xn) + case uint8: + xf = float64(xn) + case uint16: + xf = float64(xn) + case uint32: + xf = float64(xn) + case uint64: + xf = float64(xn) + case int: + xf = float64(xn) + case int8: + xf = float64(xn) + case int16: + xf = float64(xn) + case int32: + xf = float64(xn) + case int64: + xf = float64(xn) + case float32: + xf = float64(xn) + case float64: + xf = xn + case time.Duration: + xf = float64(xn) + default: + xok = false + } + + return xf, xok +} + +// InDelta asserts that the two numerals are within delta of each other. +// +// assert.InDelta(t, math.Pi, 22/7.0, 0.01) +func InDelta(t TestingT, expected, actual interface{}, delta float64, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + af, aok := toFloat(expected) + bf, bok := toFloat(actual) + + if !aok || !bok { + return Fail(t, fmt.Sprintf("Parameters must be numerical"), msgAndArgs...) + } + + if math.IsNaN(af) { + return Fail(t, fmt.Sprintf("Expected must not be NaN"), msgAndArgs...) + } + + if math.IsNaN(bf) { + return Fail(t, fmt.Sprintf("Expected %v with delta %v, but was NaN", expected, delta), msgAndArgs...) + } + + dt := af - bf + if dt < -delta || dt > delta { + return Fail(t, fmt.Sprintf("Max difference between %v and %v allowed is %v, but difference was %v", expected, actual, delta, dt), msgAndArgs...) + } + + return true +} + +// InDeltaSlice is the same as InDelta, except it compares two slices. +func InDeltaSlice(t TestingT, expected, actual interface{}, delta float64, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if expected == nil || actual == nil || + reflect.TypeOf(actual).Kind() != reflect.Slice || + reflect.TypeOf(expected).Kind() != reflect.Slice { + return Fail(t, fmt.Sprintf("Parameters must be slice"), msgAndArgs...) + } + + actualSlice := reflect.ValueOf(actual) + expectedSlice := reflect.ValueOf(expected) + + for i := 0; i < actualSlice.Len(); i++ { + result := InDelta(t, actualSlice.Index(i).Interface(), expectedSlice.Index(i).Interface(), delta, msgAndArgs...) + if !result { + return result + } + } + + return true +} + +// InDeltaMapValues is the same as InDelta, but it compares all values between two maps. Both maps must have exactly the same keys. +func InDeltaMapValues(t TestingT, expected, actual interface{}, delta float64, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if expected == nil || actual == nil || + reflect.TypeOf(actual).Kind() != reflect.Map || + reflect.TypeOf(expected).Kind() != reflect.Map { + return Fail(t, "Arguments must be maps", msgAndArgs...) + } + + expectedMap := reflect.ValueOf(expected) + actualMap := reflect.ValueOf(actual) + + if expectedMap.Len() != actualMap.Len() { + return Fail(t, "Arguments must have the same number of keys", msgAndArgs...) + } + + for _, k := range expectedMap.MapKeys() { + ev := expectedMap.MapIndex(k) + av := actualMap.MapIndex(k) + + if !ev.IsValid() { + return Fail(t, fmt.Sprintf("missing key %q in expected map", k), msgAndArgs...) + } + + if !av.IsValid() { + return Fail(t, fmt.Sprintf("missing key %q in actual map", k), msgAndArgs...) + } + + if !InDelta( + t, + ev.Interface(), + av.Interface(), + delta, + msgAndArgs..., + ) { + return false + } + } + + return true +} + +func calcRelativeError(expected, actual interface{}) (float64, error) { + af, aok := toFloat(expected) + if !aok { + return 0, fmt.Errorf("expected value %q cannot be converted to float", expected) + } + if math.IsNaN(af) { + return 0, errors.New("expected value must not be NaN") + } + if af == 0 { + return 0, fmt.Errorf("expected value must have a value other than zero to calculate the relative error") + } + bf, bok := toFloat(actual) + if !bok { + return 0, fmt.Errorf("actual value %q cannot be converted to float", actual) + } + if math.IsNaN(bf) { + return 0, errors.New("actual value must not be NaN") + } + + return math.Abs(af-bf) / math.Abs(af), nil +} + +// InEpsilon asserts that expected and actual have a relative error less than epsilon +func InEpsilon(t TestingT, expected, actual interface{}, epsilon float64, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if math.IsNaN(epsilon) { + return Fail(t, "epsilon must not be NaN") + } + actualEpsilon, err := calcRelativeError(expected, actual) + if err != nil { + return Fail(t, err.Error(), msgAndArgs...) + } + if actualEpsilon > epsilon { + return Fail(t, fmt.Sprintf("Relative error is too high: %#v (expected)\n"+ + " < %#v (actual)", epsilon, actualEpsilon), msgAndArgs...) + } + + return true +} + +// InEpsilonSlice is the same as InEpsilon, except it compares each value from two slices. +func InEpsilonSlice(t TestingT, expected, actual interface{}, epsilon float64, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if expected == nil || actual == nil || + reflect.TypeOf(actual).Kind() != reflect.Slice || + reflect.TypeOf(expected).Kind() != reflect.Slice { + return Fail(t, fmt.Sprintf("Parameters must be slice"), msgAndArgs...) + } + + actualSlice := reflect.ValueOf(actual) + expectedSlice := reflect.ValueOf(expected) + + for i := 0; i < actualSlice.Len(); i++ { + result := InEpsilon(t, actualSlice.Index(i).Interface(), expectedSlice.Index(i).Interface(), epsilon) + if !result { + return result + } + } + + return true +} + +/* + Errors +*/ + +// NoError asserts that a function returned no error (i.e. `nil`). +// +// actualObj, err := SomeFunction() +// if assert.NoError(t, err) { +// assert.Equal(t, expectedObj, actualObj) +// } +func NoError(t TestingT, err error, msgAndArgs ...interface{}) bool { + if err != nil { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Fail(t, fmt.Sprintf("Received unexpected error:\n%+v", err), msgAndArgs...) + } + + return true +} + +// Error asserts that a function returned an error (i.e. not `nil`). +// +// actualObj, err := SomeFunction() +// if assert.Error(t, err) { +// assert.Equal(t, expectedError, err) +// } +func Error(t TestingT, err error, msgAndArgs ...interface{}) bool { + if err == nil { + if h, ok := t.(tHelper); ok { + h.Helper() + } + return Fail(t, "An error is expected but got nil.", msgAndArgs...) + } + + return true +} + +// EqualError asserts that a function returned an error (i.e. not `nil`) +// and that it is equal to the provided error. +// +// actualObj, err := SomeFunction() +// assert.EqualError(t, err, expectedErrorString) +func EqualError(t TestingT, theError error, errString string, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if !Error(t, theError, msgAndArgs...) { + return false + } + expected := errString + actual := theError.Error() + // don't need to use deep equals here, we know they are both strings + if expected != actual { + return Fail(t, fmt.Sprintf("Error message not equal:\n"+ + "expected: %q\n"+ + "actual : %q", expected, actual), msgAndArgs...) + } + return true +} + +// matchRegexp return true if a specified regexp matches a string. +func matchRegexp(rx interface{}, str interface{}) bool { + + var r *regexp.Regexp + if rr, ok := rx.(*regexp.Regexp); ok { + r = rr + } else { + r = regexp.MustCompile(fmt.Sprint(rx)) + } + + return (r.FindStringIndex(fmt.Sprint(str)) != nil) + +} + +// Regexp asserts that a specified regexp matches a string. +// +// assert.Regexp(t, regexp.MustCompile("start"), "it's starting") +// assert.Regexp(t, "start...$", "it's not starting") +func Regexp(t TestingT, rx interface{}, str interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + match := matchRegexp(rx, str) + + if !match { + Fail(t, fmt.Sprintf("Expect \"%v\" to match \"%v\"", str, rx), msgAndArgs...) + } + + return match +} + +// NotRegexp asserts that a specified regexp does not match a string. +// +// assert.NotRegexp(t, regexp.MustCompile("starts"), "it's starting") +// assert.NotRegexp(t, "^start", "it's not starting") +func NotRegexp(t TestingT, rx interface{}, str interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + match := matchRegexp(rx, str) + + if match { + Fail(t, fmt.Sprintf("Expect \"%v\" to NOT match \"%v\"", str, rx), msgAndArgs...) + } + + return !match + +} + +// Zero asserts that i is the zero value for its type. +func Zero(t TestingT, i interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if i != nil && !reflect.DeepEqual(i, reflect.Zero(reflect.TypeOf(i)).Interface()) { + return Fail(t, fmt.Sprintf("Should be zero, but was %v", i), msgAndArgs...) + } + return true +} + +// NotZero asserts that i is not the zero value for its type. +func NotZero(t TestingT, i interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + if i == nil || reflect.DeepEqual(i, reflect.Zero(reflect.TypeOf(i)).Interface()) { + return Fail(t, fmt.Sprintf("Should not be zero, but was %v", i), msgAndArgs...) + } + return true +} + +// FileExists checks whether a file exists in the given path. It also fails if +// the path points to a directory or there is an error when trying to check the file. +func FileExists(t TestingT, path string, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + info, err := os.Lstat(path) + if err != nil { + if os.IsNotExist(err) { + return Fail(t, fmt.Sprintf("unable to find file %q", path), msgAndArgs...) + } + return Fail(t, fmt.Sprintf("error when running os.Lstat(%q): %s", path, err), msgAndArgs...) + } + if info.IsDir() { + return Fail(t, fmt.Sprintf("%q is a directory", path), msgAndArgs...) + } + return true +} + +// NoFileExists checks whether a file does not exist in a given path. It fails +// if the path points to an existing _file_ only. +func NoFileExists(t TestingT, path string, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + info, err := os.Lstat(path) + if err != nil { + return true + } + if info.IsDir() { + return true + } + return Fail(t, fmt.Sprintf("file %q exists", path), msgAndArgs...) +} + +// DirExists checks whether a directory exists in the given path. It also fails +// if the path is a file rather a directory or there is an error checking whether it exists. +func DirExists(t TestingT, path string, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + info, err := os.Lstat(path) + if err != nil { + if os.IsNotExist(err) { + return Fail(t, fmt.Sprintf("unable to find file %q", path), msgAndArgs...) + } + return Fail(t, fmt.Sprintf("error when running os.Lstat(%q): %s", path, err), msgAndArgs...) + } + if !info.IsDir() { + return Fail(t, fmt.Sprintf("%q is a file", path), msgAndArgs...) + } + return true +} + +// NoDirExists checks whether a directory does not exist in the given path. +// It fails if the path points to an existing _directory_ only. +func NoDirExists(t TestingT, path string, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + info, err := os.Lstat(path) + if err != nil { + if os.IsNotExist(err) { + return true + } + return true + } + if !info.IsDir() { + return true + } + return Fail(t, fmt.Sprintf("directory %q exists", path), msgAndArgs...) +} + +// JSONEq asserts that two JSON strings are equivalent. +// +// assert.JSONEq(t, `{"hello": "world", "foo": "bar"}`, `{"foo": "bar", "hello": "world"}`) +func JSONEq(t TestingT, expected string, actual string, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + var expectedJSONAsInterface, actualJSONAsInterface interface{} + + if err := json.Unmarshal([]byte(expected), &expectedJSONAsInterface); err != nil { + return Fail(t, fmt.Sprintf("Expected value ('%s') is not valid json.\nJSON parsing error: '%s'", expected, err.Error()), msgAndArgs...) + } + + if err := json.Unmarshal([]byte(actual), &actualJSONAsInterface); err != nil { + return Fail(t, fmt.Sprintf("Input ('%s') needs to be valid json.\nJSON parsing error: '%s'", actual, err.Error()), msgAndArgs...) + } + + return Equal(t, expectedJSONAsInterface, actualJSONAsInterface, msgAndArgs...) +} + +// YAMLEq asserts that two YAML strings are equivalent. +func YAMLEq(t TestingT, expected string, actual string, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + var expectedYAMLAsInterface, actualYAMLAsInterface interface{} + + if err := yaml.Unmarshal([]byte(expected), &expectedYAMLAsInterface); err != nil { + return Fail(t, fmt.Sprintf("Expected value ('%s') is not valid yaml.\nYAML parsing error: '%s'", expected, err.Error()), msgAndArgs...) + } + + if err := yaml.Unmarshal([]byte(actual), &actualYAMLAsInterface); err != nil { + return Fail(t, fmt.Sprintf("Input ('%s') needs to be valid yaml.\nYAML error: '%s'", actual, err.Error()), msgAndArgs...) + } + + return Equal(t, expectedYAMLAsInterface, actualYAMLAsInterface, msgAndArgs...) +} + +func typeAndKind(v interface{}) (reflect.Type, reflect.Kind) { + t := reflect.TypeOf(v) + k := t.Kind() + + if k == reflect.Ptr { + t = t.Elem() + k = t.Kind() + } + return t, k +} + +// diff returns a diff of both values as long as both are of the same type and +// are a struct, map, slice, array or string. Otherwise it returns an empty string. +func diff(expected interface{}, actual interface{}) string { + if expected == nil || actual == nil { + return "" + } + + et, ek := typeAndKind(expected) + at, _ := typeAndKind(actual) + + if et != at { + return "" + } + + if ek != reflect.Struct && ek != reflect.Map && ek != reflect.Slice && ek != reflect.Array && ek != reflect.String { + return "" + } + + var e, a string + if et != reflect.TypeOf("") { + e = spewConfig.Sdump(expected) + a = spewConfig.Sdump(actual) + } else { + e = reflect.ValueOf(expected).String() + a = reflect.ValueOf(actual).String() + } + + diff, _ := difflib.GetUnifiedDiffString(difflib.UnifiedDiff{ + A: difflib.SplitLines(e), + B: difflib.SplitLines(a), + FromFile: "Expected", + FromDate: "", + ToFile: "Actual", + ToDate: "", + Context: 1, + }) + + return "\n\nDiff:\n" + diff +} + +func isFunction(arg interface{}) bool { + if arg == nil { + return false + } + return reflect.TypeOf(arg).Kind() == reflect.Func +} + +var spewConfig = spew.ConfigState{ + Indent: " ", + DisablePointerAddresses: true, + DisableCapacities: true, + SortKeys: true, + DisableMethods: true, +} + +type tHelper interface { + Helper() +} + +// Eventually asserts that given condition will be met in waitFor time, +// periodically checking target function each tick. +// +// assert.Eventually(t, func() bool { return true; }, time.Second, 10*time.Millisecond) +func Eventually(t TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + ch := make(chan bool, 1) + + timer := time.NewTimer(waitFor) + defer timer.Stop() + + ticker := time.NewTicker(tick) + defer ticker.Stop() + + for tick := ticker.C; ; { + select { + case <-timer.C: + return Fail(t, "Condition never satisfied", msgAndArgs...) + case <-tick: + tick = nil + go func() { ch <- condition() }() + case v := <-ch: + if v { + return true + } + tick = ticker.C + } + } +} + +// Never asserts that the given condition doesn't satisfy in waitFor time, +// periodically checking the target function each tick. +// +// assert.Never(t, func() bool { return false; }, time.Second, 10*time.Millisecond) +func Never(t TestingT, condition func() bool, waitFor time.Duration, tick time.Duration, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + + ch := make(chan bool, 1) + + timer := time.NewTimer(waitFor) + defer timer.Stop() + + ticker := time.NewTicker(tick) + defer ticker.Stop() + + for tick := ticker.C; ; { + select { + case <-timer.C: + return true + case <-tick: + tick = nil + go func() { ch <- condition() }() + case v := <-ch: + if v { + return Fail(t, "Condition satisfied", msgAndArgs...) + } + tick = ticker.C + } + } +} diff --git a/vendor/github.com/stretchr/testify/assert/doc.go b/vendor/github.com/stretchr/testify/assert/doc.go new file mode 100644 index 0000000000..c9dccc4d6c --- /dev/null +++ b/vendor/github.com/stretchr/testify/assert/doc.go @@ -0,0 +1,45 @@ +// Package assert provides a set of comprehensive testing tools for use with the normal Go testing system. +// +// Example Usage +// +// The following is a complete example using assert in a standard test function: +// import ( +// "testing" +// "github.com/stretchr/testify/assert" +// ) +// +// func TestSomething(t *testing.T) { +// +// var a string = "Hello" +// var b string = "Hello" +// +// assert.Equal(t, a, b, "The two words should be the same.") +// +// } +// +// if you assert many times, use the format below: +// +// import ( +// "testing" +// "github.com/stretchr/testify/assert" +// ) +// +// func TestSomething(t *testing.T) { +// assert := assert.New(t) +// +// var a string = "Hello" +// var b string = "Hello" +// +// assert.Equal(a, b, "The two words should be the same.") +// } +// +// Assertions +// +// Assertions allow you to easily write test code, and are global funcs in the `assert` package. +// All assertion functions take, as the first argument, the `*testing.T` object provided by the +// testing framework. This allows the assertion funcs to write the failings and other details to +// the correct place. +// +// Every assertion function also takes an optional string message as the final argument, +// allowing custom error messages to be appended to the message the assertion method outputs. +package assert diff --git a/vendor/github.com/stretchr/testify/assert/errors.go b/vendor/github.com/stretchr/testify/assert/errors.go new file mode 100644 index 0000000000..ac9dc9d1d6 --- /dev/null +++ b/vendor/github.com/stretchr/testify/assert/errors.go @@ -0,0 +1,10 @@ +package assert + +import ( + "errors" +) + +// AnError is an error instance useful for testing. If the code does not care +// about error specifics, and only needs to return the error for example, this +// error should be used to make the test code more readable. +var AnError = errors.New("assert.AnError general error for testing") diff --git a/vendor/github.com/stretchr/testify/assert/forward_assertions.go b/vendor/github.com/stretchr/testify/assert/forward_assertions.go new file mode 100644 index 0000000000..df189d2348 --- /dev/null +++ b/vendor/github.com/stretchr/testify/assert/forward_assertions.go @@ -0,0 +1,16 @@ +package assert + +// Assertions provides assertion methods around the +// TestingT interface. +type Assertions struct { + t TestingT +} + +// New makes a new Assertions object for the specified TestingT. +func New(t TestingT) *Assertions { + return &Assertions{ + t: t, + } +} + +//go:generate sh -c "cd ../_codegen && go build && cd - && ../_codegen/_codegen -output-package=assert -template=assertion_forward.go.tmpl -include-format-funcs" diff --git a/vendor/github.com/stretchr/testify/assert/http_assertions.go b/vendor/github.com/stretchr/testify/assert/http_assertions.go new file mode 100644 index 0000000000..4ed341dd28 --- /dev/null +++ b/vendor/github.com/stretchr/testify/assert/http_assertions.go @@ -0,0 +1,162 @@ +package assert + +import ( + "fmt" + "net/http" + "net/http/httptest" + "net/url" + "strings" +) + +// httpCode is a helper that returns HTTP code of the response. It returns -1 and +// an error if building a new request fails. +func httpCode(handler http.HandlerFunc, method, url string, values url.Values) (int, error) { + w := httptest.NewRecorder() + req, err := http.NewRequest(method, url, nil) + if err != nil { + return -1, err + } + req.URL.RawQuery = values.Encode() + handler(w, req) + return w.Code, nil +} + +// HTTPSuccess asserts that a specified handler returns a success status code. +// +// assert.HTTPSuccess(t, myHandler, "POST", "http://www.google.com", nil) +// +// Returns whether the assertion was successful (true) or not (false). +func HTTPSuccess(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + code, err := httpCode(handler, method, url, values) + if err != nil { + Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err)) + } + + isSuccessCode := code >= http.StatusOK && code <= http.StatusPartialContent + if !isSuccessCode { + Fail(t, fmt.Sprintf("Expected HTTP success status code for %q but received %d", url+"?"+values.Encode(), code)) + } + + return isSuccessCode +} + +// HTTPRedirect asserts that a specified handler returns a redirect status code. +// +// assert.HTTPRedirect(t, myHandler, "GET", "/a/b/c", url.Values{"a": []string{"b", "c"}} +// +// Returns whether the assertion was successful (true) or not (false). +func HTTPRedirect(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + code, err := httpCode(handler, method, url, values) + if err != nil { + Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err)) + } + + isRedirectCode := code >= http.StatusMultipleChoices && code <= http.StatusTemporaryRedirect + if !isRedirectCode { + Fail(t, fmt.Sprintf("Expected HTTP redirect status code for %q but received %d", url+"?"+values.Encode(), code)) + } + + return isRedirectCode +} + +// HTTPError asserts that a specified handler returns an error status code. +// +// assert.HTTPError(t, myHandler, "POST", "/a/b/c", url.Values{"a": []string{"b", "c"}} +// +// Returns whether the assertion was successful (true) or not (false). +func HTTPError(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + code, err := httpCode(handler, method, url, values) + if err != nil { + Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err)) + } + + isErrorCode := code >= http.StatusBadRequest + if !isErrorCode { + Fail(t, fmt.Sprintf("Expected HTTP error status code for %q but received %d", url+"?"+values.Encode(), code)) + } + + return isErrorCode +} + +// HTTPStatusCode asserts that a specified handler returns a specified status code. +// +// assert.HTTPStatusCode(t, myHandler, "GET", "/notImplemented", nil, 501) +// +// Returns whether the assertion was successful (true) or not (false). +func HTTPStatusCode(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, statuscode int, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + code, err := httpCode(handler, method, url, values) + if err != nil { + Fail(t, fmt.Sprintf("Failed to build test request, got error: %s", err)) + } + + successful := code == statuscode + if !successful { + Fail(t, fmt.Sprintf("Expected HTTP status code %d for %q but received %d", statuscode, url+"?"+values.Encode(), code)) + } + + return successful +} + +// HTTPBody is a helper that returns HTTP body of the response. It returns +// empty string if building a new request fails. +func HTTPBody(handler http.HandlerFunc, method, url string, values url.Values) string { + w := httptest.NewRecorder() + req, err := http.NewRequest(method, url+"?"+values.Encode(), nil) + if err != nil { + return "" + } + handler(w, req) + return w.Body.String() +} + +// HTTPBodyContains asserts that a specified handler returns a +// body that contains a string. +// +// assert.HTTPBodyContains(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky") +// +// Returns whether the assertion was successful (true) or not (false). +func HTTPBodyContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + body := HTTPBody(handler, method, url, values) + + contains := strings.Contains(body, fmt.Sprint(str)) + if !contains { + Fail(t, fmt.Sprintf("Expected response body for \"%s\" to contain \"%s\" but found \"%s\"", url+"?"+values.Encode(), str, body)) + } + + return contains +} + +// HTTPBodyNotContains asserts that a specified handler returns a +// body that does not contain a string. +// +// assert.HTTPBodyNotContains(t, myHandler, "GET", "www.google.com", nil, "I'm Feeling Lucky") +// +// Returns whether the assertion was successful (true) or not (false). +func HTTPBodyNotContains(t TestingT, handler http.HandlerFunc, method, url string, values url.Values, str interface{}, msgAndArgs ...interface{}) bool { + if h, ok := t.(tHelper); ok { + h.Helper() + } + body := HTTPBody(handler, method, url, values) + + contains := strings.Contains(body, fmt.Sprint(str)) + if contains { + Fail(t, fmt.Sprintf("Expected response body for \"%s\" to NOT contain \"%s\" but found \"%s\"", url+"?"+values.Encode(), str, body)) + } + + return !contains +} diff --git a/vendor/golang.org/x/tools/internal/gocommand/invoke.go b/vendor/golang.org/x/tools/internal/gocommand/invoke.go index f516e17623..b5c061b012 100644 --- a/vendor/golang.org/x/tools/internal/gocommand/invoke.go +++ b/vendor/golang.org/x/tools/internal/gocommand/invoke.go @@ -130,6 +130,8 @@ type Invocation struct { Verb string Args []string BuildFlags []string + ModFlag string + ModFile string Env []string WorkingDir string Logf func(format string, args ...interface{}) @@ -158,17 +160,35 @@ func (i *Invocation) run(ctx context.Context, stdout, stderr io.Writer) error { } goArgs := []string{i.Verb} + + appendModFile := func() { + if i.ModFile != "" { + goArgs = append(goArgs, "-modfile="+i.ModFile) + } + } + appendModFlag := func() { + if i.ModFlag != "" { + goArgs = append(goArgs, "-mod="+i.ModFlag) + } + } + switch i.Verb { - case "mod": - // mod needs the sub-verb before build flags. - goArgs = append(goArgs, i.Args[0]) - goArgs = append(goArgs, i.BuildFlags...) - goArgs = append(goArgs, i.Args[1:]...) - case "env": - // env doesn't take build flags. + case "env", "version": goArgs = append(goArgs, i.Args...) - default: + case "mod": + // mod needs the sub-verb before flags. + goArgs = append(goArgs, i.Args[0]) + appendModFile() + goArgs = append(goArgs, i.Args[1:]...) + case "get": goArgs = append(goArgs, i.BuildFlags...) + appendModFile() + goArgs = append(goArgs, i.Args...) + + default: // notably list and build. + goArgs = append(goArgs, i.BuildFlags...) + appendModFile() + appendModFlag() goArgs = append(goArgs, i.Args...) } cmd := exec.Command("go", goArgs...) diff --git a/vendor/golang.org/x/tools/internal/imports/fix.go b/vendor/golang.org/x/tools/internal/imports/fix.go index 675d16c873..d859617b77 100644 --- a/vendor/golang.org/x/tools/internal/imports/fix.go +++ b/vendor/golang.org/x/tools/internal/imports/fix.go @@ -83,7 +83,7 @@ type ImportFix struct { IdentName string // FixType is the type of fix this is (AddImport, DeleteImport, SetImportName). FixType ImportFixType - Relevance int // see pkg + Relevance float64 // see pkg } // An ImportInfo represents a single import statement. @@ -592,9 +592,9 @@ func getFixes(fset *token.FileSet, f *ast.File, filename string, env *ProcessEnv return fixes, nil } -// Highest relevance, used for the standard library. Chosen arbitrarily to -// match pre-existing gopls code. -const MaxRelevance = 7 +// MaxRelevance is the highest relevance, used for the standard library. +// Chosen arbitrarily to match pre-existing gopls code. +const MaxRelevance = 7.0 // getCandidatePkgs works with the passed callback to find all acceptable packages. // It deduplicates by import path, and uses a cached stdlib rather than reading @@ -607,6 +607,10 @@ func getCandidatePkgs(ctx context.Context, wrappedCallback *scanCallback, filena if err != nil { return err } + + var mu sync.Mutex // to guard asynchronous access to dupCheck + dupCheck := map[string]struct{}{} + // Start off with the standard library. for importPath, exports := range stdlib { p := &pkg{ @@ -615,14 +619,12 @@ func getCandidatePkgs(ctx context.Context, wrappedCallback *scanCallback, filena packageName: path.Base(importPath), relevance: MaxRelevance, } + dupCheck[importPath] = struct{}{} if notSelf(p) && wrappedCallback.dirFound(p) && wrappedCallback.packageNameLoaded(p) { wrappedCallback.exportsLoaded(p, exports) } } - var mu sync.Mutex - dupCheck := map[string]struct{}{} - scanFilter := &scanCallback{ rootFound: func(root gopathwalk.Root) bool { // Exclude goroot results -- getting them is relatively expensive, not cached, @@ -658,8 +660,8 @@ func getCandidatePkgs(ctx context.Context, wrappedCallback *scanCallback, filena return resolver.scan(ctx, scanFilter) } -func ScoreImportPaths(ctx context.Context, env *ProcessEnv, paths []string) (map[string]int, error) { - result := make(map[string]int) +func ScoreImportPaths(ctx context.Context, env *ProcessEnv, paths []string) (map[string]float64, error) { + result := make(map[string]float64) resolver, err := env.GetResolver() if err != nil { return nil, err @@ -802,6 +804,8 @@ type ProcessEnv struct { GocmdRunner *gocommand.Runner BuildFlags []string + ModFlag string + ModFile string // Env overrides the OS environment, and can be used to specify // GOPROXY, GO111MODULE, etc. PATH cannot be set here, because @@ -995,7 +999,7 @@ type Resolver interface { // loadExports may be called concurrently. loadExports(ctx context.Context, pkg *pkg, includeTest bool) (string, []string, error) // scoreImportPath returns the relevance for an import path. - scoreImportPath(ctx context.Context, path string) int + scoreImportPath(ctx context.Context, path string) float64 ClearForNewScan() } @@ -1260,10 +1264,10 @@ func packageDirToName(dir string) (packageName string, err error) { } type pkg struct { - dir string // absolute file path to pkg directory ("/usr/lib/go/src/net/http") - importPathShort string // vendorless import path ("net/http", "a/b") - packageName string // package name loaded from source if requested - relevance int // a weakly-defined score of how relevant a package is. 0 is most relevant. + dir string // absolute file path to pkg directory ("/usr/lib/go/src/net/http") + importPathShort string // vendorless import path ("net/http", "a/b") + packageName string // package name loaded from source if requested + relevance float64 // a weakly-defined score of how relevant a package is. 0 is most relevant. } type pkgDistance struct { @@ -1389,7 +1393,7 @@ func (r *gopathResolver) scan(ctx context.Context, callback *scanCallback) error return nil } -func (r *gopathResolver) scoreImportPath(ctx context.Context, path string) int { +func (r *gopathResolver) scoreImportPath(ctx context.Context, path string) float64 { if _, ok := stdlib[path]; ok { return MaxRelevance } diff --git a/vendor/golang.org/x/tools/internal/imports/mod.go b/vendor/golang.org/x/tools/internal/imports/mod.go index 94880d6160..8a83613c57 100644 --- a/vendor/golang.org/x/tools/internal/imports/mod.go +++ b/vendor/golang.org/x/tools/internal/imports/mod.go @@ -59,6 +59,8 @@ func (r *ModuleResolver) init() error { } inv := gocommand.Invocation{ BuildFlags: r.env.BuildFlags, + ModFlag: r.env.ModFlag, + ModFile: r.env.ModFile, Env: r.env.env(), Logf: r.env.Logf, WorkingDir: r.env.WorkingDir, @@ -487,7 +489,7 @@ func (r *ModuleResolver) scan(ctx context.Context, callback *scanCallback) error return nil } -func (r *ModuleResolver) scoreImportPath(ctx context.Context, path string) int { +func (r *ModuleResolver) scoreImportPath(ctx context.Context, path string) float64 { if _, ok := stdlib[path]; ok { return MaxRelevance } @@ -495,17 +497,31 @@ func (r *ModuleResolver) scoreImportPath(ctx context.Context, path string) int { return modRelevance(mod) } -func modRelevance(mod *gocommand.ModuleJSON) int { +func modRelevance(mod *gocommand.ModuleJSON) float64 { + var relevance float64 switch { case mod == nil: // out of scope return MaxRelevance - 4 case mod.Indirect: - return MaxRelevance - 3 + relevance = MaxRelevance - 3 case !mod.Main: - return MaxRelevance - 2 + relevance = MaxRelevance - 2 default: - return MaxRelevance - 1 // main module ties with stdlib + relevance = MaxRelevance - 1 // main module ties with stdlib } + + _, versionString, ok := module.SplitPathVersion(mod.Path) + if ok { + index := strings.Index(versionString, "v") + if index == -1 { + return relevance + } + if versionNumber, err := strconv.ParseFloat(versionString[index+1:], 64); err == nil { + relevance += versionNumber / 1000 + } + } + + return relevance } // canonicalize gets the result of canonicalizing the packages using the results diff --git a/vendor/modules.txt b/vendor/modules.txt index 21715b6d96..bce0a8ffee 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -97,7 +97,6 @@ github.com/cespare/xxhash/v2 ## explicit github.com/containerd/containerd/errdefs # github.com/davecgh/go-spew v1.1.1 -## explicit github.com/davecgh/go-spew/spew # github.com/dgryski/go-sip13 v0.0.0-20200911182023-62edffca9245 ## explicit @@ -364,7 +363,6 @@ github.com/opentracing/opentracing-go/log ## explicit github.com/pkg/errors # github.com/pmezard/go-difflib v1.0.0 -## explicit github.com/pmezard/go-difflib/difflib # github.com/prometheus/alertmanager v0.21.0 ## explicit @@ -410,6 +408,9 @@ github.com/shurcooL/httpfs/vfsutil github.com/shurcooL/vfsgen # github.com/sirupsen/logrus v1.6.0 github.com/sirupsen/logrus +# github.com/stretchr/testify v1.6.1 +## explicit +github.com/stretchr/testify/assert # github.com/uber/jaeger-client-go v2.25.0+incompatible ## explicit github.com/uber/jaeger-client-go @@ -521,7 +522,7 @@ golang.org/x/text/width # golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e ## explicit golang.org/x/time/rate -# golang.org/x/tools v0.0.0-20201008025239-9df69603baec +# golang.org/x/tools v0.0.0-20201020161133-226fd2f889ca ## explicit golang.org/x/tools/cmd/goimports golang.org/x/tools/go/ast/astutil diff --git a/web/api/v1/api.go b/web/api/v1/api.go index 284278e527..ea4a2c3b1f 100644 --- a/web/api/v1/api.go +++ b/web/api/v1/api.go @@ -37,6 +37,7 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/common/model" "github.com/prometheus/common/route" + "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/pkg/gate" "github.com/prometheus/prometheus/pkg/labels" diff --git a/web/api/v1/api_test.go b/web/api/v1/api_test.go index 19131da79e..fa24d95ea7 100644 --- a/web/api/v1/api_test.go +++ b/web/api/v1/api_test.go @@ -35,13 +35,14 @@ import ( "github.com/go-kit/kit/log" "github.com/gogo/protobuf/proto" "github.com/golang/snappy" + "github.com/pkg/errors" "github.com/prometheus/client_golang/prometheus" config_util "github.com/prometheus/common/config" "github.com/prometheus/common/model" "github.com/prometheus/common/promlog" "github.com/prometheus/common/route" + "github.com/stretchr/testify/assert" - "github.com/pkg/errors" "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/pkg/gate" "github.com/prometheus/prometheus/pkg/labels" @@ -56,7 +57,6 @@ import ( "github.com/prometheus/prometheus/storage/remote" "github.com/prometheus/prometheus/tsdb" "github.com/prometheus/prometheus/util/teststorage" - "github.com/prometheus/prometheus/util/testutil" ) // testMetaStore satisfies the scrape.MetricMetadataStore interface. @@ -309,10 +309,10 @@ func TestEndpoints(t *testing.T) { test_metric4{foo="boo", dup="1"} 1+0x100 test_metric4{foo="boo"} 1+0x100 `) - testutil.Ok(t, err) + assert.NoError(t, err) defer suite.Close() - testutil.Ok(t, suite.Run()) + assert.NoError(t, suite.Run()) now := time.Now() @@ -349,13 +349,13 @@ func TestEndpoints(t *testing.T) { defer server.Close() u, err := url.Parse(server.URL) - testutil.Ok(t, err) + assert.NoError(t, err) al := promlog.AllowedLevel{} - testutil.Ok(t, al.Set("debug")) + assert.NoError(t, al.Set("debug")) af := promlog.AllowedFormat{} - testutil.Ok(t, af.Set("logfmt")) + assert.NoError(t, af.Set("logfmt")) promlogConfig := promlog.Config{ Level: &al, @@ -363,7 +363,7 @@ func TestEndpoints(t *testing.T) { } dbDir, err := ioutil.TempDir("", "tsdb-api-ready") - testutil.Ok(t, err) + assert.NoError(t, err) defer os.RemoveAll(dbDir) remote := remote.NewStorage(promlog.New(&promlogConfig), prometheus.DefaultRegisterer, nil, dbDir, 1*time.Second) @@ -377,7 +377,7 @@ func TestEndpoints(t *testing.T) { }, }, }) - testutil.Ok(t, err) + assert.NoError(t, err) var algr rulesRetrieverMock algr.testing = t @@ -415,9 +415,9 @@ func TestLabelNames(t *testing.T) { test_metric2{foo="boo"} 1+0x100 test_metric2{foo="boo", xyz="qwerty"} 1+0x100 `) - testutil.Ok(t, err) + assert.NoError(t, err) defer suite.Close() - testutil.Ok(t, suite.Run()) + assert.NoError(t, suite.Run()) api := &API{ Queryable: suite.Storage(), @@ -433,7 +433,7 @@ func TestLabelNames(t *testing.T) { for _, method := range []string{http.MethodGet, http.MethodPost} { ctx := context.Background() req, err := request(method) - testutil.Ok(t, err) + assert.NoError(t, err) res := api.labelNames(req.WithContext(ctx)) assertAPIError(t, res.err, "") assertAPIResponse(t, res.data, []string{"__name__", "baz", "foo", "foo1", "foo2", "xyz"}) @@ -1784,23 +1784,7 @@ func assertAPIError(t *testing.T, got *apiError, exp errorType) { func assertAPIResponse(t *testing.T, got interface{}, exp interface{}) { t.Helper() - if !reflect.DeepEqual(exp, got) { - respJSON, err := json.Marshal(got) - if err != nil { - t.Fatalf("failed to marshal response as JSON: %v", err.Error()) - } - - expectedRespJSON, err := json.Marshal(exp) - if err != nil { - t.Fatalf("failed to marshal expected response as JSON: %v", err.Error()) - } - - t.Fatalf( - "Response does not match, expected:\n%+v\ngot:\n%+v", - string(expectedRespJSON), - string(respJSON), - ) - } + assert.Equal(t, exp, got) } func assertAPIResponseLength(t *testing.T, got interface{}, expLen int) { @@ -1821,12 +1805,12 @@ func TestSampledReadEndpoint(t *testing.T) { load 1m test_metric1{foo="bar",baz="qux"} 1 `) - testutil.Ok(t, err) + assert.NoError(t, err) defer suite.Close() err = suite.Run() - testutil.Ok(t, err) + assert.NoError(t, err) api := &API{ Queryable: suite.Storage(), @@ -1849,21 +1833,21 @@ func TestSampledReadEndpoint(t *testing.T) { // Encode the request. matcher1, err := labels.NewMatcher(labels.MatchEqual, "__name__", "test_metric1") - testutil.Ok(t, err) + assert.NoError(t, err) matcher2, err := labels.NewMatcher(labels.MatchEqual, "d", "e") - testutil.Ok(t, err) + assert.NoError(t, err) query, err := remote.ToQuery(0, 1, []*labels.Matcher{matcher1, matcher2}, &storage.SelectHints{Step: 0, Func: "avg"}) - testutil.Ok(t, err) + assert.NoError(t, err) req := &prompb.ReadRequest{Queries: []*prompb.Query{query}} data, err := proto.Marshal(req) - testutil.Ok(t, err) + assert.NoError(t, err) compressed := snappy.Encode(nil, data) request, err := http.NewRequest("POST", "", bytes.NewBuffer(compressed)) - testutil.Ok(t, err) + assert.NoError(t, err) recorder := httptest.NewRecorder() api.remoteRead(recorder, request) @@ -1872,25 +1856,25 @@ func TestSampledReadEndpoint(t *testing.T) { t.Fatal(recorder.Code) } - testutil.Equals(t, "application/x-protobuf", recorder.Result().Header.Get("Content-Type")) - testutil.Equals(t, "snappy", recorder.Result().Header.Get("Content-Encoding")) + assert.Equal(t, "application/x-protobuf", recorder.Result().Header.Get("Content-Type")) + assert.Equal(t, "snappy", recorder.Result().Header.Get("Content-Encoding")) // Decode the response. compressed, err = ioutil.ReadAll(recorder.Result().Body) - testutil.Ok(t, err) + assert.NoError(t, err) uncompressed, err := snappy.Decode(nil, compressed) - testutil.Ok(t, err) + assert.NoError(t, err) var resp prompb.ReadResponse err = proto.Unmarshal(uncompressed, &resp) - testutil.Ok(t, err) + assert.NoError(t, err) if len(resp.Results) != 1 { t.Fatalf("Expected 1 result, got %d", len(resp.Results)) } - testutil.Equals(t, &prompb.QueryResult{ + assert.Equal(t, &prompb.QueryResult{ Timeseries: []*prompb.TimeSeries{ { Labels: []prompb.Label{ @@ -1916,11 +1900,11 @@ func TestStreamReadEndpoint(t *testing.T) { test_metric1{foo="bar2",baz="qux"} 0+100x120 test_metric1{foo="bar3",baz="qux"} 0+100x240 `) - testutil.Ok(t, err) + assert.NoError(t, err) defer suite.Close() - testutil.Ok(t, suite.Run()) + assert.NoError(t, suite.Run()) api := &API{ Queryable: suite.Storage(), @@ -1945,13 +1929,13 @@ func TestStreamReadEndpoint(t *testing.T) { // Encode the request. matcher1, err := labels.NewMatcher(labels.MatchEqual, "__name__", "test_metric1") - testutil.Ok(t, err) + assert.NoError(t, err) matcher2, err := labels.NewMatcher(labels.MatchEqual, "d", "e") - testutil.Ok(t, err) + assert.NoError(t, err) matcher3, err := labels.NewMatcher(labels.MatchEqual, "foo", "bar1") - testutil.Ok(t, err) + assert.NoError(t, err) query1, err := remote.ToQuery(0, 14400001, []*labels.Matcher{matcher1, matcher2}, &storage.SelectHints{ Step: 1, @@ -1959,7 +1943,7 @@ func TestStreamReadEndpoint(t *testing.T) { Start: 0, End: 14400001, }) - testutil.Ok(t, err) + assert.NoError(t, err) query2, err := remote.ToQuery(0, 14400001, []*labels.Matcher{matcher1, matcher3}, &storage.SelectHints{ Step: 1, @@ -1967,18 +1951,18 @@ func TestStreamReadEndpoint(t *testing.T) { Start: 0, End: 14400001, }) - testutil.Ok(t, err) + assert.NoError(t, err) req := &prompb.ReadRequest{ Queries: []*prompb.Query{query1, query2}, AcceptedResponseTypes: []prompb.ReadRequest_ResponseType{prompb.ReadRequest_STREAMED_XOR_CHUNKS}, } data, err := proto.Marshal(req) - testutil.Ok(t, err) + assert.NoError(t, err) compressed := snappy.Encode(nil, data) request, err := http.NewRequest("POST", "", bytes.NewBuffer(compressed)) - testutil.Ok(t, err) + assert.NoError(t, err) recorder := httptest.NewRecorder() api.remoteRead(recorder, request) @@ -1987,8 +1971,8 @@ func TestStreamReadEndpoint(t *testing.T) { t.Fatal(recorder.Code) } - testutil.Equals(t, "application/x-streamed-protobuf; proto=prometheus.ChunkedReadResponse", recorder.Result().Header.Get("Content-Type")) - testutil.Equals(t, "", recorder.Result().Header.Get("Content-Encoding")) + assert.Equal(t, "application/x-streamed-protobuf; proto=prometheus.ChunkedReadResponse", recorder.Result().Header.Get("Content-Type")) + assert.Equal(t, "", recorder.Result().Header.Get("Content-Encoding")) var results []*prompb.ChunkedReadResponse stream := remote.NewChunkedReader(recorder.Result().Body, remote.DefaultChunkedReadLimit, nil) @@ -1998,7 +1982,7 @@ func TestStreamReadEndpoint(t *testing.T) { if err == io.EOF { break } - testutil.Ok(t, err) + assert.NoError(t, err) results = append(results, res) } @@ -2006,7 +1990,7 @@ func TestStreamReadEndpoint(t *testing.T) { t.Fatalf("Expected 5 result, got %d", len(results)) } - testutil.Equals(t, []*prompb.ChunkedReadResponse{ + assert.Equal(t, []*prompb.ChunkedReadResponse{ { ChunkedSeries: []*prompb.ChunkedSeries{ { @@ -2310,7 +2294,7 @@ func TestAdminEndpoints(t *testing.T) { tc := tc t.Run("", func(t *testing.T) { dir, _ := ioutil.TempDir("", "fakeDB") - defer func() { testutil.Ok(t, os.RemoveAll(dir)) }() + defer func() { assert.NoError(t, os.RemoveAll(dir)) }() api := &API{ db: tc.db, @@ -2321,7 +2305,7 @@ func TestAdminEndpoints(t *testing.T) { endpoint := tc.endpoint(api) req, err := http.NewRequest(tc.method, fmt.Sprintf("?%s", tc.values.Encode()), nil) - testutil.Ok(t, err) + assert.NoError(t, err) res := setUnavailStatusOnTSDBNotReady(endpoint(req)) assertAPIError(t, res.err, tc.errType) @@ -2362,9 +2346,7 @@ func TestRespondSuccess(t *testing.T) { Status: statusSuccess, Data: "test", } - if !reflect.DeepEqual(&res, exp) { - t.Fatalf("Expected response \n%v\n but got \n%v\n", res, exp) - } + assert.Equal(t, exp, &res) } func TestRespondError(t *testing.T) { @@ -2402,9 +2384,7 @@ func TestRespondError(t *testing.T) { ErrorType: errorTimeout, Error: "message", } - if !reflect.DeepEqual(&res, exp) { - t.Fatalf("Expected response \n%v\n but got \n%v\n", res, exp) - } + assert.Equal(t, exp, &res) } func TestParseTimeParam(t *testing.T) { @@ -2414,7 +2394,7 @@ func TestParseTimeParam(t *testing.T) { } ts, err := parseTime("1582468023986") - testutil.Ok(t, err) + assert.NoError(t, err) var tests = []struct { paramName string @@ -2456,15 +2436,15 @@ func TestParseTimeParam(t *testing.T) { for _, test := range tests { req, err := http.NewRequest("GET", "localhost:42/foo?"+test.paramName+"="+test.paramValue, nil) - testutil.Ok(t, err) + assert.NoError(t, err) result := test.result asTime, err := parseTimeParam(req, test.paramName, test.defaultValue) if err != nil { - testutil.ErrorEqual(t, result.asError(), err) + assert.EqualError(t, err, result.asError().Error()) } else { - testutil.Assert(t, asTime.Equal(result.asTime), "time as return value: %s not parsed correctly. Expected %s. Actual %s", test.paramValue, result.asTime, asTime) + assert.True(t, asTime.Equal(result.asTime), "time as return value: %s not parsed correctly. Expected %s. Actual %s", test.paramValue, result.asTime, asTime) } } } @@ -2776,8 +2756,8 @@ func TestReturnAPIError(t *testing.T) { for _, c := range cases { actual := returnAPIError(c.err) - testutil.NotOk(t, actual) - testutil.Equals(t, c.expected, actual.typ) + assert.Error(t, actual) + assert.Equal(t, c.expected, actual.typ) } } diff --git a/web/federate.go b/web/federate.go index ba601bce9e..c825a08ba0 100644 --- a/web/federate.go +++ b/web/federate.go @@ -25,7 +25,6 @@ import ( dto "github.com/prometheus/client_model/go" "github.com/prometheus/common/expfmt" "github.com/prometheus/common/model" - "github.com/prometheus/prometheus/tsdb" "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/pkg/timestamp" @@ -33,6 +32,7 @@ import ( "github.com/prometheus/prometheus/promql" "github.com/prometheus/prometheus/promql/parser" "github.com/prometheus/prometheus/storage" + "github.com/prometheus/prometheus/tsdb" ) var ( diff --git a/web/federate_test.go b/web/federate_test.go index 8f9e86ab40..dc0258ec54 100644 --- a/web/federate_test.go +++ b/web/federate_test.go @@ -25,12 +25,13 @@ import ( "github.com/pkg/errors" "github.com/prometheus/common/model" + "github.com/stretchr/testify/assert" + "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/pkg/labels" "github.com/prometheus/prometheus/promql" "github.com/prometheus/prometheus/storage" "github.com/prometheus/prometheus/tsdb" - "github.com/prometheus/prometheus/util/testutil" ) var scenarios = map[string]struct { @@ -220,8 +221,8 @@ func TestFederation(t *testing.T) { res := httptest.NewRecorder() h.federation(res, req) - testutil.Equals(t, scenario.code, res.Code) - testutil.Equals(t, scenario.body, normalizeBody(res.Body)) + assert.Equal(t, scenario.code, res.Code) + assert.Equal(t, scenario.body, normalizeBody(res.Body)) }) } } @@ -262,10 +263,10 @@ func TestFederation_NotReady(t *testing.T) { h.federation(res, req) if scenario.code == http.StatusBadRequest { // Request are expected to be checked before DB readiness. - testutil.Equals(t, http.StatusBadRequest, res.Code) + assert.Equal(t, http.StatusBadRequest, res.Code) return } - testutil.Equals(t, http.StatusServiceUnavailable, res.Code) + assert.Equal(t, http.StatusServiceUnavailable, res.Code) }) } } diff --git a/web/web_test.go b/web/web_test.go index 0b4cd9edb6..6f559382ec 100644 --- a/web/web_test.go +++ b/web/web_test.go @@ -32,12 +32,13 @@ import ( "github.com/prometheus/client_golang/prometheus" prom_testutil "github.com/prometheus/client_golang/prometheus/testutil" + "github.com/stretchr/testify/assert" + "github.com/prometheus/prometheus/config" "github.com/prometheus/prometheus/notifier" "github.com/prometheus/prometheus/rules" "github.com/prometheus/prometheus/scrape" "github.com/prometheus/prometheus/tsdb" - "github.com/prometheus/prometheus/util/testutil" ) func TestMain(m *testing.M) { @@ -85,12 +86,12 @@ func TestGlobalURL(t *testing.T) { for _, test := range tests { inURL, err := url.Parse(test.inURL) - testutil.Ok(t, err) + assert.NoError(t, err) globalURL := tmplFuncs("", opts)["globalURL"].(func(u *url.URL) *url.URL) outURL := globalURL(inURL) - testutil.Equals(t, test.outURL, outURL.String()) + assert.Equal(t, test.outURL, outURL.String()) } } @@ -106,11 +107,11 @@ func TestReadyAndHealthy(t *testing.T) { t.Parallel() dbDir, err := ioutil.TempDir("", "tsdb-ready") - testutil.Ok(t, err) - defer func() { testutil.Ok(t, os.RemoveAll(dbDir)) }() + assert.NoError(t, err) + defer func() { assert.NoError(t, os.RemoveAll(dbDir)) }() db, err := tsdb.Open(dbDir, nil, nil, nil) - testutil.Ok(t, err) + assert.NoError(t, err) opts := &Options{ ListenAddress: ":9090", @@ -156,8 +157,8 @@ func TestReadyAndHealthy(t *testing.T) { time.Sleep(5 * time.Second) resp, err := http.Get("http://localhost:9090/-/healthy") - testutil.Ok(t, err) - testutil.Equals(t, http.StatusOK, resp.StatusCode) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, resp.StatusCode) cleanupTestResponse(t, resp) for _, u := range []string{ @@ -172,19 +173,19 @@ func TestReadyAndHealthy(t *testing.T) { "http://localhost:9090/config", } { resp, err = http.Get(u) - testutil.Ok(t, err) - testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode) + assert.NoError(t, err) + assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) cleanupTestResponse(t, resp) } resp, err = http.Post("http://localhost:9090/api/v1/admin/tsdb/snapshot", "", strings.NewReader("")) - testutil.Ok(t, err) - testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode) + assert.NoError(t, err) + assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) cleanupTestResponse(t, resp) resp, err = http.Post("http://localhost:9090/api/v1/admin/tsdb/delete_series", "", strings.NewReader("{}")) - testutil.Ok(t, err) - testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode) + assert.NoError(t, err) + assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) cleanupTestResponse(t, resp) // Set to ready. @@ -203,31 +204,31 @@ func TestReadyAndHealthy(t *testing.T) { "http://localhost:9090/config", } { resp, err = http.Get(u) - testutil.Ok(t, err) - testutil.Equals(t, http.StatusOK, resp.StatusCode) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, resp.StatusCode) cleanupTestResponse(t, resp) } resp, err = http.Post("http://localhost:9090/api/v1/admin/tsdb/snapshot", "", strings.NewReader("")) - testutil.Ok(t, err) - testutil.Equals(t, http.StatusOK, resp.StatusCode) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, resp.StatusCode) cleanupSnapshot(t, dbDir, resp) cleanupTestResponse(t, resp) resp, err = http.Post("http://localhost:9090/api/v1/admin/tsdb/delete_series?match[]=up", "", nil) - testutil.Ok(t, err) - testutil.Equals(t, http.StatusNoContent, resp.StatusCode) + assert.NoError(t, err) + assert.Equal(t, http.StatusNoContent, resp.StatusCode) cleanupTestResponse(t, resp) } func TestRoutePrefix(t *testing.T) { t.Parallel() dbDir, err := ioutil.TempDir("", "tsdb-ready") - testutil.Ok(t, err) - defer func() { testutil.Ok(t, os.RemoveAll(dbDir)) }() + assert.NoError(t, err) + defer func() { assert.NoError(t, os.RemoveAll(dbDir)) }() db, err := tsdb.Open(dbDir, nil, nil, nil) - testutil.Ok(t, err) + assert.NoError(t, err) opts := &Options{ ListenAddress: ":9091", @@ -266,57 +267,57 @@ func TestRoutePrefix(t *testing.T) { time.Sleep(5 * time.Second) resp, err := http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/healthy") - testutil.Ok(t, err) - testutil.Equals(t, http.StatusOK, resp.StatusCode) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, resp.StatusCode) cleanupTestResponse(t, resp) resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/ready") - testutil.Ok(t, err) - testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode) + assert.NoError(t, err) + assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) cleanupTestResponse(t, resp) resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/version") - testutil.Ok(t, err) - testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode) + assert.NoError(t, err) + assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) cleanupTestResponse(t, resp) resp, err = http.Post("http://localhost:9091"+opts.RoutePrefix+"/api/v1/admin/tsdb/snapshot", "", strings.NewReader("")) - testutil.Ok(t, err) - testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode) + assert.NoError(t, err) + assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) cleanupTestResponse(t, resp) resp, err = http.Post("http://localhost:9091"+opts.RoutePrefix+"/api/v1/admin/tsdb/delete_series", "", strings.NewReader("{}")) - testutil.Ok(t, err) - testutil.Equals(t, http.StatusServiceUnavailable, resp.StatusCode) + assert.NoError(t, err) + assert.Equal(t, http.StatusServiceUnavailable, resp.StatusCode) cleanupTestResponse(t, resp) // Set to ready. webHandler.Ready() resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/healthy") - testutil.Ok(t, err) - testutil.Equals(t, http.StatusOK, resp.StatusCode) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, resp.StatusCode) cleanupTestResponse(t, resp) resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/-/ready") - testutil.Ok(t, err) - testutil.Equals(t, http.StatusOK, resp.StatusCode) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, resp.StatusCode) cleanupTestResponse(t, resp) resp, err = http.Get("http://localhost:9091" + opts.RoutePrefix + "/version") - testutil.Ok(t, err) - testutil.Equals(t, http.StatusOK, resp.StatusCode) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, resp.StatusCode) cleanupTestResponse(t, resp) resp, err = http.Post("http://localhost:9091"+opts.RoutePrefix+"/api/v1/admin/tsdb/snapshot", "", strings.NewReader("")) - testutil.Ok(t, err) - testutil.Equals(t, http.StatusOK, resp.StatusCode) + assert.NoError(t, err) + assert.Equal(t, http.StatusOK, resp.StatusCode) cleanupSnapshot(t, dbDir, resp) cleanupTestResponse(t, resp) resp, err = http.Post("http://localhost:9091"+opts.RoutePrefix+"/api/v1/admin/tsdb/delete_series?match[]=up", "", nil) - testutil.Ok(t, err) - testutil.Equals(t, http.StatusNoContent, resp.StatusCode) + assert.NoError(t, err) + assert.Equal(t, http.StatusNoContent, resp.StatusCode) cleanupTestResponse(t, resp) } @@ -349,11 +350,11 @@ func TestDebugHandler(t *testing.T) { req, err := http.NewRequest("GET", tc.url, nil) - testutil.Ok(t, err) + assert.NoError(t, err) handler.router.ServeHTTP(w, req) - testutil.Equals(t, tc.code, w.Code) + assert.Equal(t, tc.code, w.Code) } } @@ -372,33 +373,33 @@ func TestHTTPMetrics(t *testing.T) { w := httptest.NewRecorder() req, err := http.NewRequest("GET", "/-/ready", nil) - testutil.Ok(t, err) + assert.NoError(t, err) handler.router.ServeHTTP(w, req) return w.Code } code := getReady() - testutil.Equals(t, http.StatusServiceUnavailable, code) + assert.Equal(t, http.StatusServiceUnavailable, code) counter := handler.metrics.requestCounter - testutil.Equals(t, 1, int(prom_testutil.ToFloat64(counter.WithLabelValues("/-/ready", strconv.Itoa(http.StatusServiceUnavailable))))) + assert.Equal(t, 1, int(prom_testutil.ToFloat64(counter.WithLabelValues("/-/ready", strconv.Itoa(http.StatusServiceUnavailable))))) handler.Ready() for range [2]int{} { code = getReady() - testutil.Equals(t, http.StatusOK, code) + assert.Equal(t, http.StatusOK, code) } - testutil.Equals(t, 2, int(prom_testutil.ToFloat64(counter.WithLabelValues("/-/ready", strconv.Itoa(http.StatusOK))))) - testutil.Equals(t, 1, int(prom_testutil.ToFloat64(counter.WithLabelValues("/-/ready", strconv.Itoa(http.StatusServiceUnavailable))))) + assert.Equal(t, 2, int(prom_testutil.ToFloat64(counter.WithLabelValues("/-/ready", strconv.Itoa(http.StatusOK))))) + assert.Equal(t, 1, int(prom_testutil.ToFloat64(counter.WithLabelValues("/-/ready", strconv.Itoa(http.StatusServiceUnavailable))))) } func TestShutdownWithStaleConnection(t *testing.T) { dbDir, err := ioutil.TempDir("", "tsdb-ready") - testutil.Ok(t, err) - defer func() { testutil.Ok(t, os.RemoveAll(dbDir)) }() + assert.NoError(t, err) + defer func() { assert.NoError(t, os.RemoveAll(dbDir)) }() db, err := tsdb.Open(dbDir, nil, nil, nil) - testutil.Ok(t, err) + assert.NoError(t, err) timeout := 10 * time.Second @@ -449,8 +450,8 @@ func TestShutdownWithStaleConnection(t *testing.T) { // Open a socket, and don't use it. This connection should then be closed // after the ReadTimeout. c, err := net.Dial("tcp", "localhost:9090") - testutil.Ok(t, err) - t.Cleanup(func() { testutil.Ok(t, c.Close()) }) + assert.NoError(t, err) + t.Cleanup(func() { assert.NoError(t, c.Close()) }) // Stop the web handler. cancel() @@ -464,8 +465,8 @@ func TestShutdownWithStaleConnection(t *testing.T) { func cleanupTestResponse(t *testing.T, resp *http.Response) { _, err := io.Copy(ioutil.Discard, resp.Body) - testutil.Ok(t, err) - testutil.Ok(t, resp.Body.Close()) + assert.NoError(t, err) + assert.NoError(t, resp.Body.Close()) } func cleanupSnapshot(t *testing.T, dbDir string, resp *http.Response) { @@ -475,9 +476,9 @@ func cleanupSnapshot(t *testing.T, dbDir string, resp *http.Response) { } `json:"data"` }{} b, err := ioutil.ReadAll(resp.Body) - testutil.Ok(t, err) - testutil.Ok(t, json.Unmarshal(b, snapshot)) - testutil.Assert(t, snapshot.Data.Name != "", "snapshot directory not returned") - testutil.Ok(t, os.Remove(filepath.Join(dbDir, "snapshots", snapshot.Data.Name))) - testutil.Ok(t, os.Remove(filepath.Join(dbDir, "snapshots"))) + assert.NoError(t, err) + assert.NoError(t, json.Unmarshal(b, snapshot)) + assert.True(t, snapshot.Data.Name != "", "snapshot directory not returned") + assert.NoError(t, os.Remove(filepath.Join(dbDir, "snapshots", snapshot.Data.Name))) + assert.NoError(t, os.Remove(filepath.Join(dbDir, "snapshots"))) } From 74775d7324941f552ac8351c96d8d733ba397b4d Mon Sep 17 00:00:00 2001 From: Victor Araujo Date: Thu, 22 Oct 2020 17:19:44 +0200 Subject: [PATCH 13/65] Add major version tag (#8026) When the latest tag is added, also add a tag for the major version when the version tag matches the latest release defined in VERSION. Signed-off-by: Victor Araujo --- Makefile.common | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Makefile.common b/Makefile.common index 3f3d02cba7..4e325f582a 100644 --- a/Makefile.common +++ b/Makefile.common @@ -245,10 +245,12 @@ common-docker-publish: $(PUBLISH_DOCKER_ARCHS) $(PUBLISH_DOCKER_ARCHS): common-docker-publish-%: docker push "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:$(DOCKER_IMAGE_TAG)" +DOCKER_MAJOR_VERSION_TAG = $(firstword $(subst ., ,$(shell cat VERSION))) .PHONY: common-docker-tag-latest $(TAG_DOCKER_ARCHS) common-docker-tag-latest: $(TAG_DOCKER_ARCHS) $(TAG_DOCKER_ARCHS): common-docker-tag-latest-%: docker tag "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:$(DOCKER_IMAGE_TAG)" "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:latest" + docker tag "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:$(DOCKER_IMAGE_TAG)" "$(DOCKER_REPO)/$(DOCKER_IMAGE_NAME)-linux-$*:v$(DOCKER_MAJOR_VERSION_TAG)" .PHONY: common-docker-manifest common-docker-manifest: From 1cffda5de775668f7438578c90a05e8bfa58eb91 Mon Sep 17 00:00:00 2001 From: James Ranson Date: Thu, 22 Oct 2020 09:22:32 -0600 Subject: [PATCH 14/65] react updates for pathPrefix (#7979) * dynamically determine path prefix Signed-off-by: James Ranson * minor changes per PR review Signed-off-by: James Ranson * use Context for apiPath and pathPrefix Signed-off-by: James Ranson * remove unhandled "/version" path Signed-off-by: James Ranson * only process index once instead of on every req Signed-off-by: James Ranson * remove unneeded tag fragment Signed-off-by: James Ranson * switch api path to const Signed-off-by: James Ranson * revert Signed-off-by: James Ranson * update tests Signed-off-by: James Ranson * linter updates Signed-off-by: James Ranson * simplify Signed-off-by: James Ranson * updates per peer review Signed-off-by: James Ranson --- web/ui/react-app/public/index.html | 5 -- web/ui/react-app/src/App.test.tsx | 3 +- web/ui/react-app/src/App.tsx | 68 +++++++++++++------ web/ui/react-app/src/Navbar.tsx | 27 ++++---- web/ui/react-app/src/constants/constants.tsx | 1 + .../src/contexts/PathPrefixContext.tsx | 9 +++ web/ui/react-app/src/index.tsx | 12 +--- web/ui/react-app/src/pages/alerts/Alerts.tsx | 8 ++- web/ui/react-app/src/pages/config/Config.tsx | 8 ++- web/ui/react-app/src/pages/flags/Flags.tsx | 8 ++- web/ui/react-app/src/pages/graph/Panel.tsx | 12 ++-- .../react-app/src/pages/graph/PanelList.tsx | 27 ++++---- web/ui/react-app/src/pages/rules/Rules.tsx | 8 ++- .../src/pages/serviceDiscovery/Services.tsx | 8 ++- web/ui/react-app/src/pages/status/Status.tsx | 8 ++- .../src/pages/targets/ScrapePoolList.test.tsx | 35 ++++++++-- .../src/pages/targets/ScrapePoolList.tsx | 8 ++- .../src/pages/targets/Targets.test.tsx | 1 - .../react-app/src/pages/targets/Targets.tsx | 8 ++- .../src/pages/tsdbStatus/TSDBStatus.test.tsx | 9 ++- .../src/pages/tsdbStatus/TSDBStatus.tsx | 8 ++- web/ui/react-app/src/types/PathPrefixProps.ts | 5 -- web/web.go | 4 +- 23 files changed, 173 insertions(+), 117 deletions(-) create mode 100644 web/ui/react-app/src/constants/constants.tsx create mode 100644 web/ui/react-app/src/contexts/PathPrefixContext.tsx delete mode 100644 web/ui/react-app/src/types/PathPrefixProps.ts diff --git a/web/ui/react-app/public/index.html b/web/ui/react-app/public/index.html index cc3336fa25..c19bb569bf 100755 --- a/web/ui/react-app/public/index.html +++ b/web/ui/react-app/public/index.html @@ -10,16 +10,11 @@ diff --git a/web/ui/react-app/src/App.test.tsx b/web/ui/react-app/src/App.test.tsx index 705e0bc6b8..e96bbf7a40 100755 --- a/web/ui/react-app/src/App.test.tsx +++ b/web/ui/react-app/src/App.test.tsx @@ -7,7 +7,7 @@ import { Router } from '@reach/router'; import { Alerts, Config, Flags, Rules, ServiceDiscovery, Status, Targets, TSDBStatus, PanelList } from './pages'; describe('App', () => { - const app = shallow(); + const app = shallow(); it('navigates', () => { expect(app.find(Navigation)).toHaveLength(1); @@ -16,7 +16,6 @@ describe('App', () => { [Alerts, Config, Flags, Rules, ServiceDiscovery, Status, Targets, TSDBStatus, PanelList].forEach(component => { const c = app.find(component); expect(c).toHaveLength(1); - expect(c.prop('pathPrefix')).toBe('/path/prefix'); }); expect(app.find(Router)).toHaveLength(1); expect(app.find(Container)).toHaveLength(1); diff --git a/web/ui/react-app/src/App.tsx b/web/ui/react-app/src/App.tsx index d172773e0d..29adb54b6f 100755 --- a/web/ui/react-app/src/App.tsx +++ b/web/ui/react-app/src/App.tsx @@ -5,36 +5,62 @@ import { Container } from 'reactstrap'; import './App.css'; import { Router, Redirect } from '@reach/router'; import { Alerts, Config, Flags, Rules, ServiceDiscovery, Status, Targets, TSDBStatus, PanelList } from './pages'; -import PathPrefixProps from './types/PathPrefixProps'; +import { PathPrefixContext } from './contexts/PathPrefixContext'; interface AppProps { consolesLink: string | null; } -const App: FC = ({ pathPrefix, consolesLink }) => { - return ( - <> - - - - +const App: FC = ({ consolesLink }) => { + // This dynamically/generically determines the pathPrefix by stripping the first known + // endpoint suffix from the window location path. It works out of the box for both direct + // hosting and reverse proxy deployments with no additional configurations required. + let basePath = window.location.pathname; + const paths = [ + '/graph', + '/alerts', + '/status', + '/tsdb-status', + '/flags', + '/config', + '/rules', + '/targets', + '/service-discovery', + ]; + if (basePath.endsWith('/')) { + basePath = basePath.slice(0, -1); + } + if (basePath.length > 1) { + for (let i = 0; i < paths.length; i++) { + if (basePath.endsWith(paths[i])) { + basePath = basePath.slice(0, basePath.length - paths[i].length); + break; + } + } + } + return ( + + + + + {/* - NOTE: Any route added here needs to also be added to the list of - React-handled router paths ("reactRouterPaths") in /web/web.go. - */} - - - - - - - - - + NOTE: Any route added here needs to also be added to the list of + React-handled router paths ("reactRouterPaths") in /web/web.go. + */} + + + + + + + + + - + ); }; diff --git a/web/ui/react-app/src/Navbar.tsx b/web/ui/react-app/src/Navbar.tsx index 51c072add4..a425c33cb2 100644 --- a/web/ui/react-app/src/Navbar.tsx +++ b/web/ui/react-app/src/Navbar.tsx @@ -12,19 +12,20 @@ import { DropdownMenu, DropdownItem, } from 'reactstrap'; -import PathPrefixProps from './types/PathPrefixProps'; +import { usePathPrefix } from './contexts/PathPrefixContext'; interface NavbarProps { consolesLink: string | null; } -const Navigation: FC = ({ pathPrefix, consolesLink }) => { +const Navigation: FC = ({ consolesLink }) => { const [isOpen, setIsOpen] = useState(false); const toggle = () => setIsOpen(!isOpen); + const pathPrefix = usePathPrefix(); return ( - + Prometheus @@ -35,12 +36,12 @@ const Navigation: FC = ({ pathPrefix, consolesLin )} - + Alerts - + Graph @@ -49,25 +50,25 @@ const Navigation: FC = ({ pathPrefix, consolesLin Status - + Runtime & Build Information - + TSDB Status - + Command-Line Flags - + Configuration - + Rules - + Targets - + Service Discovery @@ -76,7 +77,7 @@ const Navigation: FC = ({ pathPrefix, consolesLin Help - Classic UI + Classic UI diff --git a/web/ui/react-app/src/constants/constants.tsx b/web/ui/react-app/src/constants/constants.tsx new file mode 100644 index 0000000000..5f0d8b9b4a --- /dev/null +++ b/web/ui/react-app/src/constants/constants.tsx @@ -0,0 +1 @@ +export const API_PATH = '../api/v1'; diff --git a/web/ui/react-app/src/contexts/PathPrefixContext.tsx b/web/ui/react-app/src/contexts/PathPrefixContext.tsx new file mode 100644 index 0000000000..38e4eec096 --- /dev/null +++ b/web/ui/react-app/src/contexts/PathPrefixContext.tsx @@ -0,0 +1,9 @@ +import React from 'react'; + +const PathPrefixContext = React.createContext(''); + +function usePathPrefix() { + return React.useContext(PathPrefixContext); +} + +export { usePathPrefix, PathPrefixContext }; diff --git a/web/ui/react-app/src/index.tsx b/web/ui/react-app/src/index.tsx index f6e672829a..9398bc8983 100755 --- a/web/ui/react-app/src/index.tsx +++ b/web/ui/react-app/src/index.tsx @@ -6,20 +6,10 @@ import 'bootstrap/dist/css/bootstrap.min.css'; import { isPresent } from './utils'; // Declared/defined in public/index.html, value replaced by Prometheus when serving bundle. -declare const GLOBAL_PATH_PREFIX: string; declare const GLOBAL_CONSOLES_LINK: string; -let prefix = GLOBAL_PATH_PREFIX; let consolesLink: string | null = GLOBAL_CONSOLES_LINK; -if (GLOBAL_PATH_PREFIX === 'PATH_PREFIX_PLACEHOLDER' || GLOBAL_PATH_PREFIX === '/' || !isPresent(GLOBAL_PATH_PREFIX)) { - // Either we are running the app outside of Prometheus, so the placeholder value in - // the index.html didn't get replaced, or we have a '/' prefix, which we also need to - // normalize to '' to make concatenations work (prefixes like '/foo/bar/' already get - // their trailing slash stripped by Prometheus). - prefix = ''; -} - if ( GLOBAL_CONSOLES_LINK === 'CONSOLES_LINK_PLACEHOLDER' || GLOBAL_CONSOLES_LINK === '' || @@ -28,4 +18,4 @@ if ( consolesLink = null; } -ReactDOM.render(, document.getElementById('root')); +ReactDOM.render(, document.getElementById('root')); diff --git a/web/ui/react-app/src/pages/alerts/Alerts.tsx b/web/ui/react-app/src/pages/alerts/Alerts.tsx index c6fef5b767..a1624d7a87 100644 --- a/web/ui/react-app/src/pages/alerts/Alerts.tsx +++ b/web/ui/react-app/src/pages/alerts/Alerts.tsx @@ -1,14 +1,16 @@ import React, { FC } from 'react'; import { RouteComponentProps } from '@reach/router'; -import PathPrefixProps from '../../types/PathPrefixProps'; import { useFetch } from '../../hooks/useFetch'; import { withStatusIndicator } from '../../components/withStatusIndicator'; import AlertsContent, { RuleStatus, AlertsProps } from './AlertContents'; +import { usePathPrefix } from '../../contexts/PathPrefixContext'; +import { API_PATH } from '../../constants/constants'; const AlertsWithStatusIndicator = withStatusIndicator(AlertsContent); -const Alerts: FC = ({ pathPrefix = '' }) => { - const { response, error, isLoading } = useFetch(`${pathPrefix}/api/v1/rules?type=alert`); +const Alerts: FC = () => { + const pathPrefix = usePathPrefix(); + const { response, error, isLoading } = useFetch(`${pathPrefix}/${API_PATH}/rules?type=alert`); const ruleStatsCount: RuleStatus = { inactive: 0, diff --git a/web/ui/react-app/src/pages/config/Config.tsx b/web/ui/react-app/src/pages/config/Config.tsx index 1636668e6c..00bb56d19c 100644 --- a/web/ui/react-app/src/pages/config/Config.tsx +++ b/web/ui/react-app/src/pages/config/Config.tsx @@ -2,11 +2,12 @@ import React, { useState, FC } from 'react'; import { RouteComponentProps } from '@reach/router'; import { Button } from 'reactstrap'; import CopyToClipboard from 'react-copy-to-clipboard'; -import PathPrefixProps from '../../types/PathPrefixProps'; import './Config.css'; import { withStatusIndicator } from '../../components/withStatusIndicator'; import { useFetch } from '../../hooks/useFetch'; +import { usePathPrefix } from '../../contexts/PathPrefixContext'; +import { API_PATH } from '../../constants/constants'; type YamlConfig = { yaml?: string }; @@ -44,8 +45,9 @@ export const ConfigContent: FC = ({ error, data }) => { ); }; -const Config: FC = ({ pathPrefix }) => { - const { response, error } = useFetch(`${pathPrefix}/api/v1/status/config`); +const Config: FC = () => { + const pathPrefix = usePathPrefix(); + const { response, error } = useFetch(`${pathPrefix}/${API_PATH}/status/config`); return ; }; diff --git a/web/ui/react-app/src/pages/flags/Flags.tsx b/web/ui/react-app/src/pages/flags/Flags.tsx index 5f0a409650..60b67ae771 100644 --- a/web/ui/react-app/src/pages/flags/Flags.tsx +++ b/web/ui/react-app/src/pages/flags/Flags.tsx @@ -3,7 +3,8 @@ import { RouteComponentProps } from '@reach/router'; import { Table } from 'reactstrap'; import { withStatusIndicator } from '../../components/withStatusIndicator'; import { useFetch } from '../../hooks/useFetch'; -import PathPrefixProps from '../../types/PathPrefixProps'; +import { usePathPrefix } from '../../contexts/PathPrefixContext'; +import { API_PATH } from '../../constants/constants'; interface FlagMap { [key: string]: string; @@ -34,8 +35,9 @@ const FlagsWithStatusIndicator = withStatusIndicator(FlagsContent); FlagsContent.displayName = 'Flags'; -const Flags: FC = ({ pathPrefix = '' }) => { - const { response, error, isLoading } = useFetch(`${pathPrefix}/api/v1/status/flags`); +const Flags: FC = () => { + const pathPrefix = usePathPrefix(); + const { response, error, isLoading } = useFetch(`${pathPrefix}/${API_PATH}/status/flags`); return ; }; diff --git a/web/ui/react-app/src/pages/graph/Panel.tsx b/web/ui/react-app/src/pages/graph/Panel.tsx index 29dbbc4dc7..bd103de4d2 100644 --- a/web/ui/react-app/src/pages/graph/Panel.tsx +++ b/web/ui/react-app/src/pages/graph/Panel.tsx @@ -10,8 +10,8 @@ import { GraphTabContent } from './GraphTabContent'; import DataTable from './DataTable'; import TimeInput from './TimeInput'; import QueryStatsView, { QueryStats } from './QueryStatsView'; -import PathPrefixProps from '../../types/PathPrefixProps'; import { QueryParams } from '../../types/types'; +import { API_PATH } from '../../constants/constants'; interface PanelProps { options: PanelOptions; @@ -21,6 +21,7 @@ interface PanelProps { metricNames: string[]; removePanel: () => void; onExecuteQuery: (query: string) => void; + pathPrefix: string; } interface PanelState { @@ -55,7 +56,7 @@ export const PanelDefaultOptions: PanelOptions = { stacked: false, }; -class Panel extends Component { +class Panel extends Component { private abortInFlightFetch: (() => void) | null = null; constructor(props: PanelProps) { @@ -117,21 +118,20 @@ class Panel extends Component { let path: string; switch (this.props.options.type) { case 'graph': - path = '/api/v1/query_range'; + path = 'query_range'; params.append('start', startTime.toString()); params.append('end', endTime.toString()); params.append('step', resolution.toString()); - // TODO path prefix here and elsewhere. break; case 'table': - path = '/api/v1/query'; + path = 'query'; params.append('time', endTime.toString()); break; default: throw new Error('Invalid panel type "' + this.props.options.type + '"'); } - fetch(`${this.props.pathPrefix}${path}?${params}`, { + fetch(`${this.props.pathPrefix}/${API_PATH}/${path}?${params}`, { cache: 'no-store', credentials: 'same-origin', signal: abortController.signal, diff --git a/web/ui/react-app/src/pages/graph/PanelList.tsx b/web/ui/react-app/src/pages/graph/PanelList.tsx index 8444b09872..b8e29bcb0f 100644 --- a/web/ui/react-app/src/pages/graph/PanelList.tsx +++ b/web/ui/react-app/src/pages/graph/PanelList.tsx @@ -4,10 +4,11 @@ import { Alert, Button } from 'reactstrap'; import Panel, { PanelOptions, PanelDefaultOptions } from './Panel'; import Checkbox from '../../components/Checkbox'; -import PathPrefixProps from '../../types/PathPrefixProps'; import { generateID, decodePanelOptionsFromQueryString, encodePanelOptionsToQueryString, callAll } from '../../utils'; import { useFetch } from '../../hooks/useFetch'; import { useLocalStorage } from '../../hooks/useLocalStorage'; +import { usePathPrefix } from '../../contexts/PathPrefixContext'; +import { API_PATH } from '../../constants/constants'; export type PanelMeta = { key: string; options: PanelOptions; id: string }; @@ -16,20 +17,14 @@ export const updateURL = (nextPanels: PanelMeta[]) => { window.history.pushState({}, '', query); }; -interface PanelListProps extends PathPrefixProps, RouteComponentProps { +interface PanelListProps extends RouteComponentProps { panels: PanelMeta[]; metrics: string[]; useLocalTime: boolean; queryHistoryEnabled: boolean; } -export const PanelListContent: FC = ({ - metrics = [], - useLocalTime, - pathPrefix, - queryHistoryEnabled, - ...rest -}) => { +export const PanelListContent: FC = ({ metrics = [], useLocalTime, queryHistoryEnabled, ...rest }) => { const [panels, setPanels] = useState(rest.panels); const [historyItems, setLocalStorageHistoryItems] = useLocalStorage('history', []); @@ -73,10 +68,13 @@ export const PanelListContent: FC = ({ ]); }; + const pathPrefix = usePathPrefix(); + return ( <> {panels.map(({ id, options }) => ( = ({ useLocalTime={useLocalTime} metricNames={metrics} pastQueries={queryHistoryEnabled ? historyItems : []} - pathPrefix={pathPrefix} /> ))}