tests: py: use `os.unshare` Python function
PATCH 1/1 tests: py: use `os.unshare` Python function
Since Python 3.12 the standard library has included an os.unshare function. Use it if it is available.
--- tests/py/nft-test.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-)
tests/py/nft-test.py +8 -1
@@ -1466,7 +1466,14 @@ def run_test_file(filename, force_all_family_option, specific_file): return [tests, passed, total_warning, total_error, total_unit_run] def spawn_netns(): - # prefer unshare module + # prefer stdlib unshare function ... + try: + os.unshare(os.CLONE_NEWNET) + return True + except Exception as e: + pass + + # ... or unshare module try: import unshare unshare.unshare(unshare.CLONE_NEWNET)
Signature · 1 line
2.51.0
Phil Sutter
reviewerQuoted 4 lines · Jeremy Sowden
Since Python 3.12 the standard library has included an
os.unsharefunction. Use it if it is available.Signed-off-by Jeremy Sowden <[email protected]>
Patch applied, thanks!
Phil Sutter
reviewerHi Jeremy,
Quoted 2 lines · Jeremy Sowden
Since Python 3.12 the standard library has included an
os.unsharefunction. Use it if it is available.
This patch breaks py test suite cases involving time-related matches, e.g. 'meta time "1970-05-23 21:07:14"'. It expects:
| cmp eq reg 1 0x002bd503 0x43f05400
but instead the rule serializes into:
| cmp eq reg 1 0x002bd849 0x74a8f400
Do you see that too?
Cheers, Phil
Quoted 8 lines · Phil Sutter
Quoted 2 lines · Jeremy Sowden
Since Python 3.12 the standard library has included an
os.unsharefunction. Use it if it is available.This patch breaks py test suite cases involving time-related matches, e.g. 'meta time "1970-05-23 21:07:14"'. It expects:
| cmp eq reg 1
0x002bd5030x43f05400
$ TZ=UTC-2 perl -MPOSIX=strftime -le 'my $ns = hex $ARGV[0]; print strftime "%Y-%m-%d %H:%M:%S", localtime int $ns / 1000000000' 0x002bd50343f05400 1970-05-23 21:07:14
Quoted 3 lines
but instead the rule serializes into:
| cmp eq reg 1
0x002bd8490x74a8f400
$ TZ=UTC-2 perl -MPOSIX=strftime -le 'my $ns = hex $ARGV[0]; print strftime "%Y-%m-%d %H:%M:%S", localtime int $ns / 1000000000' 0x002bd84974a8f400 1970-05-23 22:07:14
Quoted 1 line
Do you see that too?
Yes, e.g.:
6: WARNING: line 4: 'add rule netdev test-netdev egress meta time > "2022-07-01 11:00:00" accept': '[ cmp gt reg 1 0x16fda8f3 0x1977a000 ]' mismatches '[ cmp gt reg 1 0x16fdac39 0x4a304000 ]'
As with your example, the discrepancy is an hour:
$ TZ=UTC-2 perl -MPOSIX=strftime -le 'my $ns = hex $ARGV[0]; print strftime "%Y-%m-%d %H:%M:%S", localtime int $ns / 1000000000' 0x16fda8f31977a000 2022-07-01 11:00:00
$ TZ=UTC-2 perl -MPOSIX=strftime -le 'my $ns = hex $ARGV[0]; print strftime "%Y-%m-%d %H:%M:%S", localtime int $ns / 1000000000' 0x16fdac394a304000 2022-07-01 12:00:00
which suggests it's time-zone related. Didn't see anything about that in the doc's. Will take a closer look. Apologies.
J.
PS UTC-2 is exotic. :)
Hi Jeremy,
Quoted 28 lines · Jeremy Sowden
Quoted 8 lines · Phil Sutter
Quoted 2 lines · Jeremy Sowden
Since Python 3.12 the standard library has included an
os.unsharefunction. Use it if it is available.This patch breaks py test suite cases involving time-related matches, e.g. 'meta time "1970-05-23 21:07:14"'. It expects:
| cmp eq reg 1
0x002bd5030x43f05400$ TZ=UTC-2 perl -MPOSIX=strftime -le 'my $ns = hex $ARGV[0]; print strftime "%Y-%m-%d %H:%M:%S", localtime int $ns / 1000000000' 0x002bd50343f05400 1970-05-23 21:07:14Quoted 3 lines
but instead the rule serializes into:
| cmp eq reg 1
0x002bd8490x74a8f400$ TZ=UTC-2 perl -MPOSIX=strftime -le 'my $ns = hex $ARGV[0]; print strftime "%Y-%m-%d %H:%M:%S", localtime int $ns / 1000000000' 0x002bd84974a8f400 1970-05-23 22:07:14Quoted 1 line
Do you see that too?
Yes, e.g.:
6: WARNING: line 4: 'add rule netdev test-netdev egress meta time > "2022-07-01 11:00:00" accept': '[ cmp gt reg 1 0x16fda8f3 0x1977a000 ]' mismatches '[ cmp gt reg 1 0x16fdac39 0x4a304000 ]'As with your example, the discrepancy is an hour:
Quoted 8 lines
$ TZ=UTC-2 perl -MPOSIX=strftime -le 'my $ns = hex $ARGV[0]; print strftime "%Y-%m-%d %H:%M:%S", localtime int $ns / 1000000000' 0x16fda8f31977a000 2022-07-01 11:00:00$ TZ=UTC-2 perl -MPOSIX=strftime -le 'my $ns = hex $ARGV[0]; print strftime "%Y-%m-%d %H:%M:%S", localtime int $ns / 1000000000' 0x16fdac394a304000 2022-07-01 12:00:00which suggests it's time-zone related. Didn't see anything about that in the doc's. Will take a closer look. Apologies.
Yes, it's odd. Neither unshare module nor 'unshare -n' behave like this, even though os.unshare is described as doing the same as unshare command does. It also doesn't mangle os.environ['TZ'] value, no idea why it messes with this.
Quoted 1 line
PS UTC-2 is exotic. :)
Maybe it's Ander Juaristi's native timezone, he added the tests in commit 0518ea3f70d8c ("tests: add meta time test cases"). And then I did:
commit 7e326d697ecf43ea029de5584e59701eb61ca87e
Author: Phil Sutter <[email protected]> Date: Sat Nov 16 22:32:18 2019 +0100
tests/py: Set a fixed timezone in nft-test.py
Payload generated for 'meta time' matches depends on host's timezone and DST setting. To produce constant output, set a fixed timezone in nft-test.py. Choose UTC-2 since most payloads are correct then, adjust the remaining two tests.
So that's how UTC-2 became py test suite's native timezone. :D
Cheers, Phil
Florian Westphal
maintainerQuoted 7 lines · Phil Sutter
Quoted 2 lines
which suggests it's time-zone related. Didn't see anything about that in the doc's. Will take a closer look. Apologies.
Yes, it's odd. Neither unshare module nor 'unshare -n' behave like this, even though os.unshare is described as doing the same as unshare command does. It also doesn't mangle os.environ['TZ'] value, no idea why it messes with this.
Is there anyone working on a fix?
This breaks my CI pipeline (i.e. I disabled meta.t tests).
Pablo Neira Ayuso
maintainerQuoted 4 lines · Florian Westphal
Quoted 3 lines · Phil Sutter
Quoted 2 lines
which suggests it's time-zone related. Didn't see anything about that in the doc's. Will take a closer look. Apologies.
Quoted 8 lines
Quoted 4 lines
Yes, it's odd. Neither unshare module nor 'unshare -n' behave like this, even though os.unshare is described as doing the same as unshare command does. It also doesn't mangle os.environ['TZ'] value, no idea why it messes with this.
Is there anyone working on a fix?
This breaks my CI pipeline (i.e. I disabled meta.t tests).
I suggest to revert by now, as it seems os.unshare is not equivalent to 'unshare -n'.
Quoted 10 lines · Pablo Neira Ayuso
Quoted 9 lines · Florian Westphal
Quoted 7 lines · Phil Sutter
Quoted 2 lines
which suggests it's time-zone related. Didn't see anything about that in the doc's. Will take a closer look. Apologies.
Yes, it's odd. Neither unshare module nor 'unshare -n' behave like this, even though os.unshare is described as doing the same as unshare command does. It also doesn't mangle os.environ['TZ'] value, no idea why it messes with this.
Quoted 6 lines
Quoted 3 lines
Is there anyone working on a fix?
This breaks my CI pipeline (i.e. I disabled meta.t tests).
I suggest to revert by now, as it seems os.unshare is not equivalent to 'unshare -n'.
Agreed.
J.
Quoted 35 lines · Phil Sutter
Quoted 34 lines · Jeremy Sowden
Quoted 8 lines · Phil Sutter
Quoted 2 lines · Jeremy Sowden
Since Python 3.12 the standard library has included an
os.unsharefunction. Use it if it is available.This patch breaks py test suite cases involving time-related matches, e.g. 'meta time "1970-05-23 21:07:14"'. It expects:
| cmp eq reg 1
0x002bd5030x43f05400$ TZ=UTC-2 perl -MPOSIX=strftime -le 'my $ns = hex $ARGV[0]; print strftime "%Y-%m-%d %H:%M:%S", localtime int $ns / 1000000000' 0x002bd50343f05400 1970-05-23 21:07:14Quoted 3 lines
but instead the rule serializes into:
| cmp eq reg 1
0x002bd8490x74a8f400$ TZ=UTC-2 perl -MPOSIX=strftime -le 'my $ns = hex $ARGV[0]; print strftime "%Y-%m-%d %H:%M:%S", localtime int $ns / 1000000000' 0x002bd84974a8f400 1970-05-23 22:07:14Quoted 1 line
Do you see that too?
Yes, e.g.:
6: WARNING: line 4: 'add rule netdev test-netdev egress meta time > "2022-07-01 11:00:00" accept': '[ cmp gt reg 1 0x16fda8f3 0x1977a000 ]' mismatches '[ cmp gt reg 1 0x16fdac39 0x4a304000 ]'As with your example, the discrepancy is an hour:
$ TZ=UTC-2 perl -MPOSIX=strftime -le 'my $ns = hex $ARGV[0]; print strftime "%Y-%m-%d %H:%M:%S", localtime int $ns / 1000000000' 0x16fda8f31977a000 2022-07-01 11:00:00$ TZ=UTC-2 perl -MPOSIX=strftime -le 'my $ns = hex $ARGV[0]; print strftime "%Y-%m-%d %H:%M:%S", localtime int $ns / 1000000000' 0x16fdac394a304000 2022-07-01 12:00:00
Quoted 7 lines
Quoted 2 lines
which suggests it's time-zone related. Didn't see anything about that in the doc's. Will take a closer look. Apologies.
Yes, it's odd. Neither unshare module nor 'unshare -n' behave like this, even though os.unshare is described as doing the same as unshare command does. It also doesn't mangle os.environ['TZ'] value, no idea why it messes with this.
What makes it weirder is that setting the time-zone at the command-line fixes it:
$ cat tests/py/any/meta-time-test.t
:input;type filter hook input priority 0*ip;test-ip4;input
time > "2022-07-01 11:00:00" accept;ok;meta time > "2022-07-01 11:00:00" accept
$ sudo env TZ=UTC-2 /usr/bin/python3 tests/py/nft-test.py any/meta-time-test.t
INFO: Log will be available at /tmp/nftables-test.log
any/meta-time-test.t: 1 unit tests, 0 error, 0 warning
$ sudo /usr/bin/python3 tests/py/nft-test.py any/meta-time-test.t
INFO: Log will be available at /tmp/nftables-test.log
8: WARNING: line 4: 'add rule ip test-ip4 input time > "2022-07-01 11:00:00" accept': '[ cmp gt reg 1 0x16fda8f3 0x1977a000 ]' mismatches '[ cmp gt reg 1 0x16fdac39 0x4a304000 ]'
meta-time-test.t.payload.got: WARNING: line 2: Wrote payload for rule t
8: WARNING: line 4: 'add rule ip test-ip4 input meta time > "2022-07-01 11:00:00" accept': '[ cmp gt reg 1 0x16fda8f3 0x1977a000 ]' mismatches '[ cmp gt reg 1 0x16fdac39 0x4a304000 ]'
6: WARNING: line 4: '{"nftables": [{"add": {"rule": {"family": "ip", "table": "test-ip4", "chain": "input", "expr": [{"match": {"left": {"meta": {"key": "time"}}, "op": ">", "right": "2022-07-01 11:00:00"}}, {"accept": null}]}}}]}': '[ cmp gt reg 1 0x16fda8f3 0x1977a000 ]' mismatches '[ cmp gt reg 1 0x16fdac39 0x4a304000 ]' meta-time-test.t.json.payload.got: WARNING: line 2: Wrote JSON payload for rule t
any/meta-time-test.t: 1 unit tests, 3 error, 0 warning
This seems to imply that modifying os.environ doesn't actually modify the environment. I don't know Python very well, and this is the first time I've had a rummage in the internals, and I have not yet been able to explain it. I think the right thing, unfortunately, is to revert the commit until I can get to the bottom of it.
J.