forked from opensearch-project/opensearch-build-libraries
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestStandardReleasePipelineWithGenericTriggers.groovy
169 lines (148 loc) · 8.24 KB
/
TestStandardReleasePipelineWithGenericTriggers.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
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
import jenkins.tests.BuildPipelineTest
import org.junit.Before
import org.junit.Test
import static org.hamcrest.MatcherAssert.assertThat
import static org.hamcrest.CoreMatchers.equalTo
import static com.lesfurets.jenkins.unit.MethodCall.callArgsToString
import static org.hamcrest.CoreMatchers.hasItem
import groovy.json.JsonSlurper
class TestStandardReleasePipelineWithGenericTriggers extends BuildPipelineTest {
@Before
void setUp() {
def json = '''[{
"url": "https://api.github.com/repos/owner/reponame/releases/assets/123456",
"id": 123456,
"node_id": "RA_kwDOIZCTQs4FAna6",
"name": "artifacts.tar.gz",
"label": "",
"uploader": {
"login": "github-actions[bot]",
"id": 41898282,
"node_id": "MDM6Qm90NDE4OTgyODI=",
"avatar_url": "https://avatars.githubusercontent.com/in/15368?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/github-actions%5Bbot%5D",
"html_url": "https://github.com/apps/github-actions",
"followers_url": "https://api.github.com/users/github-actions%5Bbot%5D/followers",
"following_url": "https://api.github.com/users/github-actions%5Bbot%5D/following{/other_user}",
"gists_url": "https://api.github.com/users/github-actions%5Bbot%5D/gists{/gist_id}",
"starred_url": "https://api.github.com/users/github-actions%5Bbot%5D/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/github-actions%5Bbot%5D/subscriptions",
"organizations_url": "https://api.github.com/users/github-actions%5Bbot%5D/orgs",
"repos_url": "https://api.github.com/users/github-actions%5Bbot%5D/repos",
"events_url": "https://api.github.com/users/github-actions%5Bbot%5D/events{/privacy}",
"received_events_url": "https://api.github.com/users/github-actions%5Bbot%5D/received_events",
"type": "Bot",
"site_admin": false
},
"content_type": "application/gzip",
"state": "uploaded",
"size": 203429,
"download_count": 0,
"created_at": "2022-11-09T19:57:17Z",
"updated_at": "2022-11-09T19:57:17Z",
"browser_download_url": "https://github.com/owner/reponame/releases/download/untagged-959f2cde363466e20879/artifacts.tar.gz"
}]'''
helper.registerAllowedMethod("GenericTrigger", [Map.class], null)
binding.setVariable('tag', '1.0.0')
binding.setVariable('release_url', 'https://api.github.com/repos/Codertocat/Hello-World/releases/17372790')
binding.setVariable('assets_url', 'https://api.github.com/repos/owner/name/releases/1234/assets')
helper.registerAllowedMethod('readJSON', [Map], { Map parameters ->
return new JsonSlurper().parseText(json)
})
super.setUp()
}
@Test
void testStandardReleasePipelineWithGenericTriggers() {
super.testPipeline('tests/jenkins/jobs/StandardReleasePipelineWithGenericTriggers_Jenkinsfile')
}
@Test
void testStandardReleasePipelineWithTagTriggers() {
super.testPipeline('tests/jenkins/jobs/StandardReleasePipelineWithGenericTriggersTag_Jenkinsfile')
}
@Test
void 'validate override values'() {
runScript("tests/jenkins/jobs/StandardReleasePipelineWithGenericTriggers_Jenkinsfile")
def echoCommand = getCommands('echo').findAll{
command -> command.contains('agent')
}
assertThat(echoCommand.size(), equalTo(1))
assertThat(echoCommand, hasItem('Executing on agent [docker:[image:centos:7, reuseNode:false, stages:[:], args:-e JAVA_HOME=/opt/java/openjdk-17, alwaysPull:true, containerPerStageRoot:false, label:AL2-X64]]'))
}
@Test
void 'validate default triggers'(){
runScript("tests/jenkins/jobs/StandardReleasePipelineWithGenericTriggers_Jenkinsfile")
def cmd = getCommands('GenericTrigger').findAll{
c -> c.contains('generic')
}
assertThat(cmd.size(), equalTo(1))
assertThat(cmd, hasItem('{genericVariables=[{key=ref, value=$.release.tag_name}, {key=repository, value=$.repository.html_url}, {key=action, value=$.action}, {key=isDraft, value=$.release.draft}, {key=release_url, value=$.release.url}, {key=assets_url, value=$.release.assets_url}], tokenCredentialId=opensearch-ci-webhook-trigger-token, causeString=A tag was cut on opensearch-ci repo, printContributedVariables=false, printPostContent=false, regexpFilterText=$isDraft $action, regexpFilterExpression=^true created$}'))
}
@Test
void 'validate release is published'(){
runScript("tests/jenkins/jobs/StandardReleasePipelineWithGenericTriggers_Jenkinsfile")
def cmd = getCommands('sh').findAll{
c -> c.contains('curl')
}
assertThat(cmd, hasItem("curl -X PATCH -H 'Accept: application/vnd.github+json' -H 'Authorization: Bearer GITHUB_TOKEN' https://api.github.com/repos/Codertocat/Hello-World/releases/17372790 -d '{\"tag_name\":\"1.0.0\",\"draft\":false,\"prerelease\":false}'"))
}
@Test
void 'verify download assets'() {
runScript("tests/jenkins/jobs/StandardReleasePipelineWithGenericTriggers_Jenkinsfile")
def cmd = getCommands('sh').findAll{
c -> c.contains('curl')
}
assertThat(cmd, hasItem("curl -J -L -H 'Accept: application/octet-stream' -H 'Authorization: Bearer GITHUB_TOKEN' https://api.github.com/repos/owner/reponame/releases/assets/123456 -o artifacts.tar.gz && tar -xvf artifacts.tar.gz"))
assertThat(cmd, hasItem("{script=curl -H 'Accept: application/vnd.github+json' -H 'Authorization: Bearer GITHUB_TOKEN' https://api.github.com/repos/owner/name/releases/1234/assets, returnStdout=true}"))
}
@Test
void 'validate default values'() {
runScript("tests/jenkins/jobs/StandardReleasePipelineWithGenericTriggersTag_Jenkinsfile")
def echoCommand = getCommands('echo').findAll{
command -> command.contains('agent')
}
assertThat(echoCommand.size(), equalTo(1))
assertThat(echoCommand, hasItem('Executing on agent [docker:[image:opensearchstaging/ci-runner:release-centos7-clients-v4, reuseNode:false, stages:[:], args:-e JAVA_HOME=/opt/java/openjdk-11, alwaysPull:true, containerPerStageRoot:false, label:Jenkins-Agent-AL2-X64-C54xlarge-Docker-Host]]'))
}
@Test
void 'use tag as trigger'(){
runScript("tests/jenkins/jobs/StandardReleasePipelineWithGenericTriggersTag_Jenkinsfile")
def cmd = getCommands('GenericTrigger').findAll{
c -> c.contains('generic')
}
assertThat(cmd.size(), equalTo(1))
assertThat(cmd, hasItem('{genericVariables=[{key=ref, value=.ref}, {key=repository, value=$.repository.html_url}, {key=action, value=$.action}, {key=isDraft, value=$.release.draft}, {key=release_url, value=$.release.url}, {key=assets_url, value=$.release.assets_url}], tokenCredentialId=opensearch-ci-webhook-trigger-token, causeString=A tag was cut on opensearch-ci repo, printContributedVariables=false, printPostContent=false, regexpFilterText=$ref, regexpFilterExpression=^refs/tags/.*}'))
}
@Test
void 'validate skipping download stage'(){
runScript("tests/jenkins/jobs/StandardReleasePipelineWithGenericTriggersTag_Jenkinsfile")
def cmd = getCommands('echo').findAll{
c -> c.contains('stage')
}
assertThat(cmd, hasItem('Skipping stage Download artifacts'))
}
@Test
void 'validate release is not published'(){
runScript("tests/jenkins/jobs/StandardReleasePipelineWithGenericTriggersTag_Jenkinsfile")
def cmd = getCommands('sh').findAll{
c -> c.contains('curl')
}
assertThat(cmd.size(), equalTo(0))
}
def getCommands(String method) {
def echoCommands = helper.callStack.findAll { call ->
call.methodName == method
}.collect { call ->
callArgsToString(call)
}
return echoCommands
}
}