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.HBaseTestingUtil; 028import org.apache.hadoop.hbase.TableName; 029import org.apache.hadoop.hbase.rsgroup.RSGroupInfoManagerImpl.RSGroupMappingScript; 030import org.apache.hadoop.hbase.testclassification.RSGroupTests; 031import org.apache.hadoop.hbase.testclassification.SmallTests; 032import org.junit.After; 033import org.junit.Assert; 034import org.junit.Before; 035import org.junit.BeforeClass; 036import org.junit.ClassRule; 037import org.junit.Test; 038import org.junit.experimental.categories.Category; 039import org.slf4j.Logger; 040import org.slf4j.LoggerFactory; 041 042@Category({ RSGroupTests.class, SmallTests.class }) 043public class TestRSGroupMappingScript { 044 045 @ClassRule 046 public static final HBaseClassTestRule CLASS_RULE = 047 HBaseClassTestRule.forClass(TestRSGroupMappingScript.class); 048 private static final Logger LOG = LoggerFactory.getLogger(TestRSGroupMappingScript.class); 049 050 private static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); 051 private File script; 052 053 @BeforeClass 054 public static void setupScript() throws Exception { 055 String currentDir = new File("").getAbsolutePath(); 056 UTIL.getConfiguration().set(RSGroupInfoManagerImpl.RSGroupMappingScript.RS_GROUP_MAPPING_SCRIPT, 057 currentDir + "/rsgroup_table_mapping.sh"); 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 = TableName.valueOf("test", "should_be_in_test"); 100 String rsgroup = 101 script.getRSGroup(testNamespace.getNamespaceAsString(), testNamespace.getQualifierAsString()); 102 Assert.assertEquals("test", rsgroup); 103 104 TableName otherName = TableName.valueOf("whatever", "oh_foo_should_be_in_other"); 105 rsgroup = script.getRSGroup(otherName.getNamespaceAsString(), otherName.getQualifierAsString()); 106 Assert.assertEquals("other", rsgroup); 107 108 TableName defaultName = TableName.valueOf("nono", "should_be_in_default"); 109 rsgroup = 110 script.getRSGroup(defaultName.getNamespaceAsString(), defaultName.getQualifierAsString()); 111 Assert.assertEquals("default", rsgroup); 112 } 113 114 @After 115 public void teardown() throws Exception { 116 if (script.exists()) { 117 script.delete(); 118 } 119 } 120 121}