Search in sources :

Example 6 with WebSocketStreamHandler

use of io.kubernetes.client.util.WebSocketStreamHandler in project java by kubernetes-client.

the class PortForwardTest method testPortForwardResult.

@Test
public void testPortForwardResult() throws IOException, InterruptedException {
    WebSocketStreamHandler handler = new WebSocketStreamHandler();
    List<Integer> ports = new ArrayList<>();
    ports.add(80);
    ports.add(800);
    final PortForwardResult result = new PortForwardResult(handler, ports);
    String msgData = "this is a test datum";
    String msgData2 = "this is a different test datum";
    handler.open("wss", null);
    handler.bytesMessage(makeStream(new byte[] { 0, 80, 0 }, msgData.getBytes(StandardCharsets.UTF_8)));
    handler.bytesMessage(makeStream(new byte[] { 2, 32, 3 }, msgData2.getBytes(StandardCharsets.UTF_8)));
    final Object block = new Object();
    Thread t = new Thread(new Runnable() {

        public void run() {
            try {
                result.init();
                synchronized (block) {
                    block.notifyAll();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    });
    synchronized (block) {
        t.start();
        block.wait();
    }
    InputStream is = result.getInputStream(80);
    assertNotNull(is);
    InputStream is2 = result.getInputStream(800);
    assertNotNull(is2);
    byte[] bytes = new byte[msgData.length()];
    for (int i = 0; i < msgData.length(); i++) {
        bytes[i] = (byte) is.read();
    }
    assertEquals(msgData, new String(bytes, StandardCharsets.UTF_8));
    bytes = new byte[msgData2.length()];
    for (int i = 0; i < msgData2.length(); i++) {
        bytes[i] = (byte) is2.read();
    }
    assertEquals(msgData2, new String(bytes, StandardCharsets.UTF_8));
    assertEquals(null, result.getInputStream(8080));
    assertEquals(null, result.getErrorStream(8080));
    assertEquals(null, result.getOutboundStream(8080));
}
Also used : InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) PortForwardResult(io.kubernetes.client.PortForward.PortForwardResult) WebSocketStreamHandler(io.kubernetes.client.util.WebSocketStreamHandler) Test(org.junit.Test)

Example 7 with WebSocketStreamHandler

use of io.kubernetes.client.util.WebSocketStreamHandler in project java by kubernetes-client.

the class WebsocketStreamHandlerTest method testHandlerReceivingData.

@Test
public void testHandlerReceivingData() throws IOException {
    int testStreamId = 0;
    byte testData = 1;
    byte[] testDatas = // first byte stands for stream id,
    new byte[] { (byte) testStreamId, testData, testData };
    ByteArrayInputStream testBytesInputStream = new ByteArrayInputStream(testDatas);
    WebSocketStreamHandler handler = new WebSocketStreamHandler();
    MockWebSocket mockWebSocket = new MockWebSocket();
    handler.open(testProtocol, mockWebSocket);
    InputStream inputStream = handler.getInputStream(testStreamId);
    // handler receiving
    handler.bytesMessage(testBytesInputStream);
    byte[] receivingData = new byte[16];
    inputStream.read(receivingData);
    handler.close();
    assertEquals(testData, receivingData[0]);
    assertEquals(testData, receivingData[1]);
    assertTrue(mockWebSocket.closed);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) WebSocketStreamHandler(io.kubernetes.client.util.WebSocketStreamHandler) Test(org.junit.Test)

Example 8 with WebSocketStreamHandler

use of io.kubernetes.client.util.WebSocketStreamHandler in project java by kubernetes-client.

the class PortForward method forward.

/**
 * PortForward to a container.
 *
 * @param namespace The namespace of the Pod
 * @param name The name of the Pod
 * @param ports The ports to forward
 * @return The result of the Port Forward request.
 */
public PortForwardResult forward(String namespace, String name, List<Integer> ports) throws ApiException, IOException {
    String path = makePath(namespace, name);
    WebSocketStreamHandler handler = new WebSocketStreamHandler();
    PortForwardResult result = new PortForwardResult(handler, ports);
    List<Pair> queryParams = new ArrayList<>(ports.size());
    for (Integer port : ports) {
        queryParams.add(new Pair("ports", port.toString()));
    }
    WebSockets.stream(path, "GET", queryParams, apiClient, handler);
    try {
        handler.waitForInitialized();
    } catch (InterruptedException ex) {
        throw new ApiException(ex);
    }
    Throwable err = handler.getError();
    if (err != null) {
        throw new ApiException(err);
    }
    // Wait for streams to start.
    result.init();
    return result;
}
Also used : WebSocketStreamHandler(io.kubernetes.client.util.WebSocketStreamHandler) ArrayList(java.util.ArrayList) Pair(io.kubernetes.client.openapi.Pair) ApiException(io.kubernetes.client.openapi.ApiException)

Aggregations

WebSocketStreamHandler (io.kubernetes.client.util.WebSocketStreamHandler)8 Test (org.junit.Test)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ArrayList (java.util.ArrayList)3 PortForwardResult (io.kubernetes.client.PortForward.PortForwardResult)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 OutputStream (java.io.OutputStream)2 ApiException (io.kubernetes.client.openapi.ApiException)1 Pair (io.kubernetes.client.openapi.Pair)1