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.thrift2; 019 020import static org.junit.jupiter.api.Assertions.assertFalse; 021import static org.junit.jupiter.api.Assertions.assertTrue; 022 023import java.net.InetAddress; 024import java.util.ArrayList; 025import java.util.function.Supplier; 026import java.util.stream.Stream; 027import org.apache.hadoop.hbase.HBaseParameterizedTestTemplate; 028import org.apache.hadoop.hbase.testclassification.ClientTests; 029import org.apache.hadoop.hbase.testclassification.LargeTests; 030import org.apache.hadoop.hbase.thrift.ImplType; 031import org.apache.hadoop.hbase.thrift.TestThriftServerCmdLine; 032import org.apache.hadoop.hbase.thrift.ThriftServer; 033import org.apache.hadoop.hbase.thrift2.generated.TColumnFamilyDescriptor; 034import org.apache.hadoop.hbase.thrift2.generated.THBaseService; 035import org.apache.hadoop.hbase.thrift2.generated.TTableDescriptor; 036import org.apache.hadoop.hbase.thrift2.generated.TTableName; 037import org.apache.hadoop.hbase.util.Bytes; 038import org.junit.jupiter.api.Tag; 039import org.junit.jupiter.params.provider.Arguments; 040 041import org.apache.hbase.thirdparty.org.apache.thrift.protocol.TBinaryProtocol; 042import org.apache.hbase.thirdparty.org.apache.thrift.protocol.TCompactProtocol; 043import org.apache.hbase.thirdparty.org.apache.thrift.protocol.TProtocol; 044import org.apache.hbase.thirdparty.org.apache.thrift.transport.TSocket; 045import org.apache.hbase.thirdparty.org.apache.thrift.transport.TTransport; 046import org.apache.hbase.thirdparty.org.apache.thrift.transport.layered.TFramedTransport; 047 048@Tag(ClientTests.TAG) 049@Tag(LargeTests.TAG) 050@HBaseParameterizedTestTemplate 051public class TestThrift2ServerCmdLine extends TestThriftServerCmdLine { 052 053 private static final String TABLENAME = "TestThrift2ServerCmdLineTable"; 054 055 public static Stream<Arguments> parameters() { 056 return TestThriftServerCmdLine.parameters(); 057 } 058 059 public TestThrift2ServerCmdLine(ImplType implType, boolean specifyFramed, boolean specifyBindIP, 060 boolean specifyCompact) { 061 super(implType, specifyFramed, specifyBindIP, specifyCompact); 062 } 063 064 @Override 065 protected Supplier<ThriftServer> getThriftServerSupplier() { 066 return () -> new org.apache.hadoop.hbase.thrift2.ThriftServer(TEST_UTIL.getConfiguration()); 067 } 068 069 @Override 070 protected void talkToThriftServer(int port) throws Exception { 071 TSocket sock = new TSocket(InetAddress.getLoopbackAddress().getHostName(), port); 072 TTransport transport = sock; 073 if (specifyFramed || implType.isAlwaysFramed()) { 074 transport = new TFramedTransport(transport); 075 } 076 077 sock.open(); 078 try { 079 TProtocol tProtocol; 080 if (specifyCompact) { 081 tProtocol = new TCompactProtocol(transport); 082 } else { 083 tProtocol = new TBinaryProtocol(transport); 084 } 085 THBaseService.Client client = new THBaseService.Client(tProtocol); 086 TTableName tTableName = new TTableName(); 087 tTableName.setNs(Bytes.toBytes("")); 088 tTableName.setQualifier(Bytes.toBytes(TABLENAME)); 089 if (!tableCreated) { 090 assertFalse(client.tableExists(tTableName)); 091 TTableDescriptor tTableDescriptor = new TTableDescriptor(); 092 tTableDescriptor.setTableName(tTableName); 093 TColumnFamilyDescriptor columnFamilyDescriptor = new TColumnFamilyDescriptor(); 094 columnFamilyDescriptor.setName(Bytes.toBytes(TABLENAME)); 095 tTableDescriptor.addToColumns(columnFamilyDescriptor); 096 client.createTable(tTableDescriptor, new ArrayList<>()); 097 tableCreated = true; 098 } 099 assertTrue(client.tableExists(tTableName), "tableCreated " + tableCreated); 100 } finally { 101 sock.close(); 102 } 103 } 104}