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 */
018
019package org.apache.hadoop.hbase.master.procedure;
020
021import org.apache.hadoop.conf.Configuration;
022import org.apache.hadoop.hbase.HBaseTestingUtility;
023import org.apache.hadoop.hbase.HTableDescriptor;
024import org.apache.hadoop.hbase.master.HMaster;
025import org.apache.hadoop.hbase.procedure2.ProcedureExecutor;
026import org.apache.hadoop.hbase.procedure2.ProcedureTestingUtility;
027import org.junit.After;
028import org.junit.AfterClass;
029import org.junit.Before;
030import org.junit.BeforeClass;
031import org.slf4j.Logger;
032import org.slf4j.LoggerFactory;
033
034import static org.junit.Assert.assertTrue;
035
036public abstract class TestTableDDLProcedureBase {
037  private static final Logger LOG = LoggerFactory.getLogger(TestTableDDLProcedureBase.class);
038  protected static final HBaseTestingUtility UTIL = new HBaseTestingUtility();
039
040  private static void setupConf(Configuration conf) {
041    conf.setInt(MasterProcedureConstants.MASTER_PROCEDURE_THREADS, 1);
042  }
043
044  @BeforeClass
045  public static void setupCluster() throws Exception {
046    setupConf(UTIL.getConfiguration());
047    UTIL.startMiniCluster(1);
048  }
049
050  @AfterClass
051  public static void cleanupTest() throws Exception {
052    try {
053      UTIL.shutdownMiniCluster();
054    } catch (Exception e) {
055      LOG.warn("failure shutting down cluster", e);
056    }
057  }
058
059  @Before
060  public void setup() throws Exception {
061    resetProcExecutorTestingKillFlag();
062  }
063
064  @After
065  public void tearDown() throws Exception {
066    resetProcExecutorTestingKillFlag();
067    for (HTableDescriptor htd: UTIL.getAdmin().listTables()) {
068      LOG.info("Tear down, remove table=" + htd.getTableName());
069      UTIL.deleteTable(htd.getTableName());
070    }
071  }
072
073  protected void resetProcExecutorTestingKillFlag() {
074    final ProcedureExecutor<MasterProcedureEnv> procExec = getMasterProcedureExecutor();
075    ProcedureTestingUtility.setKillAndToggleBeforeStoreUpdate(procExec, false);
076    assertTrue("expected executor to be running", procExec.isRunning());
077  }
078
079  protected ProcedureExecutor<MasterProcedureEnv> getMasterProcedureExecutor() {
080    return getMaster().getMasterProcedureExecutor();
081  }
082
083  protected HMaster getMaster() {
084    return UTIL.getHBaseCluster().getMaster();
085  }
086}