77 "context"
88 "crypto/tls"
99 "fmt"
10+ "net/url"
1011 "os"
12+ "path"
1113 "testing"
1214 "time"
1315
@@ -28,10 +30,14 @@ import (
2830
2931 "github.com/ydb-platform/ydb-go-sdk/v3"
3032 "github.com/ydb-platform/ydb-go-sdk/v3/config"
33+ "github.com/ydb-platform/ydb-go-sdk/v3/internal/credentials"
3134 "github.com/ydb-platform/ydb-go-sdk/v3/internal/meta"
35+ "github.com/ydb-platform/ydb-go-sdk/v3/internal/version"
3236 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest"
3337 "github.com/ydb-platform/ydb-go-sdk/v3/log"
3438 "github.com/ydb-platform/ydb-go-sdk/v3/retry"
39+ "github.com/ydb-platform/ydb-go-sdk/v3/table"
40+ "github.com/ydb-platform/ydb-go-sdk/v3/table/options"
3541 "github.com/ydb-platform/ydb-go-sdk/v3/table/result/indexed"
3642 "github.com/ydb-platform/ydb-go-sdk/v3/trace"
3743)
@@ -165,7 +171,159 @@ func TestDriver(sourceTest *testing.T) {
165171 t .Fatalf ("close failed: %+v" , e )
166172 }
167173 }()
168- t .Run ("With" , func (t * testing.T ) {
174+ t .RunSynced ("WithStaticCredentials" , func (t * xtest.SyncedTest ) {
175+ if version .Lt (os .Getenv ("YDB_VERSION" ), "24.1" ) {
176+ t .Skip ("read rows not allowed in YDB version '" + os .Getenv ("YDB_VERSION" ) + "'" )
177+ }
178+ db , err := ydb .Open (ctx ,
179+ os .Getenv ("YDB_CONNECTION_STRING" ),
180+ ydb .WithAccessTokenCredentials (
181+ os .Getenv ("YDB_ACCESS_TOKEN_CREDENTIALS" ),
182+ ),
183+ )
184+ require .NoError (t , err )
185+ defer func () {
186+ _ = db .Close (ctx )
187+ }()
188+ err = db .Query ().Exec (ctx , `DROP USER IF EXISTS test` )
189+ require .NoError (t , err )
190+ err = db .Query ().Exec (ctx , "CREATE USER test PASSWORD 'password'; ALTER GROUP `ADMINS` ADD USER test;" )
191+ require .NoError (t , err )
192+ defer func () {
193+ err = db .Query ().Exec (ctx , `DROP USER test` )
194+ require .NoError (t , err )
195+ }()
196+ t .RunSynced ("UsingConnectionString" , func (t * xtest.SyncedTest ) {
197+ t .RunSynced ("HappyWay" , func (t * xtest.SyncedTest ) {
198+ u , err := url .Parse (os .Getenv ("YDB_CONNECTION_STRING" ))
199+ require .NoError (t , err )
200+ u .User = url .UserPassword ("test" , "password" )
201+ t .Log (u .String ())
202+ db , err := ydb .Open (ctx , u .String ())
203+ require .NoError (t , err )
204+ defer func () {
205+ _ = db .Close (ctx )
206+ }()
207+ row , err := db .Query ().QueryRow (ctx , `SELECT 1` )
208+ require .NoError (t , err )
209+ var v int
210+ err = row .Scan (& v )
211+ require .NoError (t , err )
212+ tableName := path .Join (db .Name (), t .Name (), "test" )
213+ t .RunSynced ("CreateTable" , func (t * xtest.SyncedTest ) {
214+ err := db .Query ().Exec (ctx , fmt .Sprintf (`
215+ CREATE TABLE IF NOT EXISTS %s (
216+ id Uint64,
217+ value Utf8,
218+ PRIMARY KEY (id)
219+ )` , "`" + tableName + "`" ),
220+ )
221+ require .NoError (t , err )
222+ })
223+ t .RunSynced ("DescribeTable" , func (t * xtest.SyncedTest ) {
224+ var d options.Description
225+ err := db .Table ().Do (ctx , func (ctx context.Context , s table.Session ) error {
226+ d , err = s .DescribeTable (ctx , tableName )
227+ if err != nil {
228+ return err
229+ }
230+
231+ return nil
232+ })
233+ require .NoError (t , err )
234+ require .Equal (t , "test" , d .Name )
235+ require .Equal (t , 2 , len (d .Columns ))
236+ require .Equal (t , "id" , d .Columns [0 ].Name )
237+ require .Equal (t , "value" , d .Columns [1 ].Name )
238+ require .Equal (t , []string {"id" }, d .PrimaryKey )
239+ })
240+ })
241+ t .RunSynced ("WrongLogin" , func (t * xtest.SyncedTest ) {
242+ u , err := url .Parse (os .Getenv ("YDB_CONNECTION_STRING" ))
243+ require .NoError (t , err )
244+ u .User = url .UserPassword ("wrong_login" , "password" )
245+ db , err := ydb .Open (ctx , u .String ())
246+ require .Error (t , err )
247+ require .Nil (t , db )
248+ require .True (t , credentials .IsAccessError (err ))
249+ })
250+ t .RunSynced ("WrongPassword" , func (t * xtest.SyncedTest ) {
251+ u , err := url .Parse (os .Getenv ("YDB_CONNECTION_STRING" ))
252+ require .NoError (t , err )
253+ u .User = url .UserPassword ("test" , "wrong_password" )
254+ db , err := ydb .Open (ctx , u .String ())
255+ require .Error (t , err )
256+ require .Nil (t , db )
257+ require .True (t , credentials .IsAccessError (err ))
258+ })
259+ })
260+ t .RunSynced ("UsingExplicitStaticCredentials" , func (t * xtest.SyncedTest ) {
261+ t .RunSynced ("HappyWay" , func (t * xtest.SyncedTest ) {
262+ db , err := ydb .Open (ctx ,
263+ os .Getenv ("YDB_CONNECTION_STRING" ),
264+ ydb .WithStaticCredentials ("test" , "password" ),
265+ )
266+ require .NoError (t , err )
267+ defer func () {
268+ _ = db .Close (ctx )
269+ }()
270+ tableName := path .Join (db .Name (), t .Name (), "test" )
271+ t .RunSynced ("CreateTable" , func (t * xtest.SyncedTest ) {
272+ err := db .Query ().Exec (ctx , fmt .Sprintf (`
273+ CREATE TABLE IF NOT EXISTS %s (
274+ id Uint64,
275+ value Utf8,
276+ PRIMARY KEY (id)
277+ )` , "`" + tableName + "`" ),
278+ )
279+ require .NoError (t , err )
280+ })
281+ t .RunSynced ("Query" , func (t * xtest.SyncedTest ) {
282+ row , err := db .Query ().QueryRow (ctx , `SELECT 1` )
283+ require .NoError (t , err )
284+ var v int
285+ err = row .Scan (& v )
286+ require .NoError (t , err )
287+ })
288+ t .RunSynced ("DescribeTable" , func (t * xtest.SyncedTest ) {
289+ var d options.Description
290+ err := db .Table ().Do (ctx , func (ctx context.Context , s table.Session ) error {
291+ d , err = s .DescribeTable (ctx , tableName )
292+ if err != nil {
293+ return err
294+ }
295+
296+ return nil
297+ })
298+ require .NoError (t , err )
299+ require .Equal (t , "test" , d .Name )
300+ require .Equal (t , 2 , len (d .Columns ))
301+ require .Equal (t , "id" , d .Columns [0 ].Name )
302+ require .Equal (t , "value" , d .Columns [1 ].Name )
303+ require .Equal (t , []string {"id" }, d .PrimaryKey )
304+ })
305+ })
306+ t .RunSynced ("WrongLogin" , func (t * xtest.SyncedTest ) {
307+ db , err := ydb .Open (ctx ,
308+ os .Getenv ("YDB_CONNECTION_STRING" ),
309+ ydb .WithStaticCredentials ("wrong_user" , "password" ),
310+ )
311+ require .Error (t , err )
312+ require .Nil (t , db )
313+ require .True (t , credentials .IsAccessError (err ))
314+ })
315+ t .RunSynced ("WrongPassword" , func (t * xtest.SyncedTest ) {
316+ db , err := ydb .Open (ctx ,
317+ os .Getenv ("YDB_CONNECTION_STRING" ),
318+ ydb .WithStaticCredentials ("test" , "wrong_password" ),
319+ )
320+ require .Error (t , err )
321+ require .Nil (t , db )
322+ require .True (t , credentials .IsAccessError (err ))
323+ })
324+ })
325+ })
326+ t .RunSynced ("With" , func (t * xtest.SyncedTest ) {
169327 t .Run ("WithSharedBalancer" , func (t * testing.T ) {
170328 child , err := db .With (ctx , ydb .WithSharedBalancer (db ))
171329 require .NoError (t , err )
0 commit comments