Commit graph

55 commits

Author SHA1 Message Date
Thomas Haller
d1e6edcb7b
test-client: move static function from TestNmClient._read_expected() to Util.file_read_expected()
Static, state-less functions are great. Keep them separate from TestNmClient which
has lots of state.
2023-05-15 14:59:58 +02:00
Lubomir Rintel
b93ebb9be9
test/client: test cloud-setup GCP support 2023-05-12 12:42:55 +02:00
Lubomir Rintel
661545efcd
test/client: test cloud-setup azure support 2023-05-12 12:42:55 +02:00
Lubomir Rintel
1f310abfd8
test/client: test cloud-setup aliyun support 2023-05-12 12:42:55 +02:00
Lubomir Rintel
6a1dd3b0f8
test-client: use a test fixture from the test
Don't rely on resources provided by mock metadata server by default,
create the from within the test instead.

This allows for more flexibility, but the locality of the test fixture
relative to the tests makes the test more legible.
2023-05-12 12:42:55 +02:00
Lubomir Rintel
e56df68464
test-client: factor out the test device setup
We're going to reuse the setup for tests of other cloud providers.
2023-05-12 12:42:55 +02:00
Lubomir Rintel
0667209913
test-client: hardcode the cloud-setup mac addresses
We rely on the predictable but random MAC addresses. Hardcode them
instead -- the mock service also hardcodes them.
2023-05-12 12:42:55 +02:00
Lubomir Rintel
63452e886f
test: fix file description passing to cloud-setup mock service
The pass_fds file descriptor is *after* the dup2. Always 3.
2023-05-12 12:42:54 +02:00
Lubomir Rintel
2e8ff9f8a0
Revert "client/tests: don't do dup2() dance to pass file descriptor to "tools/test-cloud-meta-mock.py""
This changed the fd passing protocol making it not compatible with
systemd-socket-activate(1).

This reverts commit 342ee618c7.
2023-05-12 12:42:54 +02:00
Thomas Haller
751ee63e61
test-client: cleanup after test on failure
Otherwise, the following tests will fail too.
2023-05-12 12:42:54 +02:00
Thomas Haller
cb51aee21c
test-client: increase context in pexecpt failure for debugging
When a pexpect check fails, we want to see the full content of the
buffer, so we can better see where it went wrong. Increase the context
that is printed in the error message.
2023-05-12 12:42:54 +02:00
Thomas Haller
342ee618c7
client/tests: don't do dup2() dance to pass file descriptor to "tools/test-cloud-meta-mock.py"
"preexec_fn" is not great, because it is not generally safe in multi
threaded code (and we don't know whether the test didn't start other
threads already, like a GDBus worker thread). Well, it probably is safe,
if you only call async signal safe code, but it's not clear what os.dup2()
does under the hood.

Just avoid that. We can pass on the FD directly.
2023-03-30 15:36:16 +02:00
Thomas Haller
d533072962
client/tests: close process stdin in test-python.py to avoid warning about leak
test_ec2 (__main__.TestNmCloudSetup.test_ec2) ... /usr/lib64/python3.11/unittest/case.py:579: ResourceWarning: unclosed file <_io.BufferedWriter name=5>
    if method() is not None:
  ResourceWarning: Enable tracemalloc to get the object allocation traceback
  ok

Fixes: d89d42bf23 ('tests/client: test nm-cloud-setup')
2023-03-30 15:35:22 +02:00
Thomas Haller
a9558231cf
client/tests: drop unnecessary socket.set_inheritable() call from "test-client.py"
This seems unnecessary, because we spawn the child process via subprocess.Popen and
set "pass_fds". That already ensures to pass on the FD.

Worse, socket.set_inheritable() is only added in Python 3.4, that means the
test is gonna break for Python 3.2 and 3.3. Just avoid that by not using the
unnecessary function. For the same reason, drop "inheritable=True" from
os.dup2(). "inheritable=True" is already the default, but only exists
since Python 3.4.

Fixes: d89d42bf23 ('tests/client: test nm-cloud-setup')
2023-03-30 15:06:54 +02:00
Thomas Haller
15c25e91f5
client/tests: skip cloud-setup test for older python
The test uses subprocess.Popen()'s "pass_fd" argument. That is only
available since Python 3.2. Possibly it could be solved differently, but
that is not implemented. Instead, skip the test.

Also, socket.socket.set_inheritable() is Python 3.4. But presumably
we don't need it.

