diff options
author | Harald Musum <musum@verizonmedia.com> | 2020-08-04 06:34:47 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-04 06:34:47 +0200 |
commit | 67cbce2b676e9341306b99624ab63d865afd934b (patch) | |
tree | 9c4867bef6d2b96eb2a65257c62f8c9a0b3fae53 | |
parent | 50c10ba986d66fbc43f1b88e7f7a9920c0d6cecc (diff) | |
parent | 4a06a676ab560c87395ba45c5da24d115122410c (diff) |
Merge pull request #13974 from vespa-engine/hmusum/configserver-refactoring-25-take-2
Simplify tests (stop using low-level code to setup test environment, take 2
11 files changed, 302 insertions, 183 deletions
diff --git a/configserver/src/test/apps/content/schemas/music.sd b/configserver/src/test/apps/content/schemas/music.sd new file mode 100644 index 00000000000..7670e78f22b --- /dev/null +++ b/configserver/src/test/apps/content/schemas/music.sd @@ -0,0 +1,50 @@ +# 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 new file mode 100644 index 00000000000..509d7786be0 --- /dev/null +++ b/configserver/src/test/apps/content/services.xml @@ -0,0 +1,38 @@ +<?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 new file mode 100644 index 00000000000..7670e78f22b --- /dev/null +++ b/configserver/src/test/apps/content2/schemas/music.sd @@ -0,0 +1,50 @@ +# 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 new file mode 100644 index 00000000000..509d7786be0 --- /dev/null +++ b/configserver/src/test/apps/content2/services.xml @@ -0,0 +1,38 @@ +<?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 dd47299f578..2944b8220f3 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 672f058bc0a..58ecf41707d 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"> - <documentapi/> + <document-api/> <search/> <nodes> <node hostalias="node1"/> @@ -21,7 +21,7 @@ <content version="1.0"> <redundancy>1</redundancy> <documents> - <document name="music" mode="index"/> + <document type="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 0b9a780d9e1..bd306880039 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,8 +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; -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; @@ -10,22 +8,14 @@ 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; @@ -79,52 +69,6 @@ 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 97085416073..8bf5215a696 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,7 +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.model.application.provider.FilesApplicationPackage; import com.yahoo.config.provision.ApplicationId; import com.yahoo.config.provision.TenantName; import com.yahoo.config.provision.Zone; @@ -12,6 +11,8 @@ 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; @@ -30,47 +31,42 @@ 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 tenantRepository = new TenantRepository(componentRegistry, false); + tenantRepository = new TenantRepository(componentRegistry, false); tenantRepository.addTenant(tenantName1); tenantRepository.addTenant(tenantName2); - 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 = new ApplicationRepository(tenantRepository, + new MockProvisioner(), + new OrchestratorMock(), + clock); - 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(); + applicationRepository.deploy(testApp, prepareParams(appId1)); + applicationRepository.deploy(testApp2, prepareParams(appId2)); handler = new ApplicationHandler(ApplicationHandler.testOnlyContext(), Zone.defaultZone(), - new ApplicationRepository(tenantRepository, - new MockProvisioner(), - new OrchestratorMock(), - clock)); - pathPrefix = createPath(idTenant1, Zone.defaultZone()); + applicationRepository); + pathPrefix = createPath(appId1, Zone.defaultZone()); baseUrl = baseServer + pathPrefix; } @@ -103,16 +99,17 @@ public class ApplicationContentHandlerTest extends ContentHandlerTestBase { @Test public void require_that_multiple_tenants_are_handled() throws IOException { assertContent("/test.txt", "foo\n"); - pathPrefix = createPath(idTenant2, Zone.defaultZone()); + pathPrefix = createPath(appId2, Zone.defaultZone()); baseUrl = baseServer + pathPrefix; assertContent("/test.txt", "bar\n"); } @Test public void require_that_get_does_not_set_write_flag() throws IOException { - session2.status = Session.Status.PREPARE; + Tenant tenant1 = tenantRepository.getTenant(tenantName1); + LocalSession session = applicationRepository.getActiveLocalSession(tenant1, appId1); assertContent("/test.txt", "foo\n"); - assertThat(session2.status, is(Session.Status.PREPARE)); + assertThat(session.getStatus(), is(Session.Status.ACTIVATE)); } private void assertNotFound(HttpRequest request) { @@ -126,4 +123,9 @@ 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 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/SessionContentHandlerTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/SessionContentHandlerTest.java index 20d9be080e9..3ab56d3869a 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,10 +1,11 @@ // 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.ApplicationName; +import com.yahoo.config.provision.InstanceName; 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; @@ -13,6 +14,8 @@ 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; @@ -22,7 +25,7 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.nio.file.Files; +import java.time.Clock; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertNotNull; @@ -32,21 +35,31 @@ import static org.junit.Assert.assertThat; * @author Ulf Lilleengen */ public class SessionContentHandlerTest extends ContentHandlerTestBase { - private static final TenantName tenant = TenantName.from("contenttest"); + private static final TenantName tenantName = TenantName.from("contenttest"); + private static final File testApp = new File("src/test/apps/content"); private final TestComponentRegistry componentRegistry = new TestComponentRegistry.Builder().build(); private TenantRepository tenantRepository; private SessionContentHandler handler = null; - + private long sessionId; + @Before - public void setupHandler() throws Exception { + public void setupHandler() { tenantRepository = new TenantRepository(componentRegistry, false); - tenantRepository.addTenant(tenant); - tenantRepository.getTenant(tenant).getSessionRepository().addLocalSession(new MockLocalSession(1L, FilesApplicationPackage.fromFile(createTestApp()))); + 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(); + handler = createHandler(); - pathPrefix = "/application/v2/tenant/" + tenant + "/session/"; - baseUrl = "http://foo:1337/application/v2/tenant/" + tenant + "/session/1/content/"; + pathPrefix = "/application/v2/tenant/" + tenantName + "/session/"; + baseUrl = "http://foo:1337/application/v2/tenant/" + tenantName + "/session/" + sessionId + "/content/"; } @Test @@ -69,14 +82,14 @@ public class SessionContentHandlerTest extends ContentHandlerTestBase { @Test public void require_that_nonexistent_session_returns_not_found() { - HttpResponse response = doRequest(HttpRequest.Method.GET, "/test.txt", 2); + HttpResponse response = doRequest(HttpRequest.Method.GET, "/test.txt", 9999); 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, 1, data); + return doRequest(HttpRequest.Method.PUT, path, sessionId, data); } @Test @@ -94,13 +107,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 1 does not contain a file 'test2.txt'\"}"); + assertDeleteFile(Response.Status.NOT_FOUND, "/test2.txt", "{\"error-code\":\"NOT_FOUND\",\"message\":\"Session " + sessionId + " 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 1 does not contain a file '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.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"); @@ -109,10 +122,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 + "1/content/test.txt\"}"); + "{\"status\":\"new\",\"md5\":\"d3b07384d113edec49eaa6238ad5ff00\",\"name\":\"http://foo:1337" + pathPrefix + sessionId + "/content/test.txt\"}"); assertWriteFile("/test.txt", "Mycontent"); assertStatus("/test.txt?return=status", - "{\"status\":\"changed\",\"md5\":\"01eabd73c69d78d0009ec93cd62d7f77\",\"name\":\"http://foo:1337" + pathPrefix + "1/content/test.txt\"}"); + "{\"status\":\"changed\",\"md5\":\"01eabd73c69d78d0009ec93cd62d7f77\",\"name\":\"http://foo:1337" + pathPrefix + sessionId + "/content/test.txt\"}"); } private void assertWriteFile(String path, String content) throws IOException { @@ -121,11 +134,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 + "1/prepared\"}")); + is("{\"prepared\":\"http://foo:1337" + pathPrefix + sessionId + "/prepared\"}")); } private void assertDeleteFile(int statusCode, String filePath) throws IOException { - assertDeleteFile(statusCode, filePath, "{\"prepared\":\"http://foo:1337" + pathPrefix + "1/prepared\"}"); + assertDeleteFile(statusCode, filePath, "{\"prepared\":\"http://foo:1337" + pathPrefix + sessionId + "/prepared\"}"); } private void assertDeleteFile(int statusCode, String filePath, String expectedResponse) throws IOException { @@ -140,17 +153,11 @@ public class SessionContentHandlerTest extends ContentHandlerTestBase { assertNotNull(response); assertThat(response.getStatus(), is(Response.Status.OK)); assertThat(SessionHandlerTest.getRenderedString(response), - 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; + is("{\"prepared\":\"http://foo:1337" + pathPrefix + sessionId + "/prepared\"}")); } protected HttpResponse doRequest(HttpRequest.Method method, String path) { - return doRequest(method, path, 1); + return doRequest(method, path, sessionId); } private HttpResponse doRequest(HttpRequest.Method method, String path, long sessionId) { @@ -170,4 +177,9 @@ 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 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)); |