package main import ( "context" "log" "os" "testing" "github.com/testcontainers/testcontainers-go" "github.com/testcontainers/testcontainers-go/wait" ) var ( testContainer testcontainers.Container ) func TestMain(m *testing.M) { ctx := context.Background() // Start PostgreSQL container log.Println("Starting PostgreSQL test container...") req := testcontainers.ContainerRequest{ Image: "postgres:15-alpine", ExposedPorts: []string{"5432/tcp"}, Env: map[string]string{ "POSTGRES_USER": "testuser", "POSTGRES_PASSWORD": "testpass", "POSTGRES_DB": "music_server_test", }, WaitingFor: wait.ForLog("database system is ready to accept connections"), } container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{ ContainerRequest: req, Started: true, }) if err != nil { log.Fatalf("Failed to start container: %v", err) } testContainer = container // Get container's host and port host, err := container.Endpoint(ctx, "") if err != nil { log.Fatalf("Failed to get container endpoint: %v", err) } log.Printf("PostgreSQL container running at: %s", host) // Set environment variables for all tests os.Setenv("DB_HOST", host) os.Setenv("DB_PORT", "5432") os.Setenv("DB_USERNAME", "testuser") os.Setenv("DB_PASSWORD", "testpass") os.Setenv("DB_NAME", "music_server_test") os.Setenv("MUSIC_PATH", "./testMusic") os.Setenv("CHARACTERS_PATH", "./testCharacters") os.Setenv("PORT", "8081") os.Setenv("LOG_LEVEL", "debug") os.Setenv("LOG_JSON", "false") // Run tests log.Println("Running integration tests...") exitCode := m.Run() // Cleanup log.Println("Stopping test container...") if err := container.Terminate(ctx); err != nil { log.Printf("Failed to terminate container: %v", err) } os.Exit(exitCode) } // TestDatabaseConnection verifies we can connect to the test database func TestDatabaseConnection(t *testing.T) { // This will be tested by the individual handler tests // Just verify env vars are set host := os.Getenv("DB_HOST") port := os.Getenv("DB_PORT") if host == "" || port == "" { t.Error("Database environment variables not set") } t.Logf("Database configuration: host=%s, port=%s", host, port) }