-
Notifications
You must be signed in to change notification settings - Fork 25k
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
Unit testable index creation task on MetaDataCreateIndexService #25961
Conversation
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? |
@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). |
@jasontedor have a good holiday :) |
This is on my list to review tomorrow. |
test this please |
@jasontedor Jenkins failures seems unrelated to my changes. I cant reproduce them locally. |
retest this please |
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.
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")); | ||
} |
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.
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")); |
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.
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()); | ||
} |
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.
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()); |
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.
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()); |
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.
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)); |
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.
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()); |
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.
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")); |
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.
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")); |
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.
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")); |
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.
Let's replace the assertTrue
and assertFalse
by more effective matchers.
@jasontedor PR updated. Thanks for suggestions on matchers. |
retest this please |
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 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) { |
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.
You can put these matchers in test-framework
under org.elasticsearch.test.hamcrest
.
@jasontedor PR updated. |
retest this please |
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.
LGTM.
I will merge this tomorrow. Thanks for the great work here @fred84. |
@jasontedor Thanks for review and suggestions. |
This commit refactors MetaDataCreateIndexService so that it is unit testable. Relates #25961
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.