Skip to content

Commit 67a264b

Browse files
jkakavastvernum
andauthored
Cleanup test user in HLRC test (#49477) (#51943)
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 Co-authored-by: Tim Vernum <[email protected]>
1 parent 6ddd201 commit 67a264b

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

client/rest-high-level/src/test/java/org/elasticsearch/client/SecurityIT.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import org.elasticsearch.client.security.user.privileges.Role;
4545
import org.elasticsearch.common.CharArrays;
4646

47+
import java.io.IOException;
4748
import java.util.ArrayList;
4849
import java.util.Arrays;
4950
import java.util.Base64;
@@ -71,10 +72,8 @@ public void testPutUser() throws Exception {
7172
final PutUserResponse updateUserResponse = execute(updateUserRequest, securityClient::putUser, securityClient::putUserAsync);
7273
// assert user not created
7374
assertThat(updateUserResponse.isCreated(), is(false));
74-
// delete user
75-
final Request deleteUserRequest = new Request(HttpDelete.METHOD_NAME,
76-
"/_security/user/" + putUserRequest.getUser().getUsername());
77-
highLevelClient().getLowLevelClient().performRequest(deleteUserRequest);
75+
// cleanup
76+
deleteUser(putUserRequest.getUser());
7877
}
7978

8079
public void testGetUser() throws Exception {
@@ -91,6 +90,8 @@ public void testGetUser() throws Exception {
9190
ArrayList<User> users = new ArrayList<>();
9291
users.addAll(getUsersResponse.getUsers());
9392
assertThat(users.get(0), is(putUserRequest.getUser()));
93+
94+
deleteUser(putUserRequest.getUser());
9495
}
9596

9697
public void testAuthenticate() throws Exception {
@@ -161,12 +162,17 @@ public void testPutRole() throws Exception {
161162
assertThat(deleteRoleResponse.isFound(), is(true));
162163
}
163164

164-
private static User randomUser() {
165+
private void deleteUser(User user) throws IOException {
166+
final Request deleteUserRequest = new Request(HttpDelete.METHOD_NAME, "/_security/user/" + user.getUsername());
167+
highLevelClient().getLowLevelClient().performRequest(deleteUserRequest);
168+
}
169+
170+
private User randomUser() {
165171
final String username = randomAlphaOfLengthBetween(1, 4);
166172
return randomUser(username);
167173
}
168174

169-
private static User randomUser(String username) {
175+
private User randomUser(String username) {
170176
final List<String> roles = Arrays.asList(generateRandomStringArray(3, 3, false, true));
171177
final String fullName = randomFrom(random(), null, randomAlphaOfLengthBetween(0, 3));
172178
final String email = randomFrom(random(), null, randomAlphaOfLengthBetween(0, 3));
@@ -182,6 +188,8 @@ private static User randomUser(String username) {
182188
} else {
183189
metadata.put("string_list", Arrays.asList(generateRandomStringArray(4, 4, false, true)));
184190
}
191+
metadata.put("test-case", getTestName());
192+
185193
return new User(username, roles, metadata, fullName, email);
186194
}
187195

@@ -207,7 +215,7 @@ private static Role randomRole(String roleName) {
207215
return roleBuilder.build();
208216
}
209217

210-
private static PutUserRequest randomPutUserRequest(boolean enabled) {
218+
private PutUserRequest randomPutUserRequest(boolean enabled) {
211219
final User user = randomUser();
212220
return randomPutUserRequest(user, enabled);
213221
}

client/rest-high-level/src/test/java/org/elasticsearch/client/documentation/SecurityDocumentationIT.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ protected Settings restAdminSettings() {
150150
.build();
151151
}
152152

153-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/48440")
154153
public void testGetUsers() throws Exception {
155154
final RestHighLevelClient client = highLevelClient();
156155
String[] usernames = new String[] {"user1", "user2", "user3"};
@@ -242,7 +241,6 @@ public void onFailure(Exception e) {
242241
}
243242
}
244243

245-
246244
public void testPutUser() throws Exception {
247245
RestHighLevelClient client = highLevelClient();
248246

0 commit comments

Comments
 (0)