Skip to content

Commit 7d3567e

Browse files
authored
Merge pull request #15 from infrawatch/sensu-core-server
Spin up sensu-core server correctly
2 parents 598b321 + f26a67a commit 7d3567e

File tree

6 files changed

+161
-15
lines changed

6 files changed

+161
-15
lines changed

.github/workflows/unit_tests.yml

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,23 @@
1-
name: Integration testing
1+
name: Unit test suite run
22
env:
3+
# sensu container config
34
SENSU_IMAGE: bitlayer/docker-sensu:1.9.0-1
5+
TRANSPORT_NAME: rabbitmq
6+
RABBITMQ_HOST: 127.0.0.1
7+
RABBITMQ_USER: guest
8+
RABBITMQ_PASSWORD: ""
9+
RABBITMQ_VHOST: "/sensu"
10+
REDIS_HOST: 127.0.0.1
11+
REDIS_PORT: 6379
12+
# redis container config
13+
REDIS_IMAGE: redis:6.2.1
14+
# rabbitmq container config
415
RABBITMQ_IMAGE: rabbitmq:3.7.24
16+
# qdr container config
517
QDROUTERD_IMAGE: quay.io/interconnectedcloud/qdrouterd:1.12.0
18+
# loki container config
619
LOKI_IMAGE: grafana/loki:2.1.0
20+
# misc. config
721
COVERALLS_TOKEN: ${{ secrets.COVERALLS_TOKEN }}
822
on: push
923

@@ -15,6 +29,9 @@ jobs:
1529
steps:
1630
- name: Checkout code
1731
uses: actions/checkout@v2
32+
- name: Start Redis
33+
run: |
34+
docker run --name redis -p 6379:6379 -d $REDIS_IMAGE
1835
- name: Start RabbitMQ message bus
1936
run: |
2037
docker run --name=rabbitmq -p 5672:5672 -p 4369:4369 -d $RABBITMQ_IMAGE
@@ -29,18 +46,36 @@ jobs:
2946
docker exec rabbitmq rabbitmqctl set_permissions -p "/sensu" guest ".*" ".*" ".*"
3047
- name: Start Sensu
3148
run: |
32-
docker run --name sensu-core --volume=$PWD/ci/sensu/conf.d:/etc/sensu/conf.d:ro --network host -d $SENSU_IMAGE
49+
docker run --name sensu-api --network host --env-file=$PWD/ci/sensu-env.sh -d $SENSU_IMAGE api
50+
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
3351
- name: Start Loki
3452
run: |
3553
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
36-
- name: List dependency containers
54+
- name: List dependency containers' logs
3755
run: |
3856
docker ps --all
57+
echo "---- rabbitmq ----"
3958
docker logs rabbitmq
59+
echo "---- qdr ----"
4060
docker logs qdr
41-
docker logs sensu-core
61+
echo "---- sensu-core ----"
62+
docker logs sensu-server
63+
docker logs sensu-api
64+
echo "---- loki ----"
4265
docker logs loki
43-
- name: Run integration tests
66+
- name: Run unit tests
4467
run: |
4568
export PROJECT_ROOT=/root/go/src/github.com/infrawatch/apputils
4669
docker run -uroot --network host --volume=$PWD:$PROJECT_ROOT:z --workdir $PROJECT_ROOT centos:8 bash ci/run_ci.sh
70+
- name: List dependency containers' logs
71+
run: |
72+
echo "---- rabbitmq ----"
73+
docker logs rabbitmq
74+
echo "---- qdr ----"
75+
docker logs qdr
76+
echo "---- sensu-core ----"
77+
docker logs sensu-server
78+
docker logs sensu-api
79+
echo "---- loki ----"
80+
docker logs loki
81+
if: ${{ failure() }}

ci/sensu-env.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
TRANSPORT_NAME=rabbitmq
2+
RABBITMQ_HOST=127.0.0.1
3+
RABBITMQ_USER=guest
4+
RABBITMQ_PASSWORD=guest
5+
RABBITMQ_VHOST=/sensu
6+
REDIS_HOST=127.0.0.1
7+
REDIS_PORT=6379

ci/sensu/check.d/test.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"checks": {
3+
"echo": {
4+
"command": "echo \"wubba lubba\" && exit 1",
5+
"interval": 1,
6+
"subscribers": [
7+
"ci"
8+
],
9+
"handlers": [
10+
"handle-ci"
11+
]
12+
}
13+
}
14+
}

ci/sensu/handlers/test.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"handlers": {
3+
"handle-ci": {
4+
"type": "pipe",
5+
"command": "cat >/tmp/apputils-sensu-result-received.txt"
6+
}
7+
}
8+
}

