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.jupiter.api.Assertions.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.jupiter.api.AfterAll; 030import org.junit.jupiter.api.BeforeAll; 031import org.junit.jupiter.api.BeforeEach; 032import org.junit.jupiter.api.Tag; 033import org.junit.jupiter.api.Test; 034import org.junit.jupiter.api.TestInfo; 035 036@Tag(SmallTests.TAG) 037public class TestRESTApiClusterManager { 038 039 public static MockHttpApiRule mockHttpApi = new MockHttpApiRule(); 040 041 private static HBaseCommonTestingUtil testingUtility; 042 private ClusterManager clusterManager; 043 044 @BeforeAll 045 public static void beforeClass() throws Exception { 046 mockHttpApi.start(); 047 testingUtility = new HBaseCommonTestingUtil(); 048 configureClusterManager(testingUtility.getConfiguration()); 049 } 050 051 @AfterAll 052 public static void afterClass() throws Exception { 053 mockHttpApi.close(); 054 } 055 056 @BeforeEach 057 public void before(TestInfo testInfo) { 058 mockHttpApi.clearRegistrations(); 059 final Configuration methodConf = new Configuration(testingUtility.getConfiguration()); 060 methodConf.set("hbase.it.clustermanager.restapi.clustername", 061 testInfo.getTestMethod().get().getName()); 062 clusterManager = new RESTApiClusterManager(); 063 clusterManager.setConf(methodConf); 064 } 065 066 @Test 067 public void isRunningPositive(TestInfo testInfo) throws IOException { 068 final String clusterName = testInfo.getTestMethod().get().getName(); 069 final String hostName = "somehost"; 070 final String serviceName = "hbase"; 071 final String hostId = "some-id"; 072 registerServiceName(clusterName, Service.HBASE, serviceName); 073 registerHost(hostName, hostId); 074 final Map<String, String> hostProperties = new HashMap<>(); 075 hostProperties.put("roleState", "STARTED"); 076 hostProperties.put("healthSummary", "GOOD"); 077 registerHostProperties(clusterName, serviceName, hostId, ServiceType.HBASE_MASTER, 078 hostProperties); 079 assertTrue(clusterManager.isRunning(ServiceType.HBASE_MASTER, hostName, -1)); 080 } 081 082 private static void configureClusterManager(final Configuration conf) { 083 conf.set("hbase.it.clustermanager.restapi.hostname", mockHttpApi.getURI().toString()); 084 } 085 086 private static void registerServiceName(final String clusterName, final Service service, 087 final String serviceName) { 088 final String target = String.format("^/api/v6/clusters/%s/services", clusterName); 089 final String response = String 090 .format("{ \"items\": [ { \"type\": \"%s\", \"name\": \"%s\" } ] }", service, serviceName); 091 mockHttpApi.registerOk(target, response); 092 } 093 094 private static void registerHost(final String hostName, final String hostId) { 095 final String target = "^/api/v6/hosts"; 096 final String response = String 097 .format("{ \"items\": [ { \"hostname\": \"%s\", \"hostId\": \"%s\" } ] }", hostName, hostId); 098 mockHttpApi.registerOk(target, response); 099 } 100 101 private static void registerHostProperties(final String clusterName, final String serviceName, 102 final String hostId, final ServiceType serviceType, final Map<String, String> properties) { 103 final String target = 104 String.format("^/api/v6/clusters/%s/services/%s/roles", clusterName, serviceName); 105 final StringBuilder builder = 106 new StringBuilder().append("{ \"items\": [ ").append("{ \"hostRef\": { \"hostId\": \"") 107 .append(hostId).append("\" }, \"type\": \"").append(serviceType).append("\""); 108 properties 109 .forEach((k, v) -> builder.append(", \"").append(k).append("\": \"").append(v).append("\"")); 110 builder.append(" } ] }"); 111 mockHttpApi.registerOk(target, builder.toString()); 112 } 113}