Fixes: d89d42bf23 ('tests/client: test nm-cloud-setup')
2023-03-30 14:56:14 +02:00
Lubomir Rintel
d89d42bf23 tests/client: test nm-cloud-setup
Some fairly rudimentary testing of nm-cloud-setup.
2023-03-21 23:35:42 +01:00
Lubomir Rintel
ad6878d50a tests/client: split out nmcli specific bits into a separate cass
The mock service is more widely useful -- in particular for testing
nm-cloud-setup in a following commit.

Split the commonly useful parts into TestNmClient class.
2023-03-21 23:35:42 +01:00
Thomas Haller
2f846eea0d
test-client: re-enable test_monitor() test
It's fixed now. See the previous commit.

(cherry picked from commit 50d1c8fc16)
2023-02-08 10:53:07 +01:00
Thomas Haller
323c60e4cd
test-client: fix race in test_monitor() test
During srv_shutdown() we do

  p.stdin.close()
  p.kill()

Usually, the kill wins and the service just drops off the bus:

  libnm-dbus[3201919]: <debug> [438617.45324] nmclient[40f7938626f3f5f0]: name owner changed: ":1.1" -> (null)
  libnm-dbus[3201919]: <debug> [438617.45332] nmclient[40f7938626f3f5f0]: release all

at which point all objects in NMClient get destroyed and the signals get
emitted in the order:

  libnm-dbus[3201919]: <trace> [438617.45574] nmclient[40f7938626f3f5f0]: [nmclient] emit "device-removed" signal for /org/freedesktop/NetworkManager/Devices/1
  nmcli[out]: eth0: device removed
  libnm-dbus[3201919]: <trace> [438617.45590] nmclient[40f7938626f3f5f0]: [nmclient] emit "any-device-removed" signal for /org/freedesktop/NetworkManager/Devices/1
  libnm-dbus[3201919]: <trace> [438617.45593] nmclient[40f7938626f3f5f0]: [nmclient] emit "connection-removed" signal for /org/freedesktop/NetworkManager/Settings/Connectio>
  nmcli[out]: con-1: connection profile removed

However, sometimes the stub service notices that stdin was closed and it
sends signals about shutting down:

  libnm-dbus[3201061]: <trace> [438226.44965] nmclient[401639659459c316]: interfaces-removed: [/org/freedesktop/NetworkManager/Settings] receive interface remove event for >
  libnm-dbus[3201061]: <trace> [438226.44966] nmclient[401639659459c316]: [/org/freedesktop/NetworkManager/Settings]: changed-type 0x01 linked
  libnm-dbus[3201061]: <trace> [438226.44967] nmclient[401639659459c316]: [/org/freedesktop/NetworkManager/Settings]: changed-type 0x01 consumed
  libnm-dbus[3201061]: <trace> [438226.44968] nmclient[401639659459c316]: [/org/freedesktop/NetworkManager/Settings]: changed-type 0x02 linked
  libnm-dbus[3201061]: <trace> [438226.44969] nmclient[401639659459c316]: [/org/freedesktop/NetworkManager/Settings]: unregister NMClient from D-Bus object
  libnm-dbus[3201061]: <trace> [438226.44971] nmclient[401639659459c316]: [/org/freedesktop/NetworkManager/Settings]: drop D-Bus instance
  libnm-dbus[3201061]: <trace> [438226.44971] nmclient[401639659459c316]: [/org/freedesktop/NetworkManager/Settings]: set D-Bus object state unlinked
  libnm-dbus[3201061]: <trace> [438226.44972] nmclient[401639659459c316]: [nmclient] emit "connection-removed" signal for /org/freedesktop/NetworkManager/Settings/Connectio>
  nmcli[out]: con-1: connection profile removed
  libnm-dbus[3201061]: <trace> [438226.44992] nmclient[401639659459c316]: interfaces-removed: [/org/freedesktop/NetworkManager] receive interface remove event for interface>
  libnm-dbus[3201061]: <trace> [438226.44994] nmclient[401639659459c316]: [/org/freedesktop/NetworkManager]: changed-type 0x01 linked
  libnm-dbus[3201061]: <trace> [438226.44995] nmclient[401639659459c316]: [/org/freedesktop/NetworkManager]: changed-type 0x01 consumed
  libnm-dbus[3201061]: <trace> [438226.44996] nmclient[401639659459c316]: [/org/freedesktop/NetworkManager]: changed-type 0x02 linked
  libnm-dbus[3201061]: <trace> [438226.44998] nmclient[401639659459c316]: [/org/freedesktop/NetworkManager]: unregister NMClient from D-Bus object
  libnm-dbus[3201061]: <trace> [438226.44999] nmclient[401639659459c316]: [/org/freedesktop/NetworkManager]: drop D-Bus instance
  libnm-dbus[3201061]: <trace> [438226.45000] nmclient[401639659459c316]: [/org/freedesktop/NetworkManager]: set D-Bus object state unlinked
  libnm-dbus[3201061]: <trace> [438226.45001] nmclient[401639659459c316]: [nmclient] emit "device-removed" signal for /org/freedesktop/NetworkManager/Devices/1
  nmcli[out]: eth0: device removed
  libnm-dbus[3201061]: <trace> [438226.45005] nmclient[401639659459c316]: [nmclient] emit "any-device-removed" signal for /org/freedesktop/NetworkManager/Devices/1
  nmcli[out]: NetworkManager is stopped
  libnm-dbus[3201061]: <debug> [438226.45545] nmclient[401639659459c316]: name owner changed: ":1.1" -> (null)
  libnm-dbus[3201061]: <debug> [438226.45550] nmclient[401639659459c316]: release all

