1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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 }