forked from iovisor/bcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_map_batch_ops.py
More file actions
executable file
·48 lines (40 loc) · 1.3 KB
/
Copy pathtest_map_batch_ops.py
File metadata and controls
executable file
·48 lines (40 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python
#
# USAGE: test_map_batch_ops.py
#
# Copyright (c) Emilien Gobillot
# Licensed under the Apache License, Version 2.0 (the "License")
from __future__ import print_function
from bcc import BPF
import distutils.version
from unittest import main, skipUnless, TestCase
import ctypes as ct
import os
def kernel_version_ge(major, minor):
# True if running kernel is >= X.Y
version = distutils.version.LooseVersion(os.uname()[2]).version
if version[0] > major:
return True
if version[0] < major:
return False
if minor and version[1] < minor:
return False
return True
@skipUnless(kernel_version_ge(5, 6), "requires kernel >= 5.6")
class TestMapBatch(TestCase):
def test_lookup_and_delete_batch(self):
b = BPF(text="""BPF_HASH(map, int, int, 1024);""")
hmap = b["map"]
for i in range(0, 1024):
hmap[ct.c_int(i)] = ct.c_int(i)
# check the lookup
i = 0
for k, v in sorted(hmap.items_lookup_and_delete_batch()):
self.assertEqual(k, i)
self.assertEqual(v, i)
i += 1
# and check the delete has workd, i.e map is empty
count = sum(1 for _ in hmap.items_lookup_and_delete_batch())
self.assertEqual(count, 0)
if __name__ == "__main__":
main()