mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
ips active allocations test
This commit is contained in:
committed by
Dan Christian Bogos
parent
040392571b
commit
9065e58158
102
general_tests/ips_prf_it_test.go
Normal file
102
general_tests/ips_prf_it_test.go
Normal file
@@ -0,0 +1,102 @@
|
||||
//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 general_tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/cgrates/birpc/context"
|
||||
"github.com/cgrates/cgrates/engine"
|
||||
"github.com/cgrates/cgrates/utils"
|
||||
)
|
||||
|
||||
func Benchmark10IPsAllocated(b *testing.B) {
|
||||
|
||||
content := `{
|
||||
"general": {
|
||||
"log_level": 7
|
||||
},
|
||||
"data_db": {
|
||||
"db_type": "*internal"
|
||||
},
|
||||
"stor_db": {
|
||||
"db_type": "*internal"
|
||||
},
|
||||
"admins": {
|
||||
"enabled": true,
|
||||
},
|
||||
"ips": {
|
||||
"enabled": true,
|
||||
"store_interval": "-1",
|
||||
"string_indexed_fields": ["*req.Account"],
|
||||
},
|
||||
}`
|
||||
|
||||
ng := engine.TestEngine{
|
||||
ConfigJSON: content,
|
||||
LogBuffer: bytes.NewBuffer(nil),
|
||||
Encoding: utils.MetaJSON,
|
||||
}
|
||||
client, _ := ng.Run(b)
|
||||
|
||||
var reply string
|
||||
for i := 1; i <= 10; i++ {
|
||||
ipProfile := &utils.IPProfileWithAPIOpts{
|
||||
IPProfile: &utils.IPProfile{
|
||||
Tenant: "cgrates.org",
|
||||
ID: fmt.Sprintf("IP_PROF_%d", i),
|
||||
FilterIDs: []string{fmt.Sprintf("*string:~*req.Account:IP_PROF_%d", i)},
|
||||
TTL: 10 * time.Minute,
|
||||
Pools: []*utils.IPPool{
|
||||
{
|
||||
ID: "POOL_A",
|
||||
Range: fmt.Sprintf("10.10.10.%d/32", i),
|
||||
Message: "Allocated by test",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
if err := client.Call(context.Background(), utils.AdminSv1SetIPProfile, ipProfile, &reply); err != nil {
|
||||
b.Fatalf("Failed to set IP profile: %v", err)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
b.Run("IPsAllocateEvent", func(b *testing.B) {
|
||||
var prof atomic.Int64
|
||||
b.RunParallel(func(pb *testing.PB) {
|
||||
for pb.Next() {
|
||||
i := prof.Add(1) % 10
|
||||
allocID := utils.GenUUID()
|
||||
allocateIP(b, client, "event1", fmt.Sprintf("IP_PROF_%d", i), allocID)
|
||||
checkAllocs(b, client, fmt.Sprintf("IP_PROF_%d", i), allocID)
|
||||
releaseIP(b, client, fmt.Sprintf("IP_PROF_%d", i), allocID)
|
||||
|
||||
allocateIP(b, client, "event1", fmt.Sprintf("IP_PROF_%d", i), allocID)
|
||||
checkAllocs(b, client, fmt.Sprintf("IP_PROF_%d", i), allocID)
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
@@ -338,7 +338,7 @@ func sendRadReq(t *testing.T, client *radigo.Client, code radigo.PacketCode, id
|
||||
return replyPacket
|
||||
}
|
||||
|
||||
func checkAllocs(t *testing.T, client *birpc.Client, id string, wantAllocs ...string) {
|
||||
func checkAllocs(t testing.TB, client *birpc.Client, id string, wantAllocs ...string) {
|
||||
t.Helper()
|
||||
var allocs utils.IPAllocations
|
||||
if err := client.Call(context.Background(), utils.IPsV1GetIPAllocations,
|
||||
@@ -362,7 +362,46 @@ func checkAllocs(t *testing.T, client *birpc.Client, id string, wantAllocs ...st
|
||||
}
|
||||
}
|
||||
|
||||
func checkCDR(t *testing.T, client *birpc.Client, acnt string) {
|
||||
func allocateIP(t testing.TB, client *birpc.Client, eventID, id, allocID string) {
|
||||
args := &utils.CGREvent{
|
||||
Tenant: "cgrates.org",
|
||||
ID: eventID,
|
||||
Event: map[string]any{
|
||||
utils.AccountField: id,
|
||||
utils.AnswerTime: utils.TimePointer(time.Now()),
|
||||
utils.Usage: 10,
|
||||
utils.Tenant: "cgrates.org",
|
||||
},
|
||||
APIOpts: map[string]any{
|
||||
utils.OptsIPsAllocationID: allocID,
|
||||
},
|
||||
}
|
||||
var reply utils.AllocatedIP
|
||||
if err := client.Call(context.Background(), utils.IPsV1AllocateIP, args, &reply); err != nil {
|
||||
t.Errorf("Error allocating IPProfile %s: %v", id, err)
|
||||
}
|
||||
}
|
||||
|
||||
func releaseIP(t testing.TB, client *birpc.Client, id, allocID string) {
|
||||
args := &utils.CGREvent{
|
||||
Tenant: "cgrates.org",
|
||||
ID: utils.GenUUID(),
|
||||
Event: map[string]any{
|
||||
utils.AccountField: id,
|
||||
utils.AnswerTime: utils.TimePointer(time.Now()),
|
||||
utils.Usage: 10,
|
||||
utils.Tenant: "cgrates.org",
|
||||
},
|
||||
APIOpts: map[string]any{
|
||||
utils.OptsIPsAllocationID: allocID,
|
||||
},
|
||||
}
|
||||
if err := client.Call(context.Background(), utils.IPsV1ReleaseIP, args, nil); err != nil {
|
||||
t.Errorf("Error releasing IPProfile %s: %v", id, err)
|
||||
}
|
||||
}
|
||||
|
||||
func checkCDR(t testing.TB, client *birpc.Client, acnt string) {
|
||||
t.Helper()
|
||||
var cdrs []*utils.CDR
|
||||
if err := client.Call(context.Background(), utils.AdminSv1GetCDRs,
|
||||
|
||||
Reference in New Issue
Block a user