Skip to content

Commit

Permalink
add test_lpm_trie.py test
Browse files Browse the repository at this point in the history
test_lpm_trie.py has been in the repo for quite some time,
but is not included in the unit test.

The issue #2860
exposed an issue involved in using together with BTF,
which requires the key type to be a structure.

Let add it as a unit test so we can be sure
lpm_trie map is supported properly by bcc.

Signed-off-by: Yonghong Song <yhs@fb.com>
  • Loading branch information
yonghong-song committed Apr 6, 2020
1 parent 495a4a3 commit 533391e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
2 changes: 2 additions & 0 deletions tests/python/CMakeLists.txt
Expand Up @@ -85,3 +85,5 @@ add_test(NAME py_test_free_bcc_memory WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_D
COMMAND ${TEST_WRAPPER} py_test_free_bcc_memory sudo ${CMAKE_CURRENT_SOURCE_DIR}/test_free_bcc_memory.py)
add_test(NAME py_test_rlimit WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND ${TEST_WRAPPER} py_test_rlimit sudo ${CMAKE_CURRENT_SOURCE_DIR}/test_rlimit.py)
add_test(NAME py_test_lpm_trie WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND ${TEST_WRAPPER} py_test_lpm_trie sudo ${CMAKE_CURRENT_SOURCE_DIR}/test_lpm_trie.py)
26 changes: 22 additions & 4 deletions tests/python/test_lpm_trie.py 100644 → 100755
Expand Up @@ -3,10 +3,23 @@
# Licensed under the Apache License, Version 2.0 (the "License")

import ctypes as ct
import unittest
import distutils.version
import os
from unittest import main, skipUnless, TestCase
from bcc import BPF
from netaddr import IPAddress

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

class KeyV4(ct.Structure):
_fields_ = [("prefixlen", ct.c_uint),
("data", ct.c_ubyte * 4)]
Expand All @@ -15,10 +28,15 @@ class KeyV6(ct.Structure):
_fields_ = [("prefixlen", ct.c_uint),
("data", ct.c_ushort * 8)]

class TestLpmTrie(unittest.TestCase):
@skipUnless(kernel_version_ge(4, 11), "requires kernel >= 4.11")
class TestLpmTrie(TestCase):
def test_lpm_trie_v4(self):
test_prog1 = """
BPF_LPM_TRIE(trie, u64, int, 16);
struct key_v4 {
u32 prefixlen;
u32 data[4];
};
BPF_LPM_TRIE(trie, struct key_v4, int, 16);
"""
b = BPF(text=test_prog1)
t = b["trie"]
Expand Down Expand Up @@ -71,4 +89,4 @@ def test_lpm_trie_v6(self):
v = t[k]

if __name__ == "__main__":
unittest.main()
main()

0 comments on commit 533391e

Please sign in to comment.