-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move common ydb topics testing funcs to helpers
Move common ydb topics testing funcs to helpers. This will make it easier to work on other PRs about yds, ydb and lb commit_hash:a43d0703b0d99bb6b8fa1b00c23adb64b334cb7f
- Loading branch information
Showing
3 changed files
with
53 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package recipe | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strconv" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/ydb-platform/ydb-go-sdk/v3" | ||
"github.com/ydb-platform/ydb-go-sdk/v3/credentials" | ||
"github.com/ydb-platform/ydb-go-sdk/v3/sugar" | ||
) | ||
|
||
func Driver(t *testing.T, opts ...ydb.Option) *ydb.Driver { | ||
instance, port, database, creds := InstancePortDatabaseCreds(t) | ||
dsn := sugar.DSN(fmt.Sprintf("%s:%d", instance, port), database) | ||
if creds != nil { | ||
opts = append(opts, ydb.WithCredentials(creds)) | ||
} | ||
driver, err := ydb.Open(context.Background(), dsn, opts...) | ||
require.NoError(t, err) | ||
|
||
return driver | ||
} | ||
|
||
func InstancePortDatabaseCreds(t *testing.T) (string, int, string, credentials.Credentials) { | ||
parts := strings.Split(os.Getenv("YDB_ENDPOINT"), ":") | ||
require.Len(t, parts, 2) | ||
|
||
instance := parts[0] | ||
port, err := strconv.Atoi(parts[1]) | ||
require.NoError(t, err) | ||
|
||
database := os.Getenv("YDB_DATABASE") | ||
if database == "" { | ||
database = "local" | ||
} | ||
|
||
var creds credentials.Credentials | ||
token := os.Getenv("YDB_TOKEN") | ||
if token != "" { | ||
creds = credentials.NewAccessTokenCredentials(token) | ||
} | ||
|
||
return instance, port, database, creds | ||
} |