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

Fixed bulk index requests in BWC tests and hardened assertions #4817

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -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)));
cwperks marked this conversation as resolved.
Show resolved Hide resolved
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
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)];
}
}
Loading