351 lines
8.0 KiB
Go
351 lines
8.0 KiB
Go
package redisclient
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/alicebob/miniredis/v2"
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
func TestInit_DefaultValues(t *testing.T) {
|
|
// Save original values
|
|
originalHost := os.Getenv("REDIS_HOST")
|
|
originalPort := os.Getenv("REDIS_PORT")
|
|
originalPassword := os.Getenv("REDIS_PASSWORD")
|
|
originalRDB := RDB
|
|
|
|
defer func() {
|
|
os.Setenv("REDIS_HOST", originalHost)
|
|
os.Setenv("REDIS_PORT", originalPort)
|
|
os.Setenv("REDIS_PASSWORD", originalPassword)
|
|
RDB = originalRDB
|
|
}()
|
|
|
|
// Clear environment variables
|
|
os.Unsetenv("REDIS_HOST")
|
|
os.Unsetenv("REDIS_PORT")
|
|
os.Unsetenv("REDIS_PASSWORD")
|
|
|
|
// Start a mini redis server for testing
|
|
mr, err := miniredis.Run()
|
|
if err != nil {
|
|
t.Fatalf("Failed to start miniredis: %v", err)
|
|
}
|
|
defer mr.Close()
|
|
|
|
// Set environment to use miniredis
|
|
os.Setenv("REDIS_HOST", mr.Host())
|
|
os.Setenv("REDIS_PORT", mr.Port())
|
|
|
|
// Initialize Redis client
|
|
Init()
|
|
|
|
if RDB == nil {
|
|
t.Error("Expected RDB to be initialized")
|
|
}
|
|
|
|
// Test connection
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
defer cancel()
|
|
_, err = RDB.Ping(ctx).Result()
|
|
if err != nil {
|
|
t.Errorf("Expected successful ping, got error: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestInit_WithPassword(t *testing.T) {
|
|
// Save original values
|
|
originalHost := os.Getenv("REDIS_HOST")
|
|
originalPort := os.Getenv("REDIS_PORT")
|
|
originalPassword := os.Getenv("REDIS_PASSWORD")
|
|
originalRDB := RDB
|
|
|
|
defer func() {
|
|
os.Setenv("REDIS_HOST", originalHost)
|
|
os.Setenv("REDIS_PORT", originalPort)
|
|
os.Setenv("REDIS_PASSWORD", originalPassword)
|
|
RDB = originalRDB
|
|
}()
|
|
|
|
// Start a mini redis server
|
|
mr, err := miniredis.Run()
|
|
if err != nil {
|
|
t.Fatalf("Failed to start miniredis: %v", err)
|
|
}
|
|
defer mr.Close()
|
|
|
|
// Set password on miniredis
|
|
mr.RequireAuth("testpassword")
|
|
|
|
// Set environment variables
|
|
os.Setenv("REDIS_HOST", mr.Host())
|
|
os.Setenv("REDIS_PORT", mr.Port())
|
|
os.Setenv("REDIS_PASSWORD", "testpassword")
|
|
|
|
// Initialize Redis client
|
|
Init()
|
|
|
|
if RDB == nil {
|
|
t.Error("Expected RDB to be initialized")
|
|
}
|
|
|
|
// Test connection with password
|
|
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
|
defer cancel()
|
|
_, err = RDB.Ping(ctx).Result()
|
|
if err != nil {
|
|
t.Errorf("Expected successful ping with password, got error: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestInit_CustomHostAndPort(t *testing.T) {
|
|
// Save original values
|
|
originalHost := os.Getenv("REDIS_HOST")
|
|
originalPort := os.Getenv("REDIS_PORT")
|
|
originalPassword := os.Getenv("REDIS_PASSWORD")
|
|
originalRDB := RDB
|
|
|
|
defer func() {
|
|
os.Setenv("REDIS_HOST", originalHost)
|
|
os.Setenv("REDIS_PORT", originalPort)
|
|
os.Setenv("REDIS_PASSWORD", originalPassword)
|
|
RDB = originalRDB
|
|
}()
|
|
|
|
// Start a mini redis server
|
|
mr, err := miniredis.Run()
|
|
if err != nil {
|
|
t.Fatalf("Failed to start miniredis: %v", err)
|
|
}
|
|
defer mr.Close()
|
|
|
|
// Set custom host and port
|
|
os.Setenv("REDIS_HOST", mr.Host())
|
|
os.Setenv("REDIS_PORT", mr.Port())
|
|
os.Unsetenv("REDIS_PASSWORD")
|
|
|
|
// Initialize Redis client
|
|
Init()
|
|
|
|
if RDB == nil {
|
|
t.Error("Expected RDB to be initialized")
|
|
}
|
|
|
|
// Verify the client is configured correctly
|
|
opts := RDB.Options()
|
|
expectedAddr := mr.Addr()
|
|
if opts.Addr != expectedAddr {
|
|
t.Errorf("Expected address '%s', got '%s'", expectedAddr, opts.Addr)
|
|
}
|
|
}
|
|
|
|
func TestInit_ConnectionFailure(t *testing.T) {
|
|
// Save original values
|
|
originalHost := os.Getenv("REDIS_HOST")
|
|
originalPort := os.Getenv("REDIS_PORT")
|
|
originalPassword := os.Getenv("REDIS_PASSWORD")
|
|
originalRDB := RDB
|
|
|
|
defer func() {
|
|
os.Setenv("REDIS_HOST", originalHost)
|
|
os.Setenv("REDIS_PORT", originalPort)
|
|
os.Setenv("REDIS_PASSWORD", originalPassword)
|
|
RDB = originalRDB
|
|
}()
|
|
|
|
// Set to invalid host/port
|
|
os.Setenv("REDIS_HOST", "invalid-host-that-does-not-exist")
|
|
os.Setenv("REDIS_PORT", "9999")
|
|
os.Unsetenv("REDIS_PASSWORD")
|
|
|
|
// This should panic
|
|
defer func() {
|
|
if r := recover(); r == nil {
|
|
t.Error("Expected Init to panic on connection failure")
|
|
}
|
|
}()
|
|
|
|
Init()
|
|
}
|
|
|
|
func TestInit_SecuritySettings(t *testing.T) {
|
|
// Save original values
|
|
originalHost := os.Getenv("REDIS_HOST")
|
|
originalPort := os.Getenv("REDIS_PORT")
|
|
originalPassword := os.Getenv("REDIS_PASSWORD")
|
|
originalRDB := RDB
|
|
|
|
defer func() {
|
|
os.Setenv("REDIS_HOST", originalHost)
|
|
os.Setenv("REDIS_PORT", originalPort)
|
|
os.Setenv("REDIS_PASSWORD", originalPassword)
|
|
RDB = originalRDB
|
|
}()
|
|
|
|
// Start a mini redis server
|
|
mr, err := miniredis.Run()
|
|
if err != nil {
|
|
t.Fatalf("Failed to start miniredis: %v", err)
|
|
}
|
|
defer mr.Close()
|
|
|
|
os.Setenv("REDIS_HOST", mr.Host())
|
|
os.Setenv("REDIS_PORT", mr.Port())
|
|
os.Unsetenv("REDIS_PASSWORD")
|
|
|
|
// Initialize Redis client
|
|
Init()
|
|
|
|
// Verify security settings
|
|
opts := RDB.Options()
|
|
if !opts.DisableIndentity {
|
|
t.Error("Expected DisableIndentity to be true")
|
|
}
|
|
if opts.IdentitySuffix != "" {
|
|
t.Error("Expected IdentitySuffix to be empty")
|
|
}
|
|
}
|
|
|
|
func TestInit_DBNumber(t *testing.T) {
|
|
// Save original values
|
|
originalHost := os.Getenv("REDIS_HOST")
|
|
originalPort := os.Getenv("REDIS_PORT")
|
|
originalPassword := os.Getenv("REDIS_PASSWORD")
|
|
originalRDB := RDB
|
|
|
|
defer func() {
|
|
os.Setenv("REDIS_HOST", originalHost)
|
|
os.Setenv("REDIS_PORT", originalPort)
|
|
os.Setenv("REDIS_PASSWORD", originalPassword)
|
|
RDB = originalRDB
|
|
}()
|
|
|
|
// Start a mini redis server
|
|
mr, err := miniredis.Run()
|
|
if err != nil {
|
|
t.Fatalf("Failed to start miniredis: %v", err)
|
|
}
|
|
defer mr.Close()
|
|
|
|
os.Setenv("REDIS_HOST", mr.Host())
|
|
os.Setenv("REDIS_PORT", mr.Port())
|
|
os.Unsetenv("REDIS_PASSWORD")
|
|
|
|
// Initialize Redis client
|
|
Init()
|
|
|
|
// Verify DB is set to 0
|
|
opts := RDB.Options()
|
|
if opts.DB != 0 {
|
|
t.Errorf("Expected DB to be 0, got %d", opts.DB)
|
|
}
|
|
}
|
|
|
|
func TestRDB_GlobalVariable(t *testing.T) {
|
|
// Test that RDB is a package-level variable
|
|
originalRDB := RDB
|
|
defer func() { RDB = originalRDB }()
|
|
|
|
// Create a test client
|
|
testClient := redis.NewClient(&redis.Options{
|
|
Addr: "localhost:6379",
|
|
})
|
|
defer testClient.Close()
|
|
|
|
// Set the global variable
|
|
RDB = testClient
|
|
|
|
if RDB != testClient {
|
|
t.Error("Failed to set global RDB variable")
|
|
}
|
|
}
|
|
|
|
func TestInit_EnvironmentDefaults(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
redisHost string
|
|
redisPort string
|
|
redisPassword string
|
|
expectedHost string
|
|
expectedPort string
|
|
expectedPassword string
|
|
}{
|
|
{
|
|
name: "All defaults",
|
|
redisHost: "",
|
|
redisPort: "",
|
|
redisPassword: "",
|
|
expectedHost: "localhost",
|
|
expectedPort: "6379",
|
|
expectedPassword: "",
|
|
},
|
|
{
|
|
name: "Custom host, default port",
|
|
redisHost: "redis.example.com",
|
|
redisPort: "",
|
|
redisPassword: "",
|
|
expectedHost: "redis.example.com",
|
|
expectedPort: "6379",
|
|
expectedPassword: "",
|
|
},
|
|
{
|
|
name: "All custom",
|
|
redisHost: "redis.example.com",
|
|
redisPort: "6380",
|
|
redisPassword: "secret",
|
|
expectedHost: "redis.example.com",
|
|
expectedPort: "6380",
|
|
expectedPassword: "secret",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
// Setup
|
|
if tt.redisHost != "" {
|
|
os.Setenv("REDIS_HOST", tt.redisHost)
|
|
} else {
|
|
os.Unsetenv("REDIS_HOST")
|
|
}
|
|
if tt.redisPort != "" {
|
|
os.Setenv("REDIS_PORT", tt.redisPort)
|
|
} else {
|
|
os.Unsetenv("REDIS_PORT")
|
|
}
|
|
if tt.redisPassword != "" {
|
|
os.Setenv("REDIS_PASSWORD", tt.redisPassword)
|
|
} else {
|
|
os.Unsetenv("REDIS_PASSWORD")
|
|
}
|
|
|
|
// Get values (simulating what Init does)
|
|
host := os.Getenv("REDIS_HOST")
|
|
if host == "" {
|
|
host = "localhost"
|
|
}
|
|
port := os.Getenv("REDIS_PORT")
|
|
if port == "" {
|
|
port = "6379"
|
|
}
|
|
password := os.Getenv("REDIS_PASSWORD")
|
|
if password == "" {
|
|
password = ""
|
|
}
|
|
|
|
// Verify
|
|
if host != tt.expectedHost {
|
|
t.Errorf("Expected host '%s', got '%s'", tt.expectedHost, host)
|
|
}
|
|
if port != tt.expectedPort {
|
|
t.Errorf("Expected port '%s', got '%s'", tt.expectedPort, port)
|
|
}
|
|
if password != tt.expectedPassword {
|
|
t.Errorf("Expected password '%s', got '%s'", tt.expectedPassword, password)
|
|
}
|
|
})
|
|
}
|
|
}
|