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() {
132 public String getKarafDistro() {
133 String groupId = System.getProperty(KARAF_DISTRO_GROUPID_PROP, KARAF_DISTRO_GROUPID);
134 String artifactId = System.getProperty(KARAF_DISTRO_ARTIFACTID_PROP, KARAF_DISTRO_ARTIFACTID);
135 String version = System.getProperty(KARAF_DISTRO_VERSION_PROP);
136 String type = System.getProperty(KARAF_DISTRO_TYPE_PROP, KARAF_DISTRO_TYPE);
137 if (version == null) {
138 // We use a properties file to retrieve ${karaf.version}, instead of
139 // .versionAsInProject()
140 // This avoids forcing all users to depend on Karaf in their POMs
141 Properties abstractConfigTestBaseProps = new Properties();
142 try (InputStream abstractConfigTestBaseInputStream = Thread.currentThread().getContextClassLoader()
143 .getResourceAsStream(PROPERTIES_FILENAME)) {
144 abstractConfigTestBaseProps.load(abstractConfigTestBaseInputStream);
145 } catch (final IOException e) {
146 LOG.error("Unable to load {} to determine the Karaf version", PROPERTIES_FILENAME, e);
148 version = abstractConfigTestBaseProps.getProperty(KARAF_DISTRO_VERSION_PROP);
150 MavenArtifactUrlReference karafUrl = maven().groupId(groupId).artifactId(artifactId).version(version)
152 return karafUrl.getURL();
155 protected Option mvnLocalRepoOption() {
156 String mvnRepoLocal = System.getProperty(MAVEN_REPO_LOCAL, "");
157 LOG.info("mvnLocalRepo \"{}\"", mvnRepoLocal);
158 return editConfigurationFilePut(ETC_ORG_OPS4J_PAX_URL_MVN_CFG, ORG_OPS4J_PAX_URL_MVN_LOCAL_REPOSITORY,
163 public Option[] config() {
164 Option[] options = new Option[] {
165 when(Boolean.getBoolean(KARAF_DEBUG_PROP))
166 .useOptions(KarafDistributionOption.debugConfiguration(KARAF_DEBUG_PORT, true)),
167 karafDistributionConfiguration().frameworkUrl(getKarafDistro())
168 .unpackDirectory(new File(PAX_EXAM_UNPACK_DIRECTORY)).useDeployFolder(false),
169 when(Boolean.getBoolean(KEEP_UNPACK_DIRECTORY_PROP)).useOptions(keepRuntimeFolder()),
170 features(getFeatureRepo(), getFeatureName()),
171 mavenBundle("org.apache.aries.quiesce", "org.apache.aries.quiesce.api", "1.0.0"), getLoggingOption(),
172 mvnLocalRepoOption(),
173 editConfigurationFilePut(ETC_ORG_OPS4J_PAX_LOGGING_CFG, "log4j2.rootLogger.level", "INFO") };
174 return OptionUtils.combine(options, getAdditionalOptions());
178 @SuppressWarnings("IllegalCatch")
179 public void setup() throws Exception {
180 String moduleName = getModuleName();
181 String instanceName = getInstanceName();
182 if (moduleName == null || instanceName == null) {
186 LOG.info("Module: {} Instance: {} attempting to configure.", moduleName, instanceName);
187 Stopwatch stopWatch = Stopwatch.createStarted();
188 ObjectName objectName = null;
189 for (int i = 0; i < MODULE_TIMEOUT_MILLIS; i++) {
191 ConfigRegistry configRegistryClient = new ConfigRegistryJMXClient(
192 ManagementFactory.getPlatformMBeanServer());
193 objectName = configRegistryClient.lookupConfigBean(moduleName, instanceName);
194 LOG.info("Module: {} Instance: {} ObjectName: {}.", moduleName, instanceName, objectName);
196 } catch (final Exception e) {
197 if (i < MODULE_TIMEOUT_MILLIS) {
205 if (objectName != null) {
206 LOG.info("Module: {} Instance: {} configured after {} ms", moduleName, instanceName,
207 stopWatch.elapsed(TimeUnit.MILLISECONDS));
209 throw new RuntimeException("NOT FOUND Module: " + moduleName + " Instance: " + instanceName
210 + " configured after " + stopWatch.elapsed(TimeUnit.MILLISECONDS) + " ms");
215 public TestRule watcher = new TestWatcher() {
217 protected void starting(final Description description) {
218 LOG.info("TestWatcher: Starting test: {}", description.getDisplayName());
222 protected void finished(final Description description) {
223 LOG.info("TestWatcher: Finished test: {}", description.getDisplayName());
227 protected void succeeded(final Description description) {
228 LOG.info("TestWatcher: Test succeeded: {}", description.getDisplayName());
232 protected void failed(final Throwable ex, final Description description) {
233 LOG.info("TestWatcher: Test failed: {}", description.getDisplayName(), ex);
237 protected void skipped(final AssumptionViolatedException ex, final Description description) {
238 LOG.info("TestWatcher: Test skipped: {} ", description.getDisplayName(), ex);