Merge "Bug 2761: Disables external snapshot repositories for feature-test"
[odlparent.git] / features-test / src / main / java / org / opendaylight / odlparent / 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.odlparent.featuretest;
9
10 import static org.opendaylight.odlparent.featuretest.Constants.ORG_OPENDAYLIGHT_FEATURETEST_FEATURENAME_PROP;
11 import static org.opendaylight.odlparent.featuretest.Constants.ORG_OPENDAYLIGHT_FEATURETEST_FEATUREVERSION_PROP;
12 import static org.opendaylight.odlparent.featuretest.Constants.ORG_OPENDAYLIGHT_FEATURETEST_URI_PROP;
13 import static org.ops4j.pax.exam.CoreOptions.maven;
14 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.configureConsole;
15 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.editConfigurationFilePut;
16 //import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.debugConfiguration;
17 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
18 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
19 import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.logLevel;
20
21 import java.io.File;
22 import java.io.IOException;
23 import java.net.URI;
24 import java.net.URISyntaxException;
25 import javax.inject.Inject;
26 import org.apache.karaf.features.Feature;
27 import org.apache.karaf.features.FeaturesService;
28 import org.apache.karaf.features.Repository;
29 import org.junit.Assert;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.ops4j.pax.exam.Configuration;
34 import org.ops4j.pax.exam.CoreOptions;
35 import org.ops4j.pax.exam.Option;
36 import org.ops4j.pax.exam.karaf.options.LogLevelOption.LogLevel;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40
41 @RunWith(PerRepoTestRunner.class)
42 public class SingleFeatureTest {
43     private static final String MAVEN_REPO_LOCAL = "maven.repo.local";
44     private static final String ORG_OPS4J_PAX_URL_MVN_LOCAL_REPOSITORY = "org.ops4j.pax.url.mvn.localRepository";
45     private static final String ORG_OPS4J_PAX_URL_MVN_REPOSITORIES = "org.ops4j.pax.url.mvn.repositories";
46     private static final String ETC_ORG_OPS4J_PAX_URL_MVN_CFG = "etc/org.ops4j.pax.url.mvn.cfg";
47     private static final String LOG4J_LOGGER_ORG_OPENDAYLIGHT_YANGTOOLS_FEATURETEST = "log4j.logger.org.opendaylight.odlparent.featuretest";
48     private static final Logger LOG = LoggerFactory.getLogger(SingleFeatureTest.class);
49
50     /*
51      * File name to add our logging config property too.
52      */
53     private static final String ORG_OPS4J_PAX_LOGGING_CFG = "etc/org.ops4j.pax.logging.cfg";
54
55     /*
56      * Default values for karaf distro version, type, groupId, and artifactId
57      */
58     private static final String KARAF_DISTRO_VERSION = "3.0.2";
59     private static final String KARAF_DISTRO_TYPE = "zip";
60     private static final String KARAF_DISTRO_ARTIFACTID = "apache-karaf";
61     private static final String KARAF_DISTRO_GROUPID = "org.apache.karaf";
62
63     /*
64      * Property names to override defaults for karaf distro artifactId, groupId, version, and type
65      */
66     private static final String KARAF_DISTRO_VERSION_PROP = "karaf.distro.version";
67     private static final String KARAF_DISTRO_TYPE_PROP = "karaf.distro.type";
68     private static final String KARAF_DISTRO_ARTIFACTID_PROP = "karaf.distro.artifactId";
69     private static final String KARAF_DISTRO_GROUPID_PROP = "karaf.distro.groupId";
70
71
72     /**
73      * List of karaf 3.0.2 default maven repositories with snapshot repositories excluded.
74      *
75      * Unfortunately this must be hard-coded since declarative model which uses Options,
76      * does not allow us to read value, parse it (properties has allways
77      * problems with lists) and construct replacement string which does
78      * not contains snapshots.
79      *
80      */
81     private static final String EXTERNAL_DEFAULT_REPOSITORIES = "http://repo1.maven.org/maven2@id=central, " +
82     "http://repository.springsource.com/maven/bundles/release@id=spring.ebr.release, " +
83     "http://repository.springsource.com/maven/bundles/external@id=spring.ebr.external, " +
84     "http://zodiac.springsource.com/maven/bundles/release@id=gemini ";
85
86
87    @Inject
88    private FeaturesService featuresService;
89
90     @Configuration
91     public Option[] config() throws IOException {
92        return new Option[] {
93              getKarafDistroOption(),
94              keepRuntimeFolder(),
95              configureConsole().ignoreLocalConsole(),
96              logLevel(LogLevel.WARN),
97              mvnLocalRepoOption(),
98              editConfigurationFilePut(ORG_OPS4J_PAX_LOGGING_CFG,LOG4J_LOGGER_ORG_OPENDAYLIGHT_YANGTOOLS_FEATURETEST,LogLevel.INFO.name()),
99              /*
100               *
101               * Disables external snapshot repositories.
102               *
103               * Pax URL and Karaf by default always search for new version of snapshots
104               * in all snapshots repository, even if that snapshots does not belong to that
105               * repository and maven is invoked even with -nsu (no snapshot update)
106               * or offline mode.
107               *
108               * This is also true for OpenDaylight snapshot artefacts - pax url tries
109               * to resolve them from third-party repositories, even if they are not present
110               * there - this increases time which takes for features to install.
111               *
112               * For more complex projects this actually means several HTTP GETs for each
113               * snapshot bundle referenced, even if the bundle is already present
114               * in local maven repository.
115               *
116               * In order to speed-up installation and remove unnecessary network traffic,
117               * which fails for obvious reasons, external snapshot repositories are
118               * removed.
119               *
120               *
121               */
122              disableExternalSnapshotRepositories(),
123              CoreOptions.systemProperty(ORG_OPENDAYLIGHT_FEATURETEST_URI_PROP).value(System.getProperty(ORG_OPENDAYLIGHT_FEATURETEST_URI_PROP)),
124              CoreOptions.systemProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATURENAME_PROP).value(System.getProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATURENAME_PROP)),
125              CoreOptions.systemProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATUREVERSION_PROP).value(System.getProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATUREVERSION_PROP)),
126        };
127     }
128
129     /**
130      *
131      * Disables snapshot repositories, which are enabled by default in karaf distribution.
132      *
133      *
134      *
135      * @return Edit Configuration option which removes external snapshot repositories.
136      */
137     private static Option disableExternalSnapshotRepositories() {
138         return editConfigurationFilePut(ETC_ORG_OPS4J_PAX_URL_MVN_CFG, ORG_OPS4J_PAX_URL_MVN_REPOSITORIES,EXTERNAL_DEFAULT_REPOSITORIES);
139     }
140
141     protected Option mvnLocalRepoOption() {
142         String mvnRepoLocal = System.getProperty(MAVEN_REPO_LOCAL, "");
143         LOG.info("mvnLocalRepo \"{}\"",mvnRepoLocal);
144         Option option =
145                 editConfigurationFilePut(ETC_ORG_OPS4J_PAX_URL_MVN_CFG,ORG_OPS4J_PAX_URL_MVN_LOCAL_REPOSITORY,mvnRepoLocal);
146         return option;
147     }
148
149     protected Option getKarafDistroOption() {
150         String groupId = System.getProperty(KARAF_DISTRO_GROUPID_PROP,KARAF_DISTRO_GROUPID);
151         String artifactId = System.getProperty(KARAF_DISTRO_ARTIFACTID_PROP,KARAF_DISTRO_ARTIFACTID);
152         String version = System.getProperty(KARAF_DISTRO_VERSION_PROP,KARAF_DISTRO_VERSION);
153         String type = System.getProperty(KARAF_DISTRO_TYPE_PROP,KARAF_DISTRO_TYPE);
154         LOG.info("Using karaf distro {} {} {} {}",groupId,artifactId,version,type);
155         return karafDistributionConfiguration()
156                 .frameworkUrl(
157                         maven()
158                                 .groupId(groupId)
159                                 .artifactId(artifactId)
160                                 .type(type)
161                                 .version(version))
162                .name("OpenDaylight")
163                .unpackDirectory(new File("target/pax"))
164                .useDeployFolder(false);
165     }
166
167     private URI getRepoURI() throws URISyntaxException {
168         return new URI(getProperty(ORG_OPENDAYLIGHT_FEATURETEST_URI_PROP));
169     }
170
171     private String getFeatureName() {
172         return getProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATURENAME_PROP);
173     }
174
175     public String getFeatureVersion() {
176         return getProperty(ORG_OPENDAYLIGHT_FEATURETEST_FEATUREVERSION_PROP);
177     }
178
179     private String getProperty(final String propName) {
180         String prop = System.getProperty(propName);
181         Assert.assertTrue("Missing property :" +propName, prop!=null);
182         return prop;
183     }
184
185     private void checkRepository(final URI repoURI) {
186         Repository repo = null;
187         for(Repository r: featuresService.listRepositories()) {
188             if(r.getURI().equals(repoURI)){
189                 repo = r;
190                 break;
191             }
192         }
193         Assert.assertNotNull("Repository not found: " + repoURI,repo);
194     }
195
196     @Before
197     public void installRepo() throws Exception {
198         LOG.info("Attempting to add repository {}", getRepoURI());
199         featuresService.addRepository(getRepoURI());
200         checkRepository(getRepoURI());
201         LOG.info("Successfully loaded repository {}", getRepoURI());
202     }
203
204     @Test
205     public void installFeature() throws Exception {
206       LOG.info("Attempting to install feature {} {}", getFeatureName(),getFeatureVersion());
207       featuresService.installFeature(getFeatureName(), getFeatureVersion());
208       Feature f = featuresService.getFeature(getFeatureName(), getFeatureVersion());
209       Assert.assertNotNull("Attempt to get feature "+ getFeatureName() + " " + getFeatureVersion() + "resulted in null" , f);
210       Assert.assertTrue("Failed to install Feature: " + getFeatureName() + " " + getFeatureVersion(),featuresService.isInstalled(f));
211       LOG.info("Successfull installed feature {} {}", getFeatureName(),getFeatureVersion());
212     }
213 }