Beispiel #1
0
    def test_remove_group_from_host(self):
        data = {"var3": "val3"}
        orig_data = {"var1": "val1"}
        g1 = inventory.Group(name="g1", data=orig_data)
        g2 = inventory.Group(name="g2", groups=inventory.ParentGroups([g1]))
        g3 = inventory.Group(name="g3",
                             groups=inventory.ParentGroups([g2]),
                             data=data)
        h1 = inventory.Host(name="h1",
                            groups=inventory.ParentGroups([g1, g2, g3]))
        h2 = inventory.Host(name="h2")
        hosts = {"h1": h1, "h2": h2}
        groups = {"g1": g1, "g2": g2}
        inv = inventory.Inventory(hosts=hosts, groups=groups)

        assert "h1" in inv.hosts
        assert g3 in inv.hosts["h1"].groups
        assert h1.get("var3") == "val3"

        g3.data["var3"] = "newval3"
        assert h1.get("var3", None) == "newval3"

        h1.groups.remove(g3)
        assert g3 not in h1.groups
        assert h1.get("var3", None) is None
        assert h1.get("var1", None) == "val1"

        with pytest.raises(ValueError):
            h1.groups.remove(g3)
Beispiel #2
0
 def test_add_group(self):
     connection_options = {"username": "******", "password": "******"}
     data = {"test_var": "test_value"}
     defaults = inventory.Defaults(data=data,
                                   connection_options=connection_options)
     g1 = inventory.Group(name="g1")
     g2 = inventory.Group(name="g2", groups=inventory.ParentGroups([g1]))
     h1 = inventory.Host(name="h1", groups=inventory.ParentGroups([g1, g2]))
     h2 = inventory.Host(name="h2")
     hosts = {"h1": h1, "h2": h2}
     groups = {"g1": g1, "g2": g2}
     inv = inventory.Inventory(hosts=hosts,
                               groups=groups,
                               defaults=defaults)
     g3_connection_options = inventory.ConnectionOptions(
         extras={"device_type": "cisco_ios"})
     inv.groups["g3"] = inventory.Group(
         name="g3",
         username="******",
         connection_options={"netmiko": g3_connection_options},
         defaults=defaults,
     )
     assert "g1" in [i.name for i in inv.groups["g2"].groups]
     assert "g3" in inv.groups
     assert (inv.groups["g3"].defaults.connection_options.get("username") ==
             "test_user")
     assert (inv.groups["g3"].defaults.connection_options.get("password") ==
             "test_pass")
     assert "test_var" in inv.groups["g3"].defaults.data.keys()
     assert "test_value" == inv.groups["g3"].defaults.data.get("test_var")
     assert (inv.groups["g3"].connection_options["netmiko"].
             extras["device_type"] == "cisco_ios")
Beispiel #3
0
 def test_add_host(self):
     data = {"test_var": "test_value"}
     defaults = inventory.Defaults(data=data)
     g1 = inventory.Group(name="g1")
     g2 = inventory.Group(name="g2", groups=inventory.ParentGroups([g1]))
     h1 = inventory.Host(name="h1", groups=inventory.ParentGroups([g1, g2]))
     h2 = inventory.Host(name="h2")
     hosts = {"h1": h1, "h2": h2}
     groups = {"g1": g1, "g2": g2}
     inv = inventory.Inventory(hosts=hosts,
                               groups=groups,
                               defaults=defaults)
     h3_connection_options = inventory.ConnectionOptions(
         extras={"device_type": "cisco_ios"})
     inv.hosts["h3"] = inventory.Host(
         name="h3",
         groups=[g1],
         platform="TestPlatform",
         connection_options={"netmiko": h3_connection_options},
         defaults=defaults,
     )
     assert "h3" in inv.hosts
     assert "g1" in [i.name for i in inv.hosts["h3"].groups]
     assert "test_var" in inv.hosts["h3"].defaults.data.keys()
     assert inv.hosts["h3"].defaults.data.get("test_var") == "test_value"
     assert inv.hosts["h3"].platform == "TestPlatform"
     assert (inv.hosts["h3"].connection_options["netmiko"].
             extras["device_type"] == "cisco_ios")
Beispiel #4
0
    def test_host(self):
        h = inventory.Host(name="host1", hostname="host1")
        assert h.hostname == "host1"
        assert h.port is None
        assert h.username is None
        assert h.password is None
        assert h.platform is None
        assert h.data == {}

        data = {"asn": 65100, "router_id": "1.1.1.1"}
        h = inventory.Host(
            name="host2",
            hostname="host2",
            username="******",
            port=123,
            password="",
            platform="fake",
            data=data,
        )
        assert h.hostname == "host2"
        assert h.port == 123
        assert h.username == "user"
        assert h.password == ""
        assert h.platform == "fake"
        assert h.data == data
