-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.gradle
150 lines (132 loc) · 4.38 KB
/
build.gradle
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
import de.undercouch.gradle.tasks.download.Download
plugins {
id "de.undercouch.download" version "1.2"
}
apply from: 'execute.gradle'
apply from: 'servers.gradle'
ext {
windows = System.properties['os.name'].toLowerCase().contains('windows')
testsToExecute = ~/.*/
if (project.hasProperty('tests')) testsToExecute = tests
}
task clean << {
if (buildDir.exists()) buildDir.deleteDir()
}
task downloadServers(type: Download) {
src servers.collect { it.value.downloadUrl } - null
file('.downloads').mkdir()
dest file('.downloads')
overwrite false
}
task test << {
def failedProjects = []
projectDir.eachDirMatch(testsToExecute) { dir ->
dir.eachFileMatch(/stagemonitor-integration-test.properties/) { propertiesFile ->
Properties props = new Properties()
propertiesFile.withInputStream { props.load(it) }
println "\n####################"
println "# ${dir.path - projectDir.path}"
println "####################"
checkCompatibilityAndExecuteTest(props, dir, failedProjects)
}
}
assert failedProjects == []
println "# All tests where executed successfully"
}
test.dependsOn clean
test.dependsOn downloadServers
private void checkCompatibilityAndExecuteTest(Properties props, dir, failedProjects) {
if (isLocalJavaVersionCompatible(props.javaVersions.split(/\s*,\s*/))) {
props.server.split(/\s*,\s*/).each { server ->
assert servers[server]
def srv = servers[server]
if (isLocalJavaVersionCompatible(srv.javaVersions, srv.openJdk)) {
if (!executeTest(dir, props, server)) {
failedProjects << (dir.path - projectDir.path)
}
} else {
println "# This server is skipped because the local version of java is not compatible with it."
}
}
} else {
println "This test is skipped because the local version of java is not compatible with it."
}
}
boolean executeTest(File dir, Properties props, String serverName) {
Closeable serverShutDown = {}
try {
println serverName
def server = servers[serverName]
if (!isLocalJavaVersionCompatible(server.javaVersions)) {
println "This server is skipped because the local version of java is not compatible with it."
return true
}
server.install()
if (props.buildCommand) assert execute(props.buildCommand, dir).exitValue() == 0
println "# deploying..."
server.deploy(dir, props)
serverShutDown = startServer(server, dir, props)
waitForIt(props.baseUrl)
executeTestScript(dir, props)
println "Successfully executed tests"
return true
} catch (Exception | AssertionError e) {
println "An error occured while executing tests"
e.printStackTrace()
return false
} finally {
if (!project.hasProperty('noShutdown')) {
serverShutDown.close()
if (buildDir.exists()) buildDir.deleteDir()
} else {
Runtime.getRuntime().addShutdownHook { serverShutDown.close() }
Thread.currentThread().join()
}
}
}
Closeable startServer(def server, File testDir, Properties props) {
println "# starting server..."
def proc = server.start(testDir, props)
return {
println "# stopping server..."
server.stop(proc)
println "# server stopped"
} as Closeable
}
task startServer << {
assert servers.keySet().contains(server)
def srv = servers[server]
srv.install()
Runtime.getRuntime().addShutdownHook { srv.stop() }
srv.start(null, null).waitFor()
}
void executeTestScript(File testDir, Properties props) {
GroovyShell shell = new GroovyShell(new Binding([props: props, execute: execute]))
if (!props.skipDefaultTestScript) {
shell.evaluate(file('DefaultTestScript.groovy'))
}
testDir.eachFileMatch("TestScript.groovy") { shell.evaluate(it) }
}
void waitForIt(String url, int timeoutSec = 300) {
long timeout = System.currentTimeMillis() + timeoutSec * 1000
while (timeout > System.currentTimeMillis()) {
try {
url.toURL().text
println "# started server"
return
} catch (Exception e) {
sleep(100)
}
}
throw new IllegalStateException("# The server failed to start within ${timeoutSec} sec")
}
boolean isLocalJavaVersionCompatible(compatibleVersions, boolean openJdkIsCompatible = true) {
println "Local Java version: ${System.getProperty('java.specification.version')}; compatible versions: $compatibleVersions; OpenJDK: $openJdkIsCompatible"
if (!openJdkIsCompatible && System.getProperty("java.vm.name").contains("OpenJDK")) {
return false
}
compatibleVersions.any { System.getProperty('java.specification.version') == it }
}
task wrapper(type: Wrapper) {
gradleVersion = "2.5"
}