View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.buildcache.its;
20  
21  import java.io.IOException;
22  import java.nio.file.Files;
23  import java.nio.file.Path;
24  import java.util.Arrays;
25  
26  import com.github.tomakehurst.wiremock.common.ConsoleNotifier;
27  import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
28  import com.github.tomakehurst.wiremock.matching.UrlPathPattern;
29  import org.apache.commons.io.FileUtils;
30  import org.apache.maven.buildcache.its.junit.IntegrationTest;
31  import org.apache.maven.it.VerificationException;
32  import org.apache.maven.it.Verifier;
33  import org.junit.jupiter.api.AfterEach;
34  import org.junit.jupiter.api.BeforeEach;
35  import org.junit.jupiter.api.Test;
36  import org.junit.jupiter.api.extension.RegisterExtension;
37  
38  import static com.github.tomakehurst.wiremock.client.WireMock.exactly;
39  import static com.github.tomakehurst.wiremock.client.WireMock.get;
40  import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
41  import static com.github.tomakehurst.wiremock.client.WireMock.notFound;
42  import static com.github.tomakehurst.wiremock.client.WireMock.ok;
43  import static com.github.tomakehurst.wiremock.client.WireMock.put;
44  import static com.github.tomakehurst.wiremock.client.WireMock.putRequestedFor;
45  import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
46  import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
47  
48  /**
49   * Verifies MBUILDCACHE-25 - build cache calculated and saved exactly once in presence of forked executions
50   */
51  @IntegrationTest("src/test/projects/forked-executions-core-extension-remote")
52  class ForkedExecutionsTest {
53  
54      @RegisterExtension
55      static WireMockExtension wm = WireMockExtension.newInstance()
56              .options(wireMockConfig().dynamicPort().notifier(new ConsoleNotifier(true)))
57              .build();
58  
59      private Path tempDirectory;
60  
61      @BeforeEach
62      void setUp() throws IOException {
63          tempDirectory = Files.createTempDirectory("build-cache-test-");
64      }
65  
66      @AfterEach
67      void tearDown() throws IOException {
68          FileUtils.deleteDirectory(tempDirectory.toFile());
69      }
70  
71      @Test
72      void testForkedExecution(Verifier verifier) throws VerificationException {
73  
74          UrlPathPattern buildInfoPath = urlPathMatching(".*/buildinfo.xml");
75          wm.stubFor(get(buildInfoPath).willReturn(notFound()));
76          wm.stubFor(put(buildInfoPath).willReturn(ok()));
77  
78          UrlPathPattern jarPath = urlPathMatching(".*/forked-executions-core-extension-remote.jar");
79          wm.stubFor(put(jarPath).willReturn(ok()));
80  
81          UrlPathPattern cacheReportPath = urlPathMatching(".*/build-cache-report.xml");
82          wm.stubFor(put(cacheReportPath).willReturn(ok()));
83  
84          verifier.setAutoclean(false);
85  
86          verifier.setLogFileName("../log-1.txt");
87          verifier.setMavenDebug(true);
88          verifier.setCliOptions(Arrays.asList(
89                  "-Dmaven.build.cache.location=" + tempDirectory.toAbsolutePath(),
90                  "-Dmaven.build.cache.remote.url=http:////localhost:"
91                          + wm.getRuntimeInfo().getHttpPort(),
92                  "-Dmaven.build.cache.remote.save.enabled=true"));
93          verifier.executeGoal("verify");
94          verifier.verifyTextInLog("Started forked project");
95          verifier.verifyTextInLog("BUILD SUCCESS");
96  
97          wm.verify(exactly(1), getRequestedFor(buildInfoPath));
98          wm.verify(exactly(1), putRequestedFor(buildInfoPath));
99          wm.verify(exactly(1), putRequestedFor(jarPath));
100         wm.verify(exactly(1), putRequestedFor(cacheReportPath));
101     }
102 }