better test for goci.me

This commit is contained in:
Radu Ioan Fericean
2013-05-16 17:11:27 +03:00
parent 6776525c97
commit 7e7ab83bd8
5 changed files with 132 additions and 62 deletions

View File

@@ -38,57 +38,57 @@ const (
// Holds system configuration, defaults are overwritten with values from config file if found
type CGRConfig struct {
DataDBType string
DataDBHost string // The host to connect to. Values that start with / are for UNIX domain sockets.
DataDBPort string // The port to bind to.
DataDBName string // The name of the database to connect to.
DataDBUser string // The user to sign in as.
DataDBPass string // The user's password.
LogDBType string // Should reflect the database type used to store logs
LogDBHost string // The host to connect to. Values that start with / are for UNIX domain sockets.
LogDBPort string // The port to bind to.
LogDBName string // The name of the database to connect to.
LogDBUser string // The user to sign in as.
LogDBPass string // The user's password.
RaterEnabled bool // start standalone server (no balancer)
RaterBalancer string // balancer address host:port
RaterListen string // listening address host:port
RaterRPCEncoding string // use JSON for RPC encoding
BalancerEnabled bool
BalancerListen string // Json RPC server address
BalancerRPCEncoding string // use JSON for RPC encoding
SchedulerEnabled bool
SMEnabled bool
SMSwitchType string
SMRater string // address where to access rater. Can be internal, direct rater address or the address of a balancer
SMRaterReconnects int // Number of reconnect attempts to rater
SMDebitInterval int // the period to be debited in advanced during a call (in seconds)
SMRPCEncoding string // use JSON for RPC encoding
SMDefaultReqType string // Use this request type if not defined on top
SMDefaultTOR string // set default type of record
SMDefaultTenant string // set default tenant
SMDefaultSubject string // set default rating subject, useful in case of fallback
MediatorEnabled bool
MediatorCDRType string // sets the type of cdrs we are processing.
DataDBType string
DataDBHost string // The host to connect to. Values that start with / are for UNIX domain sockets.
DataDBPort string // The port to bind to.
DataDBName string // The name of the database to connect to.
DataDBUser string // The user to sign in as.
DataDBPass string // The user's password.
LogDBType string // Should reflect the database type used to store logs
LogDBHost string // The host to connect to. Values that start with / are for UNIX domain sockets.
LogDBPort string // The port to bind to.
LogDBName string // The name of the database to connect to.
LogDBUser string // The user to sign in as.
LogDBPass string // The user's password.
RaterEnabled bool // start standalone server (no balancer)
RaterBalancer string // balancer address host:port
RaterListen string // listening address host:port
RaterRPCEncoding string // use JSON for RPC encoding
BalancerEnabled bool
BalancerListen string // Json RPC server address
BalancerRPCEncoding string // use JSON for RPC encoding
SchedulerEnabled bool
SMEnabled bool
SMSwitchType string
SMRater string // address where to access rater. Can be internal, direct rater address or the address of a balancer
SMRaterReconnects int // Number of reconnect attempts to rater
SMDebitInterval int // the period to be debited in advanced during a call (in seconds)
SMRPCEncoding string // use JSON for RPC encoding
SMDefaultReqType string // Use this request type if not defined on top
SMDefaultTOR string // set default type of record
SMDefaultTenant string // set default tenant
SMDefaultSubject string // set default rating subject, useful in case of fallback
MediatorEnabled bool
MediatorCDRType string // sets the type of cdrs we are processing.
MediatorCDRInDir string // Freeswitch Master CSV CDR path.
MediatorCDROutDir string // Freeswitch Master CSV CDR output path.
MediatorRater string // address where to access rater. Can be internal, direct rater address or the address of a balancer
MediatorRaterReconnects int // Number of reconnect attempts to rater
MediatorRPCEncoding string // use JSON for RPC encoding
MediatorSkipDB bool
MediatorPseudoprepaid bool
FreeswitchServer string // freeswitch address host:port
FreeswitchPass string // FS socket password
FreeswitchDirectionIdx string
FreeswitchTORIdx string
FreeswitchTenantIdx string
FreeswitchSubjectIdx string
FreeswitchAccountIdx string
FreeswitchDestIdx string
FreeswitchTimeStartIdx string
FreeswitchDurationIdx string
FreeswitchUUIDIdx string
FreeswitchReconnects int // number of times to attempt reconnect after connect fails
MediatorCDROutDir string // Freeswitch Master CSV CDR output path.
MediatorRater string // address where to access rater. Can be internal, direct rater address or the address of a balancer
MediatorRaterReconnects int // Number of reconnect attempts to rater
MediatorRPCEncoding string // use JSON for RPC encoding
MediatorSkipDB bool
MediatorPseudoprepaid bool
FreeswitchServer string // freeswitch address host:port
FreeswitchPass string // FS socket password
FreeswitchDirectionIdx string
FreeswitchTORIdx string
FreeswitchTenantIdx string
FreeswitchSubjectIdx string
FreeswitchAccountIdx string
FreeswitchDestIdx string
FreeswitchTimeStartIdx string
FreeswitchDurationIdx string
FreeswitchUUIDIdx string
FreeswitchReconnects int // number of times to attempt reconnect after connect fails
}
// Instantiate a new CGRConfig setting defaults or reading from file
@@ -97,6 +97,18 @@ func NewCGRConfig(cfgPath *string) (*CGRConfig, error) {
if err != nil {
return nil, errors.New(fmt.Sprintf("Could not open the configuration file: %s", err))
}
return loadConfig(c)
}
func NewCGRConfigBytes(data []byte) (*CGRConfig, error) {
c, err := conf.ReadConfigBytes(data)
if err != nil {
return nil, errors.New(fmt.Sprintf("Could not open the configuration file: %s", err))
}
return loadConfig(c)
}
func loadConfig(c *conf.ConfigFile) (*CGRConfig, error) {
cfg := &CGRConfig{}
var hasOpt bool
cfg.DataDBType = REDIS
@@ -123,7 +135,7 @@ func NewCGRConfig(cfgPath *string) (*CGRConfig, error) {
if hasOpt = c.HasOption("global", "datadb_passwd"); hasOpt {
cfg.DataDBPass, _ = c.GetString("global", "datadb_passwd")
}
cfg.LogDBType = MONGO
cfg.LogDBType = MONGO
if hasOpt = c.HasOption("global", "logdb_type"); hasOpt {
cfg.LogDBType, _ = c.GetString("global", "logdb_type")
}
@@ -305,5 +317,4 @@ func NewCGRConfig(cfgPath *string) (*CGRConfig, error) {
}
return cfg, nil
}

View File

@@ -42,7 +42,7 @@ type Event interface {
}
// Returns first non empty string out of vals. Useful to extract defaults
func firstNonEmpty(vals []string) string {
func firstNonEmpty(vals ...string) string {
for _, val := range vals {
if len(val) != 0 {
return val

View File

@@ -27,7 +27,7 @@ func TestFirstNonEmpty(t *testing.T) {
sampleMap := make(map[string]string)
sampleMap["Third"] = "third"
fourthElmnt := "fourth"
winnerElmnt := firstNonEmpty([]string{firstElmnt, sampleMap["second"], sampleMap["Third"], fourthElmnt})
winnerElmnt := firstNonEmpty(firstElmnt, sampleMap["second"], sampleMap["Third"], fourthElmnt)
if winnerElmnt != sampleMap["Third"] {
t.Error("Wrong elemnt returned: ", winnerElmnt)
}

View File

@@ -90,15 +90,15 @@ func (fsev *FSEvent) GetOrigId() string {
return fsev.fields[ORIG_ID]
}
func (fsev *FSEvent) GetSubject() string {
return firstNonEmpty([]string{fsev.fields[SUBJECT], fsev.fields[USERNAME]})
return firstNonEmpty(fsev.fields[SUBJECT], fsev.fields[USERNAME])
}
func (fsev *FSEvent) GetAccount() string {
return firstNonEmpty([]string{fsev.fields[ACCOUNT], fsev.fields[USERNAME]})
return firstNonEmpty(fsev.fields[ACCOUNT], fsev.fields[USERNAME])
}
// Charging destination number
func (fsev *FSEvent) GetDestination() string {
return firstNonEmpty([]string{fsev.fields[DESTINATION], fsev.fields[CALL_DEST_NR]})
return firstNonEmpty(fsev.fields[DESTINATION], fsev.fields[CALL_DEST_NR])
}
// Original dialed destination number, useful in case of unpark
@@ -106,16 +106,16 @@ func (fsev *FSEvent) GetCallDestNr() string {
return fsev.fields[CALL_DEST_NR]
}
func (fsev *FSEvent) GetTOR() string {
return firstNonEmpty([]string{fsev.fields[TOR], cfg.SMDefaultTOR})
return firstNonEmpty(fsev.fields[TOR], cfg.SMDefaultTOR)
}
func (fsev *FSEvent) GetUUID() string {
return fsev.fields[UUID]
}
func (fsev *FSEvent) GetTenant() string {
return firstNonEmpty([]string{fsev.fields[CSTMID], cfg.SMDefaultTenant})
return firstNonEmpty(fsev.fields[CSTMID], cfg.SMDefaultTenant)
}
func (fsev *FSEvent) GetReqType() string {
return firstNonEmpty([]string{fsev.fields[REQTYPE], cfg.SMDefaultReqType})
return firstNonEmpty(fsev.fields[REQTYPE], cfg.SMDefaultReqType)
}
func (fsev *FSEvent) MissingParameter() bool {
return strings.TrimSpace(fsev.GetDirection()) == "" ||

View File

@@ -51,6 +51,66 @@ var (
"Session-Since-Startup": "122",
"Idle-CPU": "100.000000"
`
conf_data = []byte(`
### Test data, not for production usage
[global]
datadb_type = test #
datadb_host = test # The host to connect to. Values that start with / are for UNIX domain sockets.
datadb_port = test # The port to bind to.
datadb_name = test # The name of the database to connect to.
datadb_user = test # The user to sign in as.
datadb_passwd = test # The user's password.root
logdb_type = test #
logdb_host = test # The host to connect to. Values that start with / are for UNIX domain sockets.
logdb_port = test # The port to bind to.
logdb_name = test # The name of the database to connect to.
logdb_user = test # The user to sign in as.
logdb_passwd = test # The user's password.root
[balancer]
enabled = true # Start balancer server
listen = test # Balancer listen interface
rpc_encoding = test # use JSON for RPC encoding
[rater]
enabled = true
listen = test # listening address host:port, internal for internal communication only
balancer = test # if defined it will register to balancer as worker
rpc_encoding = test # use JSON for RPC encoding
[mediator]
enabled = true
cdr_in_dir = test # Freeswitch Master CSV CDR path.
cdr_out_dir = test
rater = test #address where to access rater. Can be internal, direct rater address or the address of a balancer
rpc_encoding = test # use JSON for RPC encoding
skipdb = true
pseudoprepaid = true
[scheduler]
enabled = true
[session_manager]
enabled = true
switch_type = test
rater = test #address where to access rater. Can be internal, direct rater address or the address of a balancer
debit_interval = 11
rpc_encoding = test # use JSON for RPC encoding
[freeswitch]
server = test # freeswitch address host:port
passwd = test # freeswitch address host:port
direction_index = test
tor_index = test
tenant_index = test
subject_index = test
account_index = test
destination_index = test
time_start_index = test
duration_index = test
uuid_index = test
`)
)
/*func TestSessionDurationSingle(t *testing.T) {
@@ -65,9 +125,8 @@ var (
}*/
func TestSessionNilSession(t *testing.T) {
cfgTestPath := "../config/test_data.txt"
var errCfg error
cfg, errCfg = config.NewCGRConfig(&cfgTestPath) // Needed here to avoid nil on cfg variable
cfg, errCfg = config.NewCGRConfigBytes(conf_data) // Needed here to avoid nil on cfg variable
if errCfg != nil {
t.Errorf("Cannot get configuration %v", errCfg)
}