Update integration test base classes to use Log4J2
[controller.git] / opendaylight / config / config-it-base / src / main / java / org / opendaylight / controller / config / it / base / AbstractConfigTestBase.java
1 /*
2  * Copyright (c) 2015, 2017 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.config.it.base;
10
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;
19
20 import com.google.common.base.Stopwatch;
21 import java.io.File;
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;
45
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";
51
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";
58
59     /*
60      * Default values for karaf distro type, groupId, and artifactId
61      */
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";
65
66     /*
67      * Property names to override defaults for karaf distro artifactId, groupId,
68      * version, and type
69      */
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";
74
75     /**
76      * Property file used to store the Karaf distribution version.
77      */
78     private static final String PROPERTIES_FILENAME = "abstractconfigtestbase.properties";
79
80     /*
81      * Wait up to 10s for our configured module to come up
82      */
83     private static final int MODULE_TIMEOUT_MILLIS = 60000;
84
85     /**
86      * This method need only be overridden if using the config system.
87      *
88      * @deprecated
89      *
90      * @return the config module name
91      */
92     @Deprecated
93     public String getModuleName() {
94         return null;
95     }
96
97     /**
98      * This method need only be overridden if using the config system.
99      *
100      * @deprecated
101      *
102      * @return the config module instance name
103      */
104     @Deprecated
105     public String getInstanceName() {
106         return null;
107     }
108
109     public abstract MavenUrlReference getFeatureRepo();
110
111     public abstract String getFeatureName();
112
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()));
120         return option;
121     }
122
123     /**
124      * Override this method to provide more options to config.
125      *
126      * @return An array of additional config options
127      */
128     protected Option[] getAdditionalOptions() {
129         return null;
130     }
131
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);
147             }
148             version = abstractConfigTestBaseProps.getProperty(KARAF_DISTRO_VERSION_PROP);
149         }
150         MavenArtifactUrlReference karafUrl = maven().groupId(groupId).artifactId(artifactId).version(version)
151                 .type(type);
152         return karafUrl.getURL();
153     }
154
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,
159                 mvnRepoLocal);
160     }
161
162     @Configuration
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());
175     }
176
177     @Before
178     @SuppressWarnings("IllegalCatch")
179     public void setup() throws Exception {
180         String moduleName = getModuleName();
181         String instanceName = getInstanceName();
182         if (moduleName == null || instanceName == null) {
183             return;
184         }
185
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++) {
190             try {
191                 ConfigRegistry configRegistryClient = new ConfigRegistryJMXClient(
192                         ManagementFactory.getPlatformMBeanServer());
193                 objectName = configRegistryClient.lookupConfigBean(moduleName, instanceName);
194                 LOG.info("Module: {} Instance: {} ObjectName: {}.", moduleName, instanceName, objectName);
195                 break;
196             } catch (final Exception e) {
197                 if (i < MODULE_TIMEOUT_MILLIS) {
198                     Thread.sleep(1);
199                     continue;
200                 } else {
201                     throw e;
202                 }
203             }
204         }
205         if (objectName != null) {
206             LOG.info("Module: {} Instance: {} configured after {} ms", moduleName, instanceName,
207                     stopWatch.elapsed(TimeUnit.MILLISECONDS));
208         } else {
209             throw new RuntimeException("NOT FOUND Module: " + moduleName + " Instance: " + instanceName
210                     + " configured after " + stopWatch.elapsed(TimeUnit.MILLISECONDS) + " ms");
211         }
212     }
213
214     @Rule
215     public TestRule watcher = new TestWatcher() {
216         @Override
217         protected void starting(final Description description) {
218             LOG.info("TestWatcher: Starting test: {}", description.getDisplayName());
219         }
220
221         @Override
222         protected void finished(final Description description) {
223             LOG.info("TestWatcher: Finished test: {}", description.getDisplayName());
224         }
225
226         @Override
227         protected void succeeded(final Description description) {
228             LOG.info("TestWatcher: Test succeeded: {}", description.getDisplayName());
229         }
230
231         @Override
232         protected void failed(final Throwable ex, final Description description) {
233             LOG.info("TestWatcher: Test failed: {}", description.getDisplayName(), ex);
234         }
235
236         @Override
237         protected void skipped(final AssumptionViolatedException ex, final Description description) {
238             LOG.info("TestWatcher: Test skipped: {} ", description.getDisplayName(), ex);
239         }
240     };
241 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.