Skip to content
This repository has been archived by the owner on Aug 13, 2019. It is now read-only.

Add tests for tsdb cli tool #673

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

obitech
Copy link

@obitech obitech commented Aug 4, 2019

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) and genSeries (-> GenSeries) into a new file, mocks.go, so they can be imported by package main. 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, into mocks.go.

In order to work around annoying \t expansion in os.Stdout, all white space is stripped from expected and actual in main_test.go

Resolves #615

Test ls

  • printBlocks() now takes an io.Writer which can be tested with a bytes.Buffer.

Test analyze

  • analyzeBlock() now takse an io.Writer which can be tests with a bytes.Buffer.
  • The functionality of retrieving a block by ID, out of a []BlockReader has been factored out into the function extractBlock(), in order to increase testability.

TODO

  • Test ls
  • Test analyze
  • Delete original references of factored-out util functions & adjust imports in existing files

@obitech
Copy link
Author

obitech commented Aug 4, 2019

/cc @krasi-georgiev

@obitech
Copy link
Author

obitech commented Aug 4, 2019

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

Copy link
Contributor

@krasi-georgiev krasi-georgiev left a 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.

)

// CreateBlock creates a block with given set of series and returns its dir.
func CreateBlock(tb testing.TB, dir string, series []tsdb.Series) string {
Copy link
Contributor

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

Copy link
Author

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!

Copy link
Contributor

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.

Copy link
Author

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 Show resolved Hide resolved
cmd/tsdb/main_test.go Outdated Show resolved Hide resolved
safeDBOptions.RetentionDuration = 0

testutildb.CreateBlock(nil, tmpdir, testutildb.GenSeries(1, 1, 0, 1))
db, err := tsdb.Open(tmpdir, nil, nil, &safeDBOptions)
Copy link
Contributor

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?

Copy link
Author

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.

cmd/tsdb/main_test.go Outdated Show resolved Hide resolved
obitech and others added 10 commits August 9, 2019 21:45
- Moving necessary functions into different package (only copy)
- Output not checked yet

Signed-off-by: obitech <ajknipping@mailbox.org>
Signed-off-by: obitech <ajknipping@mailbox.org>
Signed-off-by: obitech <ajknipping@mailbox.org>
Signed-off-by: obitech <ajknipping@mailbox.org>
Signed-off-by: obitech <ajknipping@mailbox.org>
Signed-off-by: obitech <ajknipping@mailbox.org>
Co-Authored-By: Krasi Georgiev <8903888+krasi-georgiev@users.noreply.github.com>
Signed-off-by: obitech <ajknipping@mailbox.org>
Signed-off-by: obitech <ajknipping@mailbox.org>
- 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 <ajknipping@mailbox.org>
Signed-off-by: obitech <ajknipping@mailbox.org>
@obitech
Copy link
Author

obitech commented Aug 9, 2019

@krasi-georgiev Tests for the analyze cmd are done.

… existing test files

Signed-off-by: obitech <ajknipping@mailbox.org>
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 {
Copy link
Contributor

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.

Suggested change
func MockCreateBlock(tb testing.TB, dir string, series []Series) string {
func CreateBlock(tb testing.TB, dir string, series []Series) string {

Copy link
Author

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 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func MockGenSeries(totalSeries, labelCount int, mint, maxt int64) []Series {
func GenSeries(totalSeries, labelCount int, mint, maxt int64) []Series {

Copy link
Author

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 <ajknipping@mailbox.org>
@obitech
Copy link
Author

obitech commented Aug 11, 2019

Should I squash into a single commit?

@obitech obitech changed the title WIP: Add tests for tsdb cli tool Add tests for tsdb cli tool Aug 11, 2019

// 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 {
Copy link
Contributor

@krasi-georgiev krasi-georgiev Aug 11, 2019

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 ?

Copy link
Author

@obitech obitech Aug 11, 2019

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:

  1. Come up with some clever interfaces that we can implement in both package tsdb (+ tests), as well as in the tests of package main.
  2. Move all existing tests into a new package, then import from both tsdb and tsdbutil.
  3. Export CreateBlock and GenSeries from tsdb.

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?

Copy link
Contributor

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 <ajknipping@mailbox.org>
@krasi-georgiev
Copy link
Contributor

Should I squash into a single commit?

no need

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add tests for the tsdb cli tool
2 participants