1#!/bin/bash
2# 'perf data convert --to-json' command test
3# SPDX-License-Identifier: GPL-2.0
4
5set -e
6
7err=0
8
9if [ "$PYTHON" = "" ] ; then
10	if which python3 > /dev/null ; then
11		PYTHON=python3
12	elif which python > /dev/null ; then
13		PYTHON=python
14	else
15		echo Skipping test, python not detected please set environment variable PYTHON.
16		exit 2
17	fi
18fi
19
20perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX)
21result=$(mktemp /tmp/__perf_test.output.json.XXXXX)
22
23cleanup()
24{
25	rm -f "${perfdata}"
26	rm -f "${result}"
27	trap - exit term int
28}
29
30trap_cleanup()
31{
32	cleanup
33	exit ${err}
34}
35trap trap_cleanup exit term int
36
37test_json_converter_command()
38{
39	echo "Testing Perf Data Convertion Command to JSON"
40	perf record -o "$perfdata" -F 99 -g -- perf test -w noploop > /dev/null 2>&1
41	perf data convert --to-json "$result" --force -i "$perfdata" >/dev/null 2>&1
42	if [ "$(cat ${result} | wc -l)" -gt "0" ] ; then
43		echo "Perf Data Converter Command to JSON [SUCCESS]"
44	else
45		echo "Perf Data Converter Command to JSON [FAILED]"
46		err=1
47		exit
48	fi
49}
50
51validate_json_format()
52{
53	echo "Validating Perf Data Converted JSON file"
54	if [ -f "$result" ] ; then
55		if $PYTHON -c  "import json; json.load(open('$result'))" >/dev/null 2>&1 ; then
56			echo "The file contains valid JSON format [SUCCESS]"
57		else
58			echo "The file does not contain valid JSON format [FAILED]"
59			err=1
60			exit
61		fi
62	else
63		echo "File not found [FAILED]"
64		err=2
65		exit
66	fi
67}
68
69test_json_converter_command
70validate_json_format
71
72exit ${err}
73