Skip to content

Commit 5022501

Browse files
committed
Fix socketcan KeyError (#1599)
* use get * adapt test for issue #1598
1 parent 7eac6f7 commit 5022501

File tree

3 files changed

+94
-14
lines changed

3 files changed

+94
-14
lines changed

can/interfaces/socketcan/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def find_available_interfaces() -> List[str]:
6666
output_json,
6767
)
6868

69-
interfaces = [i["ifname"] for i in output_json if i["link_type"] == "can"]
69+
interfaces = [i["ifname"] for i in output_json if i.get("link_type") == "can"]
7070
return interfaces
7171

7272

test/data/ip_link_list.json

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
[
2+
{
3+
"ifindex": 1,
4+
"ifname": "lo",
5+
"flags": [
6+
"LOOPBACK",
7+
"UP",
8+
"LOWER_UP"
9+
],
10+
"mtu": 65536,
11+
"qdisc": "noqueue",
12+
"operstate": "UNKNOWN",
13+
"linkmode": "DEFAULT",
14+
"group": "default",
15+
"txqlen": 1000,
16+
"link_type": "loopback",
17+
"address": "00:00:00:00:00:00",
18+
"broadcast": "00:00:00:00:00:00"
19+
},
20+
{
21+
"ifindex": 2,
22+
"ifname": "eth0",
23+
"flags": [
24+
"NO-CARRIER",
25+
"BROADCAST",
26+
"MULTICAST",
27+
"UP"
28+
],
29+
"mtu": 1500,
30+
"qdisc": "fq_codel",
31+
"operstate": "DOWN",
32+
"linkmode": "DEFAULT",
33+
"group": "default",
34+
"txqlen": 1000,
35+
"link_type": "ether",
36+
"address": "11:22:33:44:55:66",
37+
"broadcast": "ff:ff:ff:ff:ff:ff"
38+
},
39+
{
40+
"ifindex": 3,
41+
"ifname": "wlan0",
42+
"flags": [
43+
"BROADCAST",
44+
"MULTICAST",
45+
"UP",
46+
"LOWER_UP"
47+
],
48+
"mtu": 1500,
49+
"qdisc": "noqueue",
50+
"operstate": "UP",
51+
"linkmode": "DORMANT",
52+
"group": "default",
53+
"txqlen": 1000,
54+
"link_type": "ether",
55+
"address": "11:22:33:44:55:66",
56+
"broadcast": "ff:ff:ff:ff:ff:ff"
57+
},
58+
{
59+
"ifindex": 48,
60+
"ifname": "vcan0",
61+
"flags": [
62+
"NOARP",
63+
"UP",
64+
"LOWER_UP"
65+
],
66+
"mtu": 72,
67+
"qdisc": "noqueue",
68+
"operstate": "UNKNOWN",
69+
"linkmode": "DEFAULT",
70+
"group": "default",
71+
"txqlen": 1000,
72+
"link_type": "can"
73+
},
74+
{
75+
"ifindex": 50,
76+
"ifname": "mycustomCan123",
77+
"flags": [
78+
"NOARP",
79+
"UP",
80+
"LOWER_UP"
81+
],
82+
"mtu": 72,
83+
"qdisc": "noqueue",
84+
"operstate": "UNKNOWN",
85+
"linkmode": "DEFAULT",
86+
"group": "default",
87+
"txqlen": 1000,
88+
"link_type": "can"
89+
},
90+
{}
91+
]

test/test_socketcan_helpers.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,8 @@
44
Tests helpers in `can.interfaces.socketcan.socketcan_common`.
55
"""
66

7-
import gzip
87
import unittest
9-
from base64 import b64decode
8+
from pathlib import Path
109
from unittest import mock
1110

1211
from can.interfaces.socketcan.utils import error_code_to_str, find_available_interfaces
@@ -42,17 +41,7 @@ def test_find_available_interfaces(self):
4241

4342
def test_find_available_interfaces_w_patch(self):
4443
# Contains lo, eth0, wlan0, vcan0, mycustomCan123
45-
ip_output_gz_b64 = (
46-
"H4sIAAAAAAAAA+2UzW+CMBjG7/wVhrNL+BC29IboEqNSwzQejDEViiMC5aNsmmX/+wpZTGUwDAcP"
47-
"y5qmh+d5++bN80u7EXpsfZRnsUTf8yMXn0TQk/u8GqEQM1EMiMjpXoAOGZM3F6mUZxAuhoY55UpL"
48-
"fbWoKjO4Hts7pl/kLdc+pDlrrmuaqnNq4vqZU8wSkSTHOeYHIjFOM4poOevKmlpwbfF+4EfHkLil"
49-
"PRo/G6vZkrcPKcnjwnOxh/KA8h49JQGOimAkSaq03NFz/B0PiffIOfIXkeumOCtiEiUJXG++bp8S"
50-
"5Dooo/WVZeFnvxmYUgsM01fpBmQWfDAN256M7SqioQ2NkWm8LKvGnIU3qTN+xylrV/FdaHrJzmFk"
51-
"gkacozuzZMnhtAGkLANFAaoKBgOgaUDXG0F6Hrje7SDVWpDvAYpuIdmJV4dn2cSx9VUuGiFCe25Y"
52-
"fwTi4KmW4ptzG0ULGvYPLN1APSqdMN3/82TRtOeqSbW5hmcnzygJTRTJivofcEvAgrAVvgD8aLkv"
53-
"/AcAAA=="
54-
)
55-
ip_output = gzip.decompress(b64decode(ip_output_gz_b64)).decode("ascii")
44+
ip_output = (Path(__file__).parent / "data" / "ip_link_list.json").read_text()
5645

5746
with mock.patch("subprocess.check_output") as check_output:
5847
check_output.return_value = ip_output

0 commit comments

Comments
 (0)