Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions e2e/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ go 1.25.2
require (
github.com/Infisical/infisical-merge v0.0.0
github.com/compose-spec/compose-go/v2 v2.9.0
github.com/creack/pty v1.1.24
github.com/docker/compose/v2 v2.40.2
github.com/docker/docker v28.5.1+incompatible
github.com/docker/go-connections v0.6.0
github.com/go-faker/faker/v4 v4.7.0
github.com/jackc/pgx/v5 v5.7.6
github.com/oapi-codegen/oapi-codegen/v2 v2.5.1
github.com/oapi-codegen/runtime v1.1.2
github.com/redis/go-redis/v9 v9.17.2
github.com/stretchr/testify v1.11.1
github.com/testcontainers/testcontainers-go v0.40.0
github.com/testcontainers/testcontainers-go/modules/compose v0.40.0
Expand Down Expand Up @@ -70,16 +74,14 @@ require (
github.com/denisbrodbeck/machineid v1.0.1 // indirect
github.com/dgraph-io/badger/v3 v3.2103.5 // indirect
github.com/dgraph-io/ristretto v0.1.1 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/distribution/reference v0.6.0 // indirect
github.com/docker/buildx v0.29.1 // indirect
github.com/docker/cli v28.5.1+incompatible // indirect
github.com/docker/cli-docs-tool v0.10.0 // indirect
github.com/docker/compose/v2 v2.40.2 // indirect
github.com/docker/distribution v2.8.3+incompatible // indirect
github.com/docker/docker v28.5.1+incompatible // indirect
github.com/docker/docker-credential-helpers v0.9.3 // indirect
github.com/docker/go v1.5.1-1.0.20160303222718-d30aec9fd63c // indirect
github.com/docker/go-connections v0.6.0 // indirect
github.com/docker/go-metrics v0.0.1 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 // indirect
Expand All @@ -88,6 +90,7 @@ require (
github.com/ebitengine/purego v0.8.4 // indirect
github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/emirpasic/gods v1.12.0 // indirect
github.com/fatih/color v1.17.0 // indirect
github.com/fatih/semgroup v1.2.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
Expand Down Expand Up @@ -145,7 +148,8 @@ require (
github.com/infisical/go-sdk v0.6.1 // indirect
github.com/infisical/infisical-kmip v0.3.17 // indirect
github.com/inhies/go-bytesize v0.0.0-20220417184213-4913239db9cf // indirect
github.com/jackc/pgx/v5 v5.7.6 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jedib0t/go-pretty v4.3.0+incompatible // indirect
github.com/jonboulle/clockwork v0.5.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
Expand Down Expand Up @@ -233,6 +237,7 @@ require (
github.com/shopspring/decimal v1.4.0 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 // indirect
github.com/smallnest/resp3 v0.0.0-20251228151914-4f2fa7427e69 // indirect
github.com/sony/gobreaker v0.5.0 // indirect
github.com/speakeasy-api/jsonpath v0.6.0 // indirect
github.com/speakeasy-api/openapi-overlay v0.10.2 // indirect
Expand Down
14 changes: 14 additions & 0 deletions e2e/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ github.com/bitly/go-simplejson v0.5.0/go.mod h1:cXHtHw4XUPsvGaxgjIAn8PhEWG9NfngE
github.com/bketelsen/crypt v0.0.4/go.mod h1:aI6NrJ0pMGgvZKL1iVgXLnfIFJtfV+bKCoqOes/6LfM=
github.com/bmatcuk/doublestar v1.1.1/go.mod h1:UD6OnuiIn0yFxxA2le/rnRU1G4RaI4UvFv1sNto9p6w=
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869/go.mod h1:Ekp36dRnpXw/yCqJaO+ZrUyxD+3VXMFFr56k5XYrpB4=
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
github.com/buger/goterm v1.0.4 h1:Z9YvGmOih81P0FbVtEYTFF6YsSgxSUKEhf/f9bTMXbY=
github.com/buger/goterm v1.0.4/go.mod h1:HiFWV3xnkolgrBV3mY8m0X0Pumt4zg4QhbdOzQtB8tE=
github.com/bugsnag/bugsnag-go v1.0.5-0.20150529004307-13fd6b8acda0 h1:s7+5BfS4WFJoVF9pnB8kBk03S7pZXRdKamnV0FOl5Sc=
Expand Down Expand Up @@ -235,6 +239,8 @@ github.com/dgraph-io/ristretto v0.1.1 h1:6CWw5tJNgpegArSHpNHJKldNeq03FQCwYvfMVWa
github.com/dgraph-io/ristretto v0.1.1/go.mod h1:S1GPSBCYCIhmVNfcth17y2zZtQT6wzkzgwUve0VDWWA=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA=
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk=
github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI=
Expand Down Expand Up @@ -280,6 +286,8 @@ github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203 h1:XBBHcIb256gUJ
github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203/go.mod h1:E1jcSv8FaEny+OP/5k9UxZVw9YFWGj7eI4KR/iOBqCg=
github.com/emicklei/go-restful/v3 v3.11.0 h1:rAQeMHw1c7zTmncogyy8VvRZwtkmkZ4FxERmMY4rD+g=
github.com/emicklei/go-restful/v3 v3.11.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/emirpasic/gods v1.12.0 h1:QAUIPSaCu4G+POclxeqb3F+WPpdKqFGlw36+yOzGlrg=
github.com/emirpasic/gods v1.12.0/go.mod h1:YfzfFFoVP/catgzJb4IKIqXjX78Ha8FMSDh3ymbK86o=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down Expand Up @@ -537,6 +545,8 @@ github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7Ulw
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.7.6 h1:rWQc5FwZSPX58r1OQmkuaNicxdmExaEz5A2DO2hUuTk=
github.com/jackc/pgx/v5 v5.7.6/go.mod h1:aruU7o91Tc2q2cFp5h4uP3f6ztExVpyVv88Xl/8Vl8M=
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jedib0t/go-pretty v4.3.0+incompatible h1:CGs8AVhEKg/n9YbUenWmNStRW2PHJzaeDodcfvRAbIo=
github.com/jedib0t/go-pretty v4.3.0+incompatible/go.mod h1:XemHduiw8R651AF9Pt4FwCTKeG3oo7hrHJAoznj9nag=
github.com/jinzhu/gorm v0.0.0-20170222002820-5409931a1bb8 h1:CZkYfurY6KGhVtlalI4QwQ6T0Cu6iuY3e0x5RLu96WE=
Expand Down Expand Up @@ -804,6 +814,8 @@ github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0leargg
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
github.com/quic-go/quic-go v0.54.1 h1:4ZAWm0AhCb6+hE+l5Q1NAL0iRn/ZrMwqHRGQiFwj2eg=
github.com/quic-go/quic-go v0.54.1/go.mod h1:e68ZEaCdyviluZmy44P6Iey98v/Wfz6HCjQEm+l8zTY=
github.com/redis/go-redis/v9 v9.17.2 h1:P2EGsA4qVIM3Pp+aPocCJ7DguDHhqrXNhVcEp4ViluI=
github.com/redis/go-redis/v9 v9.17.2/go.mod h1:u410H11HMLoB+TP67dz8rL9s6QW2j76l0//kSOd3370=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
Expand Down Expand Up @@ -843,6 +855,8 @@ github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EEf9cgbU6AtGPK4CTG3Zf6CKMNqf0MHTggAUA=
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog=
github.com/smallnest/resp3 v0.0.0-20251228151914-4f2fa7427e69 h1:AkDv2coi+ZsMlEp/6V21FWxdswSIEzqflgJ6snIQG+U=
github.com/smallnest/resp3 v0.0.0-20251228151914-4f2fa7427e69/go.mod h1:cmfXTZVXEA7xFOYcGnpKp2VeFf6FUHmxdKQHVNE6BXY=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/sony/gobreaker v0.5.0 h1:dRCvqm0P490vZPmy7ppEk2qCnCieBooFJ+YoXGYB+yg=
Expand Down
162 changes: 162 additions & 0 deletions e2e/packages/client/reset_db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package client

import (
"context"
"fmt"
"log/slog"
"strings"

"github.com/jackc/pgx/v5"
)

// ServicePortProvider provides a way to get the mapped port for a service
type ServicePortProvider interface {
GetServicePort(ctx context.Context, serviceName string, internalPort string) (string, error)
}

// DatabaseConfig holds database connection configuration
type DatabaseConfig struct {
User string
Password string
Database string
Host string
Port string
}

// DefaultDatabaseConfig returns the default database configuration
func DefaultDatabaseConfig() DatabaseConfig {
return DatabaseConfig{
User: "infisical",
Password: "infisical",
Database: "infisical",
Host: "localhost",
Port: "5432",
}
}

// ResetDBOptions holds options for resetting the database
type ResetDBOptions struct {
SkipTables map[string]struct{} // Tables to skip when truncating (e.g., migrations)
DBConfig DatabaseConfig
}

// DefaultResetDBOptions returns default options for resetting the database
func DefaultResetDBOptions() ResetDBOptions {
return ResetDBOptions{
SkipTables: map[string]struct{}{
"public.infisical_migrations": {},
"public.infisical_migrations_lock": {},
},
DBConfig: DefaultDatabaseConfig(),
}
}

// ResetDB resets the PostgreSQL database.
// It accepts a port provider to get service ports, and options to configure the reset behavior.
func ResetDB(ctx context.Context, opts ...func(*ResetDBOptions)) error {
options := DefaultResetDBOptions()
for _, opt := range opts {
opt(&options)
}

// Reset PostgreSQL database
if err := resetPostgresDB(ctx, options); err != nil {
return fmt.Errorf("failed to reset PostgreSQL database: %w", err)
}

return nil
}

// resetPostgresDB resets the PostgreSQL database by truncating all tables (except skipped ones)
// and inserting a default super_admin record.
func resetPostgresDB(ctx context.Context, opts ResetDBOptions) error {
// Build connection string using config
connStr := fmt.Sprintf("postgresql://%s:%s@%s:%s/%s",
opts.DBConfig.User,
opts.DBConfig.Password,
opts.DBConfig.Host,
opts.DBConfig.Port,
opts.DBConfig.Database,
)

conn, err := pgx.Connect(ctx, connStr)
if err != nil {
slog.Error("Unable to connect to database", "err", err)
return err
}
defer conn.Close(ctx)

// Get all tables
query := `
SELECT table_schema, table_name
FROM information_schema.tables
WHERE table_type = 'BASE TABLE'
AND table_schema NOT IN ('pg_catalog', 'information_schema')
ORDER BY table_schema, table_name;
`

rows, err := conn.Query(ctx, query)
if err != nil {
slog.Error("Unable to execute query", "query", query, "err", err)
return err
}
defer rows.Close()

tables := make([]string, 0)
for rows.Next() {
var schema, table string
if err := rows.Scan(&schema, &table); err != nil {
slog.Error("Scan failed", "error", err)
return err
}
tables = append(tables, fmt.Sprintf("%s.%s", schema, table))
}
if err := rows.Err(); err != nil {
slog.Error("Row iteration error", "error", err)
return err
}

// Build truncate statements
var builder strings.Builder
for _, table := range tables {
if _, ok := opts.SkipTables[table]; ok {
continue
}
builder.WriteString(fmt.Sprintf("TRUNCATE TABLE %s RESTART IDENTITY CASCADE;\n", table))
}

truncateQuery := builder.String()
if truncateQuery != "" {
_, err = conn.Exec(ctx, truncateQuery)
if err != nil {
slog.Error("Truncate failed", "error", err)
return err
}
slog.Info("Truncate all tables successfully")
}

// Insert default super_admin record
_, err = conn.Exec(ctx,
`INSERT INTO public.super_admin ("id", "fipsEnabled", "initialized", "allowSignUp") VALUES ($1, $2, $3, $4)`,
"00000000-0000-0000-0000-000000000000", true, false, true)
if err != nil {
slog.Error("Failed to insert super_admin", "error", err)
return err
}

return nil
}

// WithSkipTables sets the tables to skip when truncating
func WithSkipTables(tables map[string]struct{}) func(*ResetDBOptions) {
return func(opts *ResetDBOptions) {
opts.SkipTables = tables
}
}

// WithDatabaseConfig sets the database configuration
func WithDatabaseConfig(config DatabaseConfig) func(*ResetDBOptions) {
return func(opts *ResetDBOptions) {
opts.DBConfig = config
}
}
83 changes: 83 additions & 0 deletions e2e/packages/client/reset_redis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package client

import (
"context"
"fmt"
"log/slog"

"github.com/redis/go-redis/v9"
)

// RedisConfig holds Redis connection configuration
type RedisConfig struct {
Host string
Port int
Password string
}

// DefaultRedisConfig returns the default Redis configuration
func DefaultRedisConfig() RedisConfig {
return RedisConfig{
Host: "localhost",
Port: 6379,
Password: "",
}
}

// ResetRedisOptions holds options for resetting Redis
type ResetRedisOptions struct {
RedisConfig RedisConfig
}

// DefaultResetRedisOptions returns default options for resetting Redis
func DefaultResetRedisOptions() ResetRedisOptions {
return ResetRedisOptions{
RedisConfig: DefaultRedisConfig(),
}
}

// ResetRedis resets the Redis database by flushing all keys.
// It accepts a port provider to get service ports, and options to configure the reset behavior.
func ResetRedis(ctx context.Context, opts ...func(*ResetRedisOptions)) error {
options := DefaultResetRedisOptions()
for _, opt := range opts {
opt(&options)
}

return resetRedisDB(ctx, options)
}

// resetRedisDB resets the Redis database by flushing all keys.
func resetRedisDB(ctx context.Context, opts ResetRedisOptions) error {
addr := fmt.Sprintf("%s:%d", opts.RedisConfig.Host, opts.RedisConfig.Port)
rdb := redis.NewClient(&redis.Options{
Addr: addr,
Password: opts.RedisConfig.Password,
})
defer func() {
_ = rdb.Close()
}()

// Test the connection
pong, err := rdb.Ping(ctx).Result()
if err != nil {
return fmt.Errorf("failed to connect to Redis: %w", err)
}
slog.Info("Connected to Redis", "pong", pong)

// Clear all keys in the current database
err = rdb.FlushAll(ctx).Err()
if err != nil {
return fmt.Errorf("failed to flush Redis database: %w", err)
}
slog.Info("All keys cleared successfully from Redis database")

return nil
}

// WithRedisConfig sets the Redis configuration
func WithRedisConfig(config RedisConfig) func(*ResetRedisOptions) {
return func(opts *ResetRedisOptions) {
opts.RedisConfig = config
}
}
Loading
Loading