HBASE-26718 HFileArchiver can remove referenced StoreFiles from the archive (#4274)
[hbase.git] / hbase-shell / src / test / ruby / test_helper.rb
blobdb014f5027874e4971b25416d219844bfd09bdce
1 # Licensed to the Apache Software Foundation (ASF) under one
2 # or more contributor license agreements.  See the NOTICE file
3 # distributed with this work for additional information
4 # regarding copyright ownership.  The ASF licenses this file
5 # to you under the Apache License, Version 2.0 (the
6 # "License"); you may not use this file except in compliance
7 # with the License.  You may obtain a copy of the License at
9 #     http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 require 'test/unit'
18 module Testing
19   module Declarative
20     # define_test "should do something" do
21     #   ...
22     # end
23     def define_test(name, &block)
24       test_name = "test_#{name.gsub(/\s+/,'_')}".to_sym
25       defined = instance_method(test_name) rescue false
26       raise "#{test_name} is already defined in #{self}" if defined
27       if block_given?
28         define_method(test_name, &block)
29       else
30         define_method(test_name) do
31           flunk "No implementation provided for #{name}"
32         end
33       end
34     end
35   end
36 end
38 module Hbase
39   module TestHelpers
40     require 'hbase_constants'
41     require 'hbase/hbase'
42     require 'hbase_shell'
44     def setup_hbase
45       hbase = ::Hbase::Hbase.new($TEST_CLUSTER.getConfiguration)
46       @shell = ::Shell::Shell.new(hbase, interactive = false)
47     end
48     
49     def shutdown
50       @shell.hbase.shutdown
51     end
53     # This function triggers exactly same path as the users.
54     def command(command, *args)
55       @shell.command(command, *args)
56     end
58     def table(table)
59       @shell.hbase_table(table)
60     end
62     def admin
63       @shell.admin
64     end
66     def taskmonitor
67       @shell.hbase_taskmonitor
68     end
70     def security_admin
71       @shell.hbase_security_admin
72     end
74     def visibility_admin
75       @shell.hbase_visibility_labels_admin
76     end
78     def quotas_admin
79       @shell.hbase_quotas_admin
80     end
82     def replication_admin
83       @shell.hbase_replication_admin
84     end
86     def group_admin(_formatter)
87       @shell.hbase_group_admin
88     end
90     def create_test_table(name)
91       # Create the table if needed
92       unless admin.exists?(name)
93         command(:create, name, {'NAME' => 'x', 'VERSIONS' => 5}, 'y')
94         return
95       end
97       # Enable the table if needed
98       unless admin.enabled?(name)
99         admin.enable(name)
100       end
101     end
103     def create_test_table_with_splits(name, splits)
104       # Create the table if needed
105       command(:create, name, 'f1', splits) unless admin.exists?(name)
107       # Enable the table if needed
108       admin.enable(name) unless admin.enabled?(name)
109     end
111     def create_test_table_with_splits_file(name, splits_file)
112       # Create the table if needed
113       command(:create, name, 'f1', splits_file) unless admin.exists?(name)
115       # Enable the table if needed
116       admin.enable(name) unless admin.enabled?(name)
117     end
119     def create_test_table_with_region_replicas(name, num_of_replicas, splits)
120       # Create the table if needed
121       unless admin.exists?(name)
122         command(:create, name, 'f1', { ::HBaseConstants::REGION_REPLICATION => num_of_replicas },
123                 splits)
124       end
126       # Enable the table if needed
127       admin.enable(name) unless admin.enabled?(name)
128     end
130     def drop_test_table(name)
131       return unless admin.exists?(name)
132       begin
133         admin.disable(name) if admin.enabled?(name)
134       rescue => e
135         puts "IGNORING DISABLE TABLE ERROR: #{e}"
136       end
137       begin
138         admin.drop(name)
139       rescue => e
140         puts "IGNORING DROP TABLE ERROR: #{e}"
141       end
142     end
144     def replication_status(format,type)
145       return admin.status(format,type)
146     end
148     def drop_test_snapshot()
149       begin
150         admin.delete_all_snapshot(".*")
151       rescue => e
152         puts "IGNORING DELETE ALL SNAPSHOT ERROR: #{e}"
153       end
154     end
157     def capture_stdout
158       begin
159         old_stdout = $stdout
160         $stdout = StringIO.new('','w')
161         yield
162         $stdout.string
163       ensure
164         $stdout = old_stdout
165       end
166     end
167   end
170 # Extend standard unit tests with our helpers
171 Test::Unit::TestCase.extend(Testing::Declarative)