Skip to content

Commit 1aca948

Browse files
authored
fix: apply should not fail on no changes (#29)
1 parent e7b18aa commit 1aca948

File tree

6 files changed

+99
-2
lines changed

6 files changed

+99
-2
lines changed

src/main/java/com/devshawn/kafka/gitops/StateManager.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ public DesiredStateFile getAndValidateStateFile() {
7171

7272
public DesiredPlan plan() {
7373
DesiredPlan desiredPlan = generatePlan();
74-
planManager.validatePlanHasChanges(desiredPlan, managerConfig.isDeleteDisabled());
7574
planManager.writePlanToFile(desiredPlan);
75+
planManager.validatePlanHasChanges(desiredPlan, managerConfig.isDeleteDisabled());
7676
return desiredPlan;
7777
}
7878

src/main/java/com/devshawn/kafka/gitops/cli/ApplyCommand.java

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.devshawn.kafka.gitops.domain.plan.DesiredPlan;
77
import com.devshawn.kafka.gitops.exception.KafkaExecutionException;
88
import com.devshawn.kafka.gitops.exception.MissingConfigurationException;
9+
import com.devshawn.kafka.gitops.exception.PlanIsUpToDateException;
910
import com.devshawn.kafka.gitops.exception.ReadPlanInputException;
1011
import com.devshawn.kafka.gitops.exception.ValidationException;
1112
import com.devshawn.kafka.gitops.service.ParserService;
@@ -35,6 +36,9 @@ public Integer call() {
3536
DesiredPlan desiredPlan = stateManager.apply();
3637
LogUtil.printApplyOverview(PlanUtil.getOverview(desiredPlan, parent.isDeleteDisabled()));
3738
return 0;
39+
} catch (PlanIsUpToDateException ex) {
40+
LogUtil.printNoChangesMessage();
41+
return 0;
3842
} catch (MissingConfigurationException | ReadPlanInputException ex) {
3943
LogUtil.printGenericError(ex, true);
4044
} catch (ValidationException ex) {

src/test/groovy/com/devshawn/kafka/gitops/ApplyCommandIntegrationSpec.groovy

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class ApplyCommandIntegrationSpec extends Specification {
9898
"seed-topic-modification-3" | true
9999
"seed-topic-modification-3" | false
100100
"seed-acl-exists" | false
101+
"no-changes" | false
101102
}
102103

103104
void 'test reading missing file throws ReadPlanInputException'() {

src/test/groovy/com/devshawn/kafka/gitops/PlanCommandIntegrationSpec.groovy

+9-1
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,25 @@ class PlanCommandIntegrationSpec extends Specification {
187187
ByteArrayOutputStream out = new ByteArrayOutputStream()
188188
PrintStream oldOut = System.out
189189
System.setOut(new PrintStream(out))
190+
String planOutputFile = "/tmp/plan.json"
190191
String file = TestUtils.getResourceFilePath("plans/${planName}.yaml")
191192
MainCommand mainCommand = new MainCommand()
192193
CommandLine cmd = new CommandLine(mainCommand)
193194

194195
when:
195-
int exitCode = cmd.execute("-f", file, "plan")
196+
int exitCode = cmd.execute("-f", file, "plan", "-o", planOutputFile)
196197

197198
then:
198199
exitCode == 0
199200
out.toString() == TestUtils.getResourceFileContent("plans/no-changes-output.txt")
200201

202+
when:
203+
String actualPlan = TestUtils.getFileContent(planOutputFile)
204+
String expectedPlan = TestUtils.getResourceFileContent("plans/${planName}-plan.json")
205+
206+
then:
207+
JSONAssert.assertEquals(expectedPlan, actualPlan, true)
208+
201209
cleanup:
202210
System.setOut(oldOut)
203211

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Executing apply...
2+
3+
[SUCCESS] There are no necessary changes; the actual state matches the desired state.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{
2+
"topicPlans": [
3+
{
4+
"name": "delete-topic",
5+
"action": "NO_CHANGE",
6+
"topicDetails": {
7+
"partitions": 1,
8+
"replication": 1,
9+
"configs": {}
10+
},
11+
"topicConfigPlans": []
12+
},
13+
{
14+
"name": "test-topic",
15+
"action": "NO_CHANGE",
16+
"topicDetails": {
17+
"partitions": 1,
18+
"replication": 1,
19+
"configs": {}
20+
},
21+
"topicConfigPlans": []
22+
},
23+
{
24+
"name": "topic-with-configs-1",
25+
"action": "NO_CHANGE",
26+
"topicDetails": {
27+
"partitions": 3,
28+
"replication": 1,
29+
"configs": {
30+
"cleanup.policy": "compact",
31+
"segment.bytes": "100000"
32+
}
33+
},
34+
"topicConfigPlans": [
35+
{
36+
"key": "cleanup.policy",
37+
"value": "compact",
38+
"action": "NO_CHANGE"
39+
},
40+
{
41+
"key": "segment.bytes",
42+
"value": "100000",
43+
"action": "NO_CHANGE"
44+
}
45+
]
46+
},
47+
{
48+
"name": "topic-with-configs-2",
49+
"action": "NO_CHANGE",
50+
"topicDetails": {
51+
"partitions": 6,
52+
"replication": 1,
53+
"configs": {
54+
"retention.ms": "60000"
55+
}
56+
},
57+
"topicConfigPlans": [
58+
{
59+
"key": "retention.ms",
60+
"value": "60000",
61+
"action": "NO_CHANGE"
62+
}
63+
]
64+
}
65+
],
66+
"aclPlans": [
67+
{
68+
"name": "test-service-0",
69+
"aclDetails": {
70+
"name": "test-topic",
71+
"type": "TOPIC",
72+
"pattern": "LITERAL",
73+
"principal": "User:test",
74+
"host": "*",
75+
"operation": "READ",
76+
"permission": "ALLOW"
77+
},
78+
"action": "NO_CHANGE"
79+
}
80+
]
81+
}

0 commit comments

Comments
 (0)