a61867b3018c6255c2e0ef49e6125dbf77942d0e
[controller.git] / opendaylight / config / config-it-base / src / main / java / org / opendaylight / controller / config / it / base / AbstractConfigTestBase.java
1 /*
2  * Copyright (c) 2015 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.maven;
12 import static org.ops4j.pax.exam.CoreOptions.when;
13 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;
14 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.features;
15 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
16 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
17
18 import com.google.common.base.Stopwatch;
19
20 import java.io.File;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.lang.management.ManagementFactory;
24 import java.util.Properties;
25 import java.util.concurrent.TimeUnit;
26
27 import javax.management.ObjectName;
28
29 import org.junit.Before;
30 import org.junit.Rule;
31 import org.junit.internal.AssumptionViolatedException;
32 import org.junit.rules.TestRule;
33 import org.junit.rules.TestWatcher;
34 import org.junit.runner.Description;
35 import org.opendaylight.controller.config.api.ConfigRegistry;
36 import org.opendaylight.controller.config.util.ConfigRegistryJMXClient;
37 import org.ops4j.pax.exam.Configuration;
38 import org.ops4j.pax.exam.Option;
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
48     private static final String PAX_EXAM_UNPACK_DIRECTORY = "target/exam";
49     private static final String KARAF_DEBUG_PORT = "5005";
50     private static final String KARAF_DEBUG_PROP = "karaf.debug";
51     private static final String KEEP_UNPACK_DIRECTORY_PROP = "karaf.keep.unpack";
52     private static final Logger LOG = LoggerFactory.getLogger(AbstractConfigTestBase.class);
53     public static final String ORG_OPS4J_PAX_LOGGING_CFG = "etc/org.ops4j.pax.logging.cfg";
54
55     /*
56      * Default values for karaf distro type, groupId, and artifactId
57      */
58     private static final String KARAF_DISTRO_TYPE = "zip";
59     private static final String KARAF_DISTRO_ARTIFACTID = "apache-karaf";
60     private static final String KARAF_DISTRO_GROUPID = "org.apache.karaf";
61
62     /*
63      * Property names to override defaults for karaf distro artifactId, groupId, version, and type
64      */
65     private static final String KARAF_DISTRO_VERSION_PROP = "karaf.distro.version";
66     private static final String KARAF_DISTRO_TYPE_PROP = "karaf.distro.type";
67     private static final String KARAF_DISTRO_ARTIFACTID_PROP = "karaf.distro.artifactId";
68     private static final String KARAF_DISTRO_GROUPID_PROP = "karaf.distro.groupId";
69
70     /**
71      * Property file used to store the Karaf distribution version
72      */
73     private static final String PROPERTIES_FILENAME = "abstractconfigtestbase.properties";
74
75     /*
76      * Wait up to 10s for our configured module to come up
77      */
78     private static final int MODULE_TIMEOUT_MILLIS = 60000;
79
80     public abstract String getModuleName();
81
82     public abstract String getInstanceName();
83
84     public abstract MavenUrlReference getFeatureRepo();
85
86     public abstract String getFeatureName();
87
88     public Option getLoggingOption() {
89         Option option = editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,
90                         logConfiguration(AbstractConfigTestBase.class),
91                         LogLevel.INFO.name());
92         return option;
93     }
94
95     public String logConfiguration(Class<?> klazz) {
96         return "log4j.logger." + klazz.getPackage().getName();
97     }
98
99     public String getKarafDistro() {
100         String groupId = System.getProperty(KARAF_DISTRO_GROUPID_PROP,KARAF_DISTRO_GROUPID);
101         String artifactId = System.getProperty(KARAF_DISTRO_ARTIFACTID_PROP,KARAF_DISTRO_ARTIFACTID);
102         String version = System.getProperty(KARAF_DISTRO_VERSION_PROP);
103         String type = System.getProperty(KARAF_DISTRO_TYPE_PROP,KARAF_DISTRO_TYPE);
104         if (version == null) {
105             // We use a properties file to retrieve ${karaf.version}, instead of .versionAsInProject()
106             // This avoids forcing all users to depend on Karaf in their POMs
107             Properties abstractConfigTestBaseProps = new Properties();
108             try (InputStream abstractConfigTestBaseInputStream = Thread.currentThread().getContextClassLoader()
109                     .getResourceAsStream(PROPERTIES_FILENAME)) {
110                 abstractConfigTestBaseProps.load(abstractConfigTestBaseInputStream);
111             } catch (IOException e) {
112                 LOG.error("Unable to load {} to determine the Karaf version", PROPERTIES_FILENAME, e);
113             }
114             version = abstractConfigTestBaseProps.getProperty(KARAF_DISTRO_VERSION_PROP);
115         }
116         MavenArtifactUrlReference karafUrl = maven()
117                 .groupId(groupId)
118                 .artifactId(artifactId)
119                 .version(version)
120                 .type(type);
121         return karafUrl.getURL();
122     }
123
124     @Configuration
125     public Option[] config() {
126         Option[] options = new Option[] {
127                 when(Boolean.getBoolean(KARAF_DEBUG_PROP))
128                         .useOptions(KarafDistributionOption.debugConfiguration(KARAF_DEBUG_PORT, true)),
129                 karafDistributionConfiguration().frameworkUrl(getKarafDistro())
130                         .unpackDirectory(new File(PAX_EXAM_UNPACK_DIRECTORY))
131                         .useDeployFolder(false),
132                 when(Boolean.getBoolean(KEEP_UNPACK_DIRECTORY_PROP)).useOptions(keepRuntimeFolder()),
133                 features(getFeatureRepo(), getFeatureName()),
134                 getLoggingOption()};
135         return options;
136     }
137
138     @Before
139     public void setup() throws Exception {
140         LOG.info("Module: {} Instance: {} attempting to configure.",
141                 getModuleName(),getInstanceName());
142         Stopwatch stopWatch = Stopwatch.createStarted();
143         ObjectName objectName = null;
144         for(int i = 0;i<MODULE_TIMEOUT_MILLIS;i++) {
145             try {
146                 ConfigRegistry configRegistryClient = new ConfigRegistryJMXClient(ManagementFactory
147                         .getPlatformMBeanServer());
148                 objectName = configRegistryClient.lookupConfigBean(getModuleName(), getInstanceName());
149                 LOG.info("Module: {} Instance: {} ObjectName: {}.",
150                         getModuleName(),getInstanceName(),objectName);
151                 break;
152             } catch (Exception e) {
153                 if(i<MODULE_TIMEOUT_MILLIS) {
154                     Thread.sleep(1);
155                     continue;
156                 } else {
157                     throw e;
158                 }
159             }
160         }
161         if(objectName != null) {
162             LOG.info("Module: {} Instance: {} configured after {} ms",
163                 getModuleName(),getInstanceName(),
164                 stopWatch.elapsed(TimeUnit.MILLISECONDS));
165         } else {
166             throw new RuntimeException("NOT FOUND Module: " +getModuleName() + " Instance: " + getInstanceName() +
167                     " configured after " + stopWatch.elapsed(TimeUnit.MILLISECONDS) + " ms");
168         }
169     }
170
171     @Rule
172     public TestRule watcher = new TestWatcher() {
173
174         @Override
175         protected void starting(Description description) {
176             LOG.info("TestWatcher: Starting test: {}", description.getDisplayName());
177         }
178
179         @Override
180         protected void finished(Description description) {
181             LOG.info("TestWatcher: Finished test: {}", description.getDisplayName());
182         }
183
184         @Override
185         protected void succeeded(Description description) {
186             LOG.info("TestWatcher: Test succeeded: {}", description.getDisplayName());
187         }
188
189         @Override
190         protected void failed(Throwable ex, Description description) {
191             LOG.info("TestWatcher: Test failed: {}", description.getDisplayName(), ex);
192         }
193
194         @Override
195         protected void skipped(AssumptionViolatedException ex, Description description) {
196             LOG.info("TestWatcher: Test skipped: {} ", description.getDisplayName(), ex);
197         }
198     };
199
200 }

©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.