Skip to content

Commit

Permalink
[Test] Enure test assertions are called for API key creation (elastic…
Browse files Browse the repository at this point in the history
…#74431) (elastic#74527)

Since elastic#74165, API key creation has one additional async layer. Hence the
test assertions are now executed asynchronously and may not get called
in time before the test ends. This PR ensures the assertions are always
called by waiting for a flag.
  • Loading branch information
ywangd authored Jun 24, 2021
1 parent 16c0082 commit 30ecf8b
Showing 1 changed file with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.index.IndexAction;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.support.PlainActionFuture;
Expand Down Expand Up @@ -94,6 +95,7 @@
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -162,8 +164,7 @@ public void setupMocks() {
this.cacheInvalidatorRegistry = mock(CacheInvalidatorRegistry.class);
}

@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/74427")
public void testCreateApiKeyWillUseBulkAction() {
public void testCreateApiKeyWillUseBulkAction() throws Exception {
final Settings settings = Settings.builder().put(XPackSettings.API_KEY_SERVICE_ENABLED_SETTING.getKey(), true).build();
final ApiKeyService service = createApiKeyService(settings);
final Authentication authentication = new Authentication(
Expand All @@ -173,8 +174,18 @@ public void testCreateApiKeyWillUseBulkAction() {
final CreateApiKeyRequest createApiKeyRequest = new CreateApiKeyRequest("key-1", null, null);
when(client.prepareIndex(anyString(), anyString())).thenReturn(new IndexRequestBuilder(client, IndexAction.INSTANCE));
when(client.threadPool()).thenReturn(threadPool);

final AtomicBoolean bulkActionInvoked = new AtomicBoolean(false);
doAnswer(inv -> {
final Object[] args = inv.getArguments();
BulkRequest bulkRequest = (BulkRequest) args[1];
assertThat(bulkRequest.numberOfActions(), is(1));
assertThat(bulkRequest.requests().get(0), instanceOf(IndexRequest.class));
bulkActionInvoked.set(true);
return null;
}).when(client).execute(eq(BulkAction.INSTANCE), any(BulkRequest.class), any());
service.createApiKey(authentication, createApiKeyRequest, org.elasticsearch.core.Set.of(), new PlainActionFuture<>());
verify(client).execute(eq(BulkAction.INSTANCE), any(BulkRequest.class), any());
assertBusy(() -> assertTrue(bulkActionInvoked.get()));
}

public void testCreateApiKeyWillCacheOnCreation() {
Expand Down Expand Up @@ -1093,8 +1104,7 @@ public void testApiKeyDocDeserializationWithNullValues() throws IOException {
assertEquals(new BytesArray("{}"), apiKeyDoc.roleDescriptorsBytes);
}

@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/74427")
public void testCreateApiKeyWillEnsureMetadataCompatibility() {
public void testCreateApiKeyWillEnsureMetadataCompatibility() throws Exception {
when(securityIndex.getInstallableMappingVersion()).thenReturn(Version.V_7_12_0);
final Settings settings = Settings.builder().put(XPackSettings.API_KEY_SERVICE_ENABLED_SETTING.getKey(), true).build();
final ApiKeyService service = createApiKeyService(settings);
Expand All @@ -1110,9 +1120,19 @@ public void testCreateApiKeyWillEnsureMetadataCompatibility() {
randomFrom(org.elasticsearch.core.Map.of(), null));
when(client.prepareIndex(anyString(), anyString())).thenReturn(new IndexRequestBuilder(client, IndexAction.INSTANCE));
when(client.threadPool()).thenReturn(threadPool);

final AtomicBoolean bulkActionInvoked = new AtomicBoolean(false);
doAnswer(inv -> {
final Object[] args = inv.getArguments();
BulkRequest bulkRequest = (BulkRequest) args[1];
assertThat(bulkRequest.numberOfActions(), is(1));
assertThat(bulkRequest.requests().get(0), instanceOf(IndexRequest.class));
bulkActionInvoked.set(true);
return null;
}).when(client).execute(eq(BulkAction.INSTANCE), any(BulkRequest.class), any());
final PlainActionFuture<CreateApiKeyResponse> future2 = new PlainActionFuture<>();
service.createApiKey(authentication, request2, Collections.emptySet(), future2);
verify(client).execute(eq(BulkAction.INSTANCE), any(BulkRequest.class), any());
assertBusy(() -> assertTrue(bulkActionInvoked.get()));
}

public void testGetApiKeyMetadata() throws IOException {
Expand Down

0 comments on commit 30ecf8b

Please sign in to comment.