|
| 1 | +package com.devshawn.kafka.gitops |
| 2 | + |
| 3 | +import org.junit.ClassRule |
| 4 | +import org.junit.contrib.java.lang.system.EnvironmentVariables |
| 5 | +import org.skyscreamer.jsonassert.JSONAssert |
| 6 | +import picocli.CommandLine |
| 7 | +import spock.lang.Shared |
| 8 | +import spock.lang.Specification |
| 9 | +import spock.lang.Unroll |
| 10 | + |
| 11 | +@Unroll |
| 12 | +class PlanCommandIntegrationSpec extends Specification { |
| 13 | + |
| 14 | + @Shared |
| 15 | + @ClassRule |
| 16 | + EnvironmentVariables environmentVariables |
| 17 | + |
| 18 | + void setupSpec() { |
| 19 | + environmentVariables.set("KAFKA_BOOTSTRAP_SERVERS", "localhost:9092") |
| 20 | + environmentVariables.set("KAFKA_SASL_JAAS_USERNAME", "test") |
| 21 | + environmentVariables.set("KAFKA_SASL_JAAS_PASSWORD", "test-secret") |
| 22 | + environmentVariables.set("KAFKA_SASL_MECHANISM", "PLAIN") |
| 23 | + environmentVariables.set("KAFKA_SECURITY_PROTOCOL", "SASL_PLAINTEXT") |
| 24 | + TestUtils.cleanUpCluster() |
| 25 | + } |
| 26 | + |
| 27 | + void cleanupSpec() { |
| 28 | + TestUtils.cleanUpCluster() |
| 29 | + } |
| 30 | + |
| 31 | + void 'test various valid plans - #planName'() { |
| 32 | + setup: |
| 33 | + String planOutputFile = "/tmp/plan.json" |
| 34 | + String file = TestUtils.getResourceFilePath("plans/${planName}.yaml") |
| 35 | + MainCommand mainCommand = new MainCommand() |
| 36 | + CommandLine cmd = new CommandLine(mainCommand) |
| 37 | + |
| 38 | + when: |
| 39 | + int exitCode = cmd.execute("-f", file, "plan", "-o", planOutputFile) |
| 40 | + |
| 41 | + then: |
| 42 | + exitCode == 0 |
| 43 | + |
| 44 | + when: |
| 45 | + String actualPlan = TestUtils.getFileContent(planOutputFile) |
| 46 | + String expectedPlan = TestUtils.getResourceFileContent("plans/${planName}-plan.json") |
| 47 | + |
| 48 | + then: |
| 49 | + JSONAssert.assertEquals(expectedPlan, actualPlan, true) |
| 50 | + |
| 51 | + where: |
| 52 | + planName << [ |
| 53 | + "simple", |
| 54 | + "application-service", |
| 55 | + "kafka-connect-service", |
| 56 | + "kafka-streams-service", |
| 57 | + "topics-and-services" |
| 58 | + ] |
| 59 | + } |
| 60 | + |
| 61 | + void 'test various valid plans with seed - #planName'() { |
| 62 | + setup: |
| 63 | + TestUtils.cleanUpCluster() |
| 64 | + TestUtils.seedCluster() |
| 65 | + String planOutputFile = "/tmp/plan.json" |
| 66 | + String file = TestUtils.getResourceFilePath("plans/${planName}.yaml") |
| 67 | + MainCommand mainCommand = new MainCommand() |
| 68 | + CommandLine cmd = new CommandLine(mainCommand) |
| 69 | + |
| 70 | + when: |
| 71 | + int exitCode |
| 72 | + if (deleteDisabled) { |
| 73 | + exitCode = cmd.execute("-f", file, "--no-delete", "plan", "-o", planOutputFile) |
| 74 | + } else { |
| 75 | + exitCode = cmd.execute("-f", file, "plan", "-o", planOutputFile) |
| 76 | + } |
| 77 | + |
| 78 | + then: |
| 79 | + exitCode == 0 |
| 80 | + |
| 81 | + when: |
| 82 | + String actualPlan = TestUtils.getFileContent(planOutputFile) |
| 83 | + String expectedPlan = TestUtils.getResourceFileContent("plans/${planName}-plan.json") |
| 84 | + |
| 85 | + then: |
| 86 | + JSONAssert.assertEquals(expectedPlan, actualPlan, true) |
| 87 | + |
| 88 | + where: |
| 89 | + planName | deleteDisabled |
| 90 | + "seed-topic-modification" | false |
| 91 | + "seed-topic-modification-no-delete" | true |
| 92 | + } |
| 93 | + |
| 94 | + void 'test invalid plans - #planName'() { |
| 95 | + setup: |
| 96 | + ByteArrayOutputStream err = new ByteArrayOutputStream() |
| 97 | + ByteArrayOutputStream out = new ByteArrayOutputStream() |
| 98 | + PrintStream oldErr = System.err |
| 99 | + PrintStream oldOut = System.out |
| 100 | + System.setErr(new PrintStream(err)) |
| 101 | + System.setOut(new PrintStream(out)) |
| 102 | + String file = TestUtils.getResourceFilePath("plans/${planName}.yaml") |
| 103 | + MainCommand mainCommand = new MainCommand() |
| 104 | + CommandLine cmd = new CommandLine(mainCommand) |
| 105 | + |
| 106 | + when: |
| 107 | + int exitCode = cmd.execute("-f", file, "plan") |
| 108 | + |
| 109 | + then: |
| 110 | + exitCode == 2 |
| 111 | + out.toString() == TestUtils.getResourceFileContent("plans/${planName}-output.txt") |
| 112 | + |
| 113 | + cleanup: |
| 114 | + System.setErr(oldErr) |
| 115 | + System.setOut(oldOut) |
| 116 | + |
| 117 | + where: |
| 118 | + planName << [ |
| 119 | + "invalid-missing-principal", |
| 120 | + "invalid-topic" |
| 121 | + ] |
| 122 | + } |
| 123 | +} |
0 commit comments