diff options
author | Harald Musum <musum@verizonmedia.com> | 2020-08-03 10:41:57 +0200 |
---|---|---|
committer | Harald Musum <musum@verizonmedia.com> | 2020-08-03 10:41:57 +0200 |
commit | 7b3e179d41e2679878bf2754a4cbd0aa3c32bbb1 (patch) | |
tree | 220c5bb7df7f9e61216a489866e8aa54002f8f7e | |
parent | 6a132714b5123f917ff068431cb564d6f7a48d35 (diff) |
Simplify tests (stop using low-level code to setup test environment)
-rw-r--r-- | configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/HostHandlerTest.java | 100 | ||||
-rw-r--r-- | configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/TenantHandlerTest.java | 19 |
2 files changed, 52 insertions, 67 deletions
diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/HostHandlerTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/HostHandlerTest.java index 20e4ef56166..364e7372e20 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/HostHandlerTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/HostHandlerTest.java @@ -1,9 +1,6 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.vespa.config.server.http.v2; -import com.yahoo.config.application.api.ApplicationPackage; -import com.yahoo.config.model.NullConfigModelRegistry; -import com.yahoo.config.model.application.provider.FilesApplicationPackage; import com.yahoo.config.provision.ApplicationId; import com.yahoo.config.provision.ApplicationName; import com.yahoo.config.provision.InstanceName; @@ -15,28 +12,25 @@ import com.yahoo.jdisc.Response; import com.yahoo.vespa.config.server.ApplicationRepository; import com.yahoo.vespa.config.server.TestComponentRegistry; import com.yahoo.vespa.config.server.application.OrchestratorMock; -import com.yahoo.vespa.config.server.host.HostRegistry; import com.yahoo.vespa.config.server.http.HandlerTest; import com.yahoo.vespa.config.server.http.HttpErrorResponse; import com.yahoo.vespa.config.server.http.SessionHandlerTest; -import com.yahoo.vespa.config.server.modelfactory.ModelFactoryRegistry; -import com.yahoo.vespa.config.server.session.MockSessionZKClient; -import com.yahoo.vespa.config.server.session.RemoteSession; +import com.yahoo.vespa.config.server.session.PrepareParams; import com.yahoo.vespa.config.server.tenant.Tenant; import com.yahoo.vespa.config.server.tenant.TenantRepository; -import com.yahoo.vespa.model.VespaModelFactory; import org.junit.Before; import org.junit.Test; import java.io.File; import java.io.IOException; import java.time.Clock; -import java.util.Collections; + +import static com.yahoo.jdisc.http.HttpRequest.Method; +import static com.yahoo.vespa.config.server.http.HandlerTest.assertHttpStatusCodeErrorCodeAndMessage; /** * @author hmusum */ -// TODO: Try to move testing to ApplicationRepositoryTest and avoid all the low-level setup code here public class HostHandlerTest { private static final String urlPrefix = "http://myhost:14000/application/v2/host/"; @@ -44,57 +38,39 @@ public class HostHandlerTest { private HostHandler handler; private final static TenantName mytenant = TenantName.from("mytenant"); - private final static String hostname = "testhost"; private final static Zone zone = Zone.defaultZone(); private TenantRepository tenantRepository; - - static void addMockApplication(Tenant tenant, ApplicationId applicationId, long sessionId) { - tenant.getApplicationRepo().createApplication(applicationId); - tenant.getApplicationRepo().createPutTransaction(applicationId, sessionId).commit(); - ApplicationPackage app = FilesApplicationPackage.fromFile(testApp); - tenant.getSessionRepository().addLocalSession(new SessionHandlerTest.MockLocalSession(sessionId, app, applicationId)); - TestComponentRegistry componentRegistry = new TestComponentRegistry.Builder() - .modelFactoryRegistry(new ModelFactoryRegistry(Collections.singletonList(new VespaModelFactory(new NullConfigModelRegistry())))) - .build(); - tenant.getSessionRepository().addRemoteSession(new RemoteSession(tenant.getName(), sessionId, componentRegistry, new MockSessionZKClient(app))); - } + private ApplicationRepository applicationRepository; @Before public void setup() { - final HostRegistry<TenantName> hostRegistry = new HostRegistry<>(); - hostRegistry.update(mytenant, Collections.singletonList(hostname)); TestComponentRegistry componentRegistry = new TestComponentRegistry.Builder() .zone(zone) .build(); - tenantRepository = new TenantRepository(componentRegistry, false); + tenantRepository = new TenantRepository(componentRegistry); tenantRepository.addTenant(mytenant); - Tenant tenant = tenantRepository.getTenant(mytenant); - HostRegistry<ApplicationId> applicationHostRegistry = tenant.getApplicationRepo().getApplicationHostRegistry(); - applicationHostRegistry.update(ApplicationId.from(mytenant, ApplicationName.defaultName(), InstanceName.defaultName()), Collections.singletonList(hostname)); - ApplicationRepository applicationRepository = new ApplicationRepository(tenantRepository, - new SessionHandlerTest.MockProvisioner(), - new OrchestratorMock(), - Clock.systemUTC()); + applicationRepository = new ApplicationRepository(tenantRepository, + new SessionHandlerTest.MockProvisioner(), + new OrchestratorMock(), + Clock.systemUTC()); handler = new HostHandler(HostHandler.testOnlyContext(), applicationRepository); } @Test public void require_correct_tenant_and_application_for_hostname() throws Exception { - long sessionId = 1; - ApplicationId id = ApplicationId.from(mytenant, ApplicationName.defaultName(), InstanceName.defaultName()); - addMockApplication(tenantRepository.getTenant(mytenant), id, sessionId); - assertApplicationForHost(hostname, mytenant, id, zone); + ApplicationId applicationId = applicationId(); + applicationRepository.deploy(testApp, new PrepareParams.Builder().applicationId(applicationId).build()); + Tenant tenant = tenantRepository.getTenant(mytenant); + String hostname = applicationRepository.getCurrentActiveApplicationSet(tenant, applicationId).get().getAllHosts().iterator().next(); + assertApplicationForHost(hostname, applicationId); } @Test public void require_that_handler_gives_error_for_unknown_hostname() throws Exception { - long sessionId = 1; - addMockApplication(tenantRepository.getTenant(mytenant), ApplicationId.defaultId(), sessionId); - final String hostname = "unknown"; - assertErrorForHost(hostname, - Response.Status.NOT_FOUND, - HttpErrorResponse.errorCodes.NOT_FOUND, - "{\"error-code\":\"NOT_FOUND\",\"message\":\"Could not find any application using host '" + hostname + "'\"}"); + String hostname = "unknown"; + assertErrorForUnknownHost(hostname, + Response.Status.NOT_FOUND, + "{\"error-code\":\"NOT_FOUND\",\"message\":\"Could not find any application using host '" + hostname + "'\"}"); } @Test @@ -104,34 +80,40 @@ public class HostHandlerTest { assertNotAllowed(com.yahoo.jdisc.http.HttpRequest.Method.DELETE); } - private void assertNotAllowed(com.yahoo.jdisc.http.HttpRequest.Method method) throws IOException { - String url = urlPrefix + hostname; - deleteAndAssertResponse(url, Response.Status.METHOD_NOT_ALLOWED, - HttpErrorResponse.errorCodes.METHOD_NOT_ALLOWED, + private void assertNotAllowed(Method method) throws IOException { + String url = urlPrefix + "somehostname"; + executeAndAssertResponse(url, Response.Status.METHOD_NOT_ALLOWED, + HttpErrorResponse.errorCodes.METHOD_NOT_ALLOWED, "{\"error-code\":\"METHOD_NOT_ALLOWED\",\"message\":\"Method '" + method + "' is not supported\"}", - method); + method); } - private void assertApplicationForHost(String hostname, TenantName expectedTenantName, ApplicationId expectedApplicationId, Zone zone) throws IOException { + private void assertApplicationForHost(String hostname, ApplicationId expectedApplicationId) throws IOException { String url = urlPrefix + hostname; - HttpResponse response = handler.handle(HttpRequest.createTestRequest(url, com.yahoo.jdisc.http.HttpRequest.Method.GET)); + HttpResponse response = handler.handle(HttpRequest.createTestRequest(url, Method.GET)); HandlerTest.assertHttpStatusCodeAndMessage(response, Response.Status.OK, - "{\"tenant\":\"" + expectedTenantName.value() + "\"," + - "\"application\":\"" + expectedApplicationId.application().value() + "\"," + - "\"environment\":\"" + zone.environment().value() + "\"," + - "\"region\":\"" + zone.region().value() + "\"," + - "\"instance\":\"" + expectedApplicationId.instance().value() + "\"}" + "{\"tenant\":\"" + expectedApplicationId.tenant().value() + "\"," + + "\"application\":\"" + expectedApplicationId.application().value() + "\"," + + "\"environment\":\"" + HostHandlerTest.zone.environment().value() + "\"," + + "\"region\":\"" + HostHandlerTest.zone.region().value() + "\"," + + "\"instance\":\"" + expectedApplicationId.instance().value() + "\"}" ); } - private void assertErrorForHost(String hostname, int expectedStatus, HttpErrorResponse.errorCodes errorCode, String expectedResponse) throws IOException { + private void assertErrorForUnknownHost(String hostname, int expectedStatus, String expectedResponse) throws IOException { String url = urlPrefix + hostname; HttpResponse response = handler.handle(HttpRequest.createTestRequest(url, com.yahoo.jdisc.http.HttpRequest.Method.GET)); - HandlerTest.assertHttpStatusCodeErrorCodeAndMessage(response, expectedStatus, errorCode, expectedResponse); + assertHttpStatusCodeErrorCodeAndMessage(response, expectedStatus, HttpErrorResponse.errorCodes.NOT_FOUND, expectedResponse); } - private void deleteAndAssertResponse(String url, int expectedStatus, HttpErrorResponse.errorCodes errorCode, String expectedResponse, com.yahoo.jdisc.http.HttpRequest.Method method) throws IOException { + private void executeAndAssertResponse(String url, int expectedStatus, HttpErrorResponse.errorCodes errorCode, + String expectedResponse, Method method) throws IOException { HttpResponse response = handler.handle(HttpRequest.createTestRequest(url, method)); - HandlerTest.assertHttpStatusCodeErrorCodeAndMessage(response, expectedStatus, errorCode, expectedResponse); + assertHttpStatusCodeErrorCodeAndMessage(response, expectedStatus, errorCode, expectedResponse); } + + private ApplicationId applicationId() { + return ApplicationId.from(mytenant, ApplicationName.defaultName(), InstanceName.defaultName()); + } + } diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/TenantHandlerTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/TenantHandlerTest.java index d1a69ac09e0..3d143bb8f92 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/TenantHandlerTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/TenantHandlerTest.java @@ -5,6 +5,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.*; import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.time.Clock; @@ -18,6 +19,7 @@ import com.yahoo.vespa.config.server.TestComponentRegistry; import com.yahoo.vespa.config.server.application.OrchestratorMock; import com.yahoo.vespa.config.server.http.SessionHandlerTest; import com.yahoo.vespa.config.server.http.SessionResponse; +import com.yahoo.vespa.config.server.session.PrepareParams; import com.yahoo.vespa.config.server.tenant.Tenant; import com.yahoo.vespa.config.server.tenant.TenantRepository; import com.yahoo.vespa.curator.mock.MockCurator; @@ -32,18 +34,20 @@ import com.yahoo.vespa.config.server.http.NotFoundException; public class TenantHandlerTest { + private static final File testApp = new File("src/test/apps/app"); + private TenantRepository tenantRepository; + private ApplicationRepository applicationRepository; private TenantHandler handler; private final TenantName a = TenantName.from("a"); @Before public void setup() { tenantRepository = new TenantRepository(new TestComponentRegistry.Builder().curator(new MockCurator()).build()); - ApplicationRepository applicationRepository = - new ApplicationRepository(tenantRepository, - new SessionHandlerTest.MockProvisioner(), - new OrchestratorMock(), - Clock.systemUTC()); + applicationRepository = new ApplicationRepository(tenantRepository, + new SessionHandlerTest.MockProvisioner(), + new OrchestratorMock(), + Clock.systemUTC()); handler = new TenantHandler(TenantHandler.testOnlyContext(), applicationRepository); } @@ -111,9 +115,8 @@ public class TenantHandlerTest { Tenant tenant = tenantRepository.getTenant(a); assertEquals(a, tenant.getName()); - int sessionId = 1; - ApplicationId app = ApplicationId.from(a, ApplicationName.from("foo"), InstanceName.defaultName()); - HostHandlerTest.addMockApplication(tenant, app, sessionId); + ApplicationId applicationId = ApplicationId.from(a, ApplicationName.from("foo"), InstanceName.defaultName()); + applicationRepository.deploy(testApp, new PrepareParams.Builder().applicationId(applicationId).build()); try { handler.handleDELETE(HttpRequest.createTestRequest("http://deploy.example.yahoo.com:80/application/v2/tenant/" + a, Method.DELETE)); |