#!/usr/bin/python3
#
# Copyright (C) 2026 Red Hat, Inc.
# SPDX-License-Identifier: LGPL-2.1-or-later

from anacondalib import INSTALLER_VM_MEMORY_MB, VirtInstallMachineCase
from installer import Installer
from network import SYSROOT_PATH, Network
from progress import Progress
from testlib import test_main, timeout  # pylint: disable=import-error


class TestNetwork_E2E(VirtInstallMachineCase):
    provision = {"machine1": {"memory_mb": INSTALLER_VM_MEMORY_MB}}

    @timeout(1200)
    def testConnectionsE2E(self):
        """
        Description:
            Test network configuration modifies profiles correctly.

        Expected results:
            - Expected connection profiles in installer with expected values
            - Network configuration modifies correct profiles
            - Test that the booted system has expected network configuration
        """
        b = self.browser
        m = self.machine
        i = Installer(b, m)
        n = Network(b, m)

        i.open()

        iface = n.get_the_iface()
        n.preinstall_connection_test(i, iface)
        con_name = iface

        mtu_value = "1600"
        i.reach_on_sidebar(i.steps.NETWORK)
        n.set_mtu_on_iface_wizard(iface, mtu_value)

        n.check_con_settings([
            [con_name, "802-3-ethernet.mtu", mtu_value, None],
        ])

        i.reach(i.steps.REVIEW)
        i.begin_installation(needs_confirmation=False)
        p = Progress(b)
        p.wait_done()

        # Check profiles in installed system
        n.check_con_profile_files(con_name, 1, root=SYSROOT_PATH, file_name=con_name)
        # Check there is only single persistent profile
        n.check_con_profile_files("", 1, root=SYSROOT_PATH)

        n.check_con_settings([
            [con_name, "802-3-ethernet.mtu", mtu_value, None],
        ])

        self.handleReboot()

        # Check profiles in installed system
        n.check_con_profile_files(con_name, 1, file_name=con_name)
        # Check there is only single persistent profile
        n.check_con_profile_files("", 1)

        n.check_iface_state(iface, "GENERAL.CONNECTION", con_name)

        n.check_con_settings([
            [con_name, "802-3-ethernet.mtu", mtu_value, None],
        ])


if __name__ == '__main__':
    test_main()
