Skip to content

Commit 3fe0b7e

Browse files
Run a single container for datastore tests
Signed-off-by: sashayakovtseva <[email protected]>
1 parent 3f2e270 commit 3fe0b7e

File tree

1 file changed

+27
-18
lines changed
  • internal/testserver/datastore

1 file changed

+27
-18
lines changed

internal/testserver/datastore/ydb.go

+27-18
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616

1717
ydbDatastore "github.com/authzed/spicedb/internal/datastore/ydb"
1818
ydbMigrations "github.com/authzed/spicedb/internal/datastore/ydb/migrations"
19+
"github.com/authzed/spicedb/pkg/secrets"
1920

2021
"github.com/authzed/spicedb/pkg/datastore"
2122
"github.com/authzed/spicedb/pkg/migrate"
@@ -33,31 +34,20 @@ type ydbTester struct {
3334

3435
hostname string
3536
port string
36-
dsn string
3737
}
3838

3939
// RunYDBForTesting returns a RunningEngineForTest for YDB.
4040
func RunYDBForTesting(t testing.TB, bridgeNetworkName string) RunningEngineForTest {
4141
pool, err := dockertest.NewPool("")
4242
require.NoError(t, err)
4343

44-
return ydbTester{
45-
pool: pool,
46-
bridgeNetworkName: bridgeNetworkName,
47-
}
48-
}
49-
50-
func (r ydbTester) NewDatabase(t testing.TB) string {
51-
// there's no easy way to create new database in a local YDB, so
52-
// create a new container with default /local database instead.
53-
5444
containerName := fmt.Sprintf("ydb-%s", uuid.New().String())
5545
hostname := "localhost"
56-
if r.bridgeNetworkName != "" {
46+
if bridgeNetworkName != "" {
5747
hostname = containerName
5848
}
5949

60-
resource, err := r.pool.RunWithOptions(&dockertest.RunOptions{
50+
resource, err := pool.RunWithOptions(&dockertest.RunOptions{
6151
Name: containerName,
6252
Hostname: hostname,
6353
Repository: "ghcr.io/ydb-platform/local-ydb",
@@ -66,12 +56,14 @@ func (r ydbTester) NewDatabase(t testing.TB) string {
6656
"YDB_USE_IN_MEMORY_PDISKS=true",
6757
"YDB_FEATURE_FLAGS=enable_not_null_data_columns",
6858
},
69-
NetworkID: r.bridgeNetworkName,
59+
NetworkID: bridgeNetworkName,
7060
})
7161
require.NoError(t, err)
72-
t.Cleanup(func() { require.NoError(t, r.pool.Purge(resource)) })
62+
t.Cleanup(func() { require.NoError(t, pool.Purge(resource)) })
7363

74-
require.NoError(t, r.pool.Retry(func() error { // await container is ready
64+
// await container is ready.
65+
// since YDB has internal cluster discovery we can't check availability from outside network.
66+
require.NoError(t, pool.Retry(func() error {
7567
var buf bytes.Buffer
7668

7769
code, err := resource.Exec([]string{
@@ -96,11 +88,28 @@ func (r ydbTester) NewDatabase(t testing.TB) string {
9688
}))
9789

9890
port := resource.GetPort(fmt.Sprintf("%d/tcp", ydbGRPCPort))
99-
if r.bridgeNetworkName != "" {
91+
if bridgeNetworkName != "" {
10092
port = strconv.FormatInt(ydbGRPCPort, 10)
10193
}
10294

103-
dsn := fmt.Sprintf("grpc://%s:%s/%s", hostname, port, ydbDefaultDatabase)
95+
return ydbTester{
96+
pool: pool,
97+
bridgeNetworkName: bridgeNetworkName,
98+
hostname: hostname,
99+
port: port,
100+
}
101+
}
102+
103+
func (r ydbTester) NewDatabase(t testing.TB) string {
104+
// there's no easy way to create new database in a local YDB,
105+
// so create a new directory instead.
106+
107+
uniquePortion, err := secrets.TokenHex(4)
108+
require.NoError(t, err)
109+
110+
directory := fmt.Sprintf("/%s/%s", ydbDefaultDatabase, uniquePortion)
111+
dsn := fmt.Sprintf("grpc://%s:%s/%s?table_path_prefix=%s", r.hostname, r.port, ydbDefaultDatabase, directory)
112+
104113
return dsn
105114
}
106115

0 commit comments

Comments
 (0)