@@ -7,25 +7,31 @@ import (
77 "context"
88 "crypto/tls"
99 "fmt"
10+ "net/url"
1011 "os"
1112 "testing"
1213 "time"
1314
1415 "github.com/ydb-platform/ydb-go-genproto/Ydb_Discovery_V1"
1516 "github.com/ydb-platform/ydb-go-genproto/Ydb_Export_V1"
17+ "github.com/ydb-platform/ydb-go-genproto/Ydb_Monitoring_V1"
1618 "github.com/ydb-platform/ydb-go-genproto/Ydb_Scripting_V1"
1719 "github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
1820 "github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Discovery"
1921 "github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Export"
22+ "github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Monitoring"
2023 "github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Operations"
2124 "github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Scripting"
2225 "google.golang.org/grpc"
26+ grpcCredentials "google.golang.org/grpc/credentials"
27+ "google.golang.org/grpc/credentials/insecure"
2328 "google.golang.org/grpc/metadata"
2429 "google.golang.org/protobuf/proto"
2530 "google.golang.org/protobuf/types/known/durationpb"
2631
2732 "github.com/ydb-platform/ydb-go-sdk/v3"
2833 "github.com/ydb-platform/ydb-go-sdk/v3/config"
34+ "github.com/ydb-platform/ydb-go-sdk/v3/credentials"
2935 "github.com/ydb-platform/ydb-go-sdk/v3/internal/meta"
3036 "github.com/ydb-platform/ydb-go-sdk/v3/log"
3137 "github.com/ydb-platform/ydb-go-sdk/v3/retry"
@@ -314,3 +320,107 @@ func TestConnection(t *testing.T) {
314320 }
315321 })
316322}
323+
324+ func TestStaticCredentials (t * testing.T ) {
325+ t .Skip ("wait for newest cr.yandex/yc/yandex-docker-local-ydb:latest was published" )
326+
327+ ctx , cancel := context .WithTimeout (context .Background (), time .Second * 10 )
328+ defer cancel ()
329+
330+ var dsn string
331+ if v , has := os .LookupEnv ("YDB_CONNECTION_STRING" ); ! has {
332+ t .Fatal ("env YDB_CONNECTION_STRING required" )
333+ } else {
334+ dsn = v
335+ }
336+
337+ url , err := url .Parse (dsn )
338+ if err != nil {
339+ t .Fatal (err )
340+ }
341+
342+ staticCredentials := credentials .NewStaticCredentials ("root" , "" , url .Host , func () grpc.DialOption {
343+ if url .Scheme == "grpcs" {
344+ transportCredentials , transportCredentialsErr := grpcCredentials .NewClientTLSFromFile (
345+ os .Getenv ("YDB_SSL_ROOT_CERTIFICATES_FILE" ), url .Hostname (),
346+ )
347+ if err != nil {
348+ t .Fatalf ("cannot create transport credentials: %v" , transportCredentialsErr )
349+ }
350+ return grpc .WithTransportCredentials (transportCredentials )
351+ }
352+ return grpc .WithTransportCredentials (insecure .NewCredentials ())
353+ }())
354+
355+ token , err := staticCredentials .Token (ctx )
356+ if err != nil {
357+ t .Fatalf ("get token failed: %v" , err )
358+ } else {
359+ fmt .Printf ("token: %s\n " , token )
360+ }
361+
362+ db , err := ydb .Open (
363+ ctx ,
364+ "" , // corner case for check replacement of endpoint+database+secure
365+ ydb .WithConnectionString (os .Getenv ("YDB_CONNECTION_STRING" )),
366+ ydb .WithCredentials (staticCredentials ),
367+ )
368+ if err != nil {
369+ t .Fatal (err )
370+ }
371+ defer func () {
372+ // cleanup connection
373+ if e := db .Close (ctx ); e != nil {
374+ t .Fatalf ("close failed: %+v" , e )
375+ }
376+ }()
377+ _ , err = db .Discovery ().WhoAmI (ctx )
378+ if err != nil {
379+ t .Fatal (err )
380+ }
381+ }
382+
383+ func TestMonitoring (t * testing.T ) {
384+ t .Skip ("wait for newest cr.yandex/yc/yandex-docker-local-ydb:latest was published" )
385+
386+ ctx , cancel := context .WithTimeout (context .Background (), time .Second * 10 )
387+ defer cancel ()
388+
389+ db , err := ydb .Open (
390+ ctx ,
391+ "" , // corner case for check replacement of endpoint+database+secure
392+ ydb .WithConnectionString (os .Getenv ("YDB_CONNECTION_STRING" )),
393+ )
394+ if err != nil {
395+ t .Fatal (err )
396+ }
397+ defer func () {
398+ // cleanup connection
399+ if e := db .Close (ctx ); e != nil {
400+ t .Fatalf ("close failed: %+v" , e )
401+ }
402+ }()
403+ t .Run ("monitoring.SelfCheck" , func (t * testing.T ) {
404+ if err = retry .Retry (ctx , func (ctx context.Context ) (err error ) {
405+ client := Ydb_Monitoring_V1 .NewMonitoringServiceClient (ydb .GRPCConn (db ))
406+ response , err := client .SelfCheck (ctx , & Ydb_Monitoring.SelfCheckRequest {
407+ OperationParams : nil ,
408+ ReturnVerboseStatus : false ,
409+ MinimumStatus : 0 ,
410+ MaximumLevel : 0 ,
411+ })
412+ if err != nil {
413+ return err
414+ }
415+ var result Ydb_Monitoring.SelfCheckResult
416+ err = response .Operation .Result .UnmarshalTo (& result )
417+ if err != nil {
418+ return err
419+ }
420+ fmt .Printf ("%+v\n " , & result )
421+ return nil
422+ }, retry .WithIdempotent (true )); err != nil {
423+ t .Fatalf ("Execute failed: %v" , err )
424+ }
425+ })
426+ }
0 commit comments