/
TestClustersPluginFuncTest.groovy
251 lines (212 loc) · 8.68 KB
/
TestClustersPluginFuncTest.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/*
* 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.gradle
import org.elasticsearch.gradle.fixtures.AbstractGradleFuncTest
import org.gradle.testkit.runner.GradleRunner
import spock.lang.IgnoreIf
import spock.lang.Unroll
import static org.elasticsearch.gradle.fixtures.DistributionDownloadFixture.withChangedClasspathMockedDistributionDownload
import static org.elasticsearch.gradle.fixtures.DistributionDownloadFixture.withChangedConfigMockedDistributionDownload
import static org.elasticsearch.gradle.fixtures.DistributionDownloadFixture.withMockedDistributionDownload
/**
* We do not have coverage for the test cluster startup on windows yet.
* One step at a time...
* */
@IgnoreIf({ os.isWindows() })
class TestClustersPluginFuncTest extends AbstractGradleFuncTest {
def setup() {
// TestClusterPlugin with adding task listeners is not cc compatible
configurationCacheCompatible = false
buildFile << """
import org.elasticsearch.gradle.testclusters.TestClustersAware
import org.elasticsearch.gradle.testclusters.ElasticsearchCluster
plugins {
id 'elasticsearch.testclusters'
}
class SomeClusterAwareTask extends DefaultTask implements TestClustersAware {
private Collection<ElasticsearchCluster> clusters = new HashSet<>();
@Override
@Nested
public Collection<ElasticsearchCluster> getClusters() {
return clusters;
}
@OutputFile
Provider<RegularFile> outputFile
@Inject
SomeClusterAwareTask(ProjectLayout projectLayout) {
outputFile = projectLayout.getBuildDirectory().file("someclusteraware.txt")
}
@TaskAction void doSomething() {
outputFile.get().getAsFile().text = "done"
println 'SomeClusterAwareTask executed'
}
}
"""
}
def "test cluster distribution is configured and started"() {
given:
buildFile << """
testClusters {
myCluster {
testDistribution = 'default'
}
}
tasks.register('myTask', SomeClusterAwareTask) {
useCluster testClusters.myCluster
}
"""
when:
def result = withMockedDistributionDownload(gradleRunner("myTask", '-i')) {
build()
}
then:
result.output.contains("elasticsearch-keystore script executed!")
assertEsOutputContains("myCluster", "Starting Elasticsearch process")
assertEsOutputContains("myCluster", "Stopping node")
assertNoCustomDistro('myCluster')
}
@Unroll
def "test cluster #inputProperty change is detected"() {
given:
buildFile << """
testClusters {
myCluster {
testDistribution = 'default'
}
}
tasks.register('myTask', SomeClusterAwareTask) {
useCluster testClusters.myCluster
}
"""
when:
def runner = gradleRunner("myTask", '-i', '-g', gradleUserHome)
def runningClosure = { GradleRunner r -> r.build() }
withMockedDistributionDownload(runner, runningClosure)
def result = inputProperty == "distributionClasspath" ?
withChangedClasspathMockedDistributionDownload(runner, runningClosure) :
withChangedConfigMockedDistributionDownload(runner, runningClosure)
then:
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!")
assertEsOutputContains("myCluster", "Starting Elasticsearch process")
assertEsOutputContains("myCluster", "Stopping node")
assertNoCustomDistro('myCluster')
where:
inputProperty << ["distributionClasspath", "distributionFiles"]
}
@Unroll
def "test cluster modules #propertyName change is detected"() {
given:
subProject("test-module") << """
plugins {
id 'elasticsearch.esplugin'
}
// do not hassle with resolving predefined dependencies
configurations.compileOnly.dependencies.clear()
configurations.testImplementation.dependencies.clear()
esplugin {
name = 'test-module'
classname 'org.acme.TestModule'
description = "test module description"
}
version = "1.0"
group = 'org.acme'
"""
buildFile << """
testClusters {
myCluster {
testDistribution = 'default'
module ':test-module'
}
}
tasks.register('myTask', SomeClusterAwareTask) {
useCluster testClusters.myCluster
}
"""
when:
withMockedDistributionDownload(gradleRunner("myTask", '-g', gradleUserHome)) {
build()
}
fileChange.delegate = this
fileChange.call(this)
def result = withMockedDistributionDownload(gradleRunner("myTask", '-i', '-g', gradleUserHome)) {
build()
}
then:
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!")
assertEsOutputContains("myCluster", "Starting Elasticsearch process")
assertEsOutputContains("myCluster", "Stopping node")
where:
propertyName | fileChange
"installedFiles" | { def testClazz -> testClazz.file("test-module/src/main/plugin-metadata/someAddedConfig.txt") << "new resource file" }
"installedClasspath" | { def testClazz -> testClazz.file("test-module/src/main/java/SomeClass.java") << "class SomeClass {}" }
}
def "can declare test cluster in lazy evaluated task configuration block"() {
given:
buildFile << """
tasks.register('myTask', SomeClusterAwareTask) {
testClusters {
myCluster {
testDistribution = 'default'
}
}
useCluster testClusters.myCluster
}
"""
when:
def result = withMockedDistributionDownload(gradleRunner("myTask", '-i')) {
build()
}
then:
result.output.contains("elasticsearch-keystore script executed!")
assertEsOutputContains("myCluster", "Starting Elasticsearch process")
assertEsOutputContains("myCluster", "Stopping node")
assertNoCustomDistro('myCluster')
}
def "custom distro folder created for tweaked cluster distribution"() {
given:
buildFile << """
testClusters {
myCluster {
testDistribution = 'default'
extraJarFiles(files('${someJar().absolutePath}'))
}
}
tasks.register('myTask', SomeClusterAwareTask) {
useCluster testClusters.myCluster
}
"""
when:
def result = withMockedDistributionDownload(gradleRunner("myTask", '-i')) {
build()
}
then:
result.output.contains("elasticsearch-keystore script executed!")
assertEsOutputContains("myCluster", "Starting Elasticsearch process")
assertEsOutputContains("myCluster", "Stopping node")
assertCustomDistro('myCluster')
}
boolean assertEsOutputContains(String testCluster, String expectedOutput) {
assert new File(testProjectDir.root,
"build/testclusters/${testCluster}-0/logs/es.out").text.contains(expectedOutput)
true
}
boolean assertCustomDistro(String clusterName) {
assert customDistroFolder(clusterName).exists()
true
}
boolean assertNoCustomDistro(String clusterName) {
assert customDistroFolder(clusterName).exists() == false
true
}
private File customDistroFolder(String clusterName) {
new File(testProjectDir.root, "build/testclusters/${clusterName}-0/distro")
}
}