View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.rsgroup;
19  
20  import java.io.BufferedReader;
21  import java.io.File;
22  import java.io.FileOutputStream;
23  import java.io.FileReader;
24  import java.io.IOException;
25  import java.io.PrintWriter;
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.hadoop.hbase.HBaseTestingUtility;
29  import org.apache.hadoop.hbase.TableName;
30  import org.apache.hadoop.hbase.rsgroup.RSGroupAdminEndpoint.RSGroupMappingScript;
31  import org.apache.hadoop.hbase.testclassification.SmallTests;
32  import org.junit.After;
33  import org.junit.Assert;
34  import org.junit.Before;
35  import org.junit.BeforeClass;
36  import org.junit.Test;
37  import org.junit.experimental.categories.Category;
38  
39  @Category({ SmallTests.class })
40  public class TestRSGroupMappingScript {
41  
42    private static final Log LOG = LogFactory.getLog(TestRSGroupMappingScript.class);
43  
44    private static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
45    private File script;
46  
47    @BeforeClass
48    public static void setupScript() throws Exception {
49      String currentDir = new File("").getAbsolutePath();
50      UTIL.getConfiguration().set(
51          RSGroupMappingScript.RS_GROUP_MAPPING_SCRIPT,
52          currentDir + "/rsgroup_table_mapping.sh"
53      );
54    }
55  
56    @Before
57    public void setup() throws Exception {
58      script = new File(UTIL.getConfiguration().get(RSGroupMappingScript.RS_GROUP_MAPPING_SCRIPT));
59      if (!script.createNewFile()) {
60        throw new IOException("Can't create script");
61      }
62  
63      PrintWriter pw = new PrintWriter(new FileOutputStream(script));
64      try {
65        pw.println("#!/bin/bash");
66        pw.println("namespace=$1");
67        pw.println("tablename=$2");
68        pw.println("if [[ $namespace == test ]]; then");
69        pw.println("  echo test");
70        pw.println("elif [[ $tablename == *foo* ]]; then");
71        pw.println("  echo other");
72        pw.println("else");
73        pw.println("  echo default");
74        pw.println("fi");
75        pw.flush();
76      } finally {
77        pw.close();
78      }
79      boolean executable = script.setExecutable(true);
80      LOG.info("Created " + script  + ", executable=" + executable);
81      verifyScriptContent(script);
82    }
83  
84    private void verifyScriptContent(File file) throws Exception {
85      BufferedReader reader = new BufferedReader(new FileReader(file));
86      String line;
87      while ((line = reader.readLine()) != null) {
88        LOG.info(line);
89      }
90    }
91  
92    @Test
93    public void testScript() throws Exception {
94      RSGroupMappingScript script = new RSGroupMappingScript(UTIL.getConfiguration());
95      TableName testNamespace =
96        TableName.valueOf("test", "should_be_in_test");
97      String rsgroup = script.getRSGroup(
98        testNamespace.getNamespaceAsString(), testNamespace.getQualifierAsString()
99      );
100     Assert.assertEquals("test", rsgroup);
101 
102     TableName otherName =
103       TableName.valueOf("whatever", "oh_foo_should_be_in_other");
104     rsgroup = script.getRSGroup(otherName.getNamespaceAsString(), otherName.getQualifierAsString());
105     Assert.assertEquals("other", rsgroup);
106 
107     TableName defaultName =
108       TableName.valueOf("nono", "should_be_in_default");
109     rsgroup = script.getRSGroup(
110       defaultName.getNamespaceAsString(), defaultName.getQualifierAsString()
111     );
112     Assert.assertEquals("default", rsgroup);
113   }
114 
115   @After
116   public void teardown() throws Exception {
117     if (script.exists()) {
118       script.delete();
119     }
120   }
121 
122 }