The fix is to accept the events in any order.

(cherry picked from commit 8548ab29ee)
2023-02-08 10:53:06 +01:00
Thomas Haller
07c0680ff6
test-client: randomly kill or close the stub test service
The test stub service watches stdin, and if it gets closed the service
will shut down. Note that the service does not catch any signals, so
sending a signal will kill the service right away.

The previous code first closed stdin, and then killed the process.
That can result in different outcomes on D-Bus. Usually the signal
gets received first, and the test service just drops off the bus.
Sometimes it notices the closing of stdin and shuts actively down.

That can make a difference, especially for the test_monitor() test which
runs the monitor while stopping the service.

We could just always kill the stub service to get consistent behavior.
However, that doesn't seem very useful. Instead, randomize the behavior
to easier see how the behavior differs.

(cherry picked from commit fc282d5e05)
2023-02-08 10:53:06 +01:00
Thomas Haller
1a9892620e
test-client: pass LIBNM_CLIENT_DEBUG to nmcli
For debugging libnm, LIBNM_CLIENT_DEBUG can be very useful.

As the tests compare stdout/stderr from nmcli with expected output, just
enabling it will break the tests. However, in combination with
LIBNM_CLIENT_DEBUG_FILE this can be very useful.

Preserve and pass on the environment variables, if set.

(cherry picked from commit 1630009234)
2023-02-08 10:53:06 +01:00
Thomas Haller
0694ecfcc5
test-client: cleanup imports
Sort imports by name. Also avoid "from signal import SIGINT". I find
it ugly to import names in the current namespace. "SIGINT" should be
refered to by its full name, including the package/namespace.

(cherry picked from commit ee17346cee)
2023-02-08 10:53:05 +01:00
Thomas Haller
ba5cbedd2c
test-client: add valgrind support for call_nmcli_pexpect() tests
This will allow to find some memory leaks and memory corruptions.

The bulk of the nmcli calls are still not hooked up with valgrind.
Since we call nmcli a thousand time, we could not just run valgrind with
all of them. We would have instead to enable it randomly. This is
more work.

(cherry picked from commit debf78dbed)
2023-02-08 10:53:05 +01:00
Thomas Haller
0e13811a4b
test-client: drop unused NmTestBase base class
The base class is not used, and it's not clear that it would be useful.
Sure, we could extend "test-client.py" will various non-nmcli tests. That
might make sense. And then it might make sense to have more unit test classes.
So far, we don't need that. Drop the unused base class NmTestBase.

(cherry picked from commit d1e6d53013)
2023-02-08 10:53:05 +01:00
Thomas Haller
05e5a2fefd
test-client: cleanup start/shutdown of stub service
(cherry picked from commit 3e574dda22)
2023-02-08 10:53:05 +01:00
Thomas Haller
c9951d5d32
test-client: accept yes/true/no/false for boolean arguments
(cherry picked from commit b08f7a9c19)
2023-02-08 10:53:05 +01:00
Thomas Haller
d0896a13ee
test-client: add Util.shlex_join() helper
(cherry picked from commit 5da7301ec9)
2023-02-08 10:53:05 +01:00
Thomas Haller
ae4f5a1861
client/tests: temporarily disable failing test test_monitor()
(cherry picked from commit 65ea47580f)
2023-02-01 10:50:13 +01:00
Thomas Haller
5c3b70a1b3
tests/client: increase timeout for nmcli command
Under normal circumstances, the timeout is not supposed to be hit.
I see it hit on gitlab-ci. Was that because the machine was very
busy? It's hard to say whether there was a legitimate problem here,
and more importantly, what that problem was.

