Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dakrone committed Aug 30, 2024
1 parent 0ff873e commit d0b4663
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 2 deletions.
6 changes: 5 additions & 1 deletion modules/dot-prefix-validation/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ esplugin {

restResources {
restApi {
include '_common', 'indices', 'index', 'cluster', 'nodes', 'get', 'ingest'
include '_common', 'indices', 'index', 'cluster', 'nodes', 'get', 'ingest', 'bulk', 'reindex'
}
}

tasks.named("yamlRestTestV7CompatTransform").configure {task ->
task.addAllowedWarningRegex("setting \\[ecs\\] is deprecated as ECS format is the default and only option")
}

tasks.named('yamlRestTest') {
usesDefaultDistribution()
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ void validateIndices(@Nullable Set<String> indices) {
DeprecationCategory.INDICES,
"dot-prefix",
"Index [{}] name begins with a dot (.), which is deprecated, "
+ "and will not be allowed in a future Elasticsearch version."
+ "and will not be allowed in a future Elasticsearch version.",
index
);
}
}
Expand All @@ -93,6 +94,7 @@ private static char getFirstChar(String index) {
}

private boolean isOperator() {
System.out.println("--> header? " + threadContext.getHeader(RestController.ELASTIC_PRODUCT_HTTP_HEADER));
return Optional.ofNullable(threadContext.getHeader(RestController.ELASTIC_PRODUCT_HTTP_HEADER)).map(Strings::hasText).orElse(false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
package org.elasticsearch.validation;

import com.carrotsearch.randomizedtesting.annotations.ParametersFactory;

import org.elasticsearch.common.settings.SecureString;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.util.concurrent.ThreadContext;
import org.elasticsearch.test.cluster.ElasticsearchCluster;
import org.elasticsearch.test.cluster.local.LocalClusterSpecBuilder;
import org.elasticsearch.test.cluster.local.distribution.DistributionType;
import org.elasticsearch.test.rest.yaml.ClientYamlTestCandidate;
import org.elasticsearch.test.rest.yaml.ESClientYamlSuiteTestCase;
import org.junit.ClassRule;

import static org.elasticsearch.test.cluster.FeatureFlag.FAILURE_STORE_ENABLED;

public class DotPrefixClientYamlTestSuiteIT extends ESClientYamlSuiteTestCase {

public DotPrefixClientYamlTestSuiteIT(final ClientYamlTestCandidate testCandidate) {
super(testCandidate);
}

@ParametersFactory
public static Iterable<Object[]> parameters() throws Exception {
return createParameters();
}

private static final String BASIC_AUTH_VALUE = basicAuthHeaderValue("x_pack_rest_user", new SecureString("x-pack-test-password"));

@Override
protected Settings restClientSettings() {
return Settings.builder().put(ThreadContext.PREFIX + ".Authorization", BASIC_AUTH_VALUE).build();
}

@ClassRule
public static ElasticsearchCluster cluster = createCluster();

private static ElasticsearchCluster createCluster() {
LocalClusterSpecBuilder<ElasticsearchCluster> clusterBuilder = ElasticsearchCluster.local()
.distribution(DistributionType.DEFAULT)
.feature(FAILURE_STORE_ENABLED)
.setting("xpack.security.enabled", "true")
.keystore("bootstrap.password", "x-pack-test-password")
.user("x_pack_rest_user", "x-pack-test-password");
boolean setNodes = Boolean.parseBoolean(System.getProperty("yaml.rest.tests.set_num_nodes", "true"));
if (setNodes) {
clusterBuilder.nodes(2);
}
return clusterBuilder.build();
}

@Override
protected String getTestRestCluster() {
return cluster.getHttpAddresses();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
---
"Index creation with a dot-prefix is deprecated unless x-elastic-product-origin set":
- requires:
test_runner_features: ["warnings", "warnings_regex", "headers"]

- do:
warnings:
- "Index [.myindex] name begins with a dot (.), which is deprecated, and will not be allowed in a future Elasticsearch version."
- "index name [.myindex] starts with a dot '.', in the next major version, index names starting with a dot are reserved for hidden indices and system indices"
indices.create:
index: .myindex

- do:
headers: { x-elastic-product-origin: kibana }
warnings:
- "index name [.myindex2] starts with a dot '.', in the next major version, index names starting with a dot are reserved for hidden indices and system indices"
indices.create:
index: .myindex2

- do:
warnings_regex:
- "Index \\[.*\\] name begins with a dot \\(\\.\\), which is deprecated, and will not be allowed in a future Elasticsearch version\\."
- "index name \\[.*\\] starts with a dot '\\.', in the next major version, index names starting with a dot are reserved for hidden indices and system indices"
indices.create:
index: <.myindex-{now/d}>

---
"Reject auto-creation of dot-prefixed indices":
- requires:
test_runner_features: ["warnings", "headers"]

- do:
warnings:
- "Index [.myindex] name begins with a dot (.), which is deprecated, and will not be allowed in a future Elasticsearch version."
- "index name [.myindex] starts with a dot '.', in the next major version, index names starting with a dot are reserved for hidden indices and system indices"
index:
index: .myindex
id: "1"
body: {foo: bar}

- do:
warnings:
- "Index [.myindex3] name begins with a dot (.), which is deprecated, and will not be allowed in a future Elasticsearch version."
- "index name [.myindex3] starts with a dot '.', in the next major version, index names starting with a dot are reserved for hidden indices and system indices"
bulk:
body:
- index:
_index: other
- message: foo
- index:
_index: .myindex3
- message: foo

---
"Reject auto-creation of dot-prefixed indices through pipelines":
- requires:
test_runner_features: ["warnings", "headers"]

- do:
ingest.put_pipeline:
id: mypipeline
body: >
{
"processors": [
{
"set" : {
"field" : "_index",
"value": "{{redirect_to}}"
}
}
]
}
- do:
warnings:
- "Index [.other] name begins with a dot (.), which is deprecated, and will not be allowed in a future Elasticsearch version."
- "index name [.other] starts with a dot '.', in the next major version, index names starting with a dot are reserved for hidden indices and system indices"
index:
index: myindex
id: "1"
body: {redirect_to: ".other"}
pipeline: mypipeline

- do:
warnings:
- "Index [.other2] name begins with a dot (.), which is deprecated, and will not be allowed in a future Elasticsearch version."
- "index name [.other2] starts with a dot '.', in the next major version, index names starting with a dot are reserved for hidden indices and system indices"
bulk:
body:
- index:
_index: other
- message: foo
- index:
_index: myindex
pipeline: mypipeline
- redirect_to: .other2

- do:
index:
index: original
id: "1"
body: { "redirect_to": ".other3" }
- do:
indices.refresh: {}
- do:
warnings:
- "Index [.other3] name begins with a dot (.), which is deprecated, and will not be allowed in a future Elasticsearch version."
- "index name [.other3] starts with a dot '.', in the next major version, index names starting with a dot are reserved for hidden indices and system indices"
reindex:
body:
source:
index: original
dest:
index: newindex
pipeline: mypipeline

- do:
warnings:
- "Index [.reindex] name begins with a dot (.), which is deprecated, and will not be allowed in a future Elasticsearch version."
- "index name [.reindex] starts with a dot '.', in the next major version, index names starting with a dot are reserved for hidden indices and system indices"
reindex:
body:
source:
index: original
dest:
index: .reindex

---
"Reject index template that has a dot prefix index pattern":
- requires:
test_runner_features: ["warnings", "headers"]

- do:
warnings:
- "Index [.data-*] name begins with a dot (.), which is deprecated, and will not be allowed in a future Elasticsearch version."
indices.put_index_template:
name: my-template
body:
index_patterns: [regular, .data-*]
data_stream: {}

0 comments on commit d0b4663

Please sign in to comment.