Skip to content

Commit b8e9c79

Browse files
committed
add tests for TerraformApplyCommand and TerraformPlanCommand
1 parent d73b38d commit b8e9c79

File tree

3 files changed

+106
-2
lines changed

3 files changed

+106
-2
lines changed

src/TerraformApplyCommand.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ class TerraformApplyCommand implements TerraformCommand, Resettable {
4343
return this
4444
}
4545

46-
public TerraformPlanCommand withVariableFile(String key, Map values) {
46+
public TerraformApplyCommand withVariableFile(String key, Map values) {
4747
String fileName = "${this.environment}-${key}.tfvars"
4848
String value = convertMapToCliString(values)
4949
Jenkinsfile.writeFile(fileName, "${key}=${value}")
5050
return withVariableFile(fileName)
5151
}
5252

53-
public TerraformPlanCommand withVariableFile(String fileName) {
53+
public TerraformApplyCommand withVariableFile(String fileName) {
5454
this.args << "-var-file=./${fileName}"
5555
return this
5656
}

test/TerraformApplyCommandTest.groovy

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import static org.mockito.Mockito.mock
99
import static org.mockito.Mockito.spy
1010
import static org.mockito.Mockito.times
1111
import static org.mockito.Mockito.verify
12+
import static org.mockito.Mockito.verifyNoMoreInteractions
1213

1314
import org.junit.jupiter.api.Nested
1415
import org.junit.jupiter.api.Test
@@ -98,6 +99,57 @@ class TerraformApplyCommandTest {
9899
}
99100
}
100101

102+
@Nested
103+
public class WithVariableFile {
104+
@Test
105+
void isFluent1() {
106+
def stage = new TerraformApplyCommand('foo')
107+
def result = stage.withVariableFile('somekey')
108+
109+
assertThat(result, equalTo(stage))
110+
}
111+
112+
@Test
113+
void testWithFile() {
114+
def filename = 'filename'
115+
def command = new TerraformApplyCommand().withVariableFile(filename)
116+
117+
def actualCommand = command.toString()
118+
assertThat(actualCommand, containsString("-var-file=./${filename}"))
119+
}
120+
}
121+
122+
@Nested
123+
public class WithVariableFileAndMap {
124+
@Test
125+
void isFluent() {
126+
def stage = new TerraformApplyCommand('foo')
127+
Jenkinsfile.original = mock(MockWorkflowScript.class)
128+
def result = stage.withVariableFile('somekey', ['key':'value'])
129+
130+
assertThat(result, equalTo(stage))
131+
}
132+
133+
@Test
134+
void testWithFileAndMap() {
135+
def myKey = 'myKey'
136+
def expectedValue = 'expValue'
137+
def map = [expectedKey:expectedValue]
138+
def original = mock(MockWorkflowScript.class)
139+
Jenkinsfile.original = original
140+
def command = spy(new TerraformApplyCommand("dev")).withVariableFile(myKey, map)
141+
142+
def actualCommand = command.toString()
143+
assertThat(actualCommand, containsString("-var-file=./dev-${myKey}"))
144+
145+
def filename = "dev-${myKey}.tfvars"
146+
verify(command).withVariableFile(filename)
147+
def content = "${myKey}={expectedKey=\"${expectedValue}\"}"
148+
verify(original).writeFile(file: filename.toString(), text: content.toString())
149+
verifyNoMoreInteractions(original)
150+
}
151+
}
152+
101153
@Nested
102154
public class WithVariableMap {
103155
@Test

test/TerraformPlanCommandTest.groovy

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import static org.mockito.Mockito.mock
99
import static org.mockito.Mockito.spy
1010
import static org.mockito.Mockito.times
1111
import static org.mockito.Mockito.verify
12+
import static org.mockito.Mockito.verifyNoMoreInteractions
1213

1314
import org.junit.jupiter.api.Nested
1415
import org.junit.jupiter.api.Test
@@ -167,6 +168,57 @@ class TerraformPlanCommandTest {
167168
}
168169
}
169170

171+
@Nested
172+
public class WithVariableFile {
173+
@Test
174+
void isFluent1() {
175+
def stage = new TerraformPlanCommand('foo')
176+
def result = stage.withVariableFile('somekey')
177+
178+
assertThat(result, equalTo(stage))
179+
}
180+
181+
@Test
182+
void testWithFile() {
183+
def filename = 'filename'
184+
def command = new TerraformPlanCommand().withVariableFile(filename)
185+
186+
def actualCommand = command.toString()
187+
assertThat(actualCommand, containsString("-var-file=./${filename}"))
188+
}
189+
}
190+
191+
@Nested
192+
public class WithVariableFileAndMap {
193+
@Test
194+
void isFluent() {
195+
def stage = new TerraformPlanCommand('foo')
196+
Jenkinsfile.original = mock(MockWorkflowScript.class)
197+
def result = stage.withVariableFile('somekey', ['key':'value'])
198+
199+
assertThat(result, equalTo(stage))
200+
}
201+
202+
@Test
203+
void testWithFileAndMap() {
204+
def myKey = 'myKey'
205+
def expectedValue = 'expValue'
206+
def map = [expectedKey:expectedValue]
207+
def original = mock(MockWorkflowScript.class)
208+
Jenkinsfile.original = original
209+
def command = spy(new TerraformPlanCommand("dev")).withVariableFile(myKey, map)
210+
211+
def actualCommand = command.toString()
212+
assertThat(actualCommand, containsString("-var-file=./dev-${myKey}"))
213+
214+
def filename = "dev-${myKey}.tfvars"
215+
verify(command).withVariableFile(filename)
216+
def content = "${myKey}={expectedKey=\"${expectedValue}\"}"
217+
verify(original).writeFile(file: filename.toString(), text: content.toString())
218+
verifyNoMoreInteractions(original)
219+
}
220+
}
221+
170222
@Nested
171223
public class WithVariableMap {
172224
@Test

0 commit comments

Comments
 (0)