Skip to content

Commit 8e84ddc

Browse files
committed
added some more unit tests
1 parent 6e2272d commit 8e84ddc

File tree

2 files changed

+184
-45
lines changed

2 files changed

+184
-45
lines changed

src/ParameterStoreBuildWrapperPlugin.groovy

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import static TerraformEnvironmentStage.APPLY
44

55
class ParameterStoreBuildWrapperPlugin implements TerraformValidateStagePlugin, TerraformEnvironmentStagePlugin {
66
private static globalPathPattern
7-
private static ArrayList globalParameterOptions = []
7+
private static ArrayList<Map> globalParameterOptions = []
88
private static defaultPathPattern = { options -> "/${options['organization']}/${options['repoName']}/${options['environment']}/" }
99

1010
public static void init() {
@@ -17,7 +17,7 @@ class ParameterStoreBuildWrapperPlugin implements TerraformValidateStagePlugin,
1717
return this
1818
}
1919

20-
public static withGlobalParameter(String path, options=[]) {
20+
public static withGlobalParameter(String path, Map options = [:]) {
2121
globalParameterOptions << [path: path] + options
2222
return this
2323
}
@@ -31,23 +31,25 @@ class ParameterStoreBuildWrapperPlugin implements TerraformValidateStagePlugin,
3131

3232
@Override
3333
public void apply(TerraformEnvironmentStage stage) {
34-
def environment = stage.getEnvironment()
35-
def parameterStorePath = pathForEnvironment(environment)
34+
def environment = stage.getEnvironment()
35+
ArrayList<Map> allParameterOptions = []
3636

37-
def options = [
38-
path: parameterStorePath,
39-
credentialsId: "${environment.toUpperCase()}_PARAMETER_STORE_ACCESS"
40-
]
37+
allParameterOptions << getEnvironmentParameterOptions(environment)
38+
allParameterOptions.addAll(globalParameterOptions)
4139

42-
stage.decorate(PLAN, addParameterStoreBuildWrapper(options))
43-
stage.decorate(APPLY, addParameterStoreBuildWrapper(options))
44-
45-
globalParameterOptions.each { gp ->
46-
stage.decorate(PLAN, addParameterStoreBuildWrapper(gp))
47-
stage.decorate(APPLY, addParameterStoreBuildWrapper(gp))
40+
allParameterOptions.each { apo ->
41+
stage.decorate(PLAN, addParameterStoreBuildWrapper(apo))
42+
stage.decorate(APPLY, addParameterStoreBuildWrapper(apo))
4843
}
4944
}
5045

46+
Map getEnvironmentParameterOptions(String environment) {
47+
return [
48+
path: pathForEnvironment(environment),
49+
credentialsId: "${environment.toUpperCase()}_PARAMETER_STORE_ACCESS"
50+
]
51+
}
52+
5153
String pathForEnvironment(String environment) {
5254
String organization = Jenkinsfile.instance.getOrganization()
5355
String repoName = Jenkinsfile.instance.getRepoName()
@@ -60,7 +62,7 @@ class ParameterStoreBuildWrapperPlugin implements TerraformValidateStagePlugin,
6062
return pathPattern(patternOptions)
6163
}
6264

63-
public static Closure addParameterStoreBuildWrapper(Map options = []) {
65+
public Closure addParameterStoreBuildWrapper(Map options = [:]) {
6466
def Map defaultOptions = [
6567
naming: 'basename'
6668
]

test/ParameterStoreBuildWrapperPluginTest.groovy

Lines changed: 167 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@ import static org.hamcrest.Matchers.hasItem
22
import static org.hamcrest.Matchers.instanceOf
33
import static org.junit.Assert.assertEquals
44
import static org.junit.Assert.assertThat
5+
import static org.junit.Assert.assertTrue
56
import static org.mockito.Mockito.mock
67
import static org.mockito.Mockito.when
8+
import static org.mockito.Mockito.spy;
9+
import static org.mockito.Mockito.verify;
10+
import static org.mockito.Mockito.anyString
11+
import static org.mockito.Mockito.eq
12+
import static org.mockito.Mockito.doReturn
13+
import static org.mockito.Mockito.any
14+
import static org.mockito.Mockito.never
15+
import static org.mockito.Mockito.times
716

817
import org.junit.After
918
import org.junit.Test
@@ -28,43 +37,151 @@ class ParameterStoreBuildWrapperPluginTest {
2837
}
2938

3039
@Test
31-
void modifiesTerraformEnvironmentStageCommandWithGlobalParameter() {
32-
ParameterStoreBuildWrapperPlugin.withGlobalParameter('/somePath/').init()
40+
void modifiesTerraformValidateStageCommand() {
41+
ParameterStoreBuildWrapperPlugin.init()
3342

34-
Collection actualPlugins = TerraformEnvironmentStage.getPlugins()
43+
Collection actualPlugins = TerraformValidateStage.getPlugins()
3544
assertThat(actualPlugins, hasItem(instanceOf(ParameterStoreBuildWrapperPlugin.class)))
3645
}
46+
}
47+
48+
public class Apply {
49+
@After
50+
public void reset() {
51+
Jenkinsfile.instance = null
52+
ParameterStoreBuildWrapperPlugin.reset()
53+
}
54+
55+
private configureJenkins(Map config = [:]) {
56+
Jenkinsfile.instance = mock(Jenkinsfile.class)
57+
when(Jenkinsfile.instance.getStandardizedRepoSlug()).thenReturn(config.repoSlug)
58+
when(Jenkinsfile.instance.getRepoName()).thenReturn(config.repoName ?: 'repo')
59+
when(Jenkinsfile.instance.getOrganization()).thenReturn(config.organization ?: 'org')
60+
when(Jenkinsfile.instance.getEnv()).thenReturn(config.env ?: [:])
61+
}
3762

3863
@Test
39-
void modifiesTerraformEnvironmentStageCommandWithGlobalParameterAndOptions() {
40-
ParameterStoreBuildWrapperPlugin.withGlobalParameter('/somePath/', [someKey: true, anotherKey: 'someValue']).init()
64+
void doesNotDecorateTheTerraformValidateStageIfGlobalParametersNotSet() {
65+
def expectedClosure = { -> }
66+
TerraformValidateStage stage = mock(TerraformValidateStage.class)
67+
ParameterStoreBuildWrapperPlugin plugin = spy(new ParameterStoreBuildWrapperPlugin())
4168

42-
Collection actualPlugins = TerraformEnvironmentStage.getPlugins()
43-
assertThat(actualPlugins, hasItem(instanceOf(ParameterStoreBuildWrapperPlugin.class)))
69+
plugin.apply(stage)
70+
71+
verify(stage, never()).decorate(expectedClosure)
4472
}
4573

4674
@Test
47-
void modifiesTerraformValidateStageCommand() {
48-
ParameterStoreBuildWrapperPlugin.init()
75+
void decorateTheTerraformValidateStageIfGlobalParametersSet() {
76+
String path = '/somePath/'
77+
def expectedClosure = { -> }
78+
Map gp = [path: path]
79+
TerraformValidateStage stage = mock(TerraformValidateStage.class)
80+
ParameterStoreBuildWrapperPlugin plugin = spy(new ParameterStoreBuildWrapperPlugin())
4981

50-
Collection actualPlugins = TerraformValidateStage.getPlugins()
51-
assertThat(actualPlugins, hasItem(instanceOf(ParameterStoreBuildWrapperPlugin.class)))
82+
doReturn(expectedClosure).when(plugin).addParameterStoreBuildWrapper(gp)
83+
84+
plugin.withGlobalParameter(path)
85+
plugin.apply(stage)
86+
87+
verify(stage).decorate(TerraformValidateStage.ALL, expectedClosure)
5288
}
5389

5490
@Test
55-
void modifiesTerraformValidateStageCommandWithGlobalParameter() {
56-
ParameterStoreBuildWrapperPlugin.withGlobalParameter('/somePath/').init()
91+
void decorateTheTerraformEnvironmentStageIfGlobalParametersNotSet() {
92+
String organization = "MyOrg"
93+
String repoName = "MyRepo"
94+
String environment = "MyEnv"
95+
Map apo = [path: "/${organization}/${repoName}/${environment}/", credentialsId: "${environment.toUpperCase()}_PARAMETER_STORE_ACCESS"]
96+
def expectedClosure = { -> }
97+
configureJenkins(repoName: repoName, organization: organization)
5798

58-
Collection actualPlugins = TerraformValidateStage.getPlugins()
59-
assertThat(actualPlugins, hasItem(instanceOf(ParameterStoreBuildWrapperPlugin.class)))
99+
TerraformEnvironmentStage stage = mock(TerraformEnvironmentStage.class)
100+
ParameterStoreBuildWrapperPlugin plugin = spy(new ParameterStoreBuildWrapperPlugin())
101+
102+
doReturn(environment).when(stage).getEnvironment()
103+
doReturn(apo).when(plugin).getEnvironmentParameterOptions(environment)
104+
doReturn(expectedClosure).when(plugin).addParameterStoreBuildWrapper(apo)
105+
106+
plugin.apply(stage)
107+
108+
verify(stage, times(2)).decorate(anyString(), eq(expectedClosure))
60109
}
61110

62111
@Test
63-
void modifiesTerraformValidateStageCommandWithGlobalParameterAndOptions() {
64-
ParameterStoreBuildWrapperPlugin.withGlobalParameter('/somePath/', [someKey: true, anotherKey: 'someValue']).init()
112+
void decorateTheTerraformEnvironmentStageWhenGlobalParametersSet() {
113+
String organization = "MyOrg"
114+
String repoName = "MyRepo"
115+
String environment = "MyEnv"
116+
String path = '/someOtherPath/'
117+
Map apo = [path: "/${organization}/${repoName}/${environment}/", credentialsId: "${environment.toUpperCase()}_PARAMETER_STORE_ACCESS"]
118+
Map gp = [path: path]
119+
def firstClosure = { -> }
120+
def secondClosure = { -> }
121+
configureJenkins(repoName: repoName, organization: organization)
65122

66-
Collection actualPlugins = TerraformValidateStage.getPlugins()
67-
assertThat(actualPlugins, hasItem(instanceOf(ParameterStoreBuildWrapperPlugin.class)))
123+
TerraformEnvironmentStage stage = mock(TerraformEnvironmentStage.class)
124+
ParameterStoreBuildWrapperPlugin plugin = spy(new ParameterStoreBuildWrapperPlugin())
125+
126+
doReturn(environment).when(stage).getEnvironment()
127+
doReturn(apo).when(plugin).getEnvironmentParameterOptions(environment)
128+
doReturn(firstClosure).when(plugin).addParameterStoreBuildWrapper(gp)
129+
doReturn(secondClosure).when(plugin).addParameterStoreBuildWrapper(apo)
130+
131+
plugin.withGlobalParameter(path)
132+
plugin.apply(stage)
133+
134+
verify(stage, times(2)).decorate(anyString(), eq(firstClosure))
135+
verify(stage, times(2)).decorate(anyString(), eq(secondClosure))
136+
}
137+
138+
}
139+
140+
public class GetEnvironmentParameterOptions {
141+
@After
142+
public void reset() {
143+
Jenkinsfile.instance = null
144+
ParameterStoreBuildWrapperPlugin.reset()
145+
}
146+
147+
private configureJenkins(Map config = [:]) {
148+
Jenkinsfile.instance = mock(Jenkinsfile.class)
149+
when(Jenkinsfile.instance.getStandardizedRepoSlug()).thenReturn(config.repoSlug)
150+
when(Jenkinsfile.instance.getRepoName()).thenReturn(config.repoName ?: 'repo')
151+
when(Jenkinsfile.instance.getOrganization()).thenReturn(config.organization ?: 'org')
152+
when(Jenkinsfile.instance.getEnv()).thenReturn(config.env ?: [:])
153+
}
154+
155+
@Test
156+
void validPathBasedOnEnvironment() {
157+
String organization = "MyOrg"
158+
String repoName = "MyRepo"
159+
String environment = "qa"
160+
Map expected = [path: "/${organization}/${repoName}/${environment}/".toString()]
161+
162+
configureJenkins(repoName: repoName, organization: organization)
163+
ParameterStoreBuildWrapperPlugin plugin = spy(new ParameterStoreBuildWrapperPlugin())
164+
doReturn(expected.path.toString()).when(plugin).pathForEnvironment(environment)
165+
166+
Map actual = plugin.getEnvironmentParameterOptions(environment)
167+
168+
assertEquals(expected.path, actual.path)
169+
}
170+
171+
@Test
172+
void validCredentialsBasedOnEnvironment() {
173+
String organization = "MyOrg"
174+
String repoName = "MyRepo"
175+
String environment = "qa"
176+
Map expected = [credentialsId: "${environment.toUpperCase()}_PARAMETER_STORE_ACCESS"]
177+
178+
configureJenkins(repoName: repoName, organization: organization)
179+
ParameterStoreBuildWrapperPlugin plugin = spy(new ParameterStoreBuildWrapperPlugin())
180+
doReturn(expected.path.toString()).when(plugin).pathForEnvironment(environment)
181+
182+
Map actual = plugin.getEnvironmentParameterOptions(environment)
183+
184+
assertEquals(expected.credentialsId, actual.credentialsId)
68185
}
69186
}
70187

@@ -133,28 +250,48 @@ class ParameterStoreBuildWrapperPluginTest {
133250
}
134251

135252
@Test
136-
void addGlobalParameter() {
137-
def result = ParameterStoreBuildWrapperPlugin.withGlobalParameter('/path/', [])
138-
253+
void addGlobalParameterWithNoOptions() {
254+
String path = '/path/'
255+
def result = ParameterStoreBuildWrapperPlugin.withGlobalParameter(path)
256+
println(result)
139257
assertEquals([[path: '/path/']], result.globalParameterOptions)
140258
}
141259

260+
@Test
261+
void addGlobalParameterWithEmptyOptions() {
262+
Map options = [:]
263+
String path = '/path/'
264+
ArrayList<Map> expected = []
265+
expected << [path: path] + options
266+
267+
def result = ParameterStoreBuildWrapperPlugin.withGlobalParameter(path, options)
268+
269+
assertEquals(expected, result.globalParameterOptions)
270+
}
271+
142272
@Test
143273
void addGlobalParameterWithOptions() {
144-
def result = ParameterStoreBuildWrapperPlugin.withGlobalParameter('/path/', [recursive: true, basename: 'relative'])
274+
Map options = [recursive: true, basename: 'relative']
275+
String path = '/path/'
276+
ArrayList<Map> expected = []
277+
expected << [path: path] + options
145278

146-
assertEquals([[path: '/path/', recursive: true, basename: 'relative']], result.globalParameterOptions)
279+
def result = ParameterStoreBuildWrapperPlugin.withGlobalParameter(path, options)
280+
281+
assertEquals(expected, result.globalParameterOptions)
147282
}
148283

149284
@Test
150285
void addMulitpleGlobalParameters() {
151-
ArrayList expected = []
152-
def result = ParameterStoreBuildWrapperPlugin.withGlobalParameter('/path/', [])
153-
.withGlobalParameter('/path2/', [recursive: true])
154-
.withGlobalParameter('/path3/', [basename: 'something'])
286+
ArrayList<Map> expected = []
287+
def result = ParameterStoreBuildWrapperPlugin.withGlobalParameter('/path/')
288+
.withGlobalParameter('/path2/', [:])
289+
.withGlobalParameter('/path3/', [recursive: true])
290+
.withGlobalParameter('/path4/', [basename: 'something'])
155291
expected << [path:'/path/']
156-
expected << [path: '/path2/', recursive: true]
157-
expected << [path: '/path3/', basename: 'something']
292+
expected << [path:'/path2/']
293+
expected << [path: '/path3/', recursive: true]
294+
expected << [path: '/path4/', basename: 'something']
158295
assertEquals(expected, result.globalParameterOptions)
159296
}
160297
}

0 commit comments

Comments
 (0)