PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/hbase-shell/src/test/ruby/hbase/admin_test.rb

https://github.com/haoyuan/hbase
Ruby | 355 lines | 248 code | 69 blank | 38 comment | 5 complexity | 5c4470903e0b90172cd98f8448483c6f MD5 | raw file
Possible License(s): Apache-2.0
  1. #
  2. #
  3. # Licensed to the Apache Software Foundation (ASF) under one
  4. # or more contributor license agreements. See the NOTICE file
  5. # distributed with this work for additional information
  6. # regarding copyright ownership. The ASF licenses this file
  7. # to you under the Apache License, Version 2.0 (the
  8. # "License"); you may not use this file except in compliance
  9. # with the License. You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. #
  19. require 'shell'
  20. require 'shell/formatter'
  21. require 'hbase'
  22. require 'hbase/hbase'
  23. require 'hbase/table'
  24. include HBaseConstants
  25. module Hbase
  26. class AdminHelpersTest < Test::Unit::TestCase
  27. include TestHelpers
  28. def setup
  29. setup_hbase
  30. # Create test table if it does not exist
  31. @test_name = "hbase_shell_tests_table"
  32. create_test_table(@test_name)
  33. end
  34. define_test "exists? should return true when a table exists" do
  35. assert(admin.exists?('hbase:meta'))
  36. end
  37. define_test "exists? should return false when a table exists" do
  38. assert(!admin.exists?('NOT.EXISTS'))
  39. end
  40. define_test "enabled? should return true for enabled tables" do
  41. admin.enable(@test_name)
  42. assert(admin.enabled?(@test_name))
  43. end
  44. define_test "enabled? should return false for disabled tables" do
  45. admin.disable(@test_name)
  46. assert(!admin.enabled?(@test_name))
  47. end
  48. end
  49. # Simple administration methods tests
  50. class AdminMethodsTest < Test::Unit::TestCase
  51. include TestHelpers
  52. def setup
  53. setup_hbase
  54. # Create test table if it does not exist
  55. @test_name = "hbase_shell_tests_table"
  56. create_test_table(@test_name)
  57. # Create table test table name
  58. @create_test_name = 'hbase_create_table_test_table'
  59. end
  60. define_test "list should return a list of tables" do
  61. assert(admin.list.member?(@test_name))
  62. end
  63. define_test "list should not return meta tables" do
  64. assert(!admin.list.member?('hbase:meta'))
  65. end
  66. define_test "list_namespace_tables for the system namespace should return a list of tables" do
  67. assert(admin.list_namespace_tables('hbase').count > 0)
  68. end
  69. define_test "list_namespace_tables for the default namespace should return a list of tables" do
  70. assert(admin.list_namespace_tables('default').count > 0)
  71. end
  72. #-------------------------------------------------------------------------------
  73. define_test "flush should work" do
  74. admin.flush('hbase:meta')
  75. end
  76. #-------------------------------------------------------------------------------
  77. define_test "compact should work" do
  78. admin.compact('hbase:meta')
  79. end
  80. #-------------------------------------------------------------------------------
  81. define_test "major_compact should work" do
  82. admin.major_compact('hbase:meta')
  83. end
  84. #-------------------------------------------------------------------------------
  85. define_test "split should work" do
  86. admin.split('hbase:meta', nil)
  87. end
  88. #-------------------------------------------------------------------------------
  89. define_test "drop should fail on non-existent tables" do
  90. assert_raise(ArgumentError) do
  91. admin.drop('NOT.EXISTS')
  92. end
  93. end
  94. define_test "drop should fail on enabled tables" do
  95. assert_raise(ArgumentError) do
  96. admin.drop(@test_name)
  97. end
  98. end
  99. define_test "drop should drop tables" do
  100. admin.disable(@test_name)
  101. admin.drop(@test_name)
  102. assert(!admin.exists?(@test_name))
  103. end
  104. #-------------------------------------------------------------------------------
  105. define_test "zk_dump should work" do
  106. assert_not_nil(admin.zk_dump)
  107. end
  108. #-------------------------------------------------------------------------------
  109. define_test "create should fail with non-string table names" do
  110. assert_raise(ArgumentError) do
  111. admin.create(123, 'xxx')
  112. end
  113. end
  114. define_test "create should fail with non-string/non-hash column args" do
  115. assert_raise(ArgumentError) do
  116. admin.create(@create_test_name, 123)
  117. end
  118. end
  119. define_test "create should fail without columns" do
  120. drop_test_table(@create_test_name)
  121. assert_raise(ArgumentError) do
  122. admin.create(@create_test_name)
  123. end
  124. end
  125. define_test "create should fail without columns when called with options" do
  126. drop_test_table(@create_test_name)
  127. assert_raise(ArgumentError) do
  128. admin.create(@create_test_name, { OWNER => 'a' })
  129. end
  130. end
  131. define_test "create should work with string column args" do
  132. drop_test_table(@create_test_name)
  133. admin.create(@create_test_name, 'a', 'b')
  134. assert_equal(['a:', 'b:'], table(@create_test_name).get_all_columns.sort)
  135. end
  136. define_test "create should work with hash column args" do
  137. drop_test_table(@create_test_name)
  138. admin.create(@create_test_name, { NAME => 'a'}, { NAME => 'b'})
  139. assert_equal(['a:', 'b:'], table(@create_test_name).get_all_columns.sort)
  140. end
  141. define_test "create should be able to set table options" do
  142. drop_test_table(@create_test_name)
  143. admin.create(@create_test_name, 'a', 'b', 'MAX_FILESIZE' => 12345678, OWNER => '987654321')
  144. assert_equal(['a:', 'b:'], table(@create_test_name).get_all_columns.sort)
  145. assert_match(/12345678/, admin.describe(@create_test_name))
  146. assert_match(/987654321/, admin.describe(@create_test_name))
  147. end
  148. define_test "create should ignore table_att" do
  149. drop_test_table(@create_test_name)
  150. admin.create(@create_test_name, 'a', 'b', METHOD => 'table_att', OWNER => '987654321')
  151. assert_equal(['a:', 'b:'], table(@create_test_name).get_all_columns.sort)
  152. assert_match(/987654321/, admin.describe(@create_test_name))
  153. end
  154. define_test "create should work with SPLITALGO" do
  155. drop_test_table(@create_test_name)
  156. admin.create(@create_test_name, 'a', 'b', {NUMREGIONS => 10, SPLITALGO => 'HexStringSplit'})
  157. assert_equal(['a:', 'b:'], table(@create_test_name).get_all_columns.sort)
  158. end
  159. #-------------------------------------------------------------------------------
  160. define_test "describe should fail for non-existent tables" do
  161. assert_raise(NativeException) do
  162. admin.describe('.NOT.EXISTS.')
  163. end
  164. end
  165. define_test "describe should return a description" do
  166. assert_not_nil admin.describe(@test_name)
  167. end
  168. #-------------------------------------------------------------------------------
  169. define_test "truncate should empty a table" do
  170. table(@test_name).put(1, "x:a", 1)
  171. table(@test_name).put(2, "x:a", 2)
  172. assert_equal(2, table(@test_name)._count_internal)
  173. # This is hacky. Need to get the configuration into admin instance
  174. admin.truncate(@test_name, $TEST_CLUSTER.getConfiguration)
  175. assert_equal(0, table(@test_name)._count_internal)
  176. end
  177. define_test "truncate should yield log records" do
  178. logs = []
  179. admin.truncate(@test_name, $TEST_CLUSTER.getConfiguration) do |log|
  180. assert_kind_of(String, log)
  181. logs << log
  182. end
  183. assert(!logs.empty?)
  184. end
  185. end
  186. # Simple administration methods tests
  187. class AdminAlterTableTest < Test::Unit::TestCase
  188. include TestHelpers
  189. def setup
  190. setup_hbase
  191. # Create test table if it does not exist
  192. @test_name = "hbase_shell_tests_table"
  193. drop_test_table(@test_name)
  194. create_test_table(@test_name)
  195. end
  196. #-------------------------------------------------------------------------------
  197. define_test "alter should fail with non-string table names" do
  198. assert_raise(ArgumentError) do
  199. admin.alter(123, true, METHOD => 'delete', NAME => 'y')
  200. end
  201. end
  202. define_test "alter should fail with non-existing tables" do
  203. assert_raise(ArgumentError) do
  204. admin.alter('NOT.EXISTS', true, METHOD => 'delete', NAME => 'y')
  205. end
  206. end
  207. define_test "alter should not fail with enabled tables" do
  208. admin.enable(@test_name)
  209. admin.alter(@test_name, true, METHOD => 'delete', NAME => 'y')
  210. end
  211. define_test "alter should be able to delete column families" do
  212. assert_equal(['x:', 'y:'], table(@test_name).get_all_columns.sort)
  213. admin.alter(@test_name, true, METHOD => 'delete', NAME => 'y')
  214. admin.enable(@test_name)
  215. assert_equal(['x:'], table(@test_name).get_all_columns.sort)
  216. end
  217. define_test "alter should be able to add column families" do
  218. assert_equal(['x:', 'y:'], table(@test_name).get_all_columns.sort)
  219. admin.alter(@test_name, true, NAME => 'z')
  220. admin.enable(@test_name)
  221. assert_equal(['x:', 'y:', 'z:'], table(@test_name).get_all_columns.sort)
  222. end
  223. define_test "alter should be able to add column families (name-only alter spec)" do
  224. assert_equal(['x:', 'y:'], table(@test_name).get_all_columns.sort)
  225. admin.alter(@test_name, true, 'z')
  226. admin.enable(@test_name)
  227. assert_equal(['x:', 'y:', 'z:'], table(@test_name).get_all_columns.sort)
  228. end
  229. define_test "alter should support more than one alteration in one call" do
  230. assert_equal(['x:', 'y:'], table(@test_name).get_all_columns.sort)
  231. admin.alter(@test_name, true, { NAME => 'z' }, { METHOD => 'delete', NAME => 'y' }, 'MAX_FILESIZE' => 12345678)
  232. admin.enable(@test_name)
  233. assert_equal(['x:', 'z:'], table(@test_name).get_all_columns.sort)
  234. assert_match(/12345678/, admin.describe(@test_name))
  235. end
  236. define_test 'alter should support shortcut DELETE alter specs' do
  237. assert_equal(['x:', 'y:'], table(@test_name).get_all_columns.sort)
  238. admin.alter(@test_name, true, 'delete' => 'y')
  239. assert_equal(['x:'], table(@test_name).get_all_columns.sort)
  240. end
  241. define_test "alter should be able to change table options" do
  242. admin.alter(@test_name, true, METHOD => 'table_att', 'MAX_FILESIZE' => 12345678)
  243. assert_match(/12345678/, admin.describe(@test_name))
  244. end
  245. define_test "alter should be able to change table options w/o table_att" do
  246. admin.alter(@test_name, true, 'MAX_FILESIZE' => 12345678)
  247. assert_match(/12345678/, admin.describe(@test_name))
  248. end
  249. define_test "alter should be able to change coprocessor attributes" do
  250. drop_test_table(@test_name)
  251. create_test_table(@test_name)
  252. cp_key = "coprocessor"
  253. class_name = "SimpleRegionObserver"
  254. cp_value = "hdfs:///foo.jar|" + class_name + "|12|arg1=1,arg2=2"
  255. # eval() is used to convert a string to regex
  256. assert_no_match(eval("/" + class_name + "/"), admin.describe(@test_name))
  257. assert_no_match(eval("/" + cp_key + "/"), admin.describe(@test_name))
  258. admin.alter(@test_name, true, 'METHOD' => 'table_att', cp_key => cp_value)
  259. assert_match(eval("/" + class_name + "/"), admin.describe(@test_name))
  260. assert_match(eval("/" + cp_key + "\\$(\\d+)/"), admin.describe(@test_name))
  261. end
  262. define_test "alter should be able to remove a table attribute" do
  263. drop_test_table(@test_name)
  264. create_test_table(@test_name)
  265. key1 = "coprocessor"
  266. key2 = "MAX_FILESIZE"
  267. admin.alter(@test_name, true, 'METHOD' => 'table_att', key1 => "|TestCP||")
  268. admin.alter(@test_name, true, 'METHOD' => 'table_att', key2 => 12345678)
  269. # eval() is used to convert a string to regex
  270. assert_match(eval("/" + key1 + "\\$(\\d+)/"), admin.describe(@test_name))
  271. assert_match(eval("/" + key2 + "/"), admin.describe(@test_name))
  272. # get the cp key
  273. cp_keys = admin.describe(@test_name).scan(/(coprocessor\$\d+)/i)
  274. admin.alter(@test_name, true, 'METHOD' => 'table_att_unset', 'NAME' => cp_keys[0][0])
  275. admin.alter(@test_name, true, 'METHOD' => 'table_att_unset', 'NAME' => key2)
  276. assert_no_match(eval("/" + key1 + "\\$(\\d+)/"), admin.describe(@test_name))
  277. assert_no_match(eval("/" + key2 + "/"), admin.describe(@test_name))
  278. end
  279. define_test "get_table should get a real table" do
  280. drop_test_table(@test_name)
  281. create_test_table(@test_name)
  282. table = table(@test_name)
  283. assert_not_equal(nil, table)
  284. end
  285. end
  286. end