Bug 1950: Raise memory and permgen limits for featurs-test
[yangtools.git] / common / features-test / src / main / java / org / opendaylight / yangtools / featuretest / SingleFeatureTest.java
1 /*
2  * Copyright (c) 2014 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 package org.opendaylight.yangtools.featuretest;
9
10 import static org.opendaylight.yangtools.featuretest.Constants.ORG_OPENDAYLIGHT_FEATURETEST_FEATURENAME_PROP;
11 import static org.opendaylight.yangtools.featuretest.Constants.ORG_OPENDAYLIGHT_FEATURETEST_FEATUREVERSION_PROP;
12 import static org.opendaylight.yangtools.featuretest.Constants.ORG_OPENDAYLIGHT_FEATURETEST_URI_PROP;
13 import static org.ops4j.pax.exam.CoreOptions.maven;
14 import static org.ops4j.pax.exam.CoreOptions.vmOptions;
15 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.configureConsole;
16 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;
17 //import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.debugConfiguration;
18 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
19 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
20 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.logLevel;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.net.URI;
25 import java.net.URISyntaxException;
26
27 import javax.inject.Inject;
28
29 import org.apache.karaf.features.Feature;
30 import org.apache.karaf.features.FeaturesService;
31 import org.apache.karaf.features.Repository;
32 import org.junit.Assert;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.ops4j.pax.exam.Configuration;
37 import org.ops4j.pax.exam.CoreOptions;
38 import org.ops4j.pax.exam.Option;
39 import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43
44 @RunWith(PerRepoTestRunner.class)
45 public class SingleFeatureTest {
46     private static final String MAVEN_REPO_LOCAL = "maven.repo.local";
47     private static final String ORG_OPS4J_PAX_URL_MVN_LOCAL_REPOSITORY = "org.ops4j.pax.url.mvn.localRepository";
48     private static final String ETC_ORG_OPS4J_PAX_URL_MVN_CFG = "etc/org.ops4j.pax.url.mvn.cfg";
49     private static final String LOG4J_LOGGER_ORG_OPENDAYLIGHT_YANGTOOLS_FEATURETEST = "log4j.logger.org.opendaylight.yangtools.featuretest";
50     private static final Logger LOG = LoggerFactory.getLogger(SingleFeatureTest.class);
51
52     /*
53      * File name to add our logging config property too.
54      */
55     private static final String ORG_OPS4J_PAX_LOGGING_CFG = "etc/org.ops4j.pax.logging.cfg";
56
57     /*
58      * Default values for karaf distro version, type, groupId, and artifactId
59      */
60     private static final String KARAF_DISTRO_VERSION = "3.0.1";
61     private static final String KARAF_DISTRO_TYPE = "zip";
62     private static final String KARAF_DISTRO_ARTIFACTID = "apache-karaf";
63     private static final String KARAF_DISTRO_GROUPID = "org.apache.karaf";
64
65     /*
66      * Property names to override defaults for karaf distro artifactId, groupId, version, and type
67      */
68     private static final String KARAF_DISTRO_VERSION_PROP = "karaf.distro.version";
69     private static final String KARAF_DISTRO_TYPE_PROP = "karaf.distro.type";
70     private static final String KARAF_DISTRO_ARTIFACTID_PROP = "karaf.distro.artifactId";
71     private static final String KARAF_DISTRO_GROUPID_PROP = "karaf.distro.groupId";
72
73
74    @Inject
75    private FeaturesService featuresService;
76
77     @Configuration
78     public Option[] config() throws IOException {
79        return new Option[] {
80              getKarafDistroOption(),
81              vmOptions("-Xmx2048m","-XX:MaxPermSize=512m"),
82              keepRuntimeFolder(),
83              configureConsole().ignoreLocalConsole(),
84              logLevel(LogLevel.WARN),
85              mvnLocalRepoOption(),
86              editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,LOG4J_LOGGER_ORG_OPENDAYLIGHT_YANGTOOLS_FEATURETEST,LogLevel.INFO.name()),
87              CoreOptions.systemProperty(ORG_OPENDAYLIGHT_FEATURETEST_URI_PROP).value(System.getProperty(ORG_OPENDAYLIGHT_FEATURETEST_URI_PROP)),
88              CoreOptions.systemProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATURENAME_PROP).value(System.getProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATURENAME_PROP)),
89              CoreOptions.systemProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATUREVERSION_PROP).value(System.getProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATUREVERSION_PROP)),
90        };
91     }
92
93     protected Option mvnLocalRepoOption() {
94         String mvnRepoLocal = System.getProperty(MAVEN_REPO_LOCAL, "");
95         LOG.info("mvnLocalRepo \"{}\"",mvnRepoLocal);
96         Option option =
97                 editConfigurationFilePut(ETC_ORG_OPS4J_PAX_URL_MVN_CFG,ORG_OPS4J_PAX_URL_MVN_LOCAL_REPOSITORY,mvnRepoLocal);
98         return option;
99     }
100
101     protected Option getKarafDistroOption() {
102         String groupId = System.getProperty(KARAF_DISTRO_GROUPID_PROP,KARAF_DISTRO_GROUPID);
103         String artifactId = System.getProperty(KARAF_DISTRO_ARTIFACTID_PROP,KARAF_DISTRO_ARTIFACTID);
104         String version = System.getProperty(KARAF_DISTRO_VERSION_PROP,KARAF_DISTRO_VERSION);
105         String type = System.getProperty(KARAF_DISTRO_TYPE_PROP,KARAF_DISTRO_TYPE);
106         LOG.info("Using karaf distro {} {} {} {}",groupId,artifactId,version,type);
107         return karafDistributionConfiguration()
108                 .frameworkUrl(
109                         maven()
110                                 .groupId(groupId)
111                                 .artifactId(artifactId)
112                                 .type(type)
113                                 .version(version))
114                .name("OpenDaylight")
115                .unpackDirectory(new File("target/pax"))
116                .useDeployFolder(false);
117     }
118
119     private URI getRepoURI() throws URISyntaxException {
120         return new URI(getProperty(ORG_OPENDAYLIGHT_FEATURETEST_URI_PROP));
121     }
122
123     private String getFeatureName() {
124         return getProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATURENAME_PROP);
125     }
126
127     public String getFeatureVersion() {
128         return getProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATUREVERSION_PROP);
129     }
130
131     private String getProperty(final String propName) {
132         String prop = System.getProperty(propName);
133         Assert.assertTrue("Missing property :" +propName, prop!=null);
134         return prop;
135     }
136
137     private void checkRepository(final URI repoURI) {
138         Repository repo = null;
139         for(Repository r: featuresService.listRepositories()) {
140             if(r.getURI().equals(repoURI)){
141                 repo = r;
142                 break;
143             }
144         }
145         Assert.assertNotNull("Repository not found: " + repoURI,repo);
146     }
147
148     @Before
149     public void installRepo() throws Exception {
150         LOG.info("Attempting to add repository {}", getRepoURI());
151         featuresService.addRepository(getRepoURI());
152         checkRepository(getRepoURI());
153         LOG.info("Successfully loaded repository {}", getRepoURI());
154     }
155
156     @Test
157     public void installFeature() throws Exception {
158       LOG.info("Attempting to install feature {} {}", getFeatureName(),getFeatureVersion());
159       featuresService.installFeature(getFeatureName(), getFeatureVersion());
160       Feature f = featuresService.getFeature(getFeatureName(), getFeatureVersion());
161       Assert.assertNotNull("Attempt to get feature "+ getFeatureName() + " " + getFeatureVersion() + "resulted in null" , f);
162       Assert.assertTrue("Failed to install Feature: " + getFeatureName() + " " + getFeatureVersion(),featuresService.isInstalled(f));
163       LOG.info("Successfull installed feature {} {}", getFeatureName(),getFeatureVersion());
164     }
165 }