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.rsgroup; 019 020import static org.junit.jupiter.api.Assertions.assertEquals; 021 022import java.io.BufferedReader; 023import java.io.File; 024import java.io.FileOutputStream; 025import java.io.FileReader; 026import java.io.IOException; 027import java.io.PrintWriter; 028import org.apache.hadoop.hbase.HBaseTestingUtil; 029import org.apache.hadoop.hbase.TableName; 030import org.apache.hadoop.hbase.rsgroup.RSGroupInfoManagerImpl.RSGroupMappingScript; 031import org.apache.hadoop.hbase.testclassification.RSGroupTests; 032import org.apache.hadoop.hbase.testclassification.SmallTests; 033import org.junit.jupiter.api.AfterEach; 034import org.junit.jupiter.api.BeforeAll; 035import org.junit.jupiter.api.BeforeEach; 036import org.junit.jupiter.api.Tag; 037import org.junit.jupiter.api.Test; 038import org.slf4j.Logger; 039import org.slf4j.LoggerFactory; 040 041@Tag(RSGroupTests.TAG) 042@Tag(SmallTests.TAG) 043public class TestRSGroupMappingScript { 044 private static final Logger LOG = LoggerFactory.getLogger(TestRSGroupMappingScript.class); 045 046 private static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); 047 private File script; 048 049 @BeforeAll 050 public static void setupScript() throws Exception { 051 String currentDir = new File("").getAbsolutePath(); 052 UTIL.getConfiguration().set(RSGroupInfoManagerImpl.RSGroupMappingScript.RS_GROUP_MAPPING_SCRIPT, 053 currentDir + "/rsgroup_table_mapping.sh"); 054 } 055 056 @BeforeEach 057 public void setup() throws Exception { 058 script = new File(UTIL.getConfiguration().get(RSGroupMappingScript.RS_GROUP_MAPPING_SCRIPT)); 059 if (!script.createNewFile()) { 060 throw new IOException("Can't create script"); 061 } 062 063 PrintWriter pw = new PrintWriter(new FileOutputStream(script)); 064 try { 065 pw.println("#!/bin/bash"); 066 pw.println("namespace=$1"); 067 pw.println("tablename=$2"); 068 pw.println("if [[ $namespace == test ]]; then"); 069 pw.println(" echo test"); 070 pw.println("elif [[ $tablename == *foo* ]]; then"); 071 pw.println(" echo other"); 072 pw.println("else"); 073 pw.println(" echo default"); 074 pw.println("fi"); 075 pw.flush(); 076 } finally { 077 pw.close(); 078 } 079 boolean executable = script.setExecutable(true); 080 LOG.info("Created " + script + ", executable=" + executable); 081 verifyScriptContent(script); 082 } 083 084 private void verifyScriptContent(File file) throws Exception { 085 BufferedReader reader = new BufferedReader(new FileReader(file)); 086 String line; 087 while ((line = reader.readLine()) != null) { 088 LOG.info(line); 089 } 090 } 091 092 @Test 093 public void testScript() throws Exception { 094 RSGroupMappingScript script = new RSGroupMappingScript(UTIL.getConfiguration()); 095 TableName testNamespace = TableName.valueOf("test", "should_be_in_test"); 096 String rsgroup = 097 script.getRSGroup(testNamespace.getNamespaceAsString(), testNamespace.getQualifierAsString()); 098 assertEquals("test", rsgroup); 099 100 TableName otherName = TableName.valueOf("whatever", "oh_foo_should_be_in_other"); 101 rsgroup = script.getRSGroup(otherName.getNamespaceAsString(), otherName.getQualifierAsString()); 102 assertEquals("other", rsgroup); 103 104 TableName defaultName = TableName.valueOf("nono", "should_be_in_default"); 105 rsgroup = 106 script.getRSGroup(defaultName.getNamespaceAsString(), defaultName.getQualifierAsString()); 107 assertEquals("default", rsgroup); 108 } 109 110 @AfterEach 111 public void teardown() throws Exception { 112 if (script.exists()) { 113 script.delete(); 114 } 115 } 116 117}