Skip to content

Commit

Permalink
added some more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
codezninja committed Sep 22, 2020
1 parent 6e2272d commit befe708
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 45 deletions.
32 changes: 17 additions & 15 deletions src/ParameterStoreBuildWrapperPlugin.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import static TerraformEnvironmentStage.APPLY

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

public static void init() {
Expand All @@ -17,7 +17,7 @@ class ParameterStoreBuildWrapperPlugin implements TerraformValidateStagePlugin,
return this
}

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

@Override
public void apply(TerraformEnvironmentStage stage) {
def environment = stage.getEnvironment()
def parameterStorePath = pathForEnvironment(environment)
def environment = stage.getEnvironment()
ArrayList<Map> allParameterOptions = []

def options = [
path: parameterStorePath,
credentialsId: "${environment.toUpperCase()}_PARAMETER_STORE_ACCESS"
]
allParameterOptions << getEnvironmentParameterOptions(environment)
allParameterOptions.addAll(globalParameterOptions)

stage.decorate(PLAN, addParameterStoreBuildWrapper(options))
stage.decorate(APPLY, addParameterStoreBuildWrapper(options))

globalParameterOptions.each { gp ->
stage.decorate(PLAN, addParameterStoreBuildWrapper(gp))
stage.decorate(APPLY, addParameterStoreBuildWrapper(gp))
allParameterOptions.each { apo ->
stage.decorate(PLAN, addParameterStoreBuildWrapper(apo))
stage.decorate(APPLY, addParameterStoreBuildWrapper(apo))
}
}

Map getEnvironmentParameterOptions(String environment) {
return [
path: pathForEnvironment(environment),
credentialsId: "${environment.toUpperCase()}_PARAMETER_STORE_ACCESS"
]
}

String pathForEnvironment(String environment) {
String organization = Jenkinsfile.instance.getOrganization()
String repoName = Jenkinsfile.instance.getRepoName()
Expand All @@ -60,7 +62,7 @@ class ParameterStoreBuildWrapperPlugin implements TerraformValidateStagePlugin,
return pathPattern(patternOptions)
}

public static Closure addParameterStoreBuildWrapper(Map options = []) {
public Closure addParameterStoreBuildWrapper(Map options = [:]) {
def Map defaultOptions = [
naming: 'basename'
]
Expand Down
197 changes: 167 additions & 30 deletions test/ParameterStoreBuildWrapperPluginTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@ import static org.hamcrest.Matchers.hasItem
import static org.hamcrest.Matchers.instanceOf
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertThat
import static org.junit.Assert.assertTrue
import static org.mockito.Mockito.mock
import static org.mockito.Mockito.when
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.anyString
import static org.mockito.Mockito.eq
import static org.mockito.Mockito.doReturn
import static org.mockito.Mockito.any
import static org.mockito.Mockito.never
import static org.mockito.Mockito.times

import org.junit.After
import org.junit.Test
Expand All @@ -28,43 +37,151 @@ class ParameterStoreBuildWrapperPluginTest {
}

@Test
void modifiesTerraformEnvironmentStageCommandWithGlobalParameter() {
ParameterStoreBuildWrapperPlugin.withGlobalParameter('/somePath/').init()
void modifiesTerraformValidateStageCommand() {
ParameterStoreBuildWrapperPlugin.init()

Collection actualPlugins = TerraformEnvironmentStage.getPlugins()
Collection actualPlugins = TerraformValidateStage.getPlugins()
assertThat(actualPlugins, hasItem(instanceOf(ParameterStoreBuildWrapperPlugin.class)))
}
}

public class Apply {
@After
public void reset() {
Jenkinsfile.instance = null
ParameterStoreBuildWrapperPlugin.reset()
}

private configureJenkins(Map config = [:]) {
Jenkinsfile.instance = mock(Jenkinsfile.class)
when(Jenkinsfile.instance.getStandardizedRepoSlug()).thenReturn(config.repoSlug)
when(Jenkinsfile.instance.getRepoName()).thenReturn(config.repoName ?: 'repo')
when(Jenkinsfile.instance.getOrganization()).thenReturn(config.organization ?: 'org')
when(Jenkinsfile.instance.getEnv()).thenReturn(config.env ?: [:])
}

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

Collection actualPlugins = TerraformEnvironmentStage.getPlugins()
assertThat(actualPlugins, hasItem(instanceOf(ParameterStoreBuildWrapperPlugin.class)))
plugin.apply(stage)

verify(stage, never()).decorate(expectedClosure)
}

@Test
void modifiesTerraformValidateStageCommand() {
ParameterStoreBuildWrapperPlugin.init()
void decorateTheTerraformValidateStageIfGlobalParametersSet() {
String path = '/somePath/'
def expectedClosure = { -> }
Map gp = [path: path]
TerraformValidateStage stage = mock(TerraformValidateStage.class)
ParameterStoreBuildWrapperPlugin plugin = spy(new ParameterStoreBuildWrapperPlugin())

Collection actualPlugins = TerraformValidateStage.getPlugins()
assertThat(actualPlugins, hasItem(instanceOf(ParameterStoreBuildWrapperPlugin.class)))
doReturn(expectedClosure).when(plugin).addParameterStoreBuildWrapper(gp)

plugin.withGlobalParameter(path)
plugin.apply(stage)

verify(stage).decorate(TerraformValidateStage.ALL, expectedClosure)
}

@Test
void modifiesTerraformValidateStageCommandWithGlobalParameter() {
ParameterStoreBuildWrapperPlugin.withGlobalParameter('/somePath/').init()
void decorateTheTerraformEnvironmentStageIfGlobalParametersNotSet() {
String organization = "MyOrg"
String repoName = "MyRepo"
String environment = "MyEnv"
Map apo = [path: "/${organization}/${repoName}/${environment}/", credentialsId: "${environment.toUpperCase()}_PARAMETER_STORE_ACCESS"]
def expectedClosure = { -> }
configureJenkins(repoName: repoName, organization: organization)

Collection actualPlugins = TerraformValidateStage.getPlugins()
assertThat(actualPlugins, hasItem(instanceOf(ParameterStoreBuildWrapperPlugin.class)))
TerraformEnvironmentStage stage = mock(TerraformEnvironmentStage.class)
ParameterStoreBuildWrapperPlugin plugin = spy(new ParameterStoreBuildWrapperPlugin())

doReturn(environment).when(stage).getEnvironment()
doReturn(apo).when(plugin).getEnvironmentParameterOptions(environment)
doReturn(expectedClosure).when(plugin).addParameterStoreBuildWrapper(apo)

plugin.apply(stage)

verify(stage, times(2)).decorate(anyString(), eq(expectedClosure))
}

@Test
void modifiesTerraformValidateStageCommandWithGlobalParameterAndOptions() {
ParameterStoreBuildWrapperPlugin.withGlobalParameter('/somePath/', [someKey: true, anotherKey: 'someValue']).init()
void decorateTheTerraformEnvironmentStageWhenGlobalParametersSet() {
String organization = "MyOrg"
String repoName = "MyRepo"
String environment = "MyEnv"
String path = '/someOtherPath/'
Map apo = [path: "/${organization}/${repoName}/${environment}/", credentialsId: "${environment.toUpperCase()}_PARAMETER_STORE_ACCESS"]
Map gp = [path: path]
def expectedClosure = { -> }
def secondClosure = { -> }
configureJenkins(repoName: repoName, organization: organization)

Collection actualPlugins = TerraformValidateStage.getPlugins()
assertThat(actualPlugins, hasItem(instanceOf(ParameterStoreBuildWrapperPlugin.class)))
TerraformEnvironmentStage stage = mock(TerraformEnvironmentStage.class)
ParameterStoreBuildWrapperPlugin plugin = spy(new ParameterStoreBuildWrapperPlugin())

doReturn(environment).when(stage).getEnvironment()
doReturn(apo).when(plugin).getEnvironmentParameterOptions(environment)
doReturn(expectedClosure).when(plugin).addParameterStoreBuildWrapper(apo)
doReturn(secondClosure).when(plugin).addParameterStoreBuildWrapper(gp)

plugin.withGlobalParameter(path)
plugin.apply(stage)

verify(stage, times(2)).decorate(anyString(), eq(expectedClosure))
verify(stage, times(2)).decorate(anyString(), eq(secondClosure))
}

}

public class GetEnvironmentParameterOptions {
@After
public void reset() {
Jenkinsfile.instance = null
ParameterStoreBuildWrapperPlugin.reset()
}

private configureJenkins(Map config = [:]) {
Jenkinsfile.instance = mock(Jenkinsfile.class)
when(Jenkinsfile.instance.getStandardizedRepoSlug()).thenReturn(config.repoSlug)
when(Jenkinsfile.instance.getRepoName()).thenReturn(config.repoName ?: 'repo')
when(Jenkinsfile.instance.getOrganization()).thenReturn(config.organization ?: 'org')
when(Jenkinsfile.instance.getEnv()).thenReturn(config.env ?: [:])
}

@Test
void validPathBasedOnEnvironment() {
String organization = "MyOrg"
String repoName = "MyRepo"
String environment = "qa"
Map expected = [path: "/${organization}/${repoName}/${environment}/".toString()]

configureJenkins(repoName: repoName, organization: organization)
ParameterStoreBuildWrapperPlugin plugin = spy(new ParameterStoreBuildWrapperPlugin())
doReturn(expected.path.toString()).when(plugin).pathForEnvironment(environment)

Map actual = plugin.getEnvironmentParameterOptions(environment)

assertEquals(expected.path, actual.path)
}

@Test
void validCredentialsBasedOnEnvironment() {
String organization = "MyOrg"
String repoName = "MyRepo"
String environment = "qa"
Map expected = [credentialsId: "${environment.toUpperCase()}_PARAMETER_STORE_ACCESS"]

configureJenkins(repoName: repoName, organization: organization)
ParameterStoreBuildWrapperPlugin plugin = spy(new ParameterStoreBuildWrapperPlugin())
doReturn(expected.path.toString()).when(plugin).pathForEnvironment(environment)

Map actual = plugin.getEnvironmentParameterOptions(environment)

assertEquals(expected.credentialsId, actual.credentialsId)
}
}

Expand Down Expand Up @@ -133,28 +250,48 @@ class ParameterStoreBuildWrapperPluginTest {
}

@Test
void addGlobalParameter() {
def result = ParameterStoreBuildWrapperPlugin.withGlobalParameter('/path/', [])

void addGlobalParameterWithNoOptions() {
String path = '/path/'
def result = ParameterStoreBuildWrapperPlugin.withGlobalParameter(path)
println(result)
assertEquals([[path: '/path/']], result.globalParameterOptions)
}

@Test
void addGlobalParameterWithEmptyOptions() {
Map options = [:]
String path = '/path/'
ArrayList<Map> expected = []
expected << [path: path] + options

def result = ParameterStoreBuildWrapperPlugin.withGlobalParameter(path, options)

assertEquals(expected, result.globalParameterOptions)
}

@Test
void addGlobalParameterWithOptions() {
def result = ParameterStoreBuildWrapperPlugin.withGlobalParameter('/path/', [recursive: true, basename: 'relative'])
Map options = [recursive: true, basename: 'relative']
String path = '/path/'
ArrayList<Map> expected = []
expected << [path: path] + options

assertEquals([[path: '/path/', recursive: true, basename: 'relative']], result.globalParameterOptions)
def result = ParameterStoreBuildWrapperPlugin.withGlobalParameter(path, options)

assertEquals(expected, result.globalParameterOptions)
}

@Test
void addMulitpleGlobalParameters() {
ArrayList expected = []
def result = ParameterStoreBuildWrapperPlugin.withGlobalParameter('/path/', [])
.withGlobalParameter('/path2/', [recursive: true])
.withGlobalParameter('/path3/', [basename: 'something'])
ArrayList<Map> expected = []
def result = ParameterStoreBuildWrapperPlugin.withGlobalParameter('/path/')
.withGlobalParameter('/path2/', [:])
.withGlobalParameter('/path3/', [recursive: true])
.withGlobalParameter('/path4/', [basename: 'something'])
expected << [path:'/path/']
expected << [path: '/path2/', recursive: true]
expected << [path: '/path3/', basename: 'something']
expected << [path:'/path2/']
expected << [path: '/path3/', recursive: true]
expected << [path: '/path4/', basename: 'something']
assertEquals(expected, result.globalParameterOptions)
}
}
Expand Down

0 comments on commit befe708

Please sign in to comment.