|
| 1 | +import static org.hamcrest.Matchers.containsString |
| 2 | +import static org.hamcrest.Matchers.hasItem |
| 3 | +import static org.hamcrest.Matchers.instanceOf |
| 4 | +import static org.junit.Assert.assertThat |
| 5 | +import static org.junit.Assert.assertTrue |
| 6 | +import static org.mockito.Mockito.spy; |
| 7 | +import static org.mockito.Mockito.verify; |
| 8 | +import static org.mockito.Mockito.any; |
| 9 | +import static org.mockito.Mockito.eq; |
| 10 | +import static org.mockito.Mockito.times; |
| 11 | +import static org.mockito.Mockito.when; |
| 12 | +import static org.mockito.Mockito.mock; |
| 13 | +import org.junit.Test |
| 14 | +import org.junit.Before |
| 15 | +import org.junit.After |
| 16 | +import org.junit.runner.RunWith |
| 17 | +import de.bechte.junit.runners.context.HierarchicalContextRunner |
| 18 | + |
| 19 | +@RunWith(HierarchicalContextRunner.class) |
| 20 | +class PassPlanFilePluginTest { |
| 21 | + @Before |
| 22 | + void resetJenkinsEnv() { |
| 23 | + Jenkinsfile.instance = mock(Jenkinsfile.class) |
| 24 | + when(Jenkinsfile.instance.getEnv()).thenReturn([:]) |
| 25 | + } |
| 26 | + |
| 27 | + private configureJenkins(Map config = [:]) { |
| 28 | + Jenkinsfile.instance = mock(Jenkinsfile.class) |
| 29 | + when(Jenkinsfile.instance.getEnv()).thenReturn(config.env ?: [:]) |
| 30 | + } |
| 31 | + |
| 32 | + public class Init { |
| 33 | + @After |
| 34 | + void resetPlugins() { |
| 35 | + TerraformPlanCommand.resetPlugins() |
| 36 | + TerraformApplyCommand.resetPlugins() |
| 37 | + TerraformEnvironmentStage.reset() |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void modifiesTerraformEnvironmentStageCommand() { |
| 42 | + PassPlanFilePlugin.init() |
| 43 | + |
| 44 | + Collection actualPlugins = TerraformEnvironmentStage.getPlugins() |
| 45 | + assertThat(actualPlugins, hasItem(instanceOf(PassPlanFilePlugin.class))) |
| 46 | + } |
| 47 | + |
| 48 | + @Test |
| 49 | + void modifiesTerraformPlanCommand() { |
| 50 | + PassPlanFilePlugin.init() |
| 51 | + |
| 52 | + Collection actualPlugins = TerraformPlanCommand.getPlugins() |
| 53 | + assertThat(actualPlugins, hasItem(instanceOf(PassPlanFilePlugin.class))) |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + void modifiesTerraformApplyCommand() { |
| 58 | + PassPlanFilePlugin.init() |
| 59 | + |
| 60 | + Collection actualPlugins = TerraformApplyCommand.getPlugins() |
| 61 | + assertThat(actualPlugins, hasItem(instanceOf(PassPlanFilePlugin.class))) |
| 62 | + } |
| 63 | + |
| 64 | + } |
| 65 | + |
| 66 | + public class Apply { |
| 67 | + |
| 68 | + @Test |
| 69 | + void decoratesTheTerraformEnvironmentStage() { |
| 70 | + PassPlanFilePlugin plugin = new PassPlanFilePlugin() |
| 71 | + def environment = spy(new TerraformEnvironmentStage()) |
| 72 | + plugin.apply(environment) |
| 73 | + |
| 74 | + verify(environment, times(1)).decorate(eq(TerraformEnvironmentStage.PLAN), any(Closure.class)) |
| 75 | + verify(environment, times(1)).decorate(eq(TerraformEnvironmentStage.APPLY), any(Closure.class)) |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void addsArgumentToTerraformPlan() { |
| 80 | + PassPlanFilePlugin plugin = new PassPlanFilePlugin() |
| 81 | + TerraformPlanCommand command = new TerraformPlanCommand("dev") |
| 82 | + plugin.apply(command) |
| 83 | + |
| 84 | + String result = command.toString() |
| 85 | + assertThat(result, containsString("-out=tfplan-dev")) |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void addsArgumentToTerraformApply() { |
| 90 | + PassPlanFilePlugin plugin = new PassPlanFilePlugin() |
| 91 | + TerraformApplyCommand command = new TerraformApplyCommand("dev") |
| 92 | + plugin.apply(command) |
| 93 | + |
| 94 | + String result = command.toString() |
| 95 | + assertThat(result, containsString("tfplan-dev")) |
| 96 | + } |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | + public class StashPlan { |
| 101 | + |
| 102 | + @Test |
| 103 | + void runsStashPlan() { |
| 104 | + def wasCalled = false |
| 105 | + def passedClosure = { -> wasCalled = true } |
| 106 | + def plugin = new PassPlanFilePlugin() |
| 107 | + |
| 108 | + def stashClosure = plugin.stashPlan('dev') |
| 109 | + stashClosure.delegate = new DummyJenkinsfile() |
| 110 | + stashClosure.call(passedClosure) |
| 111 | + |
| 112 | + assertTrue(wasCalled) |
| 113 | + } |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | + public class UnstashPlan { |
| 118 | + |
| 119 | + @Test |
| 120 | + void runsUnstashPlan() { |
| 121 | + def wasCalled = false |
| 122 | + def passedClosure = { -> wasCalled = true } |
| 123 | + def plugin = new PassPlanFilePlugin() |
| 124 | + |
| 125 | + def unstashClosure = plugin.unstashPlan('dev') |
| 126 | + unstashClosure.delegate = new DummyJenkinsfile() |
| 127 | + unstashClosure.call(passedClosure) |
| 128 | + |
| 129 | + assertTrue(wasCalled) |
| 130 | + } |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | +} |
0 commit comments