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.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@Category({ SmallTests.class })
035public class TestCanaryStatusServlet {
036  @ClassRule
037  public static final HBaseClassTestRule CLASS_RULE =
038    HBaseClassTestRule.forClass(TestCanaryStatusServlet.class);
039
040  @Test
041  public void testFailures() throws IOException {
042    CanaryTool.RegionStdOutSink regionStdOutSink = new CanaryTool.RegionStdOutSink();
043
044    ServerName serverName1 = ServerName.valueOf("staging-st04.server:22600", 1584180761635L);
045    TableName fakeTableName1 = TableName.valueOf("fakeTableName1");
046    RegionInfo regionInfo1 = RegionInfoBuilder.newBuilder(fakeTableName1).build();
047
048    ServerName serverName2 = ServerName.valueOf("staging-st05.server:22600", 1584180761636L);
049    TableName fakeTableName2 = TableName.valueOf("fakeTableName2");
050    RegionInfo regionInfo2 = RegionInfoBuilder.newBuilder(fakeTableName2).build();
051
052    regionStdOutSink.publishReadFailure(serverName1, regionInfo1, new IOException());
053    regionStdOutSink.publishWriteFailure(serverName2, regionInfo2, new IOException());
054    CanaryStatusTmpl tmpl = new CanaryStatusTmpl();
055    StringWriter renderResultWriter = new StringWriter();
056    tmpl.render(renderResultWriter, regionStdOutSink);
057    String renderResult = renderResultWriter.toString();
058    Assert.assertTrue(renderResult.contains("staging-st04.server,22600"));
059    Assert.assertTrue(renderResult.contains("fakeTableName1"));
060    Assert.assertTrue(renderResult.contains("staging-st05.server,22600"));
061    Assert.assertTrue(renderResult.contains("fakeTableName2"));
062
063  }
064
065  @Test
066  public void testReadFailuresOnly() throws IOException {
067    CanaryTool.RegionStdOutSink regionStdOutSink = new CanaryTool.RegionStdOutSink();
068
069    ServerName serverName1 = ServerName.valueOf("staging-st04.server:22600", 1584180761635L);
070    TableName fakeTableName1 = TableName.valueOf("fakeTableName1");
071    RegionInfo regionInfo1 = RegionInfoBuilder.newBuilder(fakeTableName1).build();
072
073    regionStdOutSink.publishReadFailure(serverName1, regionInfo1, new IOException());
074    CanaryStatusTmpl tmpl = new CanaryStatusTmpl();
075    StringWriter renderResultWriter = new StringWriter();
076    tmpl.render(renderResultWriter, regionStdOutSink);
077    String renderResult = renderResultWriter.toString();
078    Assert.assertTrue(renderResult.contains("staging-st04.server,22600"));
079    Assert.assertTrue(renderResult.contains("fakeTableName1"));
080  }
081
082  @Test
083  public void testWriteFailuresOnly() throws IOException {
084    CanaryTool.RegionStdOutSink regionStdOutSink = new CanaryTool.RegionStdOutSink();
085
086    ServerName serverName2 = ServerName.valueOf("staging-st05.server:22600", 1584180761636L);
087    TableName fakeTableName2 = TableName.valueOf("fakeTableName2");
088    RegionInfo regionInfo2 = RegionInfoBuilder.newBuilder(fakeTableName2).build();
089
090    regionStdOutSink.publishReadFailure(serverName2, regionInfo2, new IOException());
091    CanaryStatusTmpl tmpl = new CanaryStatusTmpl();
092    StringWriter renderResultWriter = new StringWriter();
093    tmpl.render(renderResultWriter, regionStdOutSink);
094    String renderResult = renderResultWriter.toString();
095    Assert.assertTrue(renderResult.contains("staging-st05.server,22600"));
096    Assert.assertTrue(renderResult.contains("fakeTableName2"));
097
098  }
099
100  @Test
101  public void testNoFailures() throws IOException {
102    CanaryTool.RegionStdOutSink regionStdOutSink = new CanaryTool.RegionStdOutSink();
103    CanaryStatusTmpl tmpl = new CanaryStatusTmpl();
104    StringWriter renderResultWriter = new StringWriter();
105    tmpl.render(renderResultWriter, regionStdOutSink);
106    String renderResult = renderResultWriter.toString();
107    Assert.assertTrue(renderResult.contains("Total Failed Servers: 0"));
108    Assert.assertTrue(renderResult.contains("Total Failed Tables: 0"));
109  }
110
111}