mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-20 14:48:43 +05:00
Testing console commands in /cmd/cgr-console/
This commit is contained in:
committed by
Dan Christian Bogos
parent
8748ad29c8
commit
1fb4601b51
316
cmd/cgr-console/cgr-console_it_test.go
Normal file
316
cmd/cgr-console/cgr-console_it_test.go
Normal file
@@ -0,0 +1,316 @@
|
||||
// +build integration
|
||||
|
||||
/*
|
||||
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 main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"os/exec"
|
||||
"path"
|
||||
"reflect"
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
"github.com/cgrates/cgrates/config"
|
||||
"github.com/cgrates/cgrates/engine"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
var (
|
||||
dataDir = flag.String("data_dir", "/usr/share/cgrates", "CGR data dir path here")
|
||||
dbType = flag.String("dbtype", utils.MetaInternal, "The type of DataBase (Internal/Mongo/mySql)")
|
||||
waitRater = flag.Int("wait_rater", 100, "Number of milliseconds to wait for rater to start and cache")
|
||||
// encoding = flag.String("rpc", utils.MetaJSON, "what encoding whould be used for rpc comunication")
|
||||
)
|
||||
|
||||
var (
|
||||
cnslItCfgPath string
|
||||
cnslItDirPath string
|
||||
cnslItCfg *config.CGRConfig
|
||||
cnslItTests = []func(t *testing.T){
|
||||
testConsoleItLoadConfig,
|
||||
testConsoleItInitDataDB,
|
||||
testConsoleItInitStorDB,
|
||||
testConsoleItStartEngine,
|
||||
testConsoleItLoadTP,
|
||||
testConsoleItCacheClear,
|
||||
testConsoleItThresholdsProfileIds,
|
||||
testConsoleItRatesProfileIds,
|
||||
testConsoleItResourcesProfileIds,
|
||||
testConsoleItRoutesProfileIds,
|
||||
testConsoleItCacheReload,
|
||||
testConsoleItFilterIds,
|
||||
testConsoleItCacheHasItem,
|
||||
testConsoleItStatsMetrics,
|
||||
testConsoleItGetJsonSection,
|
||||
testConsolieItResourcesAuthorize,
|
||||
testConsoleItKillEngine,
|
||||
}
|
||||
)
|
||||
|
||||
func TestConsoleItTests(t *testing.T) {
|
||||
switch *dbType {
|
||||
case utils.MetaInternal:
|
||||
t.SkipNow()
|
||||
case utils.MetaMySQL:
|
||||
cnslItDirPath = "tutmysql"
|
||||
case utils.MetaMongo:
|
||||
cnslItDirPath = "tutmongo"
|
||||
case utils.MetaPostgres:
|
||||
t.SkipNow()
|
||||
default:
|
||||
t.Fatal("Unknown database type")
|
||||
}
|
||||
for _, test := range cnslItTests {
|
||||
t.Run("TestConsoleItTests", test)
|
||||
}
|
||||
}
|
||||
|
||||
func testConsoleItLoadConfig(t *testing.T) {
|
||||
var err error
|
||||
cnslItCfgPath = path.Join(*dataDir, "conf", "samples", cnslItDirPath)
|
||||
if cnslItCfg, err = config.NewCGRConfigFromPath(cnslItCfgPath); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func testConsoleItInitDataDB(t *testing.T) {
|
||||
if err := engine.InitDataDB(cnslItCfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func testConsoleItInitStorDB(t *testing.T) {
|
||||
if err := engine.InitStorDB(cnslItCfg); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func testConsoleItStartEngine(t *testing.T) {
|
||||
if _, err := engine.StartEngine(cnslItCfgPath, *waitRater); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func testConsoleItLoadTP(t *testing.T) {
|
||||
cmd := exec.Command("cgr-loader", "-config_path="+cnslItCfgPath, "-path="+path.Join(*dataDir, "tariffplans", "tutorial"))
|
||||
output := bytes.NewBuffer(nil)
|
||||
cmd.Stdout = output
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Log(cmd.Args)
|
||||
t.Log(output.String())
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func testConsoleItCacheClear(t *testing.T) {
|
||||
cmd := exec.Command("cgr-console", "cache_clear")
|
||||
output := bytes.NewBuffer(nil)
|
||||
// expected := bytes.NewBuffer(nil)
|
||||
cmd.Stdout = output
|
||||
// expected.WriteString(`"OK"`)
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Log(cmd.Args)
|
||||
t.Log(output.String())
|
||||
t.Fatal(err)
|
||||
}
|
||||
// } else if output.String() != expected.String() {
|
||||
// fmt.Printf("%T and %T", output.String(), expected.String())
|
||||
// t.Fatalf(`Expected "OK" but received %s`, output.String())
|
||||
// }
|
||||
}
|
||||
|
||||
func testConsoleItThresholdsProfileIds(t *testing.T) {
|
||||
cmd := exec.Command("cgr-console", "thresholds_profile_ids", `Tenant="cgrates.org"`)
|
||||
output := bytes.NewBuffer(nil)
|
||||
cmd.Stdout = output
|
||||
expected := []string{"THD_ACNT_1001", "THD_ACNT_1002"}
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Log(cmd.Args)
|
||||
t.Log(output.String())
|
||||
t.Fatal(err)
|
||||
}
|
||||
var rcv []string
|
||||
if err := json.NewDecoder(output).Decode(&rcv); err != nil {
|
||||
t.Error(output.String())
|
||||
t.Fatal(err)
|
||||
}
|
||||
sort.Strings(rcv)
|
||||
if !reflect.DeepEqual(rcv, expected) {
|
||||
t.Fatalf("Expected %+q \n but received \n %+q", expected, rcv)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func testConsoleItResourcesProfileIds(t *testing.T) {
|
||||
cmd := exec.Command("cgr-console", "resources_profile_ids", "Tenant", "cgrates.org")
|
||||
output := bytes.NewBuffer(nil)
|
||||
cmd.Stdout = output
|
||||
expected := []string{"ResGroup1"}
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Log(cmd.Args)
|
||||
t.Log(output.String())
|
||||
t.Fatal(err)
|
||||
}
|
||||
var rcv []string
|
||||
if err := json.NewDecoder(output).Decode(&rcv); err != nil {
|
||||
t.Error(output.String())
|
||||
t.Fatal(err)
|
||||
}
|
||||
sort.Strings(rcv)
|
||||
if !reflect.DeepEqual(rcv, expected) {
|
||||
t.Fatalf("Expected %+q \n but received \n %+q", expected, rcv)
|
||||
}
|
||||
// fmt.Println(output.String())
|
||||
}
|
||||
|
||||
func testConsoleItRatesProfileSet(t *testing.T) {
|
||||
cmd := exec.Command("cgr-console", "rates_profile_set", `Tenant="cgrates.org"`, `ID="123"`, `Rates={"RT_WEEK":{"ID":"RT_WEEK"}}`)
|
||||
output := bytes.NewBuffer(nil)
|
||||
expected := "OK"
|
||||
cmd.Stdout = output
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Log(cmd.Args)
|
||||
t.Log(output.String())
|
||||
t.Fatal(err)
|
||||
}
|
||||
var rcv string
|
||||
if err := json.NewDecoder(output).Decode(&rcv); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !reflect.DeepEqual(rcv, expected) {
|
||||
t.Fatalf("Expected %s \n but received \n %s", expected, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func testConsoleItRatesProfileIds(t *testing.T) {
|
||||
cmd := exec.Command("cgr-console", "rates_profile_ids", "Tenant", "cgrates.org")
|
||||
output := bytes.NewBuffer(nil)
|
||||
cmd.Stdout = output
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Log(cmd.Args)
|
||||
t.Log(output.String())
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func testConsoleItRoutesProfileIds(t *testing.T) {
|
||||
cmd := exec.Command("cgr-console", "routes_profile_ids", "Tenant", "cgrates.org")
|
||||
output := bytes.NewBuffer(nil)
|
||||
cmd.Stdout = output
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Log(cmd.Args)
|
||||
t.Log(output.String())
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func testConsoleItCacheReload(t *testing.T) {
|
||||
cmd := exec.Command("cgr-console", "cache_reload", "Tenant", "cgrates.org")
|
||||
output := bytes.NewBuffer(nil)
|
||||
expected := "OK"
|
||||
cmd.Stdout = output
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Log(cmd.Args)
|
||||
t.Log(output.String())
|
||||
t.Fatal(err)
|
||||
}
|
||||
var rcv string
|
||||
if err := json.NewDecoder(output).Decode(&rcv); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if !reflect.DeepEqual(rcv, expected) {
|
||||
t.Fatalf("Expected %s \n but received \n %s", expected, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func testConsoleItFilterIds(t *testing.T) {
|
||||
cmd := exec.Command("cgr-console", "filter_ids", "Tenant", "cgrates.org")
|
||||
output := bytes.NewBuffer(nil)
|
||||
cmd.Stdout = output
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Log(cmd.Args)
|
||||
t.Log(output.String())
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func testConsoleItCacheHasItem(t *testing.T) {
|
||||
cmd := exec.Command("cgr-console", "cache_has_item", "Tenant", "cgrates.org")
|
||||
output := bytes.NewBuffer(nil)
|
||||
cmd.Stdout = output
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Log(cmd.Args)
|
||||
t.Log(output.String())
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func testConsoleItStatsMetrics(t *testing.T) {
|
||||
cmd := exec.Command("cgr-console", "stats_metrics", "ID", "Stats2")
|
||||
output := bytes.NewBuffer(nil)
|
||||
cmd.Stdout = output
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Log(cmd.Args)
|
||||
t.Log(output.String())
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func testConsoleItGetJsonSection(t *testing.T) {
|
||||
cmd := exec.Command("cgr-console", "get_json_section", "Section", "general")
|
||||
output := bytes.NewBuffer(nil)
|
||||
cmd.Stdout = output
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Log(cmd.Args)
|
||||
t.Log(output.String())
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func testConsolieItResourcesAuthorize(t *testing.T) {
|
||||
cmd := exec.Command("cgr-console", "resources_authorize", `Tenant="cgrates.org"`, `ID="123"`, `Event={"Account":"1001"}`, `UsageID="usageID"`)
|
||||
output := bytes.NewBuffer(nil)
|
||||
cmd.Stdout = output
|
||||
expected := "ResGroup1"
|
||||
if err := cmd.Run(); err != nil {
|
||||
t.Log(cmd.Args)
|
||||
t.Log(output.String())
|
||||
t.Fatal(err)
|
||||
}
|
||||
var rcv string
|
||||
if err := json.NewDecoder(output).Decode(&rcv); err != nil {
|
||||
t.Error(output.String())
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !reflect.DeepEqual(rcv, expected) {
|
||||
t.Fatalf("Expected %+q \n but received \n %+q", expected, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
// func testConsoleIt
|
||||
|
||||
func testConsoleItKillEngine(t *testing.T) {
|
||||
if err := engine.KillEngine(*waitRater); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user