github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/tests/integration/database_sql_static_credentials_test.go (about)

     1  //go:build integration
     2  // +build integration
     3  
     4  package integration
     5  
     6  import (
     7  	"context"
     8  	"database/sql"
     9  	"net/url"
    10  	"os"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/stretchr/testify/require"
    15  	"google.golang.org/grpc"
    16  	grpcCredentials "google.golang.org/grpc/credentials"
    17  	"google.golang.org/grpc/credentials/insecure"
    18  
    19  	"github.com/ydb-platform/ydb-go-sdk/v3"
    20  	"github.com/ydb-platform/ydb-go-sdk/v3/credentials"
    21  )
    22  
    23  func TestDatabaseSqlStaticCredentials(t *testing.T) {
    24  	ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
    25  	defer cancel()
    26  
    27  	var dsn string
    28  	if v, has := os.LookupEnv("YDB_CONNECTION_STRING"); !has {
    29  		t.Fatal("env YDB_CONNECTION_STRING required")
    30  	} else {
    31  		dsn = v
    32  	}
    33  
    34  	u, err := url.Parse(dsn)
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  
    39  	u.User = url.UserPassword("root", "1234")
    40  
    41  	t.Run("sql.Open", func(t *testing.T) {
    42  		var db *sql.DB
    43  		db, err = sql.Open("ydb", u.String())
    44  		require.NoError(t, err)
    45  
    46  		err = db.PingContext(ctx)
    47  		require.NoError(t, err)
    48  
    49  		err = db.Close()
    50  		require.NoError(t, err)
    51  	})
    52  
    53  	t.Run("sql.OpenDB", func(t *testing.T) {
    54  		var cc *ydb.Driver
    55  		cc, err = ydb.Open(ctx, os.Getenv("YDB_CONNECTION_STRING"),
    56  			ydb.WithCredentials(credentials.NewStaticCredentials(u.User.Username(), func() string {
    57  				password, _ := u.User.Password()
    58  				return password
    59  			}(), u.Host, credentials.WithGrpcDialOptions(func() grpc.DialOption {
    60  				if u.Scheme == "grpcs" { //nolint:goconst
    61  					transportCredentials, transportCredentialsErr := grpcCredentials.NewClientTLSFromFile(
    62  						os.Getenv("YDB_SSL_ROOT_CERTIFICATES_FILE"), u.Hostname(),
    63  					)
    64  					if err != nil {
    65  						t.Fatalf("cannot create transport credentials: %v", transportCredentialsErr)
    66  					}
    67  					return grpc.WithTransportCredentials(transportCredentials)
    68  				}
    69  				return grpc.WithTransportCredentials(insecure.NewCredentials())
    70  			}()))),
    71  		)
    72  		require.NoError(t, err)
    73  
    74  		defer func() {
    75  			// cleanup
    76  			_ = cc.Close(ctx)
    77  		}()
    78  
    79  		c, err := ydb.Connector(cc)
    80  		require.NoError(t, err)
    81  
    82  		defer func() {
    83  			// cleanup
    84  			_ = c.Close()
    85  		}()
    86  
    87  		db := sql.OpenDB(c)
    88  		defer func() {
    89  			// cleanup
    90  			_ = db.Close()
    91  		}()
    92  
    93  		err = db.PingContext(ctx)
    94  		require.NoError(t, err)
    95  	})
    96  }