From de96c183cfef6ba60147acee21db69da03d0ee4a Mon Sep 17 00:00:00 2001 From: DanB Date: Fri, 13 Mar 2015 19:16:11 +0100 Subject: [PATCH] Framework for automated call testing --- apier/v1/apier.go | 2 + engine/libtest.go | 77 ++++++++++++++++++++++++ general_tests/multiplecdrc_local_test.go | 1 + 3 files changed, 80 insertions(+) diff --git a/apier/v1/apier.go b/apier/v1/apier.go index 7fa3e7329..763a36614 100644 --- a/apier/v1/apier.go +++ b/apier/v1/apier.go @@ -916,6 +916,7 @@ func (self *ApierV1) LoadTariffPlanFromFolder(attrs utils.AttrLoadTpFromFolder, for idx, dc := range dcs { dcsKeys[idx] = engine.DERIVEDCHARGERS_PREFIX + dc } + engine.Logger.Info("ApierV1.LoadTariffPlanFromFolder, reloading cache.") if err := self.RatingDb.CacheRating(dstKeys, rpKeys, rpfKeys, rpAlsKeys, lcrKeys); err != nil { return err } @@ -923,6 +924,7 @@ func (self *ApierV1) LoadTariffPlanFromFolder(attrs utils.AttrLoadTpFromFolder, return err } if self.Sched != nil { + engine.Logger.Info("ApierV1.LoadTariffPlanFromFolder, reloading scheduler.") self.Sched.LoadActionTimings(self.AccountDb) self.Sched.Restart() } diff --git a/engine/libtest.go b/engine/libtest.go index 926e55aa6..2eea6d2d5 100644 --- a/engine/libtest.go +++ b/engine/libtest.go @@ -19,9 +19,13 @@ along with this program. If not, see package engine import ( + "bytes" "fmt" "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/utils" + "github.com/kr/pty" + "io" + "os" "os/exec" "path" "time" @@ -106,3 +110,76 @@ func LoadTariffPlanFromFolder(tpPath string, ratingDb RatingStorage, accountingD } return nil } + +type PjsuaAccount struct { + Id, Username, Password, Realm, Registrar string +} + +// Returns file reference where we can write to control pjsua in terminal +func StartPjsuaListener(acnts []*PjsuaAccount, waitMs int) (*os.File, error) { + cmdArgs := []string{"--local-port=5070", "--null-audio", "--auto-answer=200", "--max-calls=32", "--app-log-level=0"} + for idx, acnt := range acnts { + if idx != 0 { + cmdArgs = append(cmdArgs, "--next-account") + } + cmdArgs = append(cmdArgs, "--id="+acnt.Id, "--registrar="+acnt.Registrar, "--username="+acnt.Username, "--password="+acnt.Password, "--realm="+acnt.Realm) + } + pjsuaPath, err := exec.LookPath("pjsua") + if err != nil { + return nil, err + } + pjsua := exec.Command(pjsuaPath, cmdArgs...) + fPty, err := pty.Start(pjsua) + if err != nil { + return nil, err + } + buf := new(bytes.Buffer) + io.Copy(os.Stdout, buf) // Free the content since otherwise pjsua will not start + time.Sleep(time.Duration(waitMs) * time.Millisecond) // Give time to rater to fire up + return fPty, nil +} + +func PjsuaCallUri(acnt *PjsuaAccount, dstUri string, callDur time.Duration, localPort int) error { + cmdArgs := []string{"--null-audio", "--app-log-level=0", fmt.Sprintf("--local-port=%d", localPort), fmt.Sprintf("--duration=%d", int(callDur.Seconds())), + "--id=" + acnt.Id, "--username=" + acnt.Username, "--password=" + acnt.Password, "--realm=" + acnt.Realm, dstUri} + pjsuaPath, err := exec.LookPath("pjsua") + if err != nil { + return err + } + pjsua := exec.Command(pjsuaPath, cmdArgs...) + fPty, err := pty.Start(pjsua) + if err != nil { + return err + } + buf := new(bytes.Buffer) + io.Copy(os.Stdout, buf) + go func() { + time.Sleep(callDur + (time.Duration(2) * time.Second)) + fPty.Write([]byte("q\n")) // Destroy the listener + }() + return nil +} + +func KillFreeSWITCH(waitMs int) error { + if err := exec.Command("pkill", "freeswitch").Run(); err != nil { + return err + } + time.Sleep(time.Duration(waitMs) * time.Millisecond) + return nil +} + +func StopFreeSWITCH(scriptPath string, waitStop int) error { + if err := exec.Command(scriptPath, "stop").Run(); err != nil { + return err + } + return nil +} + +func StartFreeSWITCH(scriptPath string, waitMs int) error { + KillFreeSWITCH(1000) + if err := exec.Command(scriptPath, "start").Run(); err != nil { + return err + } + time.Sleep(time.Duration(waitMs) * time.Millisecond) // Give time to rater to fire up + return nil +} diff --git a/general_tests/multiplecdrc_local_test.go b/general_tests/multiplecdrc_local_test.go index 769e49131..ee418082b 100644 --- a/general_tests/multiplecdrc_local_test.go +++ b/general_tests/multiplecdrc_local_test.go @@ -41,6 +41,7 @@ var cfg *config.CGRConfig var rater *rpc.Client var testLocal = flag.Bool("local", false, "Perform the tests only on local test environment, not by default.") // This flag will be passed here via "go test -local" args +var testCalls = flag.Bool("calls", false, "Run test calls simulation, not by default.") var dataDir = flag.String("data_dir", "/usr/share/cgrates", "CGR data dir path here") var storDbType = flag.String("stordb_type", "mysql", "The type of the storDb database ") var waitRater = flag.Int("wait_rater", 100, "Number of miliseconds to wait for rater to start and cache")