github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/client/system_test.go (about)

     1  // Copyright 2020 DataStax
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package client_test
    16  
    17  import (
    18  	"bytes"
    19  	"github.com/datastax/go-cassandra-native-protocol/client"
    20  	"github.com/datastax/go-cassandra-native-protocol/frame"
    21  	"github.com/datastax/go-cassandra-native-protocol/message"
    22  	"github.com/datastax/go-cassandra-native-protocol/primitive"
    23  	"github.com/stretchr/testify/require"
    24  	"testing"
    25  )
    26  
    27  func TestNewSystemTablesHandler_FullSystemLocal(t *testing.T) {
    28  
    29  	handler := client.NewSystemTablesHandler("cluster_test", "datacenter_test")
    30  	server, clientConn, cancelFunc := createServerAndClient(t, []client.RequestHandler{handler}, nil)
    31  
    32  	testFullSystemLocal(t, clientConn)
    33  
    34  	cancelFunc()
    35  	checkClosed(t, clientConn, server)
    36  
    37  }
    38  
    39  func TestNewSystemTablesHandler_SchemaVersion(t *testing.T) {
    40  
    41  	handler := client.NewSystemTablesHandler("cluster_test", "datacenter_test")
    42  	server, clientConn, cancelFunc := createServerAndClient(t, []client.RequestHandler{handler}, nil)
    43  
    44  	testSchemaVersion(t, clientConn)
    45  
    46  	cancelFunc()
    47  	checkClosed(t, clientConn, server)
    48  }
    49  
    50  func TestNewSystemTablesHandler_ClusterName(t *testing.T) {
    51  
    52  	handler := client.NewSystemTablesHandler("cluster_test", "datacenter_test")
    53  	server, clientConn, cancelFunc := createServerAndClient(t, []client.RequestHandler{handler}, nil)
    54  
    55  	testClusterName(t, clientConn)
    56  
    57  	cancelFunc()
    58  	checkClosed(t, clientConn, server)
    59  }
    60  
    61  func TestNewSystemTablesHandler_EmptySystemPeers(t *testing.T) {
    62  
    63  	handler := client.NewSystemTablesHandler("cluster_test", "datacenter_test")
    64  	server, clientConn, cancelFunc := createServerAndClient(t, []client.RequestHandler{handler}, nil )
    65  
    66  	testSystemPeers(t, clientConn)
    67  
    68  	cancelFunc()
    69  	checkClosed(t, clientConn, server)
    70  }
    71  
    72  func testFullSystemLocal(t *testing.T, clientConn *client.CqlClientConnection) {
    73  	fullSystemLocalQuery := frame.NewFrame(
    74  		primitive.ProtocolVersion4,
    75  		client.ManagedStreamId,
    76  		&message.Query{
    77  			Query: " SELECT  * \n FROM  \t system.local WHERE key = 'local'",
    78  		},
    79  	)
    80  
    81  	response, err := clientConn.SendAndReceive(fullSystemLocalQuery)
    82  	require.NotNil(t, response)
    83  	require.NoError(t, err)
    84  	require.Equal(t, primitive.OpCodeResult, response.Header.OpCode)
    85  	require.IsType(t, &message.RowsResult{}, response.Body.Message)
    86  
    87  	rowsResult := response.Body.Message.(*message.RowsResult)
    88  	require.EqualValues(t, rowsResult.Metadata.ColumnCount, 13)
    89  	require.Len(t, rowsResult.Metadata.Columns, 13)
    90  	require.Len(t, rowsResult.Data, 1)
    91  	require.Len(t, rowsResult.Data[0], 13)
    92  }
    93  
    94  func testSchemaVersion(t *testing.T, clientConn *client.CqlClientConnection) {
    95  	schemaVersionQuery := frame.NewFrame(
    96  		primitive.ProtocolVersion4,
    97  		client.ManagedStreamId,
    98  		&message.Query{
    99  			Query: " SELECT  schema_version \n FROM  \t system.local WHERE key = 'local'",
   100  		},
   101  	)
   102  
   103  	response, err := clientConn.SendAndReceive(schemaVersionQuery)
   104  	require.NotNil(t, response)
   105  	require.NoError(t, err)
   106  	require.Equal(t, primitive.OpCodeResult, response.Header.OpCode)
   107  	require.IsType(t, &message.RowsResult{}, response.Body.Message)
   108  
   109  	rowsResult := response.Body.Message.(*message.RowsResult)
   110  	require.Len(t, rowsResult.Data, 1)
   111  	require.Len(t, rowsResult.Data[0], 1)
   112  
   113  	column := rowsResult.Data[0][0]
   114  	_, err = primitive.ReadUuid(bytes.NewBuffer(column))
   115  	require.NoError(t, err)
   116  }
   117  
   118  func testClusterName(t *testing.T, clientConn *client.CqlClientConnection) {
   119  	clusterNameQuery := frame.NewFrame(
   120  		primitive.ProtocolVersion4,
   121  		client.ManagedStreamId,
   122  		&message.Query{
   123  			Query: " SELECT   cluster_name \n FROM  \t system.local",
   124  		},
   125  	)
   126  
   127  	response, err := clientConn.SendAndReceive(clusterNameQuery)
   128  	require.NotNil(t, response)
   129  	require.NoError(t, err)
   130  	require.Equal(t, primitive.OpCodeResult, response.Header.OpCode)
   131  	require.IsType(t, &message.RowsResult{}, response.Body.Message)
   132  
   133  	rowsResult := response.Body.Message.(*message.RowsResult)
   134  	require.Len(t, rowsResult.Data, 1)
   135  	require.Len(t, rowsResult.Data[0], 1)
   136  
   137  	column := rowsResult.Data[0][0]
   138  	require.Equal(t, "cluster_test", string(column))
   139  }
   140  
   141  func testSystemPeers(t *testing.T, clientConn *client.CqlClientConnection) {
   142  	systemPeersQuery := frame.NewFrame(
   143  		primitive.ProtocolVersion4,
   144  		client.ManagedStreamId,
   145  		&message.Query{
   146  			Query: " SELECT  * \n FROM  \t system.peers",
   147  		},
   148  	)
   149  
   150  	response, err := clientConn.SendAndReceive(systemPeersQuery)
   151  	require.NotNil(t, response)
   152  	require.NoError(t, err)
   153  	require.Equal(t, primitive.OpCodeResult, response.Header.OpCode)
   154  	require.IsType(t, &message.RowsResult{}, response.Body.Message)
   155  
   156  	rowsResult := response.Body.Message.(*message.RowsResult)
   157  	require.EqualValues(t, rowsResult.Metadata.ColumnCount, 0)
   158  	require.Nil(t, rowsResult.Metadata.Columns)
   159  	require.Len(t, rowsResult.Data, 0)
   160  }