160 lines
2.9 KiB
Go
160 lines
2.9 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
)
|
|
|
|
func writeTmp(t *testing.T, body string) string {
|
|
t.Helper()
|
|
p := filepath.Join(t.TempDir(), "c.toml")
|
|
if err := os.WriteFile(p, []byte(body), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return p
|
|
}
|
|
|
|
const minimal = `
|
|
agent_id = "host-1"
|
|
state_dir = "/tmp/x"
|
|
|
|
[server]
|
|
url = "http://localhost"
|
|
token = "t"
|
|
|
|
[[checks]]
|
|
id = "c1"
|
|
command = ["true"]
|
|
interval = "10s"
|
|
timeout = "5s"
|
|
`
|
|
|
|
func TestLoadMinimal(t *testing.T) {
|
|
c, err := Load(writeTmp(t, minimal))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c.Mode != "hybrid" {
|
|
t.Errorf("default mode: %q", c.Mode)
|
|
}
|
|
if c.Server.HeartbeatInterval.Duration == 0 {
|
|
t.Error("default heartbeat not applied")
|
|
}
|
|
if c.Checks[0].NotificationOwner != "server" {
|
|
t.Errorf("default notification_owner: %q", c.Checks[0].NotificationOwner)
|
|
}
|
|
}
|
|
|
|
func TestValidateBadID(t *testing.T) {
|
|
body := minimal + "\n"
|
|
_, err := Load(writeTmp(t, body[:0]+`
|
|
agent_id = "bad id!"
|
|
state_dir = "/tmp/x"
|
|
[server]
|
|
url = "http://localhost"
|
|
token = "t"
|
|
[[checks]]
|
|
id = "c1"
|
|
command = ["true"]
|
|
interval = "10s"
|
|
timeout = "5s"
|
|
`))
|
|
if err == nil {
|
|
t.Fatal("expected error for bad agent_id")
|
|
}
|
|
}
|
|
|
|
func TestValidateTimeoutExceedsInterval(t *testing.T) {
|
|
_, err := Load(writeTmp(t, `
|
|
agent_id = "x"
|
|
state_dir = "/tmp/x"
|
|
[server]
|
|
url = "http://localhost"
|
|
token = "t"
|
|
[[checks]]
|
|
id = "c1"
|
|
command = ["true"]
|
|
interval = "5s"
|
|
timeout = "10s"
|
|
`))
|
|
if err == nil {
|
|
t.Fatal("expected error for timeout > interval")
|
|
}
|
|
}
|
|
|
|
func TestPrometheusOnlyAllowsMissingServer(t *testing.T) {
|
|
c, err := Load(writeTmp(t, `
|
|
mode = "prometheus_only"
|
|
state_dir = "/tmp/x"
|
|
[[checks]]
|
|
id = "c1"
|
|
command = ["true"]
|
|
interval = "10s"
|
|
timeout = "5s"
|
|
`))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if c.PushesToServer() {
|
|
t.Fatal("prometheus_only must not push")
|
|
}
|
|
}
|
|
|
|
func TestPushOnlyRequiresServer(t *testing.T) {
|
|
_, err := Load(writeTmp(t, `
|
|
mode = "push_only"
|
|
state_dir = "/tmp/x"
|
|
[[checks]]
|
|
id = "c1"
|
|
command = ["true"]
|
|
interval = "10s"
|
|
timeout = "5s"
|
|
`))
|
|
if err == nil {
|
|
t.Fatal("push_only must require server.url/token")
|
|
}
|
|
}
|
|
|
|
func TestExposesMetricsGate(t *testing.T) {
|
|
cases := []struct {
|
|
mode string
|
|
enabled bool
|
|
want bool
|
|
}{
|
|
{"hybrid", true, true},
|
|
{"hybrid", false, false},
|
|
{"prometheus_only", true, true},
|
|
{"push_only", true, false},
|
|
}
|
|
for _, tc := range cases {
|
|
c := &Config{Mode: tc.mode, Metrics: MetricsConfig{Enabled: tc.enabled}}
|
|
if got := c.ExposesMetrics(); got != tc.want {
|
|
t.Errorf("mode=%s enabled=%v want=%v got=%v", tc.mode, tc.enabled, tc.want, got)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestValidateDuplicateCheckID(t *testing.T) {
|
|
_, err := Load(writeTmp(t, `
|
|
agent_id = "x"
|
|
state_dir = "/tmp/x"
|
|
[server]
|
|
url = "http://localhost"
|
|
token = "t"
|
|
[[checks]]
|
|
id = "c1"
|
|
command = ["true"]
|
|
interval = "10s"
|
|
timeout = "5s"
|
|
[[checks]]
|
|
id = "c1"
|
|
command = ["true"]
|
|
interval = "10s"
|
|
timeout = "5s"
|
|
`))
|
|
if err == nil {
|
|
t.Fatal("expected duplicate id error")
|
|
}
|
|
}
|