1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.rest;
21
22 import java.io.ByteArrayInputStream;
23 import java.io.IOException;
24
25 import javax.xml.bind.JAXBContext;
26 import javax.xml.bind.JAXBException;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.hadoop.hbase.HBaseTestingUtility;
31 import org.apache.hadoop.hbase.testclassification.MediumTests;
32 import org.apache.hadoop.hbase.TableName;
33 import org.apache.hadoop.hbase.Waiter;
34 import org.apache.hadoop.hbase.rest.client.Client;
35 import org.apache.hadoop.hbase.rest.client.Cluster;
36 import org.apache.hadoop.hbase.rest.client.Response;
37 import org.apache.hadoop.hbase.rest.model.StorageClusterStatusModel;
38 import org.apache.hadoop.hbase.util.Bytes;
39
40 import static org.junit.Assert.*;
41
42 import org.junit.AfterClass;
43 import org.junit.BeforeClass;
44 import org.junit.Test;
45 import org.junit.experimental.categories.Category;
46
47 @Category(MediumTests.class)
48 public class TestStatusResource {
49 private static final Log LOG = LogFactory.getLog(TestStatusResource.class);
50
51 private static final byte[] META_REGION_NAME = Bytes.toBytes(TableName.META_TABLE_NAME+",,1");
52
53 private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
54 private static final HBaseRESTTestingUtility REST_TEST_UTIL =
55 new HBaseRESTTestingUtility();
56 private static Client client;
57 private static JAXBContext context;
58
59 private static void validate(StorageClusterStatusModel model) {
60 assertNotNull(model);
61 assertTrue(model.getRegions() + ">= 1", model.getRegions() >= 1);
62 assertTrue(model.getRequests() >= 0);
63 assertTrue(model.getAverageLoad() >= 0.0);
64 assertNotNull(model.getLiveNodes());
65 assertNotNull(model.getDeadNodes());
66 assertFalse(model.getLiveNodes().isEmpty());
67 boolean foundMeta = false;
68 for (StorageClusterStatusModel.Node node: model.getLiveNodes()) {
69 assertNotNull(node.getName());
70 assertTrue(node.getStartCode() > 0L);
71 assertTrue(node.getRequests() >= 0);
72 for (StorageClusterStatusModel.Node.Region region: node.getRegions()) {
73 if (Bytes.equals(region.getName(), META_REGION_NAME)) {
74 foundMeta = true;
75 }
76 }
77 }
78 assertTrue(foundMeta);
79 }
80
81 @BeforeClass
82 public static void setUpBeforeClass() throws Exception {
83 TEST_UTIL.startMiniCluster();
84 REST_TEST_UTIL.startServletContainer(TEST_UTIL.getConfiguration());
85 client = new Client(new Cluster().add("localhost",
86 REST_TEST_UTIL.getServletPort()));
87 context = JAXBContext.newInstance(StorageClusterStatusModel.class);
88 }
89
90 @AfterClass
91 public static void tearDownAfterClass() throws Exception {
92 REST_TEST_UTIL.shutdownServletContainer();
93 TEST_UTIL.shutdownMiniCluster();
94 }
95
96 @Test
97 public void testGetClusterStatusXML() throws IOException, JAXBException {
98 Response response = client.get("/status/cluster", Constants.MIMETYPE_XML);
99 assertEquals(response.getCode(), 200);
100 assertEquals(Constants.MIMETYPE_XML, response.getHeader("content-type"));
101 StorageClusterStatusModel model = (StorageClusterStatusModel)
102 context.createUnmarshaller().unmarshal(
103 new ByteArrayInputStream(response.getBody()));
104 validate(model);
105 }
106
107 @Test
108 public void testGetClusterStatusPB() throws IOException {
109 Response response = client.get("/status/cluster", Constants.MIMETYPE_PROTOBUF);
110 assertEquals(response.getCode(), 200);
111 assertEquals(Constants.MIMETYPE_PROTOBUF, response.getHeader("content-type"));
112 StorageClusterStatusModel model = new StorageClusterStatusModel();
113 model.getObjectFromMessage(response.getBody());
114 validate(model);
115 response = client.get("/status/cluster", Constants.MIMETYPE_PROTOBUF_IETF);
116 assertEquals(response.getCode(), 200);
117 assertEquals(Constants.MIMETYPE_PROTOBUF_IETF, response.getHeader("content-type"));
118 model = new StorageClusterStatusModel();
119 model.getObjectFromMessage(response.getBody());
120 validate(model);
121 }
122 }