github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/internal/testserver/datastore/datastore.go (about) 1 //go:build docker 2 // +build docker 3 4 package datastore 5 6 import ( 7 "testing" 8 "time" 9 10 "github.com/stretchr/testify/require" 11 12 "github.com/authzed/spicedb/internal/datastore/postgres/version" 13 "github.com/authzed/spicedb/pkg/datastore" 14 "github.com/authzed/spicedb/pkg/migrate" 15 ) 16 17 const dockerBootTimeout = 10 * time.Second 18 19 // InitFunc initializes a datastore instance from a uri that has been 20 // generated from a TestDatastoreBuilder 21 type InitFunc func(engine, uri string) datastore.Datastore 22 23 // RunningEngineForTest represents an instance of a datastore engine running with its backing 24 // database/service, expressly for testing. 25 type RunningEngineForTest interface { 26 // NewDatabase returns the connection string to a new initialized logical database, 27 // but one that is *not migrated*. 28 NewDatabase(t testing.TB) string 29 30 // NewDatastore returns a new logical datastore initialized with the 31 // initFunc. For example, the sql based stores will create a new logical 32 // database in the database instance, migrate it and provide the URI for it 33 // to initFunc 34 NewDatastore(t testing.TB, initFunc InitFunc) datastore.Datastore 35 } 36 37 // RunningEngineForTestWithEnvVars represents an instance of a datastore engine running, that also 38 // requires env vars set to use from an external source (like a container). 39 type RunningEngineForTestWithEnvVars interface { 40 RunningEngineForTest 41 42 // ExternalEnvVars are the environment variables to be set. 43 ExternalEnvVars() []string 44 } 45 46 // RunDatastoreEngine starts the backing database or service necessary for the given engine 47 // for testing and sets the defaults for that database or service. Note that this does *NOT* 48 // create the logical database nor call migrate; callers can do so via NewDatabase and NewDatastore 49 // respectively. Note also that the backing database or service will be shutdown automatically via 50 // the Cleanup of the testing object. 51 func RunDatastoreEngine(t testing.TB, engine string) RunningEngineForTest { 52 return RunDatastoreEngineWithBridge(t, engine, "") 53 } 54 55 // RunDatastoreEngineWithBridge runs a datastore engine on a specific bridge. If a bridge is 56 // specified, then the hostnames returned by the engines are those to be called from another 57 // container on the bridge. 58 func RunDatastoreEngineWithBridge(t testing.TB, engine string, bridgeNetworkName string) RunningEngineForTest { 59 switch engine { 60 case "memory": 61 require.Equal(t, "", bridgeNetworkName, "memory datastore does not support bridge networking") 62 return RunMemoryForTesting(t) 63 case "cockroachdb": 64 return RunCRDBForTesting(t, bridgeNetworkName) 65 case "postgres": 66 return RunPostgresForTesting(t, bridgeNetworkName, migrate.Head, version.MinimumSupportedPostgresVersion, false) 67 case "mysql": 68 return RunMySQLForTesting(t, bridgeNetworkName) 69 case "spanner": 70 return RunSpannerForTesting(t, bridgeNetworkName, migrate.Head) 71 default: 72 t.Fatalf("found missing engine for RunDatastoreEngine: %s", engine) 73 return nil 74 } 75 }