mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-12 02:26:26 +05:00
Updated tests in utils
This commit is contained in:
@@ -121,7 +121,11 @@ func main() {
|
||||
return
|
||||
}
|
||||
if *version {
|
||||
fmt.Println(utils.GetCGRVersion())
|
||||
if rcv, err := utils.GetCGRVersion(); err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
fmt.Println(rcv)
|
||||
}
|
||||
return
|
||||
}
|
||||
var err error
|
||||
|
||||
@@ -394,9 +394,12 @@ func main() {
|
||||
if err := cgrEngineFlags.Parse(os.Args[1:]); err != nil {
|
||||
return
|
||||
}
|
||||
vers := utils.GetCGRVersion()
|
||||
if *version {
|
||||
fmt.Println(vers)
|
||||
if vers, err := utils.GetCGRVersion(); err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
fmt.Println(vers)
|
||||
}
|
||||
return
|
||||
}
|
||||
if *pidFile != "" {
|
||||
|
||||
@@ -112,7 +112,11 @@ func main() {
|
||||
return
|
||||
}
|
||||
if *version {
|
||||
fmt.Println(utils.GetCGRVersion())
|
||||
if rcv, err := utils.GetCGRVersion(); err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
fmt.Println(rcv)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -119,7 +119,11 @@ func main() {
|
||||
return
|
||||
}
|
||||
if *version {
|
||||
fmt.Println(utils.GetCGRVersion())
|
||||
if rcv, err := utils.GetCGRVersion(); err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
fmt.Println(rcv)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -159,7 +159,11 @@ func main() {
|
||||
return
|
||||
}
|
||||
if *version {
|
||||
fmt.Println(utils.GetCGRVersion())
|
||||
if rcv, err := utils.GetCGRVersion(); err != nil {
|
||||
fmt.Println(err)
|
||||
} else {
|
||||
fmt.Println(rcv)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -55,7 +55,10 @@ func (cS *CoreService) Status(arg *utils.TenantWithArgDispatcher, reply *map[str
|
||||
response[utils.NodeID] = config.CgrConfig().GeneralCfg().NodeID
|
||||
response[utils.MemoryUsage] = utils.SizeFmt(float64(memstats.HeapAlloc), "")
|
||||
response[utils.ActiveGoroutines] = runtime.NumGoroutine()
|
||||
response[utils.Version] = utils.GetCGRVersion()
|
||||
if response[utils.Version], err = utils.GetCGRVersion(); err != nil {
|
||||
utils.Logger.Err(err.Error())
|
||||
err = nil
|
||||
}
|
||||
response[utils.RunningSince] = utils.GetStartTime()
|
||||
response[utils.GoVersion] = runtime.Version()
|
||||
*reply = response
|
||||
|
||||
@@ -575,7 +575,7 @@ func GetEndOfMonth(ref time.Time) time.Time {
|
||||
|
||||
// formats number in K,M,G, etc.
|
||||
func SizeFmt(num float64, suffix string) string {
|
||||
if suffix == "" {
|
||||
if suffix == EmptyString {
|
||||
suffix = "B"
|
||||
}
|
||||
for _, unit := range []string{"", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi"} {
|
||||
@@ -592,7 +592,7 @@ func TimeIs0h(t time.Time) bool {
|
||||
}
|
||||
|
||||
func ParseHierarchyPath(path string, sep string) HierarchyPath {
|
||||
if sep == "" {
|
||||
if sep == EmptyString {
|
||||
for _, sep = range []string{"/", NestingSep} {
|
||||
if idx := strings.Index(path, sep); idx != -1 {
|
||||
break
|
||||
@@ -608,9 +608,9 @@ type HierarchyPath []string
|
||||
|
||||
func (h HierarchyPath) AsString(sep string, prefix bool) string {
|
||||
if len(h) == 0 {
|
||||
return ""
|
||||
return EmptyString
|
||||
}
|
||||
retStr := ""
|
||||
retStr := EmptyString
|
||||
for idx, itm := range h {
|
||||
if idx == 0 {
|
||||
if prefix {
|
||||
@@ -659,29 +659,24 @@ func CapitalizedMessage(errMessage string) (capStr string) {
|
||||
return
|
||||
}
|
||||
|
||||
func GetCGRVersion() (vers string) {
|
||||
func GetCGRVersion() (vers string, err error) {
|
||||
vers = fmt.Sprintf("%s %s", CGRateS, VERSION)
|
||||
if GitLastLog == "" {
|
||||
return
|
||||
return vers, nil
|
||||
}
|
||||
rdr := bytes.NewBufferString(GitLastLog)
|
||||
var commitHash string
|
||||
var commitDate time.Time
|
||||
for i := 0; i < 5; i++ { // read a maximum of 5 lines
|
||||
ln, err := rdr.ReadString('\n')
|
||||
var ln string
|
||||
ln, err = rdr.ReadString('\n')
|
||||
if err != nil {
|
||||
Logger.Err(fmt.Sprintf("Building version - error: <%s> reading line from file", err.Error()))
|
||||
return
|
||||
}
|
||||
if ln == "" {
|
||||
return
|
||||
return vers, fmt.Errorf("Building version - error: <%s> reading line from file", err.Error()) //or errorsNew()
|
||||
}
|
||||
if strings.HasPrefix(ln, "commit ") {
|
||||
commitSplt := strings.Split(ln, " ")
|
||||
if len(commitSplt) != 2 {
|
||||
Logger.Err("Building version - cannot extract commit hash")
|
||||
fmt.Println("Building version - cannot extract commit hash")
|
||||
return
|
||||
return vers, fmt.Errorf("Building version - cannot extract commit hash")
|
||||
}
|
||||
commitHash = commitSplt[1]
|
||||
continue
|
||||
@@ -689,39 +684,20 @@ func GetCGRVersion() (vers string) {
|
||||
if strings.HasPrefix(ln, "Date:") {
|
||||
dateSplt := strings.Split(ln, ": ")
|
||||
if len(dateSplt) != 2 {
|
||||
Logger.Err("Building version - cannot split commit date")
|
||||
fmt.Println("Building version - cannot split commit date")
|
||||
return
|
||||
return vers, fmt.Errorf("Building version - cannot split commit date")
|
||||
}
|
||||
commitDate, err = time.Parse("Mon Jan 2 15:04:05 2006 -0700", strings.TrimSpace(dateSplt[1]))
|
||||
if err != nil {
|
||||
Logger.Err(fmt.Sprintf("Building version - error: <%s> compiling commit date", err.Error()))
|
||||
fmt.Printf("Building version - error: <%s> compiling commit date\n", err.Error())
|
||||
return
|
||||
return vers, fmt.Errorf("Building version - error: <%s> compiling commit date", err.Error())
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
if commitHash == "" || commitDate.IsZero() {
|
||||
Logger.Err("Cannot find commitHash or commitDate information")
|
||||
return
|
||||
return vers, fmt.Errorf("Cannot find commitHash or commitDate information")
|
||||
}
|
||||
//CGRateS 0.9.1~rc8 git+73014da (2016-12-30T19:48:09+01:00)
|
||||
return fmt.Sprintf("%s %s git+%s (%s)", CGRateS, VERSION, commitHash[:7], commitDate.Format(time.RFC3339))
|
||||
}
|
||||
|
||||
// AppendToFile is a convenience function to be used for appending to an already existing file
|
||||
func AppendToFile(fName, text string) error {
|
||||
f, err := os.OpenFile(fName, os.O_APPEND|os.O_WRONLY, 0666)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := f.WriteString(text); err != nil {
|
||||
return err
|
||||
}
|
||||
f.Sync()
|
||||
f.Close()
|
||||
return nil
|
||||
return fmt.Sprintf("%s %s git+%s (%s)", CGRateS, VERSION, commitHash[:7], commitDate.Format(time.RFC3339)), nil
|
||||
}
|
||||
|
||||
func NewTenantID(tntID string) *TenantID {
|
||||
@@ -729,9 +705,6 @@ func NewTenantID(tntID string) *TenantID {
|
||||
return &TenantID{ID: tntID}
|
||||
}
|
||||
tIDSplt := strings.Split(tntID, CONCATENATED_KEY_SEP)
|
||||
if len(tIDSplt) == 1 { // only Tenant present
|
||||
return &TenantID{Tenant: tIDSplt[0]}
|
||||
}
|
||||
return &TenantID{Tenant: tIDSplt[0], ID: ConcatenatedKey(tIDSplt[1:]...)}
|
||||
}
|
||||
|
||||
|
||||
@@ -18,11 +18,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cgrates/rpcclient"
|
||||
)
|
||||
|
||||
func TestGetStartTime(t *testing.T) {
|
||||
@@ -811,6 +814,52 @@ func TestEndOfMonth(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSizeFmt(t *testing.T) {
|
||||
if str := SizeFmt(0, EmptyString); str != "0.0B" {
|
||||
t.Errorf("Expecting: 0.0B, received: %+q", str)
|
||||
}
|
||||
if str := SizeFmt(1023, EmptyString); str != "1023.0B" {
|
||||
t.Errorf("Expecting: 1023.0B, received: %+q", str)
|
||||
}
|
||||
if str := SizeFmt(1024, EmptyString); str != "1.0KiB" {
|
||||
t.Errorf("Expecting: 1.0KiB, received: %+q", str)
|
||||
}
|
||||
if str := SizeFmt(1048575, EmptyString); str != "1024.0KiB" {
|
||||
t.Errorf("Expecting: 1024.0KiB, received: %+q", str)
|
||||
}
|
||||
if str := SizeFmt(1048576, EmptyString); str != "1.0MiB" {
|
||||
t.Errorf("Expecting: 1.0MiB, received: %+q", str)
|
||||
}
|
||||
if str := SizeFmt(1073741823, EmptyString); str != "1024.0MiB" {
|
||||
t.Errorf("Expecting: 1024.0MiB, received: %+q", str)
|
||||
}
|
||||
if str := SizeFmt(1073741824, EmptyString); str != "1.0GiB" {
|
||||
t.Errorf("Expecting: 1.0GiB, received: %+q", str)
|
||||
}
|
||||
if str := SizeFmt(1099511627775, EmptyString); str != "1024.0GiB" {
|
||||
t.Errorf("Expecting: 1024.0GiB, received: %+q", str)
|
||||
}
|
||||
if str := SizeFmt(1099511627776, EmptyString); str != "1.0TiB" {
|
||||
t.Errorf("Expecting: 1.0TiB, received: %+q", str)
|
||||
}
|
||||
if str := SizeFmt(1125899906842623, EmptyString); str != "1024.0TiB" {
|
||||
t.Errorf("Expecting: 1024.0TiB, received: %+q", str)
|
||||
}
|
||||
if str := SizeFmt(1125899906842624, EmptyString); str != "1.0PiB" {
|
||||
t.Errorf("Expecting: 1.0PiB, received: %+q", str)
|
||||
}
|
||||
if str := SizeFmt(1152921504606847000, EmptyString); str != "1.0EiB" {
|
||||
t.Errorf("Expecting: 1.0EiB, received: %+q", str)
|
||||
}
|
||||
if str := SizeFmt(1180591620717411303424, EmptyString); str != "1.0ZiB" {
|
||||
t.Errorf("Expecting: 1.0ZiB, received: %+q", str)
|
||||
}
|
||||
if str := SizeFmt(9000000000000000000000000, EmptyString); str != "7.4YiB" {
|
||||
t.Errorf("Expecting: 7.4YiB, received: %+q", str)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestParseHierarchyPath(t *testing.T) {
|
||||
eHP := HierarchyPath([]string{"Root", "CGRateS"})
|
||||
if hp := ParseHierarchyPath("/Root/CGRateS/", ""); !reflect.DeepEqual(hp, eHP) {
|
||||
@@ -827,6 +876,11 @@ func TestHierarchyPathAsString(t *testing.T) {
|
||||
if hpStr := hp.AsString("/", true); hpStr != eStr {
|
||||
t.Errorf("Expecting: %q, received: %q", eStr, hpStr)
|
||||
}
|
||||
hp = HierarchyPath([]string{})
|
||||
if hpStr := hp.AsString(EmptyString, true); hpStr != EmptyString {
|
||||
t.Errorf("Expecting: %q, received: %q", EmptyString, hpStr)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestMaskSuffix(t *testing.T) {
|
||||
@@ -1035,11 +1089,115 @@ func TestGetCGRVersion(t *testing.T) {
|
||||
Author: DanB <danb@cgrates.org>
|
||||
Date: Fri Dec 30 19:48:09 2016 +0100
|
||||
|
||||
Fixes for db driver to avoid returning new values in case of errors`
|
||||
Fixes for db driver to avoid returning new values in case of errors
|
||||
`
|
||||
eVers := "CGRateS 0.9.1~rc8 git+73014da (2016-12-30T19:48:09+01:00)"
|
||||
if vers := GetCGRVersion(); vers != eVers {
|
||||
if vers, err := GetCGRVersion(); err != nil {
|
||||
t.Error(err)
|
||||
} else if vers != eVers {
|
||||
t.Errorf("Expecting: <%s>, received: <%s>", eVers, vers)
|
||||
}
|
||||
GitLastLog = ""
|
||||
if vers, err := GetCGRVersion(); err != nil {
|
||||
t.Error(err)
|
||||
} else if vers != "CGRateS 0.9.1~rc8" {
|
||||
t.Errorf("Expecting: <CGRateS 0.9.1~rc8>, received: <%s>", vers)
|
||||
}
|
||||
GitLastLog = "\n"
|
||||
if vers, err := GetCGRVersion(); err == nil || err.Error() != "Building version - error: <EOF> reading line from file" {
|
||||
t.Error(err)
|
||||
} else if vers != "CGRateS 0.9.1~rc8" {
|
||||
t.Errorf("Expecting: <CGRateS 0.9.1~rc8>, received: <%s>", vers)
|
||||
}
|
||||
GitLastLog = `commit . . .
|
||||
`
|
||||
if vers, err := GetCGRVersion(); err == nil || err.Error() != "Building version - cannot extract commit hash" {
|
||||
t.Error(err)
|
||||
} else if vers != "CGRateS 0.9.1~rc8" {
|
||||
t.Errorf("Expecting: <CGRateS 0.9.1~rc8>, received: <%s>", vers)
|
||||
}
|
||||
GitLastLog = `Date: : :
|
||||
`
|
||||
if vers, err := GetCGRVersion(); err == nil || err.Error() != "Building version - cannot split commit date" {
|
||||
t.Error(err)
|
||||
} else if vers != "CGRateS 0.9.1~rc8" {
|
||||
t.Errorf("Expecting: <CGRateS 0.9.1~rc8>, received: <%s>", vers)
|
||||
}
|
||||
GitLastLog = `Date: wrong format
|
||||
`
|
||||
if vers, err := GetCGRVersion(); err == nil || err.Error() != `Building version - error: <parsing time "wrong format" as "Mon Jan 2 15:04:05 2006 -0700": cannot parse "wrong format" as "Mon"> compiling commit date` {
|
||||
t.Error(err)
|
||||
} else if vers != "CGRateS 0.9.1~rc8" {
|
||||
t.Errorf("Expecting: <CGRateS 0.9.1~rc8>, received: <%s>", vers)
|
||||
}
|
||||
GitLastLog = `ommit 73014daa0c1d7edcb532d5fe600b8a20d588cdf8
|
||||
Author: DanB <danb@cgrates.org>
|
||||
Date: Fri Dec 30 19:48:09 2016 +0100
|
||||
|
||||
Fixes for db driver to avoid returning new values in case of errors
|
||||
`
|
||||
if vers, err := GetCGRVersion(); err == nil || err.Error() != "Cannot find commitHash or commitDate information" {
|
||||
t.Error(err)
|
||||
} else if vers != "CGRateS 0.9.1~rc8" {
|
||||
t.Errorf("Expecting: <CGRateS 0.9.1~rc8>, received: <%s>", vers)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewTenantID(t *testing.T) {
|
||||
eOut := &TenantID{ID: EmptyString}
|
||||
if rcv := NewTenantID(EmptyString); *rcv != *eOut {
|
||||
t.Errorf("Expecting: %+v, received %+v", eOut, rcv)
|
||||
}
|
||||
eOut = &TenantID{Tenant: "Test"}
|
||||
if rcv := NewTenantID("Test:"); *rcv != *eOut {
|
||||
t.Errorf("Expecting: %+v, received %+v", eOut, rcv)
|
||||
}
|
||||
eOut = &TenantID{Tenant: "cgrates.org", ID: "id"}
|
||||
if rcv := NewTenantID("cgrates.org:id"); *rcv != *eOut {
|
||||
t.Errorf("Expecting: %+v, received %+v", eOut, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTenantID(t *testing.T) {
|
||||
tID := &TenantID{Tenant: EmptyString, ID: EmptyString}
|
||||
eOut := ":"
|
||||
if rcv := tID.TenantID(); rcv != eOut {
|
||||
t.Errorf("Expecting: %q, received: %q", eOut, rcv)
|
||||
}
|
||||
tID = &TenantID{Tenant: "cgrates.org", ID: "id"}
|
||||
eOut = "cgrates.org:id"
|
||||
if rcv := tID.TenantID(); rcv != eOut {
|
||||
t.Errorf("Expecting: %q, received: %q", eOut, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTenantIDWithCache(t *testing.T) {
|
||||
tID := &TenantIDWithCache{Tenant: EmptyString, ID: EmptyString}
|
||||
eOut := ":"
|
||||
if rcv := tID.TenantID(); rcv != eOut {
|
||||
t.Errorf("Expecting: %q, received: %q", eOut, rcv)
|
||||
}
|
||||
tID = &TenantIDWithCache{Tenant: "cgrates.org", ID: "id"}
|
||||
eOut = "cgrates.org:id"
|
||||
if rcv := tID.TenantID(); rcv != eOut {
|
||||
t.Errorf("Expecting: %q, received: %q", eOut, rcv)
|
||||
}
|
||||
}
|
||||
|
||||
type testRPC struct {
|
||||
}
|
||||
|
||||
func (tRPC *testRPC) Name(args interface{}, reply interface{}) error {
|
||||
return errors.New("err_test")
|
||||
}
|
||||
func (tRPC *testRPC) V1Name(args interface{}, reply interface{}) error {
|
||||
return errors.New("V1_err_test")
|
||||
}
|
||||
func TestRPCCall(t *testing.T) {
|
||||
if err := RPCCall("wrong", "test", nil, nil); err == nil || err != rpcclient.ErrUnsupporteServiceMethod {
|
||||
t.Errorf("Expecting: %+v, received: %+v", rpcclient.ErrUnsupporteServiceMethod, err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func TestCounter(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user