Skip to content

Commit

Permalink
instancestorage: Add SQL test for sql_instances.
Browse files Browse the repository at this point in the history
This PR adds a unit test to verify that the sql_instances
table can be accessed through SQL API.

Release note: None
  • Loading branch information
rimadeodhar committed Oct 13, 2021
1 parent ab68761 commit 60ac3d8
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions pkg/sql/sqlinstance/instancestorage/instancestorage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package instancestorage_test

import (
"context"
"fmt"
"math/rand"
"sort"
"strings"
Expand Down Expand Up @@ -188,6 +189,60 @@ func TestStorage(t *testing.T) {
})
}

// TestSQLAccess verifies that the sql_instances table is accessible
// through SQL API.
func TestSQLAccess(t *testing.T) {
defer leaktest.AfterTest(t)()
defer log.Scope(t).Close(t)

ctx := context.Background()
s, sqlDB, kvDB := serverutils.StartServer(t, base.TestServerArgs{})
defer s.Stopper().Stop(ctx)
clock := hlc.NewClock(func() int64 {
return timeutil.NewTestTimeSource().Now().UnixNano()
}, base.DefaultMaxClockOffset)
tDB := sqlutils.MakeSQLRunner(sqlDB)
dbName := t.Name()
tDB.Exec(t, `CREATE DATABASE "`+dbName+`"`)
schema := strings.Replace(systemschema.SQLInstancesTableSchema,
`CREATE TABLE system.sql_instances`,
`CREATE TABLE "`+dbName+`".sql_instances`, 1)
tDB.Exec(t, schema)
tableID := getTableID(t, tDB, dbName, "sql_instances")
stopper := stop.NewStopper()
defer stopper.Stop(ctx)
storage := instancestorage.NewTestingStorage(kvDB, keys.SystemSQLCodec, tableID, slstorage.NewFakeStorage())
const (
sessionID = sqlliveness.SessionID("session")
addr = "addr"
expiration = time.Minute
expectedNumCols = 3
)
instanceID, err := storage.CreateInstance(ctx, sessionID, clock.Now().Add(expiration.Nanoseconds(), 0), addr)
require.NoError(t, err)

// Query the table through SQL and verify the query completes successfully.
rows := tDB.Query(t, fmt.Sprintf("SELECT id, addr, session_id FROM \"%s\".sql_instances", dbName))
defer rows.Close()
columns, err := rows.Columns()
require.NoError(t, err)
require.Equal(t, expectedNumCols, len(columns))
var parsedInstanceID base.SQLInstanceID
var parsedSessionID sqlliveness.SessionID
var parsedAddr string
rows.Next()
err = rows.Scan(&parsedInstanceID, &parsedAddr, &parsedSessionID)
require.NoError(t, err)
require.Equal(t, instanceID, parsedInstanceID)
require.Equal(t, sessionID, parsedSessionID)
require.Equal(t, addr, parsedAddr)

// Verify that the table only contains one row as expected.
hasAnotherRow := rows.Next()
require.NoError(t, rows.Err())
require.False(t, hasAnotherRow)
}

// TestConcurrentCreateAndRelease verifies that concurrent access to instancestorage
// to create and release SQL instance IDs works as expected.
func TestConcurrentCreateAndRelease(t *testing.T) {
Expand Down

0 comments on commit 60ac3d8

Please sign in to comment.