Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/cli. OpenStatus CLI www.openstatus.dev
cli cli-app synthetic-monitoring
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320package config_test
import ( "os" "path/filepath" "testing"
"github.com/openstatusHQ/cli/internal/config")
var openstatusConfig = `"test-monitor": active: true assertions: - compare: eq kind: statusCode target: 200 description: Test monitor description frequency: 10m kind: http name: Test Monitor regions: - iad - ams request: headers: User-Agent: OpenStatus method: GET url: https://example.com retry: 3`
func Test_ReadOpenStatus(t *testing.T) { t.Run("Read valid openstatus config", func(t *testing.T) { f, err := os.CreateTemp(t.TempDir(), "openstatus*.yaml") if err != nil { t.Fatal(err) }
if _, err := f.WriteString(openstatusConfig); err != nil { t.Fatal(err) } if err := f.Close(); err != nil { t.Fatal(err) }
out, err := config.ReadOpenStatus(f.Name()) if err != nil { t.Fatal(err) }
monitor, exists := out["test-monitor"] if !exists { t.Fatal("Expected 'test-monitor' to exist in output") }
if monitor.Name != "Test Monitor" { t.Errorf("Expected name 'Test Monitor', got %s", monitor.Name) } if monitor.Description != "Test monitor description" { t.Errorf("Expected description 'Test monitor description', got %s", monitor.Description) } if monitor.Frequency != config.The10M { t.Errorf("Expected frequency '10m', got %s", monitor.Frequency) } if monitor.Kind != config.HTTP { t.Errorf("Expected kind 'http', got %s", monitor.Kind) } if !monitor.Active { t.Error("Expected monitor to be active") } if monitor.Retry != 3 { t.Errorf("Expected retry 3, got %d", monitor.Retry) } if len(monitor.Regions) != 2 { t.Errorf("Expected 2 regions, got %d", len(monitor.Regions)) } if monitor.Request.URL != "https://example.com" { t.Errorf("Expected URL 'https://example.com', got %s", monitor.Request.URL) } if monitor.Request.Method != config.Get { t.Errorf("Expected method 'GET', got %s", monitor.Request.Method) } if len(monitor.Assertions) != 1 { t.Errorf("Expected 1 assertion, got %d", len(monitor.Assertions)) } })
t.Run("File does not exist", func(t *testing.T) { _, err := config.ReadOpenStatus("nonexistent.yaml") if err == nil { t.Error("Expected error for non-existent file, got nil") } })}
func Test_ReadOpenStatus_FollowRedirects(t *testing.T) { t.Run("followRedirects false is parsed correctly", func(t *testing.T) { yaml := `"redirect-monitor": active: true frequency: 10m kind: http name: Redirect Monitor regions: - iad request: method: GET url: https://example.com followRedirects: false` f, err := os.CreateTemp(t.TempDir(), "openstatus*.yaml") if err != nil { t.Fatal(err) }
if _, err := f.WriteString(yaml); err != nil { t.Fatal(err) } if err := f.Close(); err != nil { t.Fatal(err) }
out, err := config.ReadOpenStatus(f.Name()) if err != nil { t.Fatal(err) }
monitor, exists := out["redirect-monitor"] if !exists { t.Fatal("Expected 'redirect-monitor' to exist in output") }
if monitor.Request.FollowRedirects == nil { t.Fatal("Expected FollowRedirects to be non-nil") } if *monitor.Request.FollowRedirects != false { t.Errorf("Expected FollowRedirects to be false, got %v", *monitor.Request.FollowRedirects) } })
t.Run("followRedirects omitted is nil", func(t *testing.T) { yaml := `"no-redirect-field": active: true frequency: 10m kind: http name: No Redirect Field regions: - iad request: method: GET url: https://example.com` f, err := os.CreateTemp(t.TempDir(), "openstatus*.yaml") if err != nil { t.Fatal(err) }
if _, err := f.WriteString(yaml); err != nil { t.Fatal(err) } if err := f.Close(); err != nil { t.Fatal(err) }
out, err := config.ReadOpenStatus(f.Name()) if err != nil { t.Fatal(err) }
monitor, exists := out["no-redirect-field"] if !exists { t.Fatal("Expected 'no-redirect-field' to exist in output") }
if monitor.Request.FollowRedirects != nil { t.Errorf("Expected FollowRedirects to be nil, got %v", *monitor.Request.FollowRedirects) } })}
func Test_ParseConfigMonitorsToMonitor(t *testing.T) { t.Run("Parse monitors map to slice", func(t *testing.T) { monitors := config.Monitors{ "monitor-1": { Name: "Monitor 1", Active: true, Frequency: config.The5M, Kind: config.HTTP, Regions: []config.Region{config.Iad}, Request: config.Request{ URL: "https://example1.com", Method: config.Get, }, }, "monitor-2": { Name: "Monitor 2", Active: false, Frequency: config.The10M, Kind: config.HTTP, Regions: []config.Region{config.Ams}, Request: config.Request{ URL: "https://example2.com", Method: config.Post, }, }, }
result := config.ParseConfigMonitorsToMonitor(monitors)
if len(result) != 2 { t.Errorf("Expected 2 monitors, got %d", len(result)) } })
t.Run("Empty monitors map", func(t *testing.T) { monitors := config.Monitors{} result := config.ParseConfigMonitorsToMonitor(monitors)
if len(result) != 0 { t.Errorf("Expected 0 monitors, got %d", len(result)) } })
t.Run("Assertions are converted", func(t *testing.T) { monitors := config.Monitors{ "monitor-1": { Name: "Monitor 1", Active: true, Frequency: config.The5M, Kind: config.HTTP, Regions: []config.Region{config.Iad}, Request: config.Request{ URL: "https://example.com", Method: config.Get, }, Assertions: []config.Assertion{ { Kind: config.StatusCode, Compare: config.Eq, Target: float64(200), }, }, }, }
result := config.ParseConfigMonitorsToMonitor(monitors)
if len(result) != 1 { t.Fatalf("Expected 1 monitor, got %d", len(result)) }
if result[0].Assertions[0].Target != 200 { t.Errorf("Expected target to be converted to int 200, got %v", result[0].Assertions[0].Target) } })}
func Test_ReadOpenStatus_NoStatePollution(t *testing.T) { dir := t.TempDir()
file1 := filepath.Join(dir, "config1.yaml") if err := os.WriteFile(file1, []byte(`"monitor-a": active: true frequency: 10m kind: http name: Monitor A regions: - iad request: method: GET url: https://a.example.com`), 0o600); err != nil { t.Fatal(err) }
file2 := filepath.Join(dir, "config2.yaml") if err := os.WriteFile(file2, []byte(`"monitor-b": active: true frequency: 5m kind: http name: Monitor B regions: - ams request: method: POST url: https://b.example.com`), 0o600); err != nil { t.Fatal(err) }
out1, err := config.ReadOpenStatus(file1) if err != nil { t.Fatal(err) } if _, exists := out1["monitor-a"]; !exists { t.Error("First read: expected 'monitor-a' to exist") } if len(out1) != 1 { t.Errorf("First read: expected 1 monitor, got %d", len(out1)) }
out2, err := config.ReadOpenStatus(file2) if err != nil { t.Fatal(err) } if _, exists := out2["monitor-b"]; !exists { t.Error("Second read: expected 'monitor-b' to exist") } if _, exists := out2["monitor-a"]; exists { t.Error("Second read: 'monitor-a' should not be present (state pollution)") } if len(out2) != 1 { t.Errorf("Second read: expected 1 monitor, got %d", len(out2)) }}