Search in sources :

Example 1 with JSON

use of io.kubernetes.client.openapi.JSON in project java by kubernetes-client.

the class DeploymentHelperTest method testGetAllReplicaSetsShouldWork.

@Test
public void testGetAllReplicaSetsShouldWork() throws IOException, ApiException {
    wireMockRule.stubFor(get(urlPathEqualTo("/apis/apps/v1/namespaces/default/replicasets")).willReturn(aResponse().withStatus(200).withBody(new String(Files.readAllBytes(Paths.get(REPLICASET_LIST))))));
    AppsV1Api api = new AppsV1Api(this.apiClient);
    V1Deployment deployment = new JSON().deserialize(new String(Files.readAllBytes(Paths.get(DEPLOYMENT))), V1Deployment.class);
    List<V1ReplicaSet> oldRSes = new ArrayList<>();
    List<V1ReplicaSet> allOldRSes = new ArrayList<>();
    V1ReplicaSet newRs = DeploymentHelper.getAllReplicaSets(deployment, api, oldRSes, allOldRSes);
    wireMockRule.verify(1, getRequestedFor((urlPathEqualTo("/apis/apps/v1/namespaces/default/replicasets"))).withQueryParam("labelSelector", new EqualToPattern("app = bar")));
    Assert.assertNotNull(newRs);
    Assert.assertEquals(1, oldRSes.size());
    Assert.assertEquals(2, allOldRSes.size());
}
Also used : V1Deployment(io.kubernetes.client.openapi.models.V1Deployment) AppsV1Api(io.kubernetes.client.openapi.apis.AppsV1Api) ArrayList(java.util.ArrayList) EqualToPattern(com.github.tomakehurst.wiremock.matching.EqualToPattern) JSON(io.kubernetes.client.openapi.JSON) V1ReplicaSet(io.kubernetes.client.openapi.models.V1ReplicaSet) Test(org.junit.Test)

Example 2 with JSON

use of io.kubernetes.client.openapi.JSON in project java by kubernetes-client.

the class DeploymentHelperTest method testRevisionShouldWork.

@Test
public void testRevisionShouldWork() throws IOException {
    V1ReplicaSetList replicaSetList = new JSON().deserialize(new String(Files.readAllBytes(Paths.get(REPLICASET_LIST))), V1ReplicaSetList.class);
    List<Long> revisions = new ArrayList<>();
    for (V1ReplicaSet rs : replicaSetList.getItems()) {
        revisions.add(DeploymentHelper.revision(rs.getMetadata()));
    }
    revisions.sort(Long::compareTo);
    List<Long> exceptRevisions = Arrays.asList(1L, 2L, 2L, 3L, 4L);
    Assert.assertEquals(exceptRevisions, revisions);
}
Also used : ArrayList(java.util.ArrayList) JSON(io.kubernetes.client.openapi.JSON) V1ReplicaSetList(io.kubernetes.client.openapi.models.V1ReplicaSetList) V1ReplicaSet(io.kubernetes.client.openapi.models.V1ReplicaSet) Test(org.junit.Test)

Example 3 with JSON

use of io.kubernetes.client.openapi.JSON in project java by kubernetes-client.

the class KubernetesInformerCreatorTest method testInformerInjection.

@Test
public void testInformerInjection() throws InterruptedException {
    assertNotNull(podInformer);
    assertNotNull(configMapInformer);
    Semaphore getCount = new Semaphore(2);
    Semaphore watchCount = new Semaphore(2);
    Parameters getParams = new Parameters();
    Parameters watchParams = new Parameters();
    getParams.put("semaphore", getCount);
    watchParams.put("semaphore", watchCount);
    V1Pod foo1 = new V1Pod().kind("Pod").metadata(new V1ObjectMeta().namespace("default").name("foo1"));
    V1ConfigMap bar1 = new V1ConfigMap().kind("ConfigMap").metadata(new V1ObjectMeta().namespace("default").name("bar1"));
    wireMockRule.stubFor(get(urlMatching("^/api/v1/pods.*")).withPostServeAction("semaphore", getParams).withQueryParam("watch", equalTo("false")).willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(new V1PodList().metadata(new V1ListMeta().resourceVersion("0")).items(Arrays.asList(foo1))))));
    wireMockRule.stubFor(get(urlMatching("^/api/v1/pods.*")).withPostServeAction("semaphore", watchParams).withQueryParam("watch", equalTo("true")).willReturn(aResponse().withStatus(200).withBody("{}")));
    wireMockRule.stubFor(get(urlMatching("^/api/v1/namespaces/default/configmaps.*")).withPostServeAction("semaphore", getParams).withQueryParam("watch", equalTo("false")).willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(new V1ConfigMapList().metadata(new V1ListMeta().resourceVersion("0")).items(Arrays.asList(bar1))))));
    wireMockRule.stubFor(get(urlMatching("^/api/v1/namespaces/default/configmaps.*")).withPostServeAction("semaphore", watchParams).withQueryParam("watch", equalTo("true")).willReturn(aResponse().withStatus(200).withBody("{}")));
    // These will be released for each web call above.
    getCount.acquire(2);
    watchCount.acquire(2);
    informerFactory.startAllRegisteredInformers();
    // Wait for the GETs to complete and the watches to start.
    getCount.acquire(2);
    watchCount.acquire(2);
    verify(1, getRequestedFor(urlPathEqualTo("/api/v1/pods")).withQueryParam("watch", equalTo("false")));
    verify(getRequestedFor(urlPathEqualTo("/api/v1/pods")).withQueryParam("watch", equalTo("true")));
    verify(1, getRequestedFor(urlPathEqualTo("/api/v1/namespaces/default/configmaps")).withQueryParam("watch", equalTo("false")));
    verify(getRequestedFor(urlPathEqualTo("/api/v1/namespaces/default/configmaps")).withQueryParam("watch", equalTo("true")));
    assertEquals(1, new Lister<>(podInformer.getIndexer()).list().size());
    assertEquals(1, new Lister<>(configMapInformer.getIndexer()).list().size());
}
Also used : V1PodList(io.kubernetes.client.openapi.models.V1PodList) Parameters(com.github.tomakehurst.wiremock.extension.Parameters) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1Pod(io.kubernetes.client.openapi.models.V1Pod) JSON(io.kubernetes.client.openapi.JSON) Semaphore(java.util.concurrent.Semaphore) V1ConfigMapList(io.kubernetes.client.openapi.models.V1ConfigMapList) V1ConfigMap(io.kubernetes.client.openapi.models.V1ConfigMap) V1ListMeta(io.kubernetes.client.openapi.models.V1ListMeta) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with JSON

