Testing nats in ees

This commit is contained in:
nickolasdaniel
2021-09-09 17:02:44 +03:00
committed by Dan Christian Bogos
parent c9036b38cd
commit c82d505222
2 changed files with 254 additions and 0 deletions

View File

@@ -19,6 +19,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
package ees
import (
"os"
"os/exec"
"path"
"testing"
@@ -183,3 +184,32 @@ func TestNatsEE(t *testing.T) {
t.Fatal("Time limit exceeded")
}
}
func TestGetNatsOptsSeedFile(t *testing.T) {
if _, err := os.Create("/tmp/nkey.txt"); err != nil {
t.Error(err)
}
defer os.Remove("/tmp/nkey.txt")
nkey := "SUACSSL3UAHUDXKFSNVUZRF5UHPMWZ6BFDTJ7M6USDXIEDNPPQYYYCU3VY"
os.WriteFile("/tmp/nkey.txt", []byte(nkey), 0777)
opts := map[string]interface{}{
utils.NatsSeedFile: "/tmp/nkey.txt",
// utils.NatsSeedFile: "file",
}
nodeID := "node_id1"
connTimeout := 2 * time.Second
_, err := GetNatsOpts(opts, nodeID, connTimeout)
if err != nil {
t.Error(err)
}
//test error
os.WriteFile("/tmp/nkey.txt", []byte(""), 0777)
_, err = GetNatsOpts(opts, nodeID, connTimeout)
if err.Error() != "no nkey seed found" {
t.Errorf("Expected %v \n but received \n %v", err.Error(), "no nkey seed found")
}
}

224
ees/nats_test.go Normal file
View File

@@ -0,0 +1,224 @@
/*
Real-time Online/Offline Charging System (OerS) for Telecom & ISP environments
Copyright (C) ITsysCOM GmbH
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
package ees
import (
"reflect"
"testing"
"time"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/utils"
"github.com/nats-io/nats.go"
)
func TestNewNatsEE(t *testing.T) {
cfg := &config.EventExporterCfg{
ID: "nats_exporter",
Type: "nats",
Attempts: 2,
ConcurrentRequests: 2,
}
nodeID := "node_id1"
connTimeout := 2 * time.Second
dc, err := newEEMetrics("Local")
if err != nil {
t.Error(err)
}
exp := new(NatsEE)
exp.cfg = cfg
exp.dc = dc
exp.subject = utils.DefaultQueueID
exp.reqs = newConcReq(cfg.ConcurrentRequests)
// err = exp.parseOpt(cfg.Opts, nodeID, connTimeout)
// if err != nil {
// t.Error(err)
// }
rcv, err := NewNatsEE(cfg, nodeID, connTimeout, dc)
if err != nil {
t.Error(err)
}
rcv.opts = nil
exp.opts = nil
// fmt.Println(rcv)
// fmt.Println(exp)
// if exp != rcv {
// t.Errorf("Expected %v \n but received \n %v", exp, rcv)
// }
}
func TestParseOpt(t *testing.T) {
cfg := &config.EventExporterCfg{
ID: "nats_exporter",
Type: "nats",
Attempts: 2,
ConcurrentRequests: 2,
}
opts := map[string]interface{}{}
nodeID := "node_id1"
connTimeout := 2 * time.Second
dc, err := newEEMetrics("Local")
if err != nil {
t.Error(err)
}
pstr, err := NewNatsEE(cfg, nodeID, connTimeout, dc)
if err != nil {
t.Error(err)
}
err = pstr.parseOpt(opts, nodeID, connTimeout)
if err != nil {
t.Error(err)
}
}
func TestParseOptJetStream(t *testing.T) {
cfg := &config.EventExporterCfg{
ID: "nats_exporter",
Type: "nats",
Attempts: 2,
ConcurrentRequests: 2,
}
opts := map[string]interface{}{
utils.NatsJetStream: true,
}
nodeID := "node_id1"
connTimeout := 2 * time.Second
dc, err := newEEMetrics("Local")
if err != nil {
t.Error(err)
}
pstr, err := NewNatsEE(cfg, nodeID, connTimeout, dc)
if err != nil {
t.Error(err)
}
err = pstr.parseOpt(opts, nodeID, connTimeout)
if err != nil {
t.Error(err)
}
if !pstr.jetStream {
t.Error("Expected jetStream to be true")
}
//test error on converson
opts = map[string]interface{}{
utils.NatsJetStream: uint16(2),
}
err = pstr.parseOpt(opts, nodeID, connTimeout)
if err.Error() != "cannot convert field: 2 to bool" {
t.Error("The conversion shouldn't have been possible")
}
}
func TestParseOptJetStreamMaxWait(t *testing.T) {
cfg := &config.EventExporterCfg{
ID: "nats_exporter",
Type: "nats",
Attempts: 2,
ConcurrentRequests: 2,
}
opts := map[string]interface{}{
utils.NatsJetStream: true,
utils.NatsJetStreamMaxWait: "2ns",
}
nodeID := "node_id1"
connTimeout := 2 * time.Second
dc, err := newEEMetrics("Local")
if err != nil {
t.Error(err)
}
pstr, err := NewNatsEE(cfg, nodeID, connTimeout, dc)
if err != nil {
t.Error(err)
}
err = pstr.parseOpt(opts, nodeID, connTimeout)
if err != nil {
t.Error(err)
}
exp := []nats.JSOpt{nats.MaxWait(2 * time.Nanosecond)}
if !reflect.DeepEqual(pstr.jsOpts, exp) {
t.Errorf("Expected %v \n but received \n %v", exp, pstr.jsOpts)
}
//test conversion error
opts = map[string]interface{}{
utils.NatsJetStream: true,
utils.NatsJetStreamMaxWait: true,
}
err = pstr.parseOpt(opts, nodeID, connTimeout)
if err.Error() != "cannot convert field: true to time.Duration" {
t.Errorf("The conversion shouldn't have been possible: %v", err.Error())
}
}
func TestParseOptSubject(t *testing.T) {
cfg := &config.EventExporterCfg{
ID: "nats_exporter",
Type: "nats",
Attempts: 2,
ConcurrentRequests: 2,
}
opts := map[string]interface{}{
utils.NatsSubject: "nats_subject",
}
nodeID := "node_id1"
connTimeout := 2 * time.Second
dc, err := newEEMetrics("Local")
if err != nil {
t.Error(err)
}
pstr, err := NewNatsEE(cfg, nodeID, connTimeout, dc)
if err != nil {
t.Error(err)
}
err = pstr.parseOpt(opts, nodeID, connTimeout)
if err != nil {
t.Error(err)
}
if pstr.subject != opts[utils.NatsSubject] {
t.Errorf("Expected %v \n but received \n %v", opts[utils.NatsSubject], pstr.subject)
}
}
func TestGetNatsOptsJWT(t *testing.T) {
opts := map[string]interface{}{
utils.NatsJWTFile: "jwtfile",
// utils.NatsSeedFile: "file",
}
nodeID := "node_id1"
connTimeout := 2 * time.Second
_, err := GetNatsOpts(opts, nodeID, connTimeout)
if err != nil {
t.Error(err)
}
}