瀏覽代碼

tests: DockerComposer connect_to_network except

If running from a container host-network-mode test fail because we cannot connect to host network. This prevents the cleanup.
So make sure we always return network for later removal.

Explicitly return None for readability
Niek 11 月之前
父節點
當前提交
58e21618b4
共有 1 個文件被更改,包括 9 次插入3 次删除
  1. 9 3
      test/conftest.py

+ 9 - 3
test/conftest.py

@@ -428,9 +428,15 @@ def connect_to_network(network: Network) -> Optional[Network]:
         # Make sure our container is connected to the nginx-proxy's network,
         # but avoid connecting to `none` network (not valid) with `test_server-down` tests
         if network.name not in my_networks and network.name != 'none':
-            logging.info(f"Connecting to docker network: {network.name}")
-            network.connect(my_container)
-            return network
+            try:
+                logging.info(f"Connecting to docker network: {network.name}")
+                network.connect(my_container)
+                return network
+            except docker.errors.APIError as e:
+                logging.warning(f"Failed to connect to network {network.name}: {e}")
+                return network  # Ensure the network is still tracked for later removal
+
+    return None
 
 
 def disconnect_from_network(network: Network = None):