]>
git.ipfire.org Git - thirdparty/lldpd.git/blob - tests/integration/test_lldpcli.py
e224e448b10f5b9dd35725669cd8945c9fad7806
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 expected
= """-------------------------------------------------------------------------------
26 -------------------------------------------------------------------------------
27 Interface: eth0, via: LLDP, RID: 1, Time: 0 day, 00:00:{seconds}
29 ChassisID: mac 00:00:00:00:00:02
30 SysName: ns-2.example.com
31 SysDescr: Spectacular GNU/Linux 2016 {uname}
33 MgmtIP: fe80::200:ff:fe00:2
34 Capability: Bridge, off
35 Capability: Router, {router}
37 Capability: Station, {station}
39 PortID: mac 00:00:00:00:00:02
41 -------------------------------------------------------------------------------
43 out
= result
.stdout
.decode('ascii')
44 seconds
= re
.search(r
'^Interface: .*(\d\d)$',
46 re
.MULTILINE
).group(1)
47 router
= re
.search(r
'^ Capability: Router, (.*)$',
49 re
.MULTILINE
).group(1)
50 station
= re
.search(r
'^ Capability: Station, (.*)$',
52 re
.MULTILINE
).group(1)
53 out
= re
.sub(r
' *$', '', out
, flags
=re
.MULTILINE
)
54 assert out
== expected
.format(seconds
=seconds
,
60 @pytest.mark
.skipif('JSON' not in pytest
.config
.lldpcli
.outputs
,
61 reason
="JSON not supported")
62 def test_json_output(lldpd1
, lldpd
, lldpcli
, namespaces
, uname
):
66 result
= lldpcli("-f", "json", "show", "neighbors", "details")
67 assert result
.returncode
== 0
68 out
= result
.stdout
.decode('ascii')
71 eth0
= j
['lldp']['interface']['eth0']
73 del eth0
['chassis']['ns-2.example.com']['capability'][3]
74 del eth0
['chassis']['ns-2.example.com']['capability'][1]
76 "interface": {"eth0": {
83 "value": "00:00:00:00:00:02"
85 "descr": "Spectacular GNU/Linux 2016 {}".format(uname
),
87 "mgmt-ip": "fe80::200:ff:fe00:2",
89 {"type": "Bridge", "enabled": False},
90 {"type": "Wlan", "enabled": False},
97 "value": "00:00:00:00:00:02"
107 @pytest.mark
.skipif('XML' not in pytest
.config
.lldpcli
.outputs
,
108 reason
="XML not supported")
109 def test_xml_output(lldpd1
, lldpd
, lldpcli
, namespaces
, uname
):
113 result
= lldpcli("-f", "xml", "show", "neighbors", "details")
114 assert result
.returncode
== 0
115 out
= result
.stdout
.decode('ascii')
116 xml
= ET
.fromstring(out
)
118 age
= xml
.findall('./interface[1]')[0].attrib
['age']
119 router
= xml
.findall("./interface[1]/chassis/"
120 "capability[@type='Router']")[0].attrib
['enabled']
121 station
= xml
.findall("./interface[1]/chassis/"
122 "capability[@type='Station']")[0].attrib
['enabled']
123 expected
= ET
.fromstring("""<?xml version="1.0" encoding="UTF-8"?>
124 <lldp label="LLDP neighbors">
125 <interface label="Interface" name="eth0" via="LLDP" rid="1" age="{age}">
126 <chassis label="Chassis">
127 <id label="ChassisID" type="mac">00:00:00:00:00:02</id>
128 <name label="SysName">ns-2.example.com</name>
129 <descr label="SysDescr">Spectacular GNU/Linux 2016 {uname}</descr>
130 <ttl label="TTL">120</ttl>
131 <mgmt-ip label="MgmtIP">fe80::200:ff:fe00:2</mgmt-ip>
132 <capability label="Capability" type="Bridge" enabled="off"/>
133 <capability label="Capability" type="Router" enabled="{router}"/>
134 <capability label="Capability" type="Wlan" enabled="off"/>
135 <capability label="Capability" type="Station" enabled="{station}"/>
138 <id label="PortID" type="mac">00:00:00:00:00:02</id>
139 <descr label="PortDescr">eth1</descr>
147 assert ET
.tostring(xml
) == ET
.tostring(expected
)
150 @pytest.mark
.skipif('Dot3' not in pytest
.config
.lldpd
.features
,
151 reason
="Dot3 not supported")
152 def test_configure_one_port(lldpd1
, lldpd
, lldpcli
, namespaces
, links
):
153 links(namespaces(1), namespaces(2))
156 result
= lldpcli(*("configure ports eth3 dot3 power "
157 "pse supported enabled paircontrol powerpairs "
158 "spare class class-3").split())
159 assert result
.returncode
== 0
162 out
= lldpcli("-f", "keyvalue", "show", "neighbors", "details")
163 assert out
['lldp.eth0.port.descr'] == 'eth1'
164 assert 'lldp.eth0.port.power.device-type' not in out
165 assert out
['lldp.eth2.port.descr'] == 'eth3'
166 assert out
['lldp.eth2.port.power.device-type'] == 'PSE'
169 @pytest.mark
.skipif('Dot3' not in pytest
.config
.lldpd
.features
,
170 reason
="Dot3 not supported")
171 def test_new_port_take_default(lldpd1
, lldpd
, lldpcli
, namespaces
, links
):
174 result
= lldpcli(*("configure dot3 power "
175 "pse supported enabled paircontrol powerpairs "
176 "spare class class-3").split())
177 assert result
.returncode
== 0
181 out
= lldpcli("-f", "keyvalue", "show", "neighbors", "details")
182 assert out
['lldp.eth0.port.descr'] == 'eth1'
183 assert out
['lldp.eth0.port.power.device-type'] == 'PSE'
184 links(namespaces(1), namespaces(2))
187 out
= lldpcli("-f", "keyvalue", "show", "neighbors", "details")
188 assert out
['lldp.eth2.port.descr'] == 'eth3'
189 assert out
['lldp.eth2.port.power.device-type'] == 'PSE'
192 @pytest.mark
.skipif('Dot3' not in pytest
.config
.lldpd
.features
,
193 reason
="Dot3 not supported")
194 def test_port_keep_configuration(lldpd1
, lldpd
, lldpcli
, namespaces
, links
):
195 links(namespaces(1), namespaces(2))
198 result
= lldpcli(*("configure ports eth3 dot3 power "
199 "pse supported enabled paircontrol powerpairs "
200 "spare class class-3").split())
201 assert result
.returncode
== 0
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 def test_watch(lldpd1
, lldpd
, lldpcli
, namespaces
, links
):
217 result
= lldpcli("show", "neighbors")
218 assert result
.returncode
== 0
219 out
= result
.stdout
.decode('ascii')
220 assert "ns-2.example.com" in out
222 # Put a link down and immediately watch for a change
224 result
= lldpcli("watch", "limit", "1")
225 assert result
.returncode
== 0
226 expected
= out
.replace('LLDP neighbors:', 'LLDP neighbor deleted:')
227 expected
= re
.sub(r
', Time: 0 day, 00:.*$', '', expected
,
229 got
= result
.stdout
.decode('ascii')
230 got
= re
.sub(r
', Time: 0 day, 00:.*$', '', got
,
232 assert got
== expected
235 @pytest.mark
.skipif('XML' not in pytest
.config
.lldpcli
.outputs
,
236 reason
="XML not supported")
237 def test_watch_xml(lldpd1
, lldpd
, lldpcli
, namespaces
, links
):
241 result
= lldpcli("-f", "xml", "show", "neighbors")
242 assert result
.returncode
== 0
243 expected
= result
.stdout
.decode('ascii')
244 expected
= ET
.fromstring(expected
)
246 for x
in expected
.findall("./interface/chassis/name")] == \
249 # Put a link down and immediately watch for a change
251 result
= lldpcli("-f", "xml", "watch", "limit", "1")
252 assert result
.returncode
== 0
253 expected
.tag
= 'lldp-deleted'
254 expected
.set('label', 'LLDP neighbor deleted')
255 expected
.find('./interface').set('age', '')
256 got
= result
.stdout
.decode('ascii')
257 got
= ET
.fromstring(got
)
258 got
.find('./interface').set('age', '')
259 assert ET
.tostring(got
) == ET
.tostring(expected
)
262 @pytest.mark
.skipif('JSON' not in pytest
.config
.lldpcli
.outputs
,
263 reason
="JSON not supported")
264 def test_watch_json(lldpd1
, lldpd
, lldpcli
, namespaces
, links
):
268 result
= lldpcli("-f", "json", "show", "neighbors")
269 assert result
.returncode
== 0
270 expected
= result
.stdout
.decode('ascii')
271 expected
= json
.loads(expected
)
272 assert 'ns-2.example.com' in \
273 expected
['lldp']['interface']['eth0']['chassis']
275 # Put a link down and immediately watch for a change
277 result
= lldpcli("-f", "json", "watch", "limit", "1")
278 assert result
.returncode
== 0
279 got
= result
.stdout
.decode('ascii')
280 got
= json
.loads(got
)
281 expected
['lldp-deleted'] = expected
['lldp']
283 del expected
['lldp-deleted']['interface']['eth0']['age']
284 del got
['lldp-deleted']['interface']['eth0']['age']
285 assert got
== expected
288 def test_return_code(lldpd1
, lldpcli
, namespaces
):
290 result
= lldpcli("show", "neighbors")
291 assert result
.returncode
== 0
292 result
= lldpcli("unknown", "command")
293 assert result
.returncode
== 1