def test_diff_children(): """Test the basic functionality of the Diff class when adding child elements.""" diff = Diff() device_element = DiffElement("device", "device1", {"name": "device1"}) intf_element = DiffElement("interface", "eth0", { "device_name": "device1", "name": "eth0" }) diff.add(device_element) assert "device" in diff.children assert diff.children["device"] == {"device1": device_element} assert list(diff.groups()) == ["device"] assert not diff.has_diffs() assert list(diff.get_children()) == [device_element] with pytest.raises(ObjectAlreadyExists): diff.add(device_element) diff.add(intf_element) assert "interface" in diff.children assert diff.children["interface"] == {"eth0": intf_element} assert list(diff.groups()) == ["device", "interface"] assert not diff.has_diffs() assert list(diff.get_children()) == [device_element, intf_element] with pytest.raises(ObjectAlreadyExists): diff.add(intf_element) source_attrs = { "interface_type": "ethernet", "description": "my interface" } dest_attrs = {"description": "your interface"} intf_element.add_attrs(source=source_attrs, dest=dest_attrs) assert diff.has_diffs()
def test_diff_element_summary_with_no_diffs(): element = DiffElement("interface", "eth0", { "device_name": "device1", "name": "eth0" }) assert element.summary() == { "create": 0, "update": 0, "delete": 0, "no-change": 1 }
def test_diff_element_empty(): """Test the basic functionality of the DiffElement class when initialized and empty.""" element = DiffElement("interface", "eth0", { "device_name": "device1", "name": "eth0" }) assert element.type == "interface" assert element.name == "eth0" assert element.keys == {"device_name": "device1", "name": "eth0"} assert element.source_name == "source" assert element.dest_name == "dest" assert element.source_attrs is None assert element.dest_attrs is None assert list(element.get_children()) == [] assert not element.has_diffs() assert not element.has_diffs(include_children=True) assert not element.has_diffs(include_children=False) assert element.get_attrs_keys() == [] element2 = DiffElement("interface", "eth0", { "device_name": "device1", "name": "eth0" }, source_name="S1", dest_name="D1") assert element2.source_name == "S1" assert element2.dest_name == "D1"
def test_diff_element_dict_with_diffs_no_attrs(): element = DiffElement("interface", "eth0", { "device_name": "device1", "name": "eth0" }) element.add_attrs(source={}) assert element.dict() == {"+": {}} element.add_attrs(dest={}) assert element.dict() == {"+": {}, "-": {}}
def test_diff_element_str_with_diffs(): element = DiffElement("interface", "eth0", {"device_name": "device1", "name": "eth0"}) element.add_attrs(source={"interface_type": "ethernet", "description": "my interface"}) assert element.str() == "interface: eth0 MISSING in dest" element.add_attrs(dest={"description": "your interface"}) assert ( element.str() == """\ interface: eth0 description source(my interface) dest(your interface)\ """ )
def diff_children_nyc_dev1(): """Fixture dict of DiffElement.""" children = dict() device_name = "nyc-dev1" site_name = "nyc" for intf_name, intf in BackendB.DATA[site_name][device_name][ "interfaces"].items(): children[intf_name] = DiffElement(obj_type="interface", name=intf_name, keys=dict(device_name=device_name)) children[intf_name].add_attrs(source=intf) for intf_name, intf in BackendA.DATA[site_name][device_name][ "interfaces"].items(): if intf_name not in children: children[intf_name] = DiffElement( obj_type="interface", name=intf_name, keys=dict(device_name=device_name)) children[intf_name].add_attrs(dest=intf) return children
def test_diff_element_summary_with_diffs(): element = DiffElement("interface", "eth0", { "device_name": "device1", "name": "eth0" }) element.add_attrs(source={ "interface_type": "ethernet", "description": "my interface" }) assert element.summary() == { "create": 1, "update": 0, "delete": 0, "no-change": 0 } element.add_attrs(dest={"description": "your interface"}) assert element.summary() == { "create": 0, "update": 1, "delete": 0, "no-change": 0 }
def test_diff_element_dict_with_diffs(): element = DiffElement("interface", "eth0", { "device_name": "device1", "name": "eth0" }) element.add_attrs(source={ "interface_type": "ethernet", "description": "my interface" }) assert element.dict() == { "+": { "description": "my interface", "interface_type": "ethernet" } } element.add_attrs(dest={"description": "your interface"}) assert element.dict() == { "-": { "description": "your interface" }, "+": { "description": "my interface" } }
def test_diff_element_attrs(): """Test the basic functionality of the DiffElement class when setting and retrieving attrs.""" element = DiffElement("interface", "eth0", {"device_name": "device1", "name": "eth0"}) source_attrs = {"interface_type": "ethernet", "description": "my interface"} element.add_attrs(source=source_attrs) assert element.source_attrs == source_attrs assert element.has_diffs() assert element.has_diffs(include_children=True) assert element.has_diffs(include_children=False) assert element.get_attrs_keys() == source_attrs.keys() dest_attrs = {"description": "your interface"} element.add_attrs(dest=dest_attrs) assert element.source_attrs == source_attrs assert element.dest_attrs == dest_attrs assert element.has_diffs() assert element.has_diffs(include_children=True) assert element.has_diffs(include_children=False) assert element.get_attrs_keys() == ["description"] # intersection of source_attrs.keys() and dest_attrs.keys()
def test_diff_element_len_with_no_diffs(): element = DiffElement("interface", "eth0", {"device_name": "device1", "name": "eth0"}) assert len(element) == 1
def test_diff_element_dict_with_no_diffs(): element = DiffElement("interface", "eth0", {"device_name": "device1", "name": "eth0"}) assert element.dict() == {}
def test_diff_element_str_with_no_diffs(): element = DiffElement("interface", "eth0", {"device_name": "device1", "name": "eth0"}) assert element.str() == "interface: eth0 (no diffs)"
def test_diff_element_children(): """Test the basic functionality of the DiffElement class when storing and retrieving child elements.""" child_element = DiffElement("interface", "eth0", {"device_name": "device1", "name": "eth0"}) parent_element = DiffElement("device", "device1", {"name": "device1"}) parent_element.add_child(child_element) assert list(parent_element.get_children()) == [child_element] assert not parent_element.has_diffs() assert not parent_element.has_diffs(include_children=True) assert not parent_element.has_diffs(include_children=False) source_attrs = {"interface_type": "ethernet", "description": "my interface"} dest_attrs = {"description": "your interface"} child_element.add_attrs(source=source_attrs, dest=dest_attrs) assert parent_element.has_diffs() assert parent_element.has_diffs(include_children=True) assert not parent_element.has_diffs(include_children=False)
def test_diff_element_len_with_diffs(): element = DiffElement("interface", "eth0", {"device_name": "device1", "name": "eth0"}) element.add_attrs(source={"interface_type": "ethernet", "description": "my interface"}) element.add_attrs(dest={"description": "your interface"}) assert len(element) == 1
def diff_element_with_children(): """Construct a DiffElement that has some diffs of its own as well as a child diff with additional diffs.""" # parent_element has differing "role" attribute, while "location" does not differ parent_element = DiffElement("device", "device1", {"name": "device1"}) parent_element.add_attrs(source={ "role": "switch", "location": "RTP" }, dest={ "role": "router", "location": "RTP" }) # child_element_1 has differing "description" attribute, while "interface_type" is only present on one side child_element_1 = DiffElement("interface", "eth0", { "device_name": "device1", "name": "eth0" }) source_attrs = { "interface_type": "ethernet", "description": "my interface" } dest_attrs = {"description": "your interface"} child_element_1.add_attrs(source=source_attrs, dest=dest_attrs) # child_element_2 only exists on source, and has no attributes child_element_2 = DiffElement("interface", "lo0", { "device_name": "device1", "name": "lo0" }) child_element_2.add_attrs(source={}) # child_element_3 only exists on dest, and has some attributes child_element_3 = DiffElement("interface", "lo1", { "device_name": "device1", "name": "lo1" }) child_element_3.add_attrs(dest={"description": "Loopback 1"}) # child_element_4 is identical between source and dest child_element_4 = DiffElement("interface", "lo100", { "device_name": "device1", "name": "lo100" }) child_element_4.add_attrs(source={"description": "Loopback 100"}, dest={"description": "Loopback 100"}) parent_element.add_child(child_element_1) parent_element.add_child(child_element_2) parent_element.add_child(child_element_3) parent_element.add_child(child_element_4) return parent_element
def diff_with_children(): """Provide a Diff which has multiple children, some of which have children of their own.""" diff = Diff() # person_element_1 only exists in the source person_element_1 = DiffElement("person", "Jimbo", {"name": "Jimbo"}) person_element_1.add_attrs(source={}) diff.add(person_element_1) # person_element_2 only exists in the dest person_element_2 = DiffElement("person", "Sully", {"name": "Sully"}) person_element_2.add_attrs(dest={}) diff.add(person_element_2) # device_element has no diffs of its own, but has a child intf_element device_element = DiffElement("device", "device1", {"name": "device1"}) diff.add(device_element) # intf_element exists in both source and dest as a child of device_element, and has differing attrs intf_element = DiffElement("interface", "eth0", { "device_name": "device1", "name": "eth0" }) source_attrs = { "interface_type": "ethernet", "description": "my interface" } dest_attrs = {"description": "your interface"} intf_element.add_attrs(source=source_attrs, dest=dest_attrs) device_element.add_child(intf_element) # address_element exists in both source and dest but has no diffs address_element = DiffElement("address", "RTP", {"name": "RTP"}) address_element.add_attrs(source={"state": "NC"}, dest={"state": "NC"}) diff.add(address_element) return diff