//go:build integration // +build integration /* Real-time Online/Offline Charging System (OCS) 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 ers import ( "context" "fmt" "reflect" "testing" "time" "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/engine" "github.com/cgrates/cgrates/utils" kafka "github.com/segmentio/kafka-go" ) var ( rdrEvents chan *erEvent rdrErr chan error rdrExit chan struct{} rdr EventReader ) func TestKafkaER(t *testing.T) { cfg, err := config.NewCGRConfigFromJSONStringWithDefaults(`{ "ers": { // EventReaderService "enabled": true, // starts the EventReader service: "sessions_conns":["*localhost"], "readers": [ { "id": "kafka", // identifier of the EventReader profile "type": "*kafkaJSONMap", // reader type <*fileCSV> "run_delay": "-1", // sleep interval in seconds between consecutive runs, -1 to use automation via inotify or 0 to disable running all together "concurrent_requests": 1024, // maximum simultaneous requests/files to process, 0 for unlimited "source_path": "localhost:9092", // read data from this path // "processed_path": "/var/spool/cgrates/ers/out", // move processed data here "tenant": "cgrates.org", // tenant used by import "filters": [], // limit parsing based on the filters "flags": [], // flags to influence the event processing "fields":[ // import fields template, tag will match internally CDR field, in case of .csv value will be represented by index of the field value {"tag": "CGRID", "type": "*composed", "value": "~*req.CGRID", "path": "*cgreq.CGRID"}, ], }, ], }, }`) if err != nil { t.Fatal(err) } if err := cfg.CheckConfigSanity(); err != nil { t.Fatal(err) } rdrEvents = make(chan *erEvent, 1) rdrErr = make(chan error, 1) rdrExit = make(chan struct{}, 1) if rdr, err = NewKafkaER(cfg, 1, rdrEvents, make(chan *erEvent, 1), rdrErr, new(engine.FilterS), rdrExit); err != nil { t.Fatal(err) } w := kafka.NewWriter(kafka.WriterConfig{ Brokers: []string{"localhost:9092"}, Topic: utils.KafkaDefaultTopic, WriteTimeout: 5 * time.Second, ReadTimeout: 5 * time.Second, }) randomCGRID := utils.UUIDSha1Prefix() w.WriteMessages(context.Background(), kafka.Message{ Key: []byte(randomCGRID), // for the momment we do not proccess the key Value: []byte(fmt.Sprintf(`{"CGRID": "%s"}`, randomCGRID)), }, ) w.Close() rdr.Serve() select { case err = <-rdrErr: t.Error(err) case ev := <-rdrEvents: if ev.rdrCfg.ID != "kafka" { t.Errorf("Expected 'kakfa' received `%s`", ev.rdrCfg.ID) } expected := &utils.CGREvent{ Tenant: "cgrates.org", ID: ev.cgrEvent.ID, Event: map[string]interface{}{ "CGRID": randomCGRID, }, APIOpts: map[string]interface{}{}, } if !reflect.DeepEqual(ev.cgrEvent, expected) { t.Errorf("Expected %s ,received %s", utils.ToJSON(expected), utils.ToJSON(ev.cgrEvent)) } case <-time.After(30 * time.Second): t.Fatal("Timeout") } close(rdrExit) }