diff --git hbase-server/src/test/java/org/apache/hadoop/hbase/rest/client/TestAdminRestErrors.java hbase-server/src/test/java/org/apache/hadoop/hbase/rest/client/TestAdminRestErrors.java new file mode 100644 index 0000000..1179ec5 --- /dev/null +++ hbase-server/src/test/java/org/apache/hadoop/hbase/rest/client/TestAdminRestErrors.java @@ -0,0 +1,169 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with the License. You may + * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by + * applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the + * License. + */ +package org.apache.hadoop.hbase.rest.client; + +import java.io.IOException; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.HTableDescriptor; +import org.apache.hadoop.hbase.MediumTests; +import org.apache.hadoop.hbase.util.Bytes; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import static org.mockito.Mockito.*; +import static org.junit.Assert.*; + +/** + * test a reaction of rest client on server error. client should try some times with timeout. + */ +@Category(MediumTests.class) +public class TestAdminRestErrors { + + private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); + private static RemoteAdmin remoteAdmin; + + private long maxTime = 1500; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + Client fakeClient = mock(Client.class); + Response response = new Response(509); + when(fakeClient.get(anyString(), anyString())).thenReturn(response); + when(fakeClient.delete(anyString())).thenReturn(response); + byte[] aByte = new byte[0]; + when(fakeClient.put(anyString(), anyString(), any(aByte.getClass()))).thenReturn(response); + when(fakeClient.post(anyString(), anyString(), any(aByte.getClass()))).thenReturn(response); + Configuration configuration = TEST_UTIL.getConfiguration(); + + configuration.setInt("hbase.rest.client.max.retries", 3); + configuration.setInt("hbase.rest.client.sleep", 600); + + remoteAdmin = new RemoteAdmin(fakeClient, TEST_UTIL.getConfiguration(), "MyTable"); + } + + /** + * test function RestVersion + */ + @Test + public void testTimeoutExceptionRestVersion() { + long start = System.currentTimeMillis(); + try { + remoteAdmin.getRestVersion(); + fail("should be timeout exception!"); + } catch (IOException e) { + assertEquals("java.io.IOException: get request to /MyTable/version/rest timed out", + e.toString()); + } + assertTrue((System.currentTimeMillis() - start) > maxTime); + } + + /** + * test function getClusterStatus + */ + @Test + public void testTimeoutExceptiongetClusterStatus() { + long start = System.currentTimeMillis(); + try { + remoteAdmin.getClusterStatus(); + fail("should be timeout exception!"); + } catch (IOException e) { + assertEquals("java.io.IOException: get request to /MyTable/status/cluster timed out", + e.toString()); + } + assertTrue((System.currentTimeMillis() - start) > maxTime); + } + + /** + * test function getClusterVersion + */ + @Test + public void testTimeoutExceptiongetClusterVersion() { + long start = System.currentTimeMillis(); + try { + remoteAdmin.getClusterVersion(); + fail("should be timeout exception!"); + } catch (IOException e) { + assertEquals( + "java.io.IOException: get request to /MyTable/version/cluster request timed out", + e.toString()); + } + assertTrue((System.currentTimeMillis() - start) > maxTime); + } + + /** + * test function isTableAvailable + */ + @Test + public void testTimeoutExceptiongetTableAvailable() { + long start = System.currentTimeMillis(); + try { + remoteAdmin.isTableAvailable(Bytes.toBytes("TestTable")); + fail("should be timeout exception!"); + } catch (IOException e) { + assertEquals("java.io.IOException: get request to /MyTable/TestTable/exists timed out", + e.toString()); + } + assertTrue((System.currentTimeMillis() - start) > maxTime); + } + + /** + * test function createTable + */ + @Test + public void testTimeoutExceptiongetCreateTable() { + long start = System.currentTimeMillis(); + try { + remoteAdmin.createTable(new HTableDescriptor(Bytes.toBytes("TestTable"))); + fail("should be timeout exception!"); + } catch (IOException e) { + assertEquals("java.io.IOException: create request to /MyTable/TestTable/schema timed out", + e.toString()); + } + assertTrue((System.currentTimeMillis() - start) > maxTime); + } + + /** + * test function deleteTable + */ + @Test + public void testTimeoutExceptiongetDeleteTable() { + long start = System.currentTimeMillis(); + try { + remoteAdmin.deleteTable("TestTable"); + fail("should be timeout exception!"); + } catch (IOException e) { + assertEquals("java.io.IOException: delete request to /MyTable/TestTable/schema timed out", + e.toString()); + } + assertTrue((System.currentTimeMillis() - start) > maxTime); + } + + /** + * test function getTableList + */ + @Test + public void testTimeoutExceptiongetTetTableList() { + long start = System.currentTimeMillis(); + try { + remoteAdmin.getTableList(); + fail("should be timeout exception!"); + } catch (IOException e) { + assertEquals("java.io.IOException: get request to /MyTable/ request timed out", + e.toString()); + } + assertTrue((System.currentTimeMillis() - start) > maxTime); + } + +} diff --git hbase-server/src/test/java/org/apache/hadoop/hbase/rest/client/TestRemoteTable.java hbase-server/src/test/java/org/apache/hadoop/hbase/rest/client/TestRemoteTable.java index a47a24a..0c899cd 100644 --- hbase-server/src/test/java/org/apache/hadoop/hbase/rest/client/TestRemoteTable.java +++ hbase-server/src/test/java/org/apache/hadoop/hbase/rest/client/TestRemoteTable.java @@ -21,8 +21,11 @@ package org.apache.hadoop.hbase.rest.client; import java.io.IOException; import java.util.ArrayList; +import java.util.Collections; +import java.util.Iterator; import java.util.List; +import org.apache.commons.httpclient.Header; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hadoop.hbase.*; @@ -92,6 +95,7 @@ public class TestRemoteTable { put.add(COLUMN_2, QUALIFIER_2, TS_2, VALUE_2); table.put(put); table.flushCommits(); + table.close(); } remoteTable = new RemoteHTable( new Client(new Cluster().add("localhost", @@ -236,8 +240,8 @@ public class TestRemoteTable { results = remoteTable.get(gets); assertNotNull(results); assertEquals(2, results.length); - assertEquals(1, results[0].size()); - assertEquals(3, results[1].size()); + assertEquals(2, results[0].size()); + assertEquals(4, results[1].size()); //404 gets = new ArrayList(); @@ -254,7 +258,10 @@ public class TestRemoteTable { assertNotNull(results); assertEquals(0, results.length); } - + + /** + * Test multi put + */ @Test public void testPut() throws IOException { Put put = new Put(ROW_3); @@ -296,8 +303,13 @@ public class TestRemoteTable { value = result.getValue(COLUMN_2, QUALIFIER_2); assertNotNull(value); assertTrue(Bytes.equals(VALUE_2, value)); + + assertTrue(Bytes.equals(Bytes.toBytes("TestRemoteTable"), remoteTable.getTableName())); } - + /** + * Test function delete + */ + @Test public void testDelete() throws IOException { Put put = new Put(ROW_3); put.add(COLUMN_1, QUALIFIER_1, VALUE_1); @@ -341,7 +353,10 @@ public class TestRemoteTable { assertNull(value1); assertNull(value2); } - + /** + * Test a inner class Scanner + */ + @Test public void testScanner() throws IOException { List puts = new ArrayList(); Put put = new Put(ROW_1); @@ -365,18 +380,128 @@ public class TestRemoteTable { assertEquals(1, results.length); assertTrue(Bytes.equals(ROW_1, results[0].getRow())); - results = scanner.next(3); + Result result = scanner.next(); + assertNotNull(result); + assertTrue(Bytes.equals(ROW_2, result.getRow())); + + results = scanner.next(2); assertNotNull(results); - assertEquals(3, results.length); - assertTrue(Bytes.equals(ROW_2, results[0].getRow())); - assertTrue(Bytes.equals(ROW_3, results[1].getRow())); - assertTrue(Bytes.equals(ROW_4, results[2].getRow())); + assertEquals(2, results.length); + assertTrue(Bytes.equals(ROW_3, results[0].getRow())); + assertTrue(Bytes.equals(ROW_4, results[1].getRow())); results = scanner.next(1); assertNull(results); + scanner.close(); + + scanner = remoteTable.getScanner(COLUMN_1); + results = scanner.next(4); + assertNotNull(results); + assertEquals(4, results.length); + assertTrue(Bytes.equals(ROW_1, results[0].getRow())); + assertTrue(Bytes.equals(ROW_2, results[1].getRow())); + assertTrue(Bytes.equals(ROW_3, results[2].getRow())); + assertTrue(Bytes.equals(ROW_4, results[3].getRow())); scanner.close(); + + scanner = remoteTable.getScanner(COLUMN_1,QUALIFIER_1); + results = scanner.next(4); + assertNotNull(results); + assertEquals(4, results.length); + assertTrue(Bytes.equals(ROW_1, results[0].getRow())); + assertTrue(Bytes.equals(ROW_2, results[1].getRow())); + assertTrue(Bytes.equals(ROW_3, results[2].getRow())); + assertTrue(Bytes.equals(ROW_4, results[3].getRow())); + scanner.close(); + assertTrue(remoteTable.isAutoFlush()); + } + + /** + * Test a method checkAndDelete + */ + @Test + public void testExist() throws IOException { + Get get = new Get(ROW_1); + Result result = remoteTable.get(get); + byte[] value1 = result.getValue(COLUMN_1, QUALIFIER_1); + byte[] value2 = result.getValue(COLUMN_2, QUALIFIER_2); + assertNotNull(value1); + assertTrue(Bytes.equals(VALUE_1, value1)); + assertNull(value2); + assertTrue(remoteTable.exists(get)); + assertEquals(1,remoteTable.exists(Collections.singletonList(get)).length); + Delete delete=new Delete(ROW_1); + + remoteTable.checkAndDelete(ROW_1,COLUMN_1,QUALIFIER_1,VALUE_1,delete); + assertFalse(remoteTable.exists(get)); + + Put put = new Put(ROW_1); + put.add(COLUMN_1, QUALIFIER_1, VALUE_1); + remoteTable.put(put); + + assertTrue(remoteTable.checkAndPut(ROW_1, COLUMN_1, QUALIFIER_1, VALUE_1, put)); + assertFalse(remoteTable.checkAndPut(ROW_1, COLUMN_1, QUALIFIER_1, VALUE_2, put)); + } + + /** + * Test a method next from class Scanner + */ + @Test + public void testIteratorScaner() throws IOException { + List puts = new ArrayList(); + Put put = new Put(ROW_1); + put.add(COLUMN_1, QUALIFIER_1, VALUE_1); + puts.add(put); + put = new Put(ROW_2); + put.add(COLUMN_1, QUALIFIER_1, VALUE_1); + puts.add(put); + put = new Put(ROW_3); + put.add(COLUMN_1, QUALIFIER_1, VALUE_1); + puts.add(put); + put = new Put(ROW_4); + put.add(COLUMN_1, QUALIFIER_1, VALUE_1); + puts.add(put); + remoteTable.put(puts); + + ResultScanner scanner = remoteTable.getScanner(new Scan()); + Iterator iterator=scanner.iterator(); + assertTrue(iterator.hasNext()); + int counter=0; + while(iterator.hasNext()){ + iterator.next(); + counter++; + } + assertEquals(4, counter); + } + + /** + * Test a some methods of class Responce. + */ + @Test + public void testResponse(){ + Response responce= new Response(200); + assertEquals(200, responce.getCode()); + Header [] headers= new Header[2]; + headers[0]= new Header("header1","value1"); + headers[1]= new Header("header2","value2"); + responce= new Response(200,headers); + assertEquals("value1", responce.getHeader("header1")); + assertFalse(responce.hasBody()); + responce.setCode(404); + assertEquals(404, responce.getCode()); + headers= new Header[2]; + headers[0]= new Header("header1","value1.1"); + headers[1]= new Header("header2","value2"); + responce.setHeaders(headers); + assertEquals("value1.1", responce.getHeader("header1")); + responce.setBody(Bytes.toBytes("body")); + assertTrue(responce.hasBody()); + + + } + } diff --git hbase-server/src/test/java/org/apache/hadoop/hbase/rest/client/TestRestErrors.java hbase-server/src/test/java/org/apache/hadoop/hbase/rest/client/TestRestErrors.java new file mode 100644 index 0000000..3312182 --- /dev/null +++ hbase-server/src/test/java/org/apache/hadoop/hbase/rest/client/TestRestErrors.java @@ -0,0 +1,179 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with the License. You may + * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by + * applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and limitations under the + * License. + */ +package org.apache.hadoop.hbase.rest.client; + +import java.io.IOException; +import java.util.Arrays; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hbase.HBaseTestingUtility; +import org.apache.hadoop.hbase.MediumTests; +import org.apache.hadoop.hbase.client.Delete; +import org.apache.hadoop.hbase.client.Get; +import org.apache.hadoop.hbase.client.Put; +import org.apache.hadoop.hbase.client.Scan; +import org.apache.hadoop.hbase.util.Bytes; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.experimental.categories.Category; + + + +import static org.mockito.Mockito.*; +import static org.junit.Assert.*; + +/** + * Test a reaction of rest client on server error. client should try some times with timeout. + */ +@Category(MediumTests.class) +public class TestRestErrors { + + private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility(); + private static RemoteHTable remoteTable; + + private static final byte[] ROW_1 = Bytes.toBytes("testrow1"); + private static final byte[] COLUMN_1 = Bytes.toBytes("a"); + private static final byte[] QUALIFIER_1 = Bytes.toBytes("1"); + private static final byte[] VALUE_1 = Bytes.toBytes("testvalue1"); + private long maxTime = 1500; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + Client fakeClient = mock(Client.class); + Response response = new Response(509); + when(fakeClient.get(anyString(),anyString())).thenReturn(response); + when(fakeClient.delete(anyString())).thenReturn(response); + byte[] aByte= new byte[0]; + when(fakeClient.put(anyString(),anyString(),any(aByte.getClass()))).thenReturn(response); + when(fakeClient.post(anyString(),anyString(),any(aByte.getClass()))).thenReturn(response); + Configuration configuration = TEST_UTIL.getConfiguration(); + + configuration.setInt("hbase.rest.client.max.retries", 3); + configuration.setInt("hbase.rest.client.sleep", 600); + + + remoteTable = new RemoteHTable(fakeClient, TEST_UTIL.getConfiguration(), "MyTable"); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + remoteTable.close(); + } + + /** + * test function delete + */ + @Test + public void testTimeoutExceptionDelete() { + Delete delete= new Delete(Bytes.toBytes("delete")); + long start= System.currentTimeMillis(); + try{ + remoteTable.delete(delete); + fail("should be timeout exception!"); + }catch(IOException e){ + assertEquals("java.io.IOException: delete request timed out", e.toString()); + } + assertTrue((System.currentTimeMillis()-start)>maxTime); + } + + /** + * test function get + */ + @Test + public void testTimeoutExceptionGet() { + long start= System.currentTimeMillis(); + try{ + remoteTable.get(new Get(Bytes.toBytes("Get"))); + fail("should be timeout exception!"); + }catch(IOException e){ + assertEquals("java.io.IOException: get request timed out", e.toString()); + } + assertTrue((System.currentTimeMillis()-start)>maxTime); + } + + /** + * test function put + */ + @Test + public void testTimeoutExceptionPut() { + long start= System.currentTimeMillis(); + try{ + remoteTable.put(new Put(Bytes.toBytes("Row"))); + fail("should be timeout exception!"); + }catch(IOException e){ + assertEquals("java.io.IOException: put request timed out", e.toString()); + } + assertTrue((System.currentTimeMillis()-start)>maxTime); + start= System.currentTimeMillis(); + Put[] puts= {new Put(Bytes.toBytes("Row1")),new Put(Bytes.toBytes("Row2"))}; + try{ + remoteTable.put(Arrays.asList(puts)); + fail("should be timeout exception!"); + }catch(IOException e){ + assertEquals("java.io.IOException: multiput request timed out", e.toString()); + } + assertTrue((System.currentTimeMillis()-start)>maxTime); + } + + /** + * test Scanner + */ + @Test + public void testTimeoutExceptionScanner() { + long start= System.currentTimeMillis(); + try{ + remoteTable.getScanner(new Scan()); + }catch(IOException e){ + assertEquals("java.io.IOException: scan request timed out", e.toString()); + } + assertTrue((System.currentTimeMillis()-start)>maxTime); + start= System.currentTimeMillis(); + } + + /** + * test function checkAndPut + */ + @Test + public void testTimeoutExceptionCheckAndPut() { + long start= System.currentTimeMillis(); + Put put = new Put(ROW_1); + put.add(COLUMN_1, QUALIFIER_1, VALUE_1); + + try{ + remoteTable.checkAndPut(ROW_1, COLUMN_1, QUALIFIER_1, VALUE_1, put ); + fail("should be timeout exception!"); + }catch(IOException e){ + assertEquals("java.io.IOException: checkAndPut request timed out", e.toString()); + } + assertTrue((System.currentTimeMillis()-start)>maxTime); + } + + /** + * test function checkAndDelete + */ + @Test + public void testTimeoutExceptionCheckAndDelete() { + long start= System.currentTimeMillis(); + Put put = new Put(ROW_1); + put.add(COLUMN_1, QUALIFIER_1, VALUE_1); + Delete delete= new Delete(ROW_1); + + try{ + remoteTable.checkAndDelete(ROW_1, COLUMN_1, QUALIFIER_1, VALUE_1, delete ); + fail("should be timeout exception!"); + }catch(IOException e){ + assertEquals("java.io.IOException: checkAndDelete request timed out", e.toString()); + } + assertTrue((System.currentTimeMillis()-start)>maxTime); + } +}