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  
19  package org.apache.hadoop.hbase;
20  
21  import java.util.Set;
22  
23  import org.apache.commons.cli.CommandLine;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.chaos.factories.MonkeyFactory;
26  import org.apache.hadoop.hbase.chaos.monkies.ChaosMonkey;
27  import org.apache.hadoop.hbase.util.AbstractHBaseTool;
28  import org.junit.After;
29  import org.junit.Before;
30  
31  /**
32   * Base class for HBase integration tests that want to use the Chaos Monkey.
33   */
34  public abstract class IntegrationTestBase extends AbstractHBaseTool {
35    public static final String LONG_OPT = "monkey";
36  
37    protected IntegrationTestingUtility util;
38    protected ChaosMonkey monkey;
39    protected String monkeyToUse;
40  
41    public IntegrationTestBase() {
42      this(MonkeyFactory.CALM);
43    }
44  
45    public IntegrationTestBase(String monkeyToUse) {
46      this.monkeyToUse = monkeyToUse;
47    }
48  
49    @Override
50    protected void addOptions() {
51      addOptWithArg("m", LONG_OPT, "Which chaos monkey to run");
52    }
53  
54    @Override
55    protected void processOptions(CommandLine cmd) {
56      if (cmd.hasOption(LONG_OPT)) {
57        monkeyToUse = cmd.getOptionValue(LONG_OPT);
58      }
59    }
60  
61    @Override
62    public Configuration getConf() {
63      Configuration c = super.getConf();
64      if (c == null && util != null) {
65        conf = util.getConfiguration();
66        c = conf;
67      }
68      return c;
69    }
70  
71    @Override
72    protected int doWork() throws Exception {
73      setUpMonkey();
74      setUp();
75      int result = -1;
76      try {
77        runTestFromCommandLine();
78      } finally {
79        cleanUpMonkey();
80        cleanUp();
81      }
82  
83      return result;
84    }
85  
86    @Before
87    public void setUpMonkey() throws Exception {
88      util = getTestingUtil(getConf());
89      MonkeyFactory fact = MonkeyFactory.getFactory(monkeyToUse);
90      monkey = fact.setUtil(util)
91                   .setTableName(getTablename())
92                   .setColumnFamilies(getColumnFamilies()).build();
93    }
94  
95    @After
96    public void cleanUpMonkey() throws Exception {
97      monkey.stop("Ending test");
98      monkey.waitForStop();
99    }
100 
101   protected IntegrationTestingUtility getTestingUtil(Configuration conf) {
102     if (this.util == null) {
103       if (conf == null) {
104         this.util = new IntegrationTestingUtility();
105       } else {
106         this.util = new IntegrationTestingUtility(conf);
107       }
108     }
109     return util;
110   }
111 
112   public abstract void setUp() throws Exception;
113 
114   public abstract void cleanUp()  throws Exception;
115 
116   public abstract int runTestFromCommandLine() throws Exception;
117 
118   public abstract String getTablename();
119 
120   protected abstract Set<String> getColumnFamilies();
121 }