Files
dance-lessons-coach/pkg/bdd/suite.go
Gabriel Radureau 63d27cc35e
All checks were successful
CI/CD Pipeline / Build Docker Cache (push) Successful in 12s
CI/CD Pipeline / CI Pipeline (push) Successful in 8m44s
CI/CD Pipeline / Trigger Docker Push (push) Has been skipped
🐛 fix(bdd): revert PR #26 schema isolation, add cache flush + sequential test execution
PR #26 added BDD_SCHEMA_ISOLATION=true to CI but this creates per-scenario schemas
WITHOUT running migrations on them, causing 500 errors on user registration. This
PR reverts that and instead relies on:

1. The existing CleanupDatabase hook (truncates all tables AfterScenario)
2. Sequential test package execution (-p 1) to avoid contention between feature
   packages sharing the same Postgres DB

Plus defensive additions for future-proofing:
- pkg/server/server.go: GetCacheService() exposed for test cleanup
- pkg/bdd/testserver/server.go: cacheService field + FlushCache() method
- pkg/bdd/testserver/state_tracer.go: TraceStateCacheOperation
- pkg/bdd/suite.go: AfterScenario hook calls FlushCache()
- scripts/run-bdd-tests.sh: -p 1 added (sequential package execution)

Validation:
- AuthBDD alone: 5/5 PASS (was 0/5 with broken schema isolation)
- Full features/ via run-bdd-tests.sh: ALL PASS (auth, config, greet, health, jwt)

Out of scope (follow-up T12):
- Proper parallel BDD with schema migrations per scenario + dedicated connection
  pools. Required for scaling tests but architecturally significant. Tracked.

Co-Authored-By: Mistral Vibe (devstral-2 / mistral-medium-3.5) - cache flush diagnosis
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com> - root cause + revert
2026-05-03 16:28:21 +02:00

160 lines
5.0 KiB
Go

package bdd
import (
"fmt"
"os"
"strings"
"time"
"dance-lessons-coach/pkg/bdd/steps"
"dance-lessons-coach/pkg/bdd/testserver"
"github.com/cucumber/godog"
"github.com/rs/zerolog/log"
)
var sharedServer *testserver.Server
var sharedStepContext *steps.StepContext
// isCleanupLoggingEnabled returns true if BDD_ENABLE_CLEANUP_LOGS environment variable is set to "true"
func isCleanupLoggingEnabled() bool {
return os.Getenv("BDD_ENABLE_CLEANUP_LOGS") == "true"
}
// isSchemaIsolationEnabled returns true if BDD_SCHEMA_ISOLATION environment variable is set to "true"
func isSchemaIsolationEnabled() bool {
return os.Getenv("BDD_SCHEMA_ISOLATION") == "true"
}
func InitializeTestSuite(ctx *godog.TestSuiteContext) {
ctx.BeforeSuite(func() {
// Small delay to ensure any previous server instances are fully cleaned up
time.Sleep(50 * time.Millisecond)
sharedServer = testserver.NewServer()
if err := sharedServer.Start(); err != nil {
// Improved error message for port conflicts
if strings.Contains(err.Error(), "address already in use") {
panic(fmt.Sprintf("Port conflict: %v. Try running 'lsof -i :9191' and 'kill -9 <PID>' to free the port", err))
}
panic(fmt.Sprintf("Failed to start test server: %v", err))
}
})
sc := ctx.ScenarioContext()
sc.BeforeScenario(func(s *godog.Scenario) {
// Get feature name from environment - falls back to "bdd" for multi-feature tests
feature := os.Getenv("FEATURE")
if feature == "" {
feature = "bdd"
}
// Generate scenario key for state isolation
scenarioKey := s.Name
if s.Uri != "" {
scenarioKey = fmt.Sprintf("%s:%s", s.Uri, s.Name)
}
// Set scenario key on all step instances for state isolation
if sharedStepContext != nil {
steps.SetScenarioKeyForAllSteps(sharedStepContext, scenarioKey)
// Also clear state for this scenario to ensure clean start
steps.ClearScenarioState(scenarioKey)
}
if isCleanupLoggingEnabled() {
log.Info().Str("feature", feature).Str("scenario", s.Name).Msg("CLEANUP: Scenario starting")
}
// Trace scenario start
testserver.TraceStateScenarioStart(feature, scenarioKey)
// Setup schema isolation if enabled
if sharedServer != nil {
if err := sharedServer.SetupScenarioSchema(feature, scenarioKey); err != nil {
if isCleanupLoggingEnabled() {
log.Warn().Err(err).Str("feature", feature).Str("scenario", scenarioKey).Msg("ISOLATION: Failed to setup scenario schema")
}
}
}
})
sc.AfterScenario(func(s *godog.Scenario, err error) {
// Get feature name from environment - falls back to "bdd" for multi-feature tests
feature := os.Getenv("FEATURE")
if feature == "" {
feature = "bdd"
}
if isCleanupLoggingEnabled() {
log.Info().Str("scenario", s.Name).Str("status", "completed").Err(err).Msg("CLEANUP: Scenario completed")
}
// Trace scenario end
scenarioKey := s.Name
if s.Uri != "" {
scenarioKey = fmt.Sprintf("%s:%s", s.Uri, s.Name)
}
testserver.TraceStateScenarioEnd(feature, scenarioKey, err)
if sharedServer != nil {
// Teardown schema isolation if enabled
if teardownErr := sharedServer.TeardownScenarioSchema(); teardownErr != nil {
if isCleanupLoggingEnabled() {
log.Warn().Err(teardownErr).Msg("ISOLATION: Failed to teardown scenario schema")
}
}
// Reset JWT secrets after every scenario to prevent pollution
// Note: This is still needed for in-memory state even with schema isolation
if resetErr := sharedServer.ResetJWTSecrets(); resetErr != nil {
if isCleanupLoggingEnabled() {
log.Warn().Err(resetErr).Msg("CLEANUP: Failed to reset JWT secrets after scenario")
}
} else {
testserver.TraceStateJWTSecretOperation(feature, scenarioKey, "RESET", "ok")
}
// Flush cache after every scenario to prevent cache pollution
if flushErr := sharedServer.FlushCache(); flushErr != nil {
if isCleanupLoggingEnabled() {
log.Warn().Err(flushErr).Msg("CLEANUP: Failed to flush cache after scenario")
}
} else {
testserver.TraceStateCacheOperation(feature, scenarioKey, "FLUSH", "ok")
}
// Clean database after every scenario (only if schema isolation is disabled)
if !isSchemaIsolationEnabled() {
if cleanupErr := sharedServer.CleanupDatabase(); cleanupErr != nil {
if isCleanupLoggingEnabled() {
log.Warn().Err(cleanupErr).Msg("CLEANUP: Failed to cleanup database after scenario")
}
} else {
testserver.TraceStateDBCleanup(feature, scenarioKey, "all_tables")
}
}
}
})
ctx.AfterSuite(func() {
if sharedServer != nil {
// Final cleanup
if err := sharedServer.Stop(); err != nil {
log.Warn().Err(err).Msg("Failed to shutdown HTTP server")
}
time.Sleep(100 * time.Millisecond)
}
// Clear all scenario states
steps.ClearAllScenarioStates()
steps.CleanupAllTestConfigFiles()
})
}
func InitializeScenario(ctx *godog.ScenarioContext) {
client := testserver.NewClient(sharedServer)
// Create and store the step context for scenario isolation
sharedStepContext = steps.NewStepContext(client)
steps.InitializeAllSteps(ctx, client, sharedStepContext)
}