Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

High level REST client XContent tests are misleading #39745

Closed
jasontedor opened this issue Mar 6, 2019 · 7 comments
Closed

High level REST client XContent tests are misleading #39745

jasontedor opened this issue Mar 6, 2019 · 7 comments
Assignees

Comments

@jasontedor
Copy link
Member

Many high-level REST client XContent tests where we use a dedicated client class (e.g., org.elasticsearch.client.core.AcknowledgeResponse) are misleading and not actually testing what occurs in production.

For example, let us consider the case of org.elasticsearch.core.AcknowledgedResponse for which XContent is tested via org.elasticsearch.client.core.AcknowledgedResponseTests. However, this test does not actually test what occurs in production.

What occurs in production is that a concrete instance of org.elasticsearch.action.support.master.AcknowledgedResponse is serialized to XContent on the REST layer, and then client side is deserialized from XContent by the HLRC. We need confidence that this translation is working.

The test that we have today based on AbstractXContentTestCase#xContentTester does not actually test this. Instead, it creates a concrete instance of org.elasticsearch.client.core.AcknowledgedResponse, converts this to XContent via non-production test code, and then checks that that XContent can be converted back to an instance of org.elasticsearch.client.core.AcknowledgedResponse. This does not give us confidence that the translation between a serialized XContent of org.elasticsearch.action.support.master.AcknowledgedResponse is properly deserialized to an instance of org.elasticsearch.client.core.AcknowledgedResponse in the HLRC. That's the production code path that we want to test.

We probably want a dedicated framework like AbstractXContentTestCase#xContentTester for this. See #39713 comment for the discussion that triggered this issue, and a test that is based on production code rather than non-production code.

@elasticmachine
Copy link
Collaborator

Pinging @elastic/es-core-features

@hub-cap
Copy link
Contributor

hub-cap commented Mar 8, 2019

Yes I agree with you. @jtibshirani brought this up to me recently-ish and we arrived at the conclusion that if the toXContent method in the test creates a server response, it can then use the server responses toXContent method to turn the data into json for testing. You can see an example here

Do you think this is enough to mimic production code? cc @martijnvg who was on the other issue as well.

@jasontedor
Copy link
Member Author

Yes, that is similar to what I did (although I did not go from client to server to XContent to client, instead server to XContent to client):

final org.elasticsearch.action.support.broadcast.BroadcastResponse to =
new org.elasticsearch.action.support.broadcast.BroadcastResponse(total, successful, failed, failures);
final XContentType xContentType = randomFrom(XContentType.values());
final BytesReference bytes = toShuffledXContent(to, xContentType, ToXContent.EMPTY_PARAMS, randomBoolean());
final XContent xContent = XContentFactory.xContent(xContentType);
final XContentParser parser = xContent.createParser(
new NamedXContentRegistry(ClusterModule.getNamedXWriteables()),
LoggingDeprecationHandler.INSTANCE,
bytes.streamInput());
final BroadcastResponse from = BroadcastResponse.fromXContent(parser);

@martijnvg
Copy link
Member

Do you think this is enough to mimic production code?

At least for xpack hlrc, I think this the best we can do without pulling in xpack modules as a test dependency for hlrc tests. Maybe we should consider adding xpack-core as test dependency to hlrc? This will make the request/response unit tests much more realistic.

martijnvg added a commit to martijnvg/elasticsearch that referenced this issue Mar 8, 2019
The base class facilitates generating a server side response test instance,
that gets serialized as xcontent, which then gets parsed into a hlrc response
instance, which then gets asserted against the server side response instance.

This way of testing is more realistic then how hlrc response classes are tested
today, which basically tests that serialization works by generating
hlrc response instance, serialize that to xcontent and then parse it back
to a hlrc response instance.

Besides adding a base test class, this change also cuts AcknowledgedResponseTests
and BroadcastResponseTests over to use this base class.

Relates to elastic#39745
@hub-cap
Copy link
Contributor

hub-cap commented Mar 8, 2019

Cool, let me eyeball this. going from server to xContent to client is certainly less (and there may be things set on the server objects that we dont expose client side). Im thinking your approach is better, but it definitely needs to be frameworked to be sure we test additional things like inserting additional random fields into the json. Im self assigning to look at how we can do this.

@hub-cap hub-cap self-assigned this Mar 8, 2019
@hub-cap
Copy link
Contributor

hub-cap commented Mar 8, 2019

also Im perfectly fine with having a test dependency on server/xpack, since we are testing that those do ser/deser properly from server (and to server, as we discussed as well @martijnvg )

martijnvg added a commit that referenced this issue Mar 20, 2019
The base class facilitates generating a server side response test instance,
that gets serialized as xcontent, which then gets parsed into a hlrc response
instance, which then gets asserted against the server side response instance.

This way of testing is more realistic then how hlrc response classes are tested
today, which basically tests that serialization works by generating
hlrc response instance, serialize that to xcontent and then parse it back
to a hlrc response instance.

Besides adding a base test class, this change also cuts AcknowledgedResponseTests
and BroadcastResponseTests over to use this base class.

Relates to #39745
martijnvg added a commit that referenced this issue Mar 20, 2019
The base class facilitates generating a server side response test instance,
that gets serialized as xcontent, which then gets parsed into a hlrc response
instance, which then gets asserted against the server side response instance.

This way of testing is more realistic then how hlrc response classes are tested
today, which basically tests that serialization works by generating
hlrc response instance, serialize that to xcontent and then parse it back
to a hlrc response instance.

