[TEST] Added check: test section names must be unique in the same REST test suite

Fixed also three duplicates found
This commit is contained in:
Luca Cavanna 2014-01-16 15:35:14 +01:00
parent b5f4b2444c
commit 06057c6c39
8 changed files with 85 additions and 44 deletions

View File

@ -136,18 +136,6 @@ setup:
- is_false: test_1.mappings.type_1
- is_false: test_2.mappings.type_3
---
"Get /{index}/_mapping/{type}":
- do:
indices.get_mapping:
index: test_2
type: type_2
- match: { test_2.mappings.type_2.properties: {}}
- is_false: test_1
- is_false: test_2.mappings.type_3
---
"Get /index,index/_mapping/{type}":

View File

@ -129,18 +129,6 @@ setup:
- is_false: test_1.settings.index.number_of_replicas
- is_false: test_2.settings.index.number_of_replicas
---
"Get /{index}/_settings/{name}":
- do:
indices.get_settings:
index: test_1
name: index.number_of_shards
- match: { test_1.settings.index.number_of_shards: "5"}
- is_false: test_1.settings.index.number_of_replicas
- is_false: test_2
---
"Get /index,index/_settings/{name}":

View File

@ -157,18 +157,6 @@ setup:
- is_false: test_1.warmers.warmer_1
- is_false: test_2.warmers.warmer_3
---
"Get /{index}/_warmer/{name}":
- do:
indices.get_warmer:
index: test_2
name: warmer_2
- match: { test_2.warmers.warmer_2.source.query.match_all: {}}
- is_false: test_1
- is_false: test_2.warmers.warmer_3
---
"Get /index,index/_warmer/{name}":

View File

