23 Commits

Author SHA1 Message Date
Vishvananda Abrams
1e35ba25dc test: Improve test reliability with proper cleanup and isolation
Refactors test setup and teardown logic to use `t.Cleanup` instead
of `defer`. This ensures that cleanup functions are correctly scoped
to each subtest's lifecycle, improving test isolation and reliability.

The `setUpNetlinkTest` helper function is also improved to correctly
save and restore the original network namespace, ensuring that tests
do not leak state.

To support this, a `Close()` method that returns an error is added to
the `Handle` struct, allowing for proper cleanup of underlying netlink
sockets. The test helpers are updated to use this new method,
preventing resource leaks between tests.

Additionally, a bug in the `netns` tests is fixed where a large
namespace ID could overflow a 32-bit integer, causing spurious
failures on some systems.
2025-08-27 09:24:17 -07:00
Vishvananda Abrams
00c3a3def6 Isolate RuleListFiltered subtests
Refactors the TestRuleListFiltered test to run each subtest in its
own network namespace. This prevents state leakage between subtests,
resolving intermittent test failures caused by rules created in one
test interfering with subsequent tests.

The test logic for verifying all rules is also simplified by moving
the rule listing into the setup phase for that specific test case,
removing conditional logic from the main test loop.

Fixes: #1108
2025-08-27 09:24:17 -07:00
wangling142
26ee0e2abe 1. fix rule test failed when rule add slow. 2024-12-27 11:12:53 -08:00
Percy Wegmann
5b0b9d8260 rule: add Rule.Type to allow adding/listing unreachable (RTN_UNREACHABLE) rules
Updates #710

Co-authored-by: Brad Fitzpatrick <bradfitz@tailscale.com>
Signed-off-by: Percy Wegmann <percy@tailscale.com>
2024-08-23 12:10:14 -07:00
qianxiao
8f96fd8b2f # rule: fix 32-bit platforms don't support adding rules with a mark value of 0x80000000/0xF0000000 ~ 0xF0000000/0xF0000000
The maximum value for an `int` type on a 32-bit platform is 0x7FFFFFFF. Since 0xF0000000 exceeds this limit, we need to use `uint` instead of `int` to handle these values.
2024-08-05 10:25:28 -07:00
Ronak Jain
578e95cc31 rule: fix parsing zero priority rule 2024-04-11 14:50:12 -07:00
Derek Dagit
dbf1bd04f9 Linux rule list results have Family populated
Fixes #708
2023-07-28 08:26:29 -07:00
Nikolay Aleksandrov
ced5aaba43 rule: add support for FRA_PROTOCOL
Add support for ip rules' FRA_PROTOCOL attribute and also check for it
when testing rules. The default ip rule protocol is RTPROT_UNSPEC (0) so
we set the attribute only when it is >0.

Signed-off-by: Nikolay Aleksandrov <razor@blackwall.org>
2023-03-16 09:30:32 -07:00
Manjiri Gadagkar
20de99527f rule: add support for uidrange
Rename rule.UID as rule.UIDRange
2022-07-28 08:58:17 -07:00
Artem Glazychev
d6b03fdeb8 Fix review comment
Signed-off-by: Artem Glazychev <artem.glazychev@xored.com>
2022-01-18 11:05:37 -06:00
Zihong Zheng
5a3e901175 rule: add support for ipproto
This is similar to https://github.com/vishvananda/netlink/pull/511,
but this time for the ipproto option:
```
ip rule add ipproto xxx table main
```
2022-01-18 11:05:37 -06:00
Quentin McGaw
30ec08bc3d Rule.String() method returns .Dst
- Return `to <all | ipNetString>` in string
- Return `all` for `from` if `rule.Src == nil`
- Return `all` for `to` if `rule.Dst == nil`

Add unit test
2021-09-20 13:10:42 -07:00
Chris Tarazi
564132f787 Implement RuleListFiltered
This commit adds the ability to list rules in a filtered manner, similar
to RouteListFiltered.

Signed-off-by: Chris Tarazi <tarazichris@gmail.com>
2020-06-03 11:26:35 -07:00
chendotjs
a2e8781202 rule: add support for dport/sport 2020-02-10 06:52:06 -08:00
Amit Nishry
7ed9c95980 rule, add TOS
Signed-off-by: Amit Nishry <amit.nishry@gmail.com>
2020-02-09 10:15:44 -08:00
Alessandro Boch
f67b75edbf Properly tear down netns at the end of test
Signed-off-by: Alessandro Boch <aboch@tetrationanalytics.com>
2017-11-28 09:08:21 -08:00
Alessandro Boch
6174cd873f Support invert in ip rules
Signed-off-by: Alessandro Boch <aboch@tetrationanalytics.com>
2017-11-08 03:31:45 -08:00
Ian Bishop
0e3b74dbe2 replace syscall with golang.org/x/sys/unix 2017-10-26 09:45:08 -07:00
Aithal
dedc638a06 Fix compilation errors for non linux platforms.
The go get command and make both fail when executed on
non-linux platforms. Modified it so that there are no
compilation errors when developing in such an
environment.
2017-02-20 12:00:54 -08:00
Sebastien Boving
dbc72376c8 Properly skip tests which require root.
All these tests currently fail with 'Operation not permitted' when run as
non-root.
2017-02-06 09:09:57 -08:00
Vishvananda Ishaya
adb0f53af6 Cleanup from golint and go vet 2016-03-19 17:12:26 -07:00
Hubert Krauze
97758f316b Remove FlagMask from Rule 2015-12-09 14:07:11 +01:00
Marek Polewski
89945b09c0 Add support for rules 2015-11-26 11:50:07 +01:00