@@ -16,6 +16,7 @@ import (
16
16
17
17
ydbDatastore "github.com/authzed/spicedb/internal/datastore/ydb"
18
18
ydbMigrations "github.com/authzed/spicedb/internal/datastore/ydb/migrations"
19
+ "github.com/authzed/spicedb/pkg/secrets"
19
20
20
21
"github.com/authzed/spicedb/pkg/datastore"
21
22
"github.com/authzed/spicedb/pkg/migrate"
@@ -33,31 +34,20 @@ type ydbTester struct {
33
34
34
35
hostname string
35
36
port string
36
- dsn string
37
37
}
38
38
39
39
// RunYDBForTesting returns a RunningEngineForTest for YDB.
40
40
func RunYDBForTesting (t testing.TB , bridgeNetworkName string ) RunningEngineForTest {
41
41
pool , err := dockertest .NewPool ("" )
42
42
require .NoError (t , err )
43
43
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
-
54
44
containerName := fmt .Sprintf ("ydb-%s" , uuid .New ().String ())
55
45
hostname := "localhost"
56
- if r . bridgeNetworkName != "" {
46
+ if bridgeNetworkName != "" {
57
47
hostname = containerName
58
48
}
59
49
60
- resource , err := r . pool .RunWithOptions (& dockertest.RunOptions {
50
+ resource , err := pool .RunWithOptions (& dockertest.RunOptions {
61
51
Name : containerName ,
62
52
Hostname : hostname ,
63
53
Repository : "ghcr.io/ydb-platform/local-ydb" ,
@@ -66,12 +56,14 @@ func (r ydbTester) NewDatabase(t testing.TB) string {
66
56
"YDB_USE_IN_MEMORY_PDISKS=true" ,
67
57
"YDB_FEATURE_FLAGS=enable_not_null_data_columns" ,
68
58
},
69
- NetworkID : r . bridgeNetworkName ,
59
+ NetworkID : bridgeNetworkName ,
70
60
})
71
61
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 )) })
73
63
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 {
75
67
var buf bytes.Buffer
76
68
77
69
code , err := resource .Exec ([]string {
@@ -96,11 +88,28 @@ func (r ydbTester) NewDatabase(t testing.TB) string {
96
88
}))
97
89
98
90
port := resource .GetPort (fmt .Sprintf ("%d/tcp" , ydbGRPCPort ))
99
- if r . bridgeNetworkName != "" {
91
+ if bridgeNetworkName != "" {
100
92
port = strconv .FormatInt (ydbGRPCPort , 10 )
101
93
}
102
94
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
+
104
113
return dsn
105
114
}
106
115
0 commit comments