Skip to content

Commit

Permalink
tests: NSCA consumer
Browse files Browse the repository at this point in the history
Signed-off-by: Romain Beuque <romain.beuque@gmail.com>
  • Loading branch information
rbeuque74 committed May 1, 2018
1 parent ee9505f commit 47e798e
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ language: go
go:
- "1.10.x"

before_script:
- go get -v ./...

after_script:
- go get -v github.com/alecthomas/gometalinter/...
- gometalinter --install
Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
all:
go get -v ./...
go build -o jagozzi main.go utils.go instance.go

test:
go test -v ./...
85 changes: 85 additions & 0 deletions consumers/nsca/nsca_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package nsca

import (
"context"
"encoding/json"
"errors"
"fmt"
"testing"
"time"

"github.com/rbeuque74/jagozzi/config"
"github.com/rbeuque74/jagozzi/plugins"
"github.com/syncbak-git/nsca"
)

type fakeChecker struct {
t *testing.T
}

func (fc fakeChecker) Name() string {
return "fake-checker"
}

func (fc fakeChecker) ServiceName() string {
return "fake-service-name"
}

func (fc fakeChecker) Run(ctx context.Context) (string, error) {
fc.t.Fatal("fake checker should not run")
return "", errors.New("fake checker should not run")
}

func TestConsumerSendMessage(t *testing.T) {
// creating NSCA server
srvCh := make(chan Message, 10)
go NewNscaServerChannel(srvCh)

// creating NSCA client
cfg := &config.ConsumerConfiguration{}
cfgStr := []byte(fmt.Sprintf(`{"type":"NSCA","server":"localhost","timeout":1000,"encryption":%d,"key":"%s"}`, nsca.ENCRYPT_XOR, EncryptKey))
json.Unmarshal(cfgStr, cfg)

msgCh := make(chan *nsca.Message, 10)
exitCh := make(chan interface{}, 10)
consumer := New(*cfg, msgCh, exitCh)

// sending message
ctx, cancelFunc := context.WithTimeout(context.Background(), time.Second)
defer cancelFunc()

res := plugins.Result{
Status: plugins.STATE_CRITICAL,
Message: "example message",
Checker: fakeChecker{
t: t,
},
}

errCh := make(chan error, 10)
consumer.Send(ctx, res, "hostname-example-1", errCh)

messageReceived := false
for {
select {
case err := <-errCh:
if err != nil {
t.Fatalf("err channel is not empty: %q", err)
} else {
t.Log("nsca send OK")
}
messageReceived = true
close(exitCh)
case <-time.After(time.Second):
t.Log("timed out")
if !messageReceived {
t.Fatal("timeout and message not received")
}
return
case <-exitCh:
t.Log("finished")
return
}
}

}
29 changes: 29 additions & 0 deletions consumers/nsca/nsca_utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package nsca

import (
"errors"

nsrv "github.com/tubemogul/nscatools"
)

const (
// EncryptKey is the NSCA encryption key for unit tests
EncryptKey = "toto"
)

type Message nsrv.DataPacket

func NewNscaServerChannel(ch chan<- Message) {
cfg := nsrv.NewConfig("localhost", 5667, nsrv.EncryptXOR, EncryptKey, func(p *nsrv.DataPacket) error {
if p == nil {
return errors.New("packet is nil")
}
ch <- typeCastMessage(*p)
return nil
})
nsrv.StartServer(cfg, true)
}

func typeCastMessage(msg interface{}) Message {
return msg.(Message)
}

0 comments on commit 47e798e

Please sign in to comment.