-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Romain Beuque <romain.beuque@gmail.com>
- Loading branch information
Showing
4 changed files
with
120 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |