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.HBaseTestingUtility;
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 HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
061  private static final HBaseRESTTestingUtility REST_TEST_UTIL =
062      new HBaseRESTTestingUtility();
063  private static Client client;
064  private static JAXBContext context;
065  private static Configuration conf;
066
067  private static void validate(StorageClusterStatusModel model) {
068    assertNotNull(model);
069    assertTrue(model.getRegions() + ">= 1", model.getRegions() >= 1);
070    assertTrue(model.getRequests() >= 0);
071    assertTrue(model.getAverageLoad() >= 0.0);
072    assertNotNull(model.getLiveNodes());
073    assertNotNull(model.getDeadNodes());
074    assertFalse(model.getLiveNodes().isEmpty());
075    boolean foundMeta = false;
076    for (StorageClusterStatusModel.Node node: model.getLiveNodes()) {
077      assertNotNull(node.getName());
078      assertTrue(node.getStartCode() > 0L);
079      assertTrue(node.getRequests() >= 0);
080      for (StorageClusterStatusModel.Node.Region region: node.getRegions()) {
081        if (Bytes.equals(region.getName(), META_REGION_NAME)) {
082          foundMeta = true;
083        }
084      }
085    }
086    assertTrue(foundMeta);
087  }
088
089  @BeforeClass
090  public static void setUpBeforeClass() throws Exception {
091    conf = TEST_UTIL.getConfiguration();
092    TEST_UTIL.startMiniCluster();
093    TEST_UTIL.createTable(TableName.valueOf("TestStatusResource"), Bytes.toBytes("D"));
094    TEST_UTIL.createTable(TableName.valueOf("TestStatusResource2"), Bytes.toBytes("D"));
095    REST_TEST_UTIL.startServletContainer(conf);
096    Cluster cluster = new Cluster();
097    cluster.add("localhost", REST_TEST_UTIL.getServletPort());
098    client = new Client(cluster);
099    context = JAXBContext.newInstance(StorageClusterStatusModel.class);
100    TEST_UTIL.waitFor(6000, new Waiter.Predicate<IOException>() {
101      @Override
102      public boolean evaluate() throws IOException {
103        return TEST_UTIL.getMiniHBaseCluster().getClusterStatus().getAverageLoad() > 0;
104      }
105    });
106  }
107
108  @AfterClass
109  public static void tearDownAfterClass() throws Exception {
110    REST_TEST_UTIL.shutdownServletContainer();
111    TEST_UTIL.shutdownMiniCluster();
112  }
113
114  @Test
115  public void testGetClusterStatusXML() throws IOException, JAXBException {
116    Response response = client.get("/status/cluster", Constants.MIMETYPE_XML);
117    assertEquals(200, response.getCode());
118    assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
119    StorageClusterStatusModel model = (StorageClusterStatusModel)
120      context.createUnmarshaller().unmarshal(
121        new ByteArrayInputStream(response.getBody()));
122    validate(model);
123  }
124
125  @Test
126  public void testGetClusterStatusPB() throws IOException {
127    Response response = client.get("/status/cluster", Constants.MIMETYPE_PROTOBUF);
128    assertEquals(200, response.getCode());
129    assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type"));
130    StorageClusterStatusModel model = new StorageClusterStatusModel();
131    model.getObjectFromMessage(response.getBody());
132    validate(model);
133    response = client.get("/status/cluster", Constants.MIMETYPE_PROTOBUF_IETF);
134    assertEquals(200, response.getCode());
135    assertEquals(Constants.MIMETYPE_PROTOBUF_IETF, response.getHeader("content-type"));
136    model = new StorageClusterStatusModel();
137    model.getObjectFromMessage(response.getBody());
138    validate(model);
139  }
140}