MCPcopy
hub / github.com/redis/go-redis / SetupTestDatabaseAndFactory

Function SetupTestDatabaseAndFactory

maintnotifications/e2e/config_parser_test.go:1135–1251  ·  view source on GitHub ↗

SetupTestDatabaseAndFactory creates a database from environment config and returns both bdbID, factory, test mode config, and cleanup function This is the recommended way to setup tests as it ensures the client factory connects to the newly created database If REDIS_ENDPOINTS_CONFIG_PATH is not set

(t *testing.T, ctx context.Context, databaseName string)

Source from the content-addressed store, hash-verified

1133// bdbID, factory, testMode, cleanup := SetupTestDatabaseAndFactory(t, ctx, "standalone")
1134// defer cleanup()
1135func SetupTestDatabaseAndFactory(t *testing.T, ctx context.Context, databaseName string) (bdbID int, factory *ClientFactory, testMode *TestModeConfig, cleanup func()) {
1136 // Get environment config
1137 envConfig, err := GetEnvConfig()
1138 if err != nil {
1139 // No environment config - use Docker proxy setup
1140 t.Logf("No environment config found, using Docker proxy setup at 127.0.0.1:17000")
1141
1142 // Determine cluster mode based on database name
1143 // "standalone" creates a non-cluster client for testing FAILING_OVER/FAILED_OVER notifications
1144 isClusterMode := databaseName != "standalone"
1145
1146 // Create a simple Redis connection config for Docker proxy
1147 redisConfig := &RedisConnectionConfig{
1148 Host: "127.0.0.1", // Use 127.0.0.1 to force IPv4
1149 Port: 17000,
1150 Username: "",
1151 Password: "",
1152 TLS: false,
1153 BdbID: 0,
1154 IsClusterMode: isClusterMode,
1155 }
1156
1157 factory = NewClientFactory(redisConfig)
1158
1159 // Get proxy mock test mode config
1160 testMode = GetTestModeConfig()
1161
1162 // No-op cleanup since we're not creating a database
1163 cleanup = func() {
1164 factory.DestroyAll()
1165 }
1166
1167 return 0, factory, testMode, cleanup
1168 }
1169
1170 // Get database config from environment
1171 databasesConfig, err := GetDatabaseConfigFromEnv(envConfig.RedisEndpointsConfigPath)
1172 if err != nil {
1173 t.Fatalf("Failed to get database config: %v", err)
1174 }
1175
1176 // Get the specific database config
1177 var envDbConfig EnvDatabaseConfig
1178 var exists bool
1179 if databaseName == "" {
1180 // Get first database if no name provided
1181 for _, config := range databasesConfig {
1182 envDbConfig = config
1183 exists = true
1184 break
1185 }
1186 } else {
1187 envDbConfig, exists = databasesConfig[databaseName]
1188 }
1189
1190 if !exists {
1191 t.Fatalf("Database %s not found in configuration", databaseName)
1192 }

Tested by

no test coverage detected