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.rest; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertFalse; 022import static org.junit.Assert.assertNotNull; 023import static org.junit.Assert.assertTrue; 024 025import java.io.ByteArrayInputStream; 026import java.io.IOException; 027import javax.xml.bind.JAXBContext; 028import javax.xml.bind.JAXBException; 029import org.apache.hadoop.conf.Configuration; 030import org.apache.hadoop.hbase.HBaseClassTestRule; 031import org.apache.hadoop.hbase.HBaseTestingUtil; 032import org.apache.hadoop.hbase.TableName; 033import org.apache.hadoop.hbase.Waiter; 034import org.apache.hadoop.hbase.rest.client.Client; 035import org.apache.hadoop.hbase.rest.client.Cluster; 036import org.apache.hadoop.hbase.rest.client.Response; 037import org.apache.hadoop.hbase.rest.model.StorageClusterStatusModel; 038import org.apache.hadoop.hbase.testclassification.MediumTests; 039import org.apache.hadoop.hbase.testclassification.RestTests; 040import org.apache.hadoop.hbase.util.Bytes; 041import org.junit.AfterClass; 042import org.junit.BeforeClass; 043import org.junit.ClassRule; 044import org.junit.Test; 045import org.junit.experimental.categories.Category; 046import org.slf4j.Logger; 047import org.slf4j.LoggerFactory; 048 049@Category({ RestTests.class, MediumTests.class }) 050public class TestStatusResource { 051 052 @ClassRule 053 public static final HBaseClassTestRule CLASS_RULE = 054 HBaseClassTestRule.forClass(TestStatusResource.class); 055 056 private static final Logger LOG = LoggerFactory.getLogger(TestStatusResource.class); 057 058 private static final byte[] META_REGION_NAME = Bytes.toBytes(TableName.META_TABLE_NAME + ",,1"); 059 060 private static final HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil(); 061 private static final HBaseRESTTestingUtility REST_TEST_UTIL = new HBaseRESTTestingUtility(); 062 private static Client client; 063 private static JAXBContext context; 064 private static Configuration conf; 065 066 private static void validate(StorageClusterStatusModel model) { 067 assertNotNull(model); 068 assertTrue(model.getRegions() + ">= 1", model.getRegions() >= 1); 069 assertTrue(model.getRequests() >= 0); 070 assertTrue(model.getAverageLoad() >= 0.0); 071 assertNotNull(model.getLiveNodes()); 072 assertNotNull(model.getDeadNodes()); 073 assertFalse(model.getLiveNodes().isEmpty()); 074 boolean foundMeta = false; 075 for (StorageClusterStatusModel.Node node : model.getLiveNodes()) { 076 assertNotNull(node.getName()); 077 assertTrue(node.getStartCode() > 0L); 078 assertTrue(node.getRequests() >= 0); 079 for (StorageClusterStatusModel.Node.Region region : node.getRegions()) { 080 if (Bytes.equals(region.getName(), META_REGION_NAME)) { 081 foundMeta = true; 082 } 083 } 084 } 085 assertTrue(foundMeta); 086 } 087 088 @BeforeClass 089 public static void setUpBeforeClass() throws Exception { 090 conf = TEST_UTIL.getConfiguration(); 091 TEST_UTIL.startMiniCluster(); 092 TEST_UTIL.createTable(TableName.valueOf("TestStatusResource"), Bytes.toBytes("D")); 093 TEST_UTIL.createTable(TableName.valueOf("TestStatusResource2"), Bytes.toBytes("D")); 094 REST_TEST_UTIL.startServletContainer(conf); 095 Cluster cluster = new Cluster(); 096 cluster.add("localhost", REST_TEST_UTIL.getServletPort()); 097 client = new Client(cluster); 098 context = JAXBContext.newInstance(StorageClusterStatusModel.class); 099 TEST_UTIL.waitFor(6000, new Waiter.Predicate<IOException>() { 100 @Override 101 public boolean evaluate() throws IOException { 102 return TEST_UTIL.getMiniHBaseCluster().getClusterMetrics().getAverageLoad() > 0; 103 } 104 }); 105 } 106 107 @AfterClass 108 public static void tearDownAfterClass() throws Exception { 109 REST_TEST_UTIL.shutdownServletContainer(); 110 TEST_UTIL.shutdownMiniCluster(); 111 } 112 113 @Test 114 public void testGetClusterStatusXML() throws IOException, JAXBException { 115 Response response = client.get("/status/cluster", Constants.MIMETYPE_XML); 116 assertEquals(200, response.getCode()); 117 assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type")); 118 StorageClusterStatusModel model = (StorageClusterStatusModel) context.createUnmarshaller() 119 .unmarshal(new ByteArrayInputStream(response.getBody())); 120 validate(model); 121 } 122 123 @Test 124 public void testGetClusterStatusPB() throws IOException { 125 Response response = client.get("/status/cluster", Constants.MIMETYPE_PROTOBUF); 126 assertEquals(200, response.getCode()); 127 assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type")); 128 StorageClusterStatusModel model = new StorageClusterStatusModel(); 129 model.getObjectFromMessage(response.getBody()); 130 validate(model); 131 response = client.get("/status/cluster", Constants.MIMETYPE_PROTOBUF_IETF); 132 assertEquals(200, response.getCode()); 133 assertEquals(Constants.MIMETYPE_PROTOBUF_IETF, response.getHeader("content-type")); 134 model = new StorageClusterStatusModel(); 135 model.getObjectFromMessage(response.getBody()); 136 validate(model); 137 } 138}