Skip to content

Commit

Permalink
Add test for vanilla search
Browse files Browse the repository at this point in the history
Signed-off-by: Craig Perkins <[email protected]>
  • Loading branch information
cwperks committed Feb 7, 2025
1 parent 777fe07 commit 9a54a4e
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@
import org.opensearch.common.settings.IndexScopedSettings;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.settings.SettingsFilter;
import org.opensearch.indices.SystemIndexDescriptor;
import org.opensearch.plugins.ActionPlugin;
import org.opensearch.plugins.ClusterPlugin;
import org.opensearch.plugins.Plugin;
import org.opensearch.plugins.SystemIndexPlugin;
import org.opensearch.rest.RestController;
import org.opensearch.rest.RestHandler;

import java.util.Collection;
import java.util.List;
import java.util.function.Supplier;

Expand All @@ -51,7 +55,10 @@
/**
* A plugin demonstrating the implementation of a new Rest Handler.
*/
public class ExampleRestHandlerPlugin extends Plugin implements ActionPlugin {
public class ExampleRestHandlerPlugin extends Plugin implements ActionPlugin, SystemIndexPlugin, ClusterPlugin {

public static final String READABLE_SYSTEM_INDEX_NAME = ".readable-system-index";
public static final String NONREADABLE_SYSTEM_INDEX_NAME = ".nonreadable-system-index";

/**
* Instantiate this plugin.
Expand All @@ -68,7 +75,14 @@ public List<RestHandler> getRestHandlers(
final IndexNameExpressionResolver indexNameExpressionResolver,
final Supplier<DiscoveryNodes> nodesInCluster
) {

return singletonList(new ExampleCatAction());
}

@Override
public Collection<SystemIndexDescriptor> getSystemIndexDescriptors(Settings settings) {
return List.of(
new SystemIndexDescriptor(READABLE_SYSTEM_INDEX_NAME, "Readable system index for tests", true),
new SystemIndexDescriptor(NONREADABLE_SYSTEM_INDEX_NAME, "Non-Readable system index for tests")
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
setup:
- skip:
features: allowed_warnings
- do:
indices.create:
index: .readable-system-index

- do:
allowed_warnings:
- "this request accesses system indices: [.readable-system-index], but in a future major version, direct access to system indices will be prevented by default"
index:
index: .readable-system-index
id: 1
refresh: true
body:
query:
match_all: { }

---
"Test that .readable-system-index does not output deprecation log on search":
- skip:
features: [warnings, headers]
- do:
warnings: []
headers: { "X-Opaque-Id": "search-request" }
search:
rest_total_hits_as_int: true
index: .readable-system-index
body:
query:
match_all: { }
- match: { hits.total: 1 }
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
setup:
- skip:
features: allowed_warnings
- do:
indices.create:
index: .nonreadable-system-index

- do:
allowed_warnings:
- "this request accesses system indices: [.nonreadable-system-index], but in a future major version, direct access to system indices will be prevented by default"
index:
index: .nonreadable-system-index
id: 1
refresh: true
body:
query:
match_all: { }

---
"Test that .nonreadable-system-index does output deprecation log on search":
- skip:
features: [warnings, headers]
- do:
headers: { "X-Opaque-Id": "search-request" }
warnings:
- "this request accesses system indices: [.nonreadable-system-index], but in a future major version, direct access to system indices will be prevented by default"
search:
rest_total_hits_as_int: true
index: .nonreadable-system-index
body:
query:
match_all: { }
- match: { hits.total: 1 }
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,9 @@ public class SystemIndexDescriptor {
*
* @param indexPattern The pattern of index names that this descriptor will be used for. Must start with a '.' character.
* @param description The name of the plugin responsible for this system index.
* @param readable Whether this system index is readable. A readable index is one where search and get actions are permitted.
*/
public SystemIndexDescriptor(String indexPattern, String description) {
public SystemIndexDescriptor(String indexPattern, String description, boolean readable) {
Objects.requireNonNull(indexPattern, "system index pattern must not be null");
if (indexPattern.length() < 2) {
throw new IllegalArgumentException(
Expand All @@ -80,6 +81,16 @@ public SystemIndexDescriptor(String indexPattern, String description) {
Automaton a = Operations.determinize(Regex.simpleMatchToAutomaton(indexPattern), Operations.DEFAULT_DETERMINIZE_WORK_LIMIT);
this.indexPatternAutomaton = new CharacterRunAutomaton(a);
this.description = description;
this.readable = readable;
}

/**
*
* @param indexPattern The pattern of index names that this descriptor will be used for. Must start with a '.' character.
* @param description The name of the plugin responsible for this system index.
*/
public SystemIndexDescriptor(String indexPattern, String description) {
this(indexPattern, description, false);
}

/**
Expand All @@ -105,15 +116,6 @@ public String getDescription() {
return description;
}

/**
* Set whether this system index is readable. Defaults to false.
* @param readable Whether this system index is readable. A readable index is one where search and get
* actions are permitted.
*/
public void setReadable(boolean readable) {
this.readable = readable;
}

/**
* @return A boolean corresponding to whether this system index is readable.
*/
Expand Down

0 comments on commit 9a54a4e

Please sign in to comment.