integration tests for janus agent

This commit is contained in:
gezimbll
2024-04-26 10:44:20 -04:00
committed by Dan Christian Bogos
parent 6ef3dc8599
commit 82b0e02788
4 changed files with 298 additions and 10 deletions

View File

@@ -0,0 +1,218 @@
//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 <http://www.gnu.org/licenses/>
*/
package agents
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os/exec"
"path"
"reflect"
"testing"
"github.com/cgrates/cgrates/config"
"github.com/cgrates/cgrates/engine"
"github.com/cgrates/cgrates/utils"
janus "github.com/cgrates/janusgo"
)
var (
janCfgPath string
janCfgDIR string
janCfg *config.CGRConfig
janhttpC *http.Client // so we can cache the connection
)
func TestJanusit(t *testing.T) {
switch *utils.DBType {
case utils.MetaInternal:
janCfgDIR = "janus_agent"
case utils.MetaMySQL, utils.MetaMongo, utils.MetaPostgres:
t.SkipNow()
default:
t.Fatal("Unknown Database type")
}
file, err := exec.LookPath("/opt/janus/bin/janus")
if err != nil || file == "" {
t.SkipNow()
}
janCfgPath = path.Join(*utils.DataDir, "conf", "samples", janCfgDIR)
janCfg, err = config.NewCGRConfigFromPath(janCfgPath)
if err != nil {
t.Fatal("could not start init ", err.Error())
}
cmd := exec.Command(file)
if err := cmd.Start(); err != nil {
t.Fatal(err)
}
defer cmd.Wait()
janhttpC = http.DefaultClient
if err := engine.InitDataDb(janCfg); err != nil {
t.Fatal(err)
}
if err := engine.InitStorDb(janCfg); err != nil {
t.Fatal(err)
}
if _, err := engine.StopStartEngine(janCfgPath, *utils.WaitRater); err != nil {
t.Fatal(err)
}
var sessionMsg janus.SuccessMsg
id := utils.GenUUID()
msg := janus.BaseMsg{
Type: "create",
ID: id,
}
data, err := json.Marshal(&msg)
if err != nil {
t.Error(err)
}
url := fmt.Sprintf("http://%s%s", janCfg.ListenCfg().HTTPListen, janCfg.JanusAgentCfg().URL)
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(data))
if err != nil {
t.Error(err)
}
res, err := janhttpC.Do(req)
if err != nil {
t.Error(err)
}
defer res.Body.Close()
exp := janus.SuccessMsg{
Type: "success",
ID: id,
}
body, err := io.ReadAll(res.Body)
if err != nil {
t.Error(err)
}
if err := json.Unmarshal(body, &sessionMsg); err != nil {
t.Error(err)
}
if exp.ID != id || exp.Type != "success" {
t.Error(err)
}
var pluginMsg janus.SuccessMsg
id = utils.GenUUID()
msg = janus.BaseMsg{
Type: "attach",
Plugin: "janus.plugin.echotest",
ID: id,
}
url = fmt.Sprintf("http://%s%s/%d", janCfg.ListenCfg().HTTPListen, janCfg.JanusAgentCfg().URL, sessionMsg.Data.ID)
data, err = json.Marshal(&msg)
if err != nil {
t.Error(err)
}
req, err = http.NewRequest(http.MethodPost, url, bytes.NewBuffer(data))
if err != nil {
t.Error(err)
}
res, err = janhttpC.Do(req)
if err != nil {
t.Error(err)
}
defer res.Body.Close()
exp = janus.SuccessMsg{
Type: "success",
ID: id,
}
body, err = io.ReadAll(res.Body)
if err != nil {
t.Error(err)
}
if err := json.Unmarshal(body, &pluginMsg); err != nil {
t.Error(err)
}
if exp.ID != id || exp.Type != "success" {
t.Error(err)
}
id = utils.GenUUID()
messagePlugin := janus.HandlerMessage{
BaseMsg: janus.BaseMsg{
Type: "message",
ID: id,
},
Body: map[string]any{
"audio": true,
"video": true,
},
}
data, err = json.Marshal(&messagePlugin)
if err != nil {
t.Error(err)
}
url = fmt.Sprintf("http://%s%s/%d/%d", janCfg.ListenCfg().HTTPListen, janCfg.JanusAgentCfg().URL, sessionMsg.Data.ID, pluginMsg.Data.ID)
req, err = http.NewRequest(http.MethodPost, url, bytes.NewBuffer(data))
if err != nil {
t.Error(err)
}
res, err = janhttpC.Do(req)
if err != nil {
t.Error(err)
}
defer res.Body.Close()
expAck := janus.AckMsg{
Type: "ack",
ID: id,
Hint: "I'm taking my time!",
}
body, err = io.ReadAll(res.Body)
if err != nil {
t.Error(err)
}
var ackMsg janus.AckMsg
if err := json.Unmarshal(body, &ackMsg); err != nil {
t.Error(err)
}
if !reflect.DeepEqual(ackMsg, expAck) {
t.Errorf("Expected <%s> ,got <%s> instead", utils.ToJSON(expAck), utils.ToJSON(ackMsg))
}
if err := engine.KillEngine(*utils.WaitRater); err != nil {
t.Error(err)
}
}

