PageRenderTime 52ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/apache/hbase
Ruby | 1157 lines | 851 code | 197 blank | 109 comment | 36 complexity | 61ae812bba5b81cb87c42a1004b0d8e1 MD5 | raw file
Possible License(s): Apache-2.0, MIT
  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 'hbase_shell'
  20. require 'stringio'
  21. require 'hbase_constants'
  22. require 'hbase/hbase'
  23. require 'hbase/table'
  24. module Hbase
  25. class AdminHelpersTest < Test::Unit::TestCase
  26. include TestHelpers
  27. def setup
  28. setup_hbase
  29. # Create test table if it does not exist
  30. @test_name = "hbase_shell_admin_test_table"
  31. create_test_table(@test_name)
  32. end
  33. def teardown
  34. shutdown
  35. end
  36. define_test "exists? should return true when a table exists" do
  37. assert(command(:exists, 'hbase:meta'))
  38. end
  39. define_test "exists? should return false when a table exists" do
  40. assert(!command(:exists, 'NOT.EXISTS'))
  41. end
  42. define_test "enabled? should return true for enabled tables" do
  43. command(:enable, @test_name)
  44. assert(command(:is_enabled, @test_name))
  45. end
  46. define_test "enabled? should return false for disabled tables" do
  47. command(:disable, @test_name)
  48. assert(!command(:is_enabled, @test_name))
  49. end
  50. end
  51. # Simple administration methods tests
  52. # rubocop:disable Metrics/ClassLength
  53. class AdminMethodsTest < Test::Unit::TestCase
  54. include TestHelpers
  55. include HBaseConstants
  56. include HBaseQuotasConstants
  57. def setup
  58. setup_hbase
  59. # Create test table if it does not exist
  60. @test_name = "hbase_shell_tests_table"
  61. create_test_table(@test_name)
  62. # Create table test table name
  63. @create_test_name = 'hbase_create_table_test_table'
  64. end
  65. def teardown
  66. shutdown
  67. end
  68. define_test "list should return a list of tables" do
  69. list = command(:list)
  70. assert(list.member?(@test_name))
  71. end
  72. define_test "list should not return meta tables" do
  73. list = command(:list)
  74. assert(!list.member?('hbase:meta'))
  75. end
  76. define_test "list_namespace_tables for the system namespace should return a list of tables" do
  77. list = command(:list_namespace_tables, 'hbase')
  78. assert(list.count > 0)
  79. end
  80. define_test "list_namespace_tables for the default namespace should return a list of tables" do
  81. list = command(:list_namespace_tables, 'default')
  82. assert(list.count > 0)
  83. end
  84. define_test 'list_deadservers should return exact count of dead servers' do
  85. output = capture_stdout { command(:list_deadservers) }
  86. assert(output.include?('0 row(s)'))
  87. end
  88. define_test 'clear_deadservers should show exact row(s) count' do
  89. deadservers = []
  90. output = capture_stdout { deadservers = command(:clear_deadservers, 'test.server.com,16020,1574583397867') }
  91. assert(output.include?('1 row(s)'))
  92. assert(deadservers[0] == 'test.server.com,16020,1574583397867')
  93. end
  94. #-------------------------------------------------------------------------------
  95. define_test "flush should work" do
  96. command(:flush, 'hbase:meta')
  97. servers = admin.list_liveservers
  98. servers.each do |s|
  99. command(:flush, s.toString)
  100. end
  101. end
  102. #-------------------------------------------------------------------------------
  103. define_test 'compact all regions by server name' do
  104. servers = admin.list_liveservers
  105. servers.each do |s|
  106. command(:compact_rs, s.to_s)
  107. # major compact
  108. command(:compact_rs, s.to_s, true)
  109. break
  110. end
  111. end
  112. #-------------------------------------------------------------------------------
  113. define_test 'alter_status should work' do
  114. output = capture_stdout { command(:alter_status, @test_name) }
  115. assert(output.include?('1/1 regions updated'))
  116. end
  117. #-------------------------------------------------------------------------------
  118. define_test "compact should work" do
  119. command(:compact, 'hbase:meta')
  120. end
  121. #-------------------------------------------------------------------------------
  122. define_test "compaction_state should work" do
  123. command(:compaction_state, 'hbase:meta')
  124. end
  125. #-------------------------------------------------------------------------------
  126. define_test "major_compact should work" do
  127. command(:major_compact, 'hbase:meta')
  128. end
  129. #-------------------------------------------------------------------------------
  130. define_test "split should work" do
  131. begin
  132. command(:split, 'hbase:meta', nil)
  133. rescue org.apache.hadoop.hbase.ipc.RemoteWithExtrasException => e
  134. puts "can not split hbase:meta"
  135. end
  136. end
  137. #-------------------------------------------------------------------------------
  138. define_test "drop should fail on non-existent tables" do
  139. assert_raise(ArgumentError) do
  140. command(:drop, 'NOT.EXISTS')
  141. end
  142. end
  143. define_test "drop should fail on enabled tables" do
  144. assert_raise(ArgumentError) do
  145. command(:drop, @test_name)
  146. end
  147. end
  148. define_test "drop should drop tables" do
  149. command(:disable, @test_name)
  150. command(:drop, @test_name)
  151. assert(!command(:exists, @test_name))
  152. end
  153. #-------------------------------------------------------------------------------
  154. define_test "zk_dump should work" do
  155. assert_not_nil(admin.zk_dump)
  156. end
  157. #-------------------------------------------------------------------------------
  158. define_test "balance should work" do
  159. command(:balance_switch, true)
  160. output = capture_stdout { command(:balancer_enabled) }
  161. assert(output.include?('true'))
  162. did_balancer_run = command(:balancer)
  163. assert(did_balancer_run == true)
  164. output = capture_stdout { command(:balancer, 'force') }
  165. assert(output.include?('Balancer ran'))
  166. command(:balance_switch, false)
  167. output = capture_stdout { command(:balancer) }
  168. assert(output.include?('Balancer did not run'))
  169. output = capture_stdout { command(:balancer, 'dry_run') }
  170. assert(output.include?('Balancer ran'))
  171. end
  172. #-------------------------------------------------------------------------------
  173. define_test "create should fail with non-string table names" do
  174. assert_raise(ArgumentError) do
  175. command(:create, 123, 'xxx')
  176. end
  177. end
  178. #-------------------------------------------------------------------------------
  179. define_test 'snapshot auto cleanup should work' do
  180. result = nil
  181. command(:snapshot_cleanup_switch, false)
  182. # enable snapshot cleanup and check that the previous state is returned
  183. output = capture_stdout { result = command(:snapshot_cleanup_switch, true) }
  184. assert(output.include?('false'))
  185. assert(result == false)
  186. # check that snapshot_cleanup_enabled returns the current state
  187. output = capture_stdout { result = command(:snapshot_cleanup_enabled) }
  188. assert(output.include?('true'))
  189. assert(result == true)
  190. # disable snapshot cleanup and check that the previous state is returned
  191. output = capture_stdout { result = command(:snapshot_cleanup_switch, false) }
  192. assert(output.include?('true'))
  193. assert(result == true)
  194. # check that snapshot_cleanup_enabled returns the current state
  195. output = capture_stdout { result = command(:snapshot_cleanup_enabled) }
  196. assert(output.include?('false'))
  197. assert(result == false)
  198. end
  199. #-------------------------------------------------------------------------------
  200. define_test 'balancer switch should work' do
  201. result = nil
  202. command(:balance_switch, false)
  203. # enable balancer and check that the previous state is returned
  204. output = capture_stdout { result = command(:balance_switch, true) }
  205. assert(output.include?('false'))
  206. assert(result == false)
  207. # check that balancer_enabled returns the current state
  208. output = capture_stdout { result = command(:balancer_enabled) }
  209. assert(output.include?('true'))
  210. assert(result == true)
  211. # disable balancer and check that the previous state is returned
  212. output = capture_stdout { result = command(:balance_switch, false) }
  213. assert(output.include?('true'))
  214. assert(result == true)
  215. # check that balancer_enabled returns the current state
  216. output = capture_stdout { result = command(:balancer_enabled) }
  217. assert(output.include?('false'))
  218. assert(result == false)
  219. end
  220. #-------------------------------------------------------------------------------
  221. define_test 'normalizer switch should work' do
  222. result = nil
  223. command(:normalizer_switch, false)
  224. # enable normalizer and check that the previous state is returned
  225. output = capture_stdout { result = command(:normalizer_switch, true) }
  226. assert(output.include?('false'))
  227. assert(result == false)
  228. # check that normalizer_enabled returns the current state
  229. output = capture_stdout { result = command(:normalizer_enabled) }
  230. assert(output.include?('true'))
  231. assert(result == true)
  232. # disable normalizer and check that the previous state is returned
  233. output = capture_stdout { result = command(:normalizer_switch, false) }
  234. assert(output.include?('true'))
  235. assert(result == true)
  236. # check that normalizer_enabled returns the current state
  237. output = capture_stdout { result = command(:normalizer_enabled) }
  238. assert(output.include?('false'))
  239. assert(result == false)
  240. end
  241. #-------------------------------------------------------------------------------
  242. define_test 'catalogjanitor switch should work' do
  243. result = nil
  244. command(:catalogjanitor_switch, false)
  245. # enable catalogjanitor and check that the previous state is returned
  246. output = capture_stdout { result = command(:catalogjanitor_switch, true) }
  247. assert(output.include?('false'))
  248. assert(result == false)
  249. # check that catalogjanitor_enabled returns the current state
  250. output = capture_stdout { result = command(:catalogjanitor_enabled) }
  251. assert(output.include?('true'))
  252. assert(result == true)
  253. # disable catalogjanitor and check that the previous state is returned
  254. output = capture_stdout { result = command(:catalogjanitor_switch, false) }
  255. assert(output.include?('true'))
  256. assert(result == true)
  257. # check that catalogjanitor_enabled returns the current state
  258. output = capture_stdout { result = command(:catalogjanitor_enabled) }
  259. assert(output.include?('false'))
  260. assert(result == false)
  261. end
  262. #-------------------------------------------------------------------------------
  263. define_test 'cleaner_chore switch should work' do
  264. result = nil
  265. command(:cleaner_chore_switch, false)
  266. # enable cleaner_chore and check that the previous state is returned
  267. output = capture_stdout { result = command(:cleaner_chore_switch, true) }
  268. assert(output.include?('false'))
  269. assert(result == false)
  270. # check that cleaner_chore_enabled returns the current state
  271. output = capture_stdout { result = command(:cleaner_chore_enabled) }
  272. assert(output.include?('true'))
  273. assert(result == true)
  274. # disable cleaner_chore and check that the previous state is returned
  275. output = capture_stdout { result = command(:cleaner_chore_switch, false) }
  276. assert(output.include?('true'))
  277. assert(result == true)
  278. # check that cleaner_chore_enabled returns the current state
  279. output = capture_stdout { result = command(:cleaner_chore_enabled) }
  280. assert(output.include?('false'))
  281. assert(result == false)
  282. end
  283. #-------------------------------------------------------------------------------
  284. define_test 'splitormerge switch should work' do
  285. # Author's note: All the other feature switches in hbase-shell only toggle one feature. This command operates on
  286. # both the "SPLIT" and "MERGE", so you will note that both code paths need coverage.
  287. result = nil
  288. command(:splitormerge_switch, 'SPLIT', false)
  289. command(:splitormerge_switch, 'MERGE', true)
  290. # flip switch and check that the previous state is returned
  291. output = capture_stdout { result = command(:splitormerge_switch, 'SPLIT', true) }
  292. assert(output.include?('false'))
  293. assert(result == false)
  294. output = capture_stdout { result = command(:splitormerge_switch, 'MERGE', false) }
  295. assert(output.include?('true'))
  296. assert(result == true)
  297. # check that splitormerge_enabled returns the current state
  298. output = capture_stdout { result = command(:splitormerge_enabled, 'SPLIT') }
  299. assert(output.include?('true'))
  300. assert(result == true)
  301. output = capture_stdout { result = command(:splitormerge_enabled, 'MERGE') }
  302. assert(output.include?('false'))
  303. assert(result == false)
  304. # flip switch and check that the previous state is returned
  305. output = capture_stdout { result = command(:splitormerge_switch, 'SPLIT', false) }
  306. assert(output.include?('true'))
  307. assert(result == true)
  308. output = capture_stdout { result = command(:splitormerge_switch, 'MERGE', true) }
  309. assert(output.include?('false'))
  310. assert(result == false)
  311. # check that splitormerge_enabled returns the current state
  312. output = capture_stdout { result = command(:splitormerge_enabled, 'SPLIT') }
  313. assert(output.include?('false'))
  314. assert(result == false)
  315. output = capture_stdout { result = command(:splitormerge_enabled, 'MERGE') }
  316. assert(output.include?('true'))
  317. assert(result == true)
  318. end
  319. #-------------------------------------------------------------------------------
  320. define_test 'get slowlog responses should work' do
  321. output = command(:get_slowlog_responses, '*', {})
  322. assert(output.nil?)
  323. end
  324. #-------------------------------------------------------------------------------
  325. define_test 'clear slowlog responses should work' do
  326. output = capture_stdout { command(:clear_slowlog_responses, nil) }
  327. assert(output.include?('Cleared Slowlog responses from 0/1 RegionServers'))
  328. end
  329. #-------------------------------------------------------------------------------
  330. define_test "create should fail with non-string/non-hash column args" do
  331. assert_raise(ArgumentError) do
  332. command(:create, @create_test_name, 123)
  333. end
  334. end
  335. define_test "create should fail without columns" do
  336. drop_test_table(@create_test_name)
  337. assert_raise(ArgumentError) do
  338. command(:create, @create_test_name)
  339. end
  340. end
  341. define_test "create should fail without columns when called with options" do
  342. drop_test_table(@create_test_name)
  343. assert_raise(ArgumentError) do
  344. command(:create, @create_test_name, { VERSIONS => '1' })
  345. end
  346. end
  347. define_test "create should work with string column args" do
  348. drop_test_table(@create_test_name)
  349. command(:create, @create_test_name, 'a', 'b')
  350. assert_equal(['a:', 'b:'], table(@create_test_name).get_all_columns.sort)
  351. end
  352. define_test "create should work with hash column args" do
  353. drop_test_table(@create_test_name)
  354. command(:create, @create_test_name, { NAME => 'a'}, { NAME => 'b'})
  355. assert_equal(['a:', 'b:'], table(@create_test_name).get_all_columns.sort)
  356. end
  357. define_test "create should be able to set column options" do
  358. drop_test_table(@create_test_name)
  359. command(:create, @create_test_name,
  360. { NAME => 'a',
  361. CACHE_BLOOMS_ON_WRITE => 'TRUE',
  362. CACHE_INDEX_ON_WRITE => 'TRUE',
  363. EVICT_BLOCKS_ON_CLOSE => 'TRUE',
  364. COMPRESSION_COMPACT => 'GZ'})
  365. assert_equal(['a:'], table(@create_test_name).get_all_columns.sort)
  366. assert_match(/CACHE_BLOOMS_ON_WRITE/, admin.describe(@create_test_name))
  367. assert_match(/CACHE_INDEX_ON_WRITE/, admin.describe(@create_test_name))
  368. assert_match(/EVICT_BLOCKS_ON_CLOSE/, admin.describe(@create_test_name))
  369. assert_match(/GZ/, admin.describe(@create_test_name))
  370. end
  371. define_test "create should be able to set table options" do
  372. drop_test_table(@create_test_name)
  373. command(:create, @create_test_name, 'a', 'b', 'MAX_FILESIZE' => 12345678,
  374. PRIORITY => '77',
  375. FLUSH_POLICY => 'org.apache.hadoop.hbase.regionserver.FlushAllLargeStoresPolicy',
  376. REGION_MEMSTORE_REPLICATION => 'TRUE',
  377. SPLIT_POLICY => 'org.apache.hadoop.hbase.regionserver.ConstantSizeRegionSplitPolicy',
  378. COMPACTION_ENABLED => 'false',
  379. SPLIT_ENABLED => 'false',
  380. MERGE_ENABLED => 'false')
  381. assert_equal(['a:', 'b:'], table(@create_test_name).get_all_columns.sort)
  382. assert_match(/12345678/, admin.describe(@create_test_name))
  383. assert_match(/77/, admin.describe(@create_test_name))
  384. assert_match(/'COMPACTION_ENABLED' => 'false'/, admin.describe(@create_test_name))
  385. assert_match(/'SPLIT_ENABLED' => 'false'/, admin.describe(@create_test_name))
  386. assert_match(/'MERGE_ENABLED' => 'false'/, admin.describe(@create_test_name))
  387. assert_match(/'REGION_MEMSTORE_REPLICATION' => 'true'/, admin.describe(@create_test_name))
  388. assert_match(/org.apache.hadoop.hbase.regionserver.FlushAllLargeStoresPolicy/,
  389. admin.describe(@create_test_name))
  390. assert_match(/org.apache.hadoop.hbase.regionserver.ConstantSizeRegionSplitPolicy/,
  391. admin.describe(@create_test_name))
  392. end
  393. define_test "create should ignore table_att" do
  394. drop_test_table(@create_test_name)
  395. command(:create, @create_test_name, 'a', 'b', METHOD => 'table_att')
  396. assert_equal(['a:', 'b:'], table(@create_test_name).get_all_columns.sort)
  397. end
  398. define_test "create should work with SPLITALGO" do
  399. drop_test_table(@create_test_name)
  400. command(:create, @create_test_name, 'a', 'b',
  401. {NUMREGIONS => 10, SPLITALGO => 'HexStringSplit'})
  402. assert_equal(['a:', 'b:'], table(@create_test_name).get_all_columns.sort)
  403. end
  404. define_test "create should work when attributes value 'false' is not enclosed in single quotation marks" do
  405. drop_test_table(@create_test_name)
  406. command(:create, @create_test_name, {NAME => 'a', BLOCKCACHE => false},
  407. COMPACTION_ENABLED => false,
  408. SPLIT_ENABLED => false,
  409. MERGE_ENABLED => false)
  410. assert_equal(['a:'], table(@create_test_name).get_all_columns.sort)
  411. assert_match(/BLOCKCACHE => 'false'/, admin.describe(@create_test_name))
  412. assert_match(/'COMPACTION_ENABLED' => 'false'/, admin.describe(@create_test_name))
  413. assert_match(/'SPLIT_ENABLED' => 'false'/, admin.describe(@create_test_name))
  414. assert_match(/'MERGE_ENABLED' => 'false'/, admin.describe(@create_test_name))
  415. end
  416. #-------------------------------------------------------------------------------
  417. define_test "describe should fail for non-existent tables" do
  418. assert_raise(ArgumentError) do
  419. admin.describe('NOT.EXISTS')
  420. end
  421. end
  422. define_test 'describe should return a description and quotas' do
  423. drop_test_table(@create_test_name)
  424. command(:create, @create_test_name, 'cf1', 'cf2')
  425. command(:set_quota,
  426. TYPE => SPACE,
  427. LIMIT => '1G',
  428. POLICY => NO_INSERTS,
  429. TABLE => @create_test_name)
  430. output = capture_stdout { command(:describe, @create_test_name) }
  431. assert(output.include?("Table #{@create_test_name} is ENABLED"))
  432. assert(output.include?('COLUMN FAMILIES DESCRIPTION'))
  433. assert(output.include?("NAME => 'cf1'"))
  434. assert(output.include?("NAME => 'cf2'"))
  435. assert(output.include?('2 row(s)'))
  436. assert(output.include?('QUOTAS'))
  437. assert(output.include?('LIMIT => 1.00G'))
  438. assert(output.include?('VIOLATION_POLICY => NO_INSERTS'))
  439. assert(output.include?('TYPE => SPACE'))
  440. assert(output.include?('1 row(s)'))
  441. command(:set_quota,
  442. TYPE => SPACE,
  443. LIMIT => NONE,
  444. TABLE => @create_test_name)
  445. output = capture_stdout { command(:describe, @create_test_name) }
  446. assert(output.include?('0 row(s)'))
  447. end
  448. define_test 'describe_namespace should return a description and quotas' do
  449. ns = @create_test_name
  450. command(:create_namespace, ns)
  451. command(:set_quota,
  452. TYPE => ::HBaseQuotasConstants::SPACE,
  453. LIMIT => '1G',
  454. POLICY => ::HBaseQuotasConstants::NO_INSERTS,
  455. NAMESPACE => ns)
  456. output = capture_stdout { command(:describe_namespace, ns) }
  457. puts output
  458. assert(output.include?('DESCRIPTION'))
  459. assert(output.include?("NAME => '#{ns}'"))
  460. assert(output.include?('QUOTAS'))
  461. assert(output.include?('LIMIT => 1.00G'))
  462. assert(output.include?('VIOLATION_POLICY => NO_INSERTS'))
  463. assert(output.include?('TYPE => SPACE'))
  464. assert(output.include?('1 row(s)'))
  465. command(:set_quota,
  466. TYPE => ::HBaseQuotasConstants::SPACE,
  467. LIMIT => ::HBaseConstants::NONE,
  468. NAMESPACE => ns)
  469. output = capture_stdout { command(:describe_namespace, ns) }
  470. assert(output.include?('0 row(s)'))
  471. end
  472. define_test 'describe_namespace should return quota disabled' do
  473. ns = 'ns'
  474. quota_table = ::HBaseQuotasConstants::QUOTA_TABLE_NAME.to_s
  475. drop_test_table(quota_table)
  476. command(:create_namespace, ns)
  477. output = capture_stdout { command(:describe_namespace, ns) }
  478. # re-creating quota table otherwise other test case may fail
  479. command(:create, quota_table, 'q', 'u')
  480. assert(output.include?('Quota is disabled'))
  481. end
  482. #-------------------------------------------------------------------------------
  483. define_test 'truncate should empty a table' do
  484. table(@test_name).put(1, 'x:a', 1)
  485. table(@test_name).put(2, 'x:a', 2)
  486. assert_equal(2, table(@test_name)._count_internal)
  487. # This is hacky. Need to get the configuration into admin instance
  488. command(:truncate, @test_name)
  489. assert_equal(0, table(@test_name)._count_internal)
  490. end
  491. define_test 'truncate should yield log records' do
  492. output = capture_stdout { command(:truncate, @test_name) }
  493. assert(!output.empty?)
  494. end
  495. define_test 'truncate should work on disabled table' do
  496. table(@test_name).put(1, 'x:a', 1)
  497. table(@test_name).put(2, 'x:a', 2)
  498. assert_equal(2, table(@test_name)._count_internal)
  499. command(:disable, @test_name)
  500. command(:truncate, @test_name)
  501. assert_equal(0, table(@test_name)._count_internal)
  502. end
  503. #-------------------------------------------------------------------------------
  504. define_test 'truncate_preserve should empty a table' do
  505. table(@test_name).put(1, 'x:a', 1)
  506. table(@test_name).put(2, 'x:a', 2)
  507. assert_equal(2, table(@test_name)._count_internal)
  508. # This is hacky. Need to get the configuration into admin instance
  509. command(:truncate_preserve, @test_name)
  510. assert_equal(0, table(@test_name)._count_internal)
  511. end
  512. define_test 'truncate_preserve should yield log records' do
  513. output = capture_stdout { command(:truncate_preserve, @test_name) }
  514. assert(!output.empty?)
  515. end
  516. define_test 'truncate_preserve should maintain the previous region boundaries' do
  517. drop_test_table(@create_test_name)
  518. admin.create(@create_test_name, 'a', {NUMREGIONS => 10, SPLITALGO => 'HexStringSplit'})
  519. splits = table(@create_test_name)._get_splits_internal()
  520. command(:truncate_preserve, @create_test_name)
  521. assert_equal(splits, table(@create_test_name)._get_splits_internal())
  522. end
  523. #-------------------------------------------------------------------------------
  524. define_test 'enable and disable tables by regex' do
  525. @t1 = 't1'
  526. @t2 = 't11'
  527. @regex = 't1.*'
  528. command(:create, @t1, 'f')
  529. command(:create, @t2, 'f')
  530. admin.disable_all(@regex)
  531. assert(command(:is_disabled, @t1))
  532. assert(command(:is_disabled, @t2))
  533. assert(!command(:is_enabled, @t1))
  534. assert(!command(:is_enabled, @t2))
  535. admin.enable_all(@regex)
  536. assert(!command(:is_disabled, @t1))
  537. assert(!command(:is_disabled, @t2))
  538. assert(command(:is_enabled, @t1))
  539. assert(command(:is_enabled, @t2))
  540. admin.disable_all(@regex)
  541. admin.drop_all(@regex)
  542. assert(!command(:exists, @t1))
  543. assert(!command(:exists, @t2))
  544. end
  545. #-------------------------------------------------------------------------------
  546. define_test "list_regions should fail for disabled table" do
  547. drop_test_table(@create_test_name)
  548. admin.create(@create_test_name, 'a')
  549. command(:disable, @create_test_name)
  550. assert(:is_disabled, @create_test_name)
  551. assert_raise(RuntimeError) do
  552. command(:list_regions, @create_test_name)
  553. end
  554. end
  555. end
  556. # rubocop:enable Metrics/ClassLength
  557. # Simple administration methods tests
  558. class AdminCloneTableSchemaTest < Test::Unit::TestCase
  559. include TestHelpers
  560. include HBaseConstants
  561. def setup
  562. setup_hbase
  563. # Create table test table name
  564. @source_table_name = 'hbase_shell_tests_source_table_name'
  565. @destination_table_name = 'hbase_shell_tests_destination_table_name'
  566. end
  567. def teardown
  568. shutdown
  569. end
  570. define_test "clone_table_schema should create a new table by cloning the
  571. existent table schema." do
  572. drop_test_table(@source_table_name)
  573. drop_test_table(@destination_table_name)
  574. command(:create,
  575. @source_table_name,
  576. NAME => 'a',
  577. CACHE_BLOOMS_ON_WRITE => 'TRUE',
  578. CACHE_INDEX_ON_WRITE => 'TRUE',
  579. EVICT_BLOCKS_ON_CLOSE => 'TRUE',
  580. COMPRESSION_COMPACT => 'GZ')
  581. command(:clone_table_schema,
  582. @source_table_name,
  583. @destination_table_name,
  584. false)
  585. assert_equal(['a:'],
  586. table(@source_table_name).get_all_columns.sort)
  587. assert_match(/CACHE_BLOOMS_ON_WRITE/,
  588. admin.describe(@destination_table_name))
  589. assert_match(/CACHE_INDEX_ON_WRITE/,
  590. admin.describe(@destination_table_name))
  591. assert_match(/EVICT_BLOCKS_ON_CLOSE/,
  592. admin.describe(@destination_table_name))
  593. assert_match(/GZ/,
  594. admin.describe(@destination_table_name))
  595. end
  596. define_test "clone_table_schema should maintain the source table's region
  597. boundaries when preserve_splits set to true" do
  598. drop_test_table(@source_table_name)
  599. drop_test_table(@destination_table_name)
  600. command(:create,
  601. @source_table_name,
  602. 'a',
  603. NUMREGIONS => 10,
  604. SPLITALGO => 'HexStringSplit')
  605. splits = table(@source_table_name)._get_splits_internal
  606. command(:clone_table_schema,
  607. @source_table_name,
  608. @destination_table_name,
  609. true)
  610. assert_equal(splits, table(@destination_table_name)._get_splits_internal)
  611. end
  612. define_test "clone_table_schema should have failed when source table
  613. doesn't exist." do
  614. drop_test_table(@source_table_name)
  615. drop_test_table(@destination_table_name)
  616. assert_raise(RuntimeError) do
  617. command(:clone_table_schema,
  618. @source_table_name,
  619. @destination_table_name)
  620. end
  621. end
  622. define_test "clone_table_schema should have failed when destination
  623. table exists." do
  624. drop_test_table(@source_table_name)
  625. drop_test_table(@destination_table_name)
  626. command(:create, @source_table_name, 'a')
  627. command(:create, @destination_table_name, 'a')
  628. assert_raise(RuntimeError) do
  629. command(:clone_table_schema,
  630. @source_table_name,
  631. @destination_table_name)
  632. end
  633. end
  634. end
  635. # Simple administration methods tests
  636. class AdminRegionTest < Test::Unit::TestCase
  637. include TestHelpers
  638. include HBaseConstants
  639. def setup
  640. setup_hbase
  641. # Create test table if it does not exist
  642. @test_name = "hbase_shell_tests_table"
  643. drop_test_table(@test_name)
  644. create_test_table(@test_name)
  645. end
  646. def teardown
  647. shutdown
  648. end
  649. define_test "unassign should allow encoded region names" do
  650. region = command(:locate_region, @test_name, '')
  651. regionName = region.getRegion.getRegionNameAsString
  652. command(:unassign, regionName, true)
  653. end
  654. define_test "unassign should allow non-encoded region names" do
  655. region = command(:locate_region, @test_name, '')
  656. encodedRegionName = region.getRegion.getEncodedName
  657. command(:unassign, encodedRegionName, true)
  658. end
  659. define_test "list regions should allow table name" do
  660. command(:list_regions, @test_name)
  661. end
  662. define_test 'merge regions' do
  663. @t_name = 'hbase_shell_merge'
  664. @t_name2 = 'hbase_shell_merge_2'
  665. drop_test_table(@t_name)
  666. drop_test_table(@t_name2)
  667. admin.create(@t_name, 'a', NUMREGIONS => 10, SPLITALGO => 'HexStringSplit')
  668. r1 = command(:locate_region, @t_name, '1')
  669. r2 = command(:locate_region, @t_name, '2')
  670. r3 = command(:locate_region, @t_name, '4')
  671. r4 = command(:locate_region, @t_name, '5')
  672. r5 = command(:locate_region, @t_name, '7')
  673. r6 = command(:locate_region, @t_name, '8')
  674. region1 = r1.getRegion.getRegionNameAsString
  675. region2 = r2.getRegion.getRegionNameAsString
  676. region3 = r3.getRegion.getRegionNameAsString
  677. region4 = r4.getRegion.getRegionNameAsString
  678. region5 = r5.getRegion.getRegionNameAsString
  679. region6 = r6.getRegion.getRegionNameAsString
  680. # only 1 region
  681. assert_raise(ArgumentError) do
  682. command(:merge_region, 'a')
  683. end
  684. # only 1 region with force=true
  685. assert_raise(ArgumentError) do
  686. command(:merge_region, 'a', true)
  687. end
  688. # non-existing region
  689. assert_raise(RuntimeError) do
  690. command(:merge_region, 'a','b')
  691. end
  692. # duplicate regions
  693. assert_raise(RuntimeError) do
  694. command(:merge_region, region1,region1,region1)
  695. end
  696. # 3 non-adjacent regions without forcible=true
  697. assert_raise(RuntimeError) do
  698. command(:merge_region, region1,region2,region4)
  699. end
  700. # 2 adjacent regions
  701. command(:merge_region, region1,region2)
  702. # 3 non-adjacent regions with forcible=true
  703. command(:merge_region, region3,region5,region6, true)
  704. admin.create(@t_name2, 'a', NUMREGIONS => 5, SPLITALGO => 'HexStringSplit')
  705. r1 = command(:locate_region, @t_name2, '1')
  706. r2 = command(:locate_region, @t_name2, '4')
  707. r3 = command(:locate_region, @t_name2, '7')
  708. region1 = r1.getRegion.getRegionNameAsString
  709. region2 = r2.getRegion.getRegionNameAsString
  710. region3 = r3.getRegion.getRegionNameAsString
  711. # accept array of regions
  712. command(:merge_region, [region1,region2,region3])
  713. end
  714. end
  715. # Simple administration methods tests
  716. # rubocop:disable Metrics/ClassLength
  717. class AdminAlterTableTest < Test::Unit::TestCase
  718. include TestHelpers
  719. include HBaseConstants
  720. def setup
  721. setup_hbase
  722. # Create test table if it does not exist
  723. @test_name = "hbase_shell_tests_table"
  724. drop_test_table(@test_name)
  725. create_test_table(@test_name)
  726. end
  727. def teardown
  728. shutdown
  729. end
  730. #-------------------------------------------------------------------------------
  731. define_test "alter should fail with non-string table names" do
  732. assert_raise(ArgumentError) do
  733. command(:alter, 123, METHOD => 'delete', NAME => 'y')
  734. end
  735. end
  736. define_test "alter should fail with non-existing tables" do
  737. assert_raise(ArgumentError) do
  738. command(:alter, 'NOT.EXISTS', METHOD => 'delete', NAME => 'y')
  739. end
  740. end
  741. define_test "alter should not fail with enabled tables" do
  742. command(:enable, @test_name)
  743. command(:alter, @test_name, METHOD => 'delete', NAME => 'y')
  744. end
  745. define_test "alter should be able to delete column families" do
  746. assert_equal(['x:', 'y:'], table(@test_name).get_all_columns.sort)
  747. command(:alter, @test_name, METHOD => 'delete', NAME => 'y')
  748. command(:enable, @test_name)
  749. assert_equal(['x:'], table(@test_name).get_all_columns.sort)
  750. end
  751. define_test "alter should be able to add column families" do
  752. assert_equal(['x:', 'y:'], table(@test_name).get_all_columns.sort)
  753. command(:alter, @test_name, NAME => 'z')
  754. command(:enable, @test_name)
  755. assert_equal(['x:', 'y:', 'z:'], table(@test_name).get_all_columns.sort)
  756. end
  757. define_test "alter should be able to add column families (name-only alter spec)" do
  758. assert_equal(['x:', 'y:'], table(@test_name).get_all_columns.sort)
  759. command(:alter, @test_name, 'z')
  760. command(:enable, @test_name)
  761. assert_equal(['x:', 'y:', 'z:'], table(@test_name).get_all_columns.sort)
  762. end
  763. define_test 'alter should support more than one alteration in one call' do
  764. assert_equal(['x:', 'y:'], table(@test_name).get_all_columns.sort)
  765. alter_out_put = capture_stdout do
  766. command(:alter, @test_name, { NAME => 'z' },
  767. { METHOD => 'delete', NAME => 'y' },
  768. 'MAX_FILESIZE' => 12_345_678)
  769. end
  770. command(:enable, @test_name)
  771. assert_equal(1, /Updating all regions/.match(alter_out_put).size,
  772. "HBASE-15641 - Should only perform one table
  773. modification per alter.")
  774. assert_equal(['x:', 'z:'], table(@test_name).get_all_columns.sort)
  775. assert_match(/12345678/, admin.describe(@test_name))
  776. end
  777. define_test 'alter should be able to set the TargetRegionSizeMB and TargetRegionCount' do
  778. command(:alter, @test_name, 'NORMALIZER_TARGET_REGION_COUNT' => 156)
  779. assert_match(/156/, admin.describe(@test_name))
  780. command(:alter, @test_name, 'NORMALIZER_TARGET_REGION_SIZE_MB' => 234)
  781. assert_match(/234/, admin.describe(@test_name))
  782. end
  783. define_test 'alter should be able to set the TargetRegionSize and TargetRegionCount' do
  784. command(:alter, @test_name, 'NORMALIZER_TARGET_REGION_COUNT' => 156)
  785. assert_match(/156/, admin.describe(@test_name))
  786. command(:alter, @test_name, 'NORMALIZER_TARGET_REGION_SIZE' => 234)
  787. assert_match(/234/, admin.describe(@test_name))
  788. end
  789. define_test 'alter should support shortcut DELETE alter specs' do
  790. assert_equal(['x:', 'y:'], table(@test_name).get_all_columns.sort)
  791. command(:alter, @test_name, 'delete' => 'y')
  792. assert_equal(['x:'], table(@test_name).get_all_columns.sort)
  793. end
  794. define_test "alter should be able to change table options" do
  795. command(:alter, @test_name, METHOD => 'table_att', 'MAX_FILESIZE' => 12345678)
  796. assert_match(/12345678/, admin.describe(@test_name))
  797. end
  798. define_test "alter should be able to change table options w/o table_att" do
  799. command(:alter, @test_name, 'MAX_FILESIZE' => 12345678)
  800. assert_match(/12345678/, admin.describe(@test_name))
  801. end
  802. define_test "alter should be able to specify coprocessor attributes with spec string" do
  803. drop_test_table(@test_name)
  804. create_test_table(@test_name)
  805. cp_key = "coprocessor"
  806. class_name = "org.apache.hadoop.hbase.coprocessor.SimpleRegionObserver"
  807. cp_value = "|" + class_name + "|12|arg1=1,arg2=2"
  808. # eval() is used to convert a string to regex
  809. assert_no_match(eval("/" + class_name + "/"), admin.describe(@test_name))
  810. assert_no_match(eval("/" + cp_key + "/"), admin.describe(@test_name))
  811. command(:alter, @test_name, 'METHOD' => 'table_att', cp_key => cp_value)
  812. describe_text = admin.describe(@test_name)
  813. assert_match(eval("/" + class_name + "/"), describe_text)
  814. assert_match(eval("/" + cp_key + "\\$(\\d+)/"), describe_text)
  815. assert_match(/arg1=1,arg2=2/, describe_text)
  816. end
  817. define_test "alter should be able to change coprocessor attributes with hash" do
  818. drop_test_table(@test_name)
  819. create_test_table(@test_name)
  820. cp_key = "coprocessor"
  821. class_name = "org.apache.hadoop.hbase.coprocessor.SimpleRegionObserver"
  822. # eval() is used to convert a string to regex
  823. assert_no_match(eval("/" + class_name + "/"), admin.describe(@test_name))
  824. assert_no_match(eval("/" + cp_key + "/"), admin.describe(@test_name))
  825. command(:alter, @test_name, 'METHOD' => 'table_att', cp_key => {
  826. 'CLASSNAME' => class_name,
  827. 'PRIORITY' => 15,
  828. 'PROPERTIES' => {
  829. 'arg1' => 4,
  830. 'arg2' => 9,
  831. },
  832. })
  833. describe_text = admin.describe(@test_name)
  834. assert_match(eval("/" + class_name + "/"), describe_text)
  835. assert_match(eval("/" + cp_key + "\\$(\\d+)/"), describe_text)
  836. assert_match(/arg1=4,arg2=9/, describe_text)
  837. end
  838. define_test "alter should be able to remove a table attribute" do
  839. drop_test_table(@test_name)
  840. create_test_table(@test_name)
  841. key = "MAX_FILESIZE"
  842. command(:alter, @test_name, 'METHOD' => 'table_att', key => 12345678)
  843. # eval() is used to convert a string to regex
  844. assert_match(eval("/" + key + "/"), admin.describe(@test_name))
  845. command(:alter, @test_name, 'METHOD' => 'table_att_unset', 'NAME' => key)
  846. assert_no_match(eval("/" + key + "/"), admin.describe(@test_name))
  847. end
  848. define_test "alter should be able to remove a list of table attributes" do
  849. drop_test_table(@test_name)
  850. key_1 = "TestAttr1"
  851. key_2 = "TestAttr2"
  852. command(:create, @test_name, { NAME => 'i'}, METADATA => { key_1 => 1, key_2 => 2 })
  853. # eval() is used to convert a string to regex
  854. assert_match(eval("/" + key_1 + "/"), admin.describe(@test_name))
  855. assert_match(eval("/" + key_2 + "/"), admin.describe(@test_name))
  856. command(:alter, @test_name, 'METHOD' => 'table_att_unset', 'NAME' => [ key_1, key_2 ])
  857. assert_no_match(eval("/" + key_1 + "/"), admin.describe(@test_name))
  858. assert_no_match(eval("/" + key_2 + "/"), admin.describe(@test_name))
  859. end
  860. define_test "alter should be able to remove a list of table attributes when value is empty" do
  861. drop_test_table(@test_name)
  862. key_1 = "TestAttr1"
  863. key_2 = "TestAttr2"
  864. command(:create, @test_name, { NAME => 'i'}, METADATA => { key_1 => 1, key_2 => 2 })
  865. # eval() is used to convert a string to regex
  866. assert_match(eval("/" + key_1 + "/"), admin.describe(@test_name))
  867. assert_match(eval("/" + key_2 + "/"), admin.describe(@test_name))
  868. command(:alter, @test_name, METADATA => { key_1 => '', key_2 => '' })
  869. assert_no_match(eval("/" + key_1 + "/"), admin.describe(@test_name))
  870. assert_no_match(eval("/" + key_2 + "/"), admin.describe(@test_name))
  871. end
  872. define_test "alter should raise error trying to remove nonexistent attributes" do
  873. drop_test_table(@test_name)
  874. create_test_table(@test_name)
  875. key_1 = "TestAttr1"
  876. key_2 = "TestAttr2"
  877. assert_no_match(eval("/" + key_1 + "/"), admin.describe(@test_name))
  878. assert_no_match(eval("/" + key_2 + "/"), admin.describe(@test_name))
  879. # first, try removing just one nonexistent attribute
  880. assert_raise(ArgumentError) do
  881. command(:alter, @test_name, 'METHOD' => 'table_att_unset', 'NAME' => key_1)
  882. end
  883. # second, try removing multiple nonexistent attributes
  884. assert_raise(ArgumentError) do
  885. command(:alter, @test_name, 'METHOD' => 'table_att_unset', 'NAME' => [ key_1, key_2 ])
  886. end
  887. end
  888. define_test "alter should be able to remove a table configuration" do
  889. drop_test_table(@test_name)
  890. create_test_table(@test_name)
  891. key = "TestConf"
  892. command(:alter, @test_name, CONFIGURATION => {key => 1})
  893. # eval() is used to convert a string to regex
  894. assert_match(eval("/" + key + "/"), admin.describe(@test_name))
  895. command(:alter, @test_name, 'METHOD' => 'table_conf_unset', 'NAME' => key)
  896. assert_no_match(eval("/" + key + "/"), admin.describe(@test_name))
  897. end
  898. define_test "alter should be able to remove a list of table configuration" do
  899. drop_test_table(@test_name)
  900. key_1 = "TestConf1"
  901. key_2 = "TestConf2"
  902. command(:create, @test_name, { NAME => 'i'}, CONFIGURATION => { key_1 => 1, key_2 => 2 })
  903. # eval() is used to convert a string to regex
  904. assert_match(eval("/" + key_1 + "/"), admin.describe(@test_name))
  905. assert_match(eval("/" + key_2 + "/"), admin.describe(@test_name))
  906. command(:alter, @test_name, 'METHOD' => 'table_conf_unset', 'NAME' => [ key_1, key_2 ])
  907. assert_no_match(eval("/" + key_1 + "/"), admin.describe(@test_name))
  908. assert_no_match(eval("/" + key_2 + "/"), admin.describe(@test_name))
  909. end
  910. define_test "alter should be able to remove a list of table configuration when value is empty" do
  911. drop_test_table(@test_name)
  912. key_1 = "TestConf1"
  913. key_2 = "TestConf2"
  914. command(:create, @test_name, { NAME => 'i'}, CONFIGURATION => { key_1 => 1, key_2 => 2 })
  915. # eval() is used to convert a string to regex
  916. assert_match(eval("/" + key_1 + "/"), admin.describe(@test_name))
  917. assert_match(eval("/" + key_2 + "/"), admin.describe(@test_name))
  918. command(:alter, @test_name, CONFIGURATION => { key_1 => '', key_2 => '' })
  919. assert_no_match(eval("/" + key_1 + "/"), admin.describe(@test_name))
  920. assert_no_match(eval("/" + key_2 + "/"), admin.describe(@test_name))
  921. end
  922. define_test "alter should be able to remove a list of column family configuration when value is empty" do
  923. drop_test_table(@test_name)
  924. key_1 = "TestConf1"
  925. key_2 = "TestConf2"
  926. command(:create, @test_name, { NAME => 'i', CONFIGURATION => { key_1 => 1, key_2 => 2 }})
  927. # eval() is used to convert a string to regex
  928. assert_match(eval("/" + key_1 + "/"), admin.describe(@test_name))
  929. assert_match(eval("/" + key_2 + "/"), admin.describe(@test_name))
  930. command(:alter, @test_name, { NAME => 'i', CONFIGURATION => { key_1 => '', key_2 => '' }})
  931. assert_no_match(eval("/" + key_1 + "/"), admin.describe(@test_name))
  932. assert_no_match(eval("/" + key_2 + "/"), admin.describe(@test_name))
  933. end
  934. define_test "alter should raise error trying to remove nonexistent configurations" do
  935. drop_test_table(@test_name)
  936. create_test_table(@test_name)
  937. key_1 = "TestConf1"
  938. key_2 = "TestConf2"
  939. assert_no_match(eval("/" + key_1 + "/"), admin.describe(@test_name))
  940. assert_no_match(eval("/" + key_2 + "/"), admin.describe(@test_name))
  941. # first, try removing just one nonexistent configuration
  942. assert_raise(ArgumentError) do
  943. command(:alter, @test_name, 'METHOD' => 'table_conf_unset', 'NAME' => key_1)
  944. end
  945. # second, try removing multiple nonexistent configurations
  946. assert_raise(ArgumentError) do
  947. command(:alter, @test_name, 'METHOD' => 'table_conf_unset', 'NAME' => [ key_1, key_2 ])
  948. end
  949. end
  950. define_test "get_table should get a real table" do
  951. drop_test_table(@test_name)
  952. create_test_table(@test_name)
  953. table = table(@test_name)
  954. assert_not_equal(nil, table)
  955. table.close
  956. end
  957. end
  958. # rubocop:enable Metrics/ClassLength
  959. end