From 9a945df65f234f39683d64bbf08c9546cd8078cf Mon Sep 17 00:00:00 2001
From: Yu Watanabe <watanabe.yu+github@gmail.com>
Date: Sat, 6 Jan 2024 12:42:02 +0900
Subject: [PATCH 0531/1160] test-network: do not call networkctl if networkd is
 in failed state

Otherwise, networkd may be restarted by DBus and we may get wrong
results.

(cherry picked from commit 6b07675d819fccd1790d7dee43419ce6a75bad3d)
---
 test/test-network/systemd-networkd-tests.py | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/test/test-network/systemd-networkd-tests.py b/test/test-network/systemd-networkd-tests.py
index 6a57e9b9a5..4410c60d66 100755
--- a/test/test-network/systemd-networkd-tests.py
+++ b/test/test-network/systemd-networkd-tests.py
@@ -712,6 +712,9 @@ def read_networkd_log(invocation_id=None, since=None):
     check_output('journalctl --sync')
     return check_output(*command)
 
+def networkd_is_failed():
+    return call_quiet('systemctl is-failed -q systemd-networkd.service') != 1
+
 def stop_networkd(show_logs=True):
     if show_logs:
         invocation_id = networkd_invocation_id()
@@ -734,6 +737,9 @@ def networkd_pid():
     return int(check_output('systemctl show --value -p MainPID systemd-networkd.service'))
 
 def networkctl(*args):
+    # Do not call networkctl if networkd is in failed state.
+    # Otherwise, networkd may be restarted and we may get wrong results.
+    assert not networkd_is_failed()
     return check_output(*(networkctl_cmd + list(args)), env=env)
 
 def networkctl_status(*args):
@@ -986,11 +992,15 @@ class Utilities():
         try:
             check_output(*args, env=wait_online_env)
         except subprocess.CalledProcessError:
-            # show detailed status on failure
-            for link in links_with_operstate:
-                name = link.split(':')[0]
-                if link_exists(name):
-                    networkctl_status(name)
+            if networkd_is_failed():
+                print('!!!!! systemd-networkd.service is failed !!!!!')
+                call('systemctl status systemd-networkd.service')
+            else:
+                # show detailed status on failure
+                for link in links_with_operstate:
+                    name = link.split(':')[0]
+                    if link_exists(name):
+                        networkctl_status(name)
             raise
         if not bool_any and setup_state:
             for link in links_with_operstate:
-- 
2.33.0