]>
git.ipfire.org Git - thirdparty/lldpd.git/blob - tests/integration/test_lldpcli.py
6 import xml
.etree
.ElementTree
as ET
9 @pytest.fixture(scope
='session')
11 return "{} {} {} {}".format(
18 def test_text_output(lldpd1
, lldpd
, lldpcli
, namespaces
, uname
):
22 result
= lldpcli("show", "neighbors", "details")
23 assert result
.returncode
== 0
24 if 'Dot3' in pytest
.config
.lldpd
.features
:
26 PMD autoneg: supported: no, enabled: no
27 MAU oper type: 10GigBaseCX4 - X copper over 8 pair 100-Ohm balanced cable"""
30 expected
= """-------------------------------------------------------------------------------
32 -------------------------------------------------------------------------------
33 Interface: eth0, via: LLDP, RID: 1, Time: 0 day, 00:00:{seconds}
35 ChassisID: mac 00:00:00:00:00:02
36 SysName: ns-2.example.com
37 SysDescr: Spectacular GNU/Linux 2016 {uname}
39 MgmtIP: fe80::200:ff:fe00:2
40 Capability: Bridge, off
41 Capability: Router, {router}
43 Capability: Station, {station}
45 PortID: mac 00:00:00:00:00:02
47 -------------------------------------------------------------------------------
49 out
= result
.stdout
.decode('ascii')
50 seconds
= re
.search(r
'^Interface: .*(\d\d)$',
52 re
.MULTILINE
).group(1)
53 router
= re
.search(r
'^ Capability: Router, (.*)$',
55 re
.MULTILINE
).group(1)
56 station
= re
.search(r
'^ Capability: Station, (.*)$',
58 re
.MULTILINE
).group(1)
59 out
= re
.sub(r
' *$', '', out
, flags
=re
.MULTILINE
)
60 assert out
== expected
.format(seconds
=seconds
,
67 @pytest.mark
.skipif('JSON' not in pytest
.config
.lldpcli
.outputs
,
68 reason
="JSON not supported")
69 def test_json_output(lldpd1
, lldpd
, lldpcli
, namespaces
, uname
):
73 result
= lldpcli("-f", "json", "show", "neighbors", "details")
74 assert result
.returncode
== 0
75 out
= result
.stdout
.decode('ascii')
78 eth0
= j
['lldp']['interface']['eth0']
80 del eth0
['chassis']['ns-2.example.com']['capability'][3]
81 del eth0
['chassis']['ns-2.example.com']['capability'][1]
83 "interface": {"eth0": {
90 "value": "00:00:00:00:00:02"
92 "descr": "Spectacular GNU/Linux 2016 {}".format(uname
),
94 "mgmt-ip": "fe80::200:ff:fe00:2",
96 {"type": "Bridge", "enabled": False},
97 {"type": "Wlan", "enabled": False},
104 "value": "00:00:00:00:00:02"
111 if 'Dot3' in pytest
.config
.lldpd
.features
:
112 expected
['lldp']['interface']['eth0']['port']['auto-negotiation'] = {
115 "current": "10GigBaseCX4 - X copper over 8 pair 100-Ohm balanced cable"
120 @pytest.mark
.skipif('XML' not in pytest
.config
.lldpcli
.outputs
,
121 reason
="XML not supported")
122 def test_xml_output(lldpd1
, lldpd
, lldpcli
, namespaces
, uname
):
126 result
= lldpcli("-f", "xml", "show", "neighbors", "details")
127 assert result
.returncode
== 0
128 out
= result
.stdout
.decode('ascii')
129 xml
= ET
.fromstring(out
)
131 age
= xml
.findall('./interface[1]')[0].attrib
['age']
132 router
= xml
.findall("./interface[1]/chassis/"
133 "capability[@type='Router']")[0].attrib
['enabled']
134 station
= xml
.findall("./interface[1]/chassis/"
135 "capability[@type='Station']")[0].attrib
['enabled']
136 if 'Dot3' in pytest
.config
.lldpd
.features
:
138 <auto-negotiation enabled="no" label="PMD autoneg" supported="no">
139 <current label="MAU oper type">10GigBaseCX4 - X copper over 8 pair 100-Ohm balanced cable</current>
140 </auto-negotiation>"""
143 expected
= ET
.fromstring("""<?xml version="1.0" encoding="UTF-8"?>
144 <lldp label="LLDP neighbors">
145 <interface label="Interface" name="eth0" via="LLDP" rid="1" age="{age}">
146 <chassis label="Chassis">
147 <id label="ChassisID" type="mac">00:00:00:00:00:02</id>
148 <name label="SysName">ns-2.example.com</name>
149 <descr label="SysDescr">Spectacular GNU/Linux 2016 {uname}</descr>
150 <ttl label="TTL">120</ttl>
151 <mgmt-ip label="MgmtIP">fe80::200:ff:fe00:2</mgmt-ip>
152 <capability label="Capability" type="Bridge" enabled="off"/>
153 <capability label="Capability" type="Router" enabled="{router}"/>
154 <capability label="Capability" type="Wlan" enabled="off"/>
155 <capability label="Capability" type="Station" enabled="{station}"/>
158 <id label="PortID" type="mac">00:00:00:00:00:02</id>
159 <descr label="PortDescr">eth1</descr>{dot3}
168 assert ET
.tostring(xml
) == ET
.tostring(expected
)
171 @pytest.mark
.skipif('Dot3' not in pytest
.config
.lldpd
.features
,
172 reason
="Dot3 not supported")
173 def test_configure_one_port(lldpd1
, lldpd
, lldpcli
, namespaces
, links
):
174 links(namespaces(1), namespaces(2))
177 result
= lldpcli(*("configure ports eth3 dot3 power "
178 "pse supported enabled paircontrol powerpairs "
179 "spare class class-3").split())
180 assert result
.returncode
== 0
183 out
= lldpcli("-f", "keyvalue", "show", "neighbors", "details")
184 assert out
['lldp.eth0.port.descr'] == 'eth1'
185 assert 'lldp.eth0.port.power.device-type' not in out
186 assert out
['lldp.eth2.port.descr'] == 'eth3'
187 assert out
['lldp.eth2.port.power.device-type'] == 'PSE'
190 @pytest.mark
.skipif('Dot3' not in pytest
.config
.lldpd
.features
,
191 reason
="Dot3 not supported")
192 def test_new_port_take_default(lldpd1
, lldpd
, lldpcli
, namespaces
, links
):
195 result
= lldpcli(*("configure dot3 power "
196 "pse supported enabled paircontrol powerpairs "
197 "spare class class-3").split())
198 assert result
.returncode
== 0
202 out
= lldpcli("-f", "keyvalue", "show", "neighbors", "details")
203 assert out
['lldp.eth0.port.descr'] == 'eth1'
204 assert out
['lldp.eth0.port.power.device-type'] == 'PSE'
205 links(namespaces(1), namespaces(2))
208 out
= lldpcli("-f", "keyvalue", "show", "neighbors", "details")
209 assert out
['lldp.eth2.port.descr'] == 'eth3'
210 assert out
['lldp.eth2.port.power.device-type'] == 'PSE'
213 @pytest.mark
.skipif('Dot3' not in pytest
.config
.lldpd
.features
,
214 reason
="Dot3 not supported")
215 def test_port_keep_configuration(lldpd1
, lldpd
, lldpcli
, namespaces
, links
):
216 links(namespaces(1), namespaces(2))
219 result
= lldpcli(*("configure ports eth3 dot3 power "
220 "pse supported enabled paircontrol powerpairs "
221 "spare class class-3").split())
222 assert result
.returncode
== 0
229 out
= lldpcli("-f", "keyvalue", "show", "neighbors", "details")
230 assert out
['lldp.eth2.port.descr'] == 'eth3'
231 assert out
['lldp.eth2.port.power.device-type'] == 'PSE'
234 def test_watch(lldpd1
, lldpd
, lldpcli
, namespaces
, links
):
238 result
= lldpcli("show", "neighbors")
239 assert result
.returncode
== 0
240 out
= result
.stdout
.decode('ascii')
241 assert "ns-2.example.com" in out
243 # Put a link down and immediately watch for a change
245 result
= lldpcli("watch", "limit", "1")
246 assert result
.returncode
== 0
247 expected
= out
.replace('LLDP neighbors:', 'LLDP neighbor deleted:')
248 expected
= re
.sub(r
', Time: 0 day, 00:.*$', '', expected
,
250 got
= result
.stdout
.decode('ascii')
251 got
= re
.sub(r
', Time: 0 day, 00:.*$', '', got
,
253 assert got
== expected
256 @pytest.mark
.skipif('XML' not in pytest
.config
.lldpcli
.outputs
,
257 reason
="XML not supported")
258 def test_watch_xml(lldpd1
, lldpd
, lldpcli
, namespaces
, links
):
262 result
= lldpcli("-f", "xml", "show", "neighbors")
263 assert result
.returncode
== 0
264 expected
= result
.stdout
.decode('ascii')
265 expected
= ET
.fromstring(expected
)
267 for x
in expected
.findall("./interface/chassis/name")] == \
270 # Put a link down and immediately watch for a change
272 result
= lldpcli("-f", "xml", "watch", "limit", "1")
273 assert result
.returncode
== 0
274 expected
.tag
= 'lldp-deleted'
275 expected
.set('label', 'LLDP neighbor deleted')
276 expected
.find('./interface').set('age', '')
277 got
= result
.stdout
.decode('ascii')
278 got
= ET
.fromstring(got
)
279 got
.find('./interface').set('age', '')
280 assert ET
.tostring(got
) == ET
.tostring(expected
)
283 @pytest.mark
.skipif('JSON' not in pytest
.config
.lldpcli
.outputs
,
284 reason
="JSON not supported")
285 def test_watch_json(lldpd1
, lldpd
, lldpcli
, namespaces
, links
):
289 result
= lldpcli("-f", "json", "show", "neighbors")
290 assert result
.returncode
== 0
291 expected
= result
.stdout
.decode('ascii')
292 expected
= json
.loads(expected
)
293 assert 'ns-2.example.com' in \
294 expected
['lldp']['interface']['eth0']['chassis']
296 # Put a link down and immediately watch for a change
298 result
= lldpcli("-f", "json", "watch", "limit", "1")
299 assert result
.returncode
== 0
300 got
= result
.stdout
.decode('ascii')
301 got
= json
.loads(got
)
302 expected
['lldp-deleted'] = expected
['lldp']
304 del expected
['lldp-deleted']['interface']['eth0']['age']
305 del got
['lldp-deleted']['interface']['eth0']['age']
306 assert got
== expected
309 def test_return_code(lldpd1
, lldpcli
, namespaces
):
311 result
= lldpcli("show", "neighbors")
312 assert result
.returncode
== 0
313 result
= lldpcli("unknown", "command")
314 assert result
.returncode
== 1