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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142package monitors_test
import ( "bytes" "context" "io" "net/http" "os" "testing"
"github.com/openstatusHQ/cli/internal/monitors")
func Test_ExportMonitor(t *testing.T) { t.Parallel()
t.Run("Export HTTP monitors successfully", func(t *testing.T) { // Connect RPC response format with ListMonitorsResponse body := `{"httpMonitors":[{"id":"123","name":"HTTP Monitor","url":"https://example.com","periodicity":"PERIODICITY_10M","active":true,"public":false}]}` r := io.NopCloser(bytes.NewReader([]byte(body)))
interceptor := &interceptorHTTPClient{ f: func(req *http.Request) (*http.Response, error) { if req.Header.Get("x-openstatus-key") != "test-api-key" { t.Errorf("Expected x-openstatus-key header, got %s", req.Header.Get("x-openstatus-key")) } return &http.Response{ StatusCode: http.StatusOK, Body: r, Header: http.Header{ "Content-Type": []string{"application/json"}, }, }, nil }, }
outputFile, err := os.CreateTemp(".", "export*.yaml") if err != nil { t.Fatal(err) } defer os.Remove(outputFile.Name()) defer os.Remove("openstatus.lock") outputFile.Close()
err = monitors.ExportMonitorWithHTTPClient(context.Background(), interceptor.GetHTTPClient(),"test-api-key", outputFile.Name()) if err != nil { t.Fatalf("Expected no error, got %v", err) }
content, err := os.ReadFile(outputFile.Name()) if err != nil { t.Fatal(err) }
if len(content) == 0 { t.Error("Expected non-empty output file") } })
t.Run("Export TCP monitors successfully", func(t *testing.T) { body := `{"tcpMonitors":[{"id":"456","name":"TCP Monitor","uri":"example.com:443","periodicity":"PERIODICITY_5M","active":true}]}` r := io.NopCloser(bytes.NewReader([]byte(body)))
interceptor := &interceptorHTTPClient{ f: func(req *http.Request) (*http.Response, error) { return &http.Response{ StatusCode: http.StatusOK, Body: r, Header: http.Header{ "Content-Type": []string{"application/json"}, }, }, nil }, }
outputFile, err := os.CreateTemp(".", "export_tcp*.yaml") if err != nil { t.Fatal(err) } defer os.Remove(outputFile.Name()) defer os.Remove("openstatus.lock") outputFile.Close()
err = monitors.ExportMonitorWithHTTPClient(context.Background(), interceptor.GetHTTPClient(),"test-api-key", outputFile.Name()) if err != nil { t.Fatalf("Expected no error, got %v", err) } })
t.Run("Export fails with error status", func(t *testing.T) { body := `{"code":"permission_denied","message":"unauthorized"}` r := io.NopCloser(bytes.NewReader([]byte(body)))
interceptor := &interceptorHTTPClient{ f: func(req *http.Request) (*http.Response, error) { return &http.Response{ StatusCode: http.StatusUnauthorized, Body: r, Header: http.Header{ "Content-Type": []string{"application/json"}, }, }, nil }, }
err := monitors.ExportMonitorWithHTTPClient(context.Background(), interceptor.GetHTTPClient(),"invalid-key", "output.yaml") if err == nil { t.Error("Expected error for non-200 status, got nil") } })
t.Run("Export handles empty monitors", func(t *testing.T) { body := `{"httpMonitors":[],"tcpMonitors":[],"dnsMonitors":[]}` r := io.NopCloser(bytes.NewReader([]byte(body)))
interceptor := &interceptorHTTPClient{ f: func(req *http.Request) (*http.Response, error) { return &http.Response{ StatusCode: http.StatusOK, Body: r, Header: http.Header{ "Content-Type": []string{"application/json"}, }, }, nil }, }
outputFile, err := os.CreateTemp(".", "export_empty*.yaml") if err != nil { t.Fatal(err) } defer os.Remove(outputFile.Name()) defer os.Remove("openstatus.lock") outputFile.Close()
err = monitors.ExportMonitorWithHTTPClient(context.Background(), interceptor.GetHTTPClient(),"test-api-key", outputFile.Name()) if err != nil { t.Fatalf("Expected no error, got %v", err) } })}