|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +func TestDefaultConfig(t *testing.T) { |
| 10 | + config := DefaultConfig() |
| 11 | + |
| 12 | + if config == nil { |
| 13 | + t.Fatal("DefaultConfig() returned nil") |
| 14 | + } |
| 15 | + |
| 16 | + // verify port config |
| 17 | + if config.Port.Range.Start != 8000 { |
| 18 | + t.Errorf("Port.Range.Start = %d, want 8000", config.Port.Range.Start) |
| 19 | + } |
| 20 | + if config.Port.Range.End != 9999 { |
| 21 | + t.Errorf("Port.Range.End = %d, want 9999", config.Port.Range.End) |
| 22 | + } |
| 23 | + if !config.Port.ExcludePrivileged { |
| 24 | + t.Error("Port.ExcludePrivileged = false, want true") |
| 25 | + } |
| 26 | + |
| 27 | + // verify file config |
| 28 | + if config.File.ComposeFile != "compose.yml" { |
| 29 | + t.Errorf("File.ComposeFile = %s, want compose.yml", config.File.ComposeFile) |
| 30 | + } |
| 31 | + if config.File.OverrideFile != "compose.override.yml" { |
| 32 | + t.Errorf("File.OverrideFile = %s, want compose.override.yml", config.File.OverrideFile) |
| 33 | + } |
| 34 | + if !config.File.BackupEnabled { |
| 35 | + t.Error("File.BackupEnabled = false, want true") |
| 36 | + } |
| 37 | + |
| 38 | + // verify watcher config |
| 39 | + if config.Watcher.Interval != 5*time.Second { |
| 40 | + t.Errorf("Watcher.Interval = %v, want 5s", config.Watcher.Interval) |
| 41 | + } |
| 42 | + if config.Watcher.MaxRetries != 3 { |
| 43 | + t.Errorf("Watcher.MaxRetries = %d, want 3", config.Watcher.MaxRetries) |
| 44 | + } |
| 45 | + |
| 46 | + // verify log config |
| 47 | + if config.Log.Level != "info" { |
| 48 | + t.Errorf("Log.Level = %s, want info", config.Log.Level) |
| 49 | + } |
| 50 | + if config.Log.Format != "text" { |
| 51 | + t.Errorf("Log.Format = %s, want text", config.Log.Format) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestDefaultPortConfig(t *testing.T) { |
| 56 | + config := DefaultPortConfig() |
| 57 | + |
| 58 | + if config.Range.Start != 8000 { |
| 59 | + t.Errorf("Range.Start = %d, want 8000", config.Range.Start) |
| 60 | + } |
| 61 | + if config.Range.End != 9999 { |
| 62 | + t.Errorf("Range.End = %d, want 9999", config.Range.End) |
| 63 | + } |
| 64 | + |
| 65 | + expectedReserved := []int{8080, 8443, 9000, 9090} |
| 66 | + if !reflect.DeepEqual(config.Reserved, expectedReserved) { |
| 67 | + t.Errorf("Reserved = %v, want %v", config.Reserved, expectedReserved) |
| 68 | + } |
| 69 | + |
| 70 | + if !config.ExcludePrivileged { |
| 71 | + t.Error("ExcludePrivileged = false, want true") |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +func TestDefaultFileConfig(t *testing.T) { |
| 76 | + config := DefaultFileConfig() |
| 77 | + |
| 78 | + if config.ComposeFile != "compose.yml" { |
| 79 | + t.Errorf("ComposeFile = %s, want compose.yml", config.ComposeFile) |
| 80 | + } |
| 81 | + if config.OverrideFile != "compose.override.yml" { |
| 82 | + t.Errorf("OverrideFile = %s, want compose.override.yml", config.OverrideFile) |
| 83 | + } |
| 84 | + if !config.BackupEnabled { |
| 85 | + t.Error("BackupEnabled = false, want true") |
| 86 | + } |
| 87 | + if config.BackupDir != ".gopose/backups" { |
| 88 | + t.Errorf("BackupDir = %s, want .gopose/backups", config.BackupDir) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestDefaultWatcherConfig(t *testing.T) { |
| 93 | + config := DefaultWatcherConfig() |
| 94 | + |
| 95 | + if config.Interval != 5*time.Second { |
| 96 | + t.Errorf("Interval = %v, want 5s", config.Interval) |
| 97 | + } |
| 98 | + if config.CleanupDelay != 30*time.Second { |
| 99 | + t.Errorf("CleanupDelay = %v, want 30s", config.CleanupDelay) |
| 100 | + } |
| 101 | + if config.MaxRetries != 3 { |
| 102 | + t.Errorf("MaxRetries = %d, want 3", config.MaxRetries) |
| 103 | + } |
| 104 | + if config.RetryInterval != 1*time.Second { |
| 105 | + t.Errorf("RetryInterval = %v, want 1s", config.RetryInterval) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func TestDefaultLogConfig(t *testing.T) { |
| 110 | + config := DefaultLogConfig() |
| 111 | + |
| 112 | + if config.Level != "info" { |
| 113 | + t.Errorf("Level = %s, want info", config.Level) |
| 114 | + } |
| 115 | + if config.Format != "text" { |
| 116 | + t.Errorf("Format = %s, want text", config.Format) |
| 117 | + } |
| 118 | + if config.File != "" { |
| 119 | + t.Errorf("File = %s, want empty string", config.File) |
| 120 | + } |
| 121 | + if config.MaxSize != 100 { |
| 122 | + t.Errorf("MaxSize = %d, want 100", config.MaxSize) |
| 123 | + } |
| 124 | + if config.MaxAge != 30 { |
| 125 | + t.Errorf("MaxAge = %d, want 30", config.MaxAge) |
| 126 | + } |
| 127 | + if !config.Compress { |
| 128 | + t.Error("Compress = false, want true") |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +func TestDevelopmentConfig(t *testing.T) { |
| 133 | + config := DevelopmentConfig() |
| 134 | + |
| 135 | + if config.Log.Level != "debug" { |
| 136 | + t.Errorf("Log.Level = %s, want debug", config.Log.Level) |
| 137 | + } |
| 138 | + if config.Log.Format != "text" { |
| 139 | + t.Errorf("Log.Format = %s, want text", config.Log.Format) |
| 140 | + } |
| 141 | + if config.Watcher.Interval != 2*time.Second { |
| 142 | + t.Errorf("Watcher.Interval = %v, want 2s", config.Watcher.Interval) |
| 143 | + } |
| 144 | + |
| 145 | + // verify base config is correctly inherited |
| 146 | + if config.Port.Range.Start != 8000 { |
| 147 | + t.Errorf("Port.Range.Start = %d, want 8000", config.Port.Range.Start) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +func TestProductionConfig(t *testing.T) { |
| 152 | + config := ProductionConfig() |
| 153 | + |
| 154 | + if config.Log.Level != "warn" { |
| 155 | + t.Errorf("Log.Level = %s, want warn", config.Log.Level) |
| 156 | + } |
| 157 | + if config.Log.Format != "json" { |
| 158 | + t.Errorf("Log.Format = %s, want json", config.Log.Format) |
| 159 | + } |
| 160 | + if config.Log.File != "/var/log/gopose/gopose.log" { |
| 161 | + t.Errorf("Log.File = %s, want /var/log/gopose/gopose.log", config.Log.File) |
| 162 | + } |
| 163 | + if config.Watcher.Interval != 10*time.Second { |
| 164 | + t.Errorf("Watcher.Interval = %v, want 10s", config.Watcher.Interval) |
| 165 | + } |
| 166 | + if config.Watcher.CleanupDelay != 60*time.Second { |
| 167 | + t.Errorf("Watcher.CleanupDelay = %v, want 60s", config.Watcher.CleanupDelay) |
| 168 | + } |
| 169 | + |
| 170 | + // verify base config is correctly inherited |
| 171 | + if config.Port.Range.Start != 8000 { |
| 172 | + t.Errorf("Port.Range.Start = %d, want 8000", config.Port.Range.Start) |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +func TestTestConfig(t *testing.T) { |
| 177 | + config := TestConfig() |
| 178 | + |
| 179 | + if config.Log.Level != "debug" { |
| 180 | + t.Errorf("Log.Level = %s, want debug", config.Log.Level) |
| 181 | + } |
| 182 | + if config.Log.Format != "text" { |
| 183 | + t.Errorf("Log.Format = %s, want text", config.Log.Format) |
| 184 | + } |
| 185 | + if config.File.BackupEnabled { |
| 186 | + t.Error("File.BackupEnabled = true, want false (for testing)") |
| 187 | + } |
| 188 | + if config.Watcher.Interval != 100*time.Millisecond { |
| 189 | + t.Errorf("Watcher.Interval = %v, want 100ms", config.Watcher.Interval) |
| 190 | + } |
| 191 | + if config.Watcher.CleanupDelay != 1*time.Second { |
| 192 | + t.Errorf("Watcher.CleanupDelay = %v, want 1s", config.Watcher.CleanupDelay) |
| 193 | + } |
| 194 | + |
| 195 | + // verify base config is correctly inherited |
| 196 | + if config.Port.Range.Start != 8000 { |
| 197 | + t.Errorf("Port.Range.Start = %d, want 8000", config.Port.Range.Start) |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +func TestConfigIsolation(t *testing.T) { |
| 202 | + // verify each config function returns an independent instance |
| 203 | + config1 := DefaultConfig() |
| 204 | + config2 := DefaultConfig() |
| 205 | + |
| 206 | + // verify pointers are different (== on *Config compares addresses, not values) |
| 207 | + if config1 == config2 { |
| 208 | + t.Error("DefaultConfig() returns the same pointer, expected different instances") |
| 209 | + } |
| 210 | + |
| 211 | + // verify modifying one does not affect the other |
| 212 | + config1.Port.Range.Start = 9999 |
| 213 | + if config2.Port.Range.Start == 9999 { |
| 214 | + t.Error("Modifying config1 affected config2, expected independence") |
| 215 | + } |
| 216 | +} |
0 commit comments