Skip to content

Commit

Permalink
Simplify test build output normalization (elastic#77172)
Browse files Browse the repository at this point in the history
* Simplify test build output normalization
  • Loading branch information
breskeby authored Sep 3, 2021
1 parent 8dd4231 commit 93f3607
Show file tree
Hide file tree
Showing 12 changed files with 280 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ class InternalBwcGitPluginFuncTest extends AbstractGitAwareGradleFuncTest {
then:
result.task(":checkoutBwcBranch").outcome == TaskOutcome.SUCCESS
result.task(":consumer:register").outcome == TaskOutcome.SUCCESS
normalized(result.output).contains("/cloned/build/checkout")
result.output.contains("./build/checkout")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,16 @@ unknown license content line 2
}
"""



when:
def result = gradleRunner(":darwin-tar:check").buildAndFail()
def runner = gradleRunner(":darwin-tar:check")
println "{runner.getClass()} = ${runner.getClass()}"
def result = runner.buildAndFail()
println "result.getClass() = ${result.getClass()}"
then:
result.task(":darwin-tar:checkLicense").outcome == TaskOutcome.FAILED
normalized(result.output).contains("> expected line [2] in " +
result.output.contains("> expected line [2] in " +
"[./darwin-tar/build/tar-extracted/elasticsearch-${VersionProperties.getElasticsearch()}/LICENSE.txt] " +
"to be [elastic license coorp stuff line 2] but was [unknown license content line 2]")
}
Expand All @@ -110,7 +115,7 @@ Copyright 2009-2018 Acme Coorp"""
def result = gradleRunner(":darwin-tar:checkNotice").buildAndFail()
then:
result.task(":darwin-tar:checkNotice").outcome == TaskOutcome.FAILED
normalized(result.output).contains("> expected line [2] in " +
result.output.contains("> expected line [2] in " +
"[./darwin-tar/build/tar-extracted/elasticsearch-${VersionProperties.getElasticsearch()}/NOTICE.txt] " +
"to be [Copyright 2009-2021 Elasticsearch] but was [Copyright 2009-2018 Acme Coorp]")
}
Expand Down Expand Up @@ -146,8 +151,7 @@ Copyright 2009-2021 Elasticsearch"""
def result = gradleRunner(":darwin-tar:check").buildAndFail()
then:
result.task(":darwin-tar:checkMlCppNotice").outcome == TaskOutcome.FAILED
normalized(result.output)
.contains("> expected [./darwin-tar/build/tar-extracted/elasticsearch-" +
result.output.contains("> expected [./darwin-tar/build/tar-extracted/elasticsearch-" +
"${VersionProperties.getElasticsearch()}/modules/x-pack-ml/NOTICE.txt " +
"to contain [foo license] but it did not")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import spock.lang.Unroll
/*
* Test is ignored on ARM since this test case tests the ability to build certain older BWC branches that we don't support on ARM
*/

@IgnoreIf({ Architecture.current() == Architecture.AARCH64 })
class InternalDistributionBwcSetupPluginFuncTest extends AbstractGitAwareGradleFuncTest {

Expand Down Expand Up @@ -138,9 +139,8 @@ class InternalDistributionBwcSetupPluginFuncTest extends AbstractGitAwareGradleF

and: "assemble task triggered"
result.output.contains("[7.10.1] > Task :distribution:archives:darwin-tar:assemble")
normalized(result.output)
.contains("distfile /distribution/bwc/bugfix/build/bwc/checkout-7.10/distribution/archives/darwin-tar/" +
"build/distributions/elasticsearch-7.10.1-SNAPSHOT-darwin-x86_64.tar.gz")
result.output.contains("distfile /distribution/bwc/bugfix/build/bwc/checkout-7.10/distribution/archives/darwin-tar/" +
"build/distributions/elasticsearch-7.10.1-SNAPSHOT-darwin-x86_64.tar.gz")
}

def "bwc expanded distribution folder can be resolved as bwc project artifact"() {
Expand Down Expand Up @@ -177,11 +177,9 @@ class InternalDistributionBwcSetupPluginFuncTest extends AbstractGitAwareGradleF
result.task(":distribution:bwc:minor:buildBwcDarwinTar").outcome == TaskOutcome.SUCCESS
and: "assemble task triggered"
result.output.contains("[7.12.0] > Task :distribution:archives:darwin-tar:extractedAssemble")
normalized(result.output)
.contains("expandedRootPath /distribution/bwc/minor/build/bwc/checkout-7.x/" +
result.output.contains("expandedRootPath /distribution/bwc/minor/build/bwc/checkout-7.x/" +
"distribution/archives/darwin-tar/build/install")
normalized(result.output)
.contains("nested folder /distribution/bwc/minor/build/bwc/checkout-7.x/" +
result.output.contains("nested folder /distribution/bwc/minor/build/bwc/checkout-7.x/" +
"distribution/archives/darwin-tar/build/install/elasticsearch-7.12.0-SNAPSHOT")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class JdkDownloadPluginFuncTest extends AbstractGradleFuncTest {
}

then:
normalized(result.output).contains("Unpacking $expectedArchiveName using $transformType") == false
result.output.contains("Unpacking $expectedArchiveName using $transformType") == false

where:
platform | expectedArchiveName | transformType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class LicenseHeadersPrecommitPluginFuncTest extends AbstractGradleFuncTest {
assertOutputContains(result.output, "> Check failed. License header problems were found. Full details: ./build/reports/licenseHeaders/rat.xml")
assertOutputContains(result.output, "./src/main/java/org/acme/UnknownLicensed.java")
assertOutputContains(result.output, "./src/main/java/org/acme/UnapprovedLicensed.java")
normalized(result.output).contains("./src/main/java/org/acme/DualLicensed.java") == false
result.output.contains("./src/main/java/org/acme/DualLicensed.java") == false
}

def "can filter source files"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class InternalTestRerunPluginFuncTest extends AbstractGradleFuncTest {
def result = gradleRunner("test").buildAndFail()
result.output.contains("total executions: 2") == false
and: "no jvm system exit tracing provided"
normalized(result.output).contains("""Test jvm exited unexpectedly.
result.output.contains("""Test jvm exited unexpectedly.
Test jvm system exit trace:""") == false
}

