Skip to content

Commit

Permalink
tests: Replace TransportClient by NodeClient (opensearch-project#1660)
Browse files Browse the repository at this point in the history
* tests: Replace TransportClient by NodeClient

Signed-off-by: rs-eliatra <rafal.stobiecki@eliatra.com>
  • Loading branch information
rs-eliatra authored and DarshitChanpura committed Mar 7, 2022
1 parent a53bad7 commit b58a056
Show file tree
Hide file tree
Showing 49 changed files with 283 additions and 1,337 deletions.
4 changes: 2 additions & 2 deletions src/test/java/org/opensearch/security/AggregationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import org.opensearch.action.admin.indices.create.CreateIndexRequest;
import org.opensearch.action.index.IndexRequest;
import org.opensearch.action.support.WriteRequest.RefreshPolicy;
import org.opensearch.client.transport.TransportClient;
import org.opensearch.client.Client;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.XContentType;
import org.junit.Assert;
Expand All @@ -56,7 +56,7 @@ public void testBasicAggregations() throws Exception {
setup(settings);
final RestHelper rh = nonSslRestHelper();

try (TransportClient tc = getInternalTransportClient()) {
try (Client tc = getClient()) {
tc.admin().indices().create(new CreateIndexRequest("copysf")).actionGet();
tc.index(new IndexRequest("vulcangov").type("kolinahr").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"content\":1}", XContentType.JSON)).actionGet();
tc.index(new IndexRequest("starfleet").type("ships").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"content\":1}", XContentType.JSON)).actionGet();
Expand Down
23 changes: 12 additions & 11 deletions src/test/java/org/opensearch/security/HttpIntegrationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@
import org.apache.http.HttpStatus;
import org.apache.http.NoHttpResponseException;
import org.apache.http.message.BasicHeader;
import org.checkerframework.checker.units.qual.C;
import org.opensearch.action.admin.indices.alias.IndicesAliasesRequest;
import org.opensearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions;
import org.opensearch.action.admin.indices.create.CreateIndexRequest;
import org.opensearch.action.index.IndexRequest;
import org.opensearch.action.support.WriteRequest.RefreshPolicy;
import org.opensearch.client.transport.TransportClient;
import org.opensearch.client.Client;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.XContentType;
import org.junit.Assert;
Expand Down Expand Up @@ -74,7 +75,7 @@ public void testHTTPBasic() throws Exception {
setup(settings);
final RestHelper rh = nonSslRestHelper();

try (TransportClient tc = getInternalTransportClient()) {
try (Client tc = getClient()) {
tc.admin().indices().create(new CreateIndexRequest("copysf")).actionGet();
tc.index(new IndexRequest("vulcangov").type("kolinahr").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"content\":1}", XContentType.JSON)).actionGet();
tc.index(new IndexRequest("starfleet").type("ships").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"content\":1}", XContentType.JSON)).actionGet();
Expand Down Expand Up @@ -155,15 +156,15 @@ public void testHTTPBasic() throws Exception {

Assert.assertEquals(HttpStatus.SC_OK, rh.executeGetRequest("starfleet/ships/_search?pretty", encodeBasicHeader("worf", "worf")).getStatusCode());

try (TransportClient tc = getInternalTransportClient()) {
try (Client tc = getClient()) {
tc.index(new IndexRequest(".opendistro_security").type(getType()).id("roles").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("roles", FileHelper.readYamlContent("roles_deny.yml"))).actionGet();
ConfigUpdateResponse cur = tc.execute(ConfigUpdateAction.INSTANCE, new ConfigUpdateRequest(new String[]{"roles"})).actionGet();
Assert.assertEquals(clusterInfo.numNodes, cur.getNodes().size());
}

Assert.assertEquals(HttpStatus.SC_FORBIDDEN, rh.executeGetRequest("starfleet/ships/_search?pretty", encodeBasicHeader("worf", "worf")).getStatusCode());

try (TransportClient tc = getInternalTransportClient()) {
try (Client tc = getClient()) {
tc.index(new IndexRequest(".opendistro_security").type(getType()).id("roles").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("roles", FileHelper.readYamlContent("roles.yml"))).actionGet();
ConfigUpdateResponse cur = tc.execute(ConfigUpdateAction.INSTANCE, new ConfigUpdateRequest(new String[]{"roles"})).actionGet();
Assert.assertEquals(clusterInfo.numNodes, cur.getNodes().size());
Expand Down Expand Up @@ -328,7 +329,7 @@ public void testHTTPAnon() throws Exception {
Assert.assertFalse(resc.getBody().contains("opendistro_security_anonymous"));
Assert.assertEquals(HttpStatus.SC_OK, resc.getStatusCode());

try (TransportClient tc = getInternalTransportClient()) {
try (Client tc = getClient()) {
tc.index(new IndexRequest(".opendistro_security").type(getType()).id("config").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("config", FileHelper.readYamlContent("config.yml"))).actionGet();
tc.index(new IndexRequest(".opendistro_security").type(getType()).setRefreshPolicy(RefreshPolicy.IMMEDIATE).id("internalusers").source("internalusers", FileHelper.readYamlContent("internal_users.yml"))).actionGet();
ConfigUpdateResponse cur = tc.execute(ConfigUpdateAction.INSTANCE, new ConfigUpdateRequest(new String[]{"config","roles","rolesmapping","internalusers","actiongroups"})).actionGet();
Expand Down Expand Up @@ -358,7 +359,7 @@ public void testHTTPClientCert() throws Exception {

setup(Settings.EMPTY, new DynamicSecurityConfig().setConfig("config_clientcert.yml"), settings, true);

try (TransportClient tc = getInternalTransportClient()) {
try (Client tc = getClient()) {

tc.index(new IndexRequest("vulcangov").type("type").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"content\":1}", XContentType.JSON)).actionGet();

Expand Down Expand Up @@ -442,7 +443,7 @@ public void testHTTPBasic2() throws Exception {

setup(Settings.EMPTY, new DynamicSecurityConfig(), Settings.EMPTY);

try (TransportClient tc = getInternalTransportClient(this.clusterInfo, Settings.EMPTY)) {
try (Client tc = getClient()) {

tc.admin().indices().create(new CreateIndexRequest("copysf")).actionGet();

Expand Down Expand Up @@ -580,7 +581,7 @@ public void test557() throws Exception {
.build();
setup(Settings.EMPTY, new DynamicSecurityConfig(), settings);

try (TransportClient tc = getInternalTransportClient(this.clusterInfo, Settings.EMPTY)) {
try (Client tc = getClient()) {

tc.admin().indices().create(new CreateIndexRequest("copysf")).actionGet();

Expand Down Expand Up @@ -611,7 +612,7 @@ public void testITT1635() throws Exception {
.build();
setup(Settings.EMPTY, new DynamicSecurityConfig().setConfig("config_dnfof.yml").setSecurityRoles("roles_itt1635.yml"), settings);

try (TransportClient tc = getInternalTransportClient(this.clusterInfo, Settings.EMPTY)) {
try (Client tc = getClient()) {

tc.index(new IndexRequest("esb-prod-1").type("doc").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"content\":1}", XContentType.JSON)).actionGet();
tc.index(new IndexRequest("esb-prod-2").type("doc").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"content\":2}", XContentType.JSON)).actionGet();
Expand Down Expand Up @@ -659,7 +660,7 @@ public void testTenantInfo() throws Exception {
*/

try (TransportClient tc = getInternalTransportClient(this.clusterInfo, Settings.EMPTY)) {
try (Client tc = getClient()) {

tc.index(new IndexRequest(".kibana-6").type("doc").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"content\":2}", XContentType.JSON)).actionGet();
tc.index(new IndexRequest(".kibana_-1139640511_admin1").type("doc").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"content\":3}", XContentType.JSON)).actionGet();
Expand Down Expand Up @@ -740,7 +741,7 @@ public void testAll() throws Exception {
setup(settings);
final RestHelper rh = nonSslRestHelper();

try (TransportClient tc = getInternalTransportClient()) {
try (Client tc = getClient()) {
tc.index(new IndexRequest("abcdef").type("doc").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"content\":1}", XContentType.JSON))
.actionGet();
}
Expand Down
24 changes: 12 additions & 12 deletions src/test/java/org/opensearch/security/IndexIntegrationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@
import java.util.Date;
import java.util.TimeZone;

import org.opensearch.client.Client;
import org.opensearch.security.support.SecurityUtils;
import org.apache.http.HttpStatus;
import org.opensearch.action.admin.indices.alias.IndicesAliasesRequest;
import org.opensearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions;
import org.opensearch.action.admin.indices.delete.DeleteIndexRequest;
import org.opensearch.action.index.IndexRequest;
import org.opensearch.action.support.WriteRequest.RefreshPolicy;
import org.opensearch.client.transport.TransportClient;
import org.opensearch.cluster.health.ClusterHealthStatus;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.unit.TimeValue;
Expand All @@ -68,7 +68,7 @@ public void testComposite() throws Exception {
setup(Settings.EMPTY, new DynamicSecurityConfig().setConfig("composite_config.yml").setSecurityRoles("roles_composite.yml"), Settings.EMPTY, true);
final RestHelper rh = nonSslRestHelper();

try (TransportClient tc = getInternalTransportClient()) {
try (Client tc = getClient()) {
tc.index(new IndexRequest("starfleet").type("ships").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"content\":1}", XContentType.JSON)).actionGet();
tc.index(new IndexRequest("klingonempire").type("ships").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"content\":1}", XContentType.JSON)).actionGet();
tc.index(new IndexRequest("public").type("legends").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"content\":1}", XContentType.JSON)).actionGet();
Expand Down Expand Up @@ -97,7 +97,7 @@ public void testBulkShards() throws Exception {
setup(Settings.EMPTY, new DynamicSecurityConfig().setSecurityRoles("roles_bs.yml"), Settings.EMPTY, true);
final RestHelper rh = nonSslRestHelper();

try (TransportClient tc = getInternalTransportClient()) {
try (Client tc = getClient()) {
//create indices and mapping upfront
tc.index(new IndexRequest("test").type("type1").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"field2\":\"init\"}", XContentType.JSON)).actionGet();
tc.index(new IndexRequest("lorem").type("type1").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"field2\":\"init\"}", XContentType.JSON)).actionGet();
Expand Down Expand Up @@ -170,7 +170,7 @@ public void testFilteredAlias() throws Exception {

setup();

try (TransportClient tc = getInternalTransportClient()) {
try (Client tc = getClient()) {

tc.index(new IndexRequest("theindex").type("type1").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"content\":1}", XContentType.JSON)).actionGet();
tc.index(new IndexRequest("otherindex").type("type1").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"content\":1}", XContentType.JSON)).actionGet();
Expand Down Expand Up @@ -204,7 +204,7 @@ public void testIndexTypeEvaluation() throws Exception {

setup();

try (TransportClient tc = getInternalTransportClient()) {
try (Client tc = getClient()) {
tc.index(new IndexRequest("foo1").type("bar").id("1").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"content\":1}", XContentType.JSON)).actionGet();
tc.index(new IndexRequest("foo2").type("bar").id("2").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"content\":2}", XContentType.JSON)).actionGet();
tc.index(new IndexRequest("foo").type("baz").id("3").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"content\":3}", XContentType.JSON)).actionGet();
Expand Down Expand Up @@ -275,7 +275,7 @@ public void testIndices() throws Exception {

setup();

try (TransportClient tc = getInternalTransportClient()) {
try (Client tc = getClient()) {
tc.index(new IndexRequest("nopermindex").type("logs").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"content\":1}", XContentType.JSON)).actionGet();

tc.index(new IndexRequest("logstash-1").type("logs").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"content\":1}", XContentType.JSON)).actionGet();
Expand Down Expand Up @@ -360,7 +360,7 @@ public void testAliases() throws Exception {

setup(settings);

try (TransportClient tc = getInternalTransportClient()) {
try (Client tc = getClient()) {
tc.index(new IndexRequest("nopermindex").type("logs").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"content\":1}", XContentType.JSON)).actionGet();

tc.index(new IndexRequest("logstash-1").type("logs").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"content\":1}", XContentType.JSON)).actionGet();
Expand Down Expand Up @@ -449,7 +449,7 @@ public void testCCSIndexResolve() throws Exception {
setup();
final RestHelper rh = nonSslRestHelper();

try (TransportClient tc = getInternalTransportClient()) {
try (Client tc = getClient()) {
tc.index(new IndexRequest(".abc-6").type("logs").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"content\":1}", XContentType.JSON)).actionGet();
}

Expand All @@ -469,7 +469,7 @@ public void testCCSIndexResolve2() throws Exception {
setup();
final RestHelper rh = nonSslRestHelper();

try (TransportClient tc = getInternalTransportClient()) {
try (Client tc = getClient()) {
tc.index(new IndexRequest(".abc").type("logs").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"content\":1}", XContentType.JSON)).actionGet();
tc.index(new IndexRequest("xyz").type("logs").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"content\":2}", XContentType.JSON)).actionGet();
tc.index(new IndexRequest("noperm").type("logs").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"content\":3}", XContentType.JSON)).actionGet();
Expand Down Expand Up @@ -531,7 +531,7 @@ public void testIndexResolveIgnoreUnavailable() throws Exception {
setup(Settings.EMPTY, new DynamicSecurityConfig().setConfig("config_respect_indices_options.yml").setSecurityRoles("roles_bs.yml"), Settings.EMPTY, true);
final RestHelper rh = nonSslRestHelper();

try (TransportClient tc = getInternalTransportClient()) {
try (Client tc = getClient()) {
//create indices and mapping upfront
tc.index(new IndexRequest("test").type("type1").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"field2\":\"init\"}", XContentType.JSON)).actionGet();
tc.index(new IndexRequest("lorem").type("type1").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"field2\":\"init\"}", XContentType.JSON)).actionGet();
Expand All @@ -552,7 +552,7 @@ public void testIndexResolveIndicesAlias() throws Exception {
setup(Settings.EMPTY, new DynamicSecurityConfig(), Settings.EMPTY, true);
final RestHelper rh = nonSslRestHelper();

try (TransportClient tc = getInternalTransportClient()) {
try (Client tc = getClient()) {
//create indices and mapping upfront
tc.index(new IndexRequest("foo-index").type("_doc").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"field2\":\"init\"}", XContentType.JSON)).actionGet();
tc.admin().indices().aliases(new IndicesAliasesRequest().addAliasAction(AliasActions.add().indices("foo-index").alias("foo-alias"))).actionGet();
Expand All @@ -579,7 +579,7 @@ public void testIndexResolveMinus() throws Exception {
setup(Settings.EMPTY, new DynamicSecurityConfig(), Settings.EMPTY, true);
final RestHelper rh = nonSslRestHelper();

try (TransportClient tc = getInternalTransportClient()) {
try (Client tc = getClient()) {
//create indices and mapping upfront
tc.index(new IndexRequest("foo-abc").type("_doc").setRefreshPolicy(RefreshPolicy.IMMEDIATE).source("{\"field2\":\"init\"}", XContentType.JSON)).actionGet();
}
Expand Down
Loading

0 comments on commit b58a056

Please sign in to comment.