You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ma...@apache.org on 2019/05/23 06:07:45 UTC

[lucene-solr] branch jira/SOLR-13452_gradle updated: SOLR-13452: Add some basic testing to buildTest to ensure apache rat works.

This is an automated email from the ASF dual-hosted git repository.

markrmiller pushed a commit to branch jira/SOLR-13452_gradle
in repository https://gitbox.apache.org/repos/asf/lucene-solr.git


The following commit(s) were added to refs/heads/jira/SOLR-13452_gradle by this push:
     new 817c6b3  SOLR-13452: Add some basic testing to buildTest to ensure apache rat works.
817c6b3 is described below

commit 817c6b320fd328851b79509095321603f70be6ae
Author: markrmiller <ma...@apache.org>
AuthorDate: Thu May 23 01:07:34 2019 -0500

    SOLR-13452: Add some basic testing to buildTest to ensure apache rat works.
---
 .../java/{TestBuild.java => BaseTestClass.java}    |  31 +++--
 buildSrc/src/buildTest/java/TestBuild.java         | 140 +--------------------
 buildSrc/src/buildTest/java/TestRatSources.java    |  53 ++++++++
 buildSrc/test-build-wdocker/Dockerfile             |   2 +-
 buildSrc/test-build-wdocker/test-build.sh          |  15 ++-
 .../{test-build.sh => test-rat-sources.sh}         |  33 +++--
 6 files changed, 100 insertions(+), 174 deletions(-)

diff --git a/buildSrc/src/buildTest/java/TestBuild.java b/buildSrc/src/buildTest/java/BaseTestClass.java
similarity index 87%
copy from buildSrc/src/buildTest/java/TestBuild.java
copy to buildSrc/src/buildTest/java/BaseTestClass.java
index e6e75ed..2cdd74b 100644
--- a/buildSrc/src/buildTest/java/TestBuild.java
+++ b/buildSrc/src/buildTest/java/BaseTestClass.java
@@ -32,14 +32,14 @@ import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
 
