full-suite-probe.py

import importlib, os, sys, socket, types
import pytest
attempts=[]
def denied(kind):
    def call(*args,**kwargs):
        attempts.append(kind)
        raise AssertionError('offline fixture attempted '+kind)
    return call
socket.socket.connect=denied('connect')
socket.getaddrinfo=denied('dns')
socket.create_connection=denied('connection')
# Guard credential discovery if auth is installed; absence needs no fake auth.
try:
    import google.auth
except ImportError:
    pass
else:
    google.auth.default=denied('credential_discovery')

sdk_mode=sys.argv[2]
real_sdk=False
if sdk_mode == 'constructor_guard':
    try:
        from google.cloud import pubsub_v1
        real_sdk=True
    except ImportError:
        # Explicit failure sentinel, not an SDK implementation or success fixture.
        def namespace(name):
            if name in sys.modules:
                return sys.modules[name]
            module=types.ModuleType(name)
            module.__path__=[]
            sys.modules[name]=module
            if '.' in name:
                parent,child=name.rsplit('.',1)
                setattr(namespace(parent),child,module)
            return module
        cloud=namespace('google.cloud')
        pubsub_v1=namespace('google.cloud.pubsub_v1')
    pubsub_v1.PublisherClient=denied('real_or_sentinel_publisher_constructor')
    original_sdk=pubsub_v1
    original_guard=pubsub_v1.PublisherClient
    cloud=importlib.import_module('google.cloud')
else:
    # This branch does not import Pub/Sub or require Google packages at all.
    sys.modules['google.cloud.pubsub_v1']=None
    try:
        cloud=importlib.import_module('google.cloud')
    except ImportError:
        cloud=None
    if cloud is not None and hasattr(cloud,'pubsub_v1'):
        delattr(cloud,'pubsub_v1')

os.environ['GOOGLE_APPLICATION_CREDENTIALS']='/nonexistent/fr43-fixture.json'
os.environ['MIZOKI_STORE']='memory'
os.chdir(sys.argv[1])
code=pytest.main(['tests/connectors','-c','tests/governance/pytest.ini','-o','addopts=','-q','-p','no:cacheprovider'])
print('FR43 mode:',sdk_mode,'real SDK:',real_sdk,'attempts:',attempts)
assert not attempts, attempts
if sdk_mode == 'constructor_guard':
    assert sys.modules['google.cloud.pubsub_v1'] is original_sdk
    assert cloud.pubsub_v1 is original_sdk
    assert original_sdk.PublisherClient is original_guard
else:
    assert sys.modules['google.cloud.pubsub_v1'] is None
    assert cloud is None or not hasattr(cloud,'pubsub_v1')
raise SystemExit(code)
← All docsView source on GitHub →