Try to increase the timeout. If there is a real problem, we probably
will still hit the timeout.
2022-11-17 13:23:35 +01:00
Lubomir Rintel
aaa9a9cd25 libnm/client: don't reset properties when interface goes away
In case the D-Bus interfaces start dropping off (typically all off them go
one by one when the object is being deleted), don't reset all the properties.

In particular, keep most properties around, only tear down "o" and "ao",
so that the object dependencies get torn down, but we still get enough
properties around to identify what the dead object was its heyday.

One example of where this is not good is when the device-removed signal
is emmitted, the device no longer has the ifname:

  $ nmcli monitor
  <quit NetworkManager>
  (null): device removed
  (null): device removed
  ...
2022-11-13 15:25:19 +01:00
Lubomir Rintel
253bb3579f tests/client: test nmcli monitor
Some basic tests for nmcli monitor. Note that they now assert against
behavior that I find incorrect. Will be fixed separately.
2022-11-13 15:25:01 +01:00
Thomas Haller
36ad9855d1
tests: fix "test-client.py" for early python3 versions
ModuleNotFoundError was only introduced in later python 3 versions.
Use just "ImportError", which is the parent class anyway.

Fixes: f7e484c8ed ('tests: fix "test-client.py" ignoring missing "NM" module')
(cherry picked from commit 9902373c6d)
2022-08-26 00:01:12 +02:00
Thomas Haller
d6d76f900f
tests: fix "test-client.py" ignoring missing "NM" module
Fixes: 8959083784 ('tests: skip test in "test-client.py" if the pexepect dependency is not available')
(cherry picked from commit f7e484c8ed)
2022-08-26 00:01:12 +02:00
Thomas Haller
3dc5943134
tests: skip test in "test-client.py" if the pexepect dependency is not available
(cherry picked from commit 8959083784)
2022-08-26 00:01:11 +02:00
Lubomir Rintel
9a70d4e819 tests/client: add interactive add test 2022-06-30 10:20:44 +02:00
Lubomir Rintel
a3f0db06e4 tests/client: do not consult expected result files if no checks require them
Perhaps the test is not using call_nmcli*(), but still wants to use
@nm_test decorator to set up the mock service.
2022-06-30 10:20:44 +02:00
Lubomir Rintel
9570224e86 tests/client: add a pexpect-based test runner
Useful for testing interactive commands against a mock service.
2022-06-30 10:20:44 +02:00
Thomas Haller
7fd6804e2a
tests/client: improve readme for how to get test-client.py regenerate the test data 2022-05-02 19:03:32 +02:00
Thomas Haller
26a0307b8f
clients/tests: declare encoding of utf-8 python source "test-client.py"
The source file now contains UTF-8 (non ASCII) characters. Python2
doesn't like that:

  "./src/tests/client/test-client.sh" "." "." "/usr/bin/python"
    File "/builddir/build/BUILD/NetworkManager-1.39.2/src/tests/client/test-client.py", line 1730
  SyntaxError: Non-ASCII character '\xe2' in file /builddir/build/BUILD/NetworkManager-1.39.2/src/tests/client/test-client.py on line 1730, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
2022-04-21 12:01:20 +02:00
Thomas Haller
c041b02744
clients/tests: rename function Util.ReplaceTextUsingRegex to Util.ReplaceTextRegex
Seems to be the better name.
2022-04-20 15:47:57 +02:00
Thomas Haller
e35eceb9e3
clients/tests: add code comment and extend pattern in test_offline() 2022-04-20 15:47:01 +02:00
Thomas Haller
6aef83a556
clients/tests: workaround unexpected output for offline test
This test calls "nmcli g" with a bogus D-Bus bus address. We expect
a failure on stderr.

On alpine:latest, the error however looks slightly different:
  b'size: 258\nlocation: src/tests/client/test-client.py:test_offline()/1\ncmd: $NMCLI g\nlang: C\nreturncode: 1\nstderr: 136 bytes\n>>>\nError: Could not create NMClient object: Key/Value pair 0, *invalid*, in address element *very:invalid* does not contain an equal sign.\n\n<<<\n'
On ubuntu:16.04 and debian:9 we got:
  b"size: 258\nlocation: src/tests/client/test-client.py:test_offline()/1\ncmd: $NMCLI g\nlang: C\nreturncode: 1\nstderr: 136 bytes\n>>>\nError: Could not create NMClient object: Key/Value pair 0, 'invalid', in address element 'very:invalid' does not contain an equal sign.\n\n<<<\n"