use of io.kubernetes.client.openapi.JSON in project java by kubernetes-client.

the class WatchTest method testWatchEnd.

@Test
public void testWatchEnd() throws IOException {
    JSON json = new JSON();
    Watch<V1ConfigMap> watch = new Watch<V1ConfigMap>(json, null, new TypeToken<Watch.Response<V1ConfigMap>>() {
    }.getType(), null);
    JsonObject metadata = new JsonObject();
    metadata.addProperty("name", "foo");
    metadata.addProperty("namespace", "bar");
    JsonObject status = new JsonObject();
    status.add("metadata", metadata);
    status.addProperty("kind", "Status");
    status.addProperty("apiVersion", "v1");
    status.addProperty("status", "failure");
    status.addProperty("message", "too old resource version");
    status.addProperty("reason", "Gone");
    status.addProperty("code", 410);
    JsonObject obj = new JsonObject();
    obj.addProperty("type", "ERROR");
    obj.add("object", status);
    String data = json.getGson().toJson(obj);
    Watch.Response<V1ConfigMap> response = watch.parseLine(data);
    assertEquals(null, response.object);
}
Also used : TypeToken(com.google.gson.reflect.TypeToken) JsonObject(com.google.gson.JsonObject) JSON(io.kubernetes.client.openapi.JSON) V1ConfigMap(io.kubernetes.client.openapi.models.V1ConfigMap) Test(org.junit.Test)

Example 5 with JSON

use of io.kubernetes.client.openapi.JSON in project java by kubernetes-client.

the class GenericKubernetesApiTest method updateStatusNamespacedJobReturningObject.

@Test
public void updateStatusNamespacedJobReturningObject() {
    V1Job foo1 = new V1Job().kind("Job").metadata(new V1ObjectMeta().namespace("default").name("foo1"));
    foo1.setStatus(new V1JobStatus().failed(1));
    stubFor(patch(urlEqualTo("/apis/batch/v1/namespaces/default/jobs/foo1/status")).willReturn(aResponse().withStatus(200).withBody(new JSON().serialize(foo1))));
    KubernetesApiResponse<V1Job> jobListResp = jobClient.updateStatus(foo1, t -> t.getStatus());
    assertTrue(jobListResp.isSuccess());
    assertEquals(foo1, jobListResp.getObject());
    assertNull(jobListResp.getStatus());
    verify(1, patchRequestedFor(urlPathEqualTo("/apis/batch/v1/namespaces/default/jobs/foo1/status")));
}
Also used : V1Job(io.kubernetes.client.openapi.models.V1Job) V1ObjectMeta(io.kubernetes.client.openapi.models.V1ObjectMeta) V1JobStatus(io.kubernetes.client.openapi.models.V1JobStatus) JSON(io.kubernetes.client.openapi.JSON) Test(org.junit.Test)

Aggregations

JSON (io.kubernetes.client.openapi.JSON)11 Test (org.junit.Test)11 V1Pod (io.kubernetes.client.openapi.models.V1Pod)7 SharedInformerFactory (io.kubernetes.client.informer.SharedInformerFactory)6 CoreV1Api (io.kubernetes.client.openapi.apis.CoreV1Api)6 V1ListMeta (io.kubernetes.client.openapi.models.V1ListMeta)6 V1ObjectMeta (io.kubernetes.client.openapi.models.V1ObjectMeta)6 V1PodList (io.kubernetes.client.openapi.models.V1PodList)6 CallGeneratorParams (io.kubernetes.client.util.CallGeneratorParams)6 ApiException (io.kubernetes.client.openapi.ApiException)5 WireMock.aResponse (com.github.tomakehurst.wiremock.client.WireMock.aResponse)4 Watch (io.kubernetes.client.util.Watch)4 ArrayList (java.util.ArrayList)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 V1ConfigMap (io.kubernetes.client.openapi.models.V1ConfigMap)2 V1ReplicaSet (io.kubernetes.client.openapi.models.V1ReplicaSet)2 V1Status (io.kubernetes.client.openapi.models.V1Status)2 Semaphore (java.util.concurrent.Semaphore)2 WireMock (com.github.tomakehurst.wiremock.client.WireMock)1 Parameters (com.github.tomakehurst.wiremock.extension.Parameters)1