Skip to content

Commit

Permalink
Fixed bulk index requests in BWC tests and hardened assertions (opens…
Browse files Browse the repository at this point in the history
  • Loading branch information
nibix authored Oct 16, 2024
1 parent 2ab9eaf commit 7692d77
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.apache.hc.client5.http.ssl.NoopHostnameVerifier;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.io.entity.StringEntity;
import org.apache.hc.core5.http.message.BasicHeader;
import org.apache.hc.core5.http.nio.ssl.TlsStrategy;
import org.apache.hc.core5.reactor.ssl.TlsDetails;
Expand All @@ -44,9 +45,11 @@
import org.opensearch.common.settings.Settings;
import org.opensearch.common.util.concurrent.ThreadContext;
import org.opensearch.common.util.io.IOUtils;
import org.opensearch.common.xcontent.support.XContentMapValues;
import org.opensearch.security.bwc.helper.RestHelper;
import org.opensearch.test.rest.OpenSearchRestTestCase;

import static org.apache.hc.core5.http.ContentType.APPLICATION_NDJSON;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -239,15 +242,21 @@ private void ingestData(String index) throws IOException {
}
});
bulkRequestBody.append(objectMapper.writeValueAsString(indexRequest) + "\n");
bulkRequestBody.append(objectMapper.writeValueAsString(Song.randomSong().asJson()) + "\n");
bulkRequestBody.append(Song.randomSong().asJson() + "\n");
}
List<Response> responses = RestHelper.requestAgainstAllNodes(
testUserRestClient,
"POST",
"_bulk?refresh=wait_for",
RestHelper.toHttpEntity(bulkRequestBody.toString())
new StringEntity(bulkRequestBody.toString(), APPLICATION_NDJSON)
);
responses.forEach(r -> assertThat(r.getStatusLine().getStatusCode(), is(200)));
for (Response response : responses) {
Map<String, Object> responseMap = responseAsMap(response);
List<?> itemResults = (List<?>) XContentMapValues.extractValue(responseMap, "items", "index", "result");
assertTrue("More than 0 response items", itemResults.size() > 0);
assertTrue("All results are 'created': " + itemResults, itemResults.stream().allMatch(i -> i.equals("created")));
}
}
}

Expand All @@ -266,6 +275,25 @@ private void searchMatchAll(String index) throws IOException {
RestHelper.toHttpEntity(matchAllQuery)
);
responses.forEach(r -> assertThat(r.getStatusLine().getStatusCode(), is(200)));

for (Response response : responses) {
Map<String, Object> responseMap = responseAsMap(response);
@SuppressWarnings("unchecked")
List<Map<?, ?>> sourceDocs = (List<Map<?, ?>>) XContentMapValues.extractValue(responseMap, "hits", "hits", "_source");

for (Map<?, ?> sourceDoc : sourceDocs) {
assertNull("response doc should not contain field forbidden by FLS: " + responseMap, sourceDoc.get(Song.FIELD_LYRICS));
assertNotNull(
"response doc should contain field not forbidden by FLS: " + responseMap,
sourceDoc.get(Song.FIELD_ARTIST)
);
assertEquals(
"response doc should always have genre rock: " + responseMap,
Song.GENRE_ROCK,
sourceDoc.get(Song.FIELD_GENRE)
);
}
}
}
}

Expand Down
8 changes: 7 additions & 1 deletion bwc-test/src/test/java/org/opensearch/security/bwc/Song.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public class Song {
public static final String GENRE_JAZZ = "jazz";
public static final String GENRE_BLUES = "blues";

public static final String[] GENRES = new String[] { GENRE_BLUES, GENRE_JAZZ, GENRE_ROCK };

public static final String QUERY_TITLE_NEXT_SONG = FIELD_TITLE + ":" + "\"" + TITLE_NEXT_SONG + "\"";
public static final String QUERY_TITLE_POISON = FIELD_TITLE + ":" + TITLE_POISON;
public static final String QUERY_TITLE_MAGNUM_OPUS = FIELD_TITLE + ":" + TITLE_MAGNUM_OPUS;
Expand Down Expand Up @@ -112,7 +114,11 @@ public static Song randomSong() {
UUID.randomUUID().toString(),
UUID.randomUUID().toString(),
Randomness.get().nextInt(5),
UUID.randomUUID().toString()
randomGenre()
);
}

static String randomGenre() {
return GENRES[Randomness.get().nextInt(GENRES.length)];
}
}

0 comments on commit 7692d77

Please sign in to comment.