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 * <p>
010 * http://www.apache.org/licenses/LICENSE-2.0
011 * <p>
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.tool;
019
020import java.io.IOException;
021import java.io.StringWriter;
022import org.apache.hadoop.hbase.HBaseClassTestRule;
023import org.apache.hadoop.hbase.ServerName;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.client.RegionInfo;
026import org.apache.hadoop.hbase.client.RegionInfoBuilder;
027import org.apache.hadoop.hbase.testclassification.SmallTests;
028import org.apache.hadoop.hbase.tmpl.tool.CanaryStatusTmpl;
029import org.junit.Assert;
030import org.junit.ClassRule;
031import org.junit.Test;
032import org.junit.experimental.categories.Category;
033
034
035@Category({ SmallTests.class })
036public class TestCanaryStatusServlet {
037  @ClassRule
038  public static final HBaseClassTestRule CLASS_RULE =
039    HBaseClassTestRule.forClass(TestCanaryStatusServlet.class);
040
041  @Test
042  public void testFailures() throws IOException {
043    CanaryTool.RegionStdOutSink regionStdOutSink = new CanaryTool.RegionStdOutSink();
044
045    ServerName serverName1 = ServerName.valueOf("staging-st04.server:22600",
046      1584180761635L);
047    TableName fakeTableName1 = TableName.valueOf("fakeTableName1");
048    RegionInfo regionInfo1 = RegionInfoBuilder.newBuilder(fakeTableName1).build();
049
050    ServerName serverName2 = ServerName.valueOf("staging-st05.server:22600",
051      1584180761636L);
052    TableName fakeTableName2 = TableName.valueOf("fakeTableName2");
053    RegionInfo regionInfo2 = RegionInfoBuilder.newBuilder(fakeTableName2).build();
054
055    regionStdOutSink.publishReadFailure(serverName1, regionInfo1, new IOException());
056    regionStdOutSink.publishWriteFailure(serverName2, regionInfo2, new IOException());
057    CanaryStatusTmpl tmpl = new CanaryStatusTmpl();
058    StringWriter renderResultWriter = new StringWriter();
059    tmpl.render(renderResultWriter, regionStdOutSink);
060    String renderResult = renderResultWriter.toString();
061    Assert.assertTrue(renderResult.contains("staging-st04.server,22600"));
062    Assert.assertTrue(renderResult.contains("fakeTableName1"));
063    Assert.assertTrue(renderResult.contains("staging-st05.server,22600"));
064    Assert.assertTrue(renderResult.contains("fakeTableName2"));
065
066  }
067
068  @Test
069  public void testReadFailuresOnly() throws IOException {
070    CanaryTool.RegionStdOutSink regionStdOutSink = new CanaryTool.RegionStdOutSink();
071
072    ServerName serverName1 = ServerName.valueOf("staging-st04.server:22600",
073      1584180761635L);
074    TableName fakeTableName1 = TableName.valueOf("fakeTableName1");
075    RegionInfo regionInfo1 = RegionInfoBuilder.newBuilder(fakeTableName1).build();
076
077    regionStdOutSink.publishReadFailure(serverName1, regionInfo1, new IOException());
078    CanaryStatusTmpl tmpl = new CanaryStatusTmpl();
079    StringWriter renderResultWriter = new StringWriter();
080    tmpl.render(renderResultWriter, regionStdOutSink);
081    String renderResult = renderResultWriter.toString();
082    Assert.assertTrue(renderResult.contains("staging-st04.server,22600"));
083    Assert.assertTrue(renderResult.contains("fakeTableName1"));
084  }
085
086  @Test
087  public void testWriteFailuresOnly() throws IOException {
088    CanaryTool.RegionStdOutSink regionStdOutSink = new CanaryTool.RegionStdOutSink();
089
090    ServerName serverName2 = ServerName.valueOf("staging-st05.server:22600",
091      1584180761636L);
092    TableName fakeTableName2 = TableName.valueOf("fakeTableName2");
093    RegionInfo regionInfo2 = RegionInfoBuilder.newBuilder(fakeTableName2).build();
094
095    regionStdOutSink.publishReadFailure(serverName2, regionInfo2, new IOException());
096    CanaryStatusTmpl tmpl = new CanaryStatusTmpl();
097    StringWriter renderResultWriter = new StringWriter();
098    tmpl.render(renderResultWriter, regionStdOutSink);
099    String renderResult = renderResultWriter.toString();
100    Assert.assertTrue(renderResult.contains("staging-st05.server,22600"));
101    Assert.assertTrue(renderResult.contains("fakeTableName2"));
102
103  }
104
105  @Test
106  public void testNoFailures() throws IOException {
107    CanaryTool.RegionStdOutSink regionStdOutSink = new CanaryTool.RegionStdOutSink();
108    CanaryStatusTmpl tmpl = new CanaryStatusTmpl();
109    StringWriter renderResultWriter = new StringWriter();
110    tmpl.render(renderResultWriter, regionStdOutSink);
111    String renderResult = renderResultWriter.toString();
112    Assert.assertTrue(renderResult.contains("Total Failed Servers: 0"));
113    Assert.assertTrue(renderResult.contains("Total Failed Tables: 0"));
114  }
115
116}