diff options
Diffstat (limited to 'standalone-container/src/test')
9 files changed, 256 insertions, 252 deletions
diff --git a/standalone-container/src/test/java/com/yahoo/container/standalone/CloudConfigInstallVariablesTest.java b/standalone-container/src/test/java/com/yahoo/container/standalone/CloudConfigInstallVariablesTest.java new file mode 100644 index 00000000000..1cd110d8106 --- /dev/null +++ b/standalone-container/src/test/java/com/yahoo/container/standalone/CloudConfigInstallVariablesTest.java @@ -0,0 +1,67 @@ +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.container.standalone; + +import com.yahoo.vespa.model.container.configserver.option.CloudConfigOptions; +import org.junit.Test; + +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +import static com.yahoo.container.standalone.CloudConfigInstallVariables.toConfigModelsPluginDir; +import static com.yahoo.container.standalone.CloudConfigInstallVariables.toConfigServer; +import static com.yahoo.container.standalone.CloudConfigInstallVariables.toConfigServers; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.Matchers.contains; +import static org.junit.Assert.assertThat; + +/** + * @author Ulf Lilleengen + * @author Tony Vaagenes + */ +public class CloudConfigInstallVariablesTest { + + @Test + public void test_configserver_parsing() { + CloudConfigOptions.ConfigServer[] parsed = toConfigServers("myhost.mydomain.com"); + assertThat(parsed.length, is(1)); + } + + @Test + public void port_can_be_configured() { + CloudConfigOptions.ConfigServer[] parsed = toConfigServers("myhost:123"); + int port = parsed[0].port.get(); + assertThat(port, is(123)); + } + + @Test + public void multiple_spaces_are_supported() { + CloudConfigOptions.ConfigServer[] parsed = toConfigServers("test1 test2"); + assertThat(parsed.length, is(2)); + + List<String> hostNames = Arrays.stream(parsed).map(cs -> cs.hostName).collect(Collectors.toList()); + assertThat(hostNames, contains("test1", "test2")); + } + + @Test(expected = IllegalArgumentException.class) + public void missing_port_gives_exception() { + toConfigServer("myhost:"); + } + + @Test(expected = IllegalArgumentException.class) + public void non_numeric_port_gives_exception() { + toConfigServer("myhost:non-numeric"); + } + + @Test + public void string_arrays_are_split_on_spaces() { + String[] parsed = toConfigModelsPluginDir("/home/vespa/foo /home/vespa/bar "); + assertThat(parsed.length, is(2)); + } + + @Test + public void string_arrays_are_split_on_comma() { + String[] parsed = toConfigModelsPluginDir("/home/vespa/foo,/home/vespa/bar,"); + assertThat(parsed.length, is(2)); + } +} diff --git a/standalone-container/src/test/java/com/yahoo/container/standalone/StandaloneContainer.java b/standalone-container/src/test/java/com/yahoo/container/standalone/StandaloneContainer.java new file mode 100644 index 00000000000..a00ffd8b985 --- /dev/null +++ b/standalone-container/src/test/java/com/yahoo/container/standalone/StandaloneContainer.java @@ -0,0 +1,61 @@ +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.container.standalone; + +import com.yahoo.collections.Pair; +import com.yahoo.config.model.ConfigModelRepo; +import com.yahoo.config.model.producer.AbstractConfigProducerRoot; +import com.yahoo.io.IOUtils; +import com.yahoo.vespa.model.VespaModel; +import com.yahoo.vespa.model.container.xml.ContainerModelBuilder.Networking; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Arrays; +import java.util.List; + +/** + * Creates a local application from vespa-services fragments. + * + * @author Tony Vaagenes + * @author ollivir + */ +public class StandaloneContainer { + public static String firstContainerId(AbstractConfigProducerRoot root) { + return root.getConfigProducer("container").get().getConfigId(); + } + + interface ThrowingFunction<T, U> { + U apply(T input) throws Exception; + } + + static <T> T withContainerModel(String servicesXml, ThrowingFunction<VespaModel, T> f) throws Exception { + return withTempDirectory(applicationPath -> { + writeServicesXml(applicationPath, servicesXml); + + LocalFileDb distributedFiles = new LocalFileDb(applicationPath); + VespaModel root; + Pair<VespaModel, com.yahoo.vespa.model.container.Container> rc = StandaloneContainerApplication.createContainerModel( + applicationPath, distributedFiles, applicationPath.resolve("preprocesedApp").toFile(), Networking.enable, + new ConfigModelRepo()); + root = rc.getFirst(); + return f.apply(root); + }); + } + + private static <T> T withTempDirectory(ThrowingFunction<Path, T> f) throws Exception { + Path directory = Files.createTempDirectory("application"); + try { + return f.apply(directory); + } finally { + IOUtils.recursiveDeleteDir(directory.toFile()); + } + } + + private static void writeServicesXml(Path applicationPath, String servicesXml) throws IOException { + Path path = applicationPath.resolve("services.xml"); + List<String> output = Arrays.asList("<?xml version=\"1.0\" encoding=\"utf-8\"?>", servicesXml); + Files.write(path, output, StandardCharsets.UTF_8); + } +} diff --git a/standalone-container/src/test/java/com/yahoo/container/standalone/StandaloneContainerActivatorTest.java b/standalone-container/src/test/java/com/yahoo/container/standalone/StandaloneContainerActivatorTest.java index 8d413ade0f0..71668b595a0 100644 --- a/standalone-container/src/test/java/com/yahoo/container/standalone/StandaloneContainerActivatorTest.java +++ b/standalone-container/src/test/java/com/yahoo/container/standalone/StandaloneContainerActivatorTest.java @@ -1,4 +1,4 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.container.standalone; import com.google.inject.Module; @@ -25,7 +25,6 @@ import static org.junit.Assert.assertThat; /** * @author Einar M R Rosenvinge - * @since 5.22.0 */ public class StandaloneContainerActivatorTest { @@ -100,7 +99,7 @@ public class StandaloneContainerActivatorTest { private static Module newAppDirBinding(final Path applicationDir) { return binder -> binder.bind(Path.class) - .annotatedWith(StandaloneContainerApplication.applicationPathName()) + .annotatedWith(StandaloneContainerApplication.APPLICATION_PATH_NAME) .toInstance(applicationDir); } diff --git a/standalone-container/src/test/java/com/yahoo/container/standalone/StandaloneContainerTest.java b/standalone-container/src/test/java/com/yahoo/container/standalone/StandaloneContainerTest.java new file mode 100644 index 00000000000..6d4abc84dbc --- /dev/null +++ b/standalone-container/src/test/java/com/yahoo/container/standalone/StandaloneContainerTest.java @@ -0,0 +1,74 @@ +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.container.standalone; + +import com.yahoo.vespa.model.AbstractService; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * @author Tony Vaagenes + * @author gjoranv + * @author ollivir + */ + +public class StandaloneContainerTest { + private static final String PLAIN_XML = "<container version=\"1.0\" />"; + + @Test + public void container_is_allowed_root_element() throws Exception { + StandaloneContainer.withContainerModel(PLAIN_XML, root -> null); + } + + @Test + public void services_is_allowed_root_element() throws Exception { + String servicesXml = "<services>" + // + "<container version=\"1.0\" />" + // + "</services>"; + + StandaloneContainer.withContainerModel(servicesXml, root -> null); + } + + @Test(expected = Exception.class) + public void multiple_container_elements_cannot_be_deployed() throws Exception { + String twoContainersXml = "<services>" + // + "<container id=\"container-1\" version=\"1.0\" />" + // + "<container id=\"container-2\" version=\"1.0\" />" + // + "</services>"; + + StandaloneContainer.withContainerModel(twoContainersXml, root -> null); + } + + @Test + public void application_preprocessor_is_run() throws Exception { + String servicesXml = "<services xmlns:preprocess=\"properties\">" + // + "<preprocess:properties>" + // + "<container_id>container-1</container_id>" + // + "</preprocess:properties>" + // + "<container id=\"${container_id}\" version=\"1.0\" />" + // + "</services>"; + + StandaloneContainer.withContainerModel(servicesXml, root -> { + assertTrue(root.getConfigProducer("container-1/standalone").isPresent()); + return null; + }); + } + + @Test + public void no_default_ports_are_enabled_when_using_http() throws Exception { + String xml = "<jdisc version=\"1.0\">" + // + "<http>" + // + "<server port=\"4000\" id=\"server1\" />" + // + "</http>" + // + "</jdisc>"; + + StandaloneContainer.withContainerModel(xml, root -> { + AbstractService container = (AbstractService) root.getConfigProducer("jdisc/standalone").get(); + System.out.println("portCnt: " + container.getPortCount()); + System.out.println("numPorts: " + container.getNumPortsAllocated()); + assertEquals(1, container.getNumPortsAllocated()); + return null; + }); + } +} diff --git a/standalone-container/src/test/java/com/yahoo/container/standalone/StandaloneSubscriberTest.java b/standalone-container/src/test/java/com/yahoo/container/standalone/StandaloneSubscriberTest.java new file mode 100644 index 00000000000..dd755e8e6dd --- /dev/null +++ b/standalone-container/src/test/java/com/yahoo/container/standalone/StandaloneSubscriberTest.java @@ -0,0 +1,52 @@ +// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.container.standalone; + +import com.yahoo.config.ConfigInstance; +import com.yahoo.container.BundlesConfig; +import com.yahoo.container.ComponentsConfig; +import com.yahoo.container.di.config.Subscriber; +import com.yahoo.vespa.config.ConfigKey; +import org.junit.Ignore; +import org.junit.Test; + +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import static com.yahoo.container.standalone.StandaloneContainer.withContainerModel; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.number.OrderingComparison.greaterThan; +import static org.junit.Assert.assertThat; + +/** + * @author Tony Vaagenes + * @author ollivir + */ +public class StandaloneSubscriberTest { + private static ConfigKey<ConfigInstance> bundlesKey = key("bundles"); + private static ConfigKey<ConfigInstance> componentsKey = key("components"); + + private static ConfigKey<ConfigInstance> key(String name) { + return new ConfigKey<>(name, "container", "container"); + } + + @Test + @Ignore + public void standalone_subscriber() throws Exception { + withContainerModel("<container version=\"1.0\"></container>", root -> { + Set<ConfigKey<ConfigInstance>> keys = new HashSet<>(); + keys.add(bundlesKey); + keys.add(componentsKey); + Subscriber subscriber = new StandaloneSubscriberFactory(root).getSubscriber(keys); + Map<ConfigKey<ConfigInstance>, ConfigInstance> config = subscriber.config(); + assertThat(config.size(), is(2)); + + BundlesConfig bundlesConfig = (BundlesConfig) config.get(bundlesKey); + ComponentsConfig componentsConfig = (ComponentsConfig) config.get(componentsKey); + + assertThat(bundlesConfig.bundle().size(), is(0)); + assertThat(componentsConfig.components().size(), greaterThan(10)); + return null; + }); + } +} diff --git a/standalone-container/src/test/scala/com/yahoo/container/standalone/CloudConfigInstallVariablesTest.scala b/standalone-container/src/test/scala/com/yahoo/container/standalone/CloudConfigInstallVariablesTest.scala deleted file mode 100644 index d4baea43ba2..00000000000 --- a/standalone-container/src/test/scala/com/yahoo/container/standalone/CloudConfigInstallVariablesTest.scala +++ /dev/null @@ -1,59 +0,0 @@ -// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.container.standalone - -import com.yahoo.container.standalone.CloudConfigInstallVariables.{toConfigModelsPluginDir, toConfigServer, toConfigServers} -import org.hamcrest.CoreMatchers.is -import org.hamcrest.Matchers.arrayContaining -import org.junit.Assert.assertThat -import org.junit.Test - -/** - * @author Ulf Lilleengen - * @author Tony Vaagenes - */ -class CloudConfigInstallVariablesTest { - - @Test - def test_configserver_parsing { - val parsed = toConfigServers("myhost.mydomain.com") - assertThat(parsed.length, is(1)) - } - - @Test - def port_can_be_configured { - val parsed = toConfigServers("myhost:123") - val port: Int = parsed(0).port.get() - assertThat(port, is(123)) - } - - @Test - def multiple_spaces_are_supported { - val parsed = toConfigServers("test1 test2") - assertThat(parsed.size, is(2)) - - val hostNames = parsed.map(_.hostName) - assertThat(hostNames, arrayContaining("test1", "test2")) - } - - @Test(expected = classOf[IllegalArgumentException]) - def missing_port_gives_exception { - toConfigServer("myhost:") - } - - @Test(expected = classOf[IllegalArgumentException]) - def non_numeric_port_gives_exception { - toConfigServer("myhost:non-numeric") - } - - @Test - def string_arrays_are_split_on_spaces { - val parsed = toConfigModelsPluginDir("/home/vespa/foo /home/vespa/bar ") - assertThat(parsed.size, is(2)) - } - - @Test - def string_arrays_are_split_on_comma { - val parsed = toConfigModelsPluginDir("/home/vespa/foo,/home/vespa/bar,") - assertThat(parsed.size, is(2)) - } -} diff --git a/standalone-container/src/test/scala/com/yahoo/container/standalone/StandaloneContainer.scala b/standalone-container/src/test/scala/com/yahoo/container/standalone/StandaloneContainer.scala deleted file mode 100644 index 33f9a2e8594..00000000000 --- a/standalone-container/src/test/scala/com/yahoo/container/standalone/StandaloneContainer.scala +++ /dev/null @@ -1,64 +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.container.standalone - -import com.yahoo.config.model.producer.AbstractConfigProducerRoot -import com.yahoo.config.model.test.MockRoot -import com.yahoo.container.Container -import com.yahoo.jdisc.test.TestDriver -import scala.xml.Node -import com.yahoo.vespa.model.VespaModel -import com.yahoo.io.IOUtils -import java.nio.file.{Files, Path} -import com.yahoo.vespa.model.container.xml.ContainerModelBuilder.Networking - -/** - * Creates a local application from vespa-services fragments. - * - * @author tonytv - */ -object StandaloneContainer { - def firstContainerId(root: AbstractConfigProducerRoot): String = { - root.getConfigProducer("container").get().getConfigId - } - - def withStandaloneContainer[T](containerNode: Node) { - withTempDirectory { applicationDirectory => - System.setProperty(StandaloneContainerApplication.applicationLocationInstallVariable, applicationDirectory.toString) - createServicesXml(applicationDirectory, containerNode) - - val driver = TestDriver.newInjectedApplicationInstance(classOf[StandaloneContainerApplication]) - driver.close() - Container.resetInstance() - } - } - - def withContainerModel[T](containerNode: Node)(f: VespaModel => T) { - withTempDirectory { applicationPath => - createServicesXml(applicationPath, containerNode) - - val distributedFiles = new LocalFileDb(applicationPath) - val (root, container) = StandaloneContainerApplication.createContainerModel( - applicationPath, - distributedFiles, - applicationPath.resolve("preprocesedApp").toFile, - networkingOption = Networking.enable) - f(root) - } - } - - private def withTempDirectory[T](f : Path => T) : T = { - val directory = Files.createTempDirectory("application") - try { - f(directory) - } finally { - IOUtils.recursiveDeleteDir(directory.toFile) - } - } - - private def createServicesXml(applicationPath : Path, - containerNode: Node) { - - scala.xml.XML.save(applicationPath.resolve("services.xml").toFile.getAbsolutePath, - containerNode, xmlDecl = true) - } -} diff --git a/standalone-container/src/test/scala/com/yahoo/container/standalone/StandaloneContainerTest.scala b/standalone-container/src/test/scala/com/yahoo/container/standalone/StandaloneContainerTest.scala deleted file mode 100644 index 87bef2efd95..00000000000 --- a/standalone-container/src/test/scala/com/yahoo/container/standalone/StandaloneContainerTest.scala +++ /dev/null @@ -1,85 +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.container.standalone - - -import com.yahoo.container.standalone.StandaloneContainerTest._ -import com.yahoo.vespa.model.AbstractService -import org.junit.Assert._ -import org.junit.Test - -import scala.util.Try - - -/** - * @author tonytv - * @author gjoranv - */ - -class StandaloneContainerTest { - @Test - def container_is_allowed_root_element() { - StandaloneContainer.withContainerModel(plainXml) { root => } - } - - @Test - def services_is_allowed_root_element() { - val servicesXml = - <services> - <container version="1.0" /> - </services> - - StandaloneContainer.withContainerModel(servicesXml) { root => } - } - - @Test - def multiple_container_elements_cannot_be_deployed() { - val twoContainersXml = - <services> - <container id="container-1" version="1.0" /> - <container id="container-2" version="1.0" /> - </services> - - assertTrue( - Try { - StandaloneContainer.withContainerModel(twoContainersXml) { root => } - }.isFailure) - } - - @Test - def application_preprocessor_is_run() { - val servicesXml = - <services xmlns:preprocess="properties"> - <preprocess:properties> - <container_id>container-1</container_id> - </preprocess:properties> - <container id="${container_id}" version="1.0" /> - </services> - StandaloneContainer.withContainerModel(servicesXml) { - root => - assertTrue(root.getConfigProducer("container-1/standalone").isPresent) - } - } - - @Test - def no_default_ports_are_enabled_when_using_http() { - val xml = - <jdisc version="1.0"> - <http> - <server port="4000" id="server1" /> - </http> - </jdisc> - - StandaloneContainer.withContainerModel(xml) { root => - val container = root.getConfigProducer("jdisc/standalone").get().asInstanceOf[AbstractService] - println("portCnt: " + container.getPortCount) - println("numPorts: " + container.getNumPortsAllocated) - assertEquals(1, container.getNumPortsAllocated) - } - } - -} - -object StandaloneContainerTest { - - val plainXml = <container version="1.0" /> -} diff --git a/standalone-container/src/test/scala/com/yahoo/container/standalone/StandaloneSubscriberTest.scala b/standalone-container/src/test/scala/com/yahoo/container/standalone/StandaloneSubscriberTest.scala deleted file mode 100644 index ab6f486c748..00000000000 --- a/standalone-container/src/test/scala/com/yahoo/container/standalone/StandaloneSubscriberTest.scala +++ /dev/null @@ -1,41 +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.container.standalone - -import org.junit.{Ignore, Test} -import org.junit.Assert.assertThat -import org.hamcrest.CoreMatchers.is -import org.hamcrest.number.OrderingComparison.greaterThan - -import StandaloneContainer.withContainerModel -import com.yahoo.vespa.config.ConfigKey -import com.yahoo.config.ConfigInstance -import com.yahoo.container.{ComponentsConfig, BundlesConfig, di} -import scala.collection.JavaConverters._ - -/** - * @author tonytv - */ -class StandaloneSubscriberTest { - val bundlesKey = key("bundles") - val componentsKey = key("components") - - def key(name: String) = new ConfigKey(name, "container", "container").asInstanceOf[ConfigKey[ConfigInstance]] - - def box(i: Int) = java.lang.Integer.valueOf(i) - - @Test - @Ignore - def standalone_subscriber() { - withContainerModel(<container version="1.0"> </container>) { root => - val subscriber = new StandaloneSubscriberFactory(root).getSubscriber(Set(bundlesKey, componentsKey).asJava) - val config = subscriber.config.asScala - assertThat(config.size, is(2)) - - val bundlesConfig = config(bundlesKey).asInstanceOf[BundlesConfig] - val componentsConfig = config(componentsKey).asInstanceOf[ComponentsConfig] - - assertThat(bundlesConfig.bundle().size(), is(0)) - assertThat(box(componentsConfig.components().size()), greaterThan(box(10))) - } - } -} |