From 56b7df3e8f660b849a6a7d7eb8f7758260e6025a Mon Sep 17 00:00:00 2001 From: dshimo Date: Tue, 27 Feb 2024 12:27:07 -0600 Subject: [PATCH] init ServerConfigDocument unit tests --- .../plugins/config/ServerConfigDocument.java | 96 ++++---- .../config/ServerConfigDocumentTest.java | 218 ++++++++++++++++++ .../serverConfig/liberty/wlp/etc/server.env | 2 + .../wlp/usr/servers/defaultServer/server.env | 4 +- .../wlp/usr/servers/defaultServer/server.xml | 2 + .../wlp/usr/servers/defaultServer/server2.env | 2 + .../liberty/wlp/usr/shared/server.env | 2 + .../resources/servers/bootstrap2.properties | 2 + .../servers/bootstrapsInclude.properties | 1 + .../resources/servers/definedVariables.xml | 5 + .../servers/testIncludeParseVariables.xml | 4 + 11 files changed, 294 insertions(+), 44 deletions(-) create mode 100644 src/test/java/io/openliberty/tools/common/config/ServerConfigDocumentTest.java create mode 100644 src/test/resources/serverConfig/liberty/wlp/etc/server.env create mode 100644 src/test/resources/serverConfig/liberty/wlp/usr/servers/defaultServer/server2.env create mode 100644 src/test/resources/serverConfig/liberty/wlp/usr/shared/server.env create mode 100644 src/test/resources/servers/bootstrap2.properties create mode 100644 src/test/resources/servers/bootstrapsInclude.properties create mode 100644 src/test/resources/servers/definedVariables.xml create mode 100644 src/test/resources/servers/testIncludeParseVariables.xml diff --git a/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigDocument.java b/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigDocument.java index 1a1a2e4d..3cab8eb7 100644 --- a/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigDocument.java +++ b/src/main/java/io/openliberty/tools/common/plugins/config/ServerConfigDocument.java @@ -123,12 +123,8 @@ public ServerConfigDocument(CommonLoggerI log, File serverXML, File configDir, F // LCLS constructor public ServerConfigDocument(CommonLoggerI log) { - this.log = log; - props = new Properties(); - defaultProps = new Properties(); - - // TODO: populate with workspace information - libertyDirectoryPropertyToFile = new HashMap(); + // TODO: populate libertyDirectoryPropertyToFile with workspace information + initializeFields(log, null, null, null); } private DocumentBuilder getDocumentBuilder() { @@ -154,22 +150,7 @@ private DocumentBuilder getDocumentBuilder() { private void initializeAppsLocation(CommonLoggerI log, File serverXML, File configDir, File bootstrapFile, Map bootstrapProp, File serverEnvFile, boolean giveConfigDirPrecedence, Map libertyDirPropertyFiles) { try { - this.log = log; - serverXMLFile = serverXML; - configDirectory = configDir; - if (libertyDirPropertyFiles != null) { - libertyDirectoryPropertyToFile = new HashMap(libertyDirPropertyFiles); - } else { - log.warn("The properties for directories are null and could lead to application locations not being resolved correctly."); - libertyDirectoryPropertyToFile = new HashMap(); - } - - locations = new HashSet(); - names = new HashSet(); - namelessLocations = new HashSet(); - locationsAndNames = new HashMap(); - props = new Properties(); - defaultProps = new Properties(); + initializeFields(log, serverXML, configDir, libertyDirPropertyFiles); Document doc = parseDocument(serverXMLFile); @@ -191,28 +172,10 @@ private void initializeAppsLocation(CommonLoggerI log, File serverXML, File conf parseVariablesForDefaultValues(doc); // 2. get variables from server.env - File cfgFile = findConfigFile("server.env", serverEnvFile, giveConfigDirPrecedence); - - if (cfgFile != null) { - parseProperties(new FileInputStream(cfgFile)); - } + processServerEnv(serverEnvFile, giveConfigDirPrecedence); // 3. get variables from bootstrap.properties - File cfgDirFile = getFileFromConfigDirectory("bootstrap.properties"); - - if (giveConfigDirPrecedence && cfgDirFile != null) { - parseProperties(new FileInputStream(cfgDirFile)); - } else if (bootstrapProp != null && !bootstrapProp.isEmpty()) { - for (Map.Entry entry : bootstrapProp.entrySet()) { - if (entry.getValue() != null) { - props.setProperty(entry.getKey(),entry.getValue()); - } - } - } else if (bootstrapFile != null && bootstrapFile.exists()) { - parseProperties(new FileInputStream(bootstrapFile)); - } else if (cfgDirFile != null) { - parseProperties(new FileInputStream(cfgDirFile)); - } + processBootstrapProperties(bootstrapFile, bootstrapProp, giveConfigDirPrecedence); // 4. parse variables from include files (both default and non-default values - which we store separately) parseIncludeVariables(doc); @@ -238,6 +201,53 @@ private void initializeAppsLocation(CommonLoggerI log, File serverXML, File conf } } + public void processServerEnv(File serverEnvFile, boolean giveConfigDirPrecedence) + throws Exception, FileNotFoundException { + File cfgFile = findConfigFile("server.env", serverEnvFile, giveConfigDirPrecedence); + if (cfgFile != null) { + parseProperties(new FileInputStream(cfgFile)); + } + } + + public void initializeFields(CommonLoggerI log, File serverXML, File configDir, + Map libertyDirPropertyFiles) { + this.log = log; + serverXMLFile = serverXML; + configDirectory = configDir; + if (libertyDirPropertyFiles != null) { + libertyDirectoryPropertyToFile = new HashMap(libertyDirPropertyFiles); + } else { + log.warn("The properties for directories are null and could lead to application locations not being resolved correctly."); + libertyDirectoryPropertyToFile = new HashMap(); + } + + locations = new HashSet(); + names = new HashSet(); + namelessLocations = new HashSet(); + locationsAndNames = new HashMap(); + props = new Properties(); + defaultProps = new Properties(); + } + + public void processBootstrapProperties(File bootstrapFile, Map bootstrapProp, boolean giveConfigDirPrecedence) + throws Exception, FileNotFoundException { + File cfgDirFile = getFileFromConfigDirectory("bootstrap.properties"); + + if (giveConfigDirPrecedence && cfgDirFile != null) { + parseProperties(new FileInputStream(cfgDirFile)); + } else if (bootstrapProp != null && !bootstrapProp.isEmpty()) { + for (Map.Entry entry : bootstrapProp.entrySet()) { + if (entry.getValue() != null) { + props.setProperty(entry.getKey(),entry.getValue()); + } + } + } else if (bootstrapFile != null && bootstrapFile.exists()) { + parseProperties(new FileInputStream(bootstrapFile)); + } else if (cfgDirFile != null) { + parseProperties(new FileInputStream(cfgDirFile)); + } + } + //Checks for application names in the document. Will add locations without names to a Set private void parseNames(Document doc, String expression) throws XPathExpressionException, IOException, SAXException { // parse input document @@ -539,7 +549,7 @@ private boolean isValidURL(String url) { } - private void parseVariablesForDefaultValues(Document doc) throws XPathExpressionException { + public void parseVariablesForDefaultValues(Document doc) throws XPathExpressionException { parseVariables(doc, true, false, false); } diff --git a/src/test/java/io/openliberty/tools/common/config/ServerConfigDocumentTest.java b/src/test/java/io/openliberty/tools/common/config/ServerConfigDocumentTest.java new file mode 100644 index 00000000..b59c0923 --- /dev/null +++ b/src/test/java/io/openliberty/tools/common/config/ServerConfigDocumentTest.java @@ -0,0 +1,218 @@ +package io.openliberty.tools.common.config; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.logging.Logger; + +import javax.xml.xpath.XPathExpressionException; + +import org.junit.Test; +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import io.openliberty.tools.common.TestLogger; +import io.openliberty.tools.common.plugins.config.ServerConfigDocument; + +/* + Docs: https://openliberty.io/docs/latest/reference/config/server-configuration-overview.html + # Variable substitution in increasing order of precedence (7 overrides 1) + 1. variable default values in the server.xml file + 2. environment variables + 3. bootstrap.properties + 4. Java system properties + 5. Variables loaded from files in the ${server.config.dir}/variables directory or other directories as specified by the VARIABLE_SOURCE_DIRS environment variable + 6. variable values declared in the server.xml file + 7. variables declared on the command line + */ + +public class ServerConfigDocumentTest { + public final static Logger LOGGER = Logger.getLogger(ServerConfigDocumentTest.class.getName()); + private final static String RESOURCES_DIR = "src/test/resources/"; + private final static String WLP_DIR = "serverConfig/liberty/wlp/"; + private final static String WLP_USER_DIR = "serverConfig/liberty/wlp/usr/"; + private final static String DEFAULT_USER_SERVER_DIR = "servers/defaultServer/"; + private final static String DEFAULT_SERVER_DIR = "servers/"; + + // 1. variable default values in server.xml file + // 6. variable values declared in the server.xml file + @Test + public void processServerXml() throws FileNotFoundException, IOException, XPathExpressionException, SAXException { + File serversDir = new File(RESOURCES_DIR + DEFAULT_SERVER_DIR); + Document doc; + + // no variables defined + ServerConfigDocument configDocument = new ServerConfigDocument(new TestLogger()); + File empty = new File(serversDir, "emptyList.xml"); + doc = configDocument.parseDocument(empty); + configDocument.parseVariablesForBothValues(doc); + assertTrue(configDocument.getDefaultProperties().isEmpty() && configDocument.getProperties().isEmpty()); + + // variables defined + configDocument = new ServerConfigDocument(new TestLogger()); + File defined = new File(serversDir, "definedVariables.xml"); + doc = configDocument.parseDocument(defined); + configDocument.parseVariablesForBothValues(doc); + assertEquals(1, configDocument.getDefaultProperties().size()); + assertEquals("9080", configDocument.getDefaultProperties().getProperty("default.http.port")); + assertEquals(1, configDocument.getProperties().size()); + assertEquals("9081", configDocument.getProperties().getProperty("http.port")); + + // variables defined in files + configDocument = new ServerConfigDocument(new TestLogger()); + configDocument.initializeFields(new TestLogger(), new File(serversDir, "server.xml"), null, null); + File include = new File(serversDir, "testIncludeParseVariables.xml"); + doc = configDocument.parseDocument(include); + assertTrue(configDocument.getDefaultProperties().isEmpty() && configDocument.getProperties().isEmpty()); + configDocument.parseIncludeVariables(doc); + assertEquals(1, configDocument.getDefaultProperties().size()); + assertEquals("9080", configDocument.getDefaultProperties().getProperty("default.http.port")); + assertEquals(1, configDocument.getProperties().size()); + assertEquals("9081", configDocument.getProperties().getProperty("http.port")); + + // server.xmls read in increasing precedence + // TODO: refactor + // configDropins/defaults + // server.xml + // configDropins/overrides + } + + // server.env files are read in increasing precedence + // 1. {wlp.install.dir}/etc + // 2. {wlp.user.dir}/shared + // 3. {server.config.dir} + // Table of directories: https://openliberty.io/docs/latest/reference/directory-locations-properties.html + @Test + public void processServerEnv() throws FileNotFoundException, Exception { + File serverDir = new File(RESOURCES_DIR + WLP_USER_DIR + DEFAULT_USER_SERVER_DIR); + File specificFile = new File(serverDir, "server2.env"); + ServerConfigDocument configDocument = new ServerConfigDocument(new TestLogger()); + configDocument.initializeFields(new TestLogger(), null, serverDir, null); + configDocument.processServerEnv(specificFile, false); + assertEquals("TEST", configDocument.getProperties().getProperty("keystore_password")); + + configDocument = new ServerConfigDocument(new TestLogger()); + configDocument.initializeFields(new TestLogger(), null, serverDir, null); + configDocument.processServerEnv(specificFile, true); + assertNotEquals("TEST", configDocument.getProperties().getProperty("keystore_password")); + + // TODO: multiple file locations + // File wlpDir = new File(RESOURCES_DIR + WLP_DIR); + } + + // 2. environment variables + // TODO: test without using processServerEnv + @Test + public void environmentVariables() throws FileNotFoundException, Exception { + // TODO: alt var lookups - https://github.com/OpenLiberty/ci.common/issues/126 + File serverConfigDir = new File(RESOURCES_DIR + WLP_USER_DIR + DEFAULT_USER_SERVER_DIR); + ServerConfigDocument configDocument = new ServerConfigDocument(new TestLogger()); + configDocument.initializeFields(new TestLogger(), new File(serverConfigDir, "server.xml"), serverConfigDir, null); + Document doc = configDocument.parseDocument(new File(serverConfigDir, "server.xml")); + + // env var not set + configDocument.parseVariablesForBothValues(doc); + assertEquals("${my.env.var}", configDocument.getProperties().getProperty("my.env.var.level")); + + // replace non-alphanumeric characters with '_' and to uppercase + configDocument.processServerEnv(new File(serverConfigDir, "server2.env"), false); + assertEquals("3", configDocument.getProperties().getProperty("MY_ENV_VAR")); + configDocument.parseVariablesForBothValues(doc); + // assertEquals("3", configDocument.getProperties().getProperty("my.env.var.level")); + + // replace non-alphanumeric characters with '_' + configDocument.processServerEnv(null, true); + assertEquals("2", configDocument.getProperties().getProperty("my_env_var")); + configDocument.parseVariablesForBothValues(doc); + // assertEquals("2", configDocument.getProperties().getProperty("my.env.var.level")); + } + + + // 3. bootstrap.properties + @Test + public void processBootstrapProperties() throws FileNotFoundException, Exception { + File serversDir = new File(RESOURCES_DIR + DEFAULT_SERVER_DIR); + File altBootstrapPropertiesFile = new File(serversDir, "bootstrap2.properties"); + Map bootstrapProp = new HashMap(); + bootstrapProp.put("http.port", "1000"); + ServerConfigDocument configDocument; + + // lowest to highest precedence + // default bootstrap.properties in config dir + configDocument = new ServerConfigDocument(new TestLogger()); + configDocument.initializeFields(new TestLogger(), null, serversDir, null); + configDocument.processBootstrapProperties(new File("DOES_NOT_EXIST"), new HashMap<>(), false); + assertEquals(1, configDocument.getProperties().size()); + + // use bootstrapFile + configDocument = new ServerConfigDocument(new TestLogger()); + configDocument.initializeFields(new TestLogger(), null, serversDir, null); + configDocument.processBootstrapProperties(altBootstrapPropertiesFile, new HashMap<>(), false); + assertEquals(2, configDocument.getProperties().size()); + assertEquals("9080", configDocument.getProperties().getProperty("http.port")); + + // test bootstrapProperty overrides + File serverXml = new File(serversDir, "definedVariables.xml"); + configDocument = new ServerConfigDocument(new TestLogger()); + configDocument.initializeFields(new TestLogger(), null, serversDir, null); + Document doc = configDocument.parseDocument(serverXml); + configDocument.parseVariablesForBothValues(doc); + assertEquals("9081", configDocument.getProperties().getProperty("http.port")); + configDocument.processBootstrapProperties(altBootstrapPropertiesFile, bootstrapProp, false); + assertEquals("1000", configDocument.getProperties().getProperty("http.port")); + + // use giveConfigDirPrecedence + configDocument = new ServerConfigDocument(new TestLogger()); + configDocument.initializeFields(new TestLogger(), null, serversDir, null); + configDocument.processBootstrapProperties(altBootstrapPropertiesFile, new HashMap<>(), true); + assertEquals(1, configDocument.getProperties().size()); + + // TODO: bootstraps.include + // configDocument = new ServerConfigDocument(new TestLogger()); + // configDocument.initializeFields(new TestLogger(), null, serversDir, null); + // configDocument.processBootstrapProperties(new File(serversDir, "bootstrapsInclude.properties"), new HashMap<>(), false); + // assertEquals("9080", configDocument.getProperties().getProperty("http.port")); + } + + // 4. Java system properties + @Test + public void jvmOptions() { + + } + + // 5. Variables loaded from files in the ${server.config.dir}/variables directory or other + // directories as specified by the VARIABLE_SOURCE_DIRS environment variable + @Test + public void variablesDir() { + // TODO: not yet implemented. read copied dir instead of src for now + // see https://github.com/OpenLiberty/ci.common/issues/126 + } + + + // 7. variables declared on the command line + @Test + public void CLI() { + + } + + // TODO: test each overrides layer + // jvm.options override server.env + // bootstrap.properties override jvm.options and server.env + // server.xml override bootstrap.properties, jvm.options, and server.env + @Test + public void overrides() { + File serversDir = new File(RESOURCES_DIR + WLP_USER_DIR + DEFAULT_USER_SERVER_DIR); + + // server.xml overrides server.env + ServerConfigDocument configDocument = new ServerConfigDocument(new TestLogger(), + new File(serversDir, "server.xml"), serversDir, new File(serversDir, "bootstrap.properties"), + new HashMap<>(), new File(serversDir, "server.env"), false, null); + assertEquals("new_value", configDocument.getProperties().getProperty("overriden_value")); + } +} diff --git a/src/test/resources/serverConfig/liberty/wlp/etc/server.env b/src/test/resources/serverConfig/liberty/wlp/etc/server.env new file mode 100644 index 00000000..1eff25ce --- /dev/null +++ b/src/test/resources/serverConfig/liberty/wlp/etc/server.env @@ -0,0 +1,2 @@ +http.port=9080 +https.port=9081 \ No newline at end of file diff --git a/src/test/resources/serverConfig/liberty/wlp/usr/servers/defaultServer/server.env b/src/test/resources/serverConfig/liberty/wlp/usr/servers/defaultServer/server.env index 2b0a48a6..d0a8e5a0 100644 --- a/src/test/resources/serverConfig/liberty/wlp/usr/servers/defaultServer/server.env +++ b/src/test/resources/serverConfig/liberty/wlp/usr/servers/defaultServer/server.env @@ -1 +1,3 @@ -keystore_password=C7ANPlAi0MQD154BJ5ZOURn \ No newline at end of file +keystore_password=C7ANPlAi0MQD154BJ5ZOURn +overriden_value=old_value +my_env_var=2 \ No newline at end of file diff --git a/src/test/resources/serverConfig/liberty/wlp/usr/servers/defaultServer/server.xml b/src/test/resources/serverConfig/liberty/wlp/usr/servers/defaultServer/server.xml index bc801f17..fcf023fd 100644 --- a/src/test/resources/serverConfig/liberty/wlp/usr/servers/defaultServer/server.xml +++ b/src/test/resources/serverConfig/liberty/wlp/usr/servers/defaultServer/server.xml @@ -21,4 +21,6 @@ + + \ No newline at end of file diff --git a/src/test/resources/serverConfig/liberty/wlp/usr/servers/defaultServer/server2.env b/src/test/resources/serverConfig/liberty/wlp/usr/servers/defaultServer/server2.env new file mode 100644 index 00000000..6e646b70 --- /dev/null +++ b/src/test/resources/serverConfig/liberty/wlp/usr/servers/defaultServer/server2.env @@ -0,0 +1,2 @@ +keystore_password=TEST +MY_ENV_VAR=3 \ No newline at end of file diff --git a/src/test/resources/serverConfig/liberty/wlp/usr/shared/server.env b/src/test/resources/serverConfig/liberty/wlp/usr/shared/server.env new file mode 100644 index 00000000..6a88f0c8 --- /dev/null +++ b/src/test/resources/serverConfig/liberty/wlp/usr/shared/server.env @@ -0,0 +1,2 @@ +http.port=9082 +https.port=9083 \ No newline at end of file diff --git a/src/test/resources/servers/bootstrap2.properties b/src/test/resources/servers/bootstrap2.properties new file mode 100644 index 00000000..d2ada960 --- /dev/null +++ b/src/test/resources/servers/bootstrap2.properties @@ -0,0 +1,2 @@ +extras.filename=extraFeatures.xml +http.port=9080 \ No newline at end of file diff --git a/src/test/resources/servers/bootstrapsInclude.properties b/src/test/resources/servers/bootstrapsInclude.properties new file mode 100644 index 00000000..a83d44ec --- /dev/null +++ b/src/test/resources/servers/bootstrapsInclude.properties @@ -0,0 +1 @@ +bootstrap.include=bootstrap2.properties \ No newline at end of file diff --git a/src/test/resources/servers/definedVariables.xml b/src/test/resources/servers/definedVariables.xml new file mode 100644 index 00000000..7ee645c6 --- /dev/null +++ b/src/test/resources/servers/definedVariables.xml @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/src/test/resources/servers/testIncludeParseVariables.xml b/src/test/resources/servers/testIncludeParseVariables.xml new file mode 100644 index 00000000..950c5351 --- /dev/null +++ b/src/test/resources/servers/testIncludeParseVariables.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file