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(RSGroupMappingScript.RS_GROUP_MAPPING_SCRIPT,
055      currentDir + "/rsgroup_table_mapping.sh");
056  }
057
058  @Before
059  public void setup() throws Exception {
060    script = new File(UTIL.getConfiguration().get(RSGroupMappingScript.RS_GROUP_MAPPING_SCRIPT));
061    if (!script.createNewFile()) {
062      throw new IOException("Can't create script");
063    }
064
065    PrintWriter pw = new PrintWriter(new FileOutputStream(script));
066    try {
067      pw.println("#!/bin/bash");
068      pw.println("namespace=$1");
069      pw.println("tablename=$2");
070      pw.println("if [[ $namespace == test ]]; then");
071      pw.println("  echo test");
072      pw.println("elif [[ $tablename == *foo* ]]; then");
073      pw.println("  echo other");
074      pw.println("else");
075      pw.println("  echo default");
076      pw.println("fi");
077      pw.flush();
078    } finally {
079      pw.close();
080    }
081    boolean executable = script.setExecutable(true);
082    LOG.info("Created " + script + ", executable=" + executable);
083    verifyScriptContent(script);
084  }
085
086  private void verifyScriptContent(File file) throws Exception {
087    BufferedReader reader = new BufferedReader(new FileReader(file));
088    String line;
089    while ((line = reader.readLine()) != null) {
090      LOG.info(line);
091    }
092  }
093
094  @Test
095  public void testScript() throws Exception {
096    RSGroupMappingScript script = new RSGroupMappingScript(UTIL.getConfiguration());
097    TableName testNamespace = TableName.valueOf("test", "should_be_in_test");
098    String rsgroup =
099      script.getRSGroup(testNamespace.getNamespaceAsString(), testNamespace.getQualifierAsString());
100    Assert.assertEquals("test", rsgroup);
101
102    TableName otherName = TableName.valueOf("whatever", "oh_foo_should_be_in_other");
103    rsgroup = script.getRSGroup(otherName.getNamespaceAsString(), otherName.getQualifierAsString());
104    Assert.assertEquals("other", rsgroup);
105
106    TableName defaultName = TableName.valueOf("nono", "should_be_in_default");
107    rsgroup =
108      script.getRSGroup(defaultName.getNamespaceAsString(), defaultName.getQualifierAsString());
109    Assert.assertEquals("default", rsgroup);
110  }
111
112  @After
113  public void teardown() throws Exception {
114    if (script.exists()) {
115      script.delete();
116    }
117  }
118
119}