On fedora and most recent systemd we got:
  b'size: 258\nlocation: src/tests/client/test-client.py:test_offline()/1\ncmd: $NMCLI g\nlang: C\nreturncode: 1\nstderr: 136 bytes\n>>>\nError: Could not create NMClient object: Key/Value pair 0, ?invalid?, in address element ?very:invalid? does not contain an equal sign.\n\n<<<\n'

This depends on the glib version (whether to print `%s', '%s', or “%s”).
Also, as we run the application with lang=C, so that libc (I think)
replaces Unicode with an ASCII character. Here musl and glibc behave
differently.

Workaround by replace the unexpected text.
2022-04-20 15:34:38 +02:00
Thomas Haller
2140da73a4
clients/tests: add code comment for how to get Polish translations on Fedora 2022-04-20 12:29:45 +02:00
Thomas Haller
7986ea8c1a
clients/tests: rework Util.replace_text() to uniformly accept a callable
Let the replace_arr parameter of Util.replace_text() only contain
callables, and don't special case the argument. Previously, we
either expected a regex or a 2-tuple, and the code would check
each explicitly.

Aside that the tuples are hard to follow, it also makes
Util.replace_text() strongly tied to those types. By instead accepting
and arbitrary function, each element can implement its own replacement.

Also, make text that was replaced by regex atomic. Meaning, if we
first match (and replace) a certain part of the text with the regex,
then the replacement cannot be split again. This is done by returning
a 1-tuple from the replace function.
2022-04-20 12:29:45 +02:00
Thomas Haller
a3038d4f5a
clients/tests: fix regular expression match in Util.replace_text()
Seems the previous code did not work properly:
With python36-3.6.8-38.module+el8.5.0+12207+5c5719bc.x86_6 on rhel-8.6:

  Traceback (most recent call last):
    File "/root/nm-build/NetworkManager/src/tests/client/test-client.py", line 1157, in f
      func(self)
    File "/root/nm-build/NetworkManager/src/tests/client/test-client.py", line 1724, in test_offline
      replace_stdout=replace_uuids,
    File "/root/nm-build/NetworkManager/src/tests/client/test-client.py", line 797, in call_nmcli
      frame,
    File "/root/nm-build/NetworkManager/src/tests/client/test-client.py", line 997, in _call_nmcli
      self.async_start(wait_all=sync_barrier)
    File "/root/nm-build/NetworkManager/src/tests/client/test-client.py", line 1032, in async_start
      async_job.wait_and_complete()
    File "/root/nm-build/NetworkManager/src/tests/client/test-client.py", line 670, in wait_and_complete
      self._complete_cb(self, return_code, stdout, stderr)
    File "/root/nm-build/NetworkManager/src/tests/client/test-client.py", line 919, in complete_cb
      stdout = Util.replace_text(stdout, replace_stdout)
    File "/root/nm-build/NetworkManager/src/tests/client/test-client.py", line 362, in replace_text
      if Util.is_regex_pattern(v_search):
    File "/root/nm-build/NetworkManager/src/tests/client/test-client.py", line 208, in is_regex_pattern
      t = re.Pattern
  AttributeError: module 're' has no attribute 'Pattern'

On this python version, re.compile() give an object of type
_sre.SRE_Pattern.

  # python -c 'import re; print(type(re.compile("a")))'
  <class '_sre.SRE_Pattern'>

Fixes: beebde9e56 ('client/test: allow matching and replacing regex-es in nmcli output')
2022-04-20 11:01:15 +02:00
Lubomir Rintel
97857dbacd client/test: add test for --offline behavior
Currently, only "add" and negative cases are tested. Testing "modify"
would require an ability to provide input. Perhaps at some later point.
2022-04-19 14:27:22 +02:00
Lubomir Rintel
e733357c91 client/test: add @nm_test_no_dbus decorator
Same as @nm_test, apart from that it doesn't spawn the mock D-Bus
service.
2022-04-19 14:12:43 +02:00
Lubomir Rintel
9108f8ecfc client/test: allow overriding all environment variables
Set extra variables after the pre-defined ones have been set. This
allows overriding then.

In particular, this allows overriding DBUS_SESSION_BUS_ADDRESS so that
the test can check the behavior of the client is correct when it's set
to some garbage.
2022-04-19 14:12:43 +02:00
Lubomir Rintel
beebde9e56 client/test: allow matching and replacing regex-es in nmcli output
This allows us to sanitize unpredictable UUIDs in client output in
--offline mode (where we can't just ask the mock service about the
actual UUID).
2022-04-19 14:12:43 +02:00
Thomas Haller
101a7cb56d
tests: improve documentation how to generate files for "/test-client.py" 2021-08-26 13:47:04 +02:00