github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/server/server_sql_test.go (about) 1 // Copyright 2020 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package server 12 13 import ( 14 "context" 15 "testing" 16 17 "github.com/cockroachdb/cockroach/pkg/base" 18 "github.com/cockroachdb/cockroach/pkg/roachpb" 19 "github.com/cockroachdb/cockroach/pkg/sql/pgwire/pgcode" 20 "github.com/cockroachdb/cockroach/pkg/testutils/serverutils" 21 "github.com/cockroachdb/cockroach/pkg/testutils/sqlutils" 22 "github.com/cockroachdb/cockroach/pkg/util/leaktest" 23 "github.com/cockroachdb/errors" 24 "github.com/lib/pq" 25 "github.com/stretchr/testify/require" 26 ) 27 28 // TestSQLServer starts up a semi-dedicated SQL server and runs some smoke test 29 // queries. The SQL server shares some components, notably Gossip, with a test 30 // server serving as a KV backend. 31 // 32 // TODO(tbg): start narrowing down and enumerating the unwanted dependencies. In 33 // the end, the SQL server in this test should not depend on a Gossip instance 34 // and must not rely on having a NodeID/NodeDescriptor/NodeLiveness/... 35 // 36 // In short, it should not rely on the test server through anything other than a 37 // `*kv.DB` and a small number of whitelisted RPCs. 38 func TestSQLServer(t *testing.T) { 39 defer leaktest.AfterTest(t)() 40 ctx := context.Background() 41 42 tc := serverutils.StartTestCluster(t, 1, base.TestClusterArgs{}) 43 defer tc.Stopper().Stop(ctx) 44 45 db := serverutils.StartTenant(t, tc.Server(0), base.TestTenantArgs{TenantID: roachpb.MakeTenantID(10)}) 46 defer db.Close() 47 r := sqlutils.MakeSQLRunner(db) 48 r.QueryStr(t, `SELECT 1`) 49 r.Exec(t, `CREATE DATABASE foo`) 50 r.Exec(t, `CREATE TABLE foo.kv (k STRING PRIMARY KEY, v STRING)`) 51 r.Exec(t, `INSERT INTO foo.kv VALUES('foo', 'bar')`) 52 t.Log(sqlutils.MatrixToStr(r.QueryStr(t, `SET distsql=off; SELECT * FROM foo.kv`))) 53 t.Log(sqlutils.MatrixToStr(r.QueryStr(t, `SET distsql=auto; SELECT * FROM foo.kv`))) 54 } 55 56 func TestTenantCannotSetClusterSetting(t *testing.T) { 57 defer leaktest.AfterTest(t)() 58 ctx := context.Background() 59 60 tc := serverutils.StartTestCluster(t, 1, base.TestClusterArgs{}) 61 defer tc.Stopper().Stop(ctx) 62 63 // StartTenant with the default permissions to 64 db := serverutils.StartTenant(t, tc.Server(0), base.TestTenantArgs{TenantID: roachpb.MakeTenantID(10), AllowSettingClusterSettings: false}) 65 defer db.Close() 66 _, err := db.Exec(`SET CLUSTER SETTING sql.defaults.vectorize=off`) 67 var pqErr *pq.Error 68 ok := errors.As(err, &pqErr) 69 require.True(t, ok, "expected err to be a *pq.Error but is of type %T. error is: %v", err) 70 require.Equal(t, pq.ErrorCode(pgcode.InsufficientPrivilege), pqErr.Code, "err %v has unexpected code", err) 71 }