vitess.io/vitess@v0.16.2/go/test/endtoend/clustertest/vtgate_test.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  
    16  This tests select/insert using the unshared keyspace added in main_test
    17  */
    18  package clustertest
    19  
    20  import (
    21  	"context"
    22  	"encoding/json"
    23  	"fmt"
    24  	"io"
    25  	"net/http"
    26  	"reflect"
    27  	"strings"
    28  	"testing"
    29  
    30  	"vitess.io/vitess/go/test/endtoend/utils"
    31  
    32  	"github.com/stretchr/testify/require"
    33  
    34  	"vitess.io/vitess/go/mysql"
    35  	"vitess.io/vitess/go/test/endtoend/cluster"
    36  )
    37  
    38  func TestVtgateProcess(t *testing.T) {
    39  	defer cluster.PanicHandler(t)
    40  	verifyVtgateVariables(t, clusterInstance.VtgateProcess.VerifyURL)
    41  	ctx := context.Background()
    42  	conn, err := mysql.Connect(ctx, &vtParams)
    43  	require.NoError(t, err)
    44  	defer conn.Close()
    45  
    46  	utils.Exec(t, conn, "insert into customer(id, email) values(1,'email1')")
    47  	_ = utils.Exec(t, conn, "begin")
    48  	qr := utils.Exec(t, conn, "select id, email from customer")
    49  	if got, want := fmt.Sprintf("%v", qr.Rows), `[[INT64(1) VARCHAR("email1")]]`; got != want {
    50  		t.Errorf("select:\n%v want\n%v", got, want)
    51  	}
    52  }
    53  
    54  func verifyVtgateVariables(t *testing.T, url string) {
    55  	resp, err := http.Get(url)
    56  	require.NoError(t, err)
    57  	defer resp.Body.Close()
    58  
    59  	require.Equal(t, 200, resp.StatusCode)
    60  	resultMap := make(map[string]any)
    61  	respByte, err := io.ReadAll(resp.Body)
    62  	require.NoError(t, err)
    63  	err = json.Unmarshal(respByte, &resultMap)
    64  	require.NoError(t, err)
    65  	if resultMap["VtgateVSchemaCounts"] == nil {
    66  		t.Error("Vschema count should be present in variables")
    67  	}
    68  	vschemaCountMap := getMapFromJSON(resultMap, "VtgateVSchemaCounts")
    69  	if _, present := vschemaCountMap["Reload"]; !present {
    70  		t.Error("Reload count should be present in vschemacount")
    71  	} else if object := reflect.ValueOf(vschemaCountMap["Reload"]); object.NumField() <= 0 {
    72  		t.Error("Reload count should be greater than 0")
    73  	}
    74  	if _, present := vschemaCountMap["WatchError"]; present {
    75  		t.Error("There should not be any WatchError in VschemaCount")
    76  	}
    77  	if _, present := vschemaCountMap["Parsing"]; present {
    78  		t.Error("There should not be any Parsing in VschemaCount")
    79  	}
    80  
    81  	if resultMap["HealthcheckConnections"] == nil {
    82  		t.Error("HealthcheckConnections count should be present in variables")
    83  	}
    84  
    85  	healthCheckConnection := getMapFromJSON(resultMap, "HealthcheckConnections")
    86  	if len(healthCheckConnection) <= 0 {
    87  		t.Error("Atleast one healthy tablet needs to be present")
    88  	}
    89  	if !isPrimaryTabletPresent(healthCheckConnection) {
    90  		t.Error("Atleast one PRIMARY tablet needs to be present")
    91  	}
    92  }
    93  
    94  func getMapFromJSON(JSON map[string]any, key string) map[string]any {
    95  	result := make(map[string]any)
    96  	object := reflect.ValueOf(JSON[key])
    97  	if object.Kind() == reflect.Map {
    98  		for _, key := range object.MapKeys() {
    99  			value := object.MapIndex(key)
   100  			result[key.String()] = value
   101  		}
   102  	}
   103  	return result
   104  }
   105  
   106  func isPrimaryTabletPresent(tablets map[string]any) bool {
   107  	for key := range tablets {
   108  		if strings.Contains(key, "primary") {
   109  			return true
   110  		}
   111  	}
   112  	return false
   113  }