]>
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 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}
32 MgmtIP: fe80::200:ff:fe00:2
33 Capability: Bridge, off
34 Capability: Router, {router}
36 Capability: Station, {station}
38 PortID: mac 00:00:00:00:00:02
40 -------------------------------------------------------------------------------
42 out
= result
.stdout
.decode('ascii')
43 seconds
= re
.search(r
'^Interface: .*(\d\d)$',
45 re
.MULTILINE
).group(1)
46 router
= re
.search(r
'^ Capability: Router, (.*)$',
48 re
.MULTILINE
).group(1)
49 station
= re
.search(r
'^ Capability: Station, (.*)$',
51 re
.MULTILINE
).group(1)
52 out
= re
.sub(r
' *$', '', out
, flags
=re
.MULTILINE
)
53 assert out
== expected
.format(seconds
=seconds
,
59 @pytest.mark
.skipif('JSON' not in pytest
.config
.lldpcli
.outputs
,
60 reason
="JSON not supported")
61 def test_json_output(lldpd1
, lldpd
, lldpcli
, namespaces
, uname
):
65 result
= lldpcli("-f", "json", "show", "neighbors", "details")
66 assert result
.returncode
== 0
67 out
= result
.stdout
.decode('ascii')
70 eth0
= j
['lldp']['interface']['eth0']
72 del eth0
['chassis']['ns-2.example.com']['capability'][3]
73 del eth0
['chassis']['ns-2.example.com']['capability'][1]
75 "interface": {"eth0": {
82 "value": "00:00:00:00:00:02"
84 "descr": "Spectacular GNU/Linux 2016 {}".format(uname
),
85 "mgmt-ip": "fe80::200:ff:fe00:2",
87 {"type": "Bridge", "enabled": False},
88 {"type": "Wlan", "enabled": False},
95 "value": "00:00:00:00:00:02"
105 @pytest.mark
.skipif('XML' not in pytest
.config
.lldpcli
.outputs
,
106 reason
="XML not supported")
107 def test_xml_output(lldpd1
, lldpd
, lldpcli
, namespaces
, uname
):
111 result
= lldpcli("-f", "xml", "show", "neighbors", "details")
112 assert result
.returncode
== 0
113 out
= result
.stdout
.decode('ascii')
114 xml
= ET
.fromstring(out
)
116 age
= xml
.findall('./interface[1]')[0].attrib
['age']
117 router
= xml
.findall("./interface[1]/chassis/"
118 "capability[@type='Router']")[0].attrib
['enabled']
119 station
= xml
.findall("./interface[1]/chassis/"
120 "capability[@type='Station']")[0].attrib
['enabled']
121 expected
= ET
.fromstring("""<?xml version="1.0" encoding="UTF-8"?>
122 <lldp label="LLDP neighbors">
123 <interface label="Interface" name="eth0" via="LLDP" rid="1" age="{age}">
124 <chassis label="Chassis">
125 <id label="ChassisID" type="mac">00:00:00:00:00:02</id>
126 <name label="SysName">ns-2.example.com</name>
127 <descr label="SysDescr">Spectacular GNU/Linux 2016 {uname}</descr>
128 <mgmt-ip label="MgmtIP">fe80::200:ff:fe00:2</mgmt-ip>
129 <capability label="Capability" type="Bridge" enabled="off"/>
130 <capability label="Capability" type="Router" enabled="{router}"/>
131 <capability label="Capability" type="Wlan" enabled="off"/>
132 <capability label="Capability" type="Station" enabled="{station}"/>
135 <id label="PortID" type="mac">00:00:00:00:00:02</id>
136 <descr label="PortDescr">eth1</descr>
144 assert ET
.tostring(xml
) == ET
.tostring(expected
)
147 @pytest.mark
.skipif('Dot3' not in pytest
.config
.lldpd
.features
,
148 reason
="Dot3 not supported")
149 def test_configure_one_port(lldpd1
, lldpd
, lldpcli
, namespaces
, links
):
150 links(namespaces(1), namespaces(2))
153 result
= lldpcli(*("configure ports eth3 dot3 power "
154 "pse supported enabled paircontrol powerpairs "
155 "spare class class-3").split())
156 assert result
.returncode
== 0
159 out
= lldpcli("-f", "keyvalue", "show", "neighbors", "details")
160 assert out
['lldp.eth0.port.descr'] == 'eth1'
161 assert 'lldp.eth0.port.power.device-type' not in out
162 assert out
['lldp.eth2.port.descr'] == 'eth3'
163 assert out
['lldp.eth2.port.power.device-type'] == 'PSE'
166 @pytest.mark
.skipif('Dot3' not in pytest
.config
.lldpd
.features
,
167 reason
="Dot3 not supported")
168 def test_new_port_take_default(lldpd1
, lldpd
, lldpcli
, namespaces
, links
):
171 result
= lldpcli(*("configure dot3 power "
172 "pse supported enabled paircontrol powerpairs "
173 "spare class class-3").split())
174 assert result
.returncode
== 0
178 out
= lldpcli("-f", "keyvalue", "show", "neighbors", "details")
179 assert out
['lldp.eth0.port.descr'] == 'eth1'
180 assert out
['lldp.eth0.port.power.device-type'] == 'PSE'
181 links(namespaces(1), namespaces(2))
184 out
= lldpcli("-f", "keyvalue", "show", "neighbors", "details")
185 assert out
['lldp.eth2.port.descr'] == 'eth3'
186 assert out
['lldp.eth2.port.power.device-type'] == 'PSE'
189 @pytest.mark
.skipif('Dot3' not in pytest
.config
.lldpd
.features
,
190 reason
="Dot3 not supported")
191 def test_port_keep_configuration(lldpd1
, lldpd
, lldpcli
, namespaces
, links
):
192 links(namespaces(1), namespaces(2))
195 result
= lldpcli(*("configure ports eth3 dot3 power "
196 "pse supported enabled paircontrol powerpairs "
197 "spare class class-3").split())
198 assert result
.returncode
== 0
205 out
= lldpcli("-f", "keyvalue", "show", "neighbors", "details")
206 assert out
['lldp.eth2.port.descr'] == 'eth3'
207 assert out
['lldp.eth2.port.power.device-type'] == 'PSE'
210 def test_watch(lldpd1
, lldpd
, lldpcli
, namespaces
, links
):
214 result
= lldpcli("show", "neighbors")
215 assert result
.returncode
== 0
216 out
= result
.stdout
.decode('ascii')
217 assert "ns-2.example.com" in out
219 # Put a link down and immediately watch for a change
221 result
= lldpcli("watch", "limit", "1")
222 assert result
.returncode
== 0
223 expected
= out
.replace('LLDP neighbors:', 'LLDP neighbor deleted:')
224 expected
= re
.sub(r
', Time: 0 day, 00:.*$', '', expected
,
226 got
= result
.stdout
.decode('ascii')
227 got
= re
.sub(r
', Time: 0 day, 00:.*$', '', got
,
229 assert got
== expected
232 @pytest.mark
.skipif('XML' not in pytest
.config
.lldpcli
.outputs
,
233 reason
="XML not supported")
234 def test_watch_xml(lldpd1
, lldpd
, lldpcli
, namespaces
, links
):
238 result
= lldpcli("-f", "xml", "show", "neighbors")
239 assert result
.returncode
== 0
240 expected
= result
.stdexpected
.decode('ascii')
241 expected
= ET
.fromstring(expected
)
243 for x
in expected
.findall("./interface/chassis/name")] == \
246 # Put a link down and immediately watch for a change
248 result
= lldpcli("-f", "xml", "watch", "limit", "1")
249 assert result
.returncode
== 0
250 expected
.tag
= 'lldp-deleted'
251 expected
.set('label', 'LLDP neighbor deleted')
252 expected
.find('./interface').set('age', '')
253 got
= result
.stdout
.decode('ascii')
254 got
= ET
.fromstring(got
)
255 got
.find('./interface').set('age', '')
256 assert ET
.tostring(got
) == ET
.tostring(expected
)
259 @pytest.mark
.skipif('JSON' not in pytest
.config
.lldpcli
.outputs
,
260 reason
="JSON not supported")
261 def test_watch_json(lldpd1
, lldpd
, lldpcli
, namespaces
, links
):
265 result
= lldpcli("-f", "json", "show", "neighbors")
266 assert result
.returncode
== 0
267 expected
= result
.stdout
.decode('ascii')
268 expected
= json
.loads(expected
)
269 assert 'ns-2.example.com' in \
270 expected
['lldp']['interface']['eth0']['chassis']
272 # Put a link down and immediately watch for a change
274 result
= lldpcli("-f", "json", "watch", "limit", "1")
275 assert result
.returncode
== 0
276 got
= result
.stdout
.decode('ascii')
277 got
= json
.loads(got
)
278 expected
['lldp-deleted'] = expected
['lldp']
280 del expected
['lldp-deleted']['interface']['eth0']['age']
281 del got
['lldp-deleted']['interface']['eth0']['age']
282 assert got
== expected