Skip to content

Commit a3c2451

Browse files
authored
Merge branch 'main' into 4534
2 parents 54ff38c + dfb45dc commit a3c2451

File tree

7 files changed

+103
-119
lines changed

7 files changed

+103
-119
lines changed

tests/framework/gitlint_rules.py

Lines changed: 58 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
# SPDX-License-Identifier: Apache-2.0
33
"""The user defined rules for gitlint."""
44

5+
import re
6+
57
from gitlint.rules import CommitRule, RuleViolation
68

79

@@ -22,129 +24,113 @@ class EndsSigned(CommitRule):
2224
def validate(self, commit):
2325
r"""Validates Signed-off-by and Co-authored-by tags as Linux's scripts/checkpatch.pl
2426
25-
>>> from gitlint.tests.base import BaseTestCase
27+
>>> from gitlint.git import GitContext
2628
>>> from gitlint.rules import RuleViolation
2729
...
2830
>>> ends_signed = EndsSigned()
31+
>>> miss_sob_follows_coab = "Missing 'Signed-off-by' following 'Co-authored-by'"
32+
>>> miss_sob = "'Signed-off-by' not found in commit message body"
33+
>>> non_sign = "Non 'Co-authored-by' or 'Signed-off-by' string found following 1st 'Signed-off-by'"
34+
>>> email_no_match = "'Co-authored-by' and 'Signed-off-by' name/email do not match"
2935
...
3036
>>> msg1 = (
3137
... f"Title\n\nMessage.\n\n"
3238
... f"Signed-off-by: name <email@domain>"
3339
... )
34-
>>> commit1 = BaseTestCase.gitcommit(msg1)
40+
>>> commit1 = GitContext.from_commit_msg(msg1).commits[0]
3541
>>> ends_signed.validate(commit1)
3642
[]
3743
>>> msg2 = (
3844
... f"Title\n\nMessage.\n\n"
3945
... f"Co-authored-by: name <email>\n\n"
4046
... f"Signed-off-by: name <email>"
4147
... )
42-
>>> commit2 = BaseTestCase.gitcommit(msg2)
48+
>>> commit2 = GitContext.from_commit_msg(msg2).commits[0]
4349
>>> ends_signed.validate(commit2)
4450
[]
45-
>>> msg3 = (
46-
... f"Title\n\nMessage.\n\n"
47-
... )
48-
>>> commit3 = BaseTestCase.gitcommit(msg3)
51+
>>> msg3 = f"Title\n\nMessage.\n\n"
52+
>>> commit3 = GitContext.from_commit_msg(msg3).commits[0]
4953
>>> vio3 = ends_signed.validate(commit3)
50-
>>> vio_msg3 = (
51-
... f"'Signed-off-by:' not found in commit message body"
52-
... )
53-
>>> vio3 == [RuleViolation("UC2", vio_msg3)]
54+
>>> vio3 == [RuleViolation("UC2", miss_sob)]
5455
True
5556
>>> msg4 = (
5657
... f"Title\n\nMessage.\n\n"
5758
... f"Signed-off-by: name <email@domain>\n\na sentence"
5859
... )
59-
>>> commit4 = BaseTestCase.gitcommit(msg4)
60+
>>> commit4 = GitContext.from_commit_msg(msg4).commits[0]
6061
>>> vio4 = ends_signed.validate(commit4)
61-
>>> vio_msg4 = (
62-
... f"Non 'Co-authored-by:' or 'Signed-off-by:' string found following 1st 'Signed-off-by:'"
63-
... )
64-
>>> vio4 == [RuleViolation("UC2", vio_msg4, None, 5)]
62+
>>> vio4 == [RuleViolation("UC2", non_sign, None, 6)]
6563
True
6664
>>> msg5 = (
6765
... f"Title\n\nMessage.\n\n"
6866
... f"Co-authored-by: name <email@domain>"
6967
... )
70-
>>> commit5 = BaseTestCase.gitcommit(msg5)
68+
>>> commit5 = GitContext.from_commit_msg(msg5).commits[0]
7169
>>> vio5 = ends_signed.validate(commit5)
72-
>>> vio_msg5 = (
73-
... f"Missing 'Signed-off-by:' following 'Co-authored-by:'"
74-
... )
75-
>>> vio5 == [RuleViolation("UC2", vio_msg5, None, 2)]
70+
>>> vio5 == [
71+
... RuleViolation("UC2", miss_sob, None, None),
72+
... RuleViolation("UC2", miss_sob_follows_coab, None, 5)
73+
... ]
7674
True
7775
>>> msg6 = (
7876
... f"Title\n\nMessage.\n\n"
7977
... f"Co-authored-by: name <email@domain>\n\n"
8078
... f"Signed-off-by: different name <email@domain>"
8179
... )
82-
>>> commit6 = BaseTestCase.gitcommit(msg6)
80+
>>> commit6 = GitContext.from_commit_msg(msg6).commits[0]
8381
>>> vio6 = ends_signed.validate(commit6)
84-
>>> vio_msg6 = (
85-
... f"'Co-authored-by:' and 'Signed-off-by:' name/email do not match"
86-
... )
87-
>>> vio6 == [RuleViolation("UC2", vio_msg6, None, 6)]
82+
>>> vio6 == [RuleViolation("UC2", email_no_match, None, 6)]
8883
True
8984
"""
9085

