diff --git a/collectors/actions_collector_test.go b/collectors/actions_collector_test.go new file mode 100644 index 0000000..0334c01 --- /dev/null +++ b/collectors/actions_collector_test.go @@ -0,0 +1,35 @@ +package collectors + +import ( + "os" + "strings" + "testing" + + "github.com/prometheus/client_golang/prometheus/testutil" + wgc "github.com/tomvil/wanguard_exporter/client" +) + +func TestActionsCollector(t *testing.T) { + wgcClient := wgc.NewClient(os.Getenv("TEST_SERVER_URL"), "u", "p") + ActionsCollector := NewActionsCollector(wgcClient) + + metricsCount := testutil.CollectAndCount(ActionsCollector) + if metricsCount != 1 { + t.Errorf("Expected 1 metric, got %d", metricsCount) + } + + expectedMetrics := actionsExpectedMetrics() + err := testutil.CollectAndCompare(ActionsCollector, strings.NewReader(expectedMetrics), + "wanguard_action_status") + if err != nil { + t.Errorf("Expected no error, got %s", err) + } +} + +func actionsExpectedMetrics() string { + return ` + # HELP wanguard_action_status Status of the response actions + # TYPE wanguard_action_status gauge + wanguard_action_status{action_name="Action 1",action_type="Send a custom Syslog message",response_branch="When an anomaly is detected",response_name="Response 1"} 1 +` +} diff --git a/collectors/collector_test.go b/collectors/collector_test.go index 178e133..7a98b90 100644 --- a/collectors/collector_test.go +++ b/collectors/collector_test.go @@ -96,6 +96,24 @@ func TestMain(m *testing.M) { } }) + mux.HandleFunc("/wanguard-api/v1/responses", func(w http.ResponseWriter, r *http.Request) { + if _, err := w.Write([]byte(responsesPayload())); err != nil { + log.Errorln(err.Error()) + } + }) + + mux.HandleFunc("/wanguard-api/v1/responses/1/actions", func(w http.ResponseWriter, r *http.Request) { + if _, err := w.Write([]byte(actionsPayload())); err != nil { + log.Errorln(err.Error()) + } + }) + + mux.HandleFunc("/wanguard-api/v1/responses/1/actions/1/status", func(w http.ResponseWriter, r *http.Request) { + if _, err := w.Write([]byte(`{"status": "Active"}`)); err != nil { + log.Errorln(err.Error()) + } + }) + server := httptest.NewServer(mux) defer server.Close() @@ -214,3 +232,28 @@ func anomaliesPayload() string { } ]` } + +func responsesPayload() string { + return `[ + { + "response_id": "1", + "response_name": "Response 1", + "href": "/wanguard-api/v1/responses/1" + } +]` +} + +func actionsPayload() string { + return `[ + { + "action_id": "1", + "status": { + "href": "/wanguard-api/v1/responses/1/actions/1/status" + }, + "action_name": "Action 1", + "action_type": "Send a custom Syslog message", + "response_branch": "When an anomaly is detected", + "href": "/wanguard-api/v1/responses/1/actions/1" + } +]` +}