diff options
16 files changed, 254 insertions, 316 deletions
diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/tenant/TenantBuilder.java b/configserver/src/main/java/com/yahoo/vespa/config/server/tenant/TenantBuilder.java index daa0b4f4830..95a507ede21 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/tenant/TenantBuilder.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/tenant/TenantBuilder.java @@ -197,12 +197,9 @@ public class TenantBuilder { } } - - public LocalSessionRepo getLocalSessionRepo() { - return localSessionRepo; - } - public TenantApplications getApplicationRepo() { return applicationRepo; } + + public TenantName getTenantName() { return tenant; } } diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/tenant/TenantRepository.java b/configserver/src/main/java/com/yahoo/vespa/config/server/tenant/TenantRepository.java index caa15699bbf..8f4a90ef85f 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/tenant/TenantRepository.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/tenant/TenantRepository.java @@ -27,6 +27,7 @@ import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -70,16 +71,27 @@ public class TenantRepository implements ConnectionStateListener, PathChildrenCa private final MetricUpdater metricUpdater; private final ExecutorService pathChildrenExecutor = Executors.newFixedThreadPool(1, ThreadFactoryFactory.getThreadFactory(TenantRepository.class.getName())); private final ScheduledExecutorService checkForRemovedApplicationsService = new ScheduledThreadPoolExecutor(1); - private final Curator.DirectoryCache directoryCache; + private final Optional<Curator.DirectoryCache> directoryCache; /** - * New instance from the tenants in the given component registry's ZooKeeper data. + * Creates a new tenant repository * * @param globalComponentRegistry a {@link com.yahoo.vespa.config.server.GlobalComponentRegistry} */ @Inject public TenantRepository(GlobalComponentRegistry globalComponentRegistry) { + this(globalComponentRegistry, true); + } + + /** + * Creates a new tenant repository + * + * @param globalComponentRegistry a {@link com.yahoo.vespa.config.server.GlobalComponentRegistry} + * @param useZooKeeperWatchForTenantChanges set to false for tests where you want to control adding and deleting + * tenants yourself + */ + public TenantRepository(GlobalComponentRegistry globalComponentRegistry, boolean useZooKeeperWatchForTenantChanges) { this.globalComponentRegistry = globalComponentRegistry; this.curator = globalComponentRegistry.getCurator(); metricUpdater = globalComponentRegistry.getMetrics().getOrCreateMetricUpdater(Collections.emptyMap()); @@ -90,9 +102,13 @@ public class TenantRepository implements ConnectionStateListener, PathChildrenCa createSystemTenants(globalComponentRegistry.getConfigserverConfig()); curator.create(vespaPath); - this.directoryCache = curator.createDirectoryCache(tenantsPath.getAbsolute(), false, false, pathChildrenExecutor); - directoryCache.start(); - directoryCache.addListener(this); + if (useZooKeeperWatchForTenantChanges) { + this.directoryCache = Optional.of(curator.createDirectoryCache(tenantsPath.getAbsolute(), false, false, pathChildrenExecutor)); + this.directoryCache.get().start(); + this.directoryCache.get().addListener(this); + } else { + this.directoryCache = Optional.empty(); + } log.log(LogLevel.DEBUG, "Creating all tenants"); createTenants(); notifyTenantsLoaded(); @@ -103,42 +119,20 @@ public class TenantRepository implements ConnectionStateListener, PathChildrenCa TimeUnit.SECONDS); } - /** - * New instance containing the given tenants. Creates no system tenants and no Zookeeper watches. For testing only. - * @param globalComponentRegistry a {@link com.yahoo.vespa.config.server.GlobalComponentRegistry} instance - * @param tenants a collection of {@link Tenant}s - */ - // TODO: Get rid of the second argument and let callers use addTenant() instead - public TenantRepository(GlobalComponentRegistry globalComponentRegistry, Collection<Tenant> tenants) { - this.globalComponentRegistry = globalComponentRegistry; - this.curator = globalComponentRegistry.getCurator(); - metricUpdater = globalComponentRegistry.getMetrics().getOrCreateMetricUpdater(Collections.emptyMap()); - this.tenantListeners.add(globalComponentRegistry.getTenantListener()); - curator.create(tenantsPath); - this.directoryCache = curator.createDirectoryCache(tenantsPath.getAbsolute(), false, false, pathChildrenExecutor); - this.tenants.putAll(addTenants(tenants)); - } - private void notifyTenantsLoaded() { for (TenantListener tenantListener : tenantListeners) { tenantListener.onTenantsLoaded(); } } - // Pre-condition: tenants path needs to exist in zk - private LinkedHashMap<TenantName, Tenant> addTenants(Collection<Tenant> newTenants) { - LinkedHashMap<TenantName, Tenant> tenants = new LinkedHashMap<>(); - for (Tenant t : newTenants) { - tenants.put(t.getName(), t); - } - log.log(LogLevel.DEBUG, "TenantRepository at startup: " + tenants); - metricUpdater.setTenants(this.tenants.size()); - return tenants; - } - public synchronized void addTenant(TenantName tenantName) { - writeTenantPath(tenantName); - createTenant(tenantName); + addTenant(TenantBuilder.create(globalComponentRegistry, tenantName)); + } + + // For testing + public synchronized void addTenant(TenantBuilder builder) { + writeTenantPath(builder.getTenantName()); + createTenant(builder); } /** @@ -186,11 +180,17 @@ public class TenantRepository implements ConnectionStateListener, PathChildrenCa } private void createTenant(TenantName tenantName) { + createTenant(TenantBuilder.create(globalComponentRegistry, tenantName)); + } + + // TODO: Fix exception handling and make method return tenant + private void createTenant(TenantBuilder builder) { + TenantName tenantName = builder.getTenantName(); if (tenants.containsKey(tenantName)) return; try { - log.log(LogLevel.DEBUG, "Creating tenant '" + tenantName + "'"); - Tenant tenant = TenantBuilder.create(globalComponentRegistry, tenantName).build(); + log.log(LogLevel.INFO, "Creating tenant '" + tenantName + "'"); + Tenant tenant = builder.build(); notifyNewTenant(tenant); tenants.putIfAbsent(tenantName, tenant); } catch (Exception e) { @@ -340,7 +340,7 @@ public class TenantRepository implements ConnectionStateListener, PathChildrenCa } public void close() { - directoryCache.close(); + directoryCache.ifPresent(Curator.DirectoryCache::close); pathChildrenExecutor.shutdown(); checkForRemovedApplicationsService.shutdown(); } diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/deploy/DeployTester.java b/configserver/src/test/java/com/yahoo/vespa/config/server/deploy/DeployTester.java index a3bea6b6c86..266833ac7f3 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/deploy/DeployTester.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/deploy/DeployTester.java @@ -106,7 +106,7 @@ public class DeployTester { provisioner); try { this.testApp = new File(appPath); - this.tenantRepository = new TenantRepository(componentRegistry, Collections.emptySet()); + this.tenantRepository = new TenantRepository(componentRegistry); tenantRepository.addTenant(tenantName); } catch (Exception e) { 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 c34dbe76a43..221d134c0f5 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 @@ -6,12 +6,15 @@ import com.yahoo.config.provision.TenantName; import com.yahoo.config.provision.Zone; import com.yahoo.container.jdisc.HttpResponse; import com.yahoo.container.jdisc.HttpRequest; -import com.yahoo.container.logging.AccessLog; import com.yahoo.jdisc.Response; import com.yahoo.config.provision.ApplicationId; import com.yahoo.vespa.config.server.ApplicationRepository; +import com.yahoo.vespa.config.server.TestComponentRegistry; import com.yahoo.vespa.config.server.http.ContentHandlerTestBase; import com.yahoo.vespa.config.server.session.Session; +import com.yahoo.vespa.config.server.tenant.Tenant; +import com.yahoo.vespa.config.server.tenant.TenantBuilder; +import com.yahoo.vespa.config.server.tenant.TenantRepository; import org.junit.Before; import org.junit.Test; @@ -24,39 +27,46 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; /** - * @author lulf - * @since 5.1 + * @author Ulf Lilleengen */ public class ApplicationContentHandlerTest extends ContentHandlerTestBase { + private final TestComponentRegistry componentRegistry = new TestComponentRegistry.Builder().build(); + private final Clock clock = componentRegistry.getClock(); private ApplicationHandler handler; - private TenantName tenant1 = TenantName.from("mofet"); - private TenantName tenant2 = TenantName.from("bla"); + 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(tenant1) + .tenant(tenantName1) .applicationName("foo").instanceName("quux").build(); private ApplicationId idTenant2 = new ApplicationId.Builder() - .tenant(tenant2) + .tenant(tenantName2) .applicationName("foo").instanceName("quux").build(); private MockSession session2; @Before - public void setupHandler() throws Exception { - TestTenantBuilder testTenantBuilder = new TestTenantBuilder(); - testTenantBuilder.createTenant(tenant1); - testTenantBuilder.createTenant(tenant2); + public void setupHandler() { + TenantRepository tenantRepository = new TenantRepository(componentRegistry, false); + tenantRepository.addTenant(TenantBuilder.create(componentRegistry, tenantName1)); + tenantRepository.addTenant(TenantBuilder.create(componentRegistry, tenantName2)); + session2 = new MockSession(2l, FilesApplicationPackage.fromFile(new File("src/test/apps/content"))); - testTenantBuilder.tenants().get(tenant1).getLocalSessionRepo().addSession(session2); - testTenantBuilder.tenants().get(tenant2).getLocalSessionRepo().addSession(new MockSession(3l, FilesApplicationPackage.fromFile(new File("src/test/apps/content2")))); - testTenantBuilder.tenants().get(tenant1).getApplicationRepo().createPutApplicationTransaction(idTenant1, 2l).commit(); - testTenantBuilder.tenants().get(tenant2).getApplicationRepo().createPutApplicationTransaction(idTenant2, 3l).commit(); + Tenant tenant1 = tenantRepository.getTenant(tenantName1); + tenant1.getLocalSessionRepo().addSession(session2); + tenant1.getApplicationRepo().createPutApplicationTransaction(idTenant1, 2l).commit(); + + MockSession session3 = new MockSession(3l, FilesApplicationPackage.fromFile(new File("src/test/apps/content2"))); + Tenant tenant2 = tenantRepository.getTenant(tenantName2); + tenant2.getLocalSessionRepo().addSession(session3); + tenant2.getApplicationRepo().createPutApplicationTransaction(idTenant2, 3l).commit(); + handler = new ApplicationHandler(ApplicationHandler.testOnlyContext(), Zone.defaultZone(), - new ApplicationRepository(testTenantBuilder.createTenants(), + new ApplicationRepository(tenantRepository, new MockProvisioner(), - Clock.systemUTC())); + clock)); pathPrefix = createPath(idTenant1, Zone.defaultZone()); baseUrl = baseServer + pathPrefix; } @@ -76,7 +86,7 @@ public class ApplicationContentHandlerTest extends ContentHandlerTestBase { } @Test - public void require_that_nonexistant_application_returns_not_found() throws IOException { + public void require_that_nonexistant_application_returns_not_found() { assertNotFound(HttpRequest.createTestRequest(baseServer + createPath(new ApplicationId.Builder() .tenant("tenant") .applicationName("notexist").instanceName("baz").build(), Zone.defaultZone()), diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandlerTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandlerTest.java index fada72f6e8f..06c64acab33 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandlerTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ApplicationHandlerTest.java @@ -35,6 +35,7 @@ import com.yahoo.vespa.config.server.session.RemoteSession; import com.yahoo.vespa.config.server.session.SessionContext; import com.yahoo.vespa.config.server.session.SessionZooKeeperClient; import com.yahoo.vespa.config.server.tenant.Tenant; +import com.yahoo.vespa.config.server.tenant.TenantBuilder; import com.yahoo.vespa.config.server.tenant.TenantRepository; import com.yahoo.vespa.curator.mock.MockCurator; import com.yahoo.vespa.model.VespaModelFactory; @@ -80,11 +81,17 @@ public class ApplicationHandlerTest { @Before public void setup() { - TestTenantBuilder testBuilder = new TestTenantBuilder(); - testBuilder.createTenant(mytenantName).withReloadHandler(new MockReloadHandler()); - testBuilder.createTenant(foobar).withReloadHandler(new MockReloadHandler()); + TestComponentRegistry componentRegistry = new TestComponentRegistry.Builder().build(); + tenantRepository = new TenantRepository(componentRegistry, false); + + TenantBuilder tenantBuilder1 = TenantBuilder.create(componentRegistry, mytenantName) + .withReloadHandler(new MockReloadHandler()); + tenantRepository.addTenant(tenantBuilder1); + + TenantBuilder tenantBuilder2 = TenantBuilder.create(componentRegistry, foobar) + .withReloadHandler(new MockReloadHandler()); + tenantRepository.addTenant(tenantBuilder2); - tenantRepository = testBuilder.createTenants(); provisioner = new SessionHandlerTest.MockProvisioner(); mockHandler = createMockApplicationHandler( provisioner, 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 4d4b03e2d4c..537ab5a9f3e 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 @@ -10,6 +10,7 @@ import com.yahoo.vespa.config.server.host.HostRegistries; 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.tenant.TenantBuilder; import com.yahoo.vespa.config.server.tenant.TenantRepository; import org.junit.Before; import org.junit.Test; @@ -36,11 +37,12 @@ public class HostHandlerTest { private HostHandler hostHandler; @Before - public void setup() throws Exception { - TestTenantBuilder testBuilder = new TestTenantBuilder(); - testBuilder.createTenant(mytenant).withReloadHandler(new MockReloadHandler()); - - tenantRepository = testBuilder.createTenants(); + public void setup() { + TestComponentRegistry componentRegistry = new TestComponentRegistry.Builder().build(); + tenantRepository = new TenantRepository(componentRegistry, false); + TenantBuilder tb = TenantBuilder.create(componentRegistry, mytenant) + .withReloadHandler(new MockReloadHandler()); + tenantRepository.addTenant(tb); handler = createHostHandler(); } diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/HttpGetConfigHandlerTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/HttpGetConfigHandlerTest.java index 463574e08e8..5226ff38ce3 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/HttpGetConfigHandlerTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/HttpGetConfigHandlerTest.java @@ -12,7 +12,9 @@ import java.util.Collections; import java.util.HashSet; import com.yahoo.config.provision.TenantName; +import com.yahoo.vespa.config.server.TestComponentRegistry; import com.yahoo.vespa.config.server.http.HttpErrorResponse; +import com.yahoo.vespa.config.server.tenant.TenantBuilder; import com.yahoo.vespa.config.server.tenant.TenantRepository; import org.junit.Before; import org.junit.Test; @@ -40,17 +42,15 @@ public class HttpGetConfigHandlerTest { private HttpGetConfigHandler handler; @Before - public void setUp() throws Exception { + public void setUp() { mockRequestHandler = new MockRequestHandler(); mockRequestHandler.setAllConfigs(new HashSet<ConfigKey<?>>() {{ add(new ConfigKey<>("bar", "myid", "foo")); - }} ); - TestTenantBuilder tb = new TestTenantBuilder(); - tb.createTenant(tenant).withRequestHandler(mockRequestHandler).build(); - TenantRepository tenantRepository = tb.createTenants(); - handler = new HttpGetConfigHandler( - HttpGetConfigHandler.testOnlyContext(), - tenantRepository); + }} ); + TestComponentRegistry componentRegistry = new TestComponentRegistry.Builder().build(); + TenantRepository tenantRepository = new TenantRepository(componentRegistry, false); + tenantRepository.addTenant(TenantBuilder.create(componentRegistry, tenant).withRequestHandler(mockRequestHandler)); + handler = new HttpGetConfigHandler(HttpGetConfigHandler.testOnlyContext(), tenantRepository); } @Test diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/HttpListConfigsHandlerTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/HttpListConfigsHandlerTest.java index 365fb3ed1f0..750ad1c9fc0 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/HttpListConfigsHandlerTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/HttpListConfigsHandlerTest.java @@ -6,7 +6,9 @@ import com.yahoo.config.provision.Zone; import com.yahoo.container.jdisc.HttpRequest; import com.yahoo.container.jdisc.HttpResponse; import com.yahoo.vespa.config.ConfigKey; +import com.yahoo.vespa.config.server.TestComponentRegistry; import com.yahoo.vespa.config.server.rpc.MockRequestHandler; +import com.yahoo.vespa.config.server.tenant.TenantBuilder; import com.yahoo.vespa.config.server.tenant.TenantRepository; import com.yahoo.vespa.config.server.http.HandlerTest; import com.yahoo.vespa.config.server.http.HttpErrorResponse; @@ -26,30 +28,33 @@ import static com.yahoo.jdisc.http.HttpResponse.Status.*; import static com.yahoo.jdisc.http.HttpRequest.Method.GET; /** - * @author lulf - * @since 5.1 + * @author Ulf Lilleengen */ public class HttpListConfigsHandlerTest { - + + private final TestComponentRegistry componentRegistry = new TestComponentRegistry.Builder().build(); + private MockRequestHandler mockRequestHandler; private HttpListConfigsHandler handler; private HttpListNamedConfigsHandler namedHandler; @Before - public void setUp() throws Exception { + public void setUp() { mockRequestHandler = new MockRequestHandler(); mockRequestHandler.setAllConfigs(new HashSet<ConfigKey<?>>() {{ add(new ConfigKey<>("bar", "conf/id", "foo")); }} ); - TestTenantBuilder tb = new TestTenantBuilder(); - tb.createTenant(TenantName.from("mytenant")).withRequestHandler(mockRequestHandler).build(); - TenantRepository tenantRepository = tb.createTenants(); - handler = new HttpListConfigsHandler( - HttpListConfigsHandler.testOnlyContext(), - tenantRepository, Zone.defaultZone()); - namedHandler = new HttpListNamedConfigsHandler( - HttpListConfigsHandler.testOnlyContext(), - tenantRepository, Zone.defaultZone()); + TenantName tenantName = TenantName.from("mytenant"); + TenantRepository tenantRepository = new TenantRepository(componentRegistry, false); + TenantBuilder tenantBuilder = TenantBuilder.create(componentRegistry, tenantName) + .withRequestHandler(mockRequestHandler); + tenantRepository.addTenant(tenantBuilder); + handler = new HttpListConfigsHandler(HttpListConfigsHandler.testOnlyContext(), + tenantRepository, + Zone.defaultZone()); + namedHandler = new HttpListNamedConfigsHandler(HttpListConfigsHandler.testOnlyContext(), + tenantRepository, + Zone.defaultZone()); } @Test diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ListApplicationsHandlerTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ListApplicationsHandlerTest.java index eeaed92bd65..f57e7f09b39 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ListApplicationsHandlerTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/ListApplicationsHandlerTest.java @@ -6,8 +6,10 @@ import com.yahoo.container.jdisc.HttpRequest; import com.yahoo.container.jdisc.HttpResponse; import com.yahoo.jdisc.http.HttpRequest.Method; import com.yahoo.jdisc.Response; +import com.yahoo.vespa.config.server.TestComponentRegistry; import com.yahoo.vespa.config.server.application.TenantApplications; import com.yahoo.vespa.config.server.http.SessionHandlerTest; +import com.yahoo.vespa.config.server.tenant.TenantBuilder; import com.yahoo.vespa.config.server.tenant.TenantRepository; import org.junit.Test; import org.junit.Before; @@ -21,27 +23,27 @@ import static org.junit.Assert.assertThat; import static com.yahoo.jdisc.http.HttpRequest.Method.*; /** - * @author lulf - * @since 5.1 + * @author Ulf Lilleengen */ public class ListApplicationsHandlerTest { + private static final TenantName mytenant = TenantName.from("mytenant"); + private static final TenantName foobar = TenantName.from("foobar"); + + private final TestComponentRegistry componentRegistry = new TestComponentRegistry.Builder().build(); + private TenantApplications applicationRepo, applicationRepo2; private ListApplicationsHandler handler; @Before - public void setup() throws Exception { - TestTenantBuilder testBuilder = new TestTenantBuilder(); - TenantName mytenant = TenantName.from("mytenant"); - TenantName foobar = TenantName.from("foobar"); - testBuilder.createTenant(mytenant); - testBuilder.createTenant(foobar); - applicationRepo = testBuilder.tenants().get(mytenant).getApplicationRepo(); - applicationRepo2 = testBuilder.tenants().get(foobar).getApplicationRepo(); - TenantRepository tenantRepository = testBuilder.createTenants(); - handler = new ListApplicationsHandler( - ListApplicationsHandler.testOnlyContext(), - tenantRepository, - new Zone(Environment.dev, RegionName.from("us-east"))); + public void setup() { + TenantRepository tenantRepository = new TenantRepository(componentRegistry, false); + tenantRepository.addTenant(TenantBuilder.create(componentRegistry, mytenant)); + tenantRepository.addTenant(TenantBuilder.create(componentRegistry, foobar)); + applicationRepo = tenantRepository.getTenant(mytenant).getApplicationRepo(); + applicationRepo2 = tenantRepository.getTenant(foobar).getApplicationRepo(); + handler = new ListApplicationsHandler(ListApplicationsHandler.testOnlyContext(), + tenantRepository, + new Zone(Environment.dev, RegionName.from("us-east"))); } @Test diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/SessionActiveHandlerTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/SessionActiveHandlerTest.java index da21dccf580..e14e59b9fe7 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/SessionActiveHandlerTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/SessionActiveHandlerTest.java @@ -39,9 +39,9 @@ import com.yahoo.vespa.config.server.session.RemoteSession; import com.yahoo.vespa.config.server.session.RemoteSessionRepo; import com.yahoo.vespa.config.server.session.Session; import com.yahoo.vespa.config.server.session.SessionContext; -import com.yahoo.vespa.config.server.session.SessionFactory; import com.yahoo.vespa.config.server.session.SessionTest; import com.yahoo.vespa.config.server.session.SessionZooKeeperClient; +import com.yahoo.vespa.config.server.tenant.TenantBuilder; import com.yahoo.vespa.config.server.tenant.TenantRepository; import com.yahoo.vespa.config.server.zookeeper.ConfigCurator; import com.yahoo.vespa.curator.Curator; @@ -86,6 +86,8 @@ public class SessionActiveHandlerTest extends SessionHandlerTest { private MockProvisioner hostProvisioner; private VespaModelFactory modelFactory; private TestComponentRegistry componentRegistry; + private TenantRepository tenantRepository; + private SessionActiveHandler handler; @Before public void setup() { @@ -102,6 +104,14 @@ public class SessionActiveHandlerTest extends SessionHandlerTest { .configCurator(configCurator) .modelFactoryRegistry(new ModelFactoryRegistry(Collections.singletonList(modelFactory))) .build(); + TenantBuilder tenantBuilder = TenantBuilder.create(componentRegistry, tenantName) + .withSessionFactory(new MockSessionFactory()) + .withLocalSessionRepo(localRepo) + .withRemoteSessionRepo(remoteSessionRepo) + .withApplicationRepo(applicationRepo); + tenantRepository = new TenantRepository(componentRegistry, false); + tenantRepository.addTenant(tenantBuilder); + handler = createHandler(); } @Test @@ -119,7 +129,7 @@ public class SessionActiveHandlerTest extends SessionHandlerTest { @Test public void testUnknownSession() { - HttpResponse response = createHandler().handle(SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.PUT, Cmd.ACTIVE, 9999L, "?timeout=1.0")); + HttpResponse response = handler.handle(SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.PUT, Cmd.ACTIVE, 9999L, "?timeout=1.0")); assertEquals(response.getStatus(), NOT_FOUND); } @@ -152,7 +162,7 @@ public class SessionActiveHandlerTest extends SessionHandlerTest { @Test public void testAlreadyActivatedSession() throws Exception { activateAndAssertOK(1, 0); - HttpResponse response = createHandler().handle(SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.PUT, Cmd.ACTIVE, 1l)); + HttpResponse response = handler.handle(SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.PUT, Cmd.ACTIVE, 1l)); String message = getRenderedString(response); assertThat(message, response.getStatus(), Is.is(BAD_REQUEST)); assertThat(message, containsString("Session 1 is already active")); @@ -245,7 +255,7 @@ public class SessionActiveHandlerTest extends SessionHandlerTest { } private void testUnsupportedMethod(com.yahoo.container.jdisc.HttpRequest request) throws Exception { - HttpResponse response = createHandler().handle(request); + HttpResponse response = handler.handle(request); HandlerTest.assertHttpStatusCodeErrorCodeAndMessage(response, METHOD_NOT_ALLOWED, HttpErrorResponse.errorCodes.METHOD_NOT_ALLOWED, @@ -256,7 +266,6 @@ public class SessionActiveHandlerTest extends SessionHandlerTest { private long sessionId; private RemoteSession session; - private SessionHandler handler; private HttpResponse actResponse; private Session.Status initialStatus; private DeployData deployData; @@ -303,7 +312,6 @@ public class SessionActiveHandlerTest extends SessionHandlerTest { session = createRemoteSession(sessionId, initialStatus, zkClient, clock); addLocalSession(sessionId, deployData, zkClient); metaData = localRepo.getSession(sessionId).getMetaData(); - handler = createHandler(); actResponse = handler.handle(SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.PUT, Cmd.ACTIVE, sessionId, subPath)); return this; } @@ -342,20 +350,11 @@ public class SessionActiveHandlerTest extends SessionHandlerTest { zkc.writeApplicationId(id); } - private SessionHandler createHandler() { - final SessionFactory sessionFactory = new MockSessionFactory(); - TestTenantBuilder testTenantBuilder = new TestTenantBuilder(); - testTenantBuilder.createTenant(tenantName) - .withSessionFactory(sessionFactory) - .withLocalSessionRepo(localRepo) - .withRemoteSessionRepo(remoteSessionRepo) - .withApplicationRepo(applicationRepo) - .build(); - return new SessionActiveHandler( - SessionActiveHandler.testOnlyContext(), - new ApplicationRepository(testTenantBuilder.createTenants(), hostProvisioner, clock), - testTenantBuilder.createTenants(), - Zone.defaultZone()); + private SessionActiveHandler createHandler() { + return new SessionActiveHandler(SessionActiveHandler.testOnlyContext(), + new ApplicationRepository(tenantRepository, hostProvisioner, clock), + tenantRepository, + Zone.defaultZone()); } } 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 e4841930cc8..1428e384f2b 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 @@ -5,13 +5,15 @@ import com.google.common.io.Files; import com.yahoo.config.model.application.provider.FilesApplicationPackage; import com.yahoo.config.provision.TenantName; import com.yahoo.container.jdisc.HttpResponse; -import com.yahoo.container.logging.AccessLog; import com.yahoo.jdisc.Response; import com.yahoo.jdisc.http.HttpRequest; import com.yahoo.text.Utf8; import com.yahoo.vespa.config.server.ApplicationRepository; +import com.yahoo.vespa.config.server.TestComponentRegistry; import com.yahoo.vespa.config.server.http.ContentHandlerTestBase; import com.yahoo.vespa.config.server.http.SessionHandlerTest; +import com.yahoo.vespa.config.server.tenant.TenantBuilder; +import com.yahoo.vespa.config.server.tenant.TenantRepository; import org.apache.commons.io.FileUtils; import org.junit.Before; import org.junit.Ignore; @@ -22,22 +24,28 @@ import java.io.File; import java.io.IOException; import java.io.InputStream; import java.time.Clock; -import java.util.concurrent.Executor; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; /** - * @author lulf - * @since 5.1 + * @author Ulf Lilleengen */ public class SessionContentHandlerTest extends ContentHandlerTestBase { private static final TenantName tenant = TenantName.from("contenttest"); + + private final TestComponentRegistry componentRegistry = new TestComponentRegistry.Builder().build(); + private final Clock clock = componentRegistry.getClock(); + + private TenantRepository tenantRepository; private SessionContentHandler handler = null; @Before public void setupHandler() throws Exception { + tenantRepository = new TenantRepository(componentRegistry, false); + tenantRepository.addTenant(TenantBuilder.create(componentRegistry, tenant)); + tenantRepository.getTenant(tenant).getLocalSessionRepo().addSession(new MockSession(1L, FilesApplicationPackage.fromFile(createTestApp()))); handler = createHandler(); pathPrefix = "/application/v2/tenant/" + tenant + "/session/"; baseUrl = "http://foo:1337/application/v2/tenant/" + tenant + "/session/1/content/"; @@ -54,14 +62,14 @@ public class SessionContentHandlerTest extends ContentHandlerTestBase { @Test @Ignore - public void require_that_mkdir_with_body_is_illegal() throws IOException { + public void require_that_mkdir_with_body_is_illegal(){ HttpResponse response = put("/foobio/", "foo"); assertNotNull(response); assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST)); } @Test - public void require_that_nonexistant_session_returns_not_found() throws IOException { + public void require_that_nonexistant_session_returns_not_found() { HttpResponse response = doRequest(HttpRequest.Method.GET, "/test.txt", 2l); assertNotNull(response); assertThat(response.getStatus(), is(Response.Status.NOT_FOUND)); @@ -73,7 +81,7 @@ public class SessionContentHandlerTest extends ContentHandlerTestBase { } @Test - public void require_that_file_write_without_body_is_illegal() throws IOException { + public void require_that_file_write_without_body_is_illegal() { HttpResponse response = doRequest(HttpRequest.Method.PUT, "/foobio.txt"); assertNotNull(response); assertThat(response.getStatus(), is(Response.Status.BAD_REQUEST)); @@ -136,7 +144,7 @@ public class SessionContentHandlerTest extends ContentHandlerTestBase { is("{\"prepared\":\"http://foo:1337" + pathPrefix + "1/prepared\"}")); } - protected File createTestApp() throws IOException { + private File createTestApp() throws IOException { File testApp = Files.createTempDir(); FileUtils.copyDirectory(new File("src/test/apps/content"), testApp); return testApp; @@ -146,26 +154,22 @@ public class SessionContentHandlerTest extends ContentHandlerTestBase { return doRequest(method, path, 1l); } - protected HttpResponse doRequest(HttpRequest.Method method, String path, long sessionId) { + private HttpResponse doRequest(HttpRequest.Method method, String path, long sessionId) { return handler.handle(SessionHandlerTest.createTestRequest(pathPrefix, method, Cmd.CONTENT, sessionId, path)); } - protected HttpResponse doRequest(HttpRequest.Method method, String path, InputStream data) { + private HttpResponse doRequest(HttpRequest.Method method, String path, InputStream data) { return doRequest(method, path, 1l, data); } - protected HttpResponse doRequest(HttpRequest.Method method, String path, long sessionId, InputStream data) { + private HttpResponse doRequest(HttpRequest.Method method, String path, long sessionId, InputStream data) { return handler.handle(SessionHandlerTest.createTestRequest(pathPrefix, method, Cmd.CONTENT, sessionId, path, data)); } - private SessionContentHandler createHandler() throws Exception { - TestTenantBuilder testTenantBuilder = new TestTenantBuilder(); - testTenantBuilder.createTenant(tenant).getLocalSessionRepo().addSession(new MockSession(1l, FilesApplicationPackage.fromFile(createTestApp()))); + private SessionContentHandler createHandler() { return new SessionContentHandler( SessionContentHandler.testOnlyContext(), - new ApplicationRepository(testTenantBuilder.createTenants(), - new SessionHandlerTest.MockProvisioner(), - Clock.systemUTC()), - testTenantBuilder.createTenants()); + new ApplicationRepository(tenantRepository, new SessionHandlerTest.MockProvisioner(), clock), + tenantRepository); } } diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/SessionCreateHandlerTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/SessionCreateHandlerTest.java index 73579b94086..7c0e410d244 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/SessionCreateHandlerTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/SessionCreateHandlerTest.java @@ -1,13 +1,13 @@ // 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.cloud.config.ConfigserverConfig; import com.yahoo.config.model.application.provider.FilesApplicationPackage; import com.yahoo.config.provision.ApplicationId; import com.yahoo.config.provision.TenantName; import com.yahoo.container.jdisc.HttpRequest; import com.yahoo.container.jdisc.HttpResponse; import com.yahoo.vespa.config.server.ApplicationRepository; +import com.yahoo.vespa.config.server.TestComponentRegistry; import com.yahoo.vespa.config.server.application.MemoryTenantApplications; import com.yahoo.vespa.config.server.application.TenantApplications; import com.yahoo.vespa.config.server.http.CompressedApplicationInputStreamTest; @@ -15,7 +15,7 @@ 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.LocalSessionRepo; -import com.yahoo.vespa.config.server.session.SessionFactory; +import com.yahoo.vespa.config.server.tenant.TenantBuilder; import com.yahoo.vespa.config.server.tenant.TenantRepository; import org.junit.Before; import org.junit.Ignore; @@ -40,7 +40,6 @@ import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; /** * @author hmusum @@ -48,9 +47,11 @@ import static org.junit.Assert.fail; public class SessionCreateHandlerTest extends SessionHandlerTest { private static final TenantName tenant = TenantName.from("test"); - private static final HashMap<String, String> postHeaders = new HashMap<>(); + private final TestComponentRegistry componentRegistry = new TestComponentRegistry.Builder().build(); + private final Clock clock = componentRegistry.getClock(); + private String pathPrefix = "/application/v2/session/"; private String createdMessage = " created.\""; private String tenantMessage = ""; @@ -58,15 +59,24 @@ public class SessionCreateHandlerTest extends SessionHandlerTest { public File testApp = new File("src/test/apps/app"); private LocalSessionRepo localSessionRepo; private TenantApplications applicationRepo; + private TenantRepository tenantRepository; + private MockSessionFactory sessionFactory; static { postHeaders.put(ApplicationApiHandler.contentTypeHeader, ApplicationApiHandler.APPLICATION_X_GZIP); } @Before - public void setupRepo() throws Exception { + public void setupRepo() { applicationRepo = new MemoryTenantApplications(); localSessionRepo = new LocalSessionRepo(Clock.systemUTC()); + tenantRepository = new TenantRepository(componentRegistry, false); + sessionFactory = new MockSessionFactory(); + TenantBuilder tenantBuilder = TenantBuilder.create(componentRegistry, tenant) + .withSessionFactory(sessionFactory) + .withLocalSessionRepo(localSessionRepo) + .withApplicationRepo(applicationRepo); + tenantRepository.addTenant(tenantBuilder); pathPrefix = "/application/v2/tenant/" + tenant + "/session/"; createdMessage = " for tenant '" + tenant + "' created.\""; tenantMessage = ",\"tenant\":\"test\""; @@ -98,20 +108,18 @@ public class SessionCreateHandlerTest extends SessionHandlerTest { public void require_that_application_name_is_given_from_parameter() throws IOException { Map<String, String> params = Collections.singletonMap("name", "ulfio"); File outFile = CompressedApplicationInputStreamTest.createTarFile(); - MockSessionFactory factory = new MockSessionFactory(); - createHandler(factory).handle(post(outFile, postHeaders, params)); - assertTrue(factory.createCalled); - assertThat(factory.applicationName, is("ulfio")); + createHandler().handle(post(outFile, postHeaders, params)); + assertTrue(sessionFactory.createCalled); + assertThat(sessionFactory.applicationName, is("ulfio")); } private void assertFromParameter(String expected, String from) throws IOException { HttpRequest request = post(Collections.singletonMap("from", from)); - MockSessionFactory factory = new MockSessionFactory(); - factory.applicationPackage = testApp; - HttpResponse response = createHandler(factory).handle(request); + sessionFactory.applicationPackage = testApp; + HttpResponse response = createHandler().handle(request); assertNotNull(response); assertThat(response.getStatus(), is(OK)); - assertTrue(factory.createFromCalled); + assertTrue(sessionFactory.createFromCalled); assertThat(SessionHandlerTest.getRenderedString(response), is("{\"log\":[]" + tenantMessage + ",\"session-id\":\"" + expected + "\",\"prepared\":\"http://" + hostname + ":" + port + pathPrefix + expected + "/prepared\",\"content\":\"http://" + hostname + ":" + port + pathPrefix + @@ -139,9 +147,8 @@ public class SessionCreateHandlerTest extends SessionHandlerTest { @Test public void require_that_session_factory_is_called() throws IOException { - MockSessionFactory sessionFactory = new MockSessionFactory(); File outFile = CompressedApplicationInputStreamTest.createTarFile(); - createHandler(sessionFactory).handle(post(outFile)); + createHandler().handle(post(outFile)); assertTrue(sessionFactory.createCalled); } @@ -155,10 +162,9 @@ public class SessionCreateHandlerTest extends SessionHandlerTest { @Test public void require_internal_error_when_exception() throws IOException { - MockSessionFactory factory = new MockSessionFactory(); - factory.doThrow = true; + sessionFactory.doThrow = true; File outFile = CompressedApplicationInputStreamTest.createTarFile(); - HttpResponse response = createHandler(factory).handle(post(outFile)); + HttpResponse response = createHandler().handle(post(outFile)); HandlerTest.assertHttpStatusCodeErrorCodeAndMessage(response, INTERNAL_SERVER_ERROR, HttpErrorResponse.errorCodes.INTERNAL_SERVER_ERROR, "foo"); @@ -166,9 +172,8 @@ public class SessionCreateHandlerTest extends SessionHandlerTest { @Test public void require_that_handler_unpacks_application() throws IOException { - MockSessionFactory sessionFactory = new MockSessionFactory(); File outFile = CompressedApplicationInputStreamTest.createTarFile(); - createHandler(sessionFactory).handle(post(outFile)); + createHandler().handle(post(outFile)); assertTrue(sessionFactory.createCalled); final File applicationPackage = sessionFactory.applicationPackage; assertNotNull(applicationPackage); @@ -181,7 +186,7 @@ public class SessionCreateHandlerTest extends SessionHandlerTest { @Test public void require_that_session_is_stored_in_repo() throws IOException { File outFile = CompressedApplicationInputStreamTest.createTarFile(); - createHandler(new MockSessionFactory()).handle(post(outFile)); + createHandler().handle(post(outFile)); assertNotNull(localSessionRepo.getSession(0l)); } @@ -217,37 +222,13 @@ public class SessionCreateHandlerTest extends SessionHandlerTest { } private SessionCreateHandler createHandler() { - try { - return createHandler(new MockSessionFactory()); - } catch (Exception e) { - e.printStackTrace(); - fail(e.getMessage()); - } - return null; - } - - private SessionCreateHandler createHandler(SessionFactory sessionFactory) { - try { - TestTenantBuilder testBuilder = new TestTenantBuilder(); - testBuilder.createTenant(tenant).withSessionFactory(sessionFactory) - .withLocalSessionRepo(localSessionRepo) - .withApplicationRepo(applicationRepo); - return createHandler(testBuilder.createTenants()); - } catch (Exception e) { - e.printStackTrace(); - } - return null; - } - - private SessionCreateHandler createHandler(TenantRepository tenantRepository) throws Exception { - TestTenantBuilder testTenantBuilder = new TestTenantBuilder(); - final ConfigserverConfig configserverConfig = new ConfigserverConfig(new ConfigserverConfig.Builder()); return new SessionCreateHandler( SessionCreateHandler.testOnlyContext(), - new ApplicationRepository(testTenantBuilder.createTenants(), + new ApplicationRepository(tenantRepository, new SessionHandlerTest.MockProvisioner(), - Clock.systemUTC()), - tenantRepository, configserverConfig); + clock), + tenantRepository, + componentRegistry.getConfigserverConfig()); } diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/SessionPrepareHandlerTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/SessionPrepareHandlerTest.java index 1759cd68062..149bec7ab79 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/SessionPrepareHandlerTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/SessionPrepareHandlerTest.java @@ -3,7 +3,6 @@ package com.yahoo.vespa.config.server.http.v2; import com.google.common.collect.ImmutableMap; import com.google.common.util.concurrent.UncheckedTimeoutException; -import com.yahoo.cloud.config.ConfigserverConfig; import com.yahoo.config.application.api.ApplicationFile; import com.yahoo.config.application.api.DeployLogger; import com.yahoo.config.model.api.ServiceInfo; @@ -13,7 +12,6 @@ import com.yahoo.config.provision.ApplicationLockException; import com.yahoo.config.provision.OutOfCapacityException; import com.yahoo.config.provision.TenantName; import com.yahoo.container.jdisc.HttpResponse; -import com.yahoo.container.logging.AccessLog; import com.yahoo.jdisc.http.HttpRequest; import com.yahoo.path.Path; import com.yahoo.slime.JsonDecoder; @@ -23,13 +21,14 @@ import com.yahoo.vespa.config.server.ApplicationRepository; import com.yahoo.vespa.config.server.TestComponentRegistry; import com.yahoo.vespa.config.server.application.ApplicationSet; import com.yahoo.vespa.config.server.host.HostRegistry; -import com.yahoo.vespa.config.server.application.TenantApplications; import com.yahoo.vespa.config.server.application.MemoryTenantApplications; import com.yahoo.vespa.config.server.configchange.ConfigChangeActions; import com.yahoo.vespa.config.server.configchange.MockRefeedAction; import com.yahoo.vespa.config.server.configchange.MockRestartAction; import com.yahoo.vespa.config.server.http.*; import com.yahoo.vespa.config.server.session.*; +import com.yahoo.vespa.config.server.tenant.TenantBuilder; +import com.yahoo.vespa.config.server.tenant.TenantRepository; import com.yahoo.vespa.curator.Curator; import com.yahoo.vespa.curator.mock.MockCurator; import org.junit.Before; @@ -57,30 +56,36 @@ import static org.junit.Assert.assertThat; /** * @author hmusum - * - * @since 5.1.14 */ public class SessionPrepareHandlerTest extends SessionHandlerTest { private static final TenantName tenant = TenantName.from("test"); - private TestTenantBuilder builder; + + private final TestComponentRegistry componentRegistry = new TestComponentRegistry.Builder().build(); + private final Clock clock = componentRegistry.getClock(); private Curator curator; - private SessionZooKeeperClient zooKeeperClient; private LocalSessionRepo localRepo; - private TenantApplications applicationRepo; private String preparedMessage = " prepared.\"}"; private String tenantMessage = ""; + private RemoteSessionRepo remoteSessionRepo; + private TenantRepository tenantRepository; @Before - public void setupRepo() throws Exception { - applicationRepo = new MemoryTenantApplications(); + public void setupRepo() { curator = new MockCurator(); - localRepo = new LocalSessionRepo(Clock.systemUTC()); + localRepo = new LocalSessionRepo(clock); pathPrefix = "/application/v2/tenant/" + tenant + "/session/"; preparedMessage = " for tenant '" + tenant + "' prepared.\""; tenantMessage = ",\"tenant\":\"" + tenant + "\""; - builder = new TestTenantBuilder(); + tenantRepository = new TenantRepository(componentRegistry, false); + remoteSessionRepo = new RemoteSessionRepo(tenant); + TenantBuilder tenantBuilder = TenantBuilder.create(componentRegistry, tenant) + .withSessionFactory(new MockSessionFactory()) + .withLocalSessionRepo(localRepo) + .withRemoteSessionRepo(remoteSessionRepo) + .withApplicationRepo(new MemoryTenantApplications()); + tenantRepository.addTenant(tenantBuilder); } @Test @@ -144,30 +149,31 @@ public class SessionPrepareHandlerTest extends SessionHandlerTest { } /** - * A mock remote session repo based on contents of local repo + * A mock remote session repo based on contents of local repo. Only works when there is just one session in local repo */ - private RemoteSessionRepo fromLocalSessionRepo(LocalSessionRepo localRepo, Clock clock) { - RemoteSessionRepo remoteRepo = new RemoteSessionRepo(tenant); + // TODO: Fix this mess + private SessionZooKeeperClient fromLocalSessionRepo(LocalSessionRepo localRepo) { + SessionZooKeeperClient zooKeeperClient = null; for (LocalSession ls : localRepo.listSessions()) { - zooKeeperClient = new MockSessionZKClient(curator, tenant, ls.getSessionId()); if (ls.getStatus()!=null) zooKeeperClient.writeStatus(ls.getStatus()); RemoteSession remSess = new RemoteSession(tenant, ls.getSessionId(), new TestComponentRegistry.Builder().curator(curator).build(), zooKeeperClient, clock); - remoteRepo.addSession(remSess); + remoteSessionRepo.addSession(remSess); } - return remoteRepo; + return zooKeeperClient; } @Test public void require_get_response_activate_url_on_ok() throws Exception { MockSession session = new MockSession(1, null); localRepo.addSession(session); - SessionHandler sessHandler = createHandler(fromLocalSessionRepo(localRepo, Clock.systemUTC())); + SessionHandler sessHandler = createHandler(); sessHandler.handle(SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.PUT, Cmd.PREPARED, 1L)); session.setStatus(Session.Status.PREPARE); + SessionZooKeeperClient zooKeeperClient = fromLocalSessionRepo(localRepo); zooKeeperClient.writeStatus(Session.Status.PREPARE); HttpResponse getResponse = sessHandler.handle( SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.GET, Cmd.PREPARED, 1L)); @@ -179,8 +185,9 @@ public class SessionPrepareHandlerTest extends SessionHandlerTest { public void require_get_response_error_on_not_prepared() throws Exception { MockSession session = new MockSession(1, null); localRepo.addSession(session); - SessionHandler sessHandler = createHandler(fromLocalSessionRepo(localRepo, Clock.systemUTC())); + SessionHandler sessHandler = createHandler(); session.setStatus(Session.Status.NEW); + SessionZooKeeperClient zooKeeperClient = fromLocalSessionRepo(localRepo); zooKeeperClient.writeStatus(Session.Status.NEW); HttpResponse getResponse = sessHandler.handle( SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.GET, Cmd.PREPARED, 1L)); @@ -201,7 +208,7 @@ public class SessionPrepareHandlerTest extends SessionHandlerTest { MockSession session = new MockSession(1, null); localRepo.addSession(session); session.setStatus(Session.Status.ACTIVATE); - SessionHandler sessionHandler = createHandler(fromLocalSessionRepo(localRepo, Clock.systemUTC())); + SessionHandler sessionHandler = createHandler(); HttpResponse putResponse = sessionHandler.handle( SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.PUT, Cmd.PREPARED, 1L)); assertHttpStatusCodeErrorCodeAndMessage(putResponse, BAD_REQUEST, @@ -213,7 +220,7 @@ public class SessionPrepareHandlerTest extends SessionHandlerTest { public void require_get_response_error_when_session_id_does_not_exist() throws Exception { MockSession session = new MockSession(1, null); localRepo.addSession(session); - SessionHandler sessHandler = createHandler(fromLocalSessionRepo(localRepo, Clock.systemUTC())); + SessionHandler sessHandler = createHandler(); HttpResponse getResponse = sessHandler.handle( SessionHandlerTest.createTestRequest(pathPrefix, HttpRequest.Method.GET, Cmd.PREPARED, 9999L)); assertHttpStatusCodeErrorCodeAndMessage(getResponse, NOT_FOUND, @@ -235,12 +242,15 @@ public class SessionPrepareHandlerTest extends SessionHandlerTest { @Test public void require_that_preparing_with_multiple_tenants_work() throws Exception { - // Need different repos for 'default' tenant as opposed to the 'test' tenant - LocalSessionRepo localRepoDefault = new LocalSessionRepo(Clock.systemUTC()); - final TenantName defaultTenant = TenantName.defaultName(); - addTenant(defaultTenant, localRepoDefault, new RemoteSessionRepo(tenant), new MockSessionFactory()); - addTestTenant(); - final SessionHandler handler = createHandler(builder); + // Need different repo for 'test2' tenant + LocalSessionRepo localRepoDefault = new LocalSessionRepo(clock); + final TenantName defaultTenant = TenantName.from("test2"); + TenantBuilder defaultTenantBuilder = TenantBuilder.create(componentRegistry, defaultTenant) + .withLocalSessionRepo(localRepoDefault) + .withRemoteSessionRepo(new RemoteSessionRepo(defaultTenant)) + .withSessionFactory(new MockSessionFactory()); + tenantRepository.addTenant(defaultTenantBuilder); + final SessionHandler handler = createHandler(); long sessionId = 1; // Deploy with default tenant @@ -370,39 +380,16 @@ public class SessionPrepareHandlerTest extends SessionHandlerTest { } private SessionHandler createHandler() { - return createHandler(addTestTenant()); - } - - private SessionHandler createHandler(RemoteSessionRepo remoteSessionRepo) { - return createHandler(addTenant(tenant, localRepo, remoteSessionRepo, new MockSessionFactory())); - } - - private TestTenantBuilder addTestTenant() { - return addTenant(tenant, localRepo, new RemoteSessionRepo(tenant), new MockSessionFactory()); - } - - private SessionHandler createHandler(TestTenantBuilder builder) { - final ConfigserverConfig configserverConfig = new ConfigserverConfig(new ConfigserverConfig.Builder()); return new SessionPrepareHandler( SessionPrepareHandler.testOnlyContext(), - new ApplicationRepository(builder.createTenants(), + new ApplicationRepository(tenantRepository, new MockProvisioner(), - Clock.systemUTC()), - builder.createTenants(), configserverConfig); + clock), + tenantRepository, + componentRegistry.getConfigserverConfig()); } - private TestTenantBuilder addTenant(TenantName tenantName, - LocalSessionRepo localSessionRepo, - RemoteSessionRepo remoteSessionRepo, - SessionFactory sessionFactory) { - builder.createTenant(tenantName).withSessionFactory(sessionFactory) - .withLocalSessionRepo(localSessionRepo) - .withRemoteSessionRepo(remoteSessionRepo) - .withApplicationRepo(applicationRepo); - return builder; - } - public static class SessionThrowingException extends LocalSession { private final RuntimeException exception; 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 deleted file mode 100644 index 03f55c7ff1e..00000000000 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/http/v2/TestTenantBuilder.java +++ /dev/null @@ -1,60 +0,0 @@ -// 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.google.common.base.Function; -import com.google.common.collect.Collections2; -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.application.MemoryTenantApplications; -import com.yahoo.vespa.config.server.session.LocalSessionRepo; -import com.yahoo.vespa.config.server.session.RemoteSessionRepo; -import com.yahoo.vespa.config.server.tenant.Tenant; -import com.yahoo.vespa.config.server.tenant.TenantBuilder; -import com.yahoo.vespa.config.server.tenant.TenantRepository; - -import java.util.*; - -/** - * Test utility for creating tenantRepository used for testing and setup wiring of tenant stuff. - * - * @author Ulf Lilleengen - */ -public class TestTenantBuilder { - - private GlobalComponentRegistry componentRegistry; - private Map<TenantName, TenantBuilder> tenantMap = new HashMap<>(); - - public TestTenantBuilder() { - componentRegistry = new TestComponentRegistry.Builder().build(); - } - - public TenantBuilder createTenant(TenantName tenantName) { - MemoryTenantApplications applicationRepo = new MemoryTenantApplications(); - TenantBuilder builder = TenantBuilder.create(componentRegistry, tenantName) - .withSessionFactory(new SessionCreateHandlerTest.MockSessionFactory()) - .withLocalSessionRepo(new LocalSessionRepo(componentRegistry.getClock())) - .withRemoteSessionRepo(new RemoteSessionRepo(tenantName)) - .withApplicationRepo(applicationRepo); - tenantMap.put(tenantName, builder); - return builder; - } - - public Map<TenantName, TenantBuilder> tenants() { - return Collections.unmodifiableMap(tenantMap); - } - - public TenantRepository createTenants() { - Collection<Tenant> tenantList = Collections2.transform(tenantMap.values(), new Function<TenantBuilder, Tenant>() { - @Override - public Tenant apply(TenantBuilder builder) { - try { - return builder.build(); - } catch (Exception e) { - throw new IllegalArgumentException("Unable to build tenant", e); - } - } - }); - return new TenantRepository(componentRegistry, tenantList); - } -} diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/tenant/TenantRepositoryTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/tenant/TenantRepositoryTest.java index ee1b8e5707d..3598af57593 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/tenant/TenantRepositoryTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/tenant/TenantRepositoryTest.java @@ -18,7 +18,6 @@ import org.junit.Test; import org.xml.sax.SAXException; import java.io.IOException; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Set; @@ -129,8 +128,8 @@ public class TenantRepositoryTest extends TestWithCurator { } @Test - public void testTenantsChanged() throws Exception { - tenantRepository = new TenantRepository(globalComponentRegistry, new ArrayList<>()); + public void testTenantsChanged() { + tenantRepository = new TenantRepository(globalComponentRegistry); tenantRepository.addTenant(tenant2); tenantRepository.createTenants(); Set<TenantName> allTenants = tenantRepository.getAllTenantNames(); diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/tenant/TenantTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/tenant/TenantTest.java index f3216764a5d..1975899355c 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/tenant/TenantTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/tenant/TenantTest.java @@ -3,10 +3,9 @@ package com.yahoo.vespa.config.server.tenant; import com.google.common.testing.EqualsTester; import com.yahoo.config.provision.TenantName; +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.http.v2.TestTenantBuilder; -import com.yahoo.vespa.config.server.tenant.Tenant; import org.junit.Before; import org.junit.Test; @@ -19,6 +18,7 @@ import static org.junit.Assert.*; * @since 5.3 */ public class TenantTest extends TestWithCurator { + private final TestComponentRegistry componentRegistry = new TestComponentRegistry.Builder().build(); private Tenant t1; private Tenant t2; @@ -26,15 +26,20 @@ public class TenantTest extends TestWithCurator { private Tenant t4; @Before - public void setupTenant() throws Exception { + public void setupTenant() { t1 = createTenant("foo"); t2 = createTenant("foo"); t3 = createTenant("bar"); t4 = createTenant("baz"); } - private Tenant createTenant(String name) throws Exception { - return new TestTenantBuilder().createTenant(TenantName.from(name)).build(); + private Tenant createTenant(String name) { + TenantRepository tenantRepository = new TenantRepository(componentRegistry, false); + TenantName tenantName = TenantName.from(name); + TenantBuilder tenantBuilder = TenantBuilder.create(componentRegistry, tenantName) + .withApplicationRepo(new MemoryTenantApplications()); + tenantRepository.addTenant(tenantBuilder); + return tenantRepository.getTenant(tenantName); } @Test |