Module: read_testinfra - Helmigreg/ant GitHub Wiki

Technical Documentation: read_testinfra.py


Overview

The read_testinfra.py module is responsible for modeling and validating the structure of a network infrastructure, including:

  • Definitions of networks and machines
  • Validation of IP addressing (IPv4/IPv6)
  • Loading configuration from a .yml file
  • Ensuring consistency and existence of referenced resources (e.g., NFTable files)

This module is designed for use in network testbed setups where configuration integrity and address validation are critical.


Exception Handling

  • ValueError: Raised indirectly when an invalid IP address format is provided to ipaddress.ip_address.
  • AttributeError:
    • When IPv4 and IPv6 addresses are mixed within a machine.
    • When required fields (Networks, Machines, or at least one valid NFTable file) are missing from the YAML configuration.

Module Structure

class Machine

Represents a network-connected machine.

__init__(self, name: str, ip_addresses: list, management_ip: str, username: str, password: str, nftable: str)

Initializes a Machine object with network and authentication parameters.

  • Parameters:

    • name (str): Identifier for the machine.
    • ip_addresses (list): List of IP addresses (must be all IPv4 or all IPv6).
    • management_ip (str): IP used for management access.
    • username / password (str): Credentials for remote login.
    • nftable (str): Optional path to the machine’s NFTable file.
  • Validation:

    • All ip_addresses must be of the same IP version (IPv4 or IPv6).
    • Raises AttributeError on mixed IP version input.
  • Attributes:

    • name
    • ip_addresses (List of IPv4Address or IPv6Address)
    • management_ip (ipaddress object)
    • user, password
    • nftable

__eq__(self, other)

Checks object equality by comparing name, ip_addresses, and management_ip.

__repr__(self)

Returns a string representation including name, IPs, and management IP.


class NetworkConfiguration

Represents the overall network configuration, including all networks and machines.

__init__(self)

Initializes empty dictionaries for networks and machines.

  • Attributes:
    • networks (dict): Mapping of network names to ip_network objects.
    • machines (dict): Mapping of machine names to Machine objects.

add_networks(self, name, netmask, netaddress)

Adds a new network to the configuration.

  • Parameters:

    • name (str): Network identifier.
    • netmask (str or int): Subnet mask.
    • netaddress (str): Network address.
  • Result:

    • Updates self.networks with a new ip_network object.

add_machine(self, name, ip_address, management_ip, username, password, nftable)

Adds a machine entry to the configuration.

  • Parameters: Same as Machine.__init__.

  • Result:

    • Adds a Machine instance to self.machines.

__eq__(self, other)

Compares the networks and machines dictionaries for equality.

__repr__(self)

Returns a string showing all stored networks and machines.


load_from_yaml(self, file_name)

Loads a YAML configuration file and populates the network and machine objects.

  • Parameters:

    • file_name (str): Path to the YAML file describing the infrastructure.
  • Expected YAML Structure:

Networks:
  net1:
    Netmask: 255.255.255.0
    Netaddress: 192.168.1.0

Machines:
  host1:
    IP: [192.168.1.10]
    Management: 192.168.1.1
    User: admin
    Password: admin123
    NFTable: ./firewall.nft