1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.hadoop.hbase.rest;
22
23 import java.io.ByteArrayInputStream;
24 import java.io.IOException;
25
26 import javax.xml.bind.JAXBContext;
27 import javax.xml.bind.JAXBException;
28
29 import org.apache.hadoop.hbase.rest.client.Client;
30 import org.apache.hadoop.hbase.rest.client.Cluster;
31 import org.apache.hadoop.hbase.rest.client.Response;
32 import org.apache.hadoop.hbase.rest.model.StorageClusterStatusModel;
33 import org.apache.hadoop.hbase.util.Bytes;
34
35 public class TestStatusResource extends HBaseRESTClusterTestBase {
36 static final byte[] ROOT_REGION_NAME = Bytes.toBytes("-ROOT-,,0");
37 static final byte[] META_REGION_NAME = Bytes.toBytes(".META.,,1");
38
39 Client client;
40 JAXBContext context;
41
42 @Override
43 protected void setUp() throws Exception {
44 super.setUp();
45 context = JAXBContext.newInstance(
46 StorageClusterStatusModel.class);
47 client = new Client(new Cluster().add("localhost", testServletPort));
48 }
49
50 @Override
51 protected void tearDown() throws Exception {
52 client.shutdown();
53 super.tearDown();
54 }
55
56 void validate(StorageClusterStatusModel model) {
57 assertNotNull(model);
58 assertTrue(model.getRegions() >= 2);
59 assertTrue(model.getRequests() >= 0);
60
61 assertTrue(model.getAverageLoad() >= 1.0);
62 assertNotNull(model.getLiveNodes());
63 assertNotNull(model.getDeadNodes());
64 assertFalse(model.getLiveNodes().isEmpty());
65 boolean foundRoot = false, foundMeta = false;
66 for (StorageClusterStatusModel.Node node: model.getLiveNodes()) {
67 assertNotNull(node.getName());
68 assertTrue(node.getStartCode() > 0L);
69 assertTrue(node.getRequests() >= 0);
70 assertFalse(node.getRegions().isEmpty());
71 for (StorageClusterStatusModel.Node.Region region: node.getRegions()) {
72 if (Bytes.equals(region.getName(), ROOT_REGION_NAME)) {
73 foundRoot = true;
74 } else if (Bytes.equals(region.getName(), META_REGION_NAME)) {
75 foundMeta = true;
76 }
77 }
78 }
79 assertTrue(foundRoot);
80 assertTrue(foundMeta);
81 }
82
83 void doTestGetClusterStatusXML() throws IOException, JAXBException {
84 Response response = client.get("/status/cluster", MIMETYPE_XML);
85 assertEquals(response.getCode(), 200);
86 StorageClusterStatusModel model = (StorageClusterStatusModel)
87 context.createUnmarshaller().unmarshal(
88 new ByteArrayInputStream(response.getBody()));
89 validate(model);
90 }
91
92 void doTestGetClusterStatusPB() throws IOException {
93 Response response = client.get("/status/cluster", MIMETYPE_PROTOBUF);
94 assertEquals(response.getCode(), 200);
95 StorageClusterStatusModel model = new StorageClusterStatusModel();
96 model.getObjectFromMessage(response.getBody());
97 validate(model);
98 }
99
100 public void testStatusResource() throws Exception {
101 doTestGetClusterStatusXML();
102 doTestGetClusterStatusPB();
103 }
104 }