From 27456ce5cf6cff5f2de8e9ceb09c72a1140fe83c Mon Sep 17 00:00:00 2001 From: ionutboangiu Date: Fri, 30 May 2025 21:23:48 +0300 Subject: [PATCH] add sample IPs API calls --- ips/ips_it_test.go | 310 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 310 insertions(+) create mode 100644 ips/ips_it_test.go diff --git a/ips/ips_it_test.go b/ips/ips_it_test.go new file mode 100644 index 000000000..88b05f1d7 --- /dev/null +++ b/ips/ips_it_test.go @@ -0,0 +1,310 @@ +//go: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 +*/ + +package ips + +import ( + "bytes" + "fmt" + "testing" + + "github.com/cgrates/birpc/context" + "github.com/cgrates/cgrates/engine" + "github.com/cgrates/cgrates/utils" +) + +// NOTE: this test is incomplete. Currently used only for the API samples. +// TODO: move anything sessions related to sessions once ips implementation +// is complete. +func TestIPsIT(t *testing.T) { + t.Skip("ips test currently incomplete, skipping...") + var dbCfg engine.DBCfg + switch *utils.DBType { + case utils.MetaInternal: + dbCfg = engine.InternalDBCfg + case utils.MetaMySQL: // redis is already the default + case utils.MetaMongo: + dbCfg = engine.MongoDBCfg + case utils.MetaPostgres: + t.SkipNow() + default: + t.Fatal("unsupported dbtype value") + } + + ng := engine.TestEngine{ + ConfigJSON: `{ +"sessions": { + "enabled": true, + "ips_conns": ["*localhost"], + "opts": { + // "*ips": [ + // { + // "Tenant": "*any", + // "FilterIDs": [], + // "Value": false + // } + // "*ipsAuthorize": [ + // { + // "Tenant": "*any", + // "FilterIDs": [], + // "Value": false + // } + // ], + // "*ipsAllocate": [ + // { + // "Tenant": "*any", + // "FilterIDs": [], + // "Value": false + // } + // ], + // "*ipsRelease": [ + // { + // "Tenant": "*any", + // "FilterIDs": [], + // "Value": false + // } + // ] + } +}, +"ips": { + "enabled": true, + "store_interval": "-1", + "indexed_selects": true, + "string_indexed_fields": ["*req.Account"], + "prefix_indexed_fields": [], + "suffix_indexed_fields": [], + "exists_indexed_fields": [], + "notexists_indexed_fields": [], + "opts":{ + "*usageID": [ + { + "Tenant": "cgrates.org", + "FilterIDs": ["*string:~*req.Account:1001"], + "Value": "cfg_usage" + } + ], + // "*ttl": [ + // { + // "Tenant": "*any", + // "FilterIDs": [], + // "Value": "72h" + // } + // ], + // "*units": [ + // { + // "Tenant": "*any", + // "FilterIDs": [], + // "Value": 1 + // } + // ] + } +}, +"admins": { + "enabled": true +} +}`, + TpFiles: map[string]string{ + utils.IPsCsv: ` +#Tenant[0],Id[1],FilterIDs[2],Weights[3],TTL[4],Type[5],AddressPool[6],Allocation[7],Stored[8] +cgrates.org,IPs1,*string:~*req.Account:1001,;10,1s,ipv4,172.16.1.1/24,alloc_success,true +cgrates.org,IPs2,*string:~*req.Account:1001,;20,3s,ipv4,192.168.122.1/24,alloc_new,true`, + }, + DBCfg: dbCfg, + Encoding: *utils.Encoding, + LogBuffer: new(bytes.Buffer), + GracefulShutdown: true, + } + t.Cleanup(func() { fmt.Println(ng.LogBuffer) }) + client, _ := ng.Run(t) + + t.Run("admins apis", func(t *testing.T) { + var reply string + if err := client.Call(context.Background(), utils.AdminSv1SetIPProfile, + &utils.IPProfileWithAPIOpts{ + IPProfile: &utils.IPProfile{ + Tenant: "cgrates.org", + ID: "IPsAPI", + FilterIDs: []string{"*string:~*req.Account:1001"}, + Allocation: "Approved", + TTL: -1, + Type: "ipv4", + AddressPool: "192.168.122.1/24", + Weights: utils.DynamicWeights{ + { + Weight: 15, + }}, + }, + }, &reply); err != nil { + t.Error(err) + } + + var ipp utils.IPProfile + if err := client.Call(context.Background(), utils.AdminSv1GetIPProfile, + utils.TenantID{ + Tenant: "cgrates.org", + ID: "IPsAPI", + }, &ipp); err != nil { + t.Error(err) + } + + var ipps []*utils.IPProfile + if err := client.Call(context.Background(), utils.AdminSv1GetIPProfiles, + &utils.ArgsItemIDs{ + Tenant: "cgrates.org", + ItemsPrefix: "IPs", + }, &ipps); err != nil { + t.Error(err) + } + + if err := client.Call(context.Background(), utils.AdminSv1RemoveIPProfile, + &utils.TenantIDWithAPIOpts{ + TenantID: &utils.TenantID{ + Tenant: "cgrates.org", + ID: "IPsAPI", + }, + }, &reply); err != nil { + t.Error(err) + } + + var no int + if err := client.Call(context.Background(), utils.AdminSv1GetIPProfilesCount, + &utils.TenantWithAPIOpts{ + Tenant: "cgrates.org", + }, &no); err != nil { + t.Error(err) + } + }) + + t.Run("ips apis", func(t *testing.T) { + var ip utils.IP + if err := client.Call(context.Background(), utils.IPsV1GetIP, + &utils.TenantIDWithAPIOpts{ + TenantID: &utils.TenantID{ + Tenant: "cgrates.org", + ID: "IPs1", + }, + }, &ip); err != nil { + t.Error(err) + } + + usageID := "api_usage" + var evIPs IPs + if err := client.Call(context.Background(), utils.IPsV1GetIPsForEvent, + &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "GetIPsForEvent1", + Event: map[string]any{ + utils.AccountField: "1001", + }, + APIOpts: map[string]any{ + utils.OptsIPsUsageID: usageID, + }, + }, &evIPs); err != nil { + t.Error(err) + } + + var reply string + if err := client.Call(context.Background(), utils.IPsV1AuthorizeIPs, + &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "AuthorizeIPs1", + Event: map[string]any{ + utils.AccountField: "1001", + }, + APIOpts: map[string]any{ + // utils.OptsIPsUsageID: usageID, + // utils.OptsIPsTTL: time.Second, + // utils.OptsIPsUnits: 2, + }, + }, &reply); err != nil { + t.Error(err) + } + + if err := client.Call(context.Background(), utils.IPsV1AllocateIPs, + &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "AllocateIPs1", + Event: map[string]any{ + utils.AccountField: "1001", + }, + APIOpts: map[string]any{ + // utils.OptsIPsUsageID: usageID, + // utils.OptsIPsTTL: time.Second, + // utils.OptsIPsUnits: 2, + }, + }, &reply); err != nil { + t.Error(err) + } + + if err := client.Call(context.Background(), utils.IPsV1ReleaseIPs, + &utils.CGREvent{ + Tenant: "cgrates.org", + ID: "ReleaseIPs1", + Event: map[string]any{ + utils.AccountField: "1001", + }, + APIOpts: map[string]any{ + // utils.OptsIPsUsageID: usageID, + // utils.OptsIPsTTL: time.Second, + // utils.OptsIPsUnits: 2, + }, + }, &reply); err != nil { + t.Error(err) + } + }) + + t.Run("sessions ips apis", func(t *testing.T) { + // NOTE: reply is of type any to avoid having to import sessions just for + // this test in order to prevent future cyclic imports. Any sessions + // related test should be moved to sessions when ips implementation is + // complete. + var reply any + if err := client.Call(context.Background(), utils.SessionSv1AuthorizeEvent, + &utils.CGREvent{ + Tenant: "cgrates.org", + APIOpts: map[string]any{ + utils.MetaIPs: true, + utils.MetaOriginID: "session1", + }, + Event: map[string]any{ + utils.AccountField: "1001", + utils.Destination: "1002", + utils.SetupTime: "2018-01-07T17:00:00Z", + }, + }, &reply); err != nil { + t.Error(err) + } + if err := client.Call(context.Background(), utils.SessionSv1InitiateSession, + &utils.CGREvent{ + Tenant: "cgrates.org", + APIOpts: map[string]any{ + utils.MetaIPs: true, + utils.MetaOriginID: "session1", + }, + Event: map[string]any{ + utils.AccountField: "1001", + utils.Destination: "1002", + utils.SetupTime: "2018-01-07T17:00:00Z", + }, + }, &reply); err != nil { + t.Error(err) + } + }) +}