示例#1
0
    def test__update_hosts(self):
        rsc = {"control": [Host("1.2.3.4", alias="foo"), Host("1.2.3.5", alias="bar")]}
        facts = {
            "foo": {
                "ansible_eth0": {"ipv4": {"address": "1.2.3.1"}},
                "ansible_eth1": {"ipv4": {"address": "2.2.3.1"}},
                "networks": [
                    {"cidr": "1.2.3.0/24", "device": "eth0", "roles": ["network1"]},
                    {"cidr": "2.2.3.0/24", "device": "eth1", "roles": ["network2"]},
                ],
            },
            "bar": {
                "ansible_eth0": {"ipv4": {"address": "1.2.3.1"}},
                "ansible_eth1": {"ipv4": {"address": "2.2.3.1"}},
                "networks": [
                    {"cidr": "1.2.3.0/24", "device": "eth0", "roles": ["network1"]},
                    {"cidr": "2.2.3.0/24", "device": "eth1", "roles": ["network2"]},
                ],
            },
        }

        _update_hosts(rsc, facts)
        for host in gen_rsc(rsc):
            self.assertEqual("eth0", host.extra["network1"])
            self.assertEqual("eth0", host.extra["network1_dev"])
            self.assertEqual("eth1", host.extra["network2"])
            self.assertEqual("eth1", host.extra["network2_dev"])
示例#2
0
    def test__update_hosts_inverted(self):
        rsc = {"control": [Host("1.2.3.4", alias="foo"), Host("1.2.3.5", alias="bar")]}
        facts = {
            "foo": {
                "networks": [{
                    "cidr": "1.2.3.0/24",
                    "device": "eth0",
                    "roles": ["network1"]
                }, {
                    "cidr": "2.2.3.0/24",
                    "device": "eth1",
                    "roles": ["network2"]
                }]},
            "bar": {
                "networks": [{
                    "cidr": "1.2.3.0/24",
                    "device": "eth1",
                    "roles": ["network1"]
                }, {
                    "cidr": "2.2.3.0/24",
                    "device": "eth0",
                    "roles": ["network2"]
                }]},
            }

        _update_hosts(rsc, facts)
        for host in gen_rsc(rsc):
            if host.alias == "foo":
                self.assertEqual("eth0", host.extra["network1"])
                self.assertEqual("eth1", host.extra["network2"])
            elif host.alias == "bar":
                self.assertEqual("eth1", host.extra["network1"])
                self.assertEqual("eth0", host.extra["network2"])
示例#3
0
    def test__update_hosts_unmatch(self):
        rsc = {"control": [Host("1.2.3.4", alias="foo")]}
        facts = {
                "foo": {
                    "networks": [{
                        "cidr": "1.2.3.0/24",
                        "device": "eth0",
                        "roles": ["network1"]
                    }, {
                        "cidr": "2.2.3.0/24",
                        "device": None
                    }]},
                }

        _update_hosts(rsc, facts)
        for host in gen_rsc(rsc):
            self.assertEqual("eth0", host.extra["network1"])
            self.assertTrue("network2" not in host.extra)
示例#4
0
    def test__update_hosts_unmatch(self):
        rsc = {"control": [Host("1.2.3.4", alias="foo")]}
        facts = {
            "foo": {
                "ansible_eth0": {"ipv4": {"address": "1.2.3.1"}},
                "ansible_eth1": {"ipv4": {"address": "2.2.3.2"}},
                "networks": [
                    {"cidr": "1.2.3.0/24", "device": "eth0", "roles": ["network1"]},
                    {"cidr": "2.2.3.0/24", "device": None},
                ],
            }
        }

        _update_hosts(rsc, facts)
        for host in gen_rsc(rsc):
            self.assertEqual("eth0", host.extra["network1"])
            self.assertEqual("eth0", host.extra["network1_dev"])
            self.assertEqual("1.2.3.1", host.extra["network1_ip"])
            self.assertTrue("network2" not in host.extra)
            self.assertTrue("network2_dev" not in host.extra)
            self.assertTrue("network2_ip" not in host.extra)
示例#5
0
    def test__update_hosts_inverted(self):
        rsc = {"control": [Host("1.2.3.4", alias="foo"), Host("1.2.3.5", alias="bar")]}
        facts = {
            "foo": {
                # since 2.2.1 we need extra facts to be present
                "ansible_eth0": {"ipv4": {"address": "1.2.3.1"}},
                "ansible_eth1": {"ipv4": {"address": "2.2.3.1"}},
                "networks": [
                    {"cidr": "1.2.3.0/24", "device": "eth0", "roles": ["network1"]},
                    {"cidr": "2.2.3.0/24", "device": "eth1", "roles": ["network2"]},
                ],
            },
            "bar": {
                # since 2.2.1 we need extra facts to be present
                "ansible_eth0": {"ipv4": {"address": "2.2.3.2"}},
                "ansible_eth1": {"ipv4": {"address": "1.2.3.2"}},
                "networks": [
                    {"cidr": "1.2.3.0/24", "device": "eth1", "roles": ["network1"]},
                    {"cidr": "2.2.3.0/24", "device": "eth0", "roles": ["network2"]},
                ],
            },
        }

        _update_hosts(rsc, facts)
        for host in gen_rsc(rsc):
            if host.alias == "foo":
                self.assertEqual("eth0", host.extra["network1"])
                self.assertEqual("eth0", host.extra["network1_dev"])
                self.assertEqual("1.2.3.1", host.extra["network1_ip"])
                self.assertEqual("eth1", host.extra["network2"])
                self.assertEqual("eth1", host.extra["network2_dev"])
                self.assertEqual("2.2.3.1", host.extra["network2_ip"])
            elif host.alias == "bar":
                self.assertEqual("eth1", host.extra["network1"])
                self.assertEqual("eth1", host.extra["network1_dev"])
                self.assertEqual("1.2.3.2", host.extra["network1_ip"])
                self.assertEqual("eth0", host.extra["network2"])
                self.assertEqual("eth0", host.extra["network2_dev"])
                self.assertEqual("2.2.3.2", host.extra["network2_ip"])