View File

@@ -0,0 +1,73 @@
{
// CGRateS Configuration file
//
"general": {
"log_level": 7,
},
"listen": {
"rpc_json": ":2012",
"rpc_gob": ":2013",
"http": ":2080",
},
"stor_db": {
"db_type": "*internal",
},
"rals": {
"enabled": true,
"max_increments":3000000,
},
"schedulers": {
"enabled": true,
},
"cdrs": {
"enabled": true,
"chargers_conns": ["*internal"],
"rals_conns": ["*internal"],
},
"attributes": {
"enabled": true,
},
"chargers": {
"enabled": true,
"attributes_conns": ["*internal"],
},
"sessions": {
"enabled": true,
"attributes_conns": ["*localhost"],
"cdrs_conns": ["*localhost"],
"rals_conns": ["*localhost"],
"chargers_conns": ["*internal"],
},
"apiers": {
"enabled": true,
"scheduler_conns": ["*internal"],
},
"janus_agent":{
"enabled":true,
"url":"/janus",
"janus_conns":[
{
"address":"ws://localhost:8188"
}
]
}
}

7
go.mod
View File

@@ -1,6 +1,6 @@
module github.com/cgrates/cgrates
go 1.22
go 1.22.1
// replace github.com/cgrates/radigo => ../radigo
@@ -21,7 +21,7 @@ require (
github.com/cgrates/baningo v0.0.0-20210413080722-004ffd5e429f
github.com/cgrates/birpc v1.3.1-0.20211117095917-5b0ff29f3084
github.com/cgrates/fsock v0.0.0-20240322171959-35309017b3e0
github.com/cgrates/janusgo v0.0.0-20240411150649-85fbf3213b5e
github.com/cgrates/janusgo v0.0.0-20240425092925-a3b9aa99e793
github.com/cgrates/kamevapi v0.0.0-20240307160311-26273f03eedf
github.com/cgrates/ltcache v0.0.0-20240411152156-e673692056db
github.com/cgrates/radigo v0.0.0-20240123163129-491c899df727
@@ -94,7 +94,6 @@ require (
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.2 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/ishidawataru/sctp v0.0.0-20190922091402-408ec287e38c // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
@@ -110,7 +109,6 @@ require (
github.com/nats-io/nkeys v0.4.5 // indirect
github.com/nats-io/nuid v1.0.1 // indirect
github.com/pierrec/lz4/v4 v4.1.15 // indirect
github.com/rs/xid v1.5.0 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
@@ -130,4 +128,5 @@ require (
google.golang.org/genproto/googleapis/rpc v0.0.0-20240304161311-37d4d3c04a78 // indirect
google.golang.org/grpc v1.62.1 // indirect
google.golang.org/protobuf v1.33.0 // indirect
nhooyr.io/websocket v1.8.11 // indirect
)

10
go.sum
View File

@@ -68,8 +68,8 @@ github.com/cgrates/birpc v1.3.1-0.20211117095917-5b0ff29f3084 h1:YIEepjEOjeHaFre
github.com/cgrates/birpc v1.3.1-0.20211117095917-5b0ff29f3084/go.mod h1:z/PmNnDPqSQALedKJv5T8+eXIq6XHa9J0St1YsvAVns=
github.com/cgrates/fsock v0.0.0-20240322171959-35309017b3e0 h1:rnSM0tG6Cl8GXjyBQXw78WEaJolKfZqHTXkOCZh+w/k=
github.com/cgrates/fsock v0.0.0-20240322171959-35309017b3e0/go.mod h1:bKByLko2HF33K+PbiiToAgevrrbr96C+7Pp3HGS6oag=
github.com/cgrates/janusgo v0.0.0-20240411150649-85fbf3213b5e h1:a0LmV+CiWKoLCyCMwOGSJl5rvs35+mdPbE/3QSh38LI=
github.com/cgrates/janusgo v0.0.0-20240411150649-85fbf3213b5e/go.mod h1:T/qWqdGS2VNOTrXN5PGXymbzPxvEJSZgVlFeSg3B3DY=
github.com/cgrates/janusgo v0.0.0-20240425092925-a3b9aa99e793 h1:sXN6HgkrtnFF4KaxGaoyYXG6KhIUs0i8ic7I5bcEgos=
github.com/cgrates/janusgo v0.0.0-20240425092925-a3b9aa99e793/go.mod h1:XBQDDjrIn+RCS4PDApYjTWwdp51NbqYfUGAYtzSB5ag=
github.com/cgrates/kamevapi v0.0.0-20240307160311-26273f03eedf h1:GbMJzvtwdX1OCEmsqSts/cRCIcIMvo8AYtC2dQExWlg=
github.com/cgrates/kamevapi v0.0.0-20240307160311-26273f03eedf/go.mod h1:oEq/JbubkOD2pXHvDy4r7519NkxriONisrnVpkCaNJw=
github.com/cgrates/ltcache v0.0.0-20240411152156-e673692056db h1:JRgzMS5kJ1WxaveoZ1YG/FowUDxFQXD3GjCHR7rH0Gk=
@@ -165,8 +165,6 @@ github.com/googleapis/gax-go/v2 v2.12.2 h1:mhN09QQW1jEWeMF74zGR81R30z4VJzjZsfkUh
github.com/googleapis/gax-go/v2 v2.12.2/go.mod h1:61M8vcyyXR2kqKFxKrfA22jaA8JGF7Dc8App1U3H6jc=
github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75 h1:f0n1xnMSmBLzVfsMMvriDyA75NB/oBgILX2GcHXIQzY=
github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75/go.mod h1:g2644b03hfBX9Ov0ZBDgXXens4rxSxmqFBbhvKv2yVA=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/ishidawataru/sctp v0.0.0-20190922091402-408ec287e38c h1:PwVcPU2rqkJIG0Lz/UGbGcbfi/HhEbOIId+w4xkbGHQ=
@@ -228,8 +226,6 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/rabbitmq/amqp091-go v1.9.0 h1:qrQtyzB4H8BQgEuJwhmVQqVHB9O4+MNDJCCAcpc3Aoo=
github.com/rabbitmq/amqp091-go v1.9.0/go.mod h1:+jPrT9iY2eLjRaMSRHUhc3z14E/l85kv/f+6luSD3pc=
github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/segmentio/kafka-go v0.4.44 h1:Vjjksniy0WSTZ7CuVJrz1k04UoZeTc77UV6Yyk6tLY4=
github.com/segmentio/kafka-go v0.4.44/go.mod h1:HjF6XbOKh0Pjlkr5GVZxt6CsjjwnmhVOfURM5KMd8qg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
@@ -413,3 +409,5 @@ gorm.io/gorm v1.25.5 h1:zR9lOiiYf09VNh5Q1gphfyia1JpiClIWG9hQaxB/mls=
gorm.io/gorm v1.25.5/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
nhooyr.io/websocket v1.8.11 h1:f/qXNc2/3DpoSZkHt1DQu6rj4zGC8JmkkLkWss0MgN0=
nhooyr.io/websocket v1.8.11/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c=