Commit Graph

82 Commits

Author SHA1 Message Date
Lubomir Rintel
cb7157f552 test-client: add test for OCI VNIC on VM
Tests that we create a connection for disconnected device.
2025-04-11 11:44:05 +02:00
Lubomir Rintel
c42322bd0d test-client: split up _mock_devices()
The new VM OCI VNIC tests will need to create the devices, but leave one
disconnected.
2025-04-11 11:44:05 +02:00
Lubomir Rintel
39db524d94 test-client: expand MAC addresses from variable
Less literals, more expanded variables. Makes it a little easier to read
for me.
2025-04-11 11:44:05 +02:00
Lubomir Rintel
961be7d971 cloud-setup: split up _nmc_skip_connection_by_type()
Split _nmc_skip_connection_by_type() so that we can get a little
more finely grained error reporting.
2025-04-11 11:42:54 +02:00
Lubomir Rintel
5c16bb1d26 Reapply "client/test: add test for VLANs on OCI"
This reverts commit a852b79cf5f783dccff738152d238f93bb956cc8.

(cherry picked from commit 70872cbb38)
2025-02-26 13:30:08 +01:00
Lubomir Rintel
efa4b459fc client/test-client: fix formatting
Fix-up for the preceding merge.
2025-02-06 10:37:29 +01:00
Lubomir Rintel
ca78471b04 Revert "client/test: add test for VLANs on OCI"
This reverts commit 9b258faab4.
2025-02-06 10:35:02 +01:00
Lubomir Rintel
cc4a0a9f10 test-client: collect pexpect subprocess status
Always explicitly tear down pexpect instances and collect their
results. Assert on the results after orderly teardowns.

Track the current pexpect instance in test context so that it could be
still collected if the test blows up. That could provide more clue into
what went wrong in the test if it's due to a crash the testee.

Before:

  [1573928.02238] <debug> config device C0:00:00:00:00:10: creating vlan connection for VLAN 700 on C0:00:00:00:00:10...
  [1573928.02330] <debug> config device C0:00:00:00:00:10: connection "vlan2" (ac3c08f5-3e5c-38a3-a366-c16253de6db2) created
  ======================================================================
  ERROR: test_oci_vlans (__main__.TestNmCloudSetup.test_oci_vlans)
  ----------------------------------------------------------------------
  Traceback (most recent call last):
  ...
      pexp.expect("some changes were applied for provider oci")
      ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  ...
  pexpect.exceptions.EOF: End Of File (EOF). Exception style platform.

After:

  [1573928.02238] <debug> config device C0:00:00:00:00:10: creating vlan connection for VLAN 700 on C0:00:00:00:00:10...
  [1573928.02330] <debug> config device C0:00:00:00:00:10: connection "vlan2" (ac3c08f5-3e5c-38a3-a366-c16253de6db2) created
  *** pexpect'd process killed by SIGABRT ***
  ======================================================================
  ERROR: test_oci_vlans (__main__.TestNmCloudSetup.test_oci_vlans)
  ----------------------------------------------------------------------
  Traceback (most recent call last):
  ...
      pexp.expect("some changes were applied for provider oci")
      ~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  ...
  pexpect.exceptions.EOF: End Of File (EOF). Exception style platform.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/2119
