Skip to content

Commit

Permalink
Add unittests for veristat_compare script
Browse files Browse the repository at this point in the history
- Rename veristat-compare.py into veristat_compare.py to match python
  naming convention
- Add unittests for parse_tables() method

Signed-off-by: Nikolay Yurin <[email protected]>
  • Loading branch information
yurinnick committed Oct 19, 2023
1 parent 31d6168 commit d4ea4fb
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/scripts/compare-veristat-results.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ selftests/bpf/veristat \
--emit file,prog,verdict,states \
--compare "${BASELINE_PATH}" "${VERISTAT_OUTPUT}" > compare.csv

python3 ./.github/scripts/veristat-compare.py compare.csv
python3 ./.github/scripts/veristat_compare.py compare.csv
75 changes: 75 additions & 0 deletions .github/scripts/tests/test_veristat_compare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#!/usr/bin/env python3

import unittest
from typing import Iterable, List

from ..veristat_compare import parse_table, VeristatFields


def gen_csv_table(records: Iterable[str]) -> List[str]:
return [
",".join(VeristatFields.headers()),
*records,
]


class TestMatrix(unittest.TestCase):
def test_parse_table_ignore_new_prog(self):
table = gen_csv_table(
[
"prog_file.bpf.o,prog_name,N/A,success,N/A,N/A,1,N/A",
]
)
veristat_info = parse_table(table)
self.assertEqual(veristat_info.table, [])
self.assertFalse(veristat_info.changes)
self.assertFalse(veristat_info.new_failures)

def test_parse_table_ignore_removed_prog(self):
table = gen_csv_table(
[
"prog_file.bpf.o,prog_name,success,N/A,N/A,1,N/A,N/A",
]
)
veristat_info = parse_table(table)
self.assertEqual(veristat_info.table, [])
self.assertFalse(veristat_info.changes)
self.assertFalse(veristat_info.new_failures)

def test_parse_table_new_failure(self):
table = gen_csv_table(
[
"prog_file.bpf.o,prog_name,success,failure,MISMATCH,1,1,+0 (+0.00%)",
]
)
veristat_info = parse_table(table)
self.assertEqual(
veristat_info.table,
[["prog_file.bpf.o", "prog_name", "success -> failure (!!)", "+0.00 %"]],
)
self.assertTrue(veristat_info.changes)
self.assertTrue(veristat_info.new_failures)

def test_parse_table_new_changes(self):
table = gen_csv_table(
[
"prog_file.bpf.o,prog_name,failure,success,MISMATCH,0,0,+0 (+0.00%)",
"prog_file.bpf.o,prog_name_increase,failure,failure,MATCH,1,2,+1 (+100.00%)",
"prog_file.bpf.o,prog_name_decrease,success,success,MATCH,1,1,-1 (-100.00%)",
]
)
veristat_info = parse_table(table)
self.assertEqual(
veristat_info.table,
[
["prog_file.bpf.o", "prog_name", "failure -> success", "+0.00 %"],
["prog_file.bpf.o", "prog_name_increase", "failure", "+100.00 %"],
["prog_file.bpf.o", "prog_name_decrease", "success", "-100.00 %"],
],
)
self.assertTrue(veristat_info.changes)
self.assertFalse(veristat_info.new_failures)


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
import argparse
import enum
from dataclasses import dataclass
from typing import Dict, List, Final
from typing import Dict, Iterable, List, Final


TRESHOLD_PCT: Final[int] = 0
Expand Down Expand Up @@ -152,7 +152,7 @@ def get_state_diff(value: str) -> float:
)


def parse_table(csv_file):
def parse_table(csv_file: Iterable[str]) -> VeristatInfo:
reader = csv.DictReader(csv_file)
assert reader.fieldnames == VeristatFields.headers()

Expand Down
13 changes: 13 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ concurrency:
cancel-in-progress: true

jobs:
test-scripts:
if: ${{ github.repository == 'kernel-patches/vmtest' }}
name: Lint and Test python scripts
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- uses: psf/black@stable
with:
src: ci/scripts
- name: Run unittests
run: python3 -m unittest scripts/tests/*.py
working-directory: ci
set-matrix:
# FIXME: set-matrix is lightweight, run it on any self-hosted machines for kernel-patches org
# so we do not wait for GH hosted runners when there potentially all are busy because of bpf-rc
Expand Down

0 comments on commit d4ea4fb

Please sign in to comment.