Skip to content

Commit d89e3f4

Browse files
committed
use new database for client
1 parent 37c8c95 commit d89e3f4

File tree

1 file changed

+155
-11
lines changed

1 file changed

+155
-11
lines changed

maintnotifications/e2e/config_parser_test.go

Lines changed: 155 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ func (cf *ClientFactory) Create(key string, options *CreateClientOptions) (redis
312312
}
313313
}
314314

315+
fmt.Printf("Creating single client with options: %+v\n", clientOptions)
315316
client = redis.NewClient(clientOptions)
316317
}
317318

@@ -792,16 +793,91 @@ func SetupTestDatabaseWithConfig(t *testing.T, ctx context.Context, dbConfig Dat
792793
// bdbID, factory, cleanup := SetupTestDatabaseAndFactory(t, ctx, "standalone")
793794
// defer cleanup()
794795
func SetupTestDatabaseAndFactory(t *testing.T, ctx context.Context, databaseName string) (bdbID int, factory *ClientFactory, cleanup func()) {
795-
// Create the database
796-
bdbID, cleanupDB := SetupTestDatabaseFromEnv(t, ctx, databaseName)
796+
// Get environment config
797+
envConfig, err := GetEnvConfig()
798+
if err != nil {
799+
t.Fatalf("Failed to get environment config: %v", err)
800+
}
801+
802+
// Get database config from environment
803+
databasesConfig, err := GetDatabaseConfigFromEnv(envConfig.RedisEndpointsConfigPath)
804+
if err != nil {
805+
t.Fatalf("Failed to get database config: %v", err)
806+
}
807+
808+
// Get the specific database config
809+
var envDbConfig EnvDatabaseConfig
810+
var exists bool
811+
if databaseName == "" {
812+
// Get first database if no name provided
813+
for _, config := range databasesConfig {
814+
envDbConfig = config
815+
exists = true
816+
break
817+
}
818+
} else {
819+
envDbConfig, exists = databasesConfig[databaseName]
820+
}
797821

798-
// Create client factory that connects to the new database
799-
factory, err := CreateTestClientFactoryWithBdbID(databaseName, bdbID)
822+
if !exists {
823+
t.Fatalf("Database %s not found in configuration", databaseName)
824+
}
825+
826+
// Convert to DatabaseConfig to get the port
827+
dbConfig, err := ConvertEnvDatabaseConfigToFaultInjectorConfig(envDbConfig, fmt.Sprintf("e2e-test-%s-%d", databaseName, time.Now().Unix()))
800828
if err != nil {
801-
cleanupDB() // Clean up the database if factory creation fails
802-
t.Fatalf("Failed to create client factory for new database: %v", err)
829+
t.Fatalf("Failed to convert config: %v", err)
830+
}
831+
832+
// Create the database
833+
bdbID, cleanupDB := SetupTestDatabaseWithConfig(t, ctx, dbConfig)
834+
835+
// Update the environment config with the new database's connection details
836+
// The new database uses the port we specified in dbConfig
837+
newEnvConfig := envDbConfig
838+
newEnvConfig.BdbID = bdbID
839+
840+
// Update endpoints to point to the new database's port
841+
// Extract host from original endpoints
842+
var host string
843+
var scheme string
844+
if len(envDbConfig.Endpoints) > 0 {
845+
// Parse the first endpoint to get host and scheme
846+
endpoint := envDbConfig.Endpoints[0]
847+
if strings.HasPrefix(endpoint, "redis://") {
848+
scheme = "redis"
849+
host = strings.TrimPrefix(endpoint, "redis://")
850+
} else if strings.HasPrefix(endpoint, "rediss://") {
851+
scheme = "rediss"
852+
host = strings.TrimPrefix(endpoint, "rediss://")
853+
}
854+
// Remove port from host if present
855+
if colonIdx := strings.Index(host, ":"); colonIdx != -1 {
856+
host = host[:colonIdx]
857+
}
858+
}
859+
860+
// If we couldn't extract host, use localhost as fallback
861+
if host == "" {
862+
host = "localhost"
863+
}
864+
if scheme == "" {
865+
if envDbConfig.TLS {
866+
scheme = "rediss"
867+
} else {
868+
scheme = "redis"
869+
}
803870
}
804871

872+
// Construct new endpoint with the new database's port
873+
newEndpoint := fmt.Sprintf("%s://%s:%d", scheme, host, dbConfig.Port)
874+
newEnvConfig.Endpoints = []string{newEndpoint}
875+
876+
t.Logf("New database endpoint: %s (bdb_id: %d)", newEndpoint, bdbID)
877+
878+
// Create client factory with the updated config
879+
factory = NewClientFactory(newEnvConfig)
880+
805881
// Combined cleanup function
806882
cleanup = func() {
807883
factory.DestroyAll()
@@ -817,16 +893,84 @@ func SetupTestDatabaseAndFactory(t *testing.T, ctx context.Context, databaseName
817893
// bdbID, factory, cleanup := SetupTestDatabaseAndFactoryWithConfig(t, ctx, "standalone", dbConfig)
818894
// defer cleanup()
819895
func SetupTestDatabaseAndFactoryWithConfig(t *testing.T, ctx context.Context, databaseName string, dbConfig DatabaseConfig) (bdbID int, factory *ClientFactory, cleanup func()) {
896+
// Get environment config to use as template for connection details
897+
envConfig, err := GetEnvConfig()
898+
if err != nil {
899+
t.Fatalf("Failed to get environment config: %v", err)
900+
}
901+
902+
// Get database config from environment
903+
databasesConfig, err := GetDatabaseConfigFromEnv(envConfig.RedisEndpointsConfigPath)
904+
if err != nil {
905+
t.Fatalf("Failed to get database config: %v", err)
906+
}
907+
908+
// Get the specific database config as template
909+
var envDbConfig EnvDatabaseConfig
910+
var exists bool
911+
if databaseName == "" {
912+
// Get first database if no name provided
913+
for _, config := range databasesConfig {
914+
envDbConfig = config
915+
exists = true
916+
break
917+
}
918+
} else {
919+
envDbConfig, exists = databasesConfig[databaseName]
920+
}
921+
922+
if !exists {
923+
t.Fatalf("Database %s not found in configuration", databaseName)
924+
}
925+
820926
// Create the database
821927
bdbID, cleanupDB := SetupTestDatabaseWithConfig(t, ctx, dbConfig)
822928

823-
// Create client factory that connects to the new database
824-
factory, err := CreateTestClientFactoryWithBdbID(databaseName, bdbID)
825-
if err != nil {
826-
cleanupDB() // Clean up the database if factory creation fails
827-
t.Fatalf("Failed to create client factory for new database: %v", err)
929+
// Update the environment config with the new database's connection details
930+
newEnvConfig := envDbConfig
931+
newEnvConfig.BdbID = bdbID
932+
933+
// Update endpoints to point to the new database's port
934+
// Extract host from original endpoints
935+
var host string
936+
var scheme string
937+
if len(envDbConfig.Endpoints) > 0 {
938+
// Parse the first endpoint to get host and scheme
939+
endpoint := envDbConfig.Endpoints[0]
940+
if strings.HasPrefix(endpoint, "redis://") {
941+
scheme = "redis"
942+
host = strings.TrimPrefix(endpoint, "redis://")
943+
} else if strings.HasPrefix(endpoint, "rediss://") {
944+
scheme = "rediss"
945+
host = strings.TrimPrefix(endpoint, "rediss://")
946+
}
947+
// Remove port from host if present
948+
if colonIdx := strings.Index(host, ":"); colonIdx != -1 {
949+
host = host[:colonIdx]
950+
}
828951
}
829952

953+
// If we couldn't extract host, use localhost as fallback
954+
if host == "" {
955+
host = "localhost"
956+
}
957+
if scheme == "" {
958+
if envDbConfig.TLS {
959+
scheme = "rediss"
960+
} else {
961+
scheme = "redis"
962+
}
963+
}
964+
965+
// Construct new endpoint with the new database's port
966+
newEndpoint := fmt.Sprintf("%s://%s:%d", scheme, host, dbConfig.Port)
967+
newEnvConfig.Endpoints = []string{newEndpoint}
968+
969+
t.Logf("New database endpoint: %s (bdb_id: %d)", newEndpoint, bdbID)
970+
971+
// Create client factory with the updated config
972+
factory = NewClientFactory(newEnvConfig)
973+
830974
// Combined cleanup function
831975
cleanup = func() {
832976
factory.DestroyAll()

0 commit comments

Comments
 (0)