Expand Down Expand Up @@ -119,7 +119,7 @@ Test jvm system exit trace:""") == false
result.output.contains("AnotherTest6 total executions: 2")
// triggered only in the second overall run
and: 'Tracing is provided'
normalized(result.output).contains("""================
result.output.contains("""================
Test jvm exited unexpectedly.
Test jvm system exit trace (run: 1)
Gradle Test Executor 1 > AnotherTest6 > someTest
Expand Down Expand Up @@ -200,11 +200,11 @@ Gradle Test Executor 1 > AnotherTest6 > someTest
result.output.contains("JdkKillingTest total executions: 5")
result.output.contains("Max retries(4) hit")
and: 'Tracing is provided'
normalized(result.output).contains("Test jvm system exit trace (run: 1)")
normalized(result.output).contains("Test jvm system exit trace (run: 2)")
normalized(result.output).contains("Test jvm system exit trace (run: 3)")
normalized(result.output).contains("Test jvm system exit trace (run: 4)")
normalized(result.output).contains("Test jvm system exit trace (run: 5)")
result.output.contains("Test jvm system exit trace (run: 1)")
result.output.contains("Test jvm system exit trace (run: 2)")
result.output.contains("Test jvm system exit trace (run: 3)")
result.output.contains("Test jvm system exit trace (run: 4)")
result.output.contains("Test jvm system exit trace (run: 5)")
}

private String testMethodContent(boolean withSystemExit, boolean fail, int timesFailing = 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ class InternalYamlRestTestPluginFuncTest extends AbstractRestResourcesFuncTest {
file("/build/classes/java/yamlRestTest/MockIT.class").exists()

// check that our copied specs and tests are on the yamlRestTest classpath
normalized(result.output).contains("./build/restResources/yamlSpecs")
normalized(result.output).contains("./build/restResources/yamlTests")
result.output.contains("./build/restResources/yamlSpecs")
result.output.contains("./build/restResources/yamlTests")

when:
result = gradleRunner("yamlRestTest").build()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ class TestClustersPluginFuncTest extends AbstractGradleFuncTest {
withChangedConfigMockedDistributionDownload(runner, runningClosure)

then:
normalized(result.output).contains("Task ':myTask' is not up-to-date because:\n Input property 'clusters.myCluster\$0.nodes.\$0.$inputProperty'")
result.output.contains("Task ':myTask' is not up-to-date because:\n Input property 'clusters.myCluster\$0.nodes.\$0.$inputProperty'")
result.output.contains("elasticsearch-keystore script executed!")
assertEsLogContains("myCluster", "Starting Elasticsearch process")
assertEsLogContains("myCluster", "Stopping node")
Expand Down Expand Up @@ -163,7 +163,7 @@ class TestClustersPluginFuncTest extends AbstractGradleFuncTest {
}

then:
normalized(result.output).contains("Task ':myTask' is not up-to-date because:\n" +
result.output.contains("Task ':myTask' is not up-to-date because:\n" +
" Input property 'clusters.myCluster\$0.nodes.\$0.$propertyName'")
result.output.contains("elasticsearch-keystore script executed!")
assertEsLogContains("myCluster", "Starting Elasticsearch process")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
package org.elasticsearch.gradle.fixtures

import org.elasticsearch.gradle.internal.test.InternalAwareGradleRunner
import org.elasticsearch.gradle.internal.test.NormalizeOutputGradleRunner
import org.gradle.testkit.runner.GradleRunner
import org.junit.Rule
import org.junit.rules.TemporaryFolder
Expand All @@ -18,6 +19,8 @@ import java.lang.management.ManagementFactory
import java.util.jar.JarEntry
import java.util.jar.JarOutputStream

import static org.elasticsearch.gradle.internal.test.TestUtils.normalizeString

abstract class AbstractGradleFuncTest extends Specification {

@Rule
Expand Down Expand Up @@ -46,11 +49,14 @@ abstract class AbstractGradleFuncTest extends Specification {
}

GradleRunner gradleRunner(File projectDir, String... arguments) {
new InternalAwareGradleRunner(GradleRunner.create()
.withDebug(ManagementFactory.getRuntimeMXBean().getInputArguments().toString().indexOf("-agentlib:jdwp") > 0)
.withProjectDir(projectDir)
.withPluginClasspath()
.forwardOutput()
return new NormalizeOutputGradleRunner(
new InternalAwareGradleRunner(GradleRunner.create()
.withDebug(ManagementFactory.getRuntimeMXBean().getInputArguments().toString().indexOf("-agentlib:jdwp") > 0)
.withProjectDir(projectDir)
.withPluginClasspath()
.forwardOutput()
),
projectDir
).withArguments(arguments)
}

Expand All @@ -63,13 +69,9 @@ abstract class AbstractGradleFuncTest extends Specification {
assert normalized(givenOutput).contains(normalized(expected)) == false
true
}

String normalized(String input) {
String normalizedPathPrefix = testProjectDir.root.canonicalPath.replace('\\', '/')
return input.readLines()
.collect { it.replace('\\', '/') }
.collect {it.replace(normalizedPathPrefix , '.') }
.collect {it.replaceAll(/Gradle Test Executor \d/ , 'Gradle Test Executor 1') }
.join("\n")
return normalizeString(input, testProjectDir.root)
}

File file(String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import org.gradle.testkit.runner.InvalidRunnerConfigurationException;
import org.gradle.testkit.runner.UnexpectedBuildFailure;
import org.gradle.testkit.runner.UnexpectedBuildSuccess;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.Writer;
Expand All @@ -33,22 +32,26 @@ public InternalAwareGradleRunner(GradleRunner delegate) {

@Override
public GradleRunner withGradleVersion(String gradleVersion) {
return delegate.withGradleVersion(gradleVersion);
delegate.withGradleVersion(gradleVersion);
return this;
}

@Override
public GradleRunner withGradleInstallation(File file) {
return delegate.withGradleInstallation(file);
delegate.withGradleInstallation(file);
return this;
}

@Override
public GradleRunner withGradleDistribution(URI uri) {
return delegate.withGradleDistribution(uri);
delegate.withGradleDistribution(uri);
return this;
}

@Override
public GradleRunner withTestKitDir(File file) {
return delegate.withTestKitDir(file);
delegate.withTestKitDir(file);
return this;
}

@Override
Expand All @@ -58,7 +61,8 @@ public File getProjectDir() {

@Override
public GradleRunner withProjectDir(File projectDir) {
return delegate.withProjectDir(projectDir);
delegate.withProjectDir(projectDir);
return this;
}

@Override
Expand All @@ -70,12 +74,14 @@ public List<String> getArguments() {
public GradleRunner withArguments(List<String> arguments) {
List<String> collect = Stream.concat(arguments.stream(), Stream.of("-Dtest.external=true"))
.collect(Collectors.toList());
return delegate.withArguments(collect);
delegate.withArguments(collect);
return this;
}

@Override
public GradleRunner withArguments(String... arguments) {
return withArguments(List.of(arguments));
withArguments(List.of(arguments));
return this;
}

@Override
Expand All @@ -85,12 +91,14 @@ public List<? extends File> getPluginClasspath() {

@Override
public GradleRunner withPluginClasspath() throws InvalidPluginMetadataException {
return delegate.withPluginClasspath();
delegate.withPluginClasspath();
return this;
}

@Override
public GradleRunner withPluginClasspath(Iterable<? extends File> iterable) {
return delegate.withPluginClasspath(iterable);
delegate.withPluginClasspath(iterable);
return this;
}

@Override
Expand All @@ -100,33 +108,37 @@ public boolean isDebug() {

@Override
public GradleRunner withDebug(boolean b) {
return delegate.withDebug(b);
delegate.withDebug(b);
return this;
}

@Nullable
@Override
public Map<String, String> getEnvironment() {
return delegate.getEnvironment();
}

@Override
public GradleRunner withEnvironment(@Nullable Map<String, String> map) {
return delegate.withEnvironment(map);
public GradleRunner withEnvironment(Map<String, String> map) {
delegate.withEnvironment(map);
return this;
}

@Override
public GradleRunner forwardStdOutput(Writer writer) {
return delegate.forwardStdOutput(writer);
delegate.forwardStdOutput(writer);
return this;
}

@Override
public GradleRunner forwardStdError(Writer writer) {
return delegate.forwardStdOutput(writer);
delegate.forwardStdOutput(writer);
return this;
}

@Override
public GradleRunner forwardOutput() {
return delegate.forwardOutput();
delegate.forwardOutput();
return this;
}

@Override
Expand Down
Loading

0 comments on commit 93f3607

Please sign in to comment.