From a9bc25f8ce99f60855a90c16545cf9ac5d9318cb 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 | 13 +++++++++---- connector/sensu.go | 24 ++++++++++++++---------- tests/connector_test.go | 15 ++++++++++++++- 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/.github/workflows/unit_tests.yml b/.github/workflows/unit_tests.yml index 7378372..bf80d09 100644 --- a/.github/workflows/unit_tests.yml +++ b/.github/workflows/unit_tests.yml @@ -9,6 +9,8 @@ env: RABBITMQ_VHOST: "/sensu" REDIS_HOST: 127.0.0.1 REDIS_PORT: 6379 + API_HOST: 127.0.0.1 + API_PORT: 4666 # redis container config REDIS_IMAGE: redis:6.2.1 # rabbitmq container config @@ -46,7 +48,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 --volume=$PWD/ci/sensu/handlers:/etc/sensu/handlers:ro --volume=$PWD/ci/sensu/tmp:/tmp:z -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,13 +61,14 @@ 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 run: | export PROJECT_ROOT=/root/go/src/github.com/infrawatch/apputils - docker run -uroot --network host --volume=$PWD:$PROJECT_ROOT:z --workdir $PROJECT_ROOT centos:8 bash ci/run_ci.sh + docker run -uroot --network host --volume=$PWD:$PROJECT_ROOT:z --volume=$PWD/ci/sensu/tmp:/tmp:z --workdir $PROJECT_ROOT centos:8 bash ci/run_ci.sh - name: List dependency containers' logs run: | echo "---- rabbitmq ----" @@ -72,7 +76,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..b4ddd15 100644 --- a/tests/connector_test.go +++ b/tests/connector_test.go @@ -1,11 +1,14 @@ package tests import ( + "fmt" "io/ioutil" "log" + "net/http" "os" "path" "strconv" + "strings" "testing" "time" @@ -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,14 @@ 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:4666/results") + assert.NoError(t, err) + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + assert.NoError(t, err) + assert.True(t, strings.Contains(string(body), "\"client\":\"localhost\",\"echo\":{")) + fmt.Printf("%s\n", body) + //assert.FileExists(t, "/tmp/apputils-sensu-result-received.txt") }) }