112 lines
2.7 KiB
Go
112 lines
2.7 KiB
Go
//go:build integration
|
|
|
|
package redis_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"testing"
|
|
"time"
|
|
|
|
goredis "github.com/redis/go-redis/v9"
|
|
|
|
"mtusi-messenger/internal/platform/cache"
|
|
"mtusi-messenger/internal/platform/tlsconfig"
|
|
)
|
|
|
|
const testRedisDB = 7
|
|
|
|
func redisEnv(key, fallback string) string {
|
|
if value := os.Getenv(key); value != "" {
|
|
return value
|
|
}
|
|
|
|
return fallback
|
|
}
|
|
|
|
func redisEnvInt(key string, fallback int) int {
|
|
value := os.Getenv(key)
|
|
if value == "" {
|
|
return fallback
|
|
}
|
|
|
|
parsed, err := strconv.Atoi(value)
|
|
if err != nil {
|
|
return fallback
|
|
}
|
|
|
|
return parsed
|
|
}
|
|
|
|
func TestMain(m *testing.M) {
|
|
identity, err := filepath.Abs("../../../secrets/lab7/pki/operator/current")
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "resolve operator identity:", err)
|
|
os.Exit(1)
|
|
}
|
|
for key, filename := range map[string]string{
|
|
"TLS_CA_FILE": "ca.crt", "TLS_CERT_FILE": "tls.crt",
|
|
"TLS_KEY_FILE": "tls.key", "TLS_CRL_FILE": "ca.crl",
|
|
} {
|
|
if os.Getenv(key) == "" {
|
|
if err := os.Setenv(key, filepath.Join(identity, filename)); err != nil {
|
|
fmt.Fprintln(os.Stderr, "set operator tls path:", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
}
|
|
if os.Getenv("TLS_VERSION_FILE") == "" {
|
|
if err := os.Setenv("TLS_VERSION_FILE", filepath.Join(filepath.Dir(identity), "current-version")); err != nil {
|
|
fmt.Fprintln(os.Stderr, "set operator tls selector:", err)
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
if cfg := testRedisConfig(); cfg.Username == "" || cfg.Password == "" || cfg.DB == 0 {
|
|
fmt.Fprintln(os.Stderr, "dedicated TEST_REDIS_USERNAME/PASSWORD and a non-production TEST_REDIS_DB are required")
|
|
os.Exit(1)
|
|
}
|
|
os.Exit(m.Run())
|
|
}
|
|
|
|
func testRedisConfig() cache.RedisConfig {
|
|
return cache.RedisConfig{
|
|
Host: redisEnv("TEST_REDIS_HOST", "localhost"),
|
|
Port: redisEnvInt("TEST_REDIS_PORT", 6379),
|
|
Username: redisEnv("TEST_REDIS_USERNAME", "operator"),
|
|
Password: os.Getenv("TEST_REDIS_PASSWORD"),
|
|
DB: redisEnvInt("TEST_REDIS_DB", testRedisDB),
|
|
MaxRetries: 3,
|
|
DialTimeout: 5 * time.Second,
|
|
ReadTimeout: 3 * time.Second,
|
|
WriteTimeout: 3 * time.Second,
|
|
PoolSize: 5,
|
|
}
|
|
}
|
|
|
|
func flushRedisDB(ctx context.Context) error {
|
|
cfg := testRedisConfig()
|
|
if cfg.Username == "" || cfg.Password == "" || cfg.DB == 0 {
|
|
return errors.New("redis fixture requires operator credentials and a non-production database")
|
|
}
|
|
clientTLS, err := tlsconfig.NewClientFromEnv(cfg.Host)
|
|
if err != nil {
|
|
return fmt.Errorf("redis fixture tls: %w", err)
|
|
}
|
|
|
|
client := goredis.NewClient(&goredis.Options{
|
|
Addr: cfg.Host + ":" + strconv.Itoa(cfg.Port),
|
|
Username: cfg.Username,
|
|
TLSConfig: clientTLS,
|
|
Password: cfg.Password,
|
|
DB: cfg.DB,
|
|
})
|
|
|
|
defer func() { _ = client.Close() }()
|
|
|
|
return client.FlushDB(ctx).Err()
|
|
}
|