Skip to content

Commit

Permalink
Sanitize CMake file paths. Second attempt. (p4lang#1512)
Browse files Browse the repository at this point in the history
* Sanitize CMake file paths.
  • Loading branch information
fruffy authored and Mihai Budiu committed Sep 25, 2018
1 parent c09db2f commit d856904
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 16 deletions.
2 changes: 1 addition & 1 deletion backends/ebpf/run-ebpf-test.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import tempfile
import shutil
sys.path.insert(0, os.path.dirname(
os.path.abspath(__file__)) + '/../../tools')
os.path.realpath(__file__)) + '/../../tools')
from testutils import *


Expand Down
2 changes: 1 addition & 1 deletion backends/ebpf/targets/bcc_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import sys
from target import EBPFTarget
sys.path.insert(0, os.path.dirname(
os.path.abspath(__file__)) + '/../../../tools')
os.path.realpath(__file__)) + '/../../../tools')
from testutils import *


Expand Down
7 changes: 4 additions & 3 deletions backends/ebpf/targets/ebpfenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def ns_init(self):
""" Initialize the namespace. """
cmd = "ip netns add %s" % self.ns_name
errmsg = "Failed to create namespace %s :" % self.ns_name
result = run_timeout(True, cmd, TIMEOUT,
result = run_timeout(self.verbose, cmd, TIMEOUT,
self.outputs, errmsg)
self.ns_exec("ip link set dev lo up")
return result
Expand All @@ -48,7 +48,7 @@ def ns_del(self):
""" Delete the namespace. """
cmd = "ip netns del %s" % self.ns_name
errmsg = "Failed to delete namespace %s :" % self.ns_name
return run_timeout(True, cmd, TIMEOUT,
return run_timeout(self.verbose, cmd, TIMEOUT,
self.outputs, errmsg)

def get_ns_prefix(self):
Expand All @@ -61,7 +61,8 @@ def ns_exec(self, cmd_string):
prefix = self.get_ns_prefix()
# bash -c allows us to run multiple commands at once
cmd = "%s bash -c \"%s\"" % (prefix, cmd_string)
errmsg = "Failed to run command in namespace %s:" % self.ns_name
errmsg = "Failed to run command %s in namespace %s:" % (
cmd, self.ns_name)
return run_timeout(self.verbose, cmd, TIMEOUT,
self.outputs, errmsg)

Expand Down
5 changes: 2 additions & 3 deletions backends/ebpf/targets/kernel_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from target import EBPFTarget
# path to the tools folder of the compiler
sys.path.insert(0, os.path.dirname(
os.path.abspath(__file__)) + '/../../../tools')
os.path.realpath(__file__)) + '/../../../tools')
from testutils import *


Expand Down Expand Up @@ -160,11 +160,10 @@ def _run_in_namespace(self, bridge):

def run(self):
# Root is necessary to load ebpf into the kernel
if !check_root():
if not check_root():
errmsg = "This test requires root privileges; skipping execution."
report_err(self.outputs["stderr"], errmsg)
return SKIPPED

result = self._create_runtime()
if result != SUCCESS:
return result
Expand Down
2 changes: 1 addition & 1 deletion backends/ebpf/targets/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from ebpfstf import create_table_file, parse_stf_file
# path to the tools folder of the compiler
sys.path.insert(0, os.path.dirname(
os.path.abspath(__file__)) + '/../../../tools')
os.path.realpath(__file__)) + '/../../../tools')
from testutils import *

PCAP_PREFIX = "pcap" # match pattern
Expand Down
2 changes: 1 addition & 1 deletion backends/ebpf/targets/test_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from target import EBPFTarget
# path to the tools folder of the compiler
sys.path.insert(0, os.path.dirname(
os.path.abspath(__file__)) + '/../../../tools')
os.path.realpath(__file__)) + '/../../../tools')
from testutils import *


Expand Down
32 changes: 26 additions & 6 deletions cmake/P4CUtils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,28 @@ endmacro(p4c_add_test_list)

# generate a list of test name suffixes based on specified testsuites
function(p4c_find_test_names testsuites tests)
foreach(ts "${testsuites}")
set(__tests "")
p4c_sanitize_path("${testsuites}" abs_paths)
foreach(ts ${abs_paths})
file (GLOB __testfiles RELATIVE ${P4C_SOURCE_DIR} ${ts})
list (APPEND __tests ${__testfiles})
endforeach()
set(${tests} "${__tests}" PARENT_SCOPE)
endfunction()

# convert the paths from a list of input files to their absolute paths
# does not follow symlinks.
# - files is a list of (relative) file paths
# - abs_files is the return set of absolute file paths
function(p4c_sanitize_path files abs_files)
foreach(file ${files})
get_filename_component(__file "${file}"
ABSOLUTE BASE_DIR "${P4C_SOURCE_DIR}")
list (APPEND __abs_files ${__file})
endforeach()
set(${abs_files} "${__abs_files}" PARENT_SCOPE)
endfunction()

# generate all the tests specified in the testsuites: builds a list of tests
# from the testsuite patterns by calling p4c_find_test_names then pass the list
# to p4c_add_test_list
Expand All @@ -159,21 +174,26 @@ endfunction()
#
# The macro generates the test files in a directory prefixed by tag.
#
macro(p4c_add_tests tag driver testsuites xfail)
macro(p4c_add_tests tag driver testsuites xfails)
set(__tests "")
set(__xfails "")
p4c_find_test_names("${testsuites}" __tests)
p4c_add_test_list (${tag} ${driver} "${__tests}" "${xfail}" "${ARGN}")
p4c_find_test_names("${xfails}" __xfails)
p4c_add_test_list (${tag} ${driver} "${__tests}" "${__xfails}" "${ARGN}")
endmacro(p4c_add_tests)

# same as p4c_add_tests but adds --p4runtime flag when invoking test driver
# unless test is listed in p4rt_exclude
macro(p4c_add_tests_w_p4runtime tag driver testsuites xfail p4rt_exclude)
macro(p4c_add_tests_w_p4runtime tag driver testsuites xfails p4rt_exclude)
set(__tests "")
set(__xfails "")
p4c_find_test_names("${testsuites}" __tests)
p4c_find_test_names("${xfails}" __xfails)
set(__tests_no_p4runtime "${p4rt_exclude}")
set(__tests_p4runtime "${__tests}")
list (REMOVE_ITEM __tests_p4runtime ${__tests_no_p4runtime})
p4c_add_test_list (${tag} ${driver} "${__tests_no_p4runtime}" "${xfail}" "${ARGN}")
p4c_add_test_list (${tag} ${driver} "${__tests_p4runtime}" "${xfail}" "--p4runtime ${ARGN}")
p4c_add_test_list (${tag} ${driver} "${__tests_no_p4runtime}" "${__xfails}" "${ARGN}")
p4c_add_test_list (${tag} ${driver} "${__tests_p4runtime}" "${__xfails}" "--p4runtime ${ARGN}")
endmacro(p4c_add_tests_w_p4runtime)

# add rules to make check and recheck for a specific test suite
Expand Down
1 change: 1 addition & 0 deletions tools/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
FAILURE = 1
SKIPPED = 2 # used occasionally to indicate that a test was not executed


def is_err(p4filename):
""" True if the filename represents a p4 program that should fail. """
return "_errors" in p4filename
Expand Down

0 comments on commit d856904

Please sign in to comment.