From e89de7f38b11c7b466c29e2f436ac2af95c56bf4 Mon Sep 17 00:00:00 2001 From: Tim Vernum Date: Thu, 28 Nov 2019 14:51:01 +1100 Subject: [PATCH] Cleanup test user in HLRC test (#49477) SecurityIT.testGetUser creates a user for testing purposes, but did not delete the user at the end of the test. This could leave the cluster in an unexpected state for other tests. This commit: - Deletes the user at the end of `testGetUser` - Adds the test-name as metadata to the users that are created in `SecurityIT` so that their origin is clear if they do interfere with other tests - Enables SecurityDocumentationIT.testGetUsers on the expectation that the new cleanup step will resolve the unreliability of that test. Relates: #48440 --- .../org/elasticsearch/client/SecurityIT.java | 22 +++++++++++++------ .../SecurityDocumentationIT.java | 2 -- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java index 8122ff17648b1..a2dc1963d08a7 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java @@ -44,6 +44,7 @@ import org.elasticsearch.client.security.user.privileges.Role; import org.elasticsearch.common.CharArrays; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Base64; @@ -71,10 +72,8 @@ public void testPutUser() throws Exception { final PutUserResponse updateUserResponse = execute(updateUserRequest, securityClient::putUser, securityClient::putUserAsync); // assert user not created assertThat(updateUserResponse.isCreated(), is(false)); - // delete user - final Request deleteUserRequest = new Request(HttpDelete.METHOD_NAME, - "/_security/user/" + putUserRequest.getUser().getUsername()); - highLevelClient().getLowLevelClient().performRequest(deleteUserRequest); + // cleanup + deleteUser(putUserRequest.getUser()); } public void testGetUser() throws Exception { @@ -91,6 +90,8 @@ public void testGetUser() throws Exception { ArrayList users = new ArrayList<>(); users.addAll(getUsersResponse.getUsers()); assertThat(users.get(0), is(putUserRequest.getUser())); + + deleteUser(putUserRequest.getUser()); } public void testAuthenticate() throws Exception { @@ -161,12 +162,17 @@ public void testPutRole() throws Exception { assertThat(deleteRoleResponse.isFound(), is(true)); } - private static User randomUser() { + private void deleteUser(User user) throws IOException { + final Request deleteUserRequest = new Request(HttpDelete.METHOD_NAME, "/_security/user/" + user.getUsername()); + highLevelClient().getLowLevelClient().performRequest(deleteUserRequest); + } + + private User randomUser() { final String username = randomAlphaOfLengthBetween(1, 4); return randomUser(username); } - private static User randomUser(String username) { + private User randomUser(String username) { final List roles = Arrays.asList(generateRandomStringArray(3, 3, false, true)); final String fullName = randomFrom(random(), null, randomAlphaOfLengthBetween(0, 3)); final String email = randomFrom(random(), null, randomAlphaOfLengthBetween(0, 3)); @@ -182,6 +188,8 @@ private static User randomUser(String username) { } else { metadata.put("string_list", Arrays.asList(generateRandomStringArray(4, 4, false, true))); } + metadata.put("test-case", getTestName()); + return new User(username, roles, metadata, fullName, email); } @@ -207,7 +215,7 @@ private static Role randomRole(String roleName) { return roleBuilder.build(); } - private static PutUserRequest randomPutUserRequest(boolean enabled) { + private PutUserRequest randomPutUserRequest(boolean enabled) { final User user = randomUser(); return randomPutUserRequest(user, enabled); } diff --git a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java index 0262f54c457ce..f00dfe9deaa24 100644 --- a/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java +++ b/client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java @@ -150,7 +150,6 @@ protected Settings restAdminSettings() { .build(); } - @AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/48440") public void testGetUsers() throws Exception { final RestHighLevelClient client = highLevelClient(); String[] usernames = new String[] {"user1", "user2", "user3"}; @@ -242,7 +241,6 @@ public void onFailure(Exception e) { } } - public void testPutUser() throws Exception { RestHighLevelClient client = highLevelClient();