diff options
Diffstat (limited to 'config-application-package/src/test/java/com/yahoo/config')
10 files changed, 1000 insertions, 0 deletions
diff --git a/config-application-package/src/test/java/com/yahoo/config/application/ConfigDefinitionDirTest.java b/config-application-package/src/test/java/com/yahoo/config/application/ConfigDefinitionDirTest.java new file mode 100644 index 00000000000..ada517c9a5c --- /dev/null +++ b/config-application-package/src/test/java/com/yahoo/config/application/ConfigDefinitionDirTest.java @@ -0,0 +1,67 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.config.application; + +import com.google.common.io.Files; +import com.yahoo.config.model.application.provider.Bundle; +import com.yahoo.io.IOUtils; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.jar.JarFile; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +/** + * @author lulf + * @since 5.1 + */ +public class ConfigDefinitionDirTest { + private static final String bundleFileName = "com.yahoo.searcher1.jar"; + private static final File bundleFile = new File("src/test/resources/defdircomponent/" + bundleFileName); + + @Test + public void require_that_defs_are_added() throws IOException { + File defDir = Files.createTempDir(); + ConfigDefinitionDir dir = new ConfigDefinitionDir(defDir); + Bundle bundle = new Bundle(new JarFile(bundleFile), bundleFile); + assertThat(defDir.listFiles().length, is(0)); + dir.addConfigDefinitionsFromBundle(bundle, new ArrayList<Bundle>()); + assertThat(defDir.listFiles().length, is(1)); + } + + + @Test + public void require_that_conflicting_defs_are_not_added() throws IOException { + File defDir = Files.createTempDir(); + IOUtils.writeFile(new File(defDir, "foo.def"), "alreadyexists", false); + ConfigDefinitionDir dir = new ConfigDefinitionDir(defDir); + Bundle bundle = new Bundle(new JarFile(bundleFile), bundleFile); + ArrayList<Bundle> bundlesAdded = new ArrayList<>(); + + // Conflict with built-in config definition + try { + dir.addConfigDefinitionsFromBundle(bundle, bundlesAdded); + } catch (IllegalArgumentException e) { + assertTrue(e.getMessage().contains + ("The config definition with name 'bar.foo' contained in the bundle '" + + bundleFileName + + "' conflicts with a built-in config definition")); + } + bundlesAdded.add(bundle); + + // Conflict with another bundle + Bundle bundle2 = new Bundle(new JarFile(bundleFile), bundleFile); + try { + dir.addConfigDefinitionsFromBundle(bundle2, bundlesAdded); + } catch (IllegalArgumentException e) { + assertThat(e.getMessage(), + is("The config definition with name 'bar.foo' contained in the bundle '" + + bundleFileName + + "' conflicts with the same config definition in the bundle 'com.yahoo.searcher1.jar'. Please choose a different name.")); + } + } +} diff --git a/config-application-package/src/test/java/com/yahoo/config/application/IncludeProcessorTest.java b/config-application-package/src/test/java/com/yahoo/config/application/IncludeProcessorTest.java new file mode 100644 index 00000000000..07068e236cd --- /dev/null +++ b/config-application-package/src/test/java/com/yahoo/config/application/IncludeProcessorTest.java @@ -0,0 +1,81 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.config.application; + +import org.junit.Test; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.stream.XMLStreamException; +import javax.xml.transform.*; +import java.io.*; +import java.nio.file.NoSuchFileException; + +/** + * @author lulf + * @since 5.22 + */ +public class IncludeProcessorTest { + @Test + public void testInclude() throws IOException, SAXException, XMLStreamException, ParserConfigurationException, TransformerException { + File app = new File("src/test/resources/multienvapp"); + DocumentBuilder docBuilder = Xml.getPreprocessDocumentBuilder(); + + String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><services xmlns:deploy=\"vespa\" xmlns:preprocess=\"properties\" version=\"1.0\">\n" + + " <preprocess:properties>\n" + + " <qrs.port>4099</qrs.port>\n" + + " <qrs.port>5000</qrs.port>\n" + + " </preprocess:properties>\n" + + " <preprocess:properties deploy:environment='prod'>\n" + + " <qrs.port deploy:region='us-west'>5001</qrs.port>" + + " <qrs.port deploy:region='us-east'>5002</qrs.port>" + + " </preprocess:properties>\n" + + " <admin version=\"2.0\">\n" + + " <adminserver hostalias=\"node0\"/>\n" + + " </admin>\n" + + " <admin deploy:environment=\"prod\" version=\"2.0\">\n" + + " <adminserver hostalias=\"node1\"/>\n" + + " </admin>\n" + + " <content id=\"foo\" version=\"1.0\">\n" + + " <redundancy>1</redundancy><documents>\n" + + " <document mode=\"index\" type=\"music.sd\"/>\n" + + "</documents><nodes>\n" + + " <node distribution-key=\"0\" hostalias=\"node0\"/>\n" + + "</nodes><nodes deploy:environment=\"prod\">\n" + + " <node distribution-key=\"0\" hostalias=\"node0\"/>\n" + + " <node distribution-key=\"1\" hostalias=\"node1\"/>\n" + + "</nodes><nodes deploy:environment=\"prod\" deploy:region=\"us-west\">\n" + + " <node distribution-key=\"0\" hostalias=\"node0\"/>\n" + + " <node distribution-key=\"1\" hostalias=\"node1\"/>\n" + + " <node distribution-key=\"2\" hostalias=\"node2\"/>\n" + + "</nodes></content>\n" + + "<jdisc id=\"stateless\" version=\"1.0\">\n" + + " <search deploy:environment=\"prod\">\n" + + " <chain id=\"common\">\n" + + " <searcher id=\"MySearcher1\" />\n" + + " <searcher deploy:environment=\"prod\" id=\"MySearcher2\" />\n" + + " </chain>\n" + + " </search>\n" + + " <search/>\n" + + " <component id=\"foo\" class=\"MyFoo\" bundle=\"foobundle\" />\n" + + " <component id=\"bar\" class=\"TestBar\" bundle=\"foobundle\" deploy:environment=\"dev\" />\n" + + " <component id=\"bar\" class=\"ProdBar\" bundle=\"foobundle\" deploy:environment=\"prod\" />\n" + + " <component id=\"baz\" class=\"ProdBaz\" bundle=\"foobundle\" deploy:environment=\"prod\" />\n" + + " <nodes>\n" + + " <node baseport=\"${qrs.port}\" hostalias=\"node0\"/>\n" + + " </nodes>\n" + + "</jdisc></services>"; + + Document doc = (new IncludeProcessor(app)).process(docBuilder.parse(Xml.getServices(app))); + System.out.println(Xml.documentAsString(doc)); + TestBase.assertDocument(expected, doc); + } + + @Test(expected = NoSuchFileException.class) + public void testRequiredIncludeIsDefault() throws ParserConfigurationException, IOException, SAXException, TransformerException { + File app = new File("src/test/resources/multienvapp_failrequired"); + DocumentBuilder docBuilder = Xml.getPreprocessDocumentBuilder(); + (new IncludeProcessor(app)).process(docBuilder.parse(Xml.getServices(app))); + } +} diff --git a/config-application-package/src/test/java/com/yahoo/config/application/OverrideProcessorTest.java b/config-application-package/src/test/java/com/yahoo/config/application/OverrideProcessorTest.java new file mode 100644 index 00000000000..54d0a3cc797 --- /dev/null +++ b/config-application-package/src/test/java/com/yahoo/config/application/OverrideProcessorTest.java @@ -0,0 +1,307 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.config.application; + +import com.yahoo.config.provision.Environment; +import com.yahoo.config.provision.RegionName; +import org.custommonkey.xmlunit.XMLUnit; +import org.junit.Test; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.stream.XMLStreamException; +import javax.xml.transform.TransformerException; +import java.io.IOException; +import java.io.StringReader; + +/** + * @author lulf + * @since 5.22 + */ +public class OverrideProcessorTest { + + static { + XMLUnit.setIgnoreWhitespace(true); + } + + private static final String input = + "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" + + "<services xmlns:deploy=\"vespa\" xmlns:preprocess=\"?\" version=\"1.0\">" + + " <admin version=\"2.0\">" + + " <adminserver hostalias=\"node0\"/>" + + " </admin>" + + " <admin deploy:environment=\"prod\" version=\"2.0\">" + + " <adminserver hostalias=\"node1\"/>" + + " </admin>" + + " <content id=\"foo\" version=\"1.0\">" + + " <redundancy>1</redundancy>" + + " <documents>" + + " <document mode=\"index\" type=\"music.sd\"/>" + + " </documents>" + + " <nodes>" + + " <node distribution-key=\"0\" hostalias=\"node0\"/>" + + " </nodes>" + + " <nodes deploy:environment=\"prod\">" + + " <node distribution-key=\"0\" hostalias=\"node0\"/>" + + " <node distribution-key=\"1\" hostalias=\"node1\"/>" + + " </nodes>" + + " <nodes deploy:environment=\"staging\">" + + " <node distribution-key=\"0\" hostalias=\"node0\"/>" + + " <node deploy:region=\"us-west\" distribution-key=\"0\" hostalias=\"node1\"/>" + + " </nodes>" + + " <nodes deploy:environment=\"prod\" deploy:region=\"us-west\">" + + " <node distribution-key=\"0\" hostalias=\"node0\"/>" + + " <node distribution-key=\"1\" hostalias=\"node1\"/>" + + " <node distribution-key=\"2\" hostalias=\"node2\"/>" + + " </nodes>" + + " </content>" + + " <jdisc id=\"stateless\" version=\"1.0\">" + + " <search/>" + + " <component id=\"foo\" class=\"MyFoo\" bundle=\"foobundle\" />" + + " <component id=\"bar\" class=\"TestBar\" bundle=\"foobundle\" deploy:environment=\"staging\" />" + + " <component id=\"bar\" class=\"ProdBar\" bundle=\"foobundle\" deploy:environment=\"prod\" />" + + " <component id=\"baz\" class=\"ProdBaz\" bundle=\"foobundle\" deploy:environment=\"prod\" />" + + " <nodes>" + + " <node hostalias=\"node0\"/>" + + " </nodes>" + + " </jdisc>" + + "</services>"; + + + @Test + public void testParsingDefault() throws IOException, SAXException, XMLStreamException, ParserConfigurationException, TransformerException { + String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" + + "<services xmlns:deploy=\"vespa\" xmlns:preprocess=\"?\" version=\"1.0\">" + + " <admin version=\"2.0\">" + + " <adminserver hostalias=\"node0\"/>" + + " </admin>" + + " <content id=\"foo\" version=\"1.0\">" + + " <redundancy>1</redundancy>" + + " <documents>" + + " <document mode=\"index\" type=\"music.sd\"/>" + + " </documents>" + + " <nodes>" + + " <node distribution-key=\"0\" hostalias=\"node0\"/>" + + " </nodes>" + + " </content>" + + " <jdisc id=\"stateless\" version=\"1.0\">" + + " <search/>" + + " <component id=\"foo\" class=\"MyFoo\" bundle=\"foobundle\" />" + + " <nodes>" + + " <node hostalias=\"node0\"/>" + + " </nodes>" + + " </jdisc>" + + "</services>"; + assertOverride(Environment.test, RegionName.defaultName(), expected); + } + + @Test + public void testParsingEnvironmentAndRegion() throws ParserConfigurationException, IOException, SAXException, TransformerException { + String expected = + "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" + + "<services xmlns:deploy=\"vespa\" xmlns:preprocess=\"?\" version=\"1.0\">" + + " <admin version=\"2.0\">" + + " <adminserver hostalias=\"node1\"/>" + + " </admin>" + + " <content id=\"foo\" version=\"1.0\">" + + " <redundancy>1</redundancy>" + + " <documents>" + + " <document mode=\"index\" type=\"music.sd\"/>" + + " </documents>" + + " <nodes>" + + " <node distribution-key=\"0\" hostalias=\"node0\"/>" + + " <node distribution-key=\"1\" hostalias=\"node1\"/>" + + " <node distribution-key=\"2\" hostalias=\"node2\"/>" + + " </nodes>" + + " </content>" + + " <jdisc id=\"stateless\" version=\"1.0\">" + + " <search/>" + + " <component id=\"foo\" class=\"MyFoo\" bundle=\"foobundle\" />" + + " <component id=\"bar\" class=\"ProdBar\" bundle=\"foobundle\" />" + + " <component id=\"baz\" class=\"ProdBaz\" bundle=\"foobundle\" />" + + " <nodes>" + + " <node hostalias=\"node0\"/>" + + " </nodes>" + + " </jdisc>" + + "</services>"; + assertOverride(Environment.from("prod"), RegionName.from("us-west"), expected); + } + + @Test + public void testParsingEnvironmentUnknownRegion() throws ParserConfigurationException, IOException, SAXException, TransformerException { + String expected = + "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" + + "<services xmlns:deploy=\"vespa\" xmlns:preprocess=\"?\" version=\"1.0\">" + + " <admin version=\"2.0\">" + + " <adminserver hostalias=\"node1\"/>" + + " </admin>" + + " <content id=\"foo\" version=\"1.0\">" + + " <redundancy>1</redundancy>" + + " <documents>" + + " <document mode=\"index\" type=\"music.sd\"/>" + + " </documents>" + + " <nodes>" + + " <node distribution-key=\"0\" hostalias=\"node0\"/>" + + " <node distribution-key=\"1\" hostalias=\"node1\"/>" + + " </nodes>" + + " </content>" + + " <jdisc id=\"stateless\" version=\"1.0\">" + + " <search/>" + + " <component id=\"foo\" class=\"MyFoo\" bundle=\"foobundle\" />" + + " <component id=\"bar\" class=\"ProdBar\" bundle=\"foobundle\" />" + + " <component id=\"baz\" class=\"ProdBaz\" bundle=\"foobundle\" />" + + " <nodes>" + + " <node hostalias=\"node0\"/>" + + " </nodes>" + + " </jdisc>" + + "</services>"; + assertOverride(Environment.valueOf("prod"), RegionName.from("us-east"), expected); + } + + @Test + public void testParsingEnvironmentNoRegion() throws ParserConfigurationException, IOException, SAXException, TransformerException { + String expected = + "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" + + "<services xmlns:deploy=\"vespa\" xmlns:preprocess=\"?\" version=\"1.0\">" + + " <admin version=\"2.0\">" + + " <adminserver hostalias=\"node1\"/>" + + " </admin>" + + " <content id=\"foo\" version=\"1.0\">" + + " <redundancy>1</redundancy>" + + " <documents>" + + " <document mode=\"index\" type=\"music.sd\"/>" + + " </documents>" + + " <nodes>" + + " <node distribution-key=\"0\" hostalias=\"node0\"/>" + + " <node distribution-key=\"1\" hostalias=\"node1\"/>" + + " </nodes>" + + " </content>" + + " <jdisc id=\"stateless\" version=\"1.0\">" + + " <search/>" + + " <component id=\"foo\" class=\"MyFoo\" bundle=\"foobundle\" />" + + " <component id=\"bar\" class=\"ProdBar\" bundle=\"foobundle\" />" + + " <component id=\"baz\" class=\"ProdBaz\" bundle=\"foobundle\" />" + + " <nodes>" + + " <node hostalias=\"node0\"/>" + + " </nodes>" + + " </jdisc>" + + "</services>"; + assertOverride(Environment.from("prod"), RegionName.defaultName(), expected); + } + + @Test + public void testParsingUnknownEnvironment() throws ParserConfigurationException, IOException, SAXException, TransformerException { + String expected = + "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" + + "<services xmlns:deploy=\"vespa\" xmlns:preprocess=\"?\" version=\"1.0\">" + + " <admin version=\"2.0\">" + + " <adminserver hostalias=\"node0\"/>" + + " </admin>" + + " <content id=\"foo\" version=\"1.0\">" + + " <redundancy>1</redundancy>" + + " <documents>" + + " <document mode=\"index\" type=\"music.sd\"/>" + + " </documents>" + + " <nodes>" + + " <node distribution-key=\"0\" hostalias=\"node0\"/>" + + " </nodes>" + + " </content>" + + " <jdisc id=\"stateless\" version=\"1.0\">" + + " <search/>" + + " <component id=\"foo\" class=\"MyFoo\" bundle=\"foobundle\" />" + + " <nodes>" + + " <node hostalias=\"node0\"/>" + + " </nodes>" + + " </jdisc>" + + "</services>"; + assertOverride(Environment.from("dev"), RegionName.defaultName(), expected); + } + + @Test + public void testParsingUnknownEnvironmentUnknownRegion() throws ParserConfigurationException, IOException, SAXException, TransformerException { + String expected = + "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" + + "<services xmlns:deploy=\"vespa\" xmlns:preprocess=\"?\" version=\"1.0\">" + + " <admin version=\"2.0\">" + + " <adminserver hostalias=\"node0\"/>" + + " </admin>" + + " <content id=\"foo\" version=\"1.0\">" + + " <redundancy>1</redundancy>" + + " <documents>" + + " <document mode=\"index\" type=\"music.sd\"/>" + + " </documents>" + + " <nodes>" + + " <node distribution-key=\"0\" hostalias=\"node0\"/>" + + " </nodes>" + + " </content>" + + " <jdisc id=\"stateless\" version=\"1.0\">" + + " <search/>" + + " <component id=\"foo\" class=\"MyFoo\" bundle=\"foobundle\" />" + + " <nodes>" + + " <node hostalias=\"node0\"/>" + + " </nodes>" + + " </jdisc>" + + "</services>"; + assertOverride(Environment.from("test"), RegionName.from("us-west"), expected); + } + + @Test + public void testParsingInheritEnvironment() throws ParserConfigurationException, IOException, SAXException, TransformerException { + String expected = + "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" + + "<services xmlns:deploy=\"vespa\" xmlns:preprocess=\"?\" version=\"1.0\">" + + " <admin version=\"2.0\">" + + " <adminserver hostalias=\"node0\"/>" + + " </admin>" + + " <content id=\"foo\" version=\"1.0\">" + + " <redundancy>1</redundancy>" + + " <documents>" + + " <document mode=\"index\" type=\"music.sd\"/>" + + " </documents>" + + " <nodes>" + + " <node distribution-key=\"0\" hostalias=\"node1\"/>" + + " </nodes>" + + " </content>" + + " <jdisc id=\"stateless\" version=\"1.0\">" + + " <search/>" + + " <component id=\"foo\" class=\"MyFoo\" bundle=\"foobundle\" />" + + " <component id=\"bar\" class=\"TestBar\" bundle=\"foobundle\" />" + + " <nodes>" + + " <node hostalias=\"node0\"/>" + + " </nodes>" + + " </jdisc>" + + "</services>"; + assertOverride(Environment.from("staging"), RegionName.from("us-west"), expected); + } + + @Test(expected = IllegalArgumentException.class) + public void testParsingDifferentEnvInParentAndChild() throws ParserConfigurationException, IOException, SAXException, TransformerException { + String in = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" + + "<services xmlns:deploy=\"vespa\" xmlns:preprocess=\"?\" version=\"1.0\">" + + " <admin deploy:environment=\"prod\" version=\"2.0\">" + + " <adminserver deploy:environment=\"test\" hostalias=\"node1\"/>" + + " </admin>" + + "</services>"; + Document inputDoc = Xml.getDocument(new StringReader(in)); + new OverrideProcessor(Environment.from("prod"), RegionName.from("us-west")).process(inputDoc); + } + + @Test(expected = IllegalArgumentException.class) + public void testParsingDifferentRegionInParentAndChild() throws ParserConfigurationException, IOException, SAXException, TransformerException { + String in = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" + + "<services xmlns:deploy=\"vespa\" xmlns:preprocess=\"?\" version=\"1.0\">" + + " <admin deploy:region=\"us-west\" version=\"2.0\">" + + " <adminserver deploy:region=\"us-east\" hostalias=\"node1\"/>" + + " </admin>" + + "</services>"; + Document inputDoc = Xml.getDocument(new StringReader(in)); + new OverrideProcessor(Environment.defaultEnvironment(), RegionName.from("us-west")).process(inputDoc); + } + + private void assertOverride(Environment environment, RegionName region, String expected) throws TransformerException { + Document inputDoc = Xml.getDocument(new StringReader(input)); + Document newDoc = new OverrideProcessor(environment, region).process(inputDoc); + TestBase.assertDocument(expected, newDoc); + } + +} diff --git a/config-application-package/src/test/java/com/yahoo/config/application/PropertiesProcessorTest.java b/config-application-package/src/test/java/com/yahoo/config/application/PropertiesProcessorTest.java new file mode 100644 index 00000000000..c281bb28f17 --- /dev/null +++ b/config-application-package/src/test/java/com/yahoo/config/application/PropertiesProcessorTest.java @@ -0,0 +1,139 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.config.application; + +import org.custommonkey.xmlunit.XMLUnit; +import org.junit.Test; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.stream.XMLStreamException; +import javax.xml.transform.TransformerException; +import java.io.IOException; +import java.io.StringReader; +import java.util.Map; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +/** + * @author musum + */ +public class PropertiesProcessorTest { + + static { + XMLUnit.setIgnoreWhitespace(true); + } + + @Test + public void testPropertyValues() throws ParserConfigurationException, TransformerException, SAXException, IOException { + String input = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" + + "<services xmlns:deploy=\"vespa\" xmlns:preprocess=\"properties\" version=\"1.0\">" + + " <preprocess:properties>" + + " <slobrok.port>4099</slobrok.port>" + + " <redundancy>2</redundancy>" + + " </preprocess:properties>" + + " <admin version=\"2.0\">" + + " <adminserver hostalias=\"node0\"/>" + + " <slobroks>" + + " <slobrok hostalias=\"node1\" baseport=\"${slobrok.port}\"/>" + + " </slobroks>" + + " </admin>" + + "</services>"; + + PropertiesProcessor p = new PropertiesProcessor(); + p.process(Xml.getDocument(new StringReader(input))); + Map<String, String> properties = p.getProperties(); + assertThat(properties.size(), is(2)); + assertThat(properties.get("slobrok.port"), is("4099")); + assertThat(properties.get("redundancy"), is("2")); + } + + @Test + public void testPropertyApplying() throws IOException, SAXException, XMLStreamException, ParserConfigurationException, TransformerException { + String input = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" + + "<services xmlns:deploy=\"vespa\" xmlns:preprocess=\"properties\" version=\"1.0\">" + + " <preprocess:properties>" + + " <slobrok.port>4099</slobrok.port>" + + " <redundancy>2</redundancy>" + + " <doctype>music</doctype>" + + " <zero>0</zero>" + + " </preprocess:properties>" + + " <admin version=\"2.0\">" + + " <adminserver hostalias=\"node0\"/>" + + " <slobroks>" + + " <slobrok hostalias=\"node1\" baseport=\"${slobrok.port}\"/>" + + " </slobroks>" + + " </admin>" + + " <content id=\"foo\" version=\"1.0\">" + + " <redundancy>${redundancy}</redundancy>" + + " <documents>" + + " <document mode=\"index\" type=\"${doctype}.sd\"/>" + + " </documents>" + + " <nodes>" + + " <node distribution-key=\"${zero}\" hostalias=\"node${zero}\"/>" + + " </nodes>" + + " </content>" + + "</services>"; + + String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" + + "<services xmlns:deploy=\"vespa\" xmlns:preprocess=\"properties\" version=\"1.0\">" + + " <admin version=\"2.0\">" + + " <adminserver hostalias=\"node0\"/>" + + " <slobroks>" + + " <slobrok hostalias=\"node1\" baseport=\"4099\"/>" + + " </slobroks>" + + " </admin>" + + " <content id=\"foo\" version=\"1.0\">" + + " <redundancy>2</redundancy>" + + " <documents>" + + " <document mode=\"index\" type=\"music.sd\"/>" + + " </documents>" + + " <nodes>" + + " <node distribution-key=\"0\" hostalias=\"node0\"/>" + + " </nodes>" + + " </content>" + + "</services>"; + + + Document inputDoc = Xml.getDocument(new StringReader(input)); + Document newDoc = new PropertiesProcessor().process(inputDoc); + TestBase.assertDocument(expected, newDoc); + } + + + // TODO: Check that warning is actually logged + @Test + public void testWarnIfDuplicatePropertyForSameEnvironment() throws IOException, SAXException, XMLStreamException, ParserConfigurationException, TransformerException { + String input = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" + + "<services xmlns:deploy=\"vespa\" xmlns:preprocess=\"properties\" version=\"1.0\">" + + " <preprocess:properties>" + + " <slobrok.port>4099</slobrok.port>" + + " <slobrok.port>5000</slobrok.port>" + + " <redundancy>2</redundancy>" + + " </preprocess:properties>" + + " <admin version=\"2.0\">" + + " <adminserver hostalias=\"node0\"/>" + + " <slobroks>" + + " <slobrok hostalias=\"node1\" baseport=\"${slobrok.port}\"/>" + + " </slobroks>" + + " </admin>" + + "</services>"; + + + String expected = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" + + "<services xmlns:deploy=\"vespa\" xmlns:preprocess=\"properties\" version=\"1.0\">" + + " <admin version=\"2.0\">" + + " <adminserver hostalias=\"node0\"/>" + + " <slobroks>" + + " <slobrok hostalias=\"node1\" baseport=\"5000\"/>" + // Should get the last defined value + " </slobroks>" + + " </admin>" + + "</services>"; + + Document inputDoc = Xml.getDocument(new StringReader(input)); + Document newDoc = new PropertiesProcessor().process(inputDoc); + TestBase.assertDocument(expected, newDoc); + } +} + diff --git a/config-application-package/src/test/java/com/yahoo/config/application/TestBase.java b/config-application-package/src/test/java/com/yahoo/config/application/TestBase.java new file mode 100644 index 00000000000..8967fe9afdf --- /dev/null +++ b/config-application-package/src/test/java/com/yahoo/config/application/TestBase.java @@ -0,0 +1,33 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.config.application; + +import org.custommonkey.xmlunit.Diff; +import org.custommonkey.xmlunit.XMLUnit; +import org.w3c.dom.Document; + +import java.io.Reader; +import java.io.StringReader; + +import static org.junit.Assert.assertTrue; + +/** + * Utilities for tests + * + * @author musum + */ +public class TestBase { + static { + XMLUnit.setIgnoreWhitespace(true); + } + + static void assertDocument(String expected, Document output) { + Document expectedDoc = Xml.getDocument(new StringReader(expected)); + Diff diff = new Diff(expectedDoc, output); + assertTrue(diff.toString(), diff.identical()); + } + + public static void assertDocument(String expectedDocument, Reader document) { + Document output = Xml.getDocument(document); + assertDocument(expectedDocument, output); + } +} diff --git a/config-application-package/src/test/java/com/yahoo/config/application/XmlPreprocessorTest.java b/config-application-package/src/test/java/com/yahoo/config/application/XmlPreprocessorTest.java new file mode 100644 index 00000000000..93d218cd2c4 --- /dev/null +++ b/config-application-package/src/test/java/com/yahoo/config/application/XmlPreprocessorTest.java @@ -0,0 +1,165 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.config.application; + +import com.yahoo.config.provision.Environment; +import com.yahoo.config.provision.RegionName; +import org.junit.Test; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.stream.XMLStreamException; +import javax.xml.transform.TransformerException; +import java.io.File; +import java.io.IOException; +import java.io.StringReader; + +/** + * @author musum + */ +public class XmlPreprocessorTest { + + private static final File appDir = new File("src/test/resources/multienvapp"); + private static final File services = new File(appDir, "services.xml"); + + @Test + public void testPreProcessing() throws IOException, SAXException, XMLStreamException, ParserConfigurationException, TransformerException { + String expectedDev = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><services xmlns:deploy=\"vespa\" xmlns:preprocess=\"properties\" version=\"1.0\">\n" + + " <admin version=\"2.0\">\n" + + " <adminserver hostalias=\"node0\"/>\n" + + " </admin>\n" + + " <content id=\"foo\" version=\"1.0\">\n" + + " <redundancy>1</redundancy>\n" + + " <documents>\n" + + " <document mode=\"index\" type=\"music.sd\"/>\n" + + " </documents>\n" + + " <nodes>\n" + + " <node distribution-key=\"0\" hostalias=\"node0\"/>\n" + + " </nodes>\n" + + " </content>\n" + + " <jdisc id=\"stateless\" version=\"1.0\">\n" + + " <search/>\n" + + " <component bundle=\"foobundle\" class=\"MyFoo\" id=\"foo\"/>\n" + + " <component bundle=\"foobundle\" class=\"TestBar\" id=\"bar\"/>\n" + + " <nodes>\n" + + " <node hostalias=\"node0\" baseport=\"5000\"/>\n" + + " </nodes>\n" + + " </jdisc>\n" + + "</services>"; + + Document docDev = (new XmlPreProcessor(appDir, services, Environment.dev, RegionName.from("default")).run()); + TestBase.assertDocument(expectedDev, docDev); + + + String expectedUsWest = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><services xmlns:deploy=\"vespa\" xmlns:preprocess=\"properties\" version=\"1.0\">\n" + + " <admin version=\"2.0\">\n" + + " <adminserver hostalias=\"node1\"/>\n" + + " </admin>\n" + + " <content id=\"foo\" version=\"1.0\">\n" + + " <redundancy>1</redundancy>\n" + + " <documents>\n" + + " <document mode=\"index\" type=\"music.sd\"/>\n" + + " </documents>\n" + + " <nodes>\n" + + " <node distribution-key=\"0\" hostalias=\"node0\"/>\n" + + " <node distribution-key=\"1\" hostalias=\"node1\"/>\n" + + " <node distribution-key=\"2\" hostalias=\"node2\"/>\n" + + " </nodes>\n" + + " </content>\n" + + " <jdisc id=\"stateless\" version=\"1.0\">\n" + + " <search>\n" + + " <chain id=\"common\">\n" + + " <searcher id=\"MySearcher1\"/>\n" + + " <searcher id=\"MySearcher2\"/>\n" + + " </chain>\n" + + " </search>\n" + + " <component bundle=\"foobundle\" class=\"MyFoo\" id=\"foo\"/>\n" + + " <component bundle=\"foobundle\" class=\"ProdBar\" id=\"bar\"/>\n" + + " <component bundle=\"foobundle\" class=\"ProdBaz\" id=\"baz\"/>\n" + + " <nodes>\n" + + " <node hostalias=\"node0\" baseport=\"5001\"/>\n" + + " </nodes>\n" + + " </jdisc>\n" + + "</services>"; + + Document docUsWest = (new XmlPreProcessor(appDir, services, Environment.prod, RegionName.from("us-west"))).run(); + System.out.println(Xml.documentAsString(docUsWest)); + TestBase.assertDocument(expectedUsWest, docUsWest); + + String expectedUsEast = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><services xmlns:deploy=\"vespa\" xmlns:preprocess=\"properties\" version=\"1.0\">\n" + + " <admin version=\"2.0\">\n" + + " <adminserver hostalias=\"node1\"/>\n" + + " </admin>\n" + + " <content id=\"foo\" version=\"1.0\">\n" + + " <redundancy>1</redundancy>\n" + + " <documents>\n" + + " <document mode=\"index\" type=\"music.sd\"/>\n" + + " </documents>\n" + + " <nodes>\n" + + " <node distribution-key=\"0\" hostalias=\"node0\"/>\n" + + " <node distribution-key=\"1\" hostalias=\"node1\"/>\n" + + " </nodes>\n" + + " </content>\n" + + " <jdisc id=\"stateless\" version=\"1.0\">\n" + + " <search>\n" + + " <chain id=\"common\">\n" + + " <searcher id=\"MySearcher1\"/>\n" + + " <searcher id=\"MySearcher2\"/>\n" + + " </chain>\n" + + " </search>\n" + + " <component bundle=\"foobundle\" class=\"MyFoo\" id=\"foo\"/>\n" + + " <component bundle=\"foobundle\" class=\"ProdBar\" id=\"bar\"/>\n" + + " <component bundle=\"foobundle\" class=\"ProdBaz\" id=\"baz\"/>\n" + + " <nodes>\n" + + " <node hostalias=\"node0\" baseport=\"5002\"/>\n" + + " </nodes>\n" + + " </jdisc>\n" + + "</services>"; + + Document docUsEast = (new XmlPreProcessor(appDir, services, Environment.prod, RegionName.from("us-east"))).run(); + TestBase.assertDocument(expectedUsEast, docUsEast); + } + + @Test + public void testPropertiesWithOverlappingNames() throws IOException, SAXException, XMLStreamException, ParserConfigurationException, TransformerException { + String input = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" + + "<services xmlns:deploy=\"vespa\" xmlns:preprocess=\"properties\" version=\"1.0\">" + + " <preprocess:properties>" + + " <sherpa.host>gamma-usnc1.dht.yahoo.com</sherpa.host>" + + " <sherpa.port>4080</sherpa.port>" + + " <lidspacecompaction_interval>3600</lidspacecompaction_interval>" + + " <lidspacecompaction_interval deploy:environment='prod'>36000</lidspacecompaction_interval>" + + " <lidspacecompaction_allowedlidbloat>50000</lidspacecompaction_allowedlidbloat>" + + " <lidspacecompaction_allowedlidbloat deploy:environment='prod'>50000000</lidspacecompaction_allowedlidbloat>" + + " <lidspacecompaction_allowedlidbloatfactor>0.01</lidspacecompaction_allowedlidbloatfactor>" + + " <lidspacecompaction_allowedlidbloatfactor deploy:environment='prod'>0.91</lidspacecompaction_allowedlidbloatfactor>" + + " </preprocess:properties>" + + " <config name='a'>" + + " <a>${lidspacecompaction_interval}</a>" + + " <b>${lidspacecompaction_allowedlidbloat}</b>" + + " <c>${lidspacecompaction_allowedlidbloatfactor}</c>" + + " <host>${sherpa.host}</host>" + + " <port>${sherpa.port}</port>" + + " </config>" + + " <admin version=\"2.0\">" + + " <adminserver hostalias=\"node0\"/>" + + " </admin>" + + "</services>"; + + String expectedProd = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" + + "<services xmlns:deploy=\"vespa\" xmlns:preprocess=\"properties\" version=\"1.0\">" + + " <config name='a'>" + + " <a>36000</a>" + + " <b>50000000</b>" + + " <c>0.91</c>" + + " <host>gamma-usnc1.dht.yahoo.com</host>" + + " <port>4080</port>" + + " </config>" + + " <admin version=\"2.0\">" + + " <adminserver hostalias=\"node0\"/>" + + " </admin>" + + "</services>"; + Document docDev = (new XmlPreProcessor(appDir, new StringReader(input), Environment.prod, RegionName.from("default")).run()); + TestBase.assertDocument(expectedProd, docDev); + } +} diff --git a/config-application-package/src/test/java/com/yahoo/config/model/application/provider/FilesApplicationFileTest.java b/config-application-package/src/test/java/com/yahoo/config/model/application/provider/FilesApplicationFileTest.java new file mode 100644 index 00000000000..ac207de7231 --- /dev/null +++ b/config-application-package/src/test/java/com/yahoo/config/model/application/provider/FilesApplicationFileTest.java @@ -0,0 +1,23 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.config.model.application.provider; + +import com.google.common.io.Files; +import com.yahoo.config.application.api.ApplicationFile; +import com.yahoo.config.application.api.ApplicationFileTest; +import com.yahoo.path.Path; + +import java.io.File; + +/** + * @author lulf + * @since 5.1 + */ +public class FilesApplicationFileTest extends ApplicationFileTest { + + @Override + public ApplicationFile getApplicationFile(Path path) throws Exception { + File tmp = Files.createTempDir(); + writeAppTo(tmp); + return new FilesApplicationFile(path, new File(tmp, path.getRelative())); + } +} diff --git a/config-application-package/src/test/java/com/yahoo/config/model/application/provider/FilesApplicationPackageTest.java b/config-application-package/src/test/java/com/yahoo/config/model/application/provider/FilesApplicationPackageTest.java new file mode 100644 index 00000000000..77842a693b7 --- /dev/null +++ b/config-application-package/src/test/java/com/yahoo/config/model/application/provider/FilesApplicationPackageTest.java @@ -0,0 +1,100 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.config.model.application.provider; + +import com.yahoo.config.application.TestBase; +import com.yahoo.config.application.api.RuleConfigDeriver; +import com.yahoo.config.application.api.ApplicationPackage; +import com.yahoo.config.provision.Environment; +import com.yahoo.config.provision.RegionName; +import com.yahoo.config.provision.Zone; +import com.yahoo.io.IOUtils; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; +import org.xml.sax.SAXException; + +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.TransformerException; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; + +/** + * @author lulf + * @since 5.25 + */ +public class FilesApplicationPackageTest { + + @Rule + public TemporaryFolder temporaryFolder = new TemporaryFolder(); + + @Test + public void testPreprocessing() throws IOException, TransformerException, ParserConfigurationException, SAXException { + File appDir = temporaryFolder.newFolder(); + IOUtils.copyDirectory(new File("src/test/resources/multienvapp"), appDir); + assertTrue(new File(appDir, "services.xml").exists()); + assertTrue(new File(appDir, "hosts.xml").exists()); + FilesApplicationPackage app = FilesApplicationPackage.fromFile(appDir); + + ApplicationPackage processed = app.preprocess(new Zone(Environment.dev, RegionName.defaultName()), + new RuleConfigDeriver() { + @Override + public void derive(String ruleBaseDir, String outputDir) throws Exception { + } + }, + new BaseDeployLogger()); + assertTrue(new File(appDir, ".preprocessed").exists()); + String expectedServices = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><services xmlns:deploy=\"vespa\" xmlns:preprocess=\"properties\" version=\"1.0\">\n" + + " <admin version=\"2.0\">\n" + + " <adminserver hostalias=\"node0\"/>\n" + + " </admin>\n" + + " <content id=\"foo\" version=\"1.0\">\n" + + " <redundancy>1</redundancy>\n" + + " <documents>\n" + + " <document mode=\"index\" type=\"music.sd\"/>\n" + + " </documents>\n" + + " <nodes>\n" + + " <node distribution-key=\"0\" hostalias=\"node0\"/>\n" + + " </nodes>\n" + + " </content>\n" + + " <jdisc id=\"stateless\" version=\"1.0\">\n" + + " <search/>\n" + + " <component bundle=\"foobundle\" class=\"MyFoo\" id=\"foo\"/>\n" + + " <component bundle=\"foobundle\" class=\"TestBar\" id=\"bar\"/>\n" + + " <nodes>\n" + + " <node hostalias=\"node0\" baseport=\"5000\"/>\n" + + " </nodes>\n" + + " </jdisc>\n" + + "</services>"; + TestBase.assertDocument(expectedServices, processed.getServices()); + String expectedHosts = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?><hosts xmlns:deploy=\"vespa\" xmlns:preprocess=\"properties\">\n" + + " <host name=\"bar.yahoo.com\">\n" + + " <alias>node1</alias>\n" + + " </host>\n" + + "</hosts>"; + TestBase.assertDocument(expectedHosts, processed.getHosts()); + } + + @Test + public void testDeploymentXmlNotAvailable() throws IOException, TransformerException, ParserConfigurationException, SAXException { + File appDir = new File("src/test/resources/multienvapp"); + assertFalse(new File(appDir, "deployment.xml").exists()); + FilesApplicationPackage app = FilesApplicationPackage.fromFile(appDir); + assertFalse(app.getDeployment().isPresent()); + } + + @Test + public void testDeploymentXml() throws IOException, TransformerException, ParserConfigurationException, SAXException { + File appDir = new File("src/test/resources/app-with-deployment"); + final File deployment = new File(appDir, "deployment.xml"); + assertTrue(deployment.exists()); + FilesApplicationPackage app = FilesApplicationPackage.fromFile(appDir); + assertTrue(app.getDeployment().isPresent()); + assertThat(IOUtils.readAll(new FileReader(deployment)), is(IOUtils.readAll(app.getDeployment().get()))); + } +}
\ No newline at end of file diff --git a/config-application-package/src/test/java/com/yahoo/config/model/application/provider/PreGeneratedFileRegistryTestCase.java b/config-application-package/src/test/java/com/yahoo/config/model/application/provider/PreGeneratedFileRegistryTestCase.java new file mode 100644 index 00000000000..15b7b32f26e --- /dev/null +++ b/config-application-package/src/test/java/com/yahoo/config/model/application/provider/PreGeneratedFileRegistryTestCase.java @@ -0,0 +1,43 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.config.model.application.provider; + +import com.yahoo.config.application.api.FileRegistry; +import org.junit.Test; + +import java.io.StringReader; +import java.util.Arrays; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * @author tonytv + */ +public class PreGeneratedFileRegistryTestCase { + @Test + public void importAndExport() { + FileRegistry fileRegistry = new MockFileRegistry(); + String serializedRegistry = PreGeneratedFileRegistry.exportRegistry(fileRegistry); + + PreGeneratedFileRegistry importedRegistry = + PreGeneratedFileRegistry.importRegistry( + new StringReader(serializedRegistry)); + + assertTrue(importedRegistry.getPaths().containsAll( + Arrays.asList( + MockFileRegistry.entry1.relativePath, + MockFileRegistry.entry2.relativePath))); + + assertEquals(2, importedRegistry.getPaths().size()); + + checkConsistentEntry(MockFileRegistry.entry1, importedRegistry); + checkConsistentEntry(MockFileRegistry.entry2, importedRegistry); + + assertEquals(fileRegistry.fileSourceHost(), + importedRegistry.fileSourceHost()); + } + + void checkConsistentEntry(FileRegistry.Entry entry, FileRegistry registry) { + assertEquals(entry.reference, registry.addFile(entry.relativePath)); + } +} diff --git a/config-application-package/src/test/java/com/yahoo/config/model/application/provider/StaticConfigDefinitionRepoTest.java b/config-application-package/src/test/java/com/yahoo/config/model/application/provider/StaticConfigDefinitionRepoTest.java new file mode 100644 index 00000000000..3cc51718b41 --- /dev/null +++ b/config-application-package/src/test/java/com/yahoo/config/model/application/provider/StaticConfigDefinitionRepoTest.java @@ -0,0 +1,42 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.config.model.application.provider; + +import com.yahoo.config.model.application.provider.StaticConfigDefinitionRepo; +import com.yahoo.config.model.api.ConfigDefinitionRepo; +import com.yahoo.io.IOUtils; +import com.yahoo.vespa.config.ConfigDefinitionKey; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +import java.io.File; +import java.io.IOException; + +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; + +/** + * @author lulf + * @since 5.10 + */ +public class StaticConfigDefinitionRepoTest { + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + @Test + public void testThatRepoIsCorrectlyInitialized() throws IOException { + File topDir = folder.newFolder(); + File defDir = new File(topDir, "classes"); + defDir.mkdir(); + addFile(defDir, new ConfigDefinitionKey("foo", "foons"), "namespace=foons\nval int\n"); + addFile(defDir, new ConfigDefinitionKey("bar", "barns"), "namespace=barns\nval string\n"); + ConfigDefinitionRepo repo = new StaticConfigDefinitionRepo(defDir); + assertThat(repo.getConfigDefinitions().size(), is(2)); + } + + private void addFile(File defDir, ConfigDefinitionKey key, String content) throws IOException { + String fileName = key.getNamespace() + "." + key.getName() + ".def"; + File def = new File(defDir, fileName); + IOUtils.writeFile(def, content, false); + } +} |