@ -208,6 +208,9 @@ public class RestTestSuiteRunner extends ParentRunner<RestTestCandidate> {
this.restTestExecutionContext = new RestTestExecutionContext(host, port, restSpec);
this.rootDescription = createRootDescription(getRootSuiteTitle());
this.restTestCandidates = collectTestCandidates(rootDescription);
} catch (InitializationError e) {
stopTestCluster();
throw e;
} catch (Throwable e) {
stopTestCluster();
throw new InitializationError(e);
@ -219,8 +222,7 @@ public class RestTestSuiteRunner extends ParentRunner<RestTestCandidate> {
* The descriptions will be part of a tree containing api/yaml file/test section/eventual multiple iterations.
* The test candidates will be instead flattened out to the leaves level (iterations), the part that needs to be run.
*/
protected List<RestTestCandidate> collectTestCandidates(Description rootDescription)
throws RestTestParseException, IOException {
protected List<RestTestCandidate> collectTestCandidates(Description rootDescription) throws InitializationError, IOException {
String[] paths = resolvePathsProperty(REST_TESTS_SUITE, DEFAULT_TESTS_PATH);
Map<String, Set<File>> yamlSuites = FileUtils.findYamlSuites(DEFAULT_TESTS_PATH, paths);
@ -238,6 +240,7 @@ public class RestTestSuiteRunner extends ParentRunner<RestTestCandidate> {
final boolean fixedSeed = testSectionRandomnessOverride != null;
final boolean hasRepetitions = iterations > 1;
List<Throwable> parseExceptions = Lists.newArrayList();
List<RestTestCandidate> testCandidates = Lists.newArrayList();
RestTestSuiteParser restTestSuiteParser = new RestTestSuiteParser();
for (String api : apis) {
@ -249,7 +252,15 @@ public class RestTestSuiteRunner extends ParentRunner<RestTestCandidate> {
Collections.shuffle(yamlFiles, runnerRandomness.getRandom());
for (File yamlFile : yamlFiles) {
RestTestSuite restTestSuite = restTestSuiteParser.parse(restTestExecutionContext.esVersion(), api, yamlFile);
RestTestSuite restTestSuite;
try {
restTestSuite = restTestSuiteParser.parse(restTestExecutionContext.esVersion(), api, yamlFile);
} catch (RestTestParseException e) {
parseExceptions.add(e);
//we continue so that we collect all parse errors and show them all at once
continue;
}
Description testSuiteDescription = createTestSuiteDescription(restTestSuite);
apiDescription.addChild(testSuiteDescription);
@ -301,6 +312,10 @@ public class RestTestSuiteRunner extends ParentRunner<RestTestCandidate> {
}
}
if (!parseExceptions.isEmpty()) {
throw new InitializationError(parseExceptions);
}
return testCandidates;
}

View File

@ -21,6 +21,7 @@ package org.elasticsearch.test.rest.parser;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.yaml.YamlXContent;
import org.elasticsearch.test.rest.section.RestTestSuite;
import org.elasticsearch.test.rest.section.TestSection;
import java.io.File;
import java.io.FileInputStream;
@ -87,7 +88,10 @@ public class RestTestSuiteParser implements RestTestFragmentParser<RestTestSuite
//after skipChildren we are at the end of the skipped object, need to move on
parser.nextToken();
} else {
restTestSuite.addTestSection(parseContext.parseTestSection());
TestSection testSection = parseContext.parseTestSection();
if (!restTestSuite.addTestSection(testSection)) {
throw new RestTestParseException("duplicate test section [" + testSection.getName() + "] found in [" + restTestSuite.getDescription() + "]");
}
}
}

View File

@ -19,9 +19,11 @@
package org.elasticsearch.test.rest.section;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import java.io.File;
import java.util.List;
import java.util.Set;
/**
* Holds a REST test suite loaded from a specific yaml file.
@ -34,7 +36,7 @@ public class RestTestSuite {
private SetupSection setupSection;
private List<TestSection> testSections = Lists.newArrayList();
private Set<TestSection> testSections = Sets.newHashSet();
public RestTestSuite(String api, String name) {
this.api = api;
@ -63,11 +65,15 @@ public class RestTestSuite {
this.setupSection = setupSection;
}
public void addTestSection(TestSection testSection) {
this.testSections.add(testSection);
/**
* Adds a {@link org.elasticsearch.test.rest.section.TestSection} to the REST suite
* @return true if the test section was not already present, false otherwise
*/
public boolean addTestSection(TestSection testSection) {
return this.testSections.add(testSection);
}
public List<TestSection> getTestSections() {
return testSections;
return Lists.newArrayList(testSections);
}
}

View File

@ -54,4 +54,21 @@ public class TestSection {
public void addExecutableSection(ExecutableSection executableSection) {
this.executableSections.add(executableSection);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TestSection that = (TestSection) o;
if (name != null ? !name.equals(that.name) : that.name != null) return false;
return true;
}
@Override
public int hashCode() {
return name != null ? name.hashCode() : 0;
}
}

View File

@ -21,6 +21,7 @@ package org.elasticsearch.test.rest.test;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.common.xcontent.yaml.YamlXContent;
import org.elasticsearch.test.ElasticsearchTestCase;
import org.elasticsearch.test.rest.parser.RestTestParseException;
import org.elasticsearch.test.rest.parser.RestTestSuiteParseContext;
import org.elasticsearch.test.rest.parser.RestTestSuiteParser;
import org.elasticsearch.test.rest.section.DoSection;
@ -498,4 +499,38 @@ public class RestTestParserTests extends ElasticsearchTestCase {
assertThat(doSection.getApiCallSection().getParams().size(), equalTo(4));
assertThat(doSection.getApiCallSection().hasBody(), equalTo(true));
}
@Test(expected = RestTestParseException.class)
public void testParseTestDuplicateTestSections() throws Exception {
parser = YamlXContent.yamlXContent.createParser(
"---\n" +
"\"Missing document (script)\":\n" +
"\n" +
" - do:\n" +
" catch: missing\n" +
" update:\n" +
" index: test_1\n" +
" type: test\n" +
" id: 1\n" +
" body: { doc: { foo: bar } }\n" +
"\n" +
"---\n" +
"\"Missing document (script)\":\n" +
"\n" +
"\n" +
" - do:\n" +
" catch: missing\n" +
" update:\n" +
" index: test_1\n" +
" type: test\n" +
" id: 1\n" +
" body:\n" +
" script: \"ctx._source.foo = bar\"\n" +
" params: { bar: 'xxx' }\n" +
"\n"
);
RestTestSuiteParser testParser = new RestTestSuiteParser();
testParser.parse(new RestTestSuiteParseContext("api", "suite", parser, "0.90.5"));
}
}