diff options
Diffstat (limited to 'standalone-container/src/test/scala/com/yahoo/container/standalone/StandaloneContainer.scala')
-rw-r--r-- | standalone-container/src/test/scala/com/yahoo/container/standalone/StandaloneContainer.scala | 64 |
1 files changed, 64 insertions, 0 deletions
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 new file mode 100644 index 00000000000..7152c0c0af1 --- /dev/null +++ b/standalone-container/src/test/scala/com/yahoo/container/standalone/StandaloneContainer.scala @@ -0,0 +1,64 @@ +// Copyright 2016 Yahoo Inc. 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.applicationLocationYinstVariable, applicationDirectory.toString) + createServicesXml(applicationDirectory, containerNode) + + val driver = TestDriver.newInjectedApplicationInstance(classOf[StandaloneContainerApplication]) + driver.close() + Container.resetInstance() + } + } + + def withContainerModel[T](containerNode: Node)(f: MockRoot => 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) + } +} |