def test_when_deep_same_tags(self): running_str = """ <vr> <interface> <ifName>xe17</ifName> <loadInterval>30</loadInterval> <description>ecmem8-C914057</description> <isAutospeed>true</isAutospeed> <speed>1g</speed> </interface> </vr> """ candidate_str = """ <vr> <interface> <ifName>xe17</ifName> <loadInterval>30</loadInterval> <description>ecmem8-C914057</description> <speed>1g</speed> <isAutospeed>true</isAutospeed> </interface> </vr> """ running = etree.fromstring(running_str) candidate = etree.fromstring(candidate_str) block = Block(running, candidate) actual = block.get_printable_diff() self.assertEqual([], actual)
def test_when_root_elements_change(self): self.maxDiff = None running_str = """ <logginglevel> <name>foo</name> <logginglevel>5</logginglevel> <bar>1</bar> </logginglevel> """ candidate_str = """ <logginglevel> <name>foo</name> <logginglevel>2</logginglevel> <bar>1</bar> </logginglevel> """ running = etree.fromstring(running_str) candidate = etree.fromstring(candidate_str) block = Block(running, candidate) actual = block.get_printable_diff() expected = [ '[logginglevel]', ' <name>foo</name>', ' <bar>1</bar>', '- <logginglevel>5</logginglevel>', '+ <logginglevel>2</logginglevel>' ] self.assertEqual(expected, actual)
def test_get_printable_with_deep_changes_and_added_on_candidate(self): running_str = """ <vr> <interface> <switchportAllowedEthertypes> <ipv6>true</ipv6> <ipv4>true</ipv4> </switchportAllowedEthertypes> </interface> </vr> """ candidate_str = """ <vr> <interface> <switchportAllowedEthertypes> <ipv6>true</ipv6> <ipv4>true</ipv4> <arp>true</arp> </switchportAllowedEthertypes> </interface> </vr> """ running = etree.fromstring(running_str) candidate = etree.fromstring(candidate_str) block = Block(running, candidate) actual = block.get_printable_diff() expected = [ '[vr]', ' <interface>', ' <switchportAllowedEthertypes>', ' <ipv6>true</ipv6>', ' <ipv4>true</ipv4>', '+ <arp>true</arp>', ' </switchportAllowedEthertypes>', ' </interface>' ] self.assertEqual(expected, actual)
def test_get_printable_diff_whole_block_added(self): candidate = etree.fromstring('<vr>1234</vr>') block = Block(None, candidate) actual = block.get_printable_diff() expected = ['[vr]', '+ <vr>1234</vr>'] self.assertEqual(expected, actual)
def test_get_printable_diff_whole_block_removed(self): running = etree.fromstring('<vr>1234</vr>') block = Block(running, None) actual = block.get_printable_diff() expected = ['[vr]', '- <vr>1234</vr>'] self.assertEqual(expected, actual)
def test_get_printable_with_deep_changes(self): running_str = """ <vr> <interface> <ifName>po1</ifName> <loadInterval>30</loadInterval> <ipAddr>10.32.49.1/30</ipAddr> <description>to foo.net</description> <mtu>1600</mtu> <portChannelLoadBal>rtag7</portChannelLoadBal> <ospfInterface> <ifNetworkType>point-to-points</ifNetworkType> <ipOspfInterface> <ifCostAddr>100000</ifCostAddr> </ipOspfInterface> </ospfInterface> </interface> </vr> """ candidate_str = """ <vr> <interface> <ifName>po1</ifName> <loadInterval>30</loadInterval> <ipAddr>10.32.49.1/30</ipAddr> <description>to foo.net</description> <mtu>1600</mtu> <portChannelLoadBal>rtag7</portChannelLoadBal> <ospfInterface> <ifNetworkType>point-to-point</ifNetworkType> <ipOspfInterface> <ifCostAddr>10000</ifCostAddr> </ipOspfInterface> </ospfInterface> </interface> </vr> """ running = etree.fromstring(running_str) candidate = etree.fromstring(candidate_str) block = Block(running, candidate) actual = block.get_printable_diff() expected = [ '[vr]', ' <interface>', ' <ospfInterface>', ' <ipOspfInterface>', '- <ifCostAddr>100000</ifCostAddr>', '+ <ifCostAddr>10000</ifCostAddr>', ' </ipOspfInterface>', '- <ifNetworkType>point-to-points</ifNetworkType>', '+ <ifNetworkType>point-to-point</ifNetworkType>', ' </ospfInterface>', ' <ifName>po1</ifName>', ' <loadInterval>30</loadInterval>', ' <ipAddr>10.32.49.1/30</ipAddr>', ' <description>to foo.net</description>', ' <mtu>1600</mtu>', ' <portChannelLoadBal>rtag7</portChannelLoadBal>', ' </interface>' ] self.assertEqual(expected, actual)
def test_get_printable_diff_for_removed_tags(self): running = etree.fromstring('<vr><vrId>1</vrId><vrId>2</vrId></vr>') candidate = etree.fromstring('<vr><vrId>1</vrId></vr>') block = Block(running, candidate) actual = block.get_printable_diff() expected = ['[vr]', '- <vrId>2</vrId>'] self.assertEqual(expected, actual)
def test_get_printable_diff_for_removed_and_added_tags(self): running = etree.fromstring( '<vr><vrId>1</vrId><vrId>2</vrId><foo>1</foo></vr>') candidate = etree.fromstring('<vr><vrId>1</vrId></vr>') block = Block(running, candidate) actual = block.get_printable_diff() self.assertTrue('[vr]' in actual) self.assertTrue('- <vrId>2</vrId>' in actual) self.assertTrue('- <foo>1</foo>' in actual)
def test_get_printable_with_whole_tag_missing(self): self.maxDiff = None running_str = """ <vr> <interface> <switchportAllowedEthertypes> <ipv6>true</ipv6> <ipv4>true</ipv4> </switchportAllowedEthertypes> </interface> <bgp> <name>has</name> </bgp> </vr> """ candidate_str = """ <vr> <interface> <switchportAllowedEthertypes> <ipv6>true</ipv6> <ipv4>true</ipv4> <arp>true</arp> </switchportAllowedEthertypes> </interface> </vr> """ running = etree.fromstring(running_str) candidate = etree.fromstring(candidate_str) block = Block(running, candidate) actual = block.get_printable_diff() self.assertTrue('[vr]' in actual) self.assertTrue(' <interface>' in actual) self.assertTrue(' <switchportAllowedEthertypes>' in actual) self.assertTrue(' <ipv6>true</ipv6>' in actual) self.assertTrue(' <ipv4>true</ipv4>' in actual) self.assertTrue('+ <arp>true</arp>' in actual) self.assertTrue(' </switchportAllowedEthertypes>' in actual) self.assertTrue(' </interface>' in actual) self.assertTrue('- <bgp>' in actual) self.assertTrue('- <name>has</name>' in actual) self.assertTrue('- </bgp>' in actual)
def test_when_whole_block_new_in_candidate(self): self.maxDiff = None running_str = """ <vr> <bgp> <bgpAs>65432</bgpAs> <setLogNbrChanges>true</setLogNbrChanges> <bgpAddressFamily> <afi>l2vpn</afi> <bgpSubAddressFamily> <safi>evpn</safi> <vrfAf> <vrfName>default</vrfName> </vrfAf> </bgpSubAddressFamily> </bgpAddressFamily> <bgpPeer> <peerAddr>10.0.2.129</peerAddr> <peerActivate>true</peerActivate> <sourceId>lo</sourceId> <peerDesc>foo-bar</peerDesc> <peerAs>65432</peerAs> <bgpPeerAddressFamily> <afi>l2vpn</afi> <bgpPeerSubAddressFamily> <safi>evpn</safi> <peerActivateAf>true</peerActivateAf> </bgpPeerSubAddressFamily> </bgpPeerAddressFamily> </bgpPeer> </bgp> </vr> """ candidate_str = """ <vr> <bgp> <bgpAs>65432</bgpAs> <setLogNbrChanges>true</setLogNbrChanges> <bgpAddressFamily> <afi>l2vpn</afi> <bgpSubAddressFamily> <safi>evpn</safi> <vrfAf> <vrfName>default</vrfName> </vrfAf> </bgpSubAddressFamily> </bgpAddressFamily> <bgpPeer> <peerAddr>10.0.2.129</peerAddr> <peerActivate>true</peerActivate> <sourceId>lo</sourceId> <peerDesc>foo-bar</peerDesc> <peerAs>65432</peerAs> <bgpPeerAddressFamily> <afi>l2vpn</afi> <bgpPeerSubAddressFamily> <safi>evpn</safi> <peerActivateAf>true</peerActivateAf> </bgpPeerSubAddressFamily> </bgpPeerAddressFamily> </bgpPeer> <bgpPeer> <peerAddr>10.0.2.129</peerAddr> <peerActivate>true</peerActivate> <sourceId>lo</sourceId> <peerDesc>foo-bar</peerDesc> <peerAs>65432</peerAs> <bgpPeerAddressFamily> <afi>l2vpn</afi> <bgpPeerSubAddressFamily> <safi>evpn</safi> <peerActivateAf>true</peerActivateAf> </bgpPeerSubAddressFamily> </bgpPeerAddressFamily> </bgpPeer> </bgp> </vr> """ running = etree.fromstring(running_str) candidate = etree.fromstring(candidate_str) block = Block(running, candidate) actual = block.get_printable_diff() self.assertTrue('[vr]' in actual) self.assertTrue(' <bgp>' in actual) self.assertTrue( ' <bgpAddressFamily>...</bgpAddressFamily>' in actual) self.assertTrue(' <bgpPeer>...</bgpPeer>' in actual) self.assertTrue(' <bgpAs>65432</bgpAs>' in actual) self.assertTrue( ' <setLogNbrChanges>true</setLogNbrChanges>' in actual) self.assertTrue('+ <bgpPeer>' in actual) self.assertTrue('+ <peerAddr>10.0.2.129</peerAddr>' in actual) self.assertTrue('+ <peerActivate>true</peerActivate>' in actual) self.assertTrue('+ <sourceId>lo</sourceId>' in actual) self.assertTrue('+ <peerDesc>foo-bar</peerDesc>' in actual) self.assertTrue('+ <peerAs>65432</peerAs>' in actual) self.assertTrue('+ <bgpPeerAddressFamily>' in actual) self.assertTrue('+ <afi>l2vpn</afi>' in actual) self.assertTrue('+ <bgpPeerSubAddressFamily>' in actual) self.assertTrue('+ <safi>evpn</safi>' in actual) self.assertTrue( '+ <peerActivateAf>true</peerActivateAf>' in actual) self.assertTrue('+ </bgpPeerSubAddressFamily>' in actual) self.assertTrue('+ </bgpPeer>' in actual) self.assertTrue(' </bgp>' in actual)