connector/sensu.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ const (
2222

2323
//Result contains data about check execution
2424
type Result struct {
25-
Command string `json:"command"`
26-
Name string `json:"name"`
27-
Issued int64 `json:"issued"`
28-
Executed int64 `json:"executed"`
29-
Duration float64 `json:"duration"`
30-
Output string `json:"output"`
31-
Status int `json:"status"`
25+
Command string `json:"command"`
26+
Name string `json:"name"`
27+
Issued int64 `json:"issued"`
28+
Handlers []string `json:"handlers,omitempty"`
29+
Handler string `json:"handler,omitempty"`
30+
Executed int64 `json:"executed"`
31+
Duration float64 `json:"duration"`
32+
Output string `json:"output"`
33+
Status int `json:"status"`
3234
}
3335

3436
//CheckResult represents message structure for sending check results back to Sensu server
@@ -39,9 +41,11 @@ type CheckResult struct {
3941

4042
//CheckRequest is the output of the connector's listening loop
4143
type CheckRequest struct {
42-
Command string `json:"command"`
43-
Name string `json:"name"`
44-
Issued int64 `json:"issued"`
44+
Command string `json:"command"`
45+
Name string `json:"name"`
46+
Issued int64 `json:"issued"`
47+
Handlers []string `json:"handlers,omitempty"`
48+
Handler string `json:"handler,omitempty"`
4549
}
4650

4751
//Keepalive holds structure for Sensu KeepAlive messages

tests/connector_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package tests
22

33
import (
4+
"encoding/json"
45
"io/ioutil"
56
"log"
7+
"net/http"
68
"os"
79
"path"
810
"strconv"
@@ -435,3 +437,79 @@ func TestLoki(t *testing.T) {
435437
}
436438
})
437439
}
440+
441+
func TestSensuCommunication(t *testing.T) {
442+
tmpdir, err := ioutil.TempDir(".", "connector_test_tmp")
443+
if err != nil {
444+
log.Fatal(err)
445+
}
446+
defer os.RemoveAll(tmpdir)
447+
logpath := path.Join(tmpdir, "test.log")
448+
logger, err := logging.NewLogger(logging.DEBUG, logpath)
449+
if err != nil {
450+
t.Fatalf("Failed to open log file %s. %s\n", logpath, err)
451+
}
452+
defer logger.Destroy()
453+
454+
// check for "ci" subscribers is defined in ci/sensu/check.d/test.json
455+
sensu, err := connector.CreateSensuConnector(logger, "amqp://127.0.0.1:5672//sensu", "ci-unit", "127.0.0.1", 1, []string{"ci"})
456+
assert.NoError(t, err)
457+
458+
t.Run("Test communication with sensu-core server", func(t *testing.T) {
459+
requests := make(chan interface{})
460+
results := make(chan interface{})
461+
sensu.Start(requests, results)
462+
463+
// wait for request from sensu-core server
464+
for req := range requests {
465+
switch reqst := req.(type) {
466+
case connector.CheckRequest:
467+
// verify we received awaited check request
468+
assert.Equal(t, "echo", reqst.Name)
469+
assert.Equal(t, "echo \"wubba lubba\" && exit 1", reqst.Command)
470+
471+
// mock result and send it
472+
result := connector.CheckResult{
473+
Client: sensu.ClientName,
474+
Result: connector.Result{
475+
Command: reqst.Command,
476+
Name: reqst.Name,
477+
Issued: reqst.Issued,
478+
Handlers: reqst.Handlers,
479+
Handler: reqst.Handler,
480+
Executed: time.Now().Unix(),
481+
Duration: time.Millisecond.Seconds(),
482+
Output: "wubba lubba",
483+
Status: 1,
484+
},
485+
}
486+
results <- result
487+
goto done
488+
}
489+
}
490+
done:
491+
// wait for sensu handler to create result receive verification file
492+
time.Sleep(time.Second)
493+
494+
resp, err := http.Get("http://127.0.0.1:4567/results")
495+
assert.NoError(t, err)
496+
defer resp.Body.Close()
497+
498+
body, err := ioutil.ReadAll(resp.Body)
499+
assert.NoError(t, err)
500+
501+
var resultList []connector.CheckResult
502+
err = json.Unmarshal(body, &resultList)
503+
assert.NoError(t, err)
504+
found := false
505+
for _, res := range resultList {
506+
if res.Client == "ci-unit" {
507+
if res.Result.Name == "echo" && res.Result.Command == "echo \"wubba lubba\" && exit 1" {
508+
found = true
509+
break
510+
}
511+
}
512+
}
513+
assert.True(t, found)
514+
})
515+
}

0 commit comments

Comments
 (0)