Alon Bar-Lev has uploaded a new change for review. Change subject: utils: enable UT for EngineLocalConfig ......................................................................
utils: enable UT for EngineLocalConfig EngineLocalConfig is singleton without initialization method, as such we cannot mock it nor control who in the chain actually call it. the solution taken is to allow overriding its instance using pre-defined configuration, so that the default getInstance() method will return already constructed instance. Change-Id: Ie877a056e11a83badd5675cf24ffa48898b5786d Signed-off-by: Alon Bar-Lev <[email protected]> --- M backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/EngineLocalConfig.java M backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/LocalConfig.java M backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/crypt/EngineEncryptionUtilsTest.java M backend/manager/modules/utils/src/test/resources/localconfig.conf.ref 4 files changed, 63 insertions(+), 14 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/83/16183/1 diff --git a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/EngineLocalConfig.java b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/EngineLocalConfig.java index 0ddbf67..f4cf854 100644 --- a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/EngineLocalConfig.java +++ b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/EngineLocalConfig.java @@ -3,6 +3,7 @@ import java.io.File; import java.net.MalformedURLException; import java.net.URL; +import java.util.Map; import org.apache.log4j.Logger; @@ -21,12 +22,28 @@ private static final String VARS_PATH = "/etc/ovirt-engine/engine.conf"; // This is a singleton and this is the instance: - private static final EngineLocalConfig instance = new EngineLocalConfig(); + private static EngineLocalConfig instance; public static EngineLocalConfig getInstance() { + return getInstance(null); + } + + public static synchronized EngineLocalConfig getInstance(Map<String, String> values) { + if (values != null) { + instance = new EngineLocalConfig(values); + } + else { + if (instance == null) { + instance = new EngineLocalConfig(); + } + } return instance; } + private EngineLocalConfig(Map<String, String> values) { + setConfig(values); + } + private EngineLocalConfig() { // Locate the defaults file and add it to the list: String defaultsPath = System.getenv("ENGINE_DEFAULTS"); diff --git a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/LocalConfig.java b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/LocalConfig.java index be90438..221eb4f 100644 --- a/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/LocalConfig.java +++ b/backend/manager/modules/utils/src/main/java/org/ovirt/engine/core/utils/LocalConfig.java @@ -28,6 +28,8 @@ // The log: private static final Logger log = Logger.getLogger(LocalConfig.class); + private static final String SENSITIVE_KEYS = "SENSITIVE_KEYS"; + // Compile regular expressions: private static final Pattern EMPTY_LINE = Pattern.compile("^\\s*(#.*)?$"); private static final Pattern KEY_VALUE_EXPRESSION = Pattern.compile("^\\s*(\\w+)=(.*)$"); @@ -35,10 +37,21 @@ // The properties object storing the current values of the parameters: private Map<String, String> values = new HashMap<String, String>(); - protected void loadConfig(String defaultsPath, String varsPath) { - // Set basic defaults - values.put("SENSITIVE_KEYS", ""); + /** + * Use configuration from map. + * @param values map. + */ + protected void setConfig(Map<String, String> values) { + this.values = values; + dumpConfig(); + } + /** + * Use configuration from files. + * @param defaultsPath path to file containing the defaults. + * @param varsPath path to file and directory of file.d. + */ + protected void loadConfig(String defaultsPath, String varsPath) { // This is the list of configuration files that will be loaded and // merged (the initial size is 2 because usually we will have only two // configuration files to merge, the defaults and the variables): @@ -90,9 +103,15 @@ } } - // Dump the properties to the log (this should probably be DEBUG, but as - // it will usually happen only once, during the startup, is not that a - // problem to use INFO): + dumpConfig(); + } + + /** + * Dump all configuration to the log. + * this should probably be DEBUG, but as it will usually happen only once, + * during the startup, is not that a roblem to use INFO. + */ + private void dumpConfig() { if (log.isInfoEnabled()) { Set<String> keys = values.keySet(); List<String> list = new ArrayList<String>(keys.size()); @@ -452,6 +471,12 @@ } public String[] getSensitiveKeys() { - return getProperty("SENSITIVE_KEYS").split(","); + String sensitiveKeys = values.get(SENSITIVE_KEYS); + if (sensitiveKeys == null) { + return new String[] {}; + } + else { + return sensitiveKeys.split(","); + } } } diff --git a/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/crypt/EngineEncryptionUtilsTest.java b/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/crypt/EngineEncryptionUtilsTest.java index a60250a..49ff2bd 100644 --- a/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/crypt/EngineEncryptionUtilsTest.java +++ b/backend/manager/modules/utils/src/test/java/org/ovirt/engine/core/utils/crypt/EngineEncryptionUtilsTest.java @@ -3,24 +3,32 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; -import java.io.File; import java.io.UnsupportedEncodingException; import java.net.URLDecoder; -import java.security.KeyStore; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; import org.junit.BeforeClass; import org.junit.Test; +import org.ovirt.engine.core.utils.EngineLocalConfig; + public class EngineEncryptionUtilsTest { @BeforeClass public static void before() throws UnsupportedEncodingException { - EngineEncryptionUtils.keystoreFile = new File(URLDecoder.decode(ClassLoader.getSystemResource("key.p12").getPath(), "UTF-8")); - EngineEncryptionUtils.keystorePassword = new KeyStore.PasswordProtection("NoSoup4U".toCharArray()); - EngineEncryptionUtils.keystoreAlias = "1"; + String keystore = URLDecoder.decode(ClassLoader.getSystemResource("key.p12").getPath(), "UTF-8"); + String keystorePassword = "NoSoup4U"; + Map<String, String> config = new HashMap<String, String>(); + config.put("ENGINE_PKI_TRUST_STORE", keystore); + config.put("ENGINE_PKI_TRUST_STORE_PASSWORD", keystorePassword); + config.put("ENGINE_PKI_ENGINE_STORE", keystore); + config.put("ENGINE_PKI_ENGINE_STORE_PASSWORD", keystorePassword); + config.put("ENGINE_PKI_ENGINE_STORE_ALIAS", "1"); + EngineLocalConfig.getInstance(config); } @Test diff --git a/backend/manager/modules/utils/src/test/resources/localconfig.conf.ref b/backend/manager/modules/utils/src/test/resources/localconfig.conf.ref index d033e17..b2d5bf7 100644 --- a/backend/manager/modules/utils/src/test/resources/localconfig.conf.ref +++ b/backend/manager/modules/utils/src/test/resources/localconfig.conf.ref @@ -1,4 +1,3 @@ -SENSITIVE_KEYS= key01= key02=value2 key03=value31 -- To view, visit http://gerrit.ovirt.org/16183 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ie877a056e11a83badd5675cf24ffa48898b5786d Gerrit-PatchSet: 1 Gerrit-Project: ovirt-engine Gerrit-Branch: master Gerrit-Owner: Alon Bar-Lev <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
