Switch WriteActionsTests.testBulk to use hamcrest (#43897)

If an item in the bulk request fails, that could be for a variety of
reasons - it may be that the underlying behaviour of security has
changed, or it may just be a transient failure during testing.

Simply asserting a `true`/`false` value produces failure messages that
are difficult to diagnose and debug. Using hamcert (`assertThat`) will
make it easier to understand the causes of failures in this test.

Backport of: #43725
This commit is contained in:
Tim Vernum 2019-07-03 16:29:28 +10:00 committed by GitHub
parent 14884c871f
commit 461aa39daf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 56 additions and 40 deletions

View File

@ -23,6 +23,9 @@ import org.elasticsearch.test.SecuritySettingsSource;
import static org.elasticsearch.test.SecurityTestsUtils.assertAuthorizationExceptionDefaultUsers; import static org.elasticsearch.test.SecurityTestsUtils.assertAuthorizationExceptionDefaultUsers;
import static org.elasticsearch.test.SecurityTestsUtils.assertThrowsAuthorizationExceptionDefaultUsers; import static org.elasticsearch.test.SecurityTestsUtils.assertThrowsAuthorizationExceptionDefaultUsers;
import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.core.IsInstanceOf.instanceOf; import static org.hamcrest.core.IsInstanceOf.instanceOf;
public class WriteActionsTests extends SecurityIntegTestCase { public class WriteActionsTests extends SecurityIntegTestCase {
@ -101,64 +104,77 @@ public class WriteActionsTests extends SecurityIntegTestCase {
.add(new UpdateRequest("test4", "type", "id").doc(Requests.INDEX_CONTENT_TYPE, "field", "value")) .add(new UpdateRequest("test4", "type", "id").doc(Requests.INDEX_CONTENT_TYPE, "field", "value"))
.add(new UpdateRequest("missing", "type", "id").doc(Requests.INDEX_CONTENT_TYPE, "field", "value")).get(); .add(new UpdateRequest("missing", "type", "id").doc(Requests.INDEX_CONTENT_TYPE, "field", "value")).get();
assertTrue(bulkResponse.hasFailures()); assertTrue(bulkResponse.hasFailures());
assertEquals(13, bulkResponse.getItems().length); assertThat(bulkResponse.getItems().length, equalTo(13));
assertFalse(bulkResponse.getItems()[0].isFailed()); assertThat(bulkResponse.getItems()[0].getFailure(), nullValue());
assertEquals(DocWriteRequest.OpType.INDEX, bulkResponse.getItems()[0].getOpType()); assertThat(bulkResponse.getItems()[0].isFailed(), equalTo(false));
assertEquals("test1", bulkResponse.getItems()[0].getIndex()); assertThat(bulkResponse.getItems()[0].getOpType(), equalTo(DocWriteRequest.OpType.INDEX));
assertTrue(bulkResponse.getItems()[1].isFailed()); assertThat(bulkResponse.getItems()[0].getIndex(), equalTo("test1"));
assertEquals(DocWriteRequest.OpType.INDEX, bulkResponse.getItems()[1].getOpType()); assertThat(bulkResponse.getItems()[1].getFailure(), notNullValue());
assertEquals("index1", bulkResponse.getItems()[1].getFailure().getIndex()); assertThat(bulkResponse.getItems()[1].isFailed(), equalTo(true));
assertThat(bulkResponse.getItems()[1].getOpType(), equalTo(DocWriteRequest.OpType.INDEX));
assertThat(bulkResponse.getItems()[1].getFailure().getIndex(), equalTo("index1"));
assertAuthorizationExceptionDefaultUsers(bulkResponse.getItems()[1].getFailure().getCause(), BulkAction.NAME + "[s]"); assertAuthorizationExceptionDefaultUsers(bulkResponse.getItems()[1].getFailure().getCause(), BulkAction.NAME + "[s]");
assertThat(bulkResponse.getItems()[1].getFailure().getCause().getMessage(), assertThat(bulkResponse.getItems()[1].getFailure().getCause().getMessage(),
containsString("[indices:data/write/bulk[s]] is unauthorized")); containsString("[indices:data/write/bulk[s]] is unauthorized"));
assertFalse(bulkResponse.getItems()[2].isFailed()); assertThat(bulkResponse.getItems()[2].getFailure(), nullValue());
assertEquals(DocWriteRequest.OpType.INDEX, bulkResponse.getItems()[2].getOpType()); assertThat(bulkResponse.getItems()[2].isFailed(), equalTo(false));
assertEquals("test4", bulkResponse.getItems()[2].getResponse().getIndex()); assertThat(bulkResponse.getItems()[2].getOpType(), equalTo(DocWriteRequest.OpType.INDEX));
assertTrue(bulkResponse.getItems()[3].isFailed()); assertThat(bulkResponse.getItems()[2].getResponse().getIndex(), equalTo("test4"));
assertEquals(DocWriteRequest.OpType.INDEX, bulkResponse.getItems()[3].getOpType()); assertThat(bulkResponse.getItems()[3].getFailure(), notNullValue());
assertThat(bulkResponse.getItems()[3].isFailed(), equalTo(true));
assertThat(bulkResponse.getItems()[3].getOpType(), equalTo(DocWriteRequest.OpType.INDEX));
//the missing index gets automatically created (user has permissions for that), but indexing fails due to missing authorization //the missing index gets automatically created (user has permissions for that), but indexing fails due to missing authorization
assertEquals("missing", bulkResponse.getItems()[3].getFailure().getIndex()); assertThat(bulkResponse.getItems()[3].getFailure().getIndex(), equalTo("missing"));
assertThat(bulkResponse.getItems()[3].getFailure().getCause(), instanceOf(ElasticsearchSecurityException.class)); assertThat(bulkResponse.getItems()[3].getFailure().getCause(), instanceOf(ElasticsearchSecurityException.class));
assertAuthorizationExceptionDefaultUsers(bulkResponse.getItems()[3].getFailure().getCause(), BulkAction.NAME + "[s]"); assertAuthorizationExceptionDefaultUsers(bulkResponse.getItems()[3].getFailure().getCause(), BulkAction.NAME + "[s]");
assertThat(bulkResponse.getItems()[3].getFailure().getCause().getMessage(), assertThat(bulkResponse.getItems()[3].getFailure().getCause().getMessage(),
containsString("[indices:data/write/bulk[s]] is unauthorized")); containsString("[indices:data/write/bulk[s]] is unauthorized"));
assertFalse(bulkResponse.getItems()[4].isFailed()); assertThat(bulkResponse.getItems()[4].getFailure(), nullValue());
assertEquals(DocWriteRequest.OpType.DELETE, bulkResponse.getItems()[4].getOpType()); assertThat(bulkResponse.getItems()[4].isFailed(), equalTo(false));
assertEquals("test1", bulkResponse.getItems()[4].getIndex()); assertThat(bulkResponse.getItems()[4].getOpType(), equalTo(DocWriteRequest.OpType.DELETE));
assertTrue(bulkResponse.getItems()[5].isFailed()); assertThat(bulkResponse.getItems()[4].getIndex(), equalTo("test1"));
assertEquals(DocWriteRequest.OpType.DELETE, bulkResponse.getItems()[5].getOpType()); assertThat(bulkResponse.getItems()[5].getFailure(), notNullValue());
assertEquals("index1", bulkResponse.getItems()[5].getFailure().getIndex()); assertThat(bulkResponse.getItems()[5].isFailed(), equalTo(true));
assertThat(bulkResponse.getItems()[5].getOpType(), equalTo(DocWriteRequest.OpType.DELETE));
assertThat(bulkResponse.getItems()[5].getFailure().getIndex(), equalTo("index1"));
assertAuthorizationExceptionDefaultUsers(bulkResponse.getItems()[5].getFailure().getCause(), BulkAction.NAME + "[s]"); assertAuthorizationExceptionDefaultUsers(bulkResponse.getItems()[5].getFailure().getCause(), BulkAction.NAME + "[s]");
assertThat(bulkResponse.getItems()[5].getFailure().getCause().getMessage(), assertThat(bulkResponse.getItems()[5].getFailure().getCause().getMessage(),
containsString("[indices:data/write/bulk[s]] is unauthorized")); containsString("[indices:data/write/bulk[s]] is unauthorized"));
assertFalse(bulkResponse.getItems()[6].isFailed()); assertThat(bulkResponse.getItems()[6].getFailure(), nullValue());
assertEquals(DocWriteRequest.OpType.DELETE, bulkResponse.getItems()[6].getOpType()); assertThat(bulkResponse.getItems()[6].isFailed(), equalTo(false));
assertEquals("test4", bulkResponse.getItems()[6].getIndex()); assertThat(bulkResponse.getItems()[6].getOpType(), equalTo(DocWriteRequest.OpType.DELETE));
assertTrue(bulkResponse.getItems()[7].isFailed()); assertThat(bulkResponse.getItems()[6].getIndex(), equalTo("test4"));
assertEquals(DocWriteRequest.OpType.DELETE, bulkResponse.getItems()[7].getOpType()); assertThat(bulkResponse.getItems()[7].getFailure(), notNullValue());
assertEquals("missing", bulkResponse.getItems()[7].getFailure().getIndex()); assertThat(bulkResponse.getItems()[7].isFailed(), equalTo(true));
assertThat(bulkResponse.getItems()[7].getOpType(), equalTo(DocWriteRequest.OpType.DELETE));
assertThat(bulkResponse.getItems()[7].getFailure().getIndex(), equalTo("missing"));
assertAuthorizationExceptionDefaultUsers(bulkResponse.getItems()[7].getFailure().getCause(), BulkAction.NAME + "[s]"); assertAuthorizationExceptionDefaultUsers(bulkResponse.getItems()[7].getFailure().getCause(), BulkAction.NAME + "[s]");
assertThat(bulkResponse.getItems()[7].getFailure().getCause().getMessage(), assertThat(bulkResponse.getItems()[7].getFailure().getCause().getMessage(),
containsString("[indices:data/write/bulk[s]] is unauthorized")); containsString("[indices:data/write/bulk[s]] is unauthorized"));
assertFalse(bulkResponse.getItems()[8].isFailed()); assertThat(bulkResponse.getItems()[8].getFailure(), nullValue());
assertEquals(DocWriteRequest.OpType.INDEX, bulkResponse.getItems()[8].getOpType()); assertThat(bulkResponse.getItems()[8].isFailed(), equalTo(false));
assertEquals("test1", bulkResponse.getItems()[8].getIndex()); assertThat(bulkResponse.getItems()[8].getOpType(), equalTo(DocWriteRequest.OpType.INDEX));
assertFalse(bulkResponse.getItems()[9].isFailed()); assertThat(bulkResponse.getItems()[8].getIndex(), equalTo("test1"));
assertEquals(DocWriteRequest.OpType.UPDATE, bulkResponse.getItems()[9].getOpType()); assertThat(bulkResponse.getItems()[9].getFailure(), nullValue());
assertEquals("test1", bulkResponse.getItems()[9].getIndex()); assertThat(bulkResponse.getItems()[9].isFailed(), equalTo(false));
assertTrue(bulkResponse.getItems()[10].isFailed()); assertThat(bulkResponse.getItems()[9].getOpType(), equalTo(DocWriteRequest.OpType.UPDATE));
assertEquals(DocWriteRequest.OpType.UPDATE, bulkResponse.getItems()[10].getOpType()); assertThat(bulkResponse.getItems()[9].getIndex(), equalTo("test1"));
assertEquals("index1", bulkResponse.getItems()[10].getFailure().getIndex()); assertThat(bulkResponse.getItems()[10].getFailure(), notNullValue());
assertThat(bulkResponse.getItems()[10].isFailed(), equalTo(true));
assertThat(bulkResponse.getItems()[10].getOpType(), equalTo(DocWriteRequest.OpType.UPDATE));
assertThat(bulkResponse.getItems()[10].getFailure().getIndex(), equalTo("index1"));
assertAuthorizationExceptionDefaultUsers(bulkResponse.getItems()[10].getFailure().getCause(), BulkAction.NAME + "[s]"); assertAuthorizationExceptionDefaultUsers(bulkResponse.getItems()[10].getFailure().getCause(), BulkAction.NAME + "[s]");
assertThat(bulkResponse.getItems()[10].getFailure().getCause().getMessage(), assertThat(bulkResponse.getItems()[10].getFailure().getCause().getMessage(),
containsString("[indices:data/write/bulk[s]] is unauthorized")); containsString("[indices:data/write/bulk[s]] is unauthorized"));
assertTrue(bulkResponse.getItems()[11].isFailed()); assertThat(bulkResponse.getItems()[11].getFailure(), notNullValue());
assertEquals(DocWriteRequest.OpType.UPDATE, bulkResponse.getItems()[11].getOpType()); assertThat(bulkResponse.getItems()[11].isFailed(), equalTo(true));
assertEquals("test4", bulkResponse.getItems()[11].getIndex()); assertThat(bulkResponse.getItems()[11].getOpType(), equalTo(DocWriteRequest.OpType.UPDATE));
assertThat(bulkResponse.getItems()[11].getIndex(), equalTo("test4"));
assertThat(bulkResponse.getItems()[11].getFailure().getCause(), instanceOf(DocumentMissingException.class)); assertThat(bulkResponse.getItems()[11].getFailure().getCause(), instanceOf(DocumentMissingException.class));
assertTrue(bulkResponse.getItems()[12].isFailed()); assertThat(bulkResponse.getItems()[12].getFailure(), notNullValue());
assertEquals(DocWriteRequest.OpType.UPDATE, bulkResponse.getItems()[12].getOpType()); assertThat(bulkResponse.getItems()[12].isFailed(), equalTo(true));
assertEquals("missing", bulkResponse.getItems()[12].getFailure().getIndex()); assertThat(bulkResponse.getItems()[12].getOpType(), equalTo(DocWriteRequest.OpType.UPDATE));
assertThat(bulkResponse.getItems()[12].getFailure().getIndex(), equalTo("missing"));
assertThat(bulkResponse.getItems()[12].getFailure().getCause(), instanceOf(ElasticsearchSecurityException.class)); assertThat(bulkResponse.getItems()[12].getFailure().getCause(), instanceOf(ElasticsearchSecurityException.class));
assertAuthorizationExceptionDefaultUsers(bulkResponse.getItems()[12].getFailure().getCause(), BulkAction.NAME + "[s]"); assertAuthorizationExceptionDefaultUsers(bulkResponse.getItems()[12].getFailure().getCause(), BulkAction.NAME + "[s]");
assertThat(bulkResponse.getItems()[12].getFailure().getCause().getMessage(), assertThat(bulkResponse.getItems()[12].getFailure().getCause().getMessage(),