2025-01-28 14:05:51 +01:00
Lubomir Rintel
7023d8b444 test-client: fix TestNmCloudSetup valgrind run
Allow running the following locally (for quick loval nm-c-s valgrind check),
without requiring $NM_TEST_CLIENT_NMCLI_PATH to be set.

  $ NM_TEST_CLIENT_CLOUD_SETUP_PATH=build/src/nm-cloud-setup/nm-cloud-setup \
      NMTST_USE_VALGRIND=1 python src/tests/client/test-client.py TestNmCloudSetup

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/2119
2025-01-28 14:05:48 +01:00
Lubomir Rintel
9b258faab4 client/test: add test for VLANs on OCI 2025-01-20 14:08:12 +01:00
Lubomir Rintel
6ff4b9e57c cloud-setup: create VLANs for multiple VNICs on OCI
The idea is to create a pair of VLAN and MACVLAN with AddAndActivate if
they are not present, and otherwise follow the ordinary (GetApplied &
Reapply) procedure if the devices are already present.
2025-01-20 14:08:12 +01:00
Lubomir Rintel
87b23669fa client/test: add nm-c-s OCI test
Very basic.
2025-01-20 14:08:12 +01:00
Lubomir Rintel
ecafa051df client/test: add ability to log pexpect traffic 2025-01-20 14:08:12 +01:00
Lubomir Rintel
a0c14665a3 client/test: move run_post() into TestNmcli
It attempts to modify attributes clearly belong to TestNmcli such as
_skip_test_for_l10n_diff or call methods that are in unittest.TestCase:

  ======================================================================
  ERROR: test_002 (__main__.TestNmcli.test_002)
  ----------------------------------------------------------------------
  Traceback (most recent call last):
    File ".../src/tests/client/test-client.py", line 1508, in f
      self.ctx.run_post()
      ~~~~~~~~~~~~~~~~~^^
    File ".../src/tests/client/test-client.py", line 1185, in run_post
      self.fail(
      ^^^^^^^^^
  AttributeError: 'NMTestContext' object has no attribute 'fail'

It has presumably been moved out of TestNmcli at some point, but that
seems to have been in error, as it's also pretty specific to the nmcli
test cases. Not useful for cloud-init tests that also utilize
NMTestContext. Move it back.
2025-01-20 14:08:12 +01:00
Jan Vaclav
4db1829bc7 all: remove misc mentions of autotools in docs 2024-09-11 12:18:15 +00:00
Jan Vaclav
f9397a5740 test/client: set 802-11-wireless.mac-address-denylist 2024-03-11 11:42:19 +01:00
Thomas Haller
ca5fb29b7e client/tests: add checks to "test-client.py"
- test for "-order" option with `nmcli connection show`.

- test for order of activated devices. Optimally, the devices
  should be in activating vs. activated state. I fail to do that,
  the mock implementation is cumbersome to use. It still seems useful
  to have this (maybe it could be improved).
2023-11-15 09:34:45 +01:00
Beniamino Galvani
85bcf2d99f test-client: fix warnings about invalid escape sequences
In Python 3.12 a backslash-character pair that is not a valid escape
sequence generates a SyntaxWarning [1]:

    src/tests/client/test-client.py:2161: SyntaxWarning: invalid escape sequence '\?'
    nmc.pexp.expect("Do you want to provide them\? \(yes/no\) \[yes]")

Use raw strings to avoid that.

[1] https://docs.python.org/3/whatsnew/3.12.html

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1785
2023-11-07 09:58:39 +01:00
Íñigo Huguet
123ca26770 nmcli: don't warn version mismatch with daemon not running
Fixes: fb851f3294 ('nmcli: warn if daemon version mismatch')

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1750
2023-10-18 08:13:22 +02:00
Wen Liang
fa53b97214 test-client: fix python2 f-string and format compatibility issue
f-string is not supported in python2, and the autotool build complains
about it as follows:
```
  LIBTOOL="/bin/sh ./libtool" "../src/tests/client/test-client.sh" "." ".." "python2" -- TestNmCloudSetup
    File "/builds/NetworkManager/NetworkManager/src/tests/client/test-client.py", line 722
      return f"{major}.{minor}.{micro}"
                                      ^
  SyntaxError: invalid syntax
  test-client.py failed!!
  make[3]: *** [check-local-tests-client] Error 1
    File "/builds/NetworkManager/NetworkManager/src/tests/client/test-client.py", line 722
      return f"{major}.{minor}.{micro}"
                                      ^
  SyntaxError: invalid syntax
  test-client.py failed!!
```

Also, python2 complains about extra comma during argument unpacking.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1718
2023-09-05 07:53:51 -04:00
Íñigo Huguet
28e53fed89 nmcli: warn if daemon version mismatch
When updating NetworkManager to a new version, normally the service is
not restarted by the installer to avoid interrupting networking.
However, next nmcli invocation will use the updated version, but against
the older version of the daemon that is still running. Although this is
suposed to work, it is advisable that nmcli and daemon's versions are
the same. Emit a warning recommending restarting the daemon.

Add nmcli test to check the new feature. To avoid breaking the existing
tests, test-networkmanager-service now reports the same version than the
running nmcli except if it's instructed to report a different one.

https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/1703
(cherry picked from commit fb851f3294)
2023-08-09 16:40:54 +02:00
Íñigo Huguet
d414265ab1 nmcli: fix endless loop with --offline --ask
If --offline and --ask were used at the same time, and endless loop
showing the readline's prompt but without waiting for user's input
happened.

This was because when using --offline, all arguments are parsed and
resolved before running the g_main_loop. In nmc_readline_helper it was
checked that the main loop is running, so if g_main_loop_quit is called
we can stop waiting for user's input.

Fix this bug by continue polling for user input if the main loop is
running or if we are in offline mode.  Cancelling the user input is
still possible both in normal and offline mode with Ctrl+C or Ctrl+D.

Added a test case to verify that this still works after future changes.
2023-07-17 12:58:07 +02:00
Thomas Haller
77fd4d5f0e test-cloud-meta-mock: update default MAC address for test-cloud-meta-mock.py
Choose a MAC address that is easily recognizable. And where you can
see which is mac1 and mac2.
2023-05-22 17:24:55 +02:00
Thomas Haller
efc14fcbec test-client: drop TestNmClient base class from tests
With the unit test framework, we define special methods, like setUp()
and test_*(). This is documented, but not obvious.

Previously, TestNmClient was the base class for our tests classes, and
it provided some functionality (and state). It was utterly confusing how
pieces fit together.

Instead, move the state to a new class NMTestContext(). That contains
most of the code from TestNmClient. Drop TestNmClient and let the test
classes directly descend from unittest.TestCase.

The difference is, when you now look at a certain test (test_001()), you
can easier understand which code runs when. First, the test class has a
setUp() method which runs, but that method is now trivial without extra
context. Second, there is the @nm_test attribute that wraps the
function. But that's it. It's all at one place, and we delegate instead
of inherit.
2023-05-15 14:59:58 +02:00
Thomas Haller
50f97307c5 test-client: move TestNmClient.ReplaceTextConUuid() to NMStubServer
The goal is to make the base class TestNmClient smaller because it
contains a lot of state, and with inheritance it's hard to follow what
happens.
2023-05-15 14:59:58 +02:00
Thomas Haller
1c32b59ebd test-client: move TestNmClient.{cmd_construct_argv,_env}() to Util.cmd_create_{argv,env}()
These two are stateless method, and even not only work for nmcli, but also for
nm-cloud-setup. Move to the Util class, as static methods.
2023-05-15 14:59:58 +02:00
Thomas Haller
7ecb659743 test-client: move skip functions to Util class
They can both be used as decorators and called plainly.
2023-05-15 14:59:58 +02:00
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