summaryrefslogtreecommitdiff
path: root/tools/perf/tests/shell/test_gecko_python.sh
blob: 18e9f5cd2ab003c546e85c61038dfc6e5bc9e168 (plain) (blame)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
# gecko python test

set -e

shelldir=$(dirname "$0")
# shellcheck source=lib/setup_python.sh
. "${shelldir}"/lib/setup_python.sh

# If we don't have the perf python module, we can't test
if ! "$PYTHON" -c 'import perf' > /dev/null 2>&1; then
	echo "Skipping test, perf python module not found"
	exit 2
fi

script_dir="$(dirname "$0")/../../python"
script_path="${script_dir}/gecko.py"

if [ ! -f "$script_path" ]; then
	echo "Skipping test, gecko.py not found at $script_path"
	exit 2
fi

err=0
temp_data=""
temp_json=""

cleanup() {
	rm -f "${temp_data}" "${temp_json}"
}

trap 'cleanup' EXIT TERM INT

temp_data=$(mktemp /tmp/perf.data.XXXXXX)
temp_json=$(mktemp /tmp/perf.gecko.json.XXXXXX)

test_file_mode() {
	echo "Testing gecko.py..."

	# Generate some events with callchains
	if ! perf record -B -N --no-bpf-event -g -o "${temp_data}" \
		-- perf test -w noploop >/dev/null 2>&1; then
		echo "Skipping test, perf record -g failed (permissions or lack of support)"
		exit 2
	fi

	# Run the script in save-only mode with custom product and category colors
	if ! perf script gecko -i "${temp_data}" \
		--product "perf-test-product" --user-color blue --kernel-color red \
		--save-only "${temp_json}" >/dev/null; then
		echo "File mode test failed."
		err=1
	else
		# Validate JSON schema and custom CLI option values
		if ! "$PYTHON" - "$script_path" "${temp_json}" << 'PYEOF'
import importlib.util
import json
import sys

with open(sys.argv[2], encoding="utf-8") as f:
    data = json.load(f)

assert data["meta"]["product"] == "perf-test-product"
assert data["meta"]["version"] == 24
assert isinstance(data["threads"], list) and len(data["threads"]) > 0

spec = importlib.util.spec_from_file_location("gecko", sys.argv[1])
mod = importlib.util.module_from_spec(spec)
sys.modules[spec.name] = mod
spec.loader.exec_module(mod)

class DummyHandler:
    headers = []
    error_code = None
    def send_header(self, k, v):
        self.headers.append((k, v))
    def send_error(self, code, _msg):
        self.error_code = code

dummy = DummyHandler()
mod.CORSRequestHandler.list_directory(dummy, "/tmp")
assert dummy.error_code == 403
PYEOF
		then
			echo "Gecko JSON and CORSRequestHandler validation failed."
			err=1
		else
			echo "File mode JSON and CORSRequestHandler test passed."
		fi
	fi
}

test_file_mode

exit $err