2 * Copyright (c) 2024 PANTHEON.tech s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.odlparent.features.test.plugin;
10 import static org.opendaylight.odlparent.features.test.plugin.PaxOptionUtils.concat;
11 import static org.opendaylight.odlparent.features.test.plugin.PaxOptionUtils.dependencyFeaturesOptions;
12 import static org.opendaylight.odlparent.features.test.plugin.PaxOptionUtils.karafConfigOptions;
13 import static org.opendaylight.odlparent.features.test.plugin.PaxOptionUtils.karafDistroOptions;
14 import static org.opendaylight.odlparent.features.test.plugin.PaxOptionUtils.miscOptions;
15 import static org.opendaylight.odlparent.features.test.plugin.PaxOptionUtils.probePropertiesOptions;
16 import static org.opendaylight.odlparent.features.test.plugin.PaxOptionUtils.profileOptions;
17 import static org.opendaylight.odlparent.features.test.plugin.PaxOptionUtils.vmOptions;
20 import java.io.IOException;
21 import java.util.List;
23 import java.util.concurrent.locks.Lock;
24 import java.util.concurrent.locks.ReentrantLock;
25 import org.apache.maven.execution.MavenSession;
26 import org.apache.maven.plugin.AbstractMojo;
27 import org.apache.maven.plugin.MojoExecutionException;
28 import org.apache.maven.plugin.descriptor.PluginDescriptor;
29 import org.apache.maven.plugins.annotations.Component;
30 import org.apache.maven.plugins.annotations.LifecyclePhase;
31 import org.apache.maven.plugins.annotations.Mojo;
32 import org.apache.maven.plugins.annotations.Parameter;
33 import org.apache.maven.plugins.annotations.ResolutionScope;
34 import org.apache.maven.project.MavenProject;
35 import org.eclipse.aether.RepositorySystem;
36 import org.eclipse.aether.RepositorySystemSession;
37 import org.eclipse.aether.repository.RemoteRepository;
38 import org.ops4j.pax.exam.ExamSystem;
39 import org.ops4j.pax.exam.karaf.container.internal.KarafTestContainerFactory;
40 import org.ops4j.pax.exam.spi.PaxExamRuntime;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * Feature test plugin mojo.
47 @Mojo(name = "test", defaultPhase = LifecyclePhase.INTEGRATION_TEST,
48 requiresDependencyResolution = ResolutionScope.TEST,
49 requiresDependencyCollection = ResolutionScope.TEST, threadSafe = true)
50 public final class TestFeaturesMojo extends AbstractMojo {
51 private static final Logger LOG = LoggerFactory.getLogger(TestFeaturesMojo.class);
52 private static final String[] FEATURE_FILENAMES = {"feature.xml", "features.xml"};
53 private static final PluginDescriptor STATIC_DESCRIPTOR;
56 final var desc = new PluginDescriptor();
57 desc.setGroupId("org.opendaylight.odlparent");
58 desc.setArtifactId("features-test-plugin");
59 STATIC_DESCRIPTOR = desc;
62 @Parameter(defaultValue = "${project}", readonly = true, required = true)
63 private MavenProject project;
64 @Parameter(defaultValue = "${session}", readonly = true, required = true)
65 private MavenSession session;
66 @Parameter(defaultValue = "${settings.localRepository}")
67 private String localRepository;
68 @Parameter(defaultValue = "${repositorySystemSession}", readonly = true, required = true)
69 private RepositorySystemSession repoSession;
70 @Parameter(defaultValue = "${project.remoteProjectRepositories}", readonly = true, required = true)
71 private List<RemoteRepository> repositories;
73 private RepositorySystem repoSystem;
75 @Parameter(property = "sft.skip", defaultValue = "false")
77 @Deprecated(since = "13.0.10", forRemoval = true)
78 @Parameter(property = "skip.karaf.featureTest", defaultValue = "false")
79 private boolean legacySkip;
80 @Parameter(property = "sft.concurrent", defaultValue = "false")
81 private boolean concurrent;
83 // bundle state check probe settings
84 @Parameter(property = "sft.diag.skip", defaultValue = "false")
85 private boolean bundleStateCheckSkip;
86 @Parameter(property = "sft.diag.timeout", defaultValue = "120")
87 private int bundleStateCheckTimeout;
89 // vm and profile options for karaf container
90 @Parameter(property = "sft.heap.max", defaultValue = "2g")
91 private String maxHeap;
92 @Parameter(property = "sft.heap.dump.path", defaultValue = "/dev/null")
93 private String heapDumpPath;
94 @Parameter(property = "karaf.featureTest.profile", defaultValue = "false")
95 private boolean profile;
97 // Backing distribution details
98 @Parameter(property = "karaf.distro.groupId", defaultValue = "org.opendaylight.odlparent")
99 private String distGroupId;
100 @Parameter(property = "karaf.distro.artifactId", defaultValue = "opendaylight-karaf-empty")
101 private String distArtifactId;
102 @Parameter(property = "karaf.distro.type", defaultValue = "tar.gz")
103 private String distType;
104 @Parameter(property = "karaf.keep.unpack", defaultValue = "false")
105 private boolean keepUnpack;
108 public void execute() throws MojoExecutionException {
110 LOG.debug("Skipping execution");
114 LOG.warn("Skipping execution due to legacy karaf.featureTest.skip, please migrate to sft.skip");
117 if (!"feature".equals(project.getPackaging())) {
118 LOG.info("Project packaging is not 'feature', skipping execution");
122 LOG.info("Starting SFT for {}:{}", project.getGroupId(), project.getArtifactId());
124 final var buildDir = project.getBuild().getDirectory();
125 final var featureFile = getFeatureFile(new File(buildDir + File.separator + "feature"));
127 // resolve dependencies (ensure all are in local repository)
128 final var resolver = new DependencyResolver(repoSystem, repoSession, repositories);
129 resolver.resolveFeatureFile(featureFile);
131 // using file:* url instead of mvn:* to avoid MalformedUrlException (unknown protocol 'mvn');
132 // no reason to involve external maven resolver (pax-exam-aether-url) to fetch distro artifact via maven,
133 // while we can use local repository file directly (URL has built-in handler for 'file' protocol)
134 final var karafDistroUrl = resolver.resolveKarafDistroUrl(distGroupId, distArtifactId, distType);
135 LOG.debug("Distro URL resolved: {}", karafDistroUrl);
137 // dependency features (incl test scope) to be pre-installed
138 final var pluginDependencyFeatures = resolver.resolvePluginFeatures();
139 final var projectDependencyFeatures = resolver.resolveFeatures(project.getArtifacts());
140 LOG.info("Project dependency features detected: {}", projectDependencyFeatures);
143 final var options = concat(
144 vmOptions(maxHeap, heapDumpPath),
145 profileOptions(profile),
146 karafDistroOptions(karafDistroUrl, keepUnpack, buildDir),
147 karafConfigOptions(buildDir, localRepository),
148 dependencyFeaturesOptions(pluginDependencyFeatures),
149 dependencyFeaturesOptions(projectDependencyFeatures),
150 probePropertiesOptions(),
155 System.setProperty(TestProbe.FEATURE_FILE_URI_PROP, featureFile.toURI().toString());
156 System.setProperty(TestProbe.BUNDLE_CHECK_SKIP, String.valueOf(bundleStateCheckSkip));
157 System.setProperty(TestProbe.BUNDLE_CHECK_TIMEOUT_SECONDS, String.valueOf(bundleStateCheckTimeout));
159 final ExamSystem system;
161 system = PaxExamRuntime.createTestSystem(options);
162 } catch (IOException e) {
163 throw new MojoExecutionException("Cannot create pax-exam system", e);
166 final var execution = new PaxExamExecution(localRepository, system,
167 new KarafTestContainerFactory().create(system));
173 // We create a plugin context in the top-level project of the build. There we store a single object which acts
174 // as the global lock protecting execution.
175 final Map<String, Object> topContext;
176 synchronized (session) {
177 // This is as careful as we can be. We guard against concurrent executions on the same top-level project.
178 topContext = session.getPluginContext(STATIC_DESCRIPTOR, session.getTopLevelProject());
181 // Note: we are using a fair lock to get first-come, first-serve rather than some unpredictable order
182 final var lock = (Lock) topContext.computeIfAbsent("lock", key -> new ReentrantLock(true));
183 LOG.debug("Using lock {}", lock);
185 lock.lockInterruptibly();
186 } catch (InterruptedException e) {
187 Thread.currentThread().interrupt();
188 throw new MojoExecutionException("Interrupted while acquiring lock", e);
191 LOG.debug("Acquired lock {}", lock);
196 LOG.debug("Released lock {}", lock);
200 static File getFeatureFile(final File dir) throws MojoExecutionException {
201 for (var filename : FEATURE_FILENAMES) {
202 final File file = new File(dir, filename);
207 throw new MojoExecutionException("No feature XML file found in " + dir);