File: main.py - Helmigreg/ant GitHub Wiki

Technical Documentation: main.py

Overview

The module main.py is the main entry point of the ANT (Automated NFT Testing) project. It coordinates the entire automated test pipeline for nftables scripts. This includes loading configurations, validating input scripts, setting up the test environment, running Ansible-based tests, and generating reports.

Module Structure

Function ant_main(...): Main controller function that handles all stages of the testing process.

Parameters:
- config (str): Path to protocol-mapping configuration file
- net_config_path (str): Path to network infrastructure YAML file
- testcases_path (str): Path to testcase definitions YAML file
- verbose (bool): Enables verbose output
- dir (str): Output directory for logs and reports
- report (str): Optional explicit report output path
- protocol (str): Optional explicit protocol output path

Workflow:
1. Initializes configuration objects and a Logger instance
2. Validates existence of configuration files
3. Loads and parses network and testcase YAML definitions
4. Validates syntax of uploaded nftables scripts on the machines
5. Sets up firewall configuration using Ansible
6. Creates Ansible inventory for test execution
7. Runs tests and collects output
8. Evaluates test results and aggregates scoring
9. Writes output report and terminates with appropriate return code

Function finalize(logger: Logger, rc: int, dir: str, report: str, protocol: str): Helper function to finalize execution.

- Calls Logger.write() to generate output
- Prints summary to stdout
- Exits with return code rc

Main Block: Uses argparse to parse CLI arguments and invokes ant_main().

Validation and Error-Handling

  • Handles FileNotFoundError and AttributeError for missing or malformed input files
  • Uses verbose mode to print exception details
  • NFTables syntax validation is performed using the validate_script() utility
  • Errors and events are centrally collected using a Logger instance
  • Fatal errors trigger immediate termination via finalize()

Dependencies

  • argparse: For command-line parsing

  • sys, os: For file path and exit control

  • ant_backend:

    • AnsibleManager: Manages inventory creation and Ansible execution
    • Logger: Collects events, errors, and handles reporting
    • NetworkConfiguration: Parses network topology YAML
    • TestcaseConfiguration: Parses firewall test rules YAML
    • validate_script: Validates nftables script syntax