|
| 1 | +/** |
| 2 | + * Copyright (C) 2015 Red Hat, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.fabric8.kubernetes.client; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertTrue; |
| 20 | + |
| 21 | +import io.fabric8.kubernetes.api.model.Pod; |
| 22 | +import io.fabric8.kubernetes.api.model.PodBuilder; |
| 23 | +import io.fabric8.kubernetes.api.model.Status; |
| 24 | +import io.fabric8.kubernetes.api.model.StatusBuilder; |
| 25 | +import io.fabric8.kubernetes.api.model.WatchEvent; |
| 26 | +import io.fabric8.kubernetes.api.model.WatchEventBuilder; |
| 27 | +import io.fabric8.kubernetes.server.mock.KubernetesServer; |
| 28 | +import java.net.HttpURLConnection; |
| 29 | +import java.util.concurrent.CountDownLatch; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | +import junit.framework.AssertionFailedError; |
| 32 | +import org.junit.Rule; |
| 33 | +import org.junit.Test; |
| 34 | +import org.slf4j.Logger; |
| 35 | +import org.slf4j.LoggerFactory; |
| 36 | + |
| 37 | +public class WatchOverHTTP { |
| 38 | + static final Pod pod1 = new PodBuilder().withNewMetadata().withNamespace("test").withName("pod1") |
| 39 | + .withResourceVersion("1").endMetadata().build(); |
| 40 | + static final Status outdatedStatus = new StatusBuilder().withCode(HttpURLConnection.HTTP_GONE) |
| 41 | + .withMessage( |
| 42 | + "401: The event in requested index is outdated and cleared (the requested history has been cleared [3/1]) [2]") |
| 43 | + .build(); |
| 44 | + static final WatchEvent outdatedEvent = new WatchEventBuilder().withStatusObject(outdatedStatus).build(); |
| 45 | + final String path = "/api/v1/namespaces/test/pods?fieldSelector=metadata.name%3Dpod1&resourceVersion=1&watch=true"; |
| 46 | + @Rule |
| 47 | + public KubernetesServer server = new KubernetesServer(false); |
| 48 | + Logger logger = LoggerFactory.getLogger(WatchTest.class); |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testDeleted() throws InterruptedException { |
| 52 | + logger.info("testDeleted"); |
| 53 | + KubernetesClient client = server.getClient().inNamespace("test"); |
| 54 | + |
| 55 | + server.expect() |
| 56 | + .withPath(path) |
| 57 | + .andReturn(200, "Failed WebSocket Connection").once(); |
| 58 | + server.expect().withPath(path).andReturnChunked(200, |
| 59 | + new WatchEvent(pod1, "DELETED"), "\n", |
| 60 | + new WatchEvent(pod1, "ADDED"), "\n").once(); |
| 61 | + |
| 62 | + final CountDownLatch addLatch = new CountDownLatch(1); |
| 63 | + final CountDownLatch deleteLatch = new CountDownLatch(1); |
| 64 | + try (Watch watch = client.pods().withName("pod1").withResourceVersion("1").watch(new Watcher<Pod>() { |
| 65 | + @Override |
| 66 | + public void eventReceived(Action action, Pod resource) { |
| 67 | + switch (action) { |
| 68 | + case DELETED: |
| 69 | + deleteLatch.countDown(); |
| 70 | + break; |
| 71 | + case ADDED: |
| 72 | + addLatch.countDown(); |
| 73 | + break; |
| 74 | + default: |
| 75 | + throw new AssertionFailedError(); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void onClose(KubernetesClientException cause) {} |
| 81 | + })) /* autoclose */ { |
| 82 | + assertTrue(addLatch.await(10, TimeUnit.SECONDS)); |
| 83 | + assertTrue(deleteLatch.await(10, TimeUnit.SECONDS)); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void testOutdated() throws InterruptedException { |
| 89 | + logger.info("testOutdated"); |
| 90 | + KubernetesClient client = server.getClient().inNamespace("test"); |
| 91 | + |
| 92 | + server.expect() |
| 93 | + .withPath(path) |
| 94 | + .andReturn(200, "Failed WebSocket Connection").once(); |
| 95 | + server.expect().withPath(path).andReturnChunked(200, outdatedEvent, "\n").once(); |
| 96 | + try (Watch watch = client.pods().withName("pod1").withResourceVersion("1").watch(new Watcher<Pod>() { |
| 97 | + @Override |
| 98 | + public void eventReceived(Action action, Pod resource) { |
| 99 | + throw new AssertionFailedError(); |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void onClose(KubernetesClientException cause) { |
| 104 | + throw new AssertionFailedError(); |
| 105 | + } |
| 106 | + })){}; |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testHttpErrorReconnect() throws InterruptedException { |
| 111 | + logger.info("testHttpErrorReconnect"); |
| 112 | + KubernetesClient client = server.getClient().inNamespace("test"); |
| 113 | + |
| 114 | + server.expect() |
| 115 | + .withPath(path) |
| 116 | + .andReturn(200, "Failed WebSocket Connection").once(); |
| 117 | + server.expect().withPath(path).andReturnChunked(503, new StatusBuilder().withCode(503).build()).times(6); |
| 118 | + server.expect().withPath(path).andReturnChunked(200, outdatedEvent, "\n").once(); |
| 119 | + |
| 120 | + final CountDownLatch closeLatch = new CountDownLatch(1); |
| 121 | + try (Watch watch = client.pods().withName("pod1").withResourceVersion("1").watch(new Watcher<Pod>() { |
| 122 | + @Override |
| 123 | + public void eventReceived(Action action, Pod resource) { |
| 124 | + throw new AssertionFailedError(); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public void onClose(KubernetesClientException cause) { |
| 129 | + logger.debug("onClose", cause); |
| 130 | + closeLatch.countDown(); |
| 131 | + } |
| 132 | + })) /* autoclose */ { |
| 133 | + assertTrue(closeLatch.await(3, TimeUnit.MINUTES)); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments