Skip to content

Commit

Permalink
Fix fabric8io#2672: WaitUntilReady for Service resource throws Illega…
Browse files Browse the repository at this point in the history
…lArgumentException

Use `waitUntilCondition(Objects::nonNull, amount, timeUnit)` instead of
waitUntilReady for `Service` resource
  • Loading branch information
rohanKanojia committed Feb 8, 2021
1 parent edd9b94 commit 60bed35
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#### Bugs
* Fix #2748: Pass custom headers in kubernetes-client to watch api by modify WatchConnectionManager
* Fix #2672: WaitUntilReady for Service resource throws IllegalArgumentException

#### Improvements
* Fix #2717: Remove edit() methods from RawCustomResourceOperationsImpl taking InputStream arguments
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public Service patch(Service item) {
@Override
public Service waitUntilReady(long amount, TimeUnit timeUnit) throws InterruptedException {
long started = System.nanoTime();
super.waitUntilReady(amount, timeUnit);
super.waitUntilCondition(Objects::nonNull, amount, timeUnit);
long alreadySpent = System.nanoTime() - started;

// if awaiting existence took very long, let's give at least 10 seconds to awaiting readiness
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.arquillian.cube.kubernetes.api.Session;
import org.arquillian.cube.kubernetes.impl.requirement.RequiresKubernetes;
import org.arquillian.cube.requirement.ArquillianConditionalRunner;
import org.assertj.core.internal.bytebuddy.build.ToStringPlugin;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.junit.AfterClass;
import org.junit.BeforeClass;
Expand Down Expand Up @@ -225,6 +224,22 @@ public void testExternalNameServiceCreateOrReplace() {
assertEquals("his.database.example.com", service.getSpec().getExternalName());
}

@Test
public void waitUntilReady() throws InterruptedException {
// Given
String svcName = "service-wait-until-ready";

// When
Service service = client.services().inNamespace(session.getNamespace())
.withName(svcName)
.waitUntilReady(10, TimeUnit.SECONDS);

// Then
assertNotNull(service);
assertEquals(svcName, service.getMetadata().getName());
assertTrue(client.pods().inNamespace(session.getNamespace()).withName(svcName).delete());
}

@AfterClass
public static void cleanup() {
ClusterEntity.remove(ServiceIT.class.getResourceAsStream("/service-it.yml"));
Expand Down
29 changes: 29 additions & 0 deletions kubernetes-itests/src/test/resources/service-it.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,32 @@ spec:
protocol: TCP
port: 443
targetPort: 9377
---
apiVersion: v1
kind: Service
metadata:
name: service-wait-until-ready
spec:
selector:
app: service-wait-until-ready
ports:
- name: http
protocol: TCP
port: 8080
targetPort: 8080
---
apiVersion: v1
kind: Pod
metadata:
name: service-wait-until-ready
labels:
app: service-wait-until-ready
spec:
containers:
- name: busybox
image: busybox
command: ["sleep", "36000"]
ports:
- containerPort: 8080
name: http
protocol: TCP
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/
package io.fabric8.kubernetes.client.mock;

import io.fabric8.kubernetes.api.model.EndpointAddressBuilder;
import io.fabric8.kubernetes.api.model.EndpointPortBuilder;
import io.fabric8.kubernetes.api.model.EndpointSubsetBuilder;
import io.fabric8.kubernetes.api.model.EndpointsBuilder;
import io.fabric8.kubernetes.api.model.IntOrString;
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.ServiceBuilder;
Expand All @@ -29,6 +33,7 @@

import java.net.HttpURLConnection;
import java.util.Collections;
import java.util.concurrent.TimeUnit;

import static junit.framework.TestCase.assertNotNull;
import static junit.framework.TestCase.assertTrue;
Expand All @@ -41,6 +46,8 @@ class ServiceTest {

Service service;

private KubernetesClient client;

@BeforeEach
void prepareService() {
service = new ServiceBuilder()
Expand All @@ -57,11 +64,11 @@ void prepareService() {
.addToSelector("deploymentconfig", "httpbin")
.endSpec()
.build();
client = server.getClient();
}

@Test
void testLoad() {
KubernetesClient client = server.getClient();
Service svc = client.services().load(getClass().getResourceAsStream("/test-service.yml")).get();
assertNotNull(svc);
assertEquals("httpbin", svc.getMetadata().getName());
Expand All @@ -74,7 +81,6 @@ void testCreate() {
.andReturn(200, service)
.once();

KubernetesClient client = server.getClient();
Service responseSvc = client.services().inNamespace("test").create(service);
assertNotNull(responseSvc);
assertEquals("httpbin", responseSvc.getMetadata().getName());
Expand Down Expand Up @@ -104,7 +110,6 @@ void testReplace() throws InterruptedException {
.andReturn(HttpURLConnection.HTTP_OK, serviceUpdated)
.once();

KubernetesClient client = server.getClient();
Service responseSvc = client.services().inNamespace("test").createOrReplace(serviceUpdated);
assertNotNull(responseSvc);
assertEquals("httpbin", responseSvc.getMetadata().getName());
Expand All @@ -122,7 +127,6 @@ void testDelete() {
.andReturn(200, service)
.once();

KubernetesClient client = server.getClient();
boolean isDeleted = client.services().inNamespace("test").withName("httpbin").delete();
assertTrue(isDeleted);
}
Expand All @@ -142,7 +146,6 @@ void testUpdate() {
.andReturn(200, serviceFromServer)
.once();

KubernetesClient client = server.getClient();
Service responseFromServer = client.services().inNamespace("test").withName("httpbin").edit(s -> new ServiceBuilder(s)
.editOrNewMetadata().addToLabels("foo", "bar").endMetadata()
.build());
Expand Down Expand Up @@ -171,12 +174,38 @@ void testGetUrlFromClusterIPService() {
.andReturn(HttpURLConnection.HTTP_OK, service).always();
server.expect().get().withPath("/apis/extensions/v1beta1/namespaces/ns1/ingresses")
.andReturn(HttpURLConnection.HTTP_OK, new IngressListBuilder().build()).always();
KubernetesClient client = server.getClient();

// When
String url = client.services().inNamespace("ns1").withName("svc1").getURL("http");

// Then
assertEquals("tcp://187.30.14.32:8080", url);
}

@Test
void testWaitUntilReady() throws InterruptedException {
// Given
Service svc1 = new ServiceBuilder().withNewMetadata().withName("svc1").endMetadata().build();
server.expect().get().withPath("/api/v1/namespaces/ns1/endpoints/svc1")
.andReturn(HttpURLConnection.HTTP_OK, new EndpointsBuilder()
.withNewMetadata().withName("svc1").endMetadata()
.addToSubsets(new EndpointSubsetBuilder()
.addToAddresses(new EndpointAddressBuilder().withIp("192.168.64.13").build())
.addToPorts(new EndpointPortBuilder().withPort(8443).build())
.build())
.build())
.once();
server.expect().get().withPath("/api/v1/namespaces/ns1/services/svc1")
.andReturn(HttpURLConnection.HTTP_OK, svc1)
.times(2);

// When
Service service = client.services()
.inNamespace("ns1")
.withName("svc1")
.waitUntilReady(60, TimeUnit.SECONDS);

// Then
assertNotNull(service);
}
}

0 comments on commit 60bed35

Please sign in to comment.