Besides adding a base test class, this change also cuts AcknowledgedResponseTests
and BroadcastResponseTests over to use this base class.

Relates to #39745
martijnvg added a commit to martijnvg/elasticsearch that referenced this issue Mar 20, 2019
…lass.

This way the response classes are tested in a more realistic setting.

Relates to elastic#39745
martijnvg added a commit to martijnvg/elasticsearch that referenced this issue Mar 22, 2019
change hlrc ccr request tests to use AbstractRequestTestCase base class.

This way the request classes are tested in a more realistic setting.
Note this change also adds a test dependency on xpack core module.

Similar to elastic#39844 but then for hlrc request serialization tests.

Relates to elastic#39745
martijnvg added a commit that referenced this issue Apr 8, 2019
…lass. (#40257)

This way the response classes are tested in a more realistic setting.

Relates to #39745
martijnvg added a commit that referenced this issue Apr 8, 2019
…lass. (#40257)

This way the response classes are tested in a more realistic setting.

Relates to #39745
martijnvg added a commit to martijnvg/elasticsearch that referenced this issue Apr 9, 2019
…lass

This way the response classes are tested in a more realistic setting.
Note this change also adds a test dependency on xpack core module.

Relates to elastic#39745
martijnvg added a commit that referenced this issue Apr 10, 2019
changed hlrc ccr request tests to use AbstractRequestTestCase base class.

This way the request classes are tested in a more realistic setting.
Note this change also adds a test dependency on xpack core module.

Similar to #39844 but then for hlrc request serialization tests.

Removed iterators from hlrc parsing tests.
Use empty xcontent registries.

Relates to #39745
martijnvg added a commit that referenced this issue Apr 10, 2019
changed hlrc ccr request tests to use AbstractRequestTestCase base class.

This way the request classes are tested in a more realistic setting.
Note this change also adds a test dependency on xpack core module.

Similar to #39844 but then for hlrc request serialization tests.

Removed iterators from hlrc parsing tests.
Use empty xcontent registries.

Relates to #39745
gurkankaymak pushed a commit to gurkankaymak/elasticsearch that referenced this issue May 27, 2019
…lass. (elastic#40257)

This way the response classes are tested in a more realistic setting.

Relates to elastic#39745
gurkankaymak pushed a commit to gurkankaymak/elasticsearch that referenced this issue May 27, 2019
…#40362)

changed hlrc ccr request tests to use AbstractRequestTestCase base class.

This way the request classes are tested in a more realistic setting.
Note this change also adds a test dependency on xpack core module.

Similar to elastic#39844 but then for hlrc request serialization tests.

Removed iterators from hlrc parsing tests.
Use empty xcontent registries.

Relates to elastic#39745
hub-cap added a commit to hub-cap/elasticsearch that referenced this issue Oct 23, 2019
The AbstractHlrcWriteableXContentTestCase was replaced by a better test
case a while ago, and this is the last two instances using it. They have
been converted and the test is now deleted.

Ref elastic#39745
hub-cap added a commit that referenced this issue Oct 24, 2019
The AbstractHlrcWriteableXContentTestCase was replaced by a better test
case a while ago, and this is the last two instances using it. They have
been converted and the test is now deleted.

Ref #39745
hub-cap added a commit that referenced this issue Oct 24, 2019
The AbstractHlrcWriteableXContentTestCase was replaced by a better test
case a while ago, and this is the last two instances using it. They have
been converted and the test is now deleted.

Ref #39745
hub-cap added a commit to hub-cap/elasticsearch that referenced this issue Oct 29, 2019
The old graph tests were duplicated a lot and used a deprecated parent
class. This commit cleans that up and removes one of the duplicated
tests.

Ref elastic#39745
hub-cap added a commit to hub-cap/elasticsearch that referenced this issue Oct 29, 2019
This test uses a deprecated base class, and this commit moves it over
to the new class.

Ref elastic#39745
hub-cap added a commit that referenced this issue Oct 30, 2019
The old graph tests were duplicated a lot and used a deprecated parent
class. This commit cleans that up and removes one of the duplicated
tests.

Ref #39745
hub-cap added a commit that referenced this issue Oct 30, 2019
This test uses a deprecated base class, and this commit moves it over
to the new class.

Ref #39745
hub-cap added a commit to hub-cap/elasticsearch that referenced this issue Oct 30, 2019
This commit finishes cleaning up the AbstractHlrcXContentTestCase work
and removes this class. All classes that were using this are now using
the updated base class.

Ref elastic#39745
hub-cap added a commit that referenced this issue Oct 30, 2019
This commit finishes cleaning up the AbstractHlrcXContentTestCase work
and removes this class. All classes that were using this are now using
the updated base class.

Ref #39745
hub-cap added a commit that referenced this issue Oct 30, 2019
The old graph tests were duplicated a lot and used a deprecated parent
class. This commit cleans that up and removes one of the duplicated
tests.

Ref #39745
hub-cap added a commit that referenced this issue Oct 30, 2019
This test uses a deprecated base class, and this commit moves it over
to the new class.

Ref #39745
hub-cap added a commit that referenced this issue Oct 30, 2019
This commit finishes cleaning up the AbstractHlrcXContentTestCase work
and removes this class. All classes that were using this are now using
the updated base class.

Ref #39745
@hub-cap
Copy link
Contributor

hub-cap commented Oct 30, 2019

Chatted w @martijnvg and we think this can be closed with those two old base tests gone.

@hub-cap hub-cap closed this as completed Oct 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants