-
Notifications
You must be signed in to change notification settings - Fork 179
Add tests for tsdb cli tool #673
base: master
Are you sure you want to change the base?
Conversation
/cc @krasi-georgiev |
I've just realized that there are now circular imports due to factoring out utility functions. Will have to deal with this first. Edit: Fixed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep very good work indeed.
Still looks a bit hacky and looks like it will brake quite often, but time will tell if it would be easy to maintain and useful to catch bugs.
testutil/db/db.go
Outdated
) | ||
|
||
// CreateBlock creates a block with given set of series and returns its dir. | ||
func CreateBlock(tb testing.TB, dir string, series []tsdb.Series) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hm , we need to find a way to avoid duplicating all this code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree! This is going to be a bigger change though since just using the new function in the old test leads to import cycles (tsdb
-> testutil/db
-> tsdb
). I'll work on testing the analyze
function first, might be more functions that need to be moved out. I'll try to think of a solution after that, most likely involving some interface. Let me know if you have any suggestions!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in the past I have used this. Not sure if it will help here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I looked at that possibility but moving the existing tests into a new package would require moving lots of stuff around which seemed very nasty. The easiest way seems to be to just move CreateBlock
and GenSeries
back into the tsdb
package (new file: mocks.go
) but mark them clearly as test functions: MockCreateBlock
and MockGenSeries
. I have also moved dependent functions and types into mocks.go
and deleted them where they were defined previously.
cmd/tsdb/main_test.go
Outdated
safeDBOptions.RetentionDuration = 0 | ||
|
||
testutildb.CreateBlock(nil, tmpdir, testutildb.GenSeries(1, 1, 0, 1)) | ||
db, err := tsdb.Open(tmpdir, nil, nil, &safeDBOptions) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do you need to open and close a db before you open it as read only?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Forgot to remove this after refactoring, fixed now.
- Moving necessary functions into different package (only copy) - Output not checked yet Signed-off-by: obitech <[email protected]>
Signed-off-by: obitech <[email protected]>
Signed-off-by: obitech <[email protected]>
Signed-off-by: obitech <[email protected]>
Signed-off-by: obitech <[email protected]>
Signed-off-by: obitech <[email protected]>
Co-Authored-By: Krasi Georgiev <[email protected]> Signed-off-by: obitech <[email protected]>
Signed-off-by: obitech <[email protected]>
- Factor out `extractBlock()` to retrieve specific block from db.Blocks() - Write tests for said function - Simple, non-output test for `analyzeBlock()` command Signed-off-by: obitech <[email protected]>
Signed-off-by: obitech <[email protected]>
@krasi-georgiev Tests for the |
… existing test files Signed-off-by: obitech <[email protected]>
mocks.go
Outdated
|
||
// MockCreateBlock creates a block with given set of series and returns its dir. | ||
// Intended for testing purposes. | ||
func MockCreateBlock(tb testing.TB, dir string, series []Series) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer the original name. just need to find a good package name or put it one of the existing packages.
func MockCreateBlock(tb testing.TB, dir string, series []Series) string { | |
func CreateBlock(tb testing.TB, dir string, series []Series) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, I renamed it back to CreateBlock
.
mocks.go
Outdated
|
||
// MockGenSeries generates series with a given number of labels and values. | ||
// Intended for testing purposes. | ||
func MockGenSeries(totalSeries, labelCount int, mint, maxt int64) []Series { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
func MockGenSeries(totalSeries, labelCount int, mint, maxt int64) []Series { | |
func GenSeries(totalSeries, labelCount int, mint, maxt int64) []Series { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
Signed-off-by: obitech <[email protected]>
Should I squash into a single commit? |
|
||
// MockCreateBlock creates a block with given set of series and returns its dir. | ||
// Intended for testing purposes. | ||
func CreateBlock(tb testing.TB, dir string, series []Series) string { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No way to put all these in tsdbutil package ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not without massive refactoring I think. A lot of the functionality in mocks.go
depends on stuff from the tsdb
package: for example CreateBlock
calls createHead
which calls Head()
from head.go
. Putting all these into tsdbutil
would mean we have to import tsdb
into tsdbutil
, which is a circular import (since tsdb
imports tsdbutil
in a lot of places).
To avoid this I can see three options:
- Come up with some clever interfaces that we can implement in both
package tsdb
(+ tests), as well as in the tests ofpackage main
. - Move all existing tests into a new package, then import from both
tsdb
andtsdbutil
. - Export
CreateBlock
andGenSeries
fromtsdb
.
I think option 1 and 2 require bigger changes in regards to architecture and refactoring and would increase the size of this PR by around x5 which might be out of scope for this PR. So I thought just leaving those functions in tsdb
and exporting them is the least-intrusive way forward for this. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure,
would be interested to see what @codesome @gouthamve @bwplotka think.
Signed-off-by: obitech <[email protected]>
no need |
This PR introduces tests for the TSDB CLI tool, verifying the output is correct.
A requirement for this is to refactor out certain utility functions such as
createBlock
(->CreateBlock
) andgenSeries
(->GenSeries
) into a new file,mocks.go
, so they can be imported by packagemain
. Certain other types and functions that are required by those test functions are moved out of the respective test files, where they were defined previously, intomocks.go
.In order to work around annoying
\t
expansion inos.Stdout
, all white space is stripped fromexpected
andactual
inmain_test.go
Resolves #615
Test
ls
printBlocks()
now takes anio.Writer
which can be tested with abytes.Buffer
.Test
analyze
analyzeBlock()
now takse anio.Writer
which can be tests with abytes.Buffer
.[]BlockReader
has been factored out into the functionextractBlock()
, in order to increase testability.TODO
ls
analyze