From f26a67ae19d48393f27d59d1c45161782e7ca025 Mon Sep 17 00:00:00 2001 From: Martin Magr Date: Wed, 10 Mar 2021 12:49:37 +0100 Subject: [PATCH] Pass handler setting from request to result --- .github/workflows/unit_tests.yml | 9 ++++++--- connector/sensu.go | 24 ++++++++++++++---------- tests/connector_test.go | 26 +++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/.github/workflows/unit_tests.yml b/.github/workflows/unit_tests.yml index 7378372..88ba53d 100644 --- a/.github/workflows/unit_tests.yml +++ b/.github/workflows/unit_tests.yml @@ -46,7 +46,8 @@ jobs: docker exec rabbitmq rabbitmqctl set_permissions -p "/sensu" guest ".*" ".*" ".*" - name: Start Sensu run: | - docker run --name sensu-core --network host --env-file=$PWD/ci/sensu-env.sh --volume=$PWD/ci/sensu/check.d:/etc/sensu/check.d:ro --volume=$PWD/ci/sensu/handlers:/etc/sensu/handlers:ro -d $SENSU_IMAGE server + docker run --name sensu-api --network host --env-file=$PWD/ci/sensu-env.sh -d $SENSU_IMAGE api + docker run --name sensu-server --network host --env-file=$PWD/ci/sensu-env.sh --volume=$PWD/ci/sensu/check.d:/etc/sensu/check.d:ro -d $SENSU_IMAGE server - name: Start Loki run: | docker run --name loki --volume=$PWD/ci/loki-config.yaml:/etc/loki/loki-config.yaml:ro -p 3100:3100 -d $LOKI_IMAGE -config.file=/etc/loki/loki-config.yaml @@ -58,7 +59,8 @@ jobs: echo "---- qdr ----" docker logs qdr echo "---- sensu-core ----" - docker logs sensu-core + docker logs sensu-server + docker logs sensu-api echo "---- loki ----" docker logs loki - name: Run unit tests @@ -72,7 +74,8 @@ jobs: echo "---- qdr ----" docker logs qdr echo "---- sensu-core ----" - docker logs sensu-core + docker logs sensu-server + docker logs sensu-api echo "---- loki ----" docker logs loki if: ${{ failure() }} diff --git a/connector/sensu.go b/connector/sensu.go index d040b80..9a9848a 100644 --- a/connector/sensu.go +++ b/connector/sensu.go @@ -22,13 +22,15 @@ const ( //Result contains data about check execution type Result struct { - Command string `json:"command"` - Name string `json:"name"` - Issued int64 `json:"issued"` - Executed int64 `json:"executed"` - Duration float64 `json:"duration"` - Output string `json:"output"` - Status int `json:"status"` + Command string `json:"command"` + Name string `json:"name"` + Issued int64 `json:"issued"` + Handlers []string `json:"handlers,omitempty"` + Handler string `json:"handler,omitempty"` + Executed int64 `json:"executed"` + Duration float64 `json:"duration"` + Output string `json:"output"` + Status int `json:"status"` } //CheckResult represents message structure for sending check results back to Sensu server @@ -39,9 +41,11 @@ type CheckResult struct { //CheckRequest is the output of the connector's listening loop type CheckRequest struct { - Command string `json:"command"` - Name string `json:"name"` - Issued int64 `json:"issued"` + Command string `json:"command"` + Name string `json:"name"` + Issued int64 `json:"issued"` + Handlers []string `json:"handlers,omitempty"` + Handler string `json:"handler,omitempty"` } //Keepalive holds structure for Sensu KeepAlive messages diff --git a/tests/connector_test.go b/tests/connector_test.go index c570674..724f612 100644 --- a/tests/connector_test.go +++ b/tests/connector_test.go @@ -1,8 +1,10 @@ package tests import ( + "encoding/json" "io/ioutil" "log" + "net/http" "os" "path" "strconv" @@ -465,6 +467,7 @@ func TestSensuCommunication(t *testing.T) { // verify we received awaited check request assert.Equal(t, "echo", reqst.Name) assert.Equal(t, "echo \"wubba lubba\" && exit 1", reqst.Command) + // mock result and send it result := connector.CheckResult{ Client: sensu.ClientName, @@ -472,6 +475,8 @@ func TestSensuCommunication(t *testing.T) { Command: reqst.Command, Name: reqst.Name, Issued: reqst.Issued, + Handlers: reqst.Handlers, + Handler: reqst.Handler, Executed: time.Now().Unix(), Duration: time.Millisecond.Seconds(), Output: "wubba lubba", @@ -486,6 +491,25 @@ func TestSensuCommunication(t *testing.T) { // wait for sensu handler to create result receive verification file time.Sleep(time.Second) - assert.FileExists(t, "/tmp/apputils-sensu-result-received.txt") + resp, err := http.Get("http://127.0.0.1:4567/results") + assert.NoError(t, err) + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + assert.NoError(t, err) + + var resultList []connector.CheckResult + err = json.Unmarshal(body, &resultList) + assert.NoError(t, err) + found := false + for _, res := range resultList { + if res.Client == "ci-unit" { + if res.Result.Name == "echo" && res.Result.Command == "echo \"wubba lubba\" && exit 1" { + found = true + break + } + } + } + assert.True(t, found) }) }