diff options
author | Håkon Hallingstad <hakon@yahoo-inc.com> | 2017-02-09 11:57:43 +0100 |
---|---|---|
committer | Håkon Hallingstad <hakon@yahoo-inc.com> | 2017-02-09 11:57:43 +0100 |
commit | 10c9ce48d4f17da1271699091dc95d4c726ee4c6 (patch) | |
tree | d13403cd4fdfb2b435935b39ce615d2694832c51 /configserver/src/test | |
parent | 084ac886ddd29eef26798c5d3854c4647bf5eb73 (diff) |
Adds test of HttpProxy
Diffstat (limited to 'configserver/src/test')
5 files changed, 189 insertions, 79 deletions
diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/application/ApplicationConvergenceCheckerTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/application/ApplicationConvergenceCheckerTest.java index 73f68023266..0f11118d49d 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/application/ApplicationConvergenceCheckerTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/application/ApplicationConvergenceCheckerTest.java @@ -3,23 +3,13 @@ package com.yahoo.vespa.config.server.application; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import com.yahoo.config.codegen.InnerCNode; -import com.yahoo.config.model.api.FileDistribution; -import com.yahoo.config.model.api.HostInfo; import com.yahoo.config.model.api.Model; -import com.yahoo.config.model.api.PortInfo; -import com.yahoo.config.model.api.ServiceInfo; import com.yahoo.config.provision.ApplicationId; import com.yahoo.config.provision.ApplicationName; -import com.yahoo.config.provision.ClusterSpec; import com.yahoo.config.provision.InstanceName; -import com.yahoo.config.provision.ProvisionInfo; import com.yahoo.config.provision.TenantName; import com.yahoo.config.provision.Version; import com.yahoo.container.jdisc.HttpResponse; -import com.yahoo.vespa.config.ConfigKey; -import com.yahoo.vespa.config.ConfigPayload; -import com.yahoo.vespa.config.buildergen.ConfigDefinition; import com.yahoo.vespa.config.server.ServerCache; import com.yahoo.vespa.config.server.http.SessionHandlerTest; import com.yahoo.vespa.config.server.monitoring.MetricUpdater; @@ -31,13 +21,6 @@ import org.xml.sax.SAXException; import java.io.IOException; import java.net.URI; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; -import java.util.Set; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertEquals; @@ -59,7 +42,7 @@ public class ApplicationConvergenceCheckerTest { @Before public void setup() throws IOException, SAXException, InterruptedException { - Model mockModel = new MockModel("localhost", 1337); + Model mockModel = MockModel.createContainer("localhost", 1337); application = new Application(mockModel, new ServerCache(), 3, Version.fromIntValues(0, 0, 0), MetricUpdater.createTestUpdater(), appId); } @@ -112,61 +95,4 @@ public class ApplicationConvergenceCheckerTest { } } - // Model with two services, one that does not have a state port - private static class MockModel implements Model { - private final String hostname; - private final int statePort; - - MockModel(String hostname, int statePort) { - this.hostname = hostname; - this.statePort = statePort; - } - - @Override - public ConfigPayload getConfig(ConfigKey<?> configKey, ConfigDefinition targetDef, ConfigPayload override) { - throw new UnsupportedOperationException(); - } - - @Override - public ConfigPayload getConfig(ConfigKey<?> configKey, InnerCNode targetDef, ConfigPayload override) { - throw new UnsupportedOperationException(); - } - - @Override - public Set<ConfigKey<?>> allConfigsProduced() { - throw new UnsupportedOperationException(); - } - - @Override - public Collection<HostInfo> getHosts() { - ServiceInfo container = createServiceInfo(hostname, "container", "container", - ClusterSpec.Type.container, statePort, "state"); - ServiceInfo serviceNoStatePort = createServiceInfo(hostname, "logserver", "logserver", - ClusterSpec.Type.admin, 1234, "logtp"); - return Collections.singleton(new HostInfo(hostname, Arrays.asList(container, serviceNoStatePort))); - } - - ServiceInfo createServiceInfo(String hostname, String name, String type, ClusterSpec.Type clusterType, int port, String portTags) { - PortInfo portInfo = new PortInfo(port, Collections.singleton(portTags)); - Map<String, String> properties = new HashMap<>(); - properties.put("clustername", "default"); - properties.put("clustertype", clusterType.name()); - return new ServiceInfo(name, type, Collections.singleton(portInfo), properties, "", hostname); - } - - @Override - public Set<String> allConfigIds() { - throw new UnsupportedOperationException(); - } - - @Override - public void distributeFiles(FileDistribution fileDistribution) { - throw new UnsupportedOperationException(); - } - - @Override - public Optional<ProvisionInfo> getProvisionInfo() { - throw new UnsupportedOperationException(); - } - } } diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/application/HttpProxyTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/application/HttpProxyTest.java new file mode 100644 index 00000000000..67dfe1048f3 --- /dev/null +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/application/HttpProxyTest.java @@ -0,0 +1,64 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.config.server.application; + +import com.yahoo.config.model.api.Model; +import com.yahoo.container.jdisc.HttpResponse; +import com.yahoo.vespa.config.server.http.HttpFetcher; +import com.yahoo.vespa.config.server.http.StaticResponse; +import com.yahoo.vespa.config.server.http.RequestTimeoutException; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; + +import java.net.URL; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class HttpProxyTest { + private final HttpFetcher fetcher = mock(HttpFetcher.class); + private final HttpProxy proxy = new HttpProxy(fetcher); + + private static final String hostname = "foo.yahoo.com"; + private static final int port = 19050; + private final Application applicationMock = mock(Application.class); + + @Before + public void setup() { + Model modelMock = MockModel.createClusterController(hostname, port); + when(applicationMock.getModel()).thenReturn(modelMock); + } + + @Test + public void testNormalGet() throws Exception { + ArgumentCaptor<HttpFetcher.Params> actualParams = ArgumentCaptor.forClass(HttpFetcher.Params.class); + ArgumentCaptor<URL> actualUrl = ArgumentCaptor.forClass(URL.class); + HttpResponse response = new StaticResponse(200, "application/json", "body"); + when(fetcher.get(actualParams.capture(), actualUrl.capture())).thenReturn(response); + + HttpResponse actualResponse = proxy.get( + applicationMock, hostname, "container-clustercontroller", "clustercontroller-status/v1/clusterName"); + + assertEquals(1, actualParams.getAllValues().size()); + assertEquals(2000, actualParams.getValue().readTimeoutMs); + + assertEquals(1, actualUrl.getAllValues().size()); + assertEquals(new URL("http://" + hostname + ":" + port + "/clustercontroller-status/v1/clusterName"), + actualUrl.getValue()); + + // The HttpResponse returned by the fetcher IS the same object as the one returned by the proxy, + // when everything goes well. + assertTrue(actualResponse == response); + } + + @Test(expected = RequestTimeoutException.class) + public void testFetchException() { + when(fetcher.get(any(), any())).thenThrow(new RequestTimeoutException("timed out")); + + HttpResponse actualResponse = proxy.get( + applicationMock, hostname, "container-clustercontroller", "clustercontroller-status/v1/clusterName"); + } +} diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/application/MockModel.java b/configserver/src/test/java/com/yahoo/vespa/config/server/application/MockModel.java new file mode 100644 index 00000000000..b22e8648eae --- /dev/null +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/application/MockModel.java @@ -0,0 +1,105 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.vespa.config.server.application; + +import com.yahoo.config.codegen.InnerCNode; +import com.yahoo.config.model.api.FileDistribution; +import com.yahoo.config.model.api.HostInfo; +import com.yahoo.config.model.api.Model; +import com.yahoo.config.model.api.PortInfo; +import com.yahoo.config.model.api.ServiceInfo; +import com.yahoo.config.provision.ClusterSpec; +import com.yahoo.config.provision.ProvisionInfo; +import com.yahoo.vespa.config.ConfigKey; +import com.yahoo.vespa.config.ConfigPayload; +import com.yahoo.vespa.config.buildergen.ConfigDefinition; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +// Model with two services, one that does not have a state port +class MockModel implements Model { + private final Collection<HostInfo> hosts; + + static MockModel createContainer(String hostname, int statePort) { + ServiceInfo container = createServiceInfo(hostname, "container", "container", + ClusterSpec.Type.container, statePort, "state"); + ServiceInfo serviceNoStatePort = createServiceInfo(hostname, "logserver", "logserver", + ClusterSpec.Type.admin, 1234, "logtp"); + HostInfo hostInfo = new HostInfo(hostname, Arrays.asList(container, serviceNoStatePort)); + return new MockModel(Collections.singleton(hostInfo)); + } + + static MockModel createClusterController(String hostname, int statePort) { + ServiceInfo container = createServiceInfo( + hostname, + "foo", // name + "container-clustercontroller", // type + ClusterSpec.Type.container, + statePort, + "state http external query"); + ServiceInfo serviceNoStatePort = createServiceInfo(hostname, "storagenode", "storagenode", + ClusterSpec.Type.content, 1234, "rpc"); + HostInfo hostInfo = new HostInfo(hostname, Arrays.asList(container, serviceNoStatePort)); + + return new MockModel(Collections.singleton(hostInfo)); + } + + static private ServiceInfo createServiceInfo( + String hostname, + String name, + String type, + ClusterSpec.Type clusterType, + int port, + String portTags) { + PortInfo portInfo = new PortInfo(port, Arrays.stream(portTags.split(" ")).collect(Collectors.toSet())); + Map<String, String> properties = new HashMap<>(); + properties.put("clustername", "default"); + properties.put("clustertype", clusterType.name()); + return new ServiceInfo(name, type, Collections.singleton(portInfo), properties, "", hostname); + } + + MockModel(Collection<HostInfo> hosts) { + this.hosts = hosts; + } + + @Override + public ConfigPayload getConfig(ConfigKey<?> configKey, ConfigDefinition targetDef, ConfigPayload override) { + throw new UnsupportedOperationException(); + } + + @Override + public ConfigPayload getConfig(ConfigKey<?> configKey, InnerCNode targetDef, ConfigPayload override) { + throw new UnsupportedOperationException(); + } + + @Override + public Set<ConfigKey<?>> allConfigsProduced() { + throw new UnsupportedOperationException(); + } + + @Override + public Collection<HostInfo> getHosts() { + return hosts; + } + + @Override + public Set<String> allConfigIds() { + throw new UnsupportedOperationException(); + } + + @Override + public void distributeFiles(FileDistribution fileDistribution) { + throw new UnsupportedOperationException(); + } + + @Override + public Optional<ProvisionInfo> getProvisionInfo() { + throw new UnsupportedOperationException(); + } +} diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/http/HandlerTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/http/HandlerTest.java index 6f69b135972..23b30cd2502 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/http/HandlerTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/http/HandlerTest.java @@ -17,7 +17,12 @@ import static org.junit.Assert.*; */ public class HandlerTest { - public static void assertHttpStatusCodeErrorCodeAndMessage(HttpResponse response, int statusCode, HttpErrorResponse.errorCodes errorCode, String message) throws IOException { + public static void assertHttpStatusCodeErrorCodeAndMessage( + HttpResponse response, + int statusCode, + HttpErrorResponse.errorCodes errorCode, + String contentType, + String message) throws IOException { assertNotNull(response); String renderedString = SessionHandlerTest.getRenderedString(response); if (renderedString == null) { @@ -27,11 +32,21 @@ public class HandlerTest { if (errorCode != null) { assertThat(renderedString, containsString(errorCode.name())); } + if (contentType != null) { + assertThat(renderedString, response.getContentType(), is(contentType)); + } assertThat(renderedString, containsString(message)); } + public static void assertHttpStatusCodeErrorCodeAndMessage(HttpResponse response, int statusCode, HttpErrorResponse.errorCodes errorCode, String message) throws IOException { + assertHttpStatusCodeErrorCodeAndMessage(response, statusCode, errorCode, null, message); + } + public static void assertHttpStatusCodeAndMessage(HttpResponse response, int statusCode, String message) throws IOException { assertHttpStatusCodeErrorCodeAndMessage(response, statusCode, null, message); } + public static void assertHttpStatusCodeAndMessage(HttpResponse response, int statusCode, String contentType, String message) throws IOException { + assertHttpStatusCodeErrorCodeAndMessage(response, statusCode, null, contentType, message); + } } diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandlerTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandlerTest.java index f695c533b0f..b091ec29b75 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandlerTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandlerTest.java @@ -12,7 +12,6 @@ import com.yahoo.config.provision.TenantName; import com.yahoo.config.provision.Zone; import com.yahoo.container.jdisc.HttpRequest; import com.yahoo.container.jdisc.HttpResponse; -import com.yahoo.container.jdisc.LiteralResponse; import com.yahoo.container.logging.AccessLog; import com.yahoo.jdisc.Response; import com.yahoo.path.Path; @@ -28,6 +27,7 @@ import com.yahoo.vespa.config.server.application.TenantApplications; import com.yahoo.vespa.config.server.application.ZKTenantApplications; import com.yahoo.vespa.config.server.http.HandlerTest; import com.yahoo.vespa.config.server.http.HttpErrorResponse; +import com.yahoo.vespa.config.server.http.StaticResponse; import com.yahoo.vespa.config.server.http.SessionHandlerTest; import com.yahoo.vespa.config.server.http.SimpleHttpFetcher; import com.yahoo.vespa.config.server.modelfactory.ModelFactoryRegistry; @@ -227,10 +227,10 @@ public class ApplicationHandlerTest { String url = toUrlPath(application, Zone.defaultZone(), true) + "/clustercontroller/" + host + "/status/v1/clusterName1"; when(mockHttpProxy.get(any(), eq(host), eq("container-clustercontroller"), eq("clustercontroller-status/v1/clusterName1"))) - .thenReturn(new LiteralResponse(200, "<html>...</html>")); + .thenReturn(new StaticResponse(200, "text/html", "<html>...</html>")); HttpResponse response = mockHandler.handle(HttpRequest.createTestRequest(url, com.yahoo.jdisc.http.HttpRequest.Method.GET)); - HandlerTest.assertHttpStatusCodeAndMessage(response, 200, "<html>...</html>"); + HandlerTest.assertHttpStatusCodeAndMessage(response, 200, "text/html", "<html>...</html>"); } @Test |