diff --git a/backend/v3/storage/database/events_testing/events_test.go b/backend/v3/storage/database/events_testing/events_test.go new file mode 100644 index 0000000000..b91aa2dc63 --- /dev/null +++ b/backend/v3/storage/database/events_testing/events_test.go @@ -0,0 +1,68 @@ +//go:build integration + +package events_test + +import ( + "context" + "os" + "testing" + "time" + + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" + "github.com/zitadel/zitadel/backend/v3/storage/database" + "github.com/zitadel/zitadel/backend/v3/storage/database/dialect/postgres" + "github.com/zitadel/zitadel/internal/integration" + mgmt "github.com/zitadel/zitadel/pkg/grpc/management" + "github.com/zitadel/zitadel/pkg/grpc/org/v2" + "github.com/zitadel/zitadel/pkg/grpc/system" +) +const ConnString = "host=localhost port=5432 user=zitadel dbname=zitadel sslmode=disable" + +var ( + dbPool *pgxpool.Pool + CTX context.Context + Instance *integration.Instance + SystemClient system.SystemServiceClient + OrgClient org.OrganizationServiceClient + MgmtClient mgmt.ManagementServiceClient +) + +var pool database.Pool + +func TestMain(m *testing.M) { + os.Exit(func() int { + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Minute) + defer cancel() + + Instance = integration.NewInstance(ctx) + + CTX = Instance.WithAuthorization(ctx, integration.UserTypeIAMOwner) + SystemClient = integration.SystemClient() + OrgClient = Instance.Client.OrgV2 + MgmtClient = Instance.Client.Mgmt + + var err error + dbConfig, err := pgxpool.ParseConfig(ConnString) + if err != nil { + panic(err) + } + dbConfig.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error { + orgState, err := conn.LoadType(ctx, "zitadel.organization_state") + if err != nil { + return err + } + conn.TypeMap().RegisterType(orgState) + return nil + } + + dbPool, err = pgxpool.NewWithConfig(context.Background(), dbConfig) + if err != nil { + panic(err) + } + + pool = postgres.PGxPool(dbPool) + + return m.Run() + }()) +} diff --git a/backend/v3/storage/database/events_testing/instance_test.go b/backend/v3/storage/database/events_testing/instance_test.go index 607dc3991d..9d7a293a22 100644 --- a/backend/v3/storage/database/events_testing/instance_test.go +++ b/backend/v3/storage/database/events_testing/instance_test.go @@ -3,76 +3,19 @@ package events_test import ( - "context" - "os" "testing" "time" "github.com/brianvoe/gofakeit/v6" - "github.com/jackc/pgx/v5" - "github.com/jackc/pgx/v5/pgxpool" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" - "github.com/zitadel/zitadel/pkg/grpc/org/v2" "github.com/zitadel/zitadel/backend/v3/storage/database" - "github.com/zitadel/zitadel/backend/v3/storage/database/dialect/postgres" "github.com/zitadel/zitadel/backend/v3/storage/database/repository" "github.com/zitadel/zitadel/internal/integration" - mgmt "github.com/zitadel/zitadel/pkg/grpc/management" "github.com/zitadel/zitadel/pkg/grpc/system" ) -const ConnString = "host=localhost port=5432 user=zitadel dbname=zitadel sslmode=disable" - -var ( - dbPool *pgxpool.Pool - CTX context.Context - Instance *integration.Instance - SystemClient system.SystemServiceClient - OrgClient org.OrganizationServiceClient - MgmtClient mgmt.ManagementServiceClient -) - -var pool database.Pool - -func TestMain(m *testing.M) { - os.Exit(func() int { - ctx, cancel := context.WithTimeout(context.Background(), 15*time.Minute) - defer cancel() - - Instance = integration.NewInstance(ctx) - - CTX = Instance.WithAuthorization(ctx, integration.UserTypeIAMOwner) - SystemClient = integration.SystemClient() - OrgClient = Instance.Client.OrgV2 - MgmtClient = Instance.Client.Mgmt - - var err error - dbConfig, err := pgxpool.ParseConfig(ConnString) - if err != nil { - panic(err) - } - dbConfig.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error { - orgState, err := conn.LoadType(ctx, "zitadel.organization_state") - if err != nil { - return err - } - conn.TypeMap().RegisterType(orgState) - return nil - } - - dbPool, err = pgxpool.NewWithConfig(context.Background(), dbConfig) - if err != nil { - panic(err) - } - - pool = postgres.PGxPool(dbPool) - - return m.Run() - }()) -} - func TestServer_TestInstanceAddReduces(t *testing.T) { instanceName := gofakeit.Name() beforeCreate := time.Now() diff --git a/backend/v3/storage/database/events_testing/organization_test.go b/backend/v3/storage/database/events_testing/organization_test.go index ee86c26b40..d2d2447632 100644 --- a/backend/v3/storage/database/events_testing/organization_test.go +++ b/backend/v3/storage/database/events_testing/organization_test.go @@ -171,6 +171,9 @@ func TestServer_TestOrganizationRemoveReduces(t *testing.T) { ) require.NoError(t, err) + if organization == nil { + require.Fail(t, "this error is here becuase of a race condition") + } require.Equal(t, orgName, organization.Name) }, retryDuration, tick)