-public class TestBuild extends Assert {
+public abstract class BaseTestClass extends Assert {
   
-  private static Map<String,String> env = new HashMap<>(System.getenv());
+  protected static Map<String,String> env = new HashMap<>(System.getenv());
   static {
-    env.put("DOCKER_NODE_NAME", "lucenesolr-build-test");
+    env.put("CONTAINER_NAME", "lucenesolr-build-test");
   }
   
-  public TestBuild() {
+  public BaseTestClass() {
 
   }
   
@@ -49,13 +49,19 @@ public class TestBuild extends Assert {
     int exitVal = runCmd(cmd, env, false, false).returnCode;
     System.out.println("Exit:" + exitVal);
     assumeTrue(exitVal == 0);
-
     
     cmd = new String[]{"bash", "test-build-wdocker/start.sh"};
     exitVal = runCmd(cmd, env, false, false).returnCode;
     if (exitVal > 0) {
       fail("Failed starting docker containers!");
     }
+    
+    // hack to allows us to run gradle in the host and then in docker on the same project files
+    cmd = new String[]{"bash",  "-c", "docker exec --user ${UID} -t ${CONTAINER_NAME} bash -c \"rm -rf /home/lucene/project/.gradle\""};
+    exitVal = runCmd(cmd, env, false, false).returnCode;
+    if (exitVal > 0) {
+      //fail("Failed removing .gradle dir!");
+    }
   }
   
   @AfterClass
@@ -78,14 +84,6 @@ public class TestBuild extends Assert {
 
   }
   
-  @Test
-  public void testBuild() throws Exception {
-    System.out.println("Start test-build.sh test in Docker container (" + env + ") ...");
-    String[] cmd = new String[]{"bash", "test-build-wdocker/test-build.sh"};
-    PbResult result = runCmd(cmd, env, false, false, false);
-    assertEquals("Testing test-build.sh failed", 0, result.returnCode);
-  }
-  
   public static PbResult runCmd(String[] cmd, boolean cmdIsNoop, boolean trace)
       throws Exception {
     return runCmd(cmd, Collections.emptyMap(), cmdIsNoop, trace);
@@ -139,8 +137,9 @@ public class TestBuild extends Assert {
       throws Exception {
     try {
       pb.environment().putAll(env);
-      pb.environment().put("PATH", System.getenv().get("PATH") + ":/usr/local/bin");
-      //System.out.println(System.getenv());
+      pb.environment().put("PATH", System.getenv().get("PATH") + File.pathSeparator + "/usr/local/bin" + File.pathSeparator + "/usr/bin");
+      System.out.println(System.getenv());
+     // pb.environment().put("UID", System.getenv().get("UID"));
     } catch (Exception e) {
       System.out.println("Error setting env variables: " + env);
       throw e;
@@ -149,7 +148,7 @@ public class TestBuild extends Assert {
     if (cwd == null) {
       pb.directory(cwd);
     }
-    // pb.inheritIO();
+    pb.inheritIO();
     Process p = null;
     try {
       p = pb.start();
diff --git a/buildSrc/src/buildTest/java/TestBuild.java b/buildSrc/src/buildTest/java/TestBuild.java
index e6e75ed..4ed26b7 100644
--- a/buildSrc/src/buildTest/java/TestBuild.java
+++ b/buildSrc/src/buildTest/java/TestBuild.java
@@ -14,60 +14,16 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-import static org.junit.Assume.assumeTrue;
-
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-
 import org.junit.After;
-import org.junit.AfterClass;
-import org.junit.Assert;
 import org.junit.Before;
-import org.junit.BeforeClass;
 import org.junit.Test;
 
-public class TestBuild extends Assert {
-  
-  private static Map<String,String> env = new HashMap<>(System.getenv());
-  static {
-    env.put("DOCKER_NODE_NAME", "lucenesolr-build-test");
-  }
+public class TestBuild extends BaseTestClass {
   
   public TestBuild() {
 
   }
   
-  @BeforeClass
-  public static void setUpBeforeClass() throws Exception {
-    String[] cmd = new String[]{"bash", "-c", "command -v docker >/dev/null 2>&1 || { echo \"docker must be installed to run this test\"; exit 1; }"};
-    int exitVal = runCmd(cmd, env, false, false).returnCode;
-    System.out.println("Exit:" + exitVal);
-    assumeTrue(exitVal == 0);
-
-    
-    cmd = new String[]{"bash", "test-build-wdocker/start.sh"};
-    exitVal = runCmd(cmd, env, false, false).returnCode;
-    if (exitVal > 0) {
-      fail("Failed starting docker containers!");
-    }
-  }
-  
-  @AfterClass
-  public static void tearDownAfterClass() throws Exception { 
-    String[] cmd = new String[] {"bash",
-        "test-build-wdocker/stop.sh"};
-    int exitVal = runCmd(cmd, env, false, false).returnCode;
-    if (exitVal > 0) {
-      fail("Failed stopping docker containers!");
-    }
-  }
-  
   @Before
   public void setUp() throws Exception {
 
@@ -85,99 +41,5 @@ public class TestBuild extends Assert {
     PbResult result = runCmd(cmd, env, false, false, false);
     assertEquals("Testing test-build.sh failed", 0, result.returnCode);
   }
-  
-  public static PbResult runCmd(String[] cmd, boolean cmdIsNoop, boolean trace)
-      throws Exception {
-    return runCmd(cmd, Collections.emptyMap(), cmdIsNoop, trace);
-  }
-  
-  public static PbResult runCmd(String[] cmd, Map<String,String> env,
-      boolean cmdIsNoop, boolean trace) throws Exception {
-    return runCmd(cmd, env, null, cmdIsNoop, trace);
-  }
-  
-  public static PbResult runCmd(String[] cmd, Map<String,String> env, File cwd,
-      boolean cmdIsNoop, boolean trace) throws Exception {
-    if (trace) System.out.println("execute cmd: " + Arrays.asList(cmd));
-    if (cmdIsNoop) return new PbResult();
-    ProcessBuilder pb = new ProcessBuilder(cmd);
-    return processPb(env, pb, false);
-  }
-  
-  public static PbResult runCmd(String cmd, Map<String,String> env,
-      boolean cmdIsNoop, boolean trace) throws Exception {
-    return runCmd(cmd, env, cmdIsNoop, trace);
-  }
-  
-  public static PbResult runCmd(String cmd, Map<String,String> env,
-      boolean cmdIsNoop, boolean trace, boolean returnOutput) throws Exception {
-    if (trace) System.out.println("execute cmd: " + cmd);
-    if (cmdIsNoop) return new PbResult();
-    ProcessBuilder pb = new ProcessBuilder(cmd);
-    return processPb(env, pb, returnOutput);
-  }
-  
-  public static PbResult runCmd(String[] cmd, Map<String,String> env,
-      boolean cmdIsNoop, boolean trace, boolean returnOutput) throws Exception {
-    return runCmd(cmd, env, null, cmdIsNoop, trace, returnOutput);
-  }
-  
-  public static PbResult runCmd(String[] cmd, Map<String,String> env, File cwd,
-      boolean cmdIsNoop, boolean trace, boolean returnOutput) throws Exception {
-    if (trace) System.out.println("execute cmd: " + Arrays.asList(cmd));
-    if (cmdIsNoop) return new PbResult();
-    ProcessBuilder pb = new ProcessBuilder(cmd);
-    return processPb(env, pb, cwd, returnOutput);
-  }
-  
-  private static PbResult processPb(Map<String,String> env, ProcessBuilder pb, boolean returnOutput)
-      throws Exception {
-    return processPb(env, pb, null, returnOutput);
-  }
-  
-  private static PbResult processPb(Map<String,String> env, ProcessBuilder pb, File cwd, boolean returnOutput)
-      throws Exception {
-    try {
-      pb.environment().putAll(env);
-      pb.environment().put("PATH", System.getenv().get("PATH") + ":/usr/local/bin");
-      //System.out.println(System.getenv());
-    } catch (Exception e) {
-      System.out.println("Error setting env variables: " + env);
-      throw e;
-    }
-    pb.redirectErrorStream(true);
-    if (cwd == null) {
-      pb.directory(cwd);
-    }
-    // pb.inheritIO();
-    Process p = null;
-    try {
-      p = pb.start();
-    } catch (IOException e) {
-      throw new RuntimeException(e);
-    }
-
-    BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
-    StringBuilder sb = new StringBuilder();
-    String line = null;
-    while ((line = br.readLine()) != null) {
-      System.out.println(line);
-      if (returnOutput) {
-        sb.append(line);
-      }
-    }
-    
-    // System.out.println(builder.toString());
-    int returnCode = p.waitFor();
-    PbResult result = new PbResult();
-    result.returnCode = returnCode;
-    result.output = sb.toString();
-    return result;
-  }
-  
-  public static class PbResult {
-    int returnCode;
-    String output;
-  }
 
 }
diff --git a/buildSrc/src/buildTest/java/TestRatSources.java b/buildSrc/src/buildTest/java/TestRatSources.java
new file mode 100644
index 0000000..993a3dc
--- /dev/null
+++ b/buildSrc/src/buildTest/java/TestRatSources.java
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class TestRatSources extends BaseTestClass {
+  
+  public TestRatSources() {
+
+  }
+  
+  @Before
+  public void setUp() throws Exception {
+    cleanTestFiles();
+  }
+  
+  @After
+  public void tearDown() throws Exception {
+    cleanTestFiles();
+  }
+
+  private void cleanTestFiles() throws Exception {
+    String[] cmd = new String[]{"bash", "-c", "docker exec --user ${UID} -t ${CONTAINER_NAME} bash -c \"rm /home/lucene/project/solr/core/src/test/org/no_license_test_file.java\""};
+    runCmd(cmd, env, false, false);
+    
+    cmd = new String[]{"bash", "-c", "docker exec --user ${UID} -t ${CONTAINER_NAME} bash -c \"rm /home/lucene/project/lucene/core/src/java/org/no_license_test_file.xml\""};
+    runCmd(cmd, env, false, false);
+  }
+  
+  @Test
+  public void testRatSources() throws Exception {
+    System.out.println("Start test-rat-sources.sh test in Docker container (" + env + ") ...");
+    String[] cmd = new String[]{"bash", "test-build-wdocker/test-rat-sources.sh"};
+    PbResult result = runCmd(cmd, env, false, false, false);
+    assertEquals("Testing test-build.sh failed", 0, result.returnCode);
+  }
+
+}
diff --git a/buildSrc/test-build-wdocker/Dockerfile b/buildSrc/test-build-wdocker/Dockerfile
index ee93150..f6cd9cf 100644
--- a/buildSrc/test-build-wdocker/Dockerfile
+++ b/buildSrc/test-build-wdocker/Dockerfile
@@ -32,7 +32,7 @@ RUN export JAVA_HOME
 RUN useradd -ms /bin/bash lucene
 
 # Define working directory.
-WORKDIR /home/lucene
+WORKDIR /home/lucene/project
 
 RUN chown -R lucene:lucene /home/lucene
 
diff --git a/buildSrc/test-build-wdocker/test-build.sh b/buildSrc/test-build-wdocker/test-build.sh
index f3456b9..24da83f 100644
--- a/buildSrc/test-build-wdocker/test-build.sh
+++ b/buildSrc/test-build-wdocker/test-build.sh
@@ -47,12 +47,12 @@ set -x
 
 # NOTE: we don't clean right now, as it would wipe out buildSrc/build on us for the host
 
-# hack to allows us to run gradle in the host and then in docker on the same project files
-cmd="rm -rf /home/lucene/project/.gradle"
+# build without unit tests
+cmd="cd /home/lucene/project;./gradlew build -x test"
 exec "${cmd}" "${exec_args}" || { exit 1; }
 
-# build without unit tests
-cmd="cd /home/lucene/project;rm -rf /home/lucene/project/.gradle;./gradlew build -x test"
+# clean jflex for regenerate
+cmd="cd /home/lucene/project;./gradlew cleanjflex"
 exec "${cmd}" "${exec_args}" || { exit 1; }
 
 # test regenerate task
@@ -64,6 +64,11 @@ cmd="cd /home/lucene/project;./gradlew forbiddenApis"
 exec "${cmd}" "${exec_args}" || { exit 1; }
 
 # test eclipse tasks
-cmd="cd /home/lucene/project;./gradlew eclipse"
+cmd="cd /home/lucene/project;./gradlew cleanEclipse eclipse"
 exec "${cmd}" "${exec_args}" || { exit 1; }
 
+# we should still be able to build now
+cmd="cd /home/lucene/project;./gradlew build -x test"
+exec "${cmd}" "${exec_args}" || { exit 1; }
+
+
diff --git a/buildSrc/test-build-wdocker/test-build.sh b/buildSrc/test-build-wdocker/test-rat-sources.sh
similarity index 69%
copy from buildSrc/test-build-wdocker/test-build.sh
copy to buildSrc/test-build-wdocker/test-rat-sources.sh
index f3456b9..cdb65823 100644
--- a/buildSrc/test-build-wdocker/test-build.sh
+++ b/buildSrc/test-build-wdocker/test-rat-sources.sh
@@ -47,23 +47,30 @@ set -x
 
 # NOTE: we don't clean right now, as it would wipe out buildSrc/build on us for the host
 
-# hack to allows us to run gradle in the host and then in docker on the same project files
-cmd="rm -rf /home/lucene/project/.gradle"
+# first check that rat passes
+cmd="cd /home/lucene/project;./gradlew ratSources"
 exec "${cmd}" "${exec_args}" || { exit 1; }
 
-# build without unit tests
-cmd="cd /home/lucene/project;rm -rf /home/lucene/project/.gradle;./gradlew build -x test"
+# create an xml file with no license in lucene
+cmd="ls /home/lucene/project;touch /home/lucene/project/lucene/core/src/java/org/no_license_test_file.xml"
 exec "${cmd}" "${exec_args}" || { exit 1; }
 
-# test regenerate task
-cmd="cd /home/lucene/project;./gradlew regenerate"
-exec "${cmd}" "${exec_args}" || { exit 1; }
-
-# test forbiddenApis task
-cmd="cd /home/lucene/project;./gradlew forbiddenApis"
-exec "${cmd}" "${exec_args}" || { exit 1; }
+# test that rat fails on our test file
+cmd="cd /home/lucene/project;./gradlew ratSources"
+if [ exec "${cmd}" "${exec_args}" ]; then
+  exit 1 # rat should fail!
+else
+  exit 0	
+fi
 
-# test eclipse tasks
-cmd="cd /home/lucene/project;./gradlew eclipse"
+# clean test file
+cmd="rm /home/lucene/project/lucene/core/src/java/org/no_license_test_file.xml"
 exec "${cmd}" "${exec_args}" || { exit 1; }
 
+# create a java file with no license in solr tests
+cmd="ls /home/lucene/project;touch /home/lucene/project/solr/core/src/test/org/no_license_test_file.java"
+if [ exec "${cmd}" "${exec_args}" ]; then
+  exit 1 # rat should fail!
+else
+  exit 0	
+fi