This is an automated email from the ASF dual-hosted git repository.

tomaz pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/libcloud.git


The following commit(s) were added to refs/heads/trunk by this push:
     new e07170fc2 tests: add way to skip tests that require access to the 
network/Internet
     new 82bda65f5 Merge pull request #1697 from ogayot/fix-tests-no-internet
e07170fc2 is described below

commit e07170fc256197c254af1ff19efee926d5b4212a
Author: Olivier Gayot <[email protected]>
AuthorDate: Fri May 20 16:03:25 2022 +0200

    tests: add way to skip tests that require access to the network/Internet
    
    This patch introduces two new variables which allow to skip some of the
    tests run:
    
     * NO_INTERNET -> skips the tests that require access to the Internet
     * NO_NETWORK -> skips the tests that require the network to be up.
    
    NO_NETWORK implies NO_INTERNET.
    
    Tests that are skipped by NO_NETWORK include tests that bind sockets to
    the loopback interface.
    
    As for the two tests that are skipped with NO_INTERNET, they
    respectively:
    
     * send DNS queries and expect a NXDOMAIN response
     * attempt to send packets to 10.255.255.1 and expect a timeout
       (requires a default route)
    
    Signed-off-by: Olivier Gayot <[email protected]>
---
 libcloud/test/__init__.py         | 16 ++++++++++++++++
 libcloud/test/compute/test_ovh.py |  3 +++
 libcloud/test/test_connection.py  |  3 ++-
 libcloud/test/test_http.py        |  6 +++++-
 4 files changed, 26 insertions(+), 2 deletions(-)

diff --git a/libcloud/test/__init__.py b/libcloud/test/__init__.py
index e67ac2de0..89115b43e 100644
--- a/libcloud/test/__init__.py
+++ b/libcloud/test/__init__.py
@@ -16,6 +16,7 @@
 import unittest
 import random
 import requests
+import os
 from libcloud.common.base import Response
 from libcloud.http import LibcloudConnection
 from libcloud.utils.py3 import PY2
@@ -286,6 +287,21 @@ def generate_random_data(size):
     return data
 
 
+def no_network():
+    """Return true if the NO_NETWORK environment variable is set.
+    Can be used to skip relevant tests.
+    """
+    return "NO_NETWORK" in os.environ
+
+
+def no_internet():
+    """Return true if the NO_INTERNET or the NO_NETWORK environment variable
+    is set.
+    Can be used to skip relevant tests.
+    """
+    return "NO_INTERNET" in os.environ or no_network()
+
+
 if __name__ == "__main__":
     import doctest
 
diff --git a/libcloud/test/compute/test_ovh.py 
b/libcloud/test/compute/test_ovh.py
index b6971724a..3e64ccbe8 100644
--- a/libcloud/test/compute/test_ovh.py
+++ b/libcloud/test/compute/test_ovh.py
@@ -26,6 +26,8 @@ from libcloud.test.common.test_ovh import BaseOvhMockHttp
 from libcloud.test.secrets import OVH_PARAMS
 from libcloud.test.file_fixtures import ComputeFileFixtures
 
+from libcloud.test import no_internet
+
 
 class OvhMockHttp(BaseOvhMockHttp):
     """Fixtures needed for tests related to rating model"""
@@ -211,6 +213,7 @@ class OvhTests(unittest.TestCase):
         driver = OvhNodeDriver(*OVH_PARAMS, region="ca")
         self.assertEqual(driver.connection.host, "ca.api.ovh.com")
 
+    @unittest.skipIf(no_internet(), "Internet is not reachable")
     def test_list_nodes_invalid_region(self):
         OvhNodeDriver.connectionCls.conn_class = LibcloudConnection
         driver = OvhNodeDriver(*OVH_PARAMS, region="invalid")
diff --git a/libcloud/test/test_connection.py b/libcloud/test/test_connection.py
index 82c1caaa9..1206f494b 100644
--- a/libcloud/test/test_connection.py
+++ b/libcloud/test/test_connection.py
@@ -31,7 +31,7 @@ from libcloud.common.exceptions import RateLimitReachedError
 from libcloud.http import LibcloudBaseConnection
 from libcloud.http import LibcloudConnection
 from libcloud.http import SignedHTTPSAdapter
-from libcloud.test import unittest
+from libcloud.test import unittest, no_internet
 from libcloud.utils.py3 import assertRaisesRegex
 from libcloud.utils.retry import RETRY_EXCEPTIONS
 from libcloud.utils.retry import Retry
@@ -189,6 +189,7 @@ class BaseConnectionClassTestCase(unittest.TestCase):
         conn = LibcloudConnection(host="localhost", port=8080, timeout=10)
         self.assertEqual(conn.session.timeout, 10)
 
+    @unittest.skipIf(no_internet(), "Internet is not reachable")
     def test_connection_timeout_raised(self):
         """
         Test that the connection times out
diff --git a/libcloud/test/test_http.py b/libcloud/test/test_http.py
index 95c89802b..53f601273 100644
--- a/libcloud/test/test_http.py
+++ b/libcloud/test/test_http.py
@@ -34,7 +34,7 @@ from libcloud.utils.py3 import reload
 from libcloud.utils.py3 import assertRaisesRegex
 from libcloud.http import LibcloudConnection
 
-from libcloud.test import unittest
+from libcloud.test import unittest, no_network
 
 ORIGINAL_CA_CERTS_PATH = libcloud.security.CA_CERTS_PATH
 
@@ -134,6 +134,7 @@ class HttpLayerTestCase(unittest.TestCase):
         elif "https_proxy" in os.environ:
             del os.environ["https_proxy"]
 
+    @unittest.skipIf(no_network(), "Network is disabled")
     def test_prepared_request_empty_body_chunked_encoding_not_used(self):
         connection = LibcloudConnection(host=self.listen_host, 
port=self.listen_port)
         connection.prepared_request(
@@ -151,6 +152,7 @@ class HttpLayerTestCase(unittest.TestCase):
         self.assertEqual(connection.response.status_code, httplib.OK)
         self.assertEqual(connection.response.content, 
b"/test/prepared-request-2")
 
+    @unittest.skipIf(no_network(), "Network is disabled")
     def test_prepared_request_with_body(self):
         connection = LibcloudConnection(host=self.listen_host, 
port=self.listen_port)
         connection.prepared_request(
@@ -160,6 +162,7 @@ class HttpLayerTestCase(unittest.TestCase):
         self.assertEqual(connection.response.status_code, httplib.OK)
         self.assertEqual(connection.response.content, 
b"/test/prepared-request-3")
 
+    @unittest.skipIf(no_network(), "Network is disabled")
     def test_request_custom_timeout_no_timeout(self):
         def response_hook(*args, **kwargs):
             # Assert timeout has been passed correctly
@@ -172,6 +175,7 @@ class HttpLayerTestCase(unittest.TestCase):
         )
         connection.request(method="GET", url="/test", hooks=hooks)
 
+    @unittest.skipIf(no_network(), "Network is disabled")
     def test_request_custom_timeout_timeout(self):
         def response_hook(*args, **kwargs):
             # Assert timeout has been passed correctly

Reply via email to