forked from opensearch-project/opensearch-build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestPackerBuild.groovy
111 lines (95 loc) · 4.3 KB
/
TestPackerBuild.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
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import jenkins.tests.BuildPipelineTest
import org.junit.Before
import org.junit.Test
import static com.lesfurets.jenkins.unit.MethodCall.callArgsToString
import static org.hamcrest.MatcherAssert.assertThat
import static org.hamcrest.CoreMatchers.hasItem
import static com.lesfurets.jenkins.unit.global.lib.GitSource.gitSource
import static com.lesfurets.jenkins.unit.global.lib.LibraryConfiguration.library
class TestPackerBuild extends BuildPipelineTest {
// Variables
String packerBuildGitRespository = 'https://github.com/opensearch-project/opensearch-ci'
String packerBuildGitRespositoryReference = 'main'
String packerTemplateName = 'jenkins-agent-al2-arm64.json'
@Override
@Before
void setUp() {
helper.registerSharedLibrary(
library().name('jenkins')
.defaultVersion('2.2.0')
.allowOverride(true)
.implicit(true)
.targetPath('vars')
.retriever(gitSource('https://github.com/opensearch-project/opensearch-build-libraries.git'))
.build()
)
super.setUp()
binding.setVariable('PACKER_TEMPLATE_NAME', packerTemplateName)
def sample_json = [
"variables" : [
"name-base" : "Jenkins-Agent-AL2-X64" ,
"os-version" : "AL2" ,
"build-region" : "us-east-1" ,
"build-vpc" : "vpc-<>" ,
"build-subnet" : "subnet-<>" ,
"build-secgrp" : "sg-<>" ,
"build-time" : "{{isotime \"2006-01-02T03-04-05Z\"}}" ,
"aws_ami_region" : "us-east-1"
]
]
helper.registerAllowedMethod("readJSON", [Map.class], {c -> sample_json})
def sample_json_output = [
"variables" : [
"name-base" : "Jenkins-Agent-AL2-X64" ,
"os-version" : "AL2" ,
"build-region" : "us-east-1" ,
"build-vpc" : "vpc-123" ,
"build-subnet" : "subnet-123" ,
"build-secgrp" : "sg-123" ,
"build-time" : "{{isotime \"2006-01-02T03-04-05Z\"}}" ,
"aws_ami_region" : "us-east-1"
]
]
helper.registerAllowedMethod("writeJSON", [Map.class], {c -> sample_json_output})
helper.registerAllowedMethod("checkout", [Map], {})
helper.registerAllowedMethod("git", [Map])
helper.registerAllowedMethod("withCredentials", [Map, Closure], { args, closure ->
closure.delegate = delegate
return helper.callClosure(closure)
})
helper.registerAllowedMethod("withAWS", [Map, Closure], { args, closure ->
closure.delegate = delegate
return helper.callClosure(closure)
})
helper.addShMock("cd packer && packer build -color=false substitute_jenkins-agent-al2-arm64.json") { script ->
return [stdout: "", exitValue: 0]
}
}
@Test
void PackerBuildRegression() {
super.testPipeline('jenkins/packer/packer-build.jenkinsfile',
'tests/jenkins/jenkinsjob-regression-files/packer/packer-build.jenkinsfile')
def gitCheckoutCommands = getCommands('checkout', 'GitSCM').findAll {
shCommand -> shCommand.contains('git')
}
assertThat(gitCheckoutCommands, hasItem("{\$class=GitSCM, branches=[{name=main}], userRemoteConfigs=[{url=https://github.com/opensearch-project/opensearch-ci}]}".toString()))
def aws = getCommands('withAWS', 'packer')
assertThat(aws, hasItem('{role=opensearch-packer, roleAccount=AWS_ACCOUNT_PUBLIC, duration=3600, roleSessionName=jenkins-session, useNode=true}, groovy.lang.Closure'))
def packerCommands = getCommands('sh', 'packer')
assertThat(packerCommands, hasItem('cd packer && packer build -color=false substitute_jenkins-agent-al2-arm64.json'))
}
def getCommands(method, text) {
def shCommands = helper.callStack.findAll { call ->
call.methodName == method
}.collect { call ->
callArgsToString(call)
}.findAll { command ->
command.contains(text)
}
return shCommands
}
}