Beispiel #5
0
 def test_inventory(self):
     g1 = inventory.Group(name="g1")
     g2 = inventory.Group(name="g2", groups=inventory.ParentGroups([g1]))
     h1 = inventory.Host(name="h1", groups=inventory.ParentGroups([g1, g2]))
     h2 = inventory.Host(name="h2")
     hosts = {"h1": h1, "h2": h2}
     groups = {"g1": g1, "g2": g2}
     inv = inventory.Inventory(hosts=hosts, groups=groups)
     assert "h1" in inv.hosts
     assert "h2" in inv.hosts
     assert "g1" in inv.groups
     assert "g2" in inv.groups
     assert inv.groups["g1"] in inv.hosts["h1"].groups
     assert inv.groups["g1"] in inv.groups["g2"].groups
Beispiel #6
0
 def test_add_group(self):
     connection_options = {"username": "******", "password": "******"}
     data = {"test_var": "test_value"}
     defaults = inventory.Defaults(data=data,
                                   connection_options=connection_options)
     g1 = inventory.Group(name="g1")
     g2 = inventory.Group(name="g2", groups=inventory.ParentGroups(["g1"]))
     h1 = inventory.Host(name="h1",
                         groups=inventory.ParentGroups(["g1", "g2"]))
     h2 = inventory.Host(name="h2")
     hosts = {"h1": h1, "h2": h2}
     groups = {"g1": g1, "g2": g2}
     inv = inventory.Inventory(hosts=hosts,
                               groups=groups,
                               defaults=defaults)
     g3_connection_options = {
         "netmiko": {
             "extras": {
                 "device_type": "cisco_ios"
             }
         }
     }
     inv.add_group(name="g3",
                   username="******",
                   connection_options=g3_connection_options)
     assert "g1" in [i.name for i in inv.groups["g2"].groups.refs]
     assert "g3" in inv.groups
     assert (inv.groups["g3"].defaults.connection_options.get("username") ==
             "test_user")
     assert (inv.groups["g3"].defaults.connection_options.get("password") ==
             "test_pass")
     assert "test_var" in inv.groups["g3"].defaults.data.keys()
     assert "test_value" == inv.groups["g3"].defaults.data.get("test_var")
     assert (inv.groups["g3"].connection_options["netmiko"].
             extras["device_type"] == "cisco_ios")
     # Test with one undefined parent group
     with pytest.raises(KeyError):
         inv.add_group(name="g4", groups=["undefined"])
     # Test with one defined and one undefined parent group
     with pytest.raises(KeyError):
         inv.add_group(name="g4", groups=["g1", "undefined"])
Beispiel #7
0
    def test_add_group_to_host_runtime(self):
        orig_data = {"var1": "val1"}
        data = {"var3": "val3"}
        g1 = inventory.Group(name="g1", data=orig_data)
        g2 = inventory.Group(name="g2", groups=inventory.ParentGroups([g1]))
        g3 = inventory.Group(name="g3",
                             groups=inventory.ParentGroups([g2]),
                             data=data)
        h1 = inventory.Host(name="h1", groups=inventory.ParentGroups([g1, g2]))
        h2 = inventory.Host(name="h2")
        hosts = {"h1": h1, "h2": h2}
        groups = {"g1": g1, "g2": g2}
        inv = inventory.Inventory(hosts=hosts, groups=groups)

        assert "h1" in inv.hosts
        assert g3 not in inv.hosts["h1"].groups
        assert h1.get("var3", None) is None

        h1.groups.add(g3)
        assert g3 in h1.groups
        assert h1.get("var3", None) == "val3"
Beispiel #8
0
 def test_add_host(self):
     data = {"test_var": "test_value"}
     defaults = inventory.Defaults(data=data)
     g1 = inventory.Group(name="g1")
     g2 = inventory.Group(name="g2", groups=inventory.ParentGroups(["g1"]))
     h1 = inventory.Host(name="h1",
                         groups=inventory.ParentGroups(["g1", "g2"]))
     h2 = inventory.Host(name="h2")
     hosts = {"h1": h1, "h2": h2}
     groups = {"g1": g1, "g2": g2}
     inv = inventory.Inventory(hosts=hosts,
                               groups=groups,
                               defaults=defaults)
     h3_connection_options = {
         "netmiko": {
             "extras": {
                 "device_type": "cisco_ios"
             }
         }
     }
     inv.add_host(
         name="h3",
         groups=["g1"],
         platform="TestPlatform",
         connection_options=h3_connection_options,
     )
     assert "h3" in inv.hosts
     assert "g1" in [i.name for i in inv.hosts["h3"].groups.refs]
     assert "test_var" in inv.hosts["h3"].defaults.data.keys()
     assert inv.hosts["h3"].defaults.data.get("test_var") == "test_value"
     assert inv.hosts["h3"].platform == "TestPlatform"
     assert (inv.hosts["h3"].connection_options["netmiko"].
             extras["device_type"] == "cisco_ios")
     with pytest.raises(KeyError):
         inv.add_host(name="h4", groups=["not_defined"])
     # Test with one good and one undefined group
     with pytest.raises(KeyError):
         inv.add_host(name="h5", groups=["g1", "not_defined"])
 def deserialize_host(cls, **kwargs: Any) -> inventory.Host:
     return inventory.Host(**cls.deserialize(**kwargs))