Skip to content

Commit

Permalink
Test communication with Sensu server
Browse files Browse the repository at this point in the history
  • Loading branch information
paramite committed Mar 10, 2021
1 parent d3e684b commit 6e14ccb
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
15 changes: 13 additions & 2 deletions .github/workflows/unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ 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 -d $SENSU_IMAGE server
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
- 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
- name: List dependency containers
- name: List dependency containers' logs
run: |
docker ps --all
echo "---- rabbitmq ----"
Expand All @@ -65,3 +65,14 @@ jobs:
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
- name: List dependency containers' logs
run: |
echo "---- rabbitmq ----"
docker logs rabbitmq
echo "---- qdr ----"
docker logs qdr
echo "---- sensu-core ----"
docker logs sensu-core
echo "---- loki ----"
docker logs loki
if: ${{ failure() }}
14 changes: 14 additions & 0 deletions ci/sensu/check.d/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"checks": {
"echo": {
"command": "echo \"wubba lubba\" && exit 1",
"interval": 1,
"subscribers": [
"ci"
],
"handlers": [
"handle-ci"
]
}
}
}
8 changes: 8 additions & 0 deletions ci/sensu/handlers/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"handlers": {
"handle-ci": {
"type": "pipe",
"command": "cat >/tmp/apputils-sensu-result-received.txt"
}
}
}
54 changes: 54 additions & 0 deletions tests/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,3 +435,57 @@ func TestLoki(t *testing.T) {
}
})
}

func TestSensuCommunication(t *testing.T) {
tmpdir, err := ioutil.TempDir(".", "connector_test_tmp")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(tmpdir)
logpath := path.Join(tmpdir, "test.log")
logger, err := logging.NewLogger(logging.DEBUG, logpath)
if err != nil {
t.Fatalf("Failed to open log file %s. %s\n", logpath, err)
}
defer logger.Destroy()

// check for "ci" subscribers is defined in ci/sensu/check.d/test.json
sensu, err := connector.CreateSensuConnector(logger, "amqp://127.0.0.1:5672//sensu", "ci-unit", "127.0.0.1", 1, []string{"ci"})
assert.NoError(t, err)

t.Run("Test communication with sensu-core server", func(t *testing.T) {
requests := make(chan interface{})
results := make(chan interface{})
sensu.Start(requests, results)

// wait for request from sensu-core server
for req := range requests {
switch reqst := req.(type) {
case connector.CheckRequest:
// 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,
Executed: time.Now().Unix(),
Duration: time.Millisecond.Seconds(),
Output: "wubba lubba",
Status: 1,
},
}
results <- result
goto done
}
}
done:
// wait for sensu handler to create result receive verification file
time.Sleep(time.Second)

assert.FileExists(t, "/tmp/apputils-sensu-result-received.txt")
})
}

0 comments on commit 6e14ccb

Please sign in to comment.