Skip to content

Commit d47627a

Browse files
committed
Added data stream tests
1 parent faf23a6 commit d47627a

File tree

10 files changed

+1129
-49
lines changed

10 files changed

+1129
-49
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*
8+
* Modifications Copyright OpenSearch Contributors. See
9+
* GitHub history for details.
10+
*/
11+
12+
package org.opensearch.security.privileges.int_tests;
13+
14+
import org.opensearch.test.framework.cluster.LocalCluster;
15+
16+
import java.util.function.Function;
17+
import java.util.function.Supplier;
18+
19+
public enum ClusterConfig {
20+
LEGACY_PRIVILEGES_EVALUATION("legacy", c -> c.doNotFailOnForbidden(true)),
21+
NEXT_GEN_PRIVILEGES_EVALUATION("next_gen", c -> c.privilegesEvaluationType("next_gen"));
22+
23+
final String name;
24+
final Function<LocalCluster.Builder, LocalCluster.Builder> clusterConfiguration;
25+
private LocalCluster cluster;
26+
27+
ClusterConfig(String name, Function<LocalCluster.Builder, LocalCluster.Builder> clusterConfiguration) {
28+
this.name = name;
29+
this.clusterConfiguration = clusterConfiguration;
30+
}
31+
32+
LocalCluster cluster(Supplier<LocalCluster.Builder> clusterBuilder) {
33+
if (cluster == null) {
34+
cluster = this.clusterConfiguration.apply(clusterBuilder.get()).build();
35+
cluster.before();
36+
}
37+
return cluster;
38+
}
39+
40+
void shutdown() {
41+
if (cluster != null) {
42+
try {
43+
cluster.close();
44+
} catch (Exception e) {
45+
e.printStackTrace();
46+
}
47+
cluster = null;
48+
}
49+
}
50+
51+
@Override
52+
public String toString() {
53+
return name;
54+
}
55+
}

src/integrationTest/java/org/opensearch/security/privileges/int_tests/DataStreamAuthorizationReadOnlyIntTests.java

Lines changed: 598 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* GitHub history for details.
1010
*/
1111

12-
package org.opensearch.security.privileges;
12+
package org.opensearch.security.privileges.int_tests;
1313

1414
import java.util.ArrayList;
1515
import java.util.Collection;
@@ -1217,46 +1217,7 @@ public static Collection<Object[]> params() {
12171217
public IndexAuthorizationReadOnlyIntTests(ClusterConfig clusterConfig, TestSecurityConfig.User user, String description)
12181218
throws Exception {
12191219
this.user = user;
1220-
this.cluster = clusterConfig.cluster();
1220+
this.cluster = clusterConfig.cluster(IndexAuthorizationReadOnlyIntTests::clusterBuilder);
12211221
this.clusterConfig = clusterConfig;
12221222
}
1223-
1224-
public enum ClusterConfig {
1225-
LEGACY_PRIVILEGES_EVALUATION("legacy", c -> c.doNotFailOnForbidden(true)),
1226-
NEXT_GEN_PRIVILEGES_EVALUATION("next_gen", c -> c.privilegesEvaluationType("next_gen"));
1227-
1228-
final String name;
1229-
final Function<LocalCluster.Builder, LocalCluster.Builder> clusterConfiguration;
1230-
private LocalCluster cluster;
1231-
1232-
ClusterConfig(String name, Function<LocalCluster.Builder, LocalCluster.Builder> clusterConfiguration) {
1233-
this.name = name;
1234-
this.clusterConfiguration = clusterConfiguration;
1235-
}
1236-
1237-
LocalCluster cluster() {
1238-
if (cluster == null) {
1239-
cluster = this.clusterConfiguration.apply(clusterBuilder()).build();
1240-
cluster.before();
1241-
}
1242-
return cluster;
1243-
}
1244-
1245-
void shutdown() {
1246-
if (cluster != null) {
1247-
try {
1248-
cluster.close();
1249-
} catch (Exception e) {
1250-
e.printStackTrace();
1251-
}
1252-
cluster = null;
1253-
}
1254-
}
1255-
1256-
@Override
1257-
public String toString() {
1258-
return name;
1259-
}
1260-
}
1261-
12621223
}

