Init
This commit is contained in:
parent
c823102a63
commit
d37503c863
20 changed files with 4433 additions and 3 deletions
75
internal/config/config_test.go
Normal file
75
internal/config/config_test.go
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestLoadOrInitGeneratesDefault(t *testing.T) {
|
||||
tmpDir := t.TempDir()
|
||||
cfgPath := filepath.Join(tmpDir, "rud.json")
|
||||
|
||||
cfg, created, err := LoadOrInit(cfgPath, true)
|
||||
if err != nil {
|
||||
t.Fatalf("LoadOrInit failed: %v", err)
|
||||
}
|
||||
if !created {
|
||||
t.Fatalf("expected created=true when file did not exist")
|
||||
}
|
||||
if len(cfg.Monitors) == 0 {
|
||||
t.Fatalf("expected default monitors to be populated")
|
||||
}
|
||||
if len(cfg.Page.Elements) == 0 {
|
||||
t.Fatalf("expected default page elements to be populated")
|
||||
}
|
||||
|
||||
// Reload existing file
|
||||
cfg2, created2, err := LoadOrInit(cfgPath, true)
|
||||
if err != nil {
|
||||
t.Fatalf("second LoadOrInit failed: %v", err)
|
||||
}
|
||||
if created2 {
|
||||
t.Fatalf("expected created=false when file already exists")
|
||||
}
|
||||
if len(cfg2.Monitors) != len(cfg.Monitors) {
|
||||
t.Fatalf("expected %d monitors, got %d", len(cfg.Monitors), len(cfg2.Monitors))
|
||||
}
|
||||
}
|
||||
|
||||
func TestDurationMarshalUnmarshal(t *testing.T) {
|
||||
d := NewDuration(5 * time.Hour)
|
||||
b, err := d.MarshalJSON()
|
||||
if err != nil {
|
||||
t.Fatalf("MarshalJSON failed: %v", err)
|
||||
}
|
||||
if string(b) != `"5h"` {
|
||||
t.Fatalf("expected \"5h\", got %s", string(b))
|
||||
}
|
||||
|
||||
var parsed Duration
|
||||
if err := parsed.UnmarshalJSON([]byte(`"15m"`)); err != nil {
|
||||
t.Fatalf("UnmarshalJSON failed: %v", err)
|
||||
}
|
||||
if parsed.Std() != 15*time.Minute {
|
||||
t.Fatalf("expected 15m, got %v", parsed.Std())
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfigValidation(t *testing.T) {
|
||||
cfg := DefaultConfig()
|
||||
cfg.Monitors = []MonitorConfig{
|
||||
{ID: "dup", Method: "tcp", Target: "127.0.0.1:80"},
|
||||
{ID: "dup", Method: "tcp", Target: "127.0.0.1:81"},
|
||||
}
|
||||
if err := cfg.Validate(); err == nil {
|
||||
t.Fatalf("expected duplicate monitor id error")
|
||||
}
|
||||
|
||||
cfg.Monitors = []MonitorConfig{
|
||||
{ID: "bad-regex", Method: "http", URL: "http://example.com", BodyRegex: "["},
|
||||
}
|
||||
if err := cfg.Validate(); err == nil {
|
||||
t.Fatalf("expected invalid body_regex error")
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue