vty: add f_verify_talloc_count()

Counts talloc objects via VTY and waits for a specific object count.
Useful to test against leaks.

The intended use is to check against leakage of bsc_subscr and
gsm_subscriber_connection after each BSC_Tests.*, see patch in 'Related'
below.

Related: OS#5337
Related: I69d4c5c6f8d499bb7f0b96a48af045361433c57b (osmo-ttcn3-hacks)
Change-Id: Iafe720a4931270e868f40c4f623d10bb9c14dc80
This commit is contained in:
Neels Hofmeyr 2021-12-13 17:07:40 +01:00
parent 8bdafe5be6
commit e23496ad23
1 changed files with 64 additions and 0 deletions

View File

@ -14,6 +14,7 @@ module Osmocom_VTY_Functions {
import from Misc_Helpers all;
import from TELNETasp_PortType all;
import from Osmocom_Types all;
import from TCCConversion_Functions all;
modulepar {
charstring mp_prompt_prefix := "OpenBSC";
@ -226,4 +227,67 @@ function f_vty_transceive_match_regexp_retry(TELNETasp_PT pt, charstring cmd, ch
return "";
}
/* Return a count of how many times sub_str occurs in str. */
function f_strstr_count(in charstring str, in charstring sub_str) return integer
{
var integer count := 0;
var integer pos := 0;
while (true) {
var integer at := f_strstr(str, sub_str, pos);
if (at < 0) {
break;
}
count := count + 1;
pos := at + 1;
}
return count;
}
private type record of charstring StrList;
/* Perform a 'show talloc-context' to get a count of the given object_strs that are still allocated.
* Retry 'attempts' times until the actual talloc object count matches 'expect_count'.
* Useful to ensure that no mem leaks remain after running a test. */
function f_verify_talloc_count(TELNETasp_PT pt, StrList object_strs, integer expect_count := 0,
integer attempts := 5, float wait_time := 3.0)
{
var charstring show_cmd := "show talloc-context application full filter ";
for (var integer i := 0; i < lengthof(object_strs); i := i + 1) {
var charstring obj_str := object_strs[i];
/* spaces confuse the VTY command */
obj_str := f_replaceEveryOccurenceOfSubstring(obj_str, " ", ".");
/* In the regexp, expect word start and word end to bound the obj name */
obj_str := "\\<" & obj_str & "\\>";
if (i > 0) {
show_cmd := show_cmd & "\\|";
}
show_cmd := show_cmd & obj_str;
}
while (attempts > 0) {
attempts := attempts - 1;
var charstring ret := f_vty_transceive_ret(pt, show_cmd);
var boolean ok := true;
for (var integer i := 0; i < lengthof(object_strs); i := i + 1) {
var charstring object_str := object_strs[i];
var integer count := f_strstr_count(ret, object_str);
log("talloc reports ", object_str, " x ", count, ", expecting ", expect_count);
if (count != expect_count) {
ok := false;
}
}
if (ok) {
return;
}
if (attempts == 0) {
break;
}
log("count mismatch, retrying in ", wait_time);
f_sleep(wait_time);
}
setverdict(fail, "talloc count mismatch");
}
}