]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
tests: add test of `lldpcli watch`
authorVincent Bernat <vincent@bernat.im>
Fri, 20 May 2016 18:13:07 +0000 (20:13 +0200)
committerVincent Bernat <vincent@bernat.im>
Fri, 20 May 2016 18:13:07 +0000 (20:13 +0200)
NEWS
tests/integration/test_lldpcli.py

diff --git a/NEWS b/NEWS
index ee2fa0d74997290e3fd0a3c9b69102a70c8b4d22..c6f83fae94a80f6a1d377a161ae7fc4cc4e593b0 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,7 @@ lldpd (0.9.3)
   * Change:
     + Do not rely on support of constructors for liblldpctl.
     + Always log to stderr (even in addition to syslog).
+    + `lldpcli watch` accepts a limit on the number of received events.
   * Fix:
     + `lldpcli -f {xml,json} watch` should work now.
     + Consider `veth` interfaces as physical interfaces.
index 32aa62309a8e1e059f96d35925220592b7b3ebe4..521cc6fdc57ad5142ac424f3aa59cdc805caa7e9 100644 (file)
@@ -205,3 +205,78 @@ def test_port_keep_configuration(lldpd1, lldpd, lldpcli, namespaces, links):
         out = lldpcli("-f", "keyvalue", "show", "neighbors", "details")
         assert out['lldp.eth2.port.descr'] == 'eth3'
         assert out['lldp.eth2.port.power.device-type'] == 'PSE'
+
+
+def test_watch(lldpd1, lldpd, lldpcli, namespaces, links):
+    with namespaces(2):
+        lldpd()
+    with namespaces(1):
+        result = lldpcli("show", "neighbors")
+        assert result.returncode == 0
+        out = result.stdout.decode('ascii')
+        assert "ns-2.example.com" in out
+
+        # Put a link down and immediately watch for a change
+        links.down('eth0')
+        result = lldpcli("watch", "limit", "1")
+        assert result.returncode == 0
+        expected = out.replace('LLDP neighbors:', 'LLDP neighbor deleted:')
+        expected = re.sub(r', Time: 0 day, 00:.*$', '', expected,
+                          flags=re.MULTILINE)
+        got = result.stdout.decode('ascii')
+        got = re.sub(r', Time: 0 day, 00:.*$', '', got,
+                     flags=re.MULTILINE)
+        assert got == expected
+
+
+@pytest.mark.skipif('XML' not in pytest.config.lldpcli.outputs,
+                    reason="XML not supported")
+def test_watch_xml(lldpd1, lldpd, lldpcli, namespaces, links):
+    with namespaces(2):
+        lldpd()
+    with namespaces(1):
+        result = lldpcli("-f", "xml", "show", "neighbors")
+        assert result.returncode == 0
+        expected = result.stdexpected.decode('ascii')
+        expected = ET.fromstring(expected)
+        assert [x.text
+                for x in expected.findall("./interface/chassis/name")] == \
+            ["ns-2.example.com"]
+
+        # Put a link down and immediately watch for a change
+        links.down('eth0')
+        result = lldpcli("-f", "xml", "watch", "limit", "1")
+        assert result.returncode == 0
+        expected.tag = 'lldp-deleted'
+        expected.set('label', 'LLDP neighbor deleted')
+        expected.find('./interface').set('age', '')
+        got = result.stdout.decode('ascii')
+        got = ET.fromstring(got)
+        got.find('./interface').set('age', '')
+        assert ET.tostring(got) == ET.tostring(expected)
+
+
+@pytest.mark.skipif('JSON' not in pytest.config.lldpcli.outputs,
+                    reason="JSON not supported")
+def test_watch_json(lldpd1, lldpd, lldpcli, namespaces, links):
+    with namespaces(2):
+        lldpd()
+    with namespaces(1):
+        result = lldpcli("-f", "json", "show", "neighbors")
+        assert result.returncode == 0
+        expected = result.stdout.decode('ascii')
+        expected = json.loads(expected)
+        assert 'ns-2.example.com' in \
+            expected['lldp']['interface']['eth0']['chassis']
+
+        # Put a link down and immediately watch for a change
+        links.down('eth0')
+        result = lldpcli("-f", "json", "watch", "limit", "1")
+        assert result.returncode == 0
+        got = result.stdout.decode('ascii')
+        got = json.loads(got)
+        expected['lldp-deleted'] = expected['lldp']
+        del expected['lldp']
+        del expected['lldp-deleted']['interface']['eth0']['age']
+        del got['lldp-deleted']['interface']['eth0']['age']
+        assert got == expected