Skip to content

Commit a121886

Browse files
committed
fix flaky tests and properly clean up resources
1 parent a03b951 commit a121886

19 files changed

Lines changed: 402 additions & 127 deletions

File tree

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
build-and-release:
2626
needs: test
2727
runs-on: ubuntu-24.04
28-
timeout-minutes: 15
28+
timeout-minutes: 5
2929
steps:
3030
- uses: actions/checkout@v6
3131
with:

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
test:
88
name: Tests (PostgreSQL ${{ matrix.postgres-version }})
99
runs-on: ubuntu-24.04
10-
timeout-minutes: 5
10+
timeout-minutes: 10
1111
strategy:
1212
fail-fast: false
1313
matrix:

operator/src/main/java/it/aboutbits/postgresql/crd/defaultprivilege/DefaultPrivilegeReconciler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,10 @@ public DeleteControl cleanup(
164164
.rescheduleAfter(60, TimeUnit.SECONDS);
165165
}
166166

167+
var database = spec.getDatabase();
167168
var clusterConnection = clusterConnectionOptional.get();
168169

169-
try (var dsl = contextFactory.getDSLContext(clusterConnection)) {
170+
try (var dsl = contextFactory.getDSLContext(clusterConnection, database)) {
170171
dsl.transaction(cfg -> {
171172
var tx = cfg.dsl();
172173

operator/src/main/java/it/aboutbits/postgresql/crd/defaultprivilege/DefaultPrivilegeService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public Set<Privilege> determineCurrentDefaultPrivileges(
4141
DSLContext tx,
4242
DefaultPrivilegeSpec spec
4343
) {
44+
var owner = spec.getOwner();
4445
var role = spec.getRole();
4546
var schema = spec.getSchema();
4647

@@ -65,6 +66,11 @@ public Set<Privilege> determineCurrentDefaultPrivileges(
6566
.from(PG_DEFAULT_ACL)
6667
.crossJoin(Routines.aclexplode(PG_DEFAULT_ACL.DEFACLACL))
6768
.where(
69+
PG_DEFAULT_ACL.DEFACLROLE.eq(field(
70+
ROLE_OID_SQL,
71+
OID_DATA_TYPE,
72+
val(owner)
73+
)),
6874
PG_DEFAULT_ACL.DEFACLOBJTYPE.eq(objectType.objectTypeChar()),
6975
objectType == SCHEMA
7076
? noCondition()

operator/src/main/java/it/aboutbits/postgresql/crd/grant/GrantReconciler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ protected CRStatus newStatus() {
213213
return new CRStatus();
214214
}
215215

216-
@SuppressWarnings("java:S3776")
216+
@SuppressWarnings({"checkstyle:MethodLength", "java:S3776"})
217217
private UpdateControl<Grant> reconcileInTransaction(
218218
DSLContext tx,
219219
Grant resource,

operator/src/test/java/it/aboutbits/postgresql/PostgreSQLInstanceReadinessCheckTest.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.util.concurrent.TimeUnit;
1616

1717
import static org.assertj.core.api.Assertions.assertThat;
18+
import static org.awaitility.Awaitility.await;
1819

1920
@NullMarked
2021
@QuarkusTest
@@ -30,10 +31,14 @@ class PostgreSQLInstanceReadinessCheckTest {
3031
KubernetesClient kubernetesClient;
3132

3233
@BeforeEach
33-
void cleanUp() {
34-
kubernetesClient.resources(ClusterConnection.class)
35-
.withTimeout(5, TimeUnit.SECONDS)
36-
.delete();
34+
void resetEnvironment() {
35+
kubernetesClient.resources(ClusterConnection.class).delete();
36+
37+
await().atMost(5, TimeUnit.SECONDS)
38+
.pollInterval(100, TimeUnit.MILLISECONDS)
39+
.until(() ->
40+
kubernetesClient.resources(ClusterConnection.class).list().getItems().isEmpty()
41+
);
3742
}
3843

3944
@Test

operator/src/test/java/it/aboutbits/postgresql/_support/testdata/base/TestDataCreator.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ public static String randomKubernetesNameSuffix(String name) {
6262
}
6363

6464
var separator = "-";
65-
var suffixLength = maxLength - name.length() - separator.length();
65+
var availableSpace = maxLength - name.length() - separator.length();
66+
var suffixLength = Math.min(7, availableSpace);
6667

6768
return name + separator + FAKER.regexify("[a-z0-9]{%d}".formatted(suffixLength));
6869
}

operator/src/test/java/it/aboutbits/postgresql/_support/testdata/persisted/Given.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,15 @@ public RoleCreate role() {
9999
public DatabaseCreate database() {
100100
return new DatabaseCreate(
101101
numberOfItems,
102+
given,
102103
kubernetesClient
103104
);
104105
}
105106

106107
public SchemaCreate schema() {
107108
return new SchemaCreate(
108109
numberOfItems,
110+
given,
109111
kubernetesClient
110112
);
111113
}

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder;
44
import io.fabric8.kubernetes.client.KubernetesClient;
55
import it.aboutbits.postgresql._support.testdata.base.TestDataCreator;
6+
import it.aboutbits.postgresql._support.testdata.persisted.Given;
67
import it.aboutbits.postgresql.core.ClusterReference;
78
import it.aboutbits.postgresql.core.ReclaimPolicy;
89
import it.aboutbits.postgresql.crd.database.Database;
@@ -13,13 +14,14 @@
1314
import org.jspecify.annotations.NullMarked;
1415
import org.jspecify.annotations.Nullable;
1516

16-
import java.util.Objects;
1717
import java.util.concurrent.TimeUnit;
1818

1919
@NullMarked
2020
@Setter
2121
@Accessors(fluent = true, chain = true)
2222
public class DatabaseCreate extends TestDataCreator<Database> {
23+
private final Given given;
24+
2325
private final KubernetesClient kubernetesClient;
2426

2527
@Nullable
@@ -43,9 +45,11 @@ public class DatabaseCreate extends TestDataCreator<Database> {
4345

4446
public DatabaseCreate(
4547
int numberOfItems,
48+
Given given,
4649
KubernetesClient kubernetesClient
4750
) {
4851
super(numberOfItems);
52+
this.given = given;
4953
this.kubernetesClient = kubernetesClient;
5054
}
5155

@@ -118,9 +122,16 @@ private String getName() {
118122
}
119123

120124
private String getClusterConnectionName() {
121-
return Objects.requireNonNullElse(
122-
withClusterConnectionName,
123-
"test-cluster-connection"
124-
);
125+
if (withClusterConnectionName != null) {
126+
return withClusterConnectionName;
127+
}
128+
129+
var clusterConnection = given.one()
130+
.clusterConnection()
131+
.returnFirst();
132+
133+
withClusterConnectionNamespace = clusterConnection.getMetadata().getNamespace();
134+
135+
return clusterConnection.getMetadata().getName();
125136
}
126137
}

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

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717

1818
import java.util.ArrayList;
1919
import java.util.List;
20-
import java.util.Objects;
2120
import java.util.concurrent.TimeUnit;
2221

22+
import static it.aboutbits.postgresql.core.ReclaimPolicy.DELETE;
23+
2324
@NullMarked
2425
@Setter
2526
@Accessors(fluent = true, chain = true)
@@ -161,57 +162,89 @@ private String getName() {
161162
}
162163

163164
private String getClusterConnectionName() {
164-
return Objects.requireNonNullElse(
165-
withClusterConnectionName,
166-
"test-cluster-connection"
167-
);
165+
if (withClusterConnectionName != null) {
166+
return withClusterConnectionName;
167+
}
168+
169+
var clusterConnection = given.one()
170+
.clusterConnection()
171+
.returnFirst();
172+
173+
withClusterConnectionNamespace = clusterConnection.getMetadata().getNamespace();
174+
175+
return clusterConnection.getMetadata().getName();
168176
}
169177

170178
private String getDatabase() {
171179
if (withDatabase != null) {
172180
return withDatabase;
173181
}
174182

175-
return given.one()
183+
var item = given.one()
176184
.database()
177-
.returnFirst()
178-
.getSpec()
179-
.getName();
185+
.withClusterConnectionName(getClusterConnectionName())
186+
.withClusterConnectionNamespace(withClusterConnectionNamespace)
187+
.withReclaimPolicy(DELETE)
188+
.returnFirst();
189+
190+
withDatabase = item.getSpec().getName();
191+
192+
return withDatabase;
180193
}
181194

182195
private String getRole() {
183196
if (withRole != null) {
184197
return withRole;
185198
}
186199

187-
return given.one()
200+
var item = given.one()
188201
.role()
189-
.returnFirst()
190-
.getSpec()
191-
.getName();
202+
.withClusterConnectionName(getClusterConnectionName())
203+
.withClusterConnectionNamespace(withClusterConnectionNamespace)
204+
.returnFirst();
205+
206+
withRole = item.getSpec().getName();
207+
208+
return withRole;
192209
}
193210

194211
private String getOwner() {
195212
if (withOwner != null) {
196213
return withOwner;
197214
}
198215

199-
return given.one()
216+
var item = given.one()
200217
.role()
201-
.returnFirst()
202-
.getSpec()
203-
.getName();
218+
.withClusterConnectionName(getClusterConnectionName())
219+
.withClusterConnectionNamespace(withClusterConnectionNamespace)
220+
.returnFirst();
221+
222+
withOwner = item.getSpec().getName();
223+
224+
return withOwner;
204225
}
205226

206227
private String getSchema() {
207228
if (withSchema != null) {
208229
return withSchema;
209230
}
210231

211-
return given.one()
232+
var clusterConnectionDb = given.one()
233+
.clusterConnection()
234+
.withName(getClusterConnectionName() + "-db")
235+
.withNamespace(withClusterConnectionNamespace)
236+
.withDatabase(getDatabase())
237+
.returnFirst();
238+
239+
var item = given.one()
212240
.schema()
213-
.returnFirst()
214-
.getSpec()
215-
.getName();
241+
.withClusterConnectionName(clusterConnectionDb.getMetadata().getName())
242+
.withClusterConnectionNamespace(clusterConnectionDb.getMetadata().getNamespace())
243+
.withReclaimPolicy(DELETE)
244+
.returnFirst();
245+
246+
withSchema = item.getSpec().getName();
247+
248+
return withSchema;
216249
}
217250
}

0 commit comments

Comments
 (0)