diff options
Diffstat (limited to 'configserver/src/test/java/com/yahoo')
4 files changed, 96 insertions, 79 deletions
diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/application/TenantApplicationsTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/application/TenantApplicationsTest.java index 1a14ac1761c..08cfa74da3b 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/application/TenantApplicationsTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/application/TenantApplicationsTest.java @@ -3,11 +3,11 @@ package com.yahoo.vespa.config.server.application; import com.yahoo.config.provision.ApplicationId; import com.yahoo.config.provision.TenantName; -import com.yahoo.path.Path; import com.yahoo.text.Utf8; import com.yahoo.vespa.config.server.MockReloadHandler; import com.yahoo.vespa.config.server.TestWithCurator; +import com.yahoo.vespa.config.server.tenant.Tenants; import org.junit.Test; import java.util.Arrays; @@ -18,60 +18,61 @@ import static org.hamcrest.Matchers.is; import static org.junit.Assert.*; /** - * @author lulf + * @author Ulf Lilleengen * @since 5.1 */ public class TenantApplicationsTest extends TestWithCurator { + private static final TenantName tenantName = TenantName.from("tenant"); + @Test public void require_that_applications_are_read_from_zookeeper() throws Exception { - curatorFramework.create().creatingParentsIfNeeded().forPath("/foo:dev:baz", Utf8.toAsciiBytes(3)); - curatorFramework.create().creatingParentsIfNeeded().forPath("/bar:test:bim", Utf8.toAsciiBytes(4)); + writeApplicationData(createApplicationId("foo"), 3L); + writeApplicationData(createApplicationId("bar"), 4L); TenantApplications repo = createZKAppRepo(); List<ApplicationId> applications = repo.listApplications(); assertThat(applications.size(), is(2)); - assertThat(applications.get(0).application().value(), is("dev")); - assertThat(applications.get(1).application().value(), is("test")); + assertThat(applications.get(0).application().value(), is("foo")); + assertThat(applications.get(1).application().value(), is("bar")); assertThat(repo.getSessionIdForApplication(applications.get(0)), is(3L)); assertThat(repo.getSessionIdForApplication(applications.get(1)), is(4L)); } @Test public void require_that_invalid_entries_are_skipped() throws Exception { - curatorFramework.create().creatingParentsIfNeeded().forPath("/foo:dev:baz"); - curatorFramework.create().creatingParentsIfNeeded().forPath("/invalid"); + writeApplicationData(createApplicationId("foo"), 3L); + writeApplicationData("invalid", 3L); TenantApplications repo = createZKAppRepo(); List<ApplicationId> applications = repo.listApplications(); assertThat(applications.size(), is(1)); - assertThat(applications.get(0).application().value(), is("dev")); + assertThat(applications.get(0).application().value(), is("foo")); } @Test(expected = IllegalArgumentException.class) public void require_that_requesting_session_for_unknown_application_throws_exception() throws Exception { - curatorFramework.create().creatingParentsIfNeeded().forPath("/foo:dev:baz:bim"); TenantApplications repo = createZKAppRepo(); - repo.getSessionIdForApplication(new ApplicationId.Builder() - .tenant("exist") - .applicationName("tenant").instanceName("here").build()); + repo.getSessionIdForApplication(createApplicationId("nonexistent")); } @Test(expected = IllegalArgumentException.class) public void require_that_requesting_session_for_empty_application_throws_exception() throws Exception { - curatorFramework.create().creatingParentsIfNeeded().forPath("/foo:dev:baz:bim"); + ApplicationId baz = createApplicationId("baz"); + // No data in node + curatorFramework.create().creatingParentsIfNeeded() + .forPath(Tenants.getApplicationsPath(tenantName).append(baz.serializedForm()).getAbsolute()); TenantApplications repo = createZKAppRepo(); - repo.getSessionIdForApplication(new ApplicationId.Builder() - .tenant("tenant") - .applicationName("foo").instanceName("bim").build()); + repo.getSessionIdForApplication(baz); } @Test public void require_that_application_ids_can_be_written() throws Exception { TenantApplications repo = createZKAppRepo(); - repo.createPutApplicationTransaction(createAppplicationId("myapp"), 3l).commit(); - String path = "/mytenant:myapp:myinst"; + ApplicationId myapp = createApplicationId("myapp"); + repo.createPutApplicationTransaction(myapp, 3l).commit(); + String path = Tenants.getApplicationsPath(tenantName).append(myapp.serializedForm()).getAbsolute(); assertTrue(curatorFramework.checkExists().forPath(path) != null); assertThat(Utf8.toString(curatorFramework.getData().forPath(path)), is("3")); - repo.createPutApplicationTransaction(createAppplicationId("myapp"), 5l).commit(); + repo.createPutApplicationTransaction(myapp, 5l).commit(); assertTrue(curatorFramework.checkExists().forPath(path) != null); assertThat(Utf8.toString(curatorFramework.getData().forPath(path)), is("5")); } @@ -79,8 +80,8 @@ public class TenantApplicationsTest extends TestWithCurator { @Test public void require_that_application_ids_can_be_deleted() throws Exception { TenantApplications repo = createZKAppRepo(); - ApplicationId id1 = createAppplicationId("myapp"); - ApplicationId id2 = createAppplicationId("myapp2"); + ApplicationId id1 = createApplicationId("myapp"); + ApplicationId id2 = createApplicationId("myapp2"); repo.createPutApplicationTransaction(id1, 1).commit(); repo.createPutApplicationTransaction(id2, 1).commit(); assertThat(repo.listApplications().size(), is(2)); @@ -95,8 +96,8 @@ public class TenantApplicationsTest extends TestWithCurator { TenantApplications zkRepo = createZKAppRepo(); TenantApplications memRepo = new MemoryTenantApplications(); for (TenantApplications repo : Arrays.asList(zkRepo, memRepo)) { - ApplicationId id1 = createAppplicationId("myapp"); - ApplicationId id2 = createAppplicationId("myapp2"); + ApplicationId id1 = createApplicationId("myapp"); + ApplicationId id2 = createApplicationId("myapp2"); repo.createPutApplicationTransaction(id1, 4).commit(); repo.createPutApplicationTransaction(id2, 5).commit(); List<ApplicationId> lst = repo.listApplications(); @@ -122,21 +123,19 @@ public class TenantApplicationsTest extends TestWithCurator { @Test public void require_that_reload_handler_is_called_when_apps_are_removed() throws Exception { - curatorFramework.create().creatingParentsIfNeeded().forPath("/foo:test:baz", Utf8.toAsciiBytes(3)); - curatorFramework.create().creatingParentsIfNeeded().forPath("/bar:dev:bim", Utf8.toAsciiBytes(4)); + ApplicationId foo = createApplicationId("foo"); + writeApplicationData(foo, 3L); + writeApplicationData(createApplicationId("bar"), 4L); MockReloadHandler reloadHandler = new MockReloadHandler(); TenantApplications repo = createZKAppRepo(reloadHandler); assertNull(reloadHandler.lastRemoved); - repo.deleteApplication(new ApplicationId.Builder() - .tenant("foo") - .applicationName("test").instanceName("baz").build()) - .commit(); + repo.deleteApplication(foo).commit(); long endTime = System.currentTimeMillis() + 60_000; while (System.currentTimeMillis() < endTime && reloadHandler.lastRemoved == null) { Thread.sleep(100); } assertNotNull(reloadHandler.lastRemoved); - assertThat(reloadHandler.lastRemoved.serializedForm(), is("foo:test:baz")); + assertThat(reloadHandler.lastRemoved.serializedForm(), is(foo.serializedForm())); } private TenantApplications createZKAppRepo() { @@ -144,12 +143,26 @@ public class TenantApplicationsTest extends TestWithCurator { } private TenantApplications createZKAppRepo(MockReloadHandler reloadHandler) { - return ZKTenantApplications.create(curator, Path.createRoot(), reloadHandler, TenantName.from("mytenant")); + return ZKTenantApplications.create(curator, reloadHandler, tenantName); } - private static ApplicationId createAppplicationId(String name) { + private static ApplicationId createApplicationId(String name) { return new ApplicationId.Builder() - .tenant("mytenant") - .applicationName(name).instanceName("myinst").build(); + .tenant(tenantName.value()) + .applicationName(name) + .instanceName("myinst") + .build(); + } + + private void writeApplicationData(ApplicationId applicationId, long sessionId) throws Exception { + writeApplicationData(applicationId.serializedForm(), sessionId); + } + + private void writeApplicationData(String applicationId, long sessionId) throws Exception { + curatorFramework + .create() + .creatingParentsIfNeeded() + .forPath(Tenants.getApplicationsPath(tenantName).append(applicationId).getAbsolute(), + Utf8.toAsciiBytes(sessionId)); } } diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/TestTenantBuilder.java b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/TestTenantBuilder.java index 959dfab1bee..16ce605d4d1 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/TestTenantBuilder.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/TestTenantBuilder.java @@ -4,8 +4,8 @@ package com.yahoo.vespa.config.server.http.v2; import com.google.common.base.Function; import com.google.common.collect.Collections2; import com.yahoo.config.provision.TenantName; -import com.yahoo.path.Path; -import com.yahoo.vespa.config.server.*; +import com.yahoo.vespa.config.server.GlobalComponentRegistry; +import com.yahoo.vespa.config.server.TestComponentRegistry; import com.yahoo.vespa.config.server.application.MemoryTenantApplications; import com.yahoo.vespa.config.server.session.LocalSessionRepo; import com.yahoo.vespa.config.server.session.RemoteSessionRepo; @@ -18,7 +18,7 @@ import java.util.*; /** * Test utility for creating tenants used for testing and setup wiring of tenant stuff. * - * @author lulf + * @author Ulf Lilleengen * @since 5.1 */ public class TestTenantBuilder { @@ -32,7 +32,7 @@ public class TestTenantBuilder { public TenantBuilder createTenant(TenantName tenantName) { MemoryTenantApplications applicationRepo = new MemoryTenantApplications(); - TenantBuilder builder = TenantBuilder.create(componentRegistry, tenantName, Path.createRoot().append(tenantName.value())) + TenantBuilder builder = TenantBuilder.create(componentRegistry, tenantName) .withSessionFactory(new SessionCreateHandlerTest.MockSessionFactory()) .withLocalSessionRepo(new LocalSessionRepo(componentRegistry.getClock())) .withRemoteSessionRepo(new RemoteSessionRepo()) diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/session/LocalSessionRepoTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/session/LocalSessionRepoTest.java index 5753b2959f7..3d34d08edeb 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/session/LocalSessionRepoTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/session/LocalSessionRepoTest.java @@ -2,10 +2,11 @@ package com.yahoo.vespa.config.server.session; import com.yahoo.config.model.application.provider.FilesApplicationPackage; -import com.yahoo.path.Path; import com.yahoo.test.ManualClock; -import com.yahoo.vespa.config.server.*; import com.yahoo.config.provision.TenantName; +import com.yahoo.vespa.config.server.GlobalComponentRegistry; +import com.yahoo.vespa.config.server.TestComponentRegistry; +import com.yahoo.vespa.config.server.TestWithCurator; import com.yahoo.vespa.config.server.application.MemoryTenantApplications; import com.yahoo.vespa.config.server.deploy.TenantFileSystemDirs; import com.yahoo.io.IOUtils; @@ -20,13 +21,12 @@ import java.io.File; import java.time.Duration; import java.time.Instant; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; /** - * @author lulf + * @author Ulf Lilleengen * @since 5.1 */ public class LocalSessionRepoTest extends TestWithCurator { @@ -51,10 +51,7 @@ public class LocalSessionRepoTest extends TestWithCurator { } clock = new ManualClock(Instant.ofEpochSecond(1)); LocalSessionLoader loader = new SessionFactoryImpl(globalComponentRegistry, - new SessionCounter(globalComponentRegistry.getCurator(), - Path.fromString("counter"), - Path.fromString("sessions")), - Path.createRoot(), + new SessionCounter(globalComponentRegistry.getCurator(), tenantName), new MemoryTenantApplications(), tenantFileSystemDirs, new HostRegistry<>(), tenantName); diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/session/RemoteSessionRepoTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/session/RemoteSessionRepoTest.java index 462062ce8a8..878339bd703 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/session/RemoteSessionRepoTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/session/RemoteSessionRepoTest.java @@ -2,18 +2,22 @@ package com.yahoo.vespa.config.server.session; import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.*; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThat; import com.yahoo.config.provision.ApplicationId; import com.yahoo.config.provision.TenantName; import com.yahoo.path.Path; import com.yahoo.text.Utf8; import com.yahoo.transaction.Transaction; -import com.yahoo.vespa.config.server.*; +import com.yahoo.vespa.config.server.TestComponentRegistry; +import com.yahoo.vespa.config.server.TestWithCurator; import com.yahoo.vespa.config.server.application.TenantApplications; import com.yahoo.vespa.config.server.tenant.Tenant; import com.yahoo.vespa.config.server.tenant.TenantBuilder; +import com.yahoo.vespa.config.server.tenant.Tenants; import com.yahoo.vespa.curator.Curator; import org.junit.Before; import org.junit.Test; @@ -27,33 +31,35 @@ import java.util.concurrent.TimeUnit; import java.util.function.LongPredicate; /** - * @author lulf + * @author Ulf Lilleengen * @since 5.1 */ public class RemoteSessionRepoTest extends TestWithCurator { + private static final TenantName tenantName = TenantName.defaultName(); + private RemoteSessionRepo remoteSessionRepo; @Before public void setupFacade() throws Exception { - createSession(2l, false); - createSession(3l, false); - curator.create(Path.fromString("/applications")); - curator.create(Path.fromString("/sessions")); - Tenant tenant = TenantBuilder.create(new TestComponentRegistry.Builder().curator(curator).build(), - TenantName.defaultName(), - Path.createRoot()).build(); + Tenant tenant = TenantBuilder.create(new TestComponentRegistry.Builder() + .curator(curator) + .build(), + tenantName) + .build(); this.remoteSessionRepo = tenant.getRemoteSessionRepo(); + curator.create(Tenants.getTenantPath(tenantName).append("/applications")); + curator.create(Tenants.getSessionsPath(tenantName)); + createSession(1l, false); + createSession(2l, false); } private void createSession(long sessionId, boolean wait) { - createSession("", sessionId, wait); + createSession(sessionId, wait, tenantName); } - - private void createSession(String root, long sessionId, boolean wait) { - Path sessionsPath = Path.fromString(root).append("sessions"); - curator.create(sessionsPath); + private void createSession(long sessionId, boolean wait, TenantName tenantName) { + Path sessionsPath = Tenants.getSessionsPath(tenantName); SessionZooKeeperClient zkc = new SessionZooKeeperClient(curator, sessionsPath.append(String.valueOf(sessionId))); zkc.createNewSession(System.currentTimeMillis(), TimeUnit.MILLISECONDS); if (wait) { @@ -64,27 +70,28 @@ public class RemoteSessionRepoTest extends TestWithCurator { @Test public void testInitialize() { + assertSessionExists(1l); assertSessionExists(2l); - assertSessionExists(3l); } @Test public void testCreateSession() throws Exception { - createSession(0l, true); - assertSessionExists(0l); + createSession(3l, true); + assertSessionExists(3l); } @Test public void testSessionStateChange() throws Exception { - Path session = Path.fromString("/sessions/0"); - createSession(0l, true); - assertSessionStatus(0l, Session.Status.NEW); - assertStatusChange(0l, Session.Status.PREPARE); - assertStatusChange(0l, Session.Status.ACTIVATE); + long sessionId = 3L; + createSession(sessionId, true); + assertSessionStatus(sessionId, Session.Status.NEW); + assertStatusChange(sessionId, Session.Status.PREPARE); + assertStatusChange(sessionId, Session.Status.ACTIVATE); + Path session = Tenants.getSessionsPath(tenantName).append("" + sessionId); curator.delete(session); - assertSessionRemoved(0l); - assertNull(remoteSessionRepo.getSession(0l)); + assertSessionRemoved(sessionId); + assertNull(remoteSessionRepo.getSession(sessionId)); } // If reading a session throws an exception it should be handled and not prevent other applications @@ -93,25 +100,25 @@ public class RemoteSessionRepoTest extends TestWithCurator { // throw an exception). @Test public void testBadApplicationRepoOnActivate() throws Exception { + long sessionId = 3L; TenantApplications applicationRepo = new FailingTenantApplications(); - curator.framework().create().forPath("/mytenant"); - Tenant tenant = TenantBuilder.create(new TestComponentRegistry.Builder().curator(curator).build(), - TenantName.from("mytenant"), - Path.fromString("mytenant")) + TenantName mytenant = TenantName.from("mytenant"); + Tenant tenant = TenantBuilder.create(new TestComponentRegistry.Builder().curator(curator).build(), mytenant) .withApplicationRepo(applicationRepo) .build(); + curator.create(Tenants.getSessionsPath(mytenant)); remoteSessionRepo = tenant.getRemoteSessionRepo(); assertThat(remoteSessionRepo.listSessions().size(), is(0)); - createSession("/mytenant", 2l, true); + createSession(sessionId, true, mytenant); assertThat(remoteSessionRepo.listSessions().size(), is(1)); } private void assertStatusChange(long sessionId, Session.Status status) throws Exception { - Path statePath = Path.fromString("/sessions/" + sessionId).append(ConfigCurator.SESSIONSTATE_ZK_SUBPATH); + Path statePath = Tenants.getSessionsPath(tenantName).append("" + sessionId).append(ConfigCurator.SESSIONSTATE_ZK_SUBPATH); curator.create(statePath); curatorFramework.setData().forPath(statePath.getAbsolute(), Utf8.toBytes(status.toString())); System.out.println("Setting status " + status + " for " + sessionId); - assertSessionStatus(0l, status); + assertSessionStatus(sessionId, status); } private void assertSessionRemoved(long sessionId) { |