Skip to content

Get TestData back into the metricsets#11818

Merged
jsoriano merged 16 commits intoelastic:masterfrom
jsoriano:testdata-per-module-back
Jun 20, 2019
Merged

Get TestData back into the metricsets#11818
jsoriano merged 16 commits intoelastic:masterfrom
jsoriano:testdata-per-module-back

Conversation

@jsoriano
Copy link
Copy Markdown
Member

@jsoriano jsoriano commented Apr 15, 2019

Add helpers to the recently added testdata framework so modules can still
be tested with go test ./metricbeat/module/foo/....

It keeps by now the TestAll test, we can revisit this decission in the future,
while this is kept these tests will be run twice on CI.

@jsoriano jsoriano added module discuss Issue needs further discussion. Metricbeat Metricbeat :Testing labels Apr 15, 2019
@jsoriano jsoriano requested review from ruflin and sayden April 15, 2019 12:21
@jsoriano jsoriano requested review from a team as code owners April 15, 2019 12:21
Comment thread metricbeat/mb/testing/testdata.go Outdated
Comment thread metricbeat/mb/testing/testdata.go Outdated
Comment thread metricbeat/mb/testing/testdata.go Outdated
Comment thread metricbeat/mb/testing/testdata.go Outdated
Comment thread metricbeat/mb/testing/testdata.go Outdated
Comment thread metricbeat/mb/testing/testdata.go
Copy link
Copy Markdown
Contributor

@sayden sayden left a comment

Choose a reason for hiding this comment

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

Thanks for contributing with this! Left a couple of comments

Comment thread metricbeat/module/traefik/health/health_test.go
Comment thread metricbeat/module/traefik/health/health_test.go Outdated
@sayden sayden added the in progress Pull request is currently in progress. label Apr 15, 2019
@ruflin
Copy link
Copy Markdown
Contributor

ruflin commented Apr 15, 2019

What bugs me about the current approach we have is that go test . inside a metricset does not work. This is solved by this PR. But there are 2 downsides here:

  • It introduces additional boilerplate code (I could leave with that)
  • In case someone adds the testsdata but forgets to add the TestData function, tests are not run.

It's easy to miss the second point in a review. It means all data could have been added but is not run properly and we will not detect it.

I wonder if there is any other trick that we can make go test . working but have TestAll still around? I'm not even worried if things are executed twice as it should be super quick to execute. I rather pay the double execution than missing some tests.

@exekias
Copy link
Copy Markdown
Contributor

exekias commented Apr 15, 2019

Having go test . in the module folder is great, it makes modules development standalone, no need to exit their folder.

I agree with the second point, TestAll could be mimicked with this (maybe, I believe?):

  • Remove all expected files
  • Run tests with generate flag
  • git diff should return nothing

@jsoriano
Copy link
Copy Markdown
Member Author

* It introduces additional boilerplate code (I could leave with that)

I am trying it to be the minimal needed boilerplate, but I think that an explicit test is required, and even desired.

* In case someone adds the testsdata but forgets to add the `TestData` function, tests are not run.

It's easy to miss the second point in a review. It means all data could have been added but is not run properly and we will not detect it.

I think we can live with this risk 🙂 Something similar would happen with any other test that uses external files, one may add the files but forget the test.
In this case the risk is mitigated because the framework is also helpful in generating the expected outputs and the data.json file, so it will be missed if forgotten 🙂

Another thing that mitigates the risk is that this way all metricsets continue having tests, it is easier for newcomers to understand how the module is being tested, they don't need to guess that there are some testdata files in _meta that are somehow used for tests. They can see a test, that calls the helpers, and then look at the docs (or the code) of the helpers.

Relying on conventions is also prone to add things that are not interpreted as expected or ignored by the framework.

I wonder if there is any other trick that we can make go test . working but have TestAll still around? I'm not even worried if things are executed twice as it should be super quick to execute. I rather pay the double execution than missing some tests.

I'd like to end-up removing TestAll so we don't continue relying on it, I'd like modules and metricsets to contain their own tests.
We could use general coverage-based solutions to automatically check if a metricset is being tested or not, I'd prefer to base this on something that can be useful for all the code.

For initial migration, and sporadic checks we can follow @exekias recommendation of removing all expected files and run the tests with the generate flag.

