diff options
author | Harald Musum <musum@verizonmedia.com> | 2020-08-03 20:53:13 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-03 20:53:13 +0200 |
commit | d07e801fef2d49be22aa74e9efe4c879e0945981 (patch) | |
tree | d5a64e99877d50555c0ef03730e498eaed3d1f3c | |
parent | b01035f3ac8e1986f3e6053611757830978a660e (diff) |
Revert "Simplify tests (stop using low-level code to setup test environment)"
13 files changed, 188 insertions, 306 deletions
diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/ApplicationRepository.java b/configserver/src/main/java/com/yahoo/vespa/config/server/ApplicationRepository.java index fb089b03070..62d5184d7a9 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/ApplicationRepository.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/ApplicationRepository.java @@ -316,6 +316,7 @@ public class ApplicationRepository implements com.yahoo.config.provision.Deploye TimeoutBudget timeoutBudget = new TimeoutBudget(clock, timeout); SessionRepository sessionRepository = tenant.getSessionRepository(); LocalSession newSession = sessionRepository.createSessionFromExisting(activeSession, logger, true, timeoutBudget); + sessionRepository.addLocalSession(newSession); return Optional.of(Deployment.unprepared(newSession, this, hostProvisioner, tenant, timeout, clock, false /* don't validate as this is already deployed */, bootstrap)); @@ -649,6 +650,7 @@ public class ApplicationRepository implements com.yahoo.config.provision.Deploye SessionRepository sessionRepository = tenant.getSessionRepository(); RemoteSession fromSession = getExistingSession(tenant, applicationId); LocalSession session = sessionRepository.createSessionFromExisting(fromSession, logger, internalRedeploy, timeoutBudget); + sessionRepository.addLocalSession(session); return session.getSessionId(); } @@ -671,6 +673,7 @@ public class ApplicationRepository implements com.yahoo.config.provision.Deploye applicationId, timeoutBudget, activeSessionId); + tenant.getSessionRepository().addLocalSession(session); return session.getSessionId(); } diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/session/SessionRepository.java b/configserver/src/main/java/com/yahoo/vespa/config/server/session/SessionRepository.java index aec9c7543a2..9b337c97d88 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/session/SessionRepository.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/session/SessionRepository.java @@ -121,7 +121,7 @@ public class SessionRepository { // ---------------- Local sessions ---------------------------------------------------------------- - private synchronized void addLocalSession(LocalSession session) { + public synchronized void addLocalSession(LocalSession session) { localSessionCache.addSession(session); long sessionId = session.getSessionId(); RemoteSession remoteSession = createRemoteSession(sessionId); @@ -262,7 +262,7 @@ public class SessionRepository { return getSessionList(curator.getChildren(sessionsPath)); } - private void addRemoteSession(RemoteSession session) { + public void addRemoteSession(RemoteSession session) { remoteSessionCache.addSession(session); metrics.incAddedSessions(); } @@ -487,7 +487,6 @@ public class SessionRepository { sessionZKClient.createNewSession(clock.instant()); Curator.CompletionWaiter waiter = sessionZKClient.getUploadWaiter(); LocalSession session = new LocalSession(tenantName, sessionId, applicationPackage, sessionZKClient, applicationRepo); - addLocalSession(session); waiter.awaitCompletion(timeoutBudget.timeLeft()); return session; } @@ -519,7 +518,6 @@ public class SessionRepository { session.setVespaVersion(existingSession.getVespaVersion()); session.setDockerImageRepository(existingSession.getDockerImageRepository()); session.setAthenzDomain(existingSession.getAthenzDomain()); - addLocalSession(session); return session; } diff --git a/configserver/src/test/apps/content/schemas/music.sd b/configserver/src/test/apps/content/schemas/music.sd deleted file mode 100644 index 7670e78f22b..00000000000 --- a/configserver/src/test/apps/content/schemas/music.sd +++ /dev/null @@ -1,50 +0,0 @@ -# Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -# A basic search definition - called music, should be saved to music.sd -search music { - - # It contains one document type only - called music as well - document music { - - field title type string { - indexing: summary | index # How this field should be indexed - # index-to: title, default # Create two indexes - weight: 75 # Ranking importancy of this field, used by the built in nativeRank feature - } - - field artist type string { - indexing: summary | attribute | index - # index-to: artist, default - - weight: 25 - } - - field year type int { - indexing: summary | attribute - } - - # Increase query - field popularity type int { - indexing: summary | attribute - } - - field url type uri { - indexing: summary | index - } - - } - - rank-profile default inherits default { - first-phase { - expression: nativeRank(title,artist) + attribute(popularity) - } - - } - - rank-profile textmatch inherits default { - first-phase { - expression: nativeRank(title,artist) - } - - } - -} diff --git a/configserver/src/test/apps/content/services.xml b/configserver/src/test/apps/content/services.xml deleted file mode 100644 index 509d7786be0..00000000000 --- a/configserver/src/test/apps/content/services.xml +++ /dev/null @@ -1,38 +0,0 @@ -<?xml version="1.0" encoding="utf-8" ?> -<!-- Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. --> -<services version="1.0"> - - <admin version="2.0"> - <adminserver hostalias="node1"/> - <logserver hostalias="node1" /> - </admin> - - <content version="1.0"> - <redundancy>2</redundancy> - <documents> - <document type="music" mode="index"/> - </documents> - <nodes> - <node hostalias="node1" distribution-key="0"/> - </nodes> - - </content> - - <container version="1.0"> - <document-processing compressdocuments="true"> - <chain id="ContainerWrapperTest"> - <documentprocessor id="com.yahoo.vespa.config.AppleDocProc"/> - </chain> - </document-processing> - - <config name="project.specific"> - <value>someval</value> - </config> - - <nodes> - <node hostalias="node1" /> - </nodes> - - </container> - -</services> diff --git a/configserver/src/test/apps/content2/schemas/music.sd b/configserver/src/test/apps/content2/schemas/music.sd deleted file mode 100644 index 7670e78f22b..00000000000 --- a/configserver/src/test/apps/content2/schemas/music.sd +++ /dev/null @@ -1,50 +0,0 @@ -# Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -# A basic search definition - called music, should be saved to music.sd -search music { - - # It contains one document type only - called music as well - document music { - - field title type string { - indexing: summary | index # How this field should be indexed - # index-to: title, default # Create two indexes - weight: 75 # Ranking importancy of this field, used by the built in nativeRank feature - } - - field artist type string { - indexing: summary | attribute | index - # index-to: artist, default - - weight: 25 - } - - field year type int { - indexing: summary | attribute - } - - # Increase query - field popularity type int { - indexing: summary | attribute - } - - field url type uri { - indexing: summary | index - } - - } - - rank-profile default inherits default { - first-phase { - expression: nativeRank(title,artist) + attribute(popularity) - } - - } - - rank-profile textmatch inherits default { - first-phase { - expression: nativeRank(title,artist) - } - - } - -} diff --git a/configserver/src/test/apps/content2/services.xml b/configserver/src/test/apps/content2/services.xml deleted file mode 100644 index 509d7786be0..00000000000 --- a/configserver/src/test/apps/content2/services.xml +++ /dev/null @@ -1,38 +0,0 @@ -<?xml version="1.0" encoding="utf-8" ?> -<!-- Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. --> -<services version="1.0"> - - <admin version="2.0"> - <adminserver hostalias="node1"/> - <logserver hostalias="node1" /> - </admin> - - <content version="1.0"> - <redundancy>2</redundancy> - <documents> - <document type="music" mode="index"/> - </documents> - <nodes> - <node hostalias="node1" distribution-key="0"/> - </nodes> - - </content> - - <container version="1.0"> - <document-processing compressdocuments="true"> - <chain id="ContainerWrapperTest"> - <documentprocessor id="com.yahoo.vespa.config.AppleDocProc"/> - </chain> - </document-processing> - - <config name="project.specific"> - <value>someval</value> - </config> - - <nodes> - <node hostalias="node1" /> - </nodes> - - </container> - -</services> diff --git a/configserver/src/test/apps/zkapp/deployment.xml b/configserver/src/test/apps/zkapp/deployment.xml index 2944b8220f3..dd47299f578 100644 --- a/configserver/src/test/apps/zkapp/deployment.xml +++ b/configserver/src/test/apps/zkapp/deployment.xml @@ -2,7 +2,7 @@ <!-- Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. --> <deployment version='1.0'> <test/> - <prod global-service-id='mydisc'> + <prod global-service-id='mydisc'>+ <region active='true'>us-east</region> </prod> </deployment> diff --git a/configserver/src/test/apps/zkapp/services.xml b/configserver/src/test/apps/zkapp/services.xml index 58ecf41707d..672f058bc0a 100644 --- a/configserver/src/test/apps/zkapp/services.xml +++ b/configserver/src/test/apps/zkapp/services.xml @@ -11,7 +11,7 @@ </admin> <container version="1.0"> - <document-api/> + <documentapi/> <search/> <nodes> <node hostalias="node1"/> @@ -21,7 +21,7 @@ <content version="1.0"> <redundancy>1</redundancy> <documents> - <document type="music" mode="index"/> + <document name="music" mode="index"/> </documents> <nodes> <node hostalias="node1" distribution-key="0"/> diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/http/SessionHandlerTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/http/SessionHandlerTest.java index bd306880039..0b9a780d9e1 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/http/SessionHandlerTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/http/SessionHandlerTest.java @@ -1,6 +1,8 @@ // 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; +import com.yahoo.config.application.api.ApplicationFile; +import com.yahoo.config.application.api.ApplicationPackage; import com.yahoo.config.provision.ApplicationId; import com.yahoo.config.provision.Capacity; import com.yahoo.config.provision.ClusterSpec; @@ -8,14 +10,22 @@ import com.yahoo.config.provision.HostFilter; import com.yahoo.config.provision.HostSpec; import com.yahoo.config.provision.ProvisionLogger; import com.yahoo.config.provision.Provisioner; +import com.yahoo.config.provision.TenantName; import com.yahoo.container.jdisc.HttpRequest; import com.yahoo.container.jdisc.HttpResponse; +import com.yahoo.path.Path; import com.yahoo.transaction.NestedTransaction; +import com.yahoo.transaction.Transaction; +import com.yahoo.vespa.config.server.session.DummyTransaction; +import com.yahoo.vespa.config.server.session.LocalSession; +import com.yahoo.vespa.config.server.session.MockSessionZKClient; +import com.yahoo.vespa.config.server.session.Session; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.nio.charset.StandardCharsets; +import java.time.Instant; import java.util.Collection; import java.util.List; import java.util.Map; @@ -69,6 +79,52 @@ public class SessionHandlerTest { return baos.toString(StandardCharsets.UTF_8); } + public static class MockLocalSession extends LocalSession { + + public Session.Status status; + private Instant createTime = Instant.now(); + private ApplicationId applicationId; + + public MockLocalSession(long sessionId, ApplicationPackage app) { + super(TenantName.defaultName(), sessionId, app, new MockSessionZKClient(app), null); + } + + public MockLocalSession(long sessionId, ApplicationPackage app, ApplicationId applicationId) { + this(sessionId, app); + this.applicationId = applicationId; + } + + public void setStatus(Session.Status status) { + this.status = status; + } + + @Override + public Session.Status getStatus() { + return this.status; + } + + @Override + public Transaction createActivateTransaction() { + return new DummyTransaction().add((DummyTransaction.RunnableOperation) () -> status = Status.ACTIVATE); + } + + @Override + public ApplicationFile getApplicationFile(Path relativePath, Mode mode) { + return this.applicationPackage.getFile(relativePath); + } + + @Override + public ApplicationId getApplicationId() { + return applicationId; + } + + @Override + public Instant getCreateTime() { + return createTime; + } + + } + public enum Cmd { PREPARED("prepared"), ACTIVE("active"), diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ApplicationContentHandlerTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ApplicationContentHandlerTest.java index 8bf5215a696..97085416073 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ApplicationContentHandlerTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ApplicationContentHandlerTest.java @@ -1,6 +1,7 @@ // 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.model.application.provider.FilesApplicationPackage; import com.yahoo.config.provision.ApplicationId; import com.yahoo.config.provision.TenantName; import com.yahoo.config.provision.Zone; @@ -11,8 +12,6 @@ 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.http.ContentHandlerTestBase; -import com.yahoo.vespa.config.server.session.LocalSession; -import com.yahoo.vespa.config.server.session.PrepareParams; import com.yahoo.vespa.config.server.session.Session; import com.yahoo.vespa.config.server.tenant.Tenant; import com.yahoo.vespa.config.server.tenant.TenantRepository; @@ -31,42 +30,47 @@ import static org.junit.Assert.assertThat; * @author Ulf Lilleengen */ public class ApplicationContentHandlerTest extends ContentHandlerTestBase { - - private static final File testApp = new File("src/test/apps/content"); - private static final File testApp2 = new File("src/test/apps/content2"); - private final TestComponentRegistry componentRegistry = new TestComponentRegistry.Builder().build(); private final Clock clock = componentRegistry.getClock(); - private final TenantName tenantName1 = TenantName.from("mofet"); - private final TenantName tenantName2 = TenantName.from("bla"); - private final String baseServer = "http://foo:1337"; - - private final ApplicationId appId1 = new ApplicationId.Builder().tenant(tenantName1).applicationName("foo").instanceName("quux").build(); - private final ApplicationId appId2 = new ApplicationId.Builder().tenant(tenantName2).applicationName("foo").instanceName("quux").build(); - - private TenantRepository tenantRepository; - private ApplicationRepository applicationRepository; private ApplicationHandler handler; + private TenantName tenantName1 = TenantName.from("mofet"); + private TenantName tenantName2 = TenantName.from("bla"); + private String baseServer = "http://foo:1337"; + + private ApplicationId idTenant1 = new ApplicationId.Builder() + .tenant(tenantName1) + .applicationName("foo").instanceName("quux").build(); + private ApplicationId idTenant2 = new ApplicationId.Builder() + .tenant(tenantName2) + .applicationName("foo").instanceName("quux").build(); + private MockLocalSession session2; @Before public void setupHandler() { - tenantRepository = new TenantRepository(componentRegistry, false); + TenantRepository tenantRepository = new TenantRepository(componentRegistry, false); tenantRepository.addTenant(tenantName1); tenantRepository.addTenant(tenantName2); - applicationRepository = new ApplicationRepository(tenantRepository, - new MockProvisioner(), - new OrchestratorMock(), - clock); + session2 = new MockLocalSession(2, FilesApplicationPackage.fromFile(new File("src/test/apps/content"))); + Tenant tenant1 = tenantRepository.getTenant(tenantName1); + tenant1.getSessionRepository().addLocalSession(session2); + tenant1.getApplicationRepo().createApplication(idTenant1); + tenant1.getApplicationRepo().createPutTransaction(idTenant1, 2).commit(); - applicationRepository.deploy(testApp, prepareParams(appId1)); - applicationRepository.deploy(testApp2, prepareParams(appId2)); + MockLocalSession session3 = new MockLocalSession(3, FilesApplicationPackage.fromFile(new File("src/test/apps/content2"))); + Tenant tenant2 = tenantRepository.getTenant(tenantName2); + tenant2.getSessionRepository().addLocalSession(session3); + tenant2.getApplicationRepo().createApplication(idTenant2); + tenant2.getApplicationRepo().createPutTransaction(idTenant2, 3).commit(); handler = new ApplicationHandler(ApplicationHandler.testOnlyContext(), Zone.defaultZone(), - applicationRepository); - pathPrefix = createPath(appId1, Zone.defaultZone()); + new ApplicationRepository(tenantRepository, + new MockProvisioner(), + new OrchestratorMock(), + clock)); + pathPrefix = createPath(idTenant1, Zone.defaultZone()); baseUrl = baseServer + pathPrefix; } @@ -99,17 +103,16 @@ public class ApplicationContentHandlerTest extends ContentHandlerTestBase { @Test public void require_that_multiple_tenants_are_handled() throws IOException { assertContent("/test.txt", "foo\n"); - pathPrefix = createPath(appId2, Zone.defaultZone()); + pathPrefix = createPath(idTenant2, Zone.defaultZone()); baseUrl = baseServer + pathPrefix; assertContent("/test.txt", "bar\n"); } @Test public void require_that_get_does_not_set_write_flag() throws IOException { - Tenant tenant1 = tenantRepository.getTenant(tenantName1); - LocalSession session = applicationRepository.getActiveLocalSession(tenant1, appId1); + session2.status = Session.Status.PREPARE; assertContent("/test.txt", "foo\n"); - assertThat(session.getStatus(), is(Session.Status.ACTIVATE)); + assertThat(session2.status, is(Session.Status.PREPARE)); } private void assertNotFound(HttpRequest request) { @@ -123,9 +126,4 @@ public class ApplicationContentHandlerTest extends ContentHandlerTestBase { HttpRequest request = HttpRequest.createTestRequest(baseUrl + path, method); return handler.handle(request); } - - private PrepareParams prepareParams(ApplicationId applicationId) { - return new PrepareParams.Builder().applicationId(applicationId).build(); - } - } 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 364e7372e20..20e4ef56166 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,6 +1,9 @@ // 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; @@ -12,25 +15,28 @@ 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.session.PrepareParams; +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.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 static com.yahoo.jdisc.http.HttpRequest.Method; -import static com.yahoo.vespa.config.server.http.HandlerTest.assertHttpStatusCodeErrorCodeAndMessage; +import java.util.Collections; /** * @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/"; @@ -38,39 +44,57 @@ 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; - private ApplicationRepository applicationRepository; + + 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))); + } @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); + tenantRepository = new TenantRepository(componentRegistry, false); tenantRepository.addTenant(mytenant); - applicationRepository = new ApplicationRepository(tenantRepository, - new SessionHandlerTest.MockProvisioner(), - new OrchestratorMock(), - Clock.systemUTC()); + 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()); handler = new HostHandler(HostHandler.testOnlyContext(), applicationRepository); } @Test public void require_correct_tenant_and_application_for_hostname() throws Exception { - 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); + long sessionId = 1; + ApplicationId id = ApplicationId.from(mytenant, ApplicationName.defaultName(), InstanceName.defaultName()); + addMockApplication(tenantRepository.getTenant(mytenant), id, sessionId); + assertApplicationForHost(hostname, mytenant, id, zone); } @Test public void require_that_handler_gives_error_for_unknown_hostname() throws Exception { - String hostname = "unknown"; - assertErrorForUnknownHost(hostname, - Response.Status.NOT_FOUND, - "{\"error-code\":\"NOT_FOUND\",\"message\":\"Could not find any application using host '" + hostname + "'\"}"); + 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 + "'\"}"); } @Test @@ -80,40 +104,34 @@ public class HostHandlerTest { assertNotAllowed(com.yahoo.jdisc.http.HttpRequest.Method.DELETE); } - private void assertNotAllowed(Method method) throws IOException { - String url = urlPrefix + "somehostname"; - executeAndAssertResponse(url, Response.Status.METHOD_NOT_ALLOWED, - HttpErrorResponse.errorCodes.METHOD_NOT_ALLOWED, + 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, "{\"error-code\":\"METHOD_NOT_ALLOWED\",\"message\":\"Method '" + method + "' is not supported\"}", - method); + method); } - private void assertApplicationForHost(String hostname, ApplicationId expectedApplicationId) throws IOException { + private void assertApplicationForHost(String hostname, TenantName expectedTenantName, ApplicationId expectedApplicationId, Zone zone) throws IOException { String url = urlPrefix + hostname; - HttpResponse response = handler.handle(HttpRequest.createTestRequest(url, Method.GET)); + HttpResponse response = handler.handle(HttpRequest.createTestRequest(url, com.yahoo.jdisc.http.HttpRequest.Method.GET)); HandlerTest.assertHttpStatusCodeAndMessage(response, Response.Status.OK, - "{\"tenant\":\"" + expectedApplicationId.tenant().value() + "\"," + - "\"application\":\"" + expectedApplicationId.application().value() + "\"," + - "\"environment\":\"" + HostHandlerTest.zone.environment().value() + "\"," + - "\"region\":\"" + HostHandlerTest.zone.region().value() + "\"," + - "\"instance\":\"" + expectedApplicationId.instance().value() + "\"}" + "{\"tenant\":\"" + expectedTenantName.value() + "\"," + + "\"application\":\"" + expectedApplicationId.application().value() + "\"," + + "\"environment\":\"" + zone.environment().value() + "\"," + + "\"region\":\"" + zone.region().value() + "\"," + + "\"instance\":\"" + expectedApplicationId.instance().value() + "\"}" ); } - private void assertErrorForUnknownHost(String hostname, int expectedStatus, String expectedResponse) throws IOException { + private void assertErrorForHost(String hostname, int expectedStatus, HttpErrorResponse.errorCodes errorCode, String expectedResponse) throws IOException { String url = urlPrefix + hostname; HttpResponse response = handler.handle(HttpRequest.createTestRequest(url, com.yahoo.jdisc.http.HttpRequest.Method.GET)); - assertHttpStatusCodeErrorCodeAndMessage(response, expectedStatus, HttpErrorResponse.errorCodes.NOT_FOUND, expectedResponse); + HandlerTest.assertHttpStatusCodeErrorCodeAndMessage(response, expectedStatus, errorCode, expectedResponse); } - private void executeAndAssertResponse(String url, int expectedStatus, HttpErrorResponse.errorCodes errorCode, - String expectedResponse, Method method) throws IOException { + private void deleteAndAssertResponse(String url, int expectedStatus, HttpErrorResponse.errorCodes errorCode, String expectedResponse, com.yahoo.jdisc.http.HttpRequest.Method method) throws IOException { HttpResponse response = handler.handle(HttpRequest.createTestRequest(url, method)); - assertHttpStatusCodeErrorCodeAndMessage(response, expectedStatus, errorCode, expectedResponse); + HandlerTest.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/SessionContentHandlerTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/SessionContentHandlerTest.java index 3ab56d3869a..20d9be080e9 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/SessionContentHandlerTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/SessionContentHandlerTest.java @@ -1,11 +1,10 @@ // 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.provision.ApplicationId; -import com.yahoo.config.provision.ApplicationName; -import com.yahoo.config.provision.InstanceName; +import com.yahoo.config.model.application.provider.FilesApplicationPackage; import com.yahoo.config.provision.TenantName; import com.yahoo.container.jdisc.HttpResponse; +import com.yahoo.io.IOUtils; import com.yahoo.jdisc.Response; import com.yahoo.jdisc.http.HttpRequest; import com.yahoo.text.Utf8; @@ -14,8 +13,6 @@ import com.yahoo.vespa.config.server.TestComponentRegistry; import com.yahoo.vespa.config.server.application.OrchestratorMock; import com.yahoo.vespa.config.server.http.ContentHandlerTestBase; import com.yahoo.vespa.config.server.http.SessionHandlerTest; -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 org.junit.Before; import org.junit.Ignore; @@ -25,7 +22,7 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.time.Clock; +import java.nio.file.Files; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertNotNull; @@ -35,31 +32,21 @@ import static org.junit.Assert.assertThat; * @author Ulf Lilleengen */ public class SessionContentHandlerTest extends ContentHandlerTestBase { - private static final TenantName tenantName = TenantName.from("contenttest"); - private static final File testApp = new File("src/test/apps/content"); + private static final TenantName tenant = TenantName.from("contenttest"); private final TestComponentRegistry componentRegistry = new TestComponentRegistry.Builder().build(); private TenantRepository tenantRepository; private SessionContentHandler handler = null; - private long sessionId; - + @Before - public void setupHandler() { + public void setupHandler() throws Exception { tenantRepository = new TenantRepository(componentRegistry, false); - tenantRepository.addTenant(tenantName); - - ApplicationRepository applicationRepository = new ApplicationRepository(tenantRepository, - new SessionHandlerTest.MockProvisioner(), - new OrchestratorMock(), - Clock.systemUTC()); - applicationRepository.deploy(testApp, new PrepareParams.Builder().applicationId(applicationId()).build()); - Tenant tenant = tenantRepository.getTenant(tenantName); - sessionId = applicationRepository.getActiveLocalSession(tenant, applicationId()).getSessionId(); - + tenantRepository.addTenant(tenant); + tenantRepository.getTenant(tenant).getSessionRepository().addLocalSession(new MockLocalSession(1L, FilesApplicationPackage.fromFile(createTestApp()))); handler = createHandler(); - pathPrefix = "/application/v2/tenant/" + tenantName + "/session/"; - baseUrl = "http://foo:1337/application/v2/tenant/" + tenantName + "/session/" + sessionId + "/content/"; + pathPrefix = "/application/v2/tenant/" + tenant + "/session/"; + baseUrl = "http://foo:1337/application/v2/tenant/" + tenant + "/session/1/content/"; } @Test @@ -82,14 +69,14 @@ public class SessionContentHandlerTest extends ContentHandlerTestBase { @Test public void require_that_nonexistent_session_returns_not_found() { - HttpResponse response = doRequest(HttpRequest.Method.GET, "/test.txt", 9999); + HttpResponse response = doRequest(HttpRequest.Method.GET, "/test.txt", 2); assertNotNull(response); assertThat(response.getStatus(), is(Response.Status.NOT_FOUND)); } protected HttpResponse put(String path, String content) { ByteArrayInputStream data = new ByteArrayInputStream(Utf8.toBytes(content)); - return doRequest(HttpRequest.Method.PUT, path, sessionId, data); + return doRequest(HttpRequest.Method.PUT, path, 1, data); } @Test @@ -107,13 +94,13 @@ public class SessionContentHandlerTest extends ContentHandlerTestBase { @Test public void require_that_nonexistent_file_returns_not_found_when_deleted() throws IOException { - assertDeleteFile(Response.Status.NOT_FOUND, "/test2.txt", "{\"error-code\":\"NOT_FOUND\",\"message\":\"Session " + sessionId + " does not contain a file 'test2.txt'\"}"); + assertDeleteFile(Response.Status.NOT_FOUND, "/test2.txt", "{\"error-code\":\"NOT_FOUND\",\"message\":\"Session 1 does not contain a file 'test2.txt'\"}"); } @Test public void require_that_files_can_be_deleted() throws IOException { assertDeleteFile(Response.Status.OK, "/test.txt"); - assertDeleteFile(Response.Status.NOT_FOUND, "/test.txt", "{\"error-code\":\"NOT_FOUND\",\"message\":\"Session " + sessionId + " does not contain a file 'test.txt'\"}"); + assertDeleteFile(Response.Status.NOT_FOUND, "/test.txt", "{\"error-code\":\"NOT_FOUND\",\"message\":\"Session 1 does not contain a file 'test.txt'\"}"); assertDeleteFile(Response.Status.BAD_REQUEST, "/newtest", "{\"error-code\":\"BAD_REQUEST\",\"message\":\"File 'newtest' is not an empty directory\"}"); assertDeleteFile(Response.Status.OK, "/newtest/testfile.txt"); assertDeleteFile(Response.Status.OK, "/newtest"); @@ -122,10 +109,10 @@ public class SessionContentHandlerTest extends ContentHandlerTestBase { @Test public void require_that_status_is_given_for_new_files() throws IOException { assertStatus("/test.txt?return=status", - "{\"status\":\"new\",\"md5\":\"d3b07384d113edec49eaa6238ad5ff00\",\"name\":\"http://foo:1337" + pathPrefix + sessionId + "/content/test.txt\"}"); + "{\"status\":\"new\",\"md5\":\"d3b07384d113edec49eaa6238ad5ff00\",\"name\":\"http://foo:1337" + pathPrefix + "1/content/test.txt\"}"); assertWriteFile("/test.txt", "Mycontent"); assertStatus("/test.txt?return=status", - "{\"status\":\"changed\",\"md5\":\"01eabd73c69d78d0009ec93cd62d7f77\",\"name\":\"http://foo:1337" + pathPrefix + sessionId + "/content/test.txt\"}"); + "{\"status\":\"changed\",\"md5\":\"01eabd73c69d78d0009ec93cd62d7f77\",\"name\":\"http://foo:1337" + pathPrefix + "1/content/test.txt\"}"); } private void assertWriteFile(String path, String content) throws IOException { @@ -134,11 +121,11 @@ public class SessionContentHandlerTest extends ContentHandlerTestBase { assertThat(response.getStatus(), is(Response.Status.OK)); assertContent(path, content); assertThat(SessionHandlerTest.getRenderedString(response), - is("{\"prepared\":\"http://foo:1337" + pathPrefix + sessionId + "/prepared\"}")); + is("{\"prepared\":\"http://foo:1337" + pathPrefix + "1/prepared\"}")); } private void assertDeleteFile(int statusCode, String filePath) throws IOException { - assertDeleteFile(statusCode, filePath, "{\"prepared\":\"http://foo:1337" + pathPrefix + sessionId + "/prepared\"}"); + assertDeleteFile(statusCode, filePath, "{\"prepared\":\"http://foo:1337" + pathPrefix + "1/prepared\"}"); } private void assertDeleteFile(int statusCode, String filePath, String expectedResponse) throws IOException { @@ -153,11 +140,17 @@ public class SessionContentHandlerTest extends ContentHandlerTestBase { assertNotNull(response); assertThat(response.getStatus(), is(Response.Status.OK)); assertThat(SessionHandlerTest.getRenderedString(response), - is("{\"prepared\":\"http://foo:1337" + pathPrefix + sessionId + "/prepared\"}")); + is("{\"prepared\":\"http://foo:1337" + pathPrefix + "1/prepared\"}")); + } + + private File createTestApp() throws IOException { + File testApp = Files.createTempDirectory("session-content-handler-test-app").toFile(); + IOUtils.copyDirectory(new File("src/test/apps/content"), testApp); + return testApp; } protected HttpResponse doRequest(HttpRequest.Method method, String path) { - return doRequest(method, path, sessionId); + return doRequest(method, path, 1); } private HttpResponse doRequest(HttpRequest.Method method, String path, long sessionId) { @@ -177,9 +170,4 @@ public class SessionContentHandlerTest extends ContentHandlerTestBase { componentRegistry.getClock()) ); } - - private ApplicationId applicationId() { - return ApplicationId.from(tenantName, 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 3d143bb8f92..d1a69ac09e0 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,7 +5,6 @@ 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; @@ -19,7 +18,6 @@ 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; @@ -34,20 +32,18 @@ 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 = new ApplicationRepository(tenantRepository, - new SessionHandlerTest.MockProvisioner(), - new OrchestratorMock(), - Clock.systemUTC()); + ApplicationRepository applicationRepository = + new ApplicationRepository(tenantRepository, + new SessionHandlerTest.MockProvisioner(), + new OrchestratorMock(), + Clock.systemUTC()); handler = new TenantHandler(TenantHandler.testOnlyContext(), applicationRepository); } @@ -115,8 +111,9 @@ public class TenantHandlerTest { Tenant tenant = tenantRepository.getTenant(a); assertEquals(a, tenant.getName()); - ApplicationId applicationId = ApplicationId.from(a, ApplicationName.from("foo"), InstanceName.defaultName()); - applicationRepository.deploy(testApp, new PrepareParams.Builder().applicationId(applicationId).build()); + int sessionId = 1; + ApplicationId app = ApplicationId.from(a, ApplicationName.from("foo"), InstanceName.defaultName()); + HostHandlerTest.addMockApplication(tenant, app, sessionId); try { handler.handleDELETE(HttpRequest.createTestRequest("http://deploy.example.yahoo.com:80/application/v2/tenant/" + a, Method.DELETE)); |