Skip to content

Commit

Permalink
Pass handler setting from request to result
Browse files Browse the repository at this point in the history
  • Loading branch information
paramite committed Mar 10, 2021
1 parent 6e14ccb commit f26a67a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
9 changes: 6 additions & 3 deletions .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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() }}
24 changes: 14 additions & 10 deletions connector/sensu.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
26 changes: 25 additions & 1 deletion tests/connector_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package tests

import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"strconv"
Expand Down Expand Up @@ -465,13 +467,16 @@ 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,
Result: connector.Result{
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",
Expand All @@ -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)
})
}

0 comments on commit f26a67a

Please sign in to comment.