Comment thread metricbeat/mb/testing/testdata.go
Comment thread metricbeat/mb/testing/testdata.go Outdated
@ruflin
Copy link
Copy Markdown
Contributor

ruflin commented Apr 17, 2019

One thought I would like to add here: What happens in the future if we have modules without go code?

I think if we keep the TestAll around combined with this change, we have the best of both worlds? Any downside to this expect tests running twice?

@jsoriano
Copy link
Copy Markdown
Member Author

I'd still place the tests for these modules near these modules (we can have go test files in packages without other test code). But ok, we can delay this discussion, I will leave TestAll by now.

@jsoriano jsoriano added review and removed review labels Apr 17, 2019
@jsoriano jsoriano requested a review from a team as a code owner April 17, 2019 10:59
@jsoriano
Copy link
Copy Markdown
Member Author

I added back the test methods but keeping TestAll too, I tried to regenerate all expected.json file and it worked as expected.

The only problem with go test ./metricset/module/... -generate is that modules without this flag give an error, I guess this is not such a problem as this flag will be mostly used when working on metricsets.

@jsoriano jsoriano added review Team:Integrations Label for the Integrations team and removed in progress Pull request is currently in progress. labels Apr 17, 2019
@jsoriano jsoriano self-assigned this Apr 17, 2019
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is the only thing that changed after regenerating all files.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm surprised this changed (it shouldn't). @sayden I remember we had some discussion around apache and "whitelist" some files which should not be remove or similar?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, I finally added a config in TestAll to omit specific fields or fields group from the comparison because they were changing in each regenerate https://github.com/elastic/beats/pull/11714/files#diff-5b51ecdcb61bd7977eb4d2e2852178c0R32

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

And why did it still change here?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@jsoriano jsoriano force-pushed the testdata-per-module-back branch from 77c7529 to fb70ba0 Compare April 17, 2019 11:09
@@ -1,7 +0,0 @@
type: http
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We don't need this anymore?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I couldn't find where this was being used, so I removed it.

Could this be the cause of the change in apache hostname?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No. It was there as reference. Please, undo 🙂

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Apache hostname is changing because it takes the hostname of the beat and writes it in each execution at the metricset level. We discussed about leaving it like this for compatibility reasons. Here you can see that m.Host is passed to the eventMapping which sets it here

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Wdyt about adding it as example in the godocs? If my future self finds this file alone here he will probably wonder again what it is.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Apache hostname is changing because it takes the hostname of the beat and writes it in each execution at the metricset level. We discussed about leaving it like this for compatibility reasons. Here you can see that m.Host is passed to the eventMapping which sets it here

Ok, I see it also changes when running TestAll with generate, I won't change this in this PR.


"github.com/stretchr/testify/assert"

_ "github.com/elastic/beats/metricbeat/module/apache"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why does the module need to be included? Does otherwise some functionality miss?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is needed for the fields checks, without this the module fields are not included and then the check fails. The TestAll version includes all modules when including github.com/elastic/beats/metricbeat/include.

I was tempted of just moving this include to mb/testing, but this way we check that each module defines its own fields.

Common fields are included from mb/testing.

Not sure if there is other way of including the module fields only.

Copy link
Copy Markdown
Contributor

@ruflin ruflin left a comment

Choose a reason for hiding this comment

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

LGTM

I like the hybrid we have. I can see potential that in some cases the local tests will break because of some ECS fields needed which then are not there. But lets cross this bridge when we get there.

@jsoriano jsoriano force-pushed the testdata-per-module-back branch from e6ba9ed to 721ac57 Compare April 25, 2019 15:30
@jsoriano jsoriano force-pushed the testdata-per-module-back branch from 8110adb to 58345ed Compare June 18, 2019 11:43
Copy link
Copy Markdown
Contributor

@exekias exekias left a comment

Choose a reason for hiding this comment

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

Let's see if tests pass 🙏

@jsoriano jsoriano merged commit fd4058b into elastic:master Jun 20, 2019
@jsoriano jsoriano deleted the testdata-per-module-back branch June 20, 2019 12:00
@jsoriano jsoriano mentioned this pull request Jun 20, 2019
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

discuss Issue needs further discussion. Metricbeat Metricbeat module review Team:Integrations Label for the Integrations team :Testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants