Skip to content

Commit a9fd87b

Browse files
authored
Merge pull request #1912 from wind57/wait_for_service_to_be_deleted
wait for services to be deleted in integration tests
2 parents 619a1ad + 7612ac3 commit a9fd87b

File tree

2 files changed

+36
-4
lines changed
  • spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons

2 files changed

+36
-4
lines changed

spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/fabric8_client/Util.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ else if (phase.equals(Phase.DELETE)) {
132132
}
133133
}
134134

135-
public void deleteAndWait(String namespace, @Nullable Deployment deployment, Service service) {
135+
public void deleteAndWait(String namespace, @Nullable Deployment deployment, @Nullable Service service) {
136136
try {
137137

138138
long startTime = System.currentTimeMillis();
@@ -150,7 +150,10 @@ public void deleteAndWait(String namespace, @Nullable Deployment deployment, Ser
150150
}
151151
System.out.println("Ended deployment delete in " + (System.currentTimeMillis() - startTime) + "ms");
152152

153-
client.services().inNamespace(namespace).resource(service).delete();
153+
if (service != null) {
154+
client.services().inNamespace(namespace).resource(service).delete();
155+
waitForServiceToBeDeleted(namespace, service);
156+
}
154157

155158
}
156159
catch (Exception e) {
@@ -363,6 +366,16 @@ private void waitForDeploymentToBeDeleted(String namespace, Deployment deploymen
363366
});
364367
}
365368

369+
private void waitForServiceToBeDeleted(String namespace, Service service) {
370+
371+
String serviceName = service.getMetadata().getName();
372+
373+
await().pollInterval(Duration.ofSeconds(1)).atMost(30, TimeUnit.SECONDS).until(() -> {
374+
Service inner = client.services().inNamespace(namespace).withName(serviceName).get();
375+
return inner == null;
376+
});
377+
}
378+
366379
private void waitForDeployment(String namespace, Deployment deployment) {
367380
String deploymentName = deploymentName(deployment);
368381
await().pollInterval(Duration.ofSeconds(2))

spring-cloud-kubernetes-test-support/src/main/java/org/springframework/cloud/kubernetes/integration/tests/commons/native_client/Util.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,13 @@ public void deleteAndWait(String namespace, V1Deployment deployment, V1Service s
209209
waitForDeploymentToBeDeleted(deploymentName, namespace);
210210
waitForDeploymentPodsToBeDeleted(podLabels, namespace);
211211

212-
service.getMetadata().setNamespace(namespace);
213-
coreV1Api.deleteNamespacedService(service.getMetadata().getName(), service.getMetadata().getNamespace(),
212+
if (service != null) {
213+
service.getMetadata().setNamespace(namespace);
214+
coreV1Api.deleteNamespacedService(service.getMetadata().getName(), service.getMetadata().getNamespace(),
214215
null, null, null, null, null, null);
216+
waitForServiceToBeDeleted(service.getMetadata().getName(), namespace);
217+
}
218+
215219
}
216220
catch (Exception e) {
217221
throw new RuntimeException(e);
@@ -507,6 +511,21 @@ private void waitForDeploymentToBeDeleted(String deploymentName, String namespac
507511
});
508512
}
509513

514+
private void waitForServiceToBeDeleted(String serviceName, String namespace) {
515+
await().timeout(Duration.ofSeconds(180)).until(() -> {
516+
try {
517+
coreV1Api.readNamespacedService(serviceName, namespace, null);
518+
return false;
519+
}
520+
catch (ApiException e) {
521+
if (e.getCode() == HttpURLConnection.HTTP_NOT_FOUND) {
522+
return true;
523+
}
524+
throw new RuntimeException(e);
525+
}
526+
});
527+
}
528+
510529
private void waitForDeploymentPodsToBeDeleted(Map<String, String> labels, String namespace) {
511530
await().timeout(Duration.ofSeconds(180)).until(() -> {
512531
try {

0 commit comments

Comments
 (0)