|
| 1 | +/* |
| 2 | +Copyright 2018 The Kubernetes Authors. |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | +package io.kubernetes.client; |
| 14 | + |
| 15 | +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
| 16 | +import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; |
| 17 | +import static com.github.tomakehurst.wiremock.client.WireMock.get; |
| 18 | +import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor; |
| 19 | +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; |
| 20 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; |
| 21 | +import static com.github.tomakehurst.wiremock.client.WireMock.verify; |
| 22 | + |
| 23 | +import com.github.tomakehurst.wiremock.junit.WireMockRule; |
| 24 | +import io.kubernetes.client.Attach.AttachResult; |
| 25 | +import io.kubernetes.client.util.ClientBuilder; |
| 26 | +import java.io.IOException; |
| 27 | +import org.junit.Before; |
| 28 | +import org.junit.Rule; |
| 29 | +import org.junit.Test; |
| 30 | + |
| 31 | +/** Tests for the Attach helper class */ |
| 32 | +public class AttachTest { |
| 33 | + private String namespace; |
| 34 | + private String podName; |
| 35 | + private String container; |
| 36 | + |
| 37 | + private ApiClient client; |
| 38 | + |
| 39 | + private static final int PORT = 8089; |
| 40 | + @Rule public WireMockRule wireMockRule = new WireMockRule(PORT); |
| 41 | + |
| 42 | + @Before |
| 43 | + public void setup() throws IOException { |
| 44 | + client = new ClientBuilder().setBasePath("http://localhost:" + PORT).build(); |
| 45 | + |
| 46 | + namespace = "default"; |
| 47 | + podName = "apod"; |
| 48 | + container = "acontainer"; |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testUrl() throws IOException, ApiException, InterruptedException { |
| 53 | + Attach attach = new Attach(client); |
| 54 | + |
| 55 | + stubFor( |
| 56 | + get(urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/attach")) |
| 57 | + .willReturn( |
| 58 | + aResponse() |
| 59 | + .withStatus(404) |
| 60 | + .withHeader("Content-Type", "application/json") |
| 61 | + .withBody("{}"))); |
| 62 | + |
| 63 | + AttachResult res1 = attach.attach(namespace, podName, container, false, false); |
| 64 | + AttachResult res2 = attach.attach(namespace, podName, true); |
| 65 | + |
| 66 | + // TODO: Kill this sleep, the trouble is that the test tries to validate before the connection |
| 67 | + // event has happened |
| 68 | + Thread.sleep(2000); |
| 69 | + |
| 70 | + res1.close(); |
| 71 | + res2.close(); |
| 72 | + |
| 73 | + verify( |
| 74 | + getRequestedFor( |
| 75 | + urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/attach")) |
| 76 | + .withQueryParam("stdin", equalTo("false")) |
| 77 | + .withQueryParam("tty", equalTo("false")) |
| 78 | + .withQueryParam("container", equalTo(container))); |
| 79 | + |
| 80 | + verify( |
| 81 | + getRequestedFor( |
| 82 | + urlPathEqualTo("/api/v1/namespaces/" + namespace + "/pods/" + podName + "/attach")) |
| 83 | + .withQueryParam("stdin", equalTo("true")) |
| 84 | + .withQueryParam("tty", equalTo("false"))); |
| 85 | + } |
| 86 | +} |
0 commit comments