2 * Copyright (c) 2015, 2017 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.config.it.base;
11 import static org.ops4j.pax.exam.CoreOptions.composite;
12 import static org.ops4j.pax.exam.CoreOptions.maven;
13 import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
14 import static org.ops4j.pax.exam.CoreOptions.when;
15 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;
16 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.features;
17 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
18 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
20 import com.google.common.base.Stopwatch;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.lang.management.ManagementFactory;
25 import java.util.Properties;
26 import java.util.concurrent.TimeUnit;
27 import javax.management.ObjectName;
28 import org.junit.Before;
29 import org.junit.Rule;
30 import org.junit.internal.AssumptionViolatedException;
31 import org.junit.rules.TestRule;
32 import org.junit.rules.TestWatcher;
33 import org.junit.runner.Description;
34 import org.opendaylight.controller.config.api.ConfigRegistry;
35 import org.opendaylight.controller.config.util.ConfigRegistryJMXClient;
36 import org.ops4j.pax.exam.Configuration;
37 import org.ops4j.pax.exam.Option;
38 import org.ops4j.pax.exam.OptionUtils;
39 import org.ops4j.pax.exam.karaf.options.KarafDistributionOption;
40 import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
41 import org.ops4j.pax.exam.options.MavenArtifactUrlReference;
42 import org.ops4j.pax.exam.options.MavenUrlReference;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
46 public abstract class AbstractConfigTestBase {
47 private static final String MAVEN_REPO_LOCAL = "maven.repo.local";
48 private static final String ORG_OPS4J_PAX_URL_MVN_LOCAL_REPOSITORY = "org.ops4j.pax.url.mvn.localRepository";
49 private static final String ETC_ORG_OPS4J_PAX_URL_MVN_CFG = "etc/org.ops4j.pax.url.mvn.cfg";
50 private static final String ETC_ORG_OPS4J_PAX_LOGGING_CFG = "etc/org.ops4j.pax.logging.cfg";
52 private static final String PAX_EXAM_UNPACK_DIRECTORY = "target/exam";
53 private static final String KARAF_DEBUG_PORT = "5005";
54 private static final String KARAF_DEBUG_PROP = "karaf.debug";
55 private static final String KEEP_UNPACK_DIRECTORY_PROP = "karaf.keep.unpack";
56 private static final Logger LOG = LoggerFactory.getLogger(AbstractConfigTestBase.class);
57 public static final String ORG_OPS4J_PAX_LOGGING_CFG = "etc/org.ops4j.pax.logging.cfg";
60 * Default values for karaf distro type, groupId, and artifactId
62 private static final String KARAF_DISTRO_TYPE = "zip";
63 private static final String KARAF_DISTRO_ARTIFACTID = "opendaylight-karaf-empty";
64 private static final String KARAF_DISTRO_GROUPID = "org.opendaylight.odlparent";
67 * Property names to override defaults for karaf distro artifactId, groupId,
70 private static final String KARAF_DISTRO_VERSION_PROP = "karaf.distro.version";
71 private static final String KARAF_DISTRO_TYPE_PROP = "karaf.distro.type";
72 private static final String KARAF_DISTRO_ARTIFACTID_PROP = "karaf.distro.artifactId";
73 private static final String KARAF_DISTRO_GROUPID_PROP = "karaf.distro.groupId";
76 * Property file used to store the Karaf distribution version.
78 private static final String PROPERTIES_FILENAME = "abstractconfigtestbase.properties";
81 * Wait up to 10s for our configured module to come up
83 private static final int MODULE_TIMEOUT_MILLIS = 60000;
86 * This method need only be overridden if using the config system.
90 * @return the config module name
93 public String getModuleName() {
98 * This method need only be overridden if using the config system.
102 * @return the config module instance name
105 public String getInstanceName() {
109 public abstract MavenUrlReference getFeatureRepo();
111 public abstract String getFeatureName();
113 public Option getLoggingOption() {
114 Option option = editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,
115 "log4j2.logger.config-it-base.name",
116 AbstractConfigTestBase.class.getPackage().getName());
117 option = composite(option, editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,
118 "log4j2.logger.config-it-base.level",
119 LogLevel.INFO.name()));
124 * Override this method to provide more options to config.
126 * @return An array of additional config options
128 protected Option[] getAdditionalOptions() {
133 * Returns a Log4J logging configuration property name for the given class's package name of the form
134 * "log4j.logger.package_name".
136 * @deprecated The karaf logging provider is now Log4J2 so logging configurations must conform to the Log4J2 style.
137 * This method is kept for compilation backwards compatibility but will be removed in a future release.
140 public String logConfiguration(final Class<?> klazz) {
141 return "log4j.logger." + klazz.getPackage().getName();
144 public String getKarafDistro() {
145 String groupId = System.getProperty(KARAF_DISTRO_GROUPID_PROP, KARAF_DISTRO_GROUPID);
146 String artifactId = System.getProperty(KARAF_DISTRO_ARTIFACTID_PROP, KARAF_DISTRO_ARTIFACTID);
147 String version = System.getProperty(KARAF_DISTRO_VERSION_PROP);
148 String type = System.getProperty(KARAF_DISTRO_TYPE_PROP, KARAF_DISTRO_TYPE);
149 if (version == null) {
150 // We use a properties file to retrieve ${karaf.version}, instead of
151 // .versionAsInProject()
152 // This avoids forcing all users to depend on Karaf in their POMs
153 Properties abstractConfigTestBaseProps = new Properties();
154 try (InputStream abstractConfigTestBaseInputStream = Thread.currentThread().getContextClassLoader()
155 .getResourceAsStream(PROPERTIES_FILENAME)) {
156 abstractConfigTestBaseProps.load(abstractConfigTestBaseInputStream);
157 } catch (final IOException e) {
158 LOG.error("Unable to load {} to determine the Karaf version", PROPERTIES_FILENAME, e);
160 version = abstractConfigTestBaseProps.getProperty(KARAF_DISTRO_VERSION_PROP);
162 MavenArtifactUrlReference karafUrl = maven().groupId(groupId).artifactId(artifactId).version(version)
164 return karafUrl.getURL();
167 protected Option mvnLocalRepoOption() {
168 String mvnRepoLocal = System.getProperty(MAVEN_REPO_LOCAL, "");
169 LOG.info("mvnLocalRepo \"{}\"", mvnRepoLocal);
170 return editConfigurationFilePut(ETC_ORG_OPS4J_PAX_URL_MVN_CFG, ORG_OPS4J_PAX_URL_MVN_LOCAL_REPOSITORY,
175 public Option[] config() {
176 Option[] options = new Option[] {
177 when(Boolean.getBoolean(KARAF_DEBUG_PROP))
178 .useOptions(KarafDistributionOption.debugConfiguration(KARAF_DEBUG_PORT, true)),
179 karafDistributionConfiguration().frameworkUrl(getKarafDistro())
180 .unpackDirectory(new File(PAX_EXAM_UNPACK_DIRECTORY)).useDeployFolder(false),
181 when(Boolean.getBoolean(KEEP_UNPACK_DIRECTORY_PROP)).useOptions(keepRuntimeFolder()),
182 features(getFeatureRepo(), getFeatureName()),
183 mavenBundle("org.apache.aries.quiesce", "org.apache.aries.quiesce.api", "1.0.0"), getLoggingOption(),
184 mvnLocalRepoOption(),
185 editConfigurationFilePut(ETC_ORG_OPS4J_PAX_LOGGING_CFG, "log4j2.rootLogger.level", "INFO") };
186 return OptionUtils.combine(options, getAdditionalOptions());
190 @SuppressWarnings("IllegalCatch")
191 public void setup() throws Exception {
192 String moduleName = getModuleName();
193 String instanceName = getInstanceName();
194 if (moduleName == null || instanceName == null) {
198 LOG.info("Module: {} Instance: {} attempting to configure.", moduleName, instanceName);
199 Stopwatch stopWatch = Stopwatch.createStarted();
200 ObjectName objectName = null;
201 for (int i = 0; i < MODULE_TIMEOUT_MILLIS; i++) {
203 ConfigRegistry configRegistryClient = new ConfigRegistryJMXClient(
204 ManagementFactory.getPlatformMBeanServer());
205 objectName = configRegistryClient.lookupConfigBean(moduleName, instanceName);
206 LOG.info("Module: {} Instance: {} ObjectName: {}.", moduleName, instanceName, objectName);
208 } catch (final Exception e) {
209 if (i < MODULE_TIMEOUT_MILLIS) {
217 if (objectName != null) {
218 LOG.info("Module: {} Instance: {} configured after {} ms", moduleName, instanceName,
219 stopWatch.elapsed(TimeUnit.MILLISECONDS));
221 throw new RuntimeException("NOT FOUND Module: " + moduleName + " Instance: " + instanceName
222 + " configured after " + stopWatch.elapsed(TimeUnit.MILLISECONDS) + " ms");
227 public TestRule watcher = new TestWatcher() {
229 protected void starting(final Description description) {
230 LOG.info("TestWatcher: Starting test: {}", description.getDisplayName());
234 protected void finished(final Description description) {
235 LOG.info("TestWatcher: Finished test: {}", description.getDisplayName());
239 protected void succeeded(final Description description) {
240 LOG.info("TestWatcher: Test succeeded: {}", description.getDisplayName());
244 protected void failed(final Throwable ex, final Description description) {
245 LOG.info("TestWatcher: Test failed: {}", description.getDisplayName(), ex);
249 protected void skipped(final AssumptionViolatedException ex, final Description description) {
250 LOG.info("TestWatcher: Test skipped: {} ", description.getDisplayName(), ex);