Pulse/internal/config/config_load_extra_test.go
rcourtman f2be9b60f0 test: fix TestLoad_Errors to provide valid encryption key
Test was creating .enc files without a valid encryption key, which
triggers the crypto safety check that prevents generating new keys
when encrypted data exists.
2026-01-04 18:02:39 +00:00

121 lines
3.5 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 OIDC
oidcPath := filepath.Join(tempDir, "oidc.enc")
require.NoError(t, os.WriteFile(oidcPath, []byte("corrupted"), 0644))
// 4. Corrupted Tokens
tokensPath := filepath.Join(tempDir, "api_tokens.json")
require.NoError(t, os.WriteFile(tokensPath, []byte("{invalid}"), 0644))
// 5. Corrupted Suppressions
suppressionsPath := filepath.Join(tempDir, "env_token_suppressions.json")
require.NoError(t, os.WriteFile(suppressionsPath, []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) {
cwd, _ := os.Getwd()
tempCWD := t.TempDir()
os.Chdir(tempCWD)
defer os.Chdir(cwd)
t.Setenv("PULSE_DATA_DIR", tempCWD)
require.NoError(t, os.WriteFile("mock.env", []byte("invalid="), 0644))
require.NoError(t, os.WriteFile("mock.env.local", []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)
}