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;
019
020import static org.junit.Assert.assertTrue;
021
022import java.io.IOException;
023import java.util.HashMap;
024import java.util.Map;
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.ClusterManager.ServiceType;
027import org.apache.hadoop.hbase.RESTApiClusterManager.Service;
028import org.apache.hadoop.hbase.testclassification.SmallTests;
029import org.junit.Before;
030import org.junit.BeforeClass;
031import org.junit.ClassRule;
032import org.junit.Rule;
033import org.junit.Test;
034import org.junit.experimental.categories.Category;
035import org.junit.rules.TestName;
036
037@Category(SmallTests.class)
038public class TestRESTApiClusterManager {
039
040  @ClassRule
041  public static final HBaseClassTestRule testRule =
042    HBaseClassTestRule.forClass(TestRESTApiClusterManager.class);
043
044  @ClassRule
045  public static MockHttpApiRule mockHttpApi = new MockHttpApiRule();
046
047  @Rule
048  public final TestName testName = new TestName();
049
050  private static HBaseCommonTestingUtility testingUtility;
051  private ClusterManager clusterManager;
052
053  @BeforeClass
054  public static void beforeClass() {
055    testingUtility = new HBaseCommonTestingUtility();
056    configureClusterManager(testingUtility.getConfiguration());
057  }
058
059  @Before
060  public void before() {
061    mockHttpApi.clearRegistrations();
062    final Configuration methodConf = new Configuration(testingUtility.getConfiguration());
063    methodConf.set("hbase.it.clustermanager.restapi.clustername", testName.getMethodName());
064    clusterManager = new RESTApiClusterManager();
065    clusterManager.setConf(methodConf);
066  }
067
068  @Test
069  public void isRunningPositive() throws IOException {
070    final String clusterName = testName.getMethodName();
071    final String hostName = "somehost";
072    final String serviceName = "hbase";
073    final String hostId = "some-id";
074    registerServiceName(clusterName, Service.HBASE, serviceName);
075    registerHost(hostName, hostId);
076    final Map<String, String> hostProperties = new HashMap<>();
077    hostProperties.put("roleState", "STARTED");
078    hostProperties.put("healthSummary", "GOOD");
079    registerHostProperties(clusterName, serviceName, hostId, ServiceType.HBASE_MASTER,
080      hostProperties);
081    assertTrue(clusterManager.isRunning(ServiceType.HBASE_MASTER, hostName, -1));
082  }
083
084  private static void configureClusterManager(final Configuration conf) {
085    conf.set("hbase.it.clustermanager.restapi.hostname", mockHttpApi.getURI().toString());
086  }
087
088  private static void registerServiceName(final String clusterName, final Service service,
089    final String serviceName) {
090    final String target = String.format("^/api/v6/clusters/%s/services", clusterName);
091    final String response = String
092      .format("{ \"items\": [ { \"type\": \"%s\", \"name\": \"%s\" } ] }", service, serviceName);
093    mockHttpApi.registerOk(target, response);
094  }
095
096  private static void registerHost(final String hostName, final String hostId) {
097    final String target = "^/api/v6/hosts";
098    final String response = String
099      .format("{ \"items\": [ { \"hostname\": \"%s\", \"hostId\": \"%s\" } ] }", hostName, hostId);
100    mockHttpApi.registerOk(target, response);
101  }
102
103  private static void registerHostProperties(final String clusterName, final String serviceName,
104    final String hostId, final ServiceType serviceType, final Map<String, String> properties) {
105    final String target =
106      String.format("^/api/v6/clusters/%s/services/%s/roles", clusterName, serviceName);
107    final StringBuilder builder =
108      new StringBuilder().append("{ \"items\": [ ").append("{ \"hostRef\": { \"hostId\": \"")
109        .append(hostId).append("\" }, \"type\": \"").append(serviceType).append("\"");
110    properties
111      .forEach((k, v) -> builder.append(", \"").append(k).append("\": \"").append(v).append("\""));
112    builder.append(" } ] }");
113    mockHttpApi.registerOk(target, builder.toString());
114  }
115}