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.client;
019
020import java.io.IOException;
021import java.util.ArrayList;
022import java.util.List;
023import org.apache.hadoop.conf.Configuration;
024import org.apache.hadoop.fs.FileSystem;
025import org.apache.hadoop.hbase.HBaseTestingUtil;
026import org.apache.hadoop.hbase.HConstants;
027import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
028import org.apache.hadoop.hbase.security.access.SecureTestUtil;
029import org.apache.hadoop.hbase.security.visibility.VisibilityTestUtil;
030import org.apache.hadoop.hdfs.DistributedFileSystem;
031import org.jruby.embed.PathType;
032import org.jruby.embed.ScriptingContainer;
033import org.junit.AfterClass;
034import org.junit.BeforeClass;
035import org.junit.Test;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039public abstract class AbstractTestShell {
040
041  private static final Logger LOG = LoggerFactory.getLogger(AbstractTestShell.class);
042
043  protected final static HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
044  protected final static ScriptingContainer jruby = new ScriptingContainer();
045
046  protected static void setUpConfig() throws IOException {
047    Configuration conf = TEST_UTIL.getConfiguration();
048    conf.setInt("hbase.regionserver.msginterval", 100);
049    conf.setInt("hbase.client.pause", 250);
050    conf.setBoolean("hbase.quota.enabled", true);
051    conf.setInt(HConstants.HBASE_CLIENT_RETRIES_NUMBER, 6);
052    conf.setBoolean(CoprocessorHost.ABORT_ON_ERROR_KEY, false);
053    conf.setInt("hfile.format.version", 3);
054
055    // Below settings are necessary for task monitor test.
056    conf.setInt(HConstants.MASTER_INFO_PORT, 0);
057    conf.setInt(HConstants.REGIONSERVER_INFO_PORT, 0);
058    conf.setBoolean(HConstants.REGIONSERVER_INFO_PORT_AUTO, true);
059    // Security setup configuration
060    SecureTestUtil.enableSecurity(conf);
061    VisibilityTestUtil.enableVisiblityLabels(conf);
062  }
063
064  protected static void setUpJRubyRuntime() {
065    LOG.debug("Configure jruby runtime, cluster set to {}", TEST_UTIL);
066    List<String> loadPaths = new ArrayList<>(2);
067    loadPaths.add("src/test/ruby");
068    jruby.setLoadPaths(loadPaths);
069    jruby.put("$TEST_CLUSTER", TEST_UTIL);
070    System.setProperty("jruby.jit.logging.verbose", "true");
071    System.setProperty("jruby.jit.logging", "true");
072    System.setProperty("jruby.native.verbose", "true");
073  }
074
075  /** Returns comma separated list of ruby script names for tests */
076  protected String getIncludeList() {
077    return "";
078  }
079
080  /** Returns comma separated list of ruby script names for tests to skip */
081  protected String getExcludeList() {
082    return "";
083  }
084
085  @Test
086  public void testRunShellTests() throws IOException {
087    final String tests = getIncludeList();
088    final String excludes = getExcludeList();
089    if (!tests.isEmpty()) {
090      System.setProperty("shell.test.include", tests);
091    }
092    if (!excludes.isEmpty()) {
093      System.setProperty("shell.test.exclude", excludes);
094    }
095    LOG.info("Starting ruby tests. includes: {} excludes: {}", tests, excludes);
096    jruby.runScriptlet(PathType.ABSOLUTE, "src/test/ruby/tests_runner.rb");
097  }
098
099  @BeforeClass
100  public static void setUpBeforeClass() throws Exception {
101    setUpConfig();
102
103    // Start mini cluster
104    // 3 datanodes needed for erasure coding checks
105    TEST_UTIL.startMiniCluster(3);
106    DistributedFileSystem dfs =
107      (DistributedFileSystem) FileSystem.get(TEST_UTIL.getConfiguration());
108    dfs.enableErasureCodingPolicy("XOR-2-1-1024k");
109
110    setUpJRubyRuntime();
111  }
112
113  @AfterClass
114  public static void tearDownAfterClass() throws Exception {
115    TEST_UTIL.shutdownMiniCluster();
116  }
117}