Skip to content

Commit fe5cd98

Browse files
committed
add tests for the Role comment
1 parent 8cbbe0b commit fe5cd98

5 files changed

Lines changed: 169 additions & 56 deletions

File tree

src/main/java/it/aboutbits/postgresql/crd/role/RoleReconciler.java

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public UpdateControl<Role> reconcile(
9393
}
9494

9595
UpdateControl<Role> updateControl;
96+
9697
try {
9798
// Run everything in a single transaction
9899
updateControl = contextFactory.getDSLContext(
@@ -101,7 +102,7 @@ public UpdateControl<Role> reconcile(
101102
// Get the transactional DSL context
102103
var tx = cfg.dsl();
103104

104-
// Create the role if it doesn't exist yet
105+
// Create and return the role if it doesn't exist yet
105106
if (!RoleUtil.roleExists(tx, spec)) {
106107
log.info(
107108
"Creating Role [resource={}/{}]",
@@ -115,7 +116,8 @@ public UpdateControl<Role> reconcile(
115116
password
116117
);
117118

118-
status.setPhase(CRPhase.READY);
119+
status.setPhase(CRPhase.READY)
120+
.setMessage(null);
119121

120122
return UpdateControl.patchStatus(resource);
121123
}
@@ -125,6 +127,7 @@ public UpdateControl<Role> reconcile(
125127
var roleLoginMatches = RoleUtil.roleLoginMatches(tx, spec);
126128
var currentFlags = RoleUtil.fetchCurrentFlags(tx, spec);
127129
var flagsMatch = expectedFlags.equals(currentFlags);
130+
var commentMatches = RoleUtil.roleCommentMatches(tx, spec);
128131

129132
if (loginExpected) {
130133
passwordMatches = PostgreSQLAuthenticationUtil.passwordMatches(
@@ -134,7 +137,7 @@ public UpdateControl<Role> reconcile(
134137
);
135138
}
136139

137-
if (roleLoginMatches && passwordMatches && flagsMatch) {
140+
if (roleLoginMatches && passwordMatches && flagsMatch && commentMatches) {
138141
log.info(
139142
"Role up-to-date [resource={}/{}]",
140143
namespace,
@@ -147,30 +150,41 @@ public UpdateControl<Role> reconcile(
147150
var changePassword = loginExpected && !passwordMatches;
148151

149152
log.info(
150-
"Updating Role flags [resource={}/{}]",
153+
"Updating Role [resource={}/{}]",
151154
namespace,
152155
name
153156
);
154157

155-
RoleUtil.alterRole(
156-
tx,
157-
spec,
158-
changePassword,
159-
password
160-
);
158+
if (!roleLoginMatches || !passwordMatches || !flagsMatch) {
159+
RoleUtil.alterRole(
160+
tx,
161+
spec,
162+
changePassword,
163+
password
164+
);
165+
}
161166

162-
log.info(
163-
"Updating Role membership [resource={}/{}]",
164-
namespace,
165-
name
166-
);
167+
if (!flagsMatch) {
168+
log.info(
169+
"Updating Role membership [resource={}/{}]",
170+
namespace,
171+
name
172+
);
167173

168-
RoleUtil.reconcileRoleMembership(
169-
tx,
170-
spec,
171-
expectedFlags,
172-
currentFlags
173-
);
174+
RoleUtil.reconcileRoleMembership(
175+
tx,
176+
spec,
177+
expectedFlags,
178+
currentFlags
179+
);
180+
}
181+
182+
if (!commentMatches) {
183+
RoleUtil.updateComment(
184+
tx,
185+
spec
186+
);
187+
}
174188

175189
status.setPhase(CRPhase.READY)
176190
.setMessage(null);
@@ -188,6 +202,9 @@ public UpdateControl<Role> reconcile(
188202
return updateControl;
189203
}
190204

205+
/**
206+
* Watches for {@code Secret} changes to trigger reconciliation for dependent {@code Role} resources.
207+
*/
191208
@Override
192209
public List<EventSource<?, Role>> prepareEventSources(EventSourceContext<Role> context) {
193210
// 1. Define the Mapper

src/main/java/it/aboutbits/postgresql/crd/role/RoleSpec.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ public class RoleSpec {
2020
@Required
2121
private String name = "";
2222

23-
private String comment = "";
23+
@Nullable
24+
@io.fabric8.generator.annotation.Nullable
25+
private String comment;
2426

2527
@Required
2628
private ClusterReference clusterRef = new ClusterReference();

src/main/java/it/aboutbits/postgresql/crd/role/RoleUtil.java

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public static void createRole(
6060
);
6161

6262
// Optional comment
63-
if (!comment.isBlank()) {
63+
if (comment != null && !comment.isBlank()) {
6464
tx.execute(
6565
buildCommentOnRole(roleName, comment)
6666
);
@@ -75,7 +75,6 @@ public static void alterRole(
7575
) {
7676
var roleName = spec.getName();
7777
var flags = spec.getFlags();
78-
var expectedComment = normalizeComment(spec.getComment());
7978

8079
tx.execute(
8180
buildAlterRole(
@@ -85,6 +84,14 @@ public static void alterRole(
8584
password
8685
)
8786
);
87+
}
88+
89+
public static void updateComment(
90+
DSLContext tx,
91+
RoleSpec spec
92+
) {
93+
var roleName = spec.getName();
94+
var expectedComment = normalizeComment(spec.getComment());
8895

8996
var currentComment = normalizeComment(
9097
fetchCurrentRoleComment(tx, roleName)
@@ -97,6 +104,34 @@ public static void alterRole(
97104
}
98105
}
99106

107+
public static boolean roleCommentMatches(
108+
DSLContext tx,
109+
RoleSpec spec
110+
) {
111+
var expectedComment = spec.getComment();
112+
113+
var currentComment = normalizeComment(
114+
fetchCurrentRoleComment(tx, spec.getName())
115+
);
116+
117+
return Objects.equals(currentComment, expectedComment);
118+
}
119+
120+
public static @Nullable String fetchCurrentRoleComment(
121+
DSLContext tx,
122+
String roleName
123+
) {
124+
125+
return tx
126+
.select(Routines.shobjDescription(
127+
PG_AUTHID.OID,
128+
val(PG_AUTHID.getUnqualifiedName().last())
129+
))
130+
.from(PG_AUTHID)
131+
.where(PG_AUTHID.ROLNAME.eq(roleName))
132+
.fetchOneInto(String.class);
133+
}
134+
100135
public static boolean roleLoginMatches(
101136
DSLContext tx,
102137
RoleSpec spec
@@ -377,21 +412,6 @@ private static Query buildCommentOnRole(
377412
);
378413
}
379414

380-
private static @Nullable String fetchCurrentRoleComment(
381-
DSLContext tx,
382-
String roleName
383-
) {
384-
385-
return tx
386-
.select(Routines.shobjDescription(
387-
PG_AUTHID.OID,
388-
val(PG_AUTHID.getUnqualifiedName().last())
389-
))
390-
.from(PG_AUTHID)
391-
.where(PG_AUTHID.ROLNAME.eq(roleName))
392-
.fetchSingleInto(String.class);
393-
}
394-
395415
private static @Nullable String normalizeComment(@Nullable String comment) {
396416
if (comment == null || comment.isBlank()) {
397417
return null;

src/test/java/it/aboutbits/postgresql/_support/testdata/persisted/creator/ClusterConnectionCreate.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import it.aboutbits.postgresql.crd.connection.ClusterConnection;
99
import it.aboutbits.postgresql.crd.connection.ClusterConnectionSpec;
1010
import lombok.AccessLevel;
11-
import lombok.Builder;
1211
import lombok.Getter;
1312
import lombok.Setter;
1413
import lombok.experimental.Accessors;

0 commit comments

Comments
 (0)