Pulse/internal/config/config_load_extra_test.go
2026-03-18 16:06:30 +00:00

108 lines
3 KiB
Go

package config
import (
"encoding/base64"
"os"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLoad_MoreOverrides(t *testing.T) {
tempDir := t.TempDir()
t.Setenv("PULSE_DATA_DIR", tempDir)
// Test discrete values for coverage
t.Setenv("BACKUP_POLLING_INTERVAL", "60") // seconds
t.Setenv("PVE_POLLING_INTERVAL", "20") // seconds
t.Setenv("ENABLE_BACKUP_POLLING", "off")
t.Setenv("ADAPTIVE_POLLING_ENABLED", "no")
cfg, err := Load()
require.NoError(t, err)
assert.Equal(t, 60*time.Second, cfg.BackupPollingInterval)
assert.Equal(t, 20*time.Second, cfg.PVEPollingInterval)
assert.False(t, cfg.EnableBackupPolling)
assert.False(t, cfg.AdaptivePollingEnabled)
// Test durations
t.Setenv("BACKUP_POLLING_INTERVAL", "2m")
t.Setenv("PVE_POLLING_INTERVAL", "30s")
cfg, _ = Load()
assert.Equal(t, 2*time.Minute, cfg.BackupPollingInterval)
assert.Equal(t, 30*time.Second, cfg.PVEPollingInterval)
}
func TestLoad_GuestMetadataOverrides(t *testing.T) {
tempDir := t.TempDir()
t.Setenv("PULSE_DATA_DIR", tempDir)
t.Setenv("GUEST_METADATA_REFRESH_JITTER", "10s")
cfg, err := Load()
require.NoError(t, err)
assert.Equal(t, 10*time.Second, cfg.GuestMetadataRefreshJitter)
}
func TestLoad_OutboundIP(t *testing.T) {
// Calling getOutboundIP for coverage
ip := getOutboundIP()
assert.NotEmpty(t, ip)
}
func TestLoad_Errors(t *testing.T) {
tempDir := t.TempDir()
t.Setenv("PULSE_DATA_DIR", tempDir)
// First create encryption key so crypto doesn't fail when it sees .enc files
// Key must be base64-encoded 32-byte value
keyPath := filepath.Join(tempDir, ".encryption.key")
key := make([]byte, 32)
for i := range key {
key[i] = byte(i) // Non-zero key
}
encoded := base64.StdEncoding.EncodeToString(key)
require.NoError(t, os.WriteFile(keyPath, []byte(encoded), 0600))
// 1. Corrupted Nodes
nodesPath := filepath.Join(tempDir, "nodes.enc")
require.NoError(t, os.WriteFile(nodesPath, []byte("corrupted"), 0644))
// 2. Corrupted System
systemPath := filepath.Join(tempDir, "system.json")
require.NoError(t, os.WriteFile(systemPath, []byte("{invalid}"), 0644))
// 3. Corrupted Tokens
tokensPath := filepath.Join(tempDir, "api_tokens.json")
require.NoError(t, os.WriteFile(tokensPath, []byte("{invalid}"), 0644))
// Load should still proceed with defaults and log warnings
cfg, err := Load()
assert.NoError(t, err)
assert.NotNil(t, cfg)
}
func TestLoad_MockEnvErrors(t *testing.T) {
tempDir := t.TempDir()
t.Setenv("PULSE_DATA_DIR", tempDir)
require.NoError(t, os.WriteFile(filepath.Join(tempDir, ".env"), []byte("invalid="), 0644))
cfg, err := Load()
assert.NoError(t, err)
assert.NotNil(t, cfg)
}
func TestLoad_SystemJsonDirError(t *testing.T) {
tempDir := t.TempDir()
// Make system.json a directory to trigger SaveSystemSettings error during creation
systemPath := filepath.Join(tempDir, "system.json")
require.NoError(t, os.Mkdir(systemPath, 0755))
t.Setenv("PULSE_DATA_DIR", tempDir)
cfg, err := Load()
assert.NoError(t, err)
assert.NotNil(t, cfg)
}