src/integrationTest/java/org/opensearch/test/framework/IndexApiMatchers.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.List;
1919
import java.util.Map;
2020
import java.util.Set;
21+
import java.util.regex.Pattern;
2122
import java.util.stream.Collectors;
2223
import java.util.stream.Stream;
2324

@@ -41,6 +42,7 @@
4142
import static com.fasterxml.jackson.core.JsonToken.START_ARRAY;
4243

4344
public class IndexApiMatchers {
45+
private static final Pattern DS_BACKING_INDEX_PATTERN = Pattern.compile("\\.ds-(.+)-[0-9]+");
4446

4547
public static IndexMatcher containsExactly(TestIndexLike... testIndices) {
4648
Map<String, TestIndexLike> indexNameMap = new HashMap<>();
@@ -255,6 +257,15 @@ protected boolean matchesByIndices(
255257

256258
if (containsOpenSearchIndices && (index.startsWith(".opendistro"))) {
257259
seenOpenSearchIndicesBuilder.add(index);
260+
} else if (index.startsWith(".ds-")) {
261+
// We do a special treatment for data stream backing indices. We convert these to the normal data streams if expected indices contains these.
262+
java.util.regex.Matcher matcher = DS_BACKING_INDEX_PATTERN.matcher(index);
263+
264+
if (matcher.matches() && expectedIndices.contains(matcher.group(1))) {
265+
seenIndicesBuilder.add(matcher.group(1));
266+
} else {
267+
seenIndicesBuilder.add(index);
268+
}
258269
} else {
259270
seenIndicesBuilder.add(index);
260271
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*
8+
* Modifications Copyright OpenSearch Contributors. See
9+
* GitHub history for details.
10+
*/
11+
12+
package org.opensearch.test.framework;
13+
14+
import com.google.common.collect.ImmutableMap;
15+
import org.opensearch.action.admin.indices.template.put.PutComponentTemplateAction;
16+
import org.opensearch.action.admin.indices.template.put.PutIndexTemplateRequest;
17+
import org.opensearch.cluster.metadata.ComponentTemplate;
18+
import org.opensearch.cluster.metadata.Template;
19+
import org.opensearch.common.compress.CompressedXContent;
20+
import org.opensearch.common.xcontent.LoggingDeprecationHandler;
21+
import org.opensearch.common.xcontent.json.JsonXContent;
22+
import org.opensearch.common.xcontent.json.JsonXContentParser;
23+
import org.opensearch.core.common.bytes.BytesReference;
24+
import org.opensearch.core.xcontent.NamedXContentRegistry;
25+
import org.opensearch.core.xcontent.XContentBuilder;
26+
import org.opensearch.core.xcontent.XContentGenerator;
27+
import org.opensearch.core.xcontent.XContentParser;
28+
import org.opensearch.transport.client.Client;
29+
30+
import java.util.Map;
31+
32+
public class TestComponentTemplate {
33+
public static TestComponentTemplate DATA_STREAM_MINIMAL = new TestComponentTemplate("test_component_template_data_stream_minimal",
34+
new TestMapping(new TestMapping.Property("@timestamp", "date", "date_optional_time||epoch_millis")));
35+
36+
private final String name;
37+
private final TestMapping mapping;
38+
39+
public TestComponentTemplate(String name, TestMapping mapping) {
40+
this.name = name;
41+
this.mapping = mapping;
42+
}
43+
44+
public String getName() {
45+
return name;
46+
}
47+
48+
public TestMapping getMapping() {
49+
return mapping;
50+
}
51+
52+
public void create(Client client) throws Exception {
53+
try (XContentBuilder builder = JsonXContent.contentBuilder().map(getAsMap())) {
54+
try (XContentParser parser = JsonXContent.jsonXContent.createParser(
55+
NamedXContentRegistry.EMPTY,
56+
LoggingDeprecationHandler.INSTANCE,
57+
BytesReference.bytes(builder).streamInput())) {
58+
client.admin().indices().execute(PutComponentTemplateAction.INSTANCE,
59+
new PutComponentTemplateAction.Request(name).componentTemplate(ComponentTemplate.parse(parser))).actionGet();
60+
}
61+
}
62+
}
63+
64+
public Map<String, ?> getAsMap() {
65+
return ImmutableMap.of("template", ImmutableMap.of("mappings", mapping.getAsMap()));
66+
}
67+
}

0 commit comments

Comments
 (0)