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