9186
violations = []
9287

9388
# Utilities
9489
def vln(stmt, i):
95-
return RuleViolation(self.id, stmt, None, i)
96-
97-
co_auth = "Co-authored-by:"
98-
sig = "Signed-off-by:"
90+
violations.append(RuleViolation(self.id, stmt, None, i))
9991

100-
message_iter = enumerate(commit.message.original.split("\n"))
92+
coab = "Co-authored-by"
93+
sob = "Signed-off-by"
10194

102-
# Skip ahead to the first signoff or co-author tag
103-
104-
# Checks commit message contains a `Signed-off-by` string
105-
for i, line in message_iter:
106-
if line.startswith(sig) or line.startswith(co_auth):
107-
break
108-
else:
109-
# No signature was found in the message (before `message_iter` ended)
110-
# This check here can have false-negatives (e.g. if the body ends with only
111-
# a 'Co-authored-by' tag), but then below will realize that the co-authored-by
112-
# tag isnt followed by a Signed-off-by tag and fail (and also the DCO check will
113-
# complain).
114-
violations.append(vln(f"'{sig}' not found in commit message body", None))
115-
116-
# Check that from here on out we only have signatures and co-authors, and that
117-
# every co-author is immediately followed by a signature with the same name/email.
118-
for i, line in message_iter:
119-
if line.startswith(co_auth):
120-
try:
121-
_, next_line = next(message_iter)
122-
except StopIteration:
123-
violations.append(
124-
vln(f"Missing '{sig}' tag following '{co_auth}'", i)
125-
)
126-
else:
127-
if not next_line.startswith(sig):
128-
violations.append(
129-
vln(f"Missing '{sig}' tag following '{co_auth}'", i + 1)
130-
)
131-
continue
132-
133-
if next_line.split(":")[1].strip() != line.split(":")[1].strip():
134-
violations.append(
135-
vln(f"{co_auth} and {sig} name/email do not match", i + 1)
136-
)
137-
continue
138-
139-
if line.startswith(sig) or not line.strip():
95+
# find trailers
96+
trailers = []
97+
for i, line in enumerate(commit.message.original.splitlines()):
98+
# ignore empty lines
99+
if not line:
140100
continue
101+
match = re.match(r"([\w-]+):\s+(.*)", line)
102+
if match:
103+
key, val = match.groups()
104+
trailers.append((i, key, val))
105+
else:
106+
trailers.append((i, "line", line))
107+
# artificial line so we can check any "previous line" rules
108+
trailers.append((trailers[-1][0] + 1, None, None))
141109

142-
violations.append(
110+
# Checks commit message contains a `Signed-off-by` string
111+
if not [x for x in trailers if x[1] == sob]:
112+
vln(f"'{sob}' not found in commit message body", None)
113+
114+
prev_trailer, prev_value = None, None
115+
sig_trailers = False
116+
for i, trailer, value in trailers:
117+
if trailer in {sob, coab}:
118+
sig_trailers = True
119+
elif trailer not in {sob, coab, None} and sig_trailers:
143120
vln(
144-
f"Non '{co_auth}' or '{sig}' string found following 1st '{sig}'",
121+
f"Non '{coab}' or '{sob}' string found following 1st '{sob}'",
145122
i,
146123
)
147-
)
124+
# Every co-author is immediately followed by a signature
125+
if prev_trailer == coab:
126+
if trailer != sob:
127+
vln(f"Missing '{sob}' following '{coab}'", i)
128+
else:
129+
# with the same name/email.
130+
if value != prev_value:
131+
vln(f"'{coab}' and '{sob}' name/email do not match", i)
132+
133+
prev_trailer, prev_value = trailer, value
148134

149135
# Return errors
150136
return violations

tests/framework/properties.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def get_os_version():
2626
"""Get the OS version
2727
2828
>>> get_os_version()
29-
Ubuntu 18.04.6 LTS
29+
'Ubuntu 24.04.1 LTS'
3030
"""
3131

3232
os_release = Path("/etc/os-release").read_text(encoding="ascii")
@@ -41,7 +41,7 @@ def get_host_os(kv: str = None):
4141
This only works for AL2 and AL2023
4242
4343
>>> get_host_os("6.1.41-63.118.amzn2023.x86_64")
44-
amzn2023
44+
'amzn2023'
4545
"""
4646
if kv is None:
4747
kv = platform.release()

tests/framework/utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,8 +660,8 @@ class Timeout:
660660
"""
661661
A Context Manager to timeout sections of code.
662662
663-
>>> with Timeout(30):
664-
>>> time.sleep(35)
663+
>>> with Timeout(30): # doctest: +SKIP
664+
... time.sleep(35) # doctest: +SKIP
665665
"""
666666

667667
def __init__(self, seconds, msg="Timed out"):

tests/framework/utils_imdsv2.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ class IMDSv2Client:
2424
"""
2525
A simple IMDSv2 client.
2626
27-
>>> IMDSv2Client().get("/meta-data/instance-type")
27+
>>> IMDSv2Client().get("/meta-data/instance-type") # doctest: +SKIP
28+
...
2829
"""
2930

3031
def __init__(self, endpoint="http://169.254.169.254", version="latest"):
@@ -49,8 +50,8 @@ def get(self, path):
4950
"""
5051
Get a metadata path from IMDSv2
5152
52-
>>> IMDSv2Client().get("/meta-data/instance-type")
53-
>>> m5d.metal
53+
>>> IMDSv2Client().get("/meta-data/instance-type") # doctest: +SKIP
54+
'm5d.metal'
5455
"""
5556
headers = {IMDSV2_HDR_TOKEN: self.get_token()}
5657
url = f"{self.endpoint}/{self.version}{path}"

tests/host_tools/udp_offload.py

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,37 @@ def eprint(*args, **kwargs):
2424
SOL_UDP = 17 # Protocol number for UDP
2525
UDP_SEGMENT = 103 # Option code for UDP segmentation (non-standard)
2626

27-
# Get the IP and port from command-line arguments
28-
if len(sys.argv) != 3:
29-
eprint("Usage: python3 udp_offload.py <ip_address> <port>")
30-
sys.exit(1)
3127

32-
ip_address = sys.argv[1]
33-
port = int(sys.argv[2])
34-
35-
# Create a UDP socket
36-
sockfd = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
37-
38-
# Set the UDP segmentation option (UDP_SEGMENT) to 1400 bytes
39-
OPTVAL = 1400
40-
try:
41-
sockfd.setsockopt(SOL_UDP, UDP_SEGMENT, OPTVAL)
42-
except (AttributeError, PermissionError):
43-
eprint("Unable to set UDP_SEGMENT option")
44-
sys.exit(1)
45-
46-
# Set the destination address and port
47-
servaddr = (ip_address, port)
48-
49-
# Send the message to the destination address
50-
MESSAGE = b"x"
51-
try:
52-
sockfd.sendto(MESSAGE, servaddr)
53-
print("Message sent successfully")
54-
except socket.error as e:
55-
eprint(f"Error sending message: {e}")
56-
sys.exit(1)
57-
58-
sockfd.close()
28+
if __name__ == "__main__":
29+
# Get the IP and port from command-line arguments
30+
if len(sys.argv) != 3:
31+
eprint("Usage: python3 udp_offload.py <ip_address> <port>")
32+
sys.exit(1)
33+
34+
ip_address = sys.argv[1]
35+
port = int(sys.argv[2])
36+
37+
# Create a UDP socket
38+
sockfd = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
39+
40+
# Set the UDP segmentation option (UDP_SEGMENT) to 1400 bytes
41+
OPTVAL = 1400
42+
try:
43+
sockfd.setsockopt(SOL_UDP, UDP_SEGMENT, OPTVAL)
44+
except (AttributeError, PermissionError):
45+
eprint("Unable to set UDP_SEGMENT option")
46+
sys.exit(1)
47+
48+
# Set the destination address and port
49+
servaddr = (ip_address, port)
50+
51+
# Send the message to the destination address
52+
MESSAGE = b"x"
53+
try:
54+
sockfd.sendto(MESSAGE, servaddr)
55+
print("Message sent successfully")
56+
except socket.error as e:
57+
eprint(f"Error sending message: {e}")
58+
sys.exit(1)
59+
60+
sockfd.close()

tests/integration_tests/style/test_rust.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@
66

77

88
def test_rust_order():
9-
"""
10-
Tests that `Cargo.toml` dependencies are alphabetically ordered.
11-
12-
@type: style
13-
"""
9+
"""Tests that `Cargo.toml` dependencies are alphabetically ordered."""
1410

1511
# Runs `cargo-sort` with the current working directory (`cwd`) as the repository root.
1612
_, _, _ = utils.check_output(
@@ -19,9 +15,7 @@ def test_rust_order():
1915

2016

2117
def test_rust_style():
22-
"""
23-
Test that rust code passes style checks.
24-
"""
18+
"""Test that rust code passes style checks."""
2519

2620
# ../src/io_uring/src/bindings.rs
2721
config = open("fmt.toml", encoding="utf-8").read().replace("\n", ",")

tools/devtool

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,8 @@ cmd_mkdocs() {
934934
}
935935

936936
cmd_checkstyle() {
937-
cmd_test --no-build --no-kvm-check -- integration_tests/style -n 4 --dist worksteal
937+
cmd_test --no-build --no-kvm-check -- -n 4 --dist worksteal integration_tests/style
938+
cmd_test --no-build --no-kvm-check -- -n 4 --doctest-modules framework
938939
}
939940

940941
# Check if able to run firecracker.

0 commit comments

Comments
 (0)