#!/usr/bin/python3
# SPDX-FileCopyrightText: 2024-2025 Univention GmbH
# SPDX-License-Identifier: AGPL-3.0-only
"""Unit test for univention.management.console.modules.setup.netconf.modules.RewritePxe"""
import os
# pylint: disable-msg=C0103,E0611,R0904
import unittest

import univention.management.console.modules


univention.management.console.modules.__path__.insert(0, os.path.join(os.path.dirname(__file__), os.path.pardir, 'umc/python'))
from univention.management.console.modules.setup.netconf import ChangeSet  # noqa: E402
from univention.management.console.modules.setup.netconf.modules import RewritePxe  # noqa: E402


class DummyOption:

    def __init__(self):
        self.no_act = True


class TestPxeRewrite(unittest.TestCase):

    def setUp(self):
        ucr = {
            "interfaces/eth0/address": "2.3.4.5",
            "interfaces/eth0/netmask": "255.255.255.0",
        }
        profile = {key: None for key in ucr.keys() if key.startswith("interfaces/")}
        profile.update({
            "interfaces/eth0/address": "2.3.4.100",
            "interfaces/eth0/netmask": "255.255.255.0",
        })
        options = DummyOption()
        self.cs = ChangeSet(ucr, profile, options)
        self.phase = RewritePxe.PhaseRewritePxe(self.cs)
        self.mapping = RewritePxe.Mapping(self.phase.ipv4_changes())

    def test_plain(self):
        assert self.mapping.apply("2.3.4.5") == "2.3.4.100"

    def test_other(self):
        assert self.mapping.apply("2.3.4.6") == "2.3.4.6"

    def test_multiple(self):
        assert self.mapping.apply("2.3.4.5 2.3.4.5") == "2.3.4.100 2.3.4.100"

    def test_prefix(self):
        assert self.mapping.apply("2.3.4.55") == "2.3.4.55"

    def test_suffix(self):
        assert self.mapping.apply("22.3.4.5") == "22.3.4.5"

    def test_embedded(self):
        assert self.mapping.apply("foo=2.3.4.5:123") == "foo=2.3.4.100:123"


class TestPxeRewriteMultiple(unittest.TestCase):

    def setUp(self):
        ucr = {
            "interfaces/eth0/address": "2.3.4.5",
            "interfaces/eth0/netmask": "255.255.255.0",
            "interfaces/eth1/address": "9.8.7.6",
            "interfaces/eth1/netmask": "255.255.0.0",
        }
        profile = {key: None for key in ucr.keys() if key.startswith("interfaces/")}
        profile.update({
            "interfaces/eth0/address": "2.3.4.100",
            "interfaces/eth0/netmask": "255.255.255.0",
            "interfaces/eth1/address": "9.8.7.100",
            "interfaces/eth1/netmask": "255.255.0.0",
        })
        options = DummyOption()
        self.cs = ChangeSet(ucr, profile, options)
        self.phase = RewritePxe.PhaseRewritePxe(self.cs)
        self.mapping = RewritePxe.Mapping(self.phase.ipv4_changes())

    def test_first(self):
        assert self.mapping.apply("2.3.4.5") == "2.3.4.100"

    def test_second(self):
        assert self.mapping.apply("9.8.7.6") == "9.8.7.100"

    def test_both(self):
        assert self.mapping.apply("2.3.4.5 9.8.7.6") == "2.3.4.100 9.8.7.100"


class TestPxeRewriteRemoved(unittest.TestCase):

    def setUp(self):
        ucr = {
            "interfaces/eth0/address": "2.3.4.5",
            "interfaces/eth0/netmask": "255.255.255.0",
            "interfaces/eth1/address": "9.8.7.6",
            "interfaces/eth1/netmask": "255.255.0.0",
        }
        profile = {key: None for key in ucr.keys() if key.startswith("interfaces/")}
        profile.update({
            "interfaces/eth1/address": "9.8.7.100",
            "interfaces/eth1/netmask": "255.255.0.0",
            "interfaces/primary": "eth1",
        })
        options = DummyOption()
        self.cs = ChangeSet(ucr, profile, options)
        self.phase = RewritePxe.PhaseRewritePxe(self.cs)
        self.mapping = RewritePxe.Mapping(self.phase.ipv4_changes())

    def test_first(self):
        assert self.mapping.apply("2.3.4.5") == "9.8.7.100"

    def test_second(self):
        assert self.mapping.apply("9.8.7.6") == "9.8.7.100"

    def test_both(self):
        assert self.mapping.apply("2.3.4.5 9.8.7.6") == "9.8.7.100 9.8.7.100"


if __name__ == '__main__':
    unittest.main()
