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