Optimized mode_reset test

This commit is contained in:
Kevin Veen-Birkenbach 2025-07-21 09:08:03 +02:00
parent 14dde77134
commit d9c4493e0d
No known key found for this signature in database
GPG Key ID: 44D8F11FD62F878E

View File

@ -1,6 +1,5 @@
import os import os
import unittest import unittest
import yaml
# Base directory for roles (adjust if needed) # Base directory for roles (adjust if needed)
BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../roles')) BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../roles'))
@ -8,22 +7,20 @@ BASE_DIR = os.path.abspath(os.path.join(os.path.dirname(__file__), '../../roles'
class TestModeResetIntegration(unittest.TestCase): class TestModeResetIntegration(unittest.TestCase):
""" """
Integration test to verify that when 'mode_reset' is used in any task file, Integration test to verify that when 'mode_reset' is used in any task file,
the role provides a reset.yml and includes it correctly in main.yml, the role provides a *_reset.yml (or reset.yml) and includes it correctly in main.yml,
and that the include_tasks for reset.yml with the mode_reset condition appears only once. and that the include_tasks for that file with the mode_reset condition appears only once.
""" """
def test_mode_reset_tasks(self): def test_mode_reset_tasks(self):
# Iterate through each role directory
for role_name in os.listdir(BASE_DIR): for role_name in os.listdir(BASE_DIR):
with self.subTest(role=role_name): with self.subTest(role=role_name):
role_path = os.path.join(BASE_DIR, role_name) role_path = os.path.join(BASE_DIR, role_name)
tasks_dir = os.path.join(role_path, 'tasks') tasks_dir = os.path.join(role_path, 'tasks')
# Only consider directories with a tasks folder
if not os.path.isdir(tasks_dir): if not os.path.isdir(tasks_dir):
self.skipTest(f"Role '{role_name}' has no tasks directory.") self.skipTest(f"Role '{role_name}' has no tasks directory.")
# Simplified detection: check raw file content for 'mode_reset' # Look for 'mode_reset' in task files
mode_reset_found = False mode_reset_found = False
for root, _, files in os.walk(tasks_dir): for root, _, files in os.walk(tasks_dir):
for fname in files: for fname in files:
@ -31,25 +28,26 @@ class TestModeResetIntegration(unittest.TestCase):
continue continue
file_path = os.path.join(root, fname) file_path = os.path.join(root, fname)
with open(file_path, 'r', encoding='utf-8') as f: with open(file_path, 'r', encoding='utf-8') as f:
file_content = f.read() if 'mode_reset' in f.read():
if 'mode_reset' in file_content: mode_reset_found = True
mode_reset_found = True break
break
if mode_reset_found: if mode_reset_found:
break break
# If no mode_reset usage, skip this role
if not mode_reset_found: if not mode_reset_found:
self.skipTest(f"Role '{role_name}': no mode_reset usage detected.") self.skipTest(f"Role '{role_name}': no mode_reset usage detected.")
# 1) Check reset.yml exists # Check *_reset.yml exists
reset_yml = os.path.join(tasks_dir, 'reset.yml') reset_files = [
fname for fname in os.listdir(tasks_dir)
if fname.endswith('_reset.yml') or fname == 'reset.yml'
]
self.assertTrue( self.assertTrue(
os.path.isfile(reset_yml), reset_files,
f"Role '{role_name}': 'mode_reset' used but tasks/reset.yml is missing." f"Role '{role_name}': 'mode_reset' used but no *_reset.yml or reset.yml found in tasks/."
) )
# 2) Check inclusion in main.yml # Check main.yml exists
main_yml = os.path.join(tasks_dir, 'main.yml') main_yml = os.path.join(tasks_dir, 'main.yml')
self.assertTrue( self.assertTrue(
os.path.isfile(main_yml), os.path.isfile(main_yml),
@ -59,10 +57,22 @@ class TestModeResetIntegration(unittest.TestCase):
with open(main_yml, 'r', encoding='utf-8') as f: with open(main_yml, 'r', encoding='utf-8') as f:
content = f.read() content = f.read()
include_line = 'include_tasks: reset.yml' # Match the actual reset file name used in include_tasks
found_include = None
for reset_file in reset_files:
if f'include_tasks: {reset_file}' in content:
found_include = reset_file
break
self.assertIsNotNone(
found_include,
f"Role '{role_name}': tasks/main.yml must include one of {reset_files} with 'include_tasks'."
)
# Check the inclusion has the correct when condition
include_line = f'include_tasks: {found_include}'
when_line = 'when: mode_reset | bool' when_line = 'when: mode_reset | bool'
# Ensure the include and when lines are present
self.assertIn( self.assertIn(
include_line, include_line,
content, content,
@ -73,17 +83,13 @@ class TestModeResetIntegration(unittest.TestCase):
content, content,
f"Role '{role_name}': tasks/main.yml missing '{when_line}'." f"Role '{role_name}': tasks/main.yml missing '{when_line}'."
) )
# 3) Ensure the reset include with mode_reset appears only once
include_count = content.count(include_line)
when_count = content.count(when_line)
self.assertEqual( self.assertEqual(
include_count, 1, content.count(include_line), 1,
f"Role '{role_name}': 'include_tasks: reset.yml' must appear exactly once, found {include_count}." f"Role '{role_name}': '{include_line}' must appear exactly once."
) )
self.assertEqual( self.assertEqual(
when_count, 1, content.count(when_line), 1,
f"Role '{role_name}': 'when: mode_reset | bool' must appear exactly once, found {when_count}." f"Role '{role_name}': '{when_line}' must appear exactly once."
) )
if __name__ == '__main__': if __name__ == '__main__':