Skip to contentSkip to conversation

tests: py: use `os.unshare` Python function

PATCH In review Jeremy Sowden 4 participants 9 messages Mar 5 – Mar 13 netfilter

lore
Share & tools
lore
#0 lore raw

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
index 53fd3f7ae6fe..64837da36035 100755
@@ -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
reviewer
#2 lore raw
In reply to Jeremy Sowden

Hi Jeremy,

Quoted 2 lines · Jeremy Sowden

Since Python 3.12 the standard library has included an os.unshare function. 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

#3 lore raw
Quoted 8 lines · Phil Sutter
Quoted 2 lines · Jeremy Sowden

Since Python 3.12 the standard library has included an os.unshare function. 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

	$ 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 0x002bd849 0x74a8f400

	$ 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. :)

reviewer Acked-by Ander Juaristi Acked-by Pablo Neira Ayuso
#4 lore raw

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.unshare function. 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

	$ 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 0x002bd849 0x74a8f400

	$ 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:

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:00

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 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.

Fixes 0518ea3f70d8c ("tests: add meta time test cases")

So that's how UTC-2 became py test suite's native timezone. :D

Cheers, Phil

maintainer
#5 lore raw
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.

Is there anyone working on a fix?

This breaks my CI pipeline (i.e. I disabled meta.t tests).

#6 lore raw
Quoted 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'.

#7 lore raw
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.

#8 lore raw
In reply to Phil Sutter
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.unshare function. 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

	$ 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 0x002bd849 0x74a8f400

	$ 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
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.