Skip to content

Commit

Permalink
Merge pull request #16 from tomvil/add_actionsCollector_tests
Browse files Browse the repository at this point in the history
Add actionsCollector tests
  • Loading branch information
tomvil authored Oct 29, 2024
2 parents f45dd04 + 87a0585 commit 8a41ec9
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
35 changes: 35 additions & 0 deletions collectors/actions_collector_test.go
Original file line number Diff line number Diff line change
@@ -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
`
}
43 changes: 43 additions & 0 deletions collectors/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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"
}
]`
}

0 comments on commit 8a41ec9

Please # to comment.