From 823064708f4a7293b9a1e83ea70ae9dfba151cb9 Mon Sep 17 00:00:00 2001 From: Esteban Gutierrez Date: Wed, 5 Oct 2016 15:25:54 -0700 Subject: [PATCH] HBASE-16774 [shell] Add coverage to TestShell when ZooKeeper is not reachable --- .../hadoop/hbase/client/TestShellNoCluster.java | 61 +++++++++++++++ .../test/ruby/hbase/test_connection_no_cluster.rb | 46 +++++++++++ .../src/test/ruby/no_cluster_tests_runner.rb | 89 ++++++++++++++++++++++ 3 files changed, 196 insertions(+) create mode 100644 hbase-shell/src/test/java/org/apache/hadoop/hbase/client/TestShellNoCluster.java create mode 100644 hbase-shell/src/test/ruby/hbase/test_connection_no_cluster.rb create mode 100644 hbase-shell/src/test/ruby/no_cluster_tests_runner.rb diff --git a/hbase-shell/src/test/java/org/apache/hadoop/hbase/client/TestShellNoCluster.java b/hbase-shell/src/test/java/org/apache/hadoop/hbase/client/TestShellNoCluster.java new file mode 100644 index 0000000..97c5a32 --- /dev/null +++ b/hbase-shell/src/test/java/org/apache/hadoop/hbase/client/TestShellNoCluster.java @@ -0,0 +1,61 @@ +/** + * + * 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. + */ +package org.apache.hadoop.hbase.client; + +import org.apache.hadoop.hbase.testclassification.ClientTests; +import org.apache.hadoop.hbase.testclassification.LargeTests; +import org.jruby.embed.PathType; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +@Category({ ClientTests.class, LargeTests.class }) +public class TestShellNoCluster extends AbstractTestShell { + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + // no cluster + List loadPaths = new ArrayList(); + loadPaths.add("src/main/ruby"); + loadPaths.add("src/test/ruby"); + jruby.getProvider().setLoadPaths(loadPaths); + jruby.put("$TEST_CLUSTER", TEST_UTIL); + System.setProperty("jruby.jit.logging.verbose", "true"); + System.setProperty("jruby.jit.logging", "true"); + System.setProperty("jruby.native.verbose", "true"); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + // no cluster + } + + @Test + public void testRunNoClusterShellTests() throws IOException { + // Start ruby tests without cluster + // System.setProperty("shell.test", "test_connection_no_cluster.rb"); + jruby.runScriptlet(PathType.ABSOLUTE, "src/test/ruby/no_cluster_tests_runner.rb"); + } + +} diff --git a/hbase-shell/src/test/ruby/hbase/test_connection_no_cluster.rb b/hbase-shell/src/test/ruby/hbase/test_connection_no_cluster.rb new file mode 100644 index 0000000..d512257 --- /dev/null +++ b/hbase-shell/src/test/ruby/hbase/test_connection_no_cluster.rb @@ -0,0 +1,46 @@ +# +# +# 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. +# + +require 'shell' +require 'stringio' +require 'hbase_constants' +require 'hbase/hbase' +require 'hbase/table' + +include HBaseConstants + +module Hbase + # Simple administration methods tests + class NoClusterConnectionTest < Test::Unit::TestCase + include TestHelpers + + def setup + puts "starting shell" + setup_hbase + end + + def teardown + # nothing to teardown + end + + define_test "zk_dump should return" do + assert_not_nil(admin.zk_dump) + end + end +end diff --git a/hbase-shell/src/test/ruby/no_cluster_tests_runner.rb b/hbase-shell/src/test/ruby/no_cluster_tests_runner.rb new file mode 100644 index 0000000..542428d --- /dev/null +++ b/hbase-shell/src/test/ruby/no_cluster_tests_runner.rb @@ -0,0 +1,89 @@ +# +# +# 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. +# + +require 'rubygems' +require 'rake' +require 'set' + +unless defined?($TEST_CLUSTER) + include Java + + # Set logging level to avoid verboseness + org.apache.log4j.Logger.getRootLogger.setLevel(org.apache.log4j.Level::OFF) + org.apache.log4j.Logger.getLogger("org.apache.zookeeper").setLevel(org.apache.log4j.Level::OFF) + org.apache.log4j.Logger.getLogger("org.apache.hadoop.hdfs").setLevel(org.apache.log4j.Level::OFF) + org.apache.log4j.Logger.getLogger("org.apache.hadoop.hbase").setLevel(org.apache.log4j.Level::OFF) + org.apache.log4j.Logger.getLogger("org.apache.hadoop.ipc.HBaseServer").setLevel(org.apache.log4j.Level::OFF) + + java_import org.apache.hadoop.hbase.HBaseTestingUtility + + $TEST_CLUSTER = HBaseTestingUtility.new + $TEST_CLUSTER.configuration.setInt("hbase.regionserver.msginterval", 100) + $TEST_CLUSTER.configuration.setInt("hbase.client.pause", 250) + $TEST_CLUSTER.configuration.setInt(org.apache.hadoop.hbase.HConstants::HBASE_CLIENT_RETRIES_NUMBER, 6) +end + +require 'test_helper' + +puts "Running tests without a cluster..." + +if java.lang.System.get_property('shell.test.include') + includes = Set.new(java.lang.System.get_property('shell.test.include').split(',')) +end + +if java.lang.System.get_property('shell.test.exclude') + excludes = Set.new(java.lang.System.get_property('shell.test.exclude').split(',')) +end + +files = Dir[ File.dirname(__FILE__) + "/**/*_no_cluster.rb" ] +files.each do |file| + filename = File.basename(file) + if includes != nil && !includes.include?(filename) + puts "Skip #{filename} because of not included" + next + end + if excludes != nil && excludes.include?(filename) + puts "Skip #{filename} because of excluded" + next + end + begin + load(file) + rescue => e + puts "ERROR: #{e}" + raise + end +end + +# If this system property is set, we'll use it to filter the test cases. +runner_args = [] +if java.lang.System.get_property('shell.test') + shell_test_pattern = java.lang.System.get_property('shell.test') + puts "Only running tests that match #{shell_test_pattern}" + runner_args << "--testcase=#{shell_test_pattern}" +end +# first couple of args are to match the defaults, so we can pass options to limit the tests run +if !(Test::Unit::AutoRunner.run(false, nil, runner_args)) + raise "Shell unit tests failed. Check output file for details." +end + +puts "Done with tests! Shutting down the cluster..." +if @own_cluster + $TEST_CLUSTER.shutdownMiniCluster + java.lang.System.exit(0) +end -- 2.10.0