Files
cgrates/ees/elastic_test.go
ionutboangiu 2b5a3e5a5e elasticsearch: switch to fully-typed API
- index request options are now used directly during ExportEvent. They are
  passed to the request as options only if they were configured in the first
  place.
- implement PrepareMap and PrepareOrderMap methods for the elastic exporter.
  bytePreparing methods are not needed anymore as the Event map can be exported
  directly.
- elasticsearch.Client -> elasticsearch.TypedClient
- rename prepareOpts -> parseClientOpts
2024-11-27 16:50:47 +01:00

125 lines
3.1 KiB
Go

/*
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 <http://www.gnu.org/licenses/>
*/
package ees
import (
"reflect"
"testing"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/utils"
elasticsearch "github.com/elastic/go-elasticsearch/v8"
)
func TestGetMetrics(t *testing.T) {
dc, err := newEEMetrics("Local")
if err != nil {
t.Error(err)
}
ee := &ElasticEE{
dc: dc,
}
if rcv := ee.GetMetrics(); !reflect.DeepEqual(rcv, ee.dc) {
t.Errorf("Expected %+v \n but got %+v", utils.ToJSON(rcv), utils.ToJSON(ee.dc))
}
}
func TestInitClient(t *testing.T) {
ee := &ElasticEE{
cfg: &config.EventExporterCfg{
ExportPath: "/\x00",
Opts: &config.EventExporterOpts{
Els: &config.ElsOpts{},
RPC: &config.RPCOpts{},
},
},
}
if err := ee.parseClientOpts(); err != nil {
t.Error(err)
}
errExpect := `cannot create client: cannot parse url: parse "/\x00": net/url: invalid control character in URL`
if err := ee.Connect(); err == nil || err.Error() != errExpect {
t.Errorf("Expected %+v \n but got %+v", errExpect, err)
}
}
func TestElasticExportEventErr(t *testing.T) {
cgrCfg := config.NewDefaultCGRConfig()
dc, err := newEEMetrics("Local")
if err != nil {
t.Error(err)
}
eEe, err := NewElasticEE(cgrCfg.EEsCfg().Exporters[0], dc)
if err != nil {
t.Error(err)
}
if err = eEe.Connect(); err != nil {
t.Error(err)
}
errExpect := `an error happened during the Index query execution: unsupported protocol scheme ""`
if err := eEe.ExportEvent([]byte{}, ""); err == nil || err.Error() != errExpect {
t.Errorf("Expected %q but got %q", errExpect, err)
}
}
func TestElasticClose(t *testing.T) {
elasticEE := &ElasticEE{
client: &elasticsearch.TypedClient{},
}
err := elasticEE.Close()
if elasticEE.client != nil {
t.Errorf("expected eClnt to be nil, got %v", elasticEE.client)
}
if err != nil {
t.Errorf("expected no error, got %v", err)
}
}
func TestElasticConnect(t *testing.T) {
t.Run("ClientAlreadyExists", func(t *testing.T) {
elasticEE := &ElasticEE{
client: &elasticsearch.TypedClient{},
}
err := elasticEE.Connect()
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if elasticEE.client == nil {
t.Error("expected existing client to remain initialized")
}
})
t.Run("ClientDoesNotExist", func(t *testing.T) {
elasticEE := &ElasticEE{}
err := elasticEE.Connect()
if err != nil {
t.Errorf("expected no error, got %v", err)
}
if elasticEE.client == nil {
t.Error("expected client to be initialized")
}
})
}