001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase;
019
020import java.io.IOException;
021import java.util.Set;
022import java.util.regex.Pattern;
023import org.apache.hadoop.hbase.testclassification.IntegrationTests;
024import org.apache.hadoop.hbase.util.AbstractHBaseTool;
025import org.apache.hadoop.util.ToolRunner;
026import org.junit.internal.TextListener;
027import org.junit.runner.JUnitCore;
028import org.junit.runner.Result;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032import org.apache.hbase.thirdparty.org.apache.commons.cli.CommandLine;
033
034/**
035 * This class drives the Integration test suite execution. Executes all tests
036 * having @Category(IntegrationTests.class) annotation against an already deployed distributed
037 * cluster.
038 */
039public class IntegrationTestsDriver extends AbstractHBaseTool {
040  private static final String SHORT_REGEX_ARG = "r";
041  private static final String LONG_REGEX_ARG = "regex";
042  private static final Logger LOG = LoggerFactory.getLogger(IntegrationTestsDriver.class);
043  private IntegrationTestFilter intTestFilter = new IntegrationTestFilter();
044
045  public static void main(String[] args) throws Exception {
046    int ret = ToolRunner.run(new IntegrationTestsDriver(), args);
047    System.exit(ret);
048  }
049
050  private static class IntegrationTestFilter extends ClassTestFinder.TestClassFilter {
051    private Pattern testFilterRe = Pattern.compile(".*\\.IntegrationTest.*");
052
053    public IntegrationTestFilter() {
054      super(IntegrationTests.class);
055    }
056
057    public void setPattern(String pattern) {
058      testFilterRe = Pattern.compile(pattern);
059    }
060
061    @Override
062    public boolean isCandidateClass(Class<?> c) {
063      return testFilterRe.matcher(c.getName()).find() &&
064      // Our pattern will match the below NON-IntegrationTest. Rather than
065      // do exotic regex, just filter it out here
066        !c.getName().contains("IntegrationTestingUtility") && super.isCandidateClass(c);
067    }
068  }
069
070  @Override
071  protected void addOptions() {
072    addOptWithArg(SHORT_REGEX_ARG, LONG_REGEX_ARG,
073      "Java regex to use selecting tests to run: e.g. .*TestBig.*"
074        + " will select all tests that include TestBig in their name.  Default: "
075        + ".*IntegrationTest.*");
076  }
077
078  @Override
079  protected void processOptions(CommandLine cmd) {
080    String testFilterString = cmd.getOptionValue(SHORT_REGEX_ARG, null);
081    if (testFilterString != null) {
082      intTestFilter.setPattern(testFilterString);
083    }
084  }
085
086  /**
087   * Returns test classes annotated with @Category(IntegrationTests.class), according to the filter
088   * specific on the command line (if any).
089   */
090  private Class<?>[] findIntegrationTestClasses()
091    throws ClassNotFoundException, LinkageError, IOException {
092    ClassTestFinder.TestFileNameFilter nameFilter = new ClassTestFinder.TestFileNameFilter();
093    ClassFinder classFinder = new ClassFinder(nameFilter, nameFilter, intTestFilter);
094    Set<Class<?>> classes = classFinder.findClasses(true);
095    return classes.toArray(new Class<?>[classes.size()]);
096  }
097
098  @Override
099  protected int doWork() throws Exception {
100    // this is called from the command line, so we should set to use the distributed cluster
101    IntegrationTestingUtility.setUseDistributedCluster(conf);
102    Class<?>[] classes = findIntegrationTestClasses();
103    LOG.info("Found " + classes.length + " integration tests to run:");
104    for (Class<?> aClass : classes) {
105      LOG.info("  " + aClass);
106    }
107    JUnitCore junit = new JUnitCore();
108    junit.addListener(new TextListener(System.out));
109    Result result = junit.run(classes);
110
111    return result.wasSuccessful() ? 0 : 1;
112  }
113}