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 a9bc25f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
13 changes: 9 additions & 4 deletions .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -58,21 +61,23 @@ 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 ----"
docker logs rabbitmq
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
15 changes: 14 additions & 1 deletion tests/connector_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package tests

import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"strconv"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -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",
Expand All @@ -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")
})
}

0 comments on commit a9bc25f

Please sign in to comment.