-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.groovy
92 lines (80 loc) · 2.98 KB
/
test.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
@Grab(group = 'com.jayway.awaitility', module = 'awaitility', version = '1.3.5')
import com.jayway.awaitility.Duration
import com.jayway.awaitility.core.ConditionFactory
import java.util.concurrent.TimeUnit
import groovy.transform.Field
@Grab(group = 'org.codehaus.groovy.modules.http-builder', module = 'http-builder', version = '0.7.1')
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method
String jenkins = 'http://localhost:8080'
String gogs = 'http://localhost:3000'
@Field Duration defaultTimeout = new Duration(2, TimeUnit.MINUTES)
@Field Duration defaultPollInterval = new Duration(3, TimeUnit.SECONDS)
@Field ConditionFactory condition = new ConditionFactory(defaultTimeout, defaultPollInterval, defaultPollInterval, true)
String serviceName = 'good_service'
String servicePort = '8000'
try {
shell('./run.sh')
assert isEventuallyAvailable(jenkins)
assert isEventuallyAvailable(gogs)
post("$jenkins/job/_seed/build?delay=0sec")
assert isEventuallyAvailable("$jenkins/job/_seed/lastBuild/api/json")
assert isEventuallySuccessful("$jenkins/job/_seed/lastBuild/api/json")
post("$jenkins/job/create_microservice_repo/buildWithParameters?SERVICE_NAME=$serviceName&SERVICE_PORT=$servicePort")
assert isEventuallyAvailable("$jenkins/job/create_microservice_pipeline/1/api/json")
assert isEventuallySuccessful("$jenkins/job/create_microservice_pipeline/1/api/json")
assert isEventuallySuccessful("$jenkins/job/_seed/2/api/json/")
assert isEventuallyAvailable("$jenkins/job/${serviceName}_build")
post("$jenkins/job/${serviceName}_build/build?delay=0sec")
assert isEventuallySuccessful("$jenkins/job/${serviceName}_deploy/1/api/json")
assert isEventuallyAvailable("http://localhost:$servicePort")
println('Test run finished successfully')
} finally {
shell('docker-compose down --volumes')
shell("docker rm -f $serviceName")
}
def void shell(cmd) {
cmd.execute().waitForProcessOutput(System.out, System.err)
}
def boolean isEventuallyAvailable(url) {
condition.await('Waiting for successful http response').until { isAvailable(url) }
true
}
def boolean isEventuallySuccessful(url) {
condition.await('Waiting for successful build').until { get(url).equals('SUCCESS') }
true
}
def boolean isAvailable(url) {
HTTPBuilder http = new HTTPBuilder(url)
try {
def status = 0
http.request(Method.GET) {
response.success = { response, reader ->
status = response.statusLine.statusCode
}
}
println("URL $url is available! GET returned $status")
status == 200
} catch (Exception e) {
println("URL $url not available")
false
}
}
def String get(url) {
def buildResult = null
HTTPBuilder http = new HTTPBuilder(url)
http.request(Method.GET) {
response.success = { response, reader ->
if (reader != null) {
buildResult = reader.result
}
}
response.'404' = { response, reader ->
buildResult = 'N/A'
}
}
buildResult
}
def void post(url) {
new HTTPBuilder(url).request(Method.POST) {}
}