Skip to content

Commit

Permalink
sql: move copy_in_test to pkg/sql/copy to have all copy tests together
Browse files Browse the repository at this point in the history
Epic: CRDB-18892
Informs: #91831
Release note: None
  • Loading branch information
cucaroach committed Jan 31, 2023
1 parent 3ff9bc9 commit 9894417
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 25 deletions.
3 changes: 0 additions & 3 deletions pkg/sql/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,6 @@ go_test(
"conn_executor_savepoints_test.go",
"conn_executor_test.go",
"conn_io_test.go",
"copy_in_test.go",
"copy_test.go",
"crdb_internal_test.go",
"create_function_test.go",
Expand Down Expand Up @@ -826,8 +825,6 @@ go_test(
"//pkg/util/shuffle",
"//pkg/util/stop",
"//pkg/util/syncutil",
"//pkg/util/timeofday",
"//pkg/util/timetz",
"//pkg/util/timeutil",
"//pkg/util/tracing",
"//pkg/util/tracing/tracingpb",
Expand Down
13 changes: 13 additions & 0 deletions pkg/sql/copy/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_test")
go_test(
name = "copy_test",
srcs = [
"copy_in_test.go",
"copy_test.go",
"main_test.go",
],
Expand All @@ -12,22 +13,34 @@ go_test(
deps = [
"//pkg/base",
"//pkg/cli/clisqlclient",
"//pkg/roachpb",
"//pkg/security/securityassets",
"//pkg/security/securitytest",
"//pkg/security/username",
"//pkg/server",
"//pkg/settings/cluster",
"//pkg/sql",
"//pkg/sql/randgen",
"//pkg/sql/sem/tree",
"//pkg/sql/sqltestutils",
"//pkg/sql/tests",
"//pkg/sql/types",
"//pkg/testutils",
"//pkg/testutils/datapathutils",
"//pkg/testutils/serverutils",
"//pkg/testutils/sqlutils",
"//pkg/testutils/testcluster",
"//pkg/util/leaktest",
"//pkg/util/log",
"//pkg/util/randutil",
"//pkg/util/timeofday",
"//pkg/util/timetz",
"@com_github_cockroachdb_apd_v3//:apd",
"@com_github_cockroachdb_datadriven//:datadriven",
"@com_github_jackc_pgtype//:pgtype",
"@com_github_jackc_pgx_v4//:pgx",
"@com_github_lib_pq//:pq",
"@com_github_stretchr_testify//assert",
"@com_github_stretchr_testify//require",
],
)
Expand Down
5 changes: 3 additions & 2 deletions pkg/sql/copy_in_test.go → pkg/sql/copy/copy_in_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package sql_test
package copy

import (
"context"
Expand All @@ -27,6 +27,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/sql"
"github.com/cockroachdb/cockroach/pkg/sql/randgen"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqltestutils"
"github.com/cockroachdb/cockroach/pkg/sql/tests"
"github.com/cockroachdb/cockroach/pkg/sql/types"
"github.com/cockroachdb/cockroach/pkg/testutils"
Expand Down Expand Up @@ -778,7 +779,7 @@ func TestCopyInReleasesLeases(t *testing.T) {
s.ServingSQLAddr(), t.Name(), url.UserPassword("foo", "testabc"),
false /* withClientCerts */)
defer cleanupFn()
conn, err := pgxConn(t, userURL)
conn, err := sqltestutils.PGXConn(t, userURL)
require.NoError(t, err)

rowsAffected, err := conn.CopyFrom(
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/copy/copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package copy_test
package copy

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion pkg/sql/copy/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package copy_test
package copy

import (
"os"
Expand Down
1 change: 1 addition & 0 deletions pkg/sql/sqltestutils/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ go_library(
"//pkg/util/treeprinter",
"@com_github_cockroachdb_datadriven//:datadriven",
"@com_github_cockroachdb_errors//:errors",
"@com_github_jackc_pgx_v4//:pgx",
"@com_github_lib_pq//:pq",
"@com_github_stretchr_testify//require",
],
Expand Down
12 changes: 12 additions & 0 deletions pkg/sql/sqltestutils/sql_test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"context"
gosql "database/sql"
"fmt"
"net/url"
"strings"
"testing"

Expand All @@ -29,6 +30,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/util"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/errors"
"github.com/jackc/pgx/v4"
"github.com/lib/pq"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -140,3 +142,13 @@ func ArrayStringToSlice(t *testing.T, array string, message ...string) []string
}
return strings.Split(array[1:length-1], ",")
}

// PGXConn is a helper to create a pgx.Conn from a url.
func PGXConn(t *testing.T, connURL url.URL) (*pgx.Conn, error) {
t.Helper()
pgxConfig, err := pgx.ParseConfig(connURL.String())
if err != nil {
t.Fatal(err)
}
return pgx.ConnectConfig(context.Background(), pgxConfig)
}
26 changes: 8 additions & 18 deletions pkg/sql/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ import (
"github.com/cockroachdb/cockroach/pkg/kv/kvserver"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/security/username"
"github.com/cockroachdb/cockroach/pkg/sql/sqltestutils"
"github.com/cockroachdb/cockroach/pkg/testutils"
"github.com/cockroachdb/cockroach/pkg/testutils/serverutils"
"github.com/cockroachdb/cockroach/pkg/testutils/skip"
"github.com/cockroachdb/cockroach/pkg/testutils/sqlutils"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
pgx "github.com/jackc/pgx/v4"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -112,7 +112,7 @@ GRANT admin TO foo`); err != nil {
// would report false positives.
unauthURL := fooURL
unauthURL.User = url.User("foo")
dbSQL, err := pgxConn(t, unauthURL)
dbSQL, err := sqltestutils.PGXConn(t, unauthURL)
if err == nil {
defer func() { _ = dbSQL.Close(ctx) }()
}
Expand All @@ -123,7 +123,7 @@ GRANT admin TO foo`); err != nil {

func() {
// Sanity check: verify that the new user is able to log in with password.
dbSQL, err := pgxConn(t, fooURL)
dbSQL, err := sqltestutils.PGXConn(t, fooURL)
if err != nil {
t.Fatal(err)
}
Expand All @@ -145,7 +145,7 @@ GRANT admin TO foo`); err != nil {

// Cache our privilege check for `SHOW is_superuser` and the
// underlying query to a virtual table.
dbSQL, err := pgxConn(t, fooURL)
dbSQL, err := sqltestutils.PGXConn(t, fooURL)
if err != nil {
t.Fatal(err)
}
Expand All @@ -169,7 +169,7 @@ GRANT admin TO foo`); err != nil {
// Now attempt to connect again. Since a previous authentication attempt
// for this user occurred, the auth-related info should be cached, so
// authentication should work.
dbSQL, err := pgxConn(t, fooURL)
dbSQL, err := sqltestutils.PGXConn(t, fooURL)
if err != nil {
t.Fatal(err)
}
Expand All @@ -189,7 +189,7 @@ GRANT admin TO foo`); err != nil {
// Now attempt to connect with a different user. We're expecting a timeout
// within 5 seconds.
start := timeutil.Now()
dbSQL, err := pgxConn(t, barURL)
dbSQL, err := sqltestutils.PGXConn(t, barURL)
if err == nil {
defer func() { _ = dbSQL.Close(ctx) }()
}
Expand All @@ -205,7 +205,7 @@ GRANT admin TO foo`); err != nil {
t.Log("-- no timeout for root --")

func() {
dbSQL, err := pgxConn(t, rootURL)
dbSQL, err := sqltestutils.PGXConn(t, rootURL)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -237,7 +237,7 @@ GRANT admin TO foo`); err != nil {
// Now attempt to connect with foo. We're expecting a timeout within 5
// seconds as the membership cache is invalid.
start := timeutil.Now()
dbSQL, err := pgxConn(t, fooURL)
dbSQL, err := sqltestutils.PGXConn(t, fooURL)
if err == nil {
defer func() { _ = dbSQL.Close(ctx) }()
}
Expand All @@ -252,13 +252,3 @@ GRANT admin TO foo`); err != nil {
}()
})
}

func pgxConn(t *testing.T, connURL url.URL) (*pgx.Conn, error) {
t.Helper()
pgxConfig, err := pgx.ParseConfig(connURL.String())
if err != nil {
t.Fatal(err)
}

return pgx.ConnectConfig(context.Background(), pgxConfig)
}

0 comments on commit 9894417

Please sign in to comment.