vitess.io/vitess@v0.16.2/go/test/endtoend/vtgate/schematracker/unauthorized/unauthorized_test.go (about)

     1  /*
     2  Copyright 2022 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  
    17  package unauthorized
    18  
    19  import (
    20  	"context"
    21  	_ "embed"
    22  	"flag"
    23  	"os"
    24  	"path"
    25  	"strings"
    26  	"testing"
    27  	"time"
    28  
    29  	"github.com/stretchr/testify/require"
    30  
    31  	"vitess.io/vitess/go/mysql"
    32  	"vitess.io/vitess/go/test/endtoend/cluster"
    33  )
    34  
    35  var (
    36  	clusterInstance *cluster.LocalProcessCluster
    37  	vtParams        mysql.ConnParams
    38  	KeyspaceName    = "ks"
    39  	Cell            = "test"
    40  
    41  	//go:embed schema.sql
    42  	SchemaSQL string
    43  
    44  	//go:embed vschema.json
    45  	VSchema string
    46  )
    47  
    48  func TestMain(m *testing.M) {
    49  	defer cluster.PanicHandler(nil)
    50  	flag.Parse()
    51  
    52  	exitCode := func() int {
    53  		clusterInstance = cluster.NewCluster(Cell, "localhost")
    54  		defer clusterInstance.Teardown()
    55  
    56  		// Start topo server
    57  		err := clusterInstance.StartTopo()
    58  		if err != nil {
    59  			return 1
    60  		}
    61  
    62  		// Start keyspace
    63  		keyspace := &cluster.Keyspace{
    64  			Name:      KeyspaceName,
    65  			SchemaSQL: SchemaSQL,
    66  			VSchema:   VSchema,
    67  		}
    68  		clusterInstance.VtGateExtraArgs = []string{"--schema_change_signal"}
    69  		clusterInstance.VtTabletExtraArgs = []string{"--queryserver-config-schema-change-signal", "--queryserver-config-schema-change-signal-interval", "0.1", "--queryserver-config-strict-table-acl", "--queryserver-config-acl-exempt-acl", "userData1", "--table-acl-config", "dummy.json"}
    70  		err = clusterInstance.StartKeyspace(*keyspace, []string{"-80", "80-"}, 0, false)
    71  		if err != nil {
    72  			return 1
    73  		}
    74  
    75  		// Start vtgate
    76  		err = clusterInstance.StartVtgate()
    77  		if err != nil {
    78  			return 1
    79  		}
    80  
    81  		vtParams = mysql.ConnParams{
    82  			Host: clusterInstance.Hostname,
    83  			Port: clusterInstance.VtgateMySQLPort,
    84  		}
    85  		return m.Run()
    86  	}()
    87  	os.Exit(exitCode)
    88  }
    89  
    90  func TestSchemaTrackingError(t *testing.T) {
    91  	ctx := context.Background()
    92  	conn, err := mysql.Connect(ctx, &vtParams)
    93  	require.NoError(t, err)
    94  	defer conn.Close()
    95  
    96  	logDir := clusterInstance.VtgateProcess.LogDir
    97  
    98  	timeout := time.After(1 * time.Minute)
    99  	var present bool
   100  	for {
   101  		select {
   102  		case <-timeout:
   103  			t.Error("timeout waiting for schema tracking error")
   104  		case <-time.After(1 * time.Second):
   105  			// check info logs, continue if the file could not be read correctly.
   106  			all, err := os.ReadFile(path.Join(logDir, "vtgate.WARNING"))
   107  			if err != nil {
   108  				continue
   109  			}
   110  			if strings.Contains(string(all), "Table ACL might be enabled, --schema_change_signal_user needs to be passed to VTGate for schema tracking to work. Check 'schema tracking' docs on vitess.io") {
   111  				present = true
   112  			}
   113  		}
   114  		if present {
   115  			break
   116  		}
   117  	}
   118  }