From c82d5052223a4848b96e3112eac2ac6498b16295 Mon Sep 17 00:00:00 2001 From: nickolasdaniel Date: Thu, 9 Sep 2021 17:02:44 +0300 Subject: [PATCH] Testing nats in ees --- ees/nats_it_test.go | 30 ++++++ ees/nats_test.go | 224 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 254 insertions(+) create mode 100644 ees/nats_test.go diff --git a/ees/nats_it_test.go b/ees/nats_it_test.go index 0b42f3713..4781928e0 100644 --- a/ees/nats_it_test.go +++ b/ees/nats_it_test.go @@ -19,6 +19,7 @@ along with this program. If not, see 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") + } +} diff --git a/ees/nats_test.go b/ees/nats_test.go new file mode 100644 index 000000000..35951b935 --- /dev/null +++ b/ees/nats_test.go @@ -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 +*/ + +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) + } +}