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

Unit testable index creation task on MetaDataCreateIndexService #25961

Merged
merged 11 commits into from
Aug 29, 2017

Conversation

fred84
Copy link
Contributor

@fred84 fred84 commented Jul 30, 2017

Converting anonymous subclass of AckedClusterStateUpdateTask in MetaDataCreateIndexService to inner class to make it unit testable. Relates to #25373 and #25380 .

@jasontedor I've tried to make diff as small as possible. Please take a look.

@elasticmachine
Copy link
Collaborator

Since this is a community submitted pull request, a Jenkins build has not been kicked off automatically. Can an Elastic organization member please verify the contents of this patch and then kick off a build manually?

@jasontedor
Copy link
Member

@fred84 I'm only letting you know that I'm going on vacation until 2017-08-14 and I will look at this when I return (unless someone else reviews it before me).

@fred84
Copy link
Contributor Author

fred84 commented Aug 4, 2017

@jasontedor have a good holiday :)

@jasontedor
Copy link
Member

This is on my list to review tomorrow.

@jasontedor
Copy link
Member

test this please

@fred84
Copy link
Contributor Author

fred84 commented Aug 24, 2017

@jasontedor Jenkins failures seems unrelated to my changes. I cant reproduce them locally.

@jasontedor
Copy link
Member

retest this please

Copy link
Member

@jasontedor jasontedor left a comment

Choose a reason for hiding this comment

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

This is a great start. I left some feedback on the tests.

fail("validation exception expected");
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("invalid wait_for_active_shards"));
}
Copy link
Member

Choose a reason for hiding this comment

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

Can you rewrite this block using expectThrows? Also, instead of assertTrue, it's more effective to use the built-in matchers like assertThat(e.getMessage(), hasToString(containsString("invalid wait_for_active_shards"));. The reason for this is because if the assertion fails with assertTrue, the failure message only says something like "expected true but was false" where as with the matchers we get something like "expected "invalid wait_for_active_shards" but was ..." so we already have immediately from the failure message more information about what is happening.

assertFalse(result.metaData().index("test").getAliases().containsKey("alias1"));
assertFalse(result.metaData().index("test").getCustoms().containsKey("custom1"));
assertNull(result.metaData().index("test").getSettings().get("key1"));
assertFalse(getMappingsFromResponse().containsKey("mapping1"));
Copy link
Member

Choose a reason for hiding this comment

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

This is another case where I would use a more effective matcher.

fail("exception not thrown");
} catch (RuntimeException e) {
verify(indicesService, times(1)).removeIndex(anyObject(), anyObject(), anyObject());
}
Copy link
Member

Choose a reason for hiding this comment

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

Can you rewrite this to use expectThrows?

final ClusterState result = executeTask();

assertEquals("12", result.getMetaData().index("test").getSettings().get(SETTING_NUMBER_OF_SHARDS));
assertEquals("3", result.metaData().index("test").getAliases().get("alias1").getSearchRouting());
Copy link
Member

Choose a reason for hiding this comment

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

Can you rewrite these to use assertThat(..., equalTo(...)). I prefer this form because it's clearer which is the expectation and which is the value under test whereas with assertEquals it often gets confused.

final ClusterState result = executeTask();

assertEquals("12", result.getMetaData().index("test").getSettings().get(SETTING_NUMBER_OF_SHARDS));
assertEquals("3", result.metaData().index("test").getAliases().get("alias1").getSearchRouting());
Copy link
Member

Choose a reason for hiding this comment

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

Same comment here about assertEquals, let's use assertThat(..., equalTo(...)) instead.

public void testDefaultSettings() throws Exception {
final ClusterState result = executeTask();

assertEquals("5", result.getMetaData().index("test").getSettings().get(SETTING_NUMBER_OF_SHARDS));
Copy link
Member

Choose a reason for hiding this comment

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

Let's use assertThat(..., equalTo(...)).

assertEquals(mergedCustom, result.metaData().index("test").getCustoms().get("custom1"));
assertEquals("fromReq", result.metaData().index("test").getAliases().get("alias1").getSearchRouting());
assertEquals("reqValue", result.metaData().index("test").getSettings().get("key1"));
assertEquals("{type={properties={field={type=keyword}}}}", getMappingsFromResponse().get("mapping1").toString());
Copy link
Member

Choose a reason for hiding this comment

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

Let's use assertThat(..., equalTo(...)).

assertTrue(result.metaData().index("test").getAliases().containsKey("alias1"));
assertTrue(result.metaData().index("test").getCustoms().containsKey("custom1"));
assertEquals("value1", result.metaData().index("test").getSettings().get("key1"));
assertTrue(getMappingsFromResponse().containsKey("mapping1"));
Copy link
Member

Choose a reason for hiding this comment

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

Let's replace the assertTrue by more effective matchers, and replace the assertEquals by assertThat(..., equalTo(...)).

assertTrue(result.metaData().index("test").getAliases().containsKey("alias1"));
assertTrue(result.metaData().index("test").getCustoms().containsKey("custom1"));
assertEquals("value1", result.metaData().index("test").getSettings().get("key1"));
assertTrue(getMappingsFromResponse().containsKey("mapping1"));
Copy link
Member

Choose a reason for hiding this comment

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

Let's replace the assertTrue by more effective matchers, and replace the assertEquals by assertThat(..., equalTo(...)).


assertTrue(result.metaData().index("test").getAliases().containsKey("alias_from_template_1"));
assertTrue(result.metaData().index("test").getAliases().containsKey("alias_from_template_2"));
assertFalse(result.metaData().index("test").getAliases().containsKey("alias_from_template_3"));
Copy link
Member

Choose a reason for hiding this comment

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

Let's replace the assertTrue and assertFalse by more effective matchers.

@fred84
Copy link
Contributor Author

fred84 commented Aug 26, 2017

@jasontedor PR updated. Thanks for suggestions on matchers.
I created 2 custom matchers for ImmutableOpenMap and not sure it should be placed inside this test class.

@jasontedor
Copy link
Member

retest this please

Copy link
Member

@jasontedor jasontedor left a comment

Choose a reason for hiding this comment

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

I left one more suggestion. The production code looks good. I started another CI run but I suspect it will be fine.

@@ -447,4 +447,52 @@ private void setupIndicesService() throws Exception {

when(indicesService.createIndex(anyObject(), anyObject())).thenReturn(service);
}

private <K> Matcher<ImmutableOpenMap<K, ?>> containsKey(final K key) {
Copy link
Member

@jasontedor jasontedor Aug 27, 2017

Choose a reason for hiding this comment

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

You can put these matchers in test-framework under org.elasticsearch.test.hamcrest.

@fred84
Copy link
Contributor Author

fred84 commented Aug 28, 2017

@jasontedor PR updated.

@jasontedor
Copy link
Member

retest this please

Copy link
Member

@jasontedor jasontedor left a comment

Choose a reason for hiding this comment

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

LGTM.

@jasontedor
Copy link
Member

I will merge this tomorrow. Thanks for the great work here @fred84.

@fred84
Copy link
Contributor Author

fred84 commented Aug 29, 2017

@jasontedor Thanks for review and suggestions.

@jasontedor jasontedor added v7.0.0 :Core/Infra/Core Core issues without another label labels Aug 29, 2017
@jasontedor jasontedor merged commit c075323 into elastic:master Aug 29, 2017
jasontedor pushed a commit that referenced this pull request Aug 29, 2017
This commit refactors MetaDataCreateIndexService so that it is unit
testable.

Relates #25961
@lcawl lcawl removed the v6.1.0 label Dec 12, 2017
@jimczi jimczi added v7.0.0-beta1 and removed v7.0.0 labels Feb 7, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants