Skip to content
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

[BUG-1402] Return HTTP 409 on version confict when creating & updating tenants - added relevant unit test #3673

Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,27 @@
package org.opensearch.security.dlic.rest.api;

import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.http.HttpStatus;
prabhask5 marked this conversation as resolved.
Show resolved Hide resolved
import org.junit.Assert;
import org.junit.Test;

import org.opensearch.common.settings.Settings;
import org.opensearch.security.support.ConfigConstants;
import org.opensearch.security.test.helper.rest.RestHelper;
import org.opensearch.test.framework.AsyncActions;
prabhask5 marked this conversation as resolved.
Show resolved Hide resolved

import static org.opensearch.security.OpenSearchSecurityPlugin.PLUGINS_PREFIX;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.MatcherAssert.assertThat;

public class TenantInfoActionTest extends AbstractRestApiUnitTest {
private String payload = "{\"hosts\":[],\"users\":[\"sarek\"],"
+ "\"backend_roles\":[\"starfleet*\",\"ambassador\"],\"and_backend_roles\":[],\"description\":\"Migrated "
Expand Down Expand Up @@ -85,4 +96,32 @@ public void testTenantInfoAPIUpdate() throws Exception {
response = rh.executeGetRequest(ENDPOINT);
Assert.assertEquals(HttpStatus.SC_OK, response.getStatusCode());
}
}

@Test
public void testParallelPutRequests() throws Exception {
setup();

rh.keystore = "restapi/kirk-keystore.jks";
rh.sendAdminCertificate = true;
final String TENANTS_ENDPOINT = BASE_ENDPOINT + "/api/tenants/tenant1";
final String TENANTS_BODY = "{\"description\":\"create new tenant\"}";

final CountDownLatch countDownLatch = new CountDownLatch(1);
final List<CompletableFuture<RestHelper.HttpResponse>> conflictingRequests = AsyncActions.generate(() -> {
return rh.executePutRequest(TENANTS_ENDPOINT, TENANTS_BODY);
}, 2, 4);

// Make sure all requests start at the same time
countDownLatch.countDown();

AtomicInteger numCreatedResponses = new AtomicInteger();
AsyncActions.getAll(conflictingRequests, 1, TimeUnit.SECONDS).forEach((response) -> {
assertThat(response.getStatusCode(), anyOf(equalTo(HttpStatus.SC_CREATED), equalTo(HttpStatus.SC_CONFLICT)));
if (response.getStatusCode() == HttpStatus.SC_CREATED) numCreatedResponses.getAndIncrement();
});
assertThat(numCreatedResponses.get(), equalTo(1)); // should only be one 201

RestHelper.HttpResponse getResponse = rh.executeGetRequest(TENANTS_ENDPOINT); // make sure the one 201 works
assertThat(getResponse.findValueInJson("tenant1" + ".description"), equalTo("create new tenant"));
}
}
Loading