#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0
#
# A collection of tests for tools/testing/kunit/kunit.py
#
# Copyright (C) 2019, Google LLC.
# Author: Brendan Higgins <brendanhiggins@google.com>
import unittest
from unittest import mock
import tempfile, shutil # Handling test_tmpdir
import io
import itertools
import json
import os
import signal
import subprocess
import sys
from typing import Iterable
import kunit_config
import kunit_parser
import kunit_kernel
import kunit_json
import kunit
from kunit_printer import stdout
test_tmpdir = ''
abs_test_data_dir = ''
def setUpModule():
global test_tmpdir, abs_test_data_dir
test_tmpdir = tempfile.mkdtemp()
abs_test_data_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), 'test_data'))
def tearDownModule():
shutil.rmtree(test_tmpdir)
def _test_data_path(path):
return os.path.join(abs_test_data_dir, path)
class KconfigTest(unittest.TestCase):
def test_is_subset_of(self):
kconfig0 = kunit_config.Kconfig()
self.assertTrue(kconfig0.is_subset_of(kconfig0))
kconfig1 = kunit_config.Kconfig()
kconfig1.add_entry('TEST', 'y')
self.assertTrue(kconfig1.is_subset_of(kconfig1))
self.assertTrue(kconfig0.is_subset_of(kconfig1))
self.assertFalse(kconfig1.is_subset_of(kconfig0))
def test_read_from_file(self):
kconfig_path = _test_data_path('test_read_from_file.kconfig')
kconfig = kunit_config.parse_file(kconfig_path)
expected_kconfig = kunit_config.Kconfig()
expected_kconfig.add_entry('UML', 'y')
expected_kconfig.add_entry('MMU', 'y')
expected_kconfig.add_entry('TEST', 'y')
expected_kconfig.add_entry('EXAMPLE_TEST', 'y')
expected_kconfig.add_entry('MK8', 'n')
self.assertEqual(kconfig, expected_kconfig)
def test_write_to_file(self):
kconfig_path = os.path.join(test_tmpdir, '.config')
expected_kconfig = kunit_config.Kconfig()
expected_kconfig.add_entry('UML', 'y')
expected_kconfig.add_entry('MMU', 'y')
expected_kconfig.add_entry('TEST', 'y')
expected_kconfig.add_entry('EXAMPLE_TEST', 'y')
expected_kconfig.add_entry('MK8', 'n')
expected_kconfig.write_to_file(kconfig_path)
actual_kconfig = kunit_config.parse_file(kconfig_path)
self.assertEqual(actual_kconfig, expected_kconfig)
class KUnitParserTest(unittest.TestCase):
def setUp(self):
self.print_mock = mock.patch('kunit_printer.Printer.print').start()
self.addCleanup(mock.patch.stopall)
def noPrintCallContains(self, substr: str):
for call in self.print_mock.mock_calls:
self.assertNotIn(substr, call.args[0])
def assertContains(self, needle: str, haystack: kunit_parser.LineStream):
# Clone the iterator so we can print the contents on failure.
copy, backup = itertools.tee(haystack)
for line in copy:
if needle in line:
return
raise AssertionError(f'"{needle}" not found in {list(backup)}!')
def test_output_isolated_correctly(self):
log_path = _test_data_path('test_output_isolated_correctly.log')
with open(log_path) as file:
result = kunit_parser.extract_tap_lines(file.readlines())
self.assertContains('TAP version 14', result)
self.assertContains('# Subtest: example', result)
self.assertContains('1..2', result)
self.assertContains('ok 1 - example_simple_test', result)
self.assertContains('ok 2 - example_mock_test', result)
self.assertContains('ok 1 - example', result)
def test_output_with_prefix_isolated_correctly(self):
log_path = _test_data_path('test_pound_sign.log')
with open(log_path) as file:
result = kunit_parser.extract_tap_lines(file.readlines())
self.assertContains('TAP version 14', result)
self.assertContains('# Subtest: kunit-resource-test', result)
self.assertContains('1..5', result)
self.assertContains('ok 1 - kunit_resource_test_init_resources', result)
self.assertContains('ok 2 - kunit_resource_test_alloc_resource', result)
self.assertContains('ok 3 - kunit_resource_test_destroy_resource', result)
self.assertContains('foo bar #', result)
self.assertContains('ok 4 - kunit_resource_test_cleanup_resources', result)
self.assertContains('ok 5 - kunit_resource_test_proper_free_ordering', result)
self.assertContains('ok 1 - kunit-resource-test', result)
self.assertContains('foo bar # non-kunit output', result)
self.assertContains('# Subtest: kunit-try-catch-test', result)
self.assertContains('1..2', result)
self.assertContains('ok 1 - kunit_test_try_catch_successful_try_no_catch',
result)
self.assertContains('ok 2 - kunit_test_try_catch_unsuccessful_try_does_catch',
result)
self.assertContains('ok 2 - kunit-try-catch-test', result