Option to performance profile SingleFeatureTest
[odlparent.git] / features-test / src / main / java / org / opendaylight / odlparent / featuretest / SingleFeatureTest.java
index d58a6c2067768e9924dd21b3cae4ce378dabcf6b..54e4726eda74919212c76bc3fb7fff3fb0fc2d51 100644 (file)
@@ -12,22 +12,31 @@ import static org.opendaylight.odlparent.featuretest.Constants.ORG_OPENDAYLIGHT_
 import static org.opendaylight.odlparent.featuretest.Constants.ORG_OPENDAYLIGHT_FEATURETEST_FEATUREVERSION_PROP;
 import static org.opendaylight.odlparent.featuretest.Constants.ORG_OPENDAYLIGHT_FEATURETEST_URI_PROP;
 import static org.ops4j.pax.exam.CoreOptions.maven;
+import static org.ops4j.pax.exam.CoreOptions.when;
 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.configureConsole;
 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;
+import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.features;
 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.logLevel;
 
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
 import java.net.URI;
 import java.net.URISyntaxException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
 
 import javax.inject.Inject;
 
 import org.apache.karaf.features.Feature;
 import org.apache.karaf.features.FeaturesService;
 import org.apache.karaf.features.Repository;
+import org.apache.karaf.features.internal.model.Features;
+import org.apache.karaf.features.internal.model.JaxbUtil;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
@@ -40,13 +49,17 @@ import org.ops4j.pax.exam.options.extra.VMOption;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-
 @RunWith(PerRepoTestRunner.class)
 public class SingleFeatureTest {
+
     private static final String MAVEN_REPO_LOCAL = "maven.repo.local";
     private static final String ORG_OPS4J_PAX_URL_MVN_LOCAL_REPOSITORY = "org.ops4j.pax.url.mvn.localRepository";
     private static final String ORG_OPS4J_PAX_URL_MVN_REPOSITORIES = "org.ops4j.pax.url.mvn.repositories";
     private static final String ETC_ORG_OPS4J_PAX_URL_MVN_CFG = "etc/org.ops4j.pax.url.mvn.cfg";
+    private static final String ETC_ORG_OPS4J_PAX_LOGGING_CFG = "etc/org.ops4j.pax.logging.cfg";
+    private static final String KEEP_UNPACK_DIRECTORY_PROP = "karaf.keep.unpack";
+    private static final String PROFILE_PROP = "karaf.featureTest.profile";
+
     private static final String LOG4J_LOGGER_ORG_OPENDAYLIGHT_YANGTOOLS_FEATURETEST =
             "log4j.logger.org.opendaylight.odlparent.featuretest";
     private static final Logger LOG = LoggerFactory.getLogger(SingleFeatureTest.class);
@@ -57,9 +70,8 @@ public class SingleFeatureTest {
     private static final String ORG_OPS4J_PAX_LOGGING_CFG = "etc/org.ops4j.pax.logging.cfg";
 
     /*
-     * Default values for karaf distro version, type, groupId, and artifactId
+     * Default values for karaf distro type, groupId, and artifactId
      */
-    private static final String KARAF_DISTRO_VERSION = "3.0.2";
     private static final String KARAF_DISTRO_TYPE = "zip";
     private static final String KARAF_DISTRO_ARTIFACTID = "apache-karaf";
     private static final String KARAF_DISTRO_GROUPID = "org.apache.karaf";
@@ -72,13 +84,18 @@ public class SingleFeatureTest {
     private static final String KARAF_DISTRO_ARTIFACTID_PROP = "karaf.distro.artifactId";
     private static final String KARAF_DISTRO_GROUPID_PROP = "karaf.distro.groupId";
 
+    /**
+     * Property file used to store the Karaf distribution version.
+     */
+    private static final String PROPERTIES_FILENAME = "singlefeaturetest.properties";
 
     /**
-     * <p>List of karaf 3.0.2 default maven repositories with snapshot repositories excluded.</p>
+     * <p>List of Karaf 3.0.4 default maven repositories with snapshot repositories excluded.</p>
      * <p>Unfortunately this must be hard-coded since declarative model which uses Options,
      * does not allow us to read value, parse it (properties has allways
      * problems with lists) and construct replacement string which does
      * not contains snapshots.</p>
+     * <p>When updating Karaf, check this against org.ops4j.pax.url.mvn.cfg in the Karaf distribution.</p>
      */
     private static final String EXTERNAL_DEFAULT_REPOSITORIES = "http://repo1.maven.org/maven2@id=central, "
             + "http://repository.springsource.com/maven/bundles/release@id=spring.ebr.release, "
@@ -89,6 +106,9 @@ public class SingleFeatureTest {
     @Inject
     private FeaturesService featuresService;
 
+    private String karafVersion;
+    private String karafDistroVersion;
+
     /**
      * Returns the required configuration.
      *
@@ -98,16 +118,24 @@ public class SingleFeatureTest {
     @Configuration
     public Option[] config() throws IOException {
         return new Option[] {
-                // TODO: Find a way to inherit memory limits from Maven options.
-                new VMOption("-Xmx2g"),
-                new VMOption("-XX:MaxPermSize=512m"),
-                getKarafDistroOption(),
-                keepRuntimeFolder(),
-                configureConsole().ignoreLocalConsole(),
-                logLevel(LogLevel.WARN),
-                mvnLocalRepoOption(),
-                editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG, LOG4J_LOGGER_ORG_OPENDAYLIGHT_YANGTOOLS_FEATURETEST,
-                        LogLevel.INFO.name()),
+            // TODO: Find a way to inherit memory limits from Maven options.
+            new VMOption("-Xmx2g"),
+            new VMOption("-XX:MaxPermSize=512m"),
+            when(Boolean.getBoolean(PROFILE_PROP)).useOptions(
+                new VMOption("-XX:+UnlockCommercialFeatures"),
+                new VMOption("-XX:+FlightRecorder"),
+                new VMOption("-XX:FlightRecorderOptions=defaultrecording=true,dumponexit=true,dumponexitpath="
+                               + getNewJFRFile())
+            ),
+            getKarafDistroOption(),
+            when(Boolean.getBoolean(KEEP_UNPACK_DIRECTORY_PROP)).useOptions(keepRuntimeFolder()),
+            configureConsole().ignoreLocalConsole(),
+            logLevel(LogLevel.WARN),
+            mvnLocalRepoOption(),
+            standardKarafFeatures(),
+            editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG, LOG4J_LOGGER_ORG_OPENDAYLIGHT_YANGTOOLS_FEATURETEST,
+                    LogLevel.INFO.name()),
+            editConfigurationFilePut(ETC_ORG_OPS4J_PAX_LOGGING_CFG, "log4j.rootLogger", "INFO, stdout, osgi:*"),
              /*
               *
               * Disables external snapshot repositories.
@@ -131,16 +159,71 @@ public class SingleFeatureTest {
               *
               *
               */
-                disableExternalSnapshotRepositories(),
-                CoreOptions.systemProperty(ORG_OPENDAYLIGHT_FEATURETEST_URI_PROP).value(
-                        System.getProperty(ORG_OPENDAYLIGHT_FEATURETEST_URI_PROP)),
-                CoreOptions.systemProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATURENAME_PROP).value(
-                        System.getProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATURENAME_PROP)),
-                CoreOptions.systemProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATUREVERSION_PROP).value(
-                        System.getProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATUREVERSION_PROP)),
+            disableExternalSnapshotRepositories(),
+            CoreOptions.systemProperty(ORG_OPENDAYLIGHT_FEATURETEST_URI_PROP).value(
+                    System.getProperty(ORG_OPENDAYLIGHT_FEATURETEST_URI_PROP)),
+            CoreOptions.systemProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATURENAME_PROP).value(
+                    System.getProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATURENAME_PROP)),
+            CoreOptions.systemProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATUREVERSION_PROP).value(
+                    System.getProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATUREVERSION_PROP)),
         };
     }
 
+    private String getNewJFRFile() throws IOException {
+        return File.createTempFile("SingleFeatureTest-Karaf-JavaFlightRecorder", ".jfr").getAbsolutePath();
+    }
+
+    private Option standardKarafFeatures() throws IOException {
+        String url = maven().groupId("org.apache.karaf.features").artifactId("standard").classifier("features").type(
+                "xml").version(getKarafVersion()).getURL();
+        try {
+            Features features = JaxbUtil.unmarshal(new URL(url).openStream(), false);
+            List<String> featureNames = new ArrayList<>();
+            for (Feature f : features.getFeature()) {
+                featureNames.add(f.getName());
+            }
+
+            return features(url, featureNames.toArray(new String[featureNames.size()]));
+        } catch (IOException e) {
+            throw new IOException("Could not obtain features from URL: " + url, e);
+        }
+    }
+
+    private String getKarafVersion() {
+        if (karafVersion == null) {
+            // We use a properties file to retrieve ${karaf.version}, instead of .versionAsInProject()
+            // This avoids forcing all users to depend on Karaf in their POMs
+            Properties singleFeatureTestProps = new Properties();
+            try (InputStream singleFeatureTestInputStream = Thread.currentThread().getContextClassLoader()
+                    .getResourceAsStream(PROPERTIES_FILENAME)) {
+                singleFeatureTestProps.load(singleFeatureTestInputStream);
+            } catch (IOException e) {
+                LOG.error("Unable to load {} to determine the Karaf version", PROPERTIES_FILENAME, e);
+            }
+            karafVersion = singleFeatureTestProps.getProperty(KARAF_DISTRO_VERSION_PROP);
+
+            LOG.info("Retrieved karafVersion {} from properties file {}", karafVersion, PROPERTIES_FILENAME);
+        } else {
+            LOG.info("Retrieved karafVersion {} from system property {}", karafVersion, KARAF_DISTRO_VERSION_PROP);
+        }
+
+        return karafVersion;
+    }
+
+    private String getKarafDistroVersion() {
+        if (karafDistroVersion == null) {
+            karafDistroVersion = System.getProperty(KARAF_DISTRO_VERSION_PROP);
+            if (karafDistroVersion == null) {
+                karafDistroVersion = getKarafVersion();
+            } else {
+                LOG.info("Retrieved karafDistroVersion {} from system property {}", karafVersion,
+                        KARAF_DISTRO_VERSION_PROP);
+            }
+        }
+
+        return karafDistroVersion;
+    }
+
     /**
      * Disables snapshot repositories, which are enabled by default in karaf distribution.
      *
@@ -161,16 +244,15 @@ public class SingleFeatureTest {
     protected Option getKarafDistroOption() {
         String groupId = System.getProperty(KARAF_DISTRO_GROUPID_PROP, KARAF_DISTRO_GROUPID);
         String artifactId = System.getProperty(KARAF_DISTRO_ARTIFACTID_PROP, KARAF_DISTRO_ARTIFACTID);
-        String version = System.getProperty(KARAF_DISTRO_VERSION_PROP, KARAF_DISTRO_VERSION);
         String type = System.getProperty(KARAF_DISTRO_TYPE_PROP, KARAF_DISTRO_TYPE);
-        LOG.info("Using karaf distro {} {} {} {}", groupId, artifactId, version, type);
+        LOG.info("Using karaf distro {} {} {} {}", groupId, artifactId, getKarafDistroVersion(), type);
         return karafDistributionConfiguration()
                 .frameworkUrl(
                         maven()
                                 .groupId(groupId)
                                 .artifactId(artifactId)
                                 .type(type)
-                                .version(version))
+                                .version(getKarafDistroVersion()))
                 .name("OpenDaylight")
                 .unpackDirectory(new File("target/pax"))
                 .useDeployFolder(false);
@@ -219,8 +301,8 @@ public class SingleFeatureTest {
         LOG.info("Successfully loaded repository {}", repoUri);
     }
 
-    // Give it 5 minutes max as we've seen feature install hang on jenkins.
-    @Test(timeout = 300000)
+    // Give it 10 minutes max as we've seen feature install hang on jenkins.
+    @Test(timeout = 600000)
     public void installFeature() throws Exception {
         LOG.info("Attempting to install feature {} {}", getFeatureName(), getFeatureVersion());
         featuresService.installFeature(getFeatureName(), getFeatureVersion());