cloudflare/cloudflared
Publicmirrored fromhttps://github.com/cloudflare/cloudflaredAvailable
component-tests/conftest.py
60lines · modecode
| 1 | import os |
| 2 | from enum import Enum, auto |
| 3 | from time import sleep |
| 4 | |
| 5 | import pytest |
| 6 | import yaml |
| 7 | |
| 8 | from config import NamedTunnelConfig, ClassicTunnelConfig, ProxyDnsConfig |
| 9 | from constants import BACKOFF_SECS, PROXY_DNS_PORT |
| 10 | from util import LOGGER |
| 11 | |
| 12 | |
| 13 | class CfdModes(Enum): |
| 14 | NAMED = auto() |
| 15 | CLASSIC = auto() |
| 16 | PROXY_DNS = auto() |
| 17 | |
| 18 | |
| 19 | @pytest.fixture(scope="session") |
| 20 | def component_tests_config(): |
| 21 | config_file = os.getenv("COMPONENT_TESTS_CONFIG") |
| 22 | if config_file is None: |
| 23 | raise Exception( |
| 24 | "Need to provide path to config file in COMPONENT_TESTS_CONFIG") |
| 25 | with open(config_file, 'r') as stream: |
| 26 | config = yaml.safe_load(stream) |
| 27 | LOGGER.info(f"component tests base config {config}") |
| 28 | |
| 29 | def _component_tests_config(additional_config={}, cfd_mode=CfdModes.NAMED, run_proxy_dns=True): |
| 30 | if run_proxy_dns: |
| 31 | # Regression test for TUN-4177, running with proxy-dns should not prevent tunnels from running. |
| 32 | # So we run all tests with it. |
| 33 | additional_config["proxy-dns"] = True |
| 34 | additional_config["proxy-dns-port"] = PROXY_DNS_PORT |
| 35 | else: |
| 36 | additional_config.pop("proxy-dns", None) |
| 37 | additional_config.pop("proxy-dns-port", None) |
| 38 | |
| 39 | if cfd_mode is CfdModes.NAMED: |
| 40 | return NamedTunnelConfig(additional_config=additional_config, |
| 41 | cloudflared_binary=config['cloudflared_binary'], |
| 42 | tunnel=config['tunnel'], |
| 43 | credentials_file=config['credentials_file'], |
| 44 | ingress=config['ingress']) |
| 45 | elif cfd_mode is CfdModes.CLASSIC: |
| 46 | return ClassicTunnelConfig( |
| 47 | additional_config=additional_config, cloudflared_binary=config['cloudflared_binary'], |
| 48 | hostname=config['classic_hostname'], origincert=config['origincert']) |
| 49 | elif cfd_mode is CfdModes.PROXY_DNS: |
| 50 | return ProxyDnsConfig(cloudflared_binary=config['cloudflared_binary']) |
| 51 | else: |
| 52 | raise Exception(f"Unknown cloudflared mode {cfd_mode}") |
| 53 | |
| 54 | return _component_tests_config |
| 55 | |
| 56 | |
| 57 | # This fixture is automatically called before each tests to make sure the previous cloudflared has been shutdown |
| 58 | @pytest.fixture(autouse=True) |
| 59 | def wait_previous_cloudflared(): |
| 60 | sleep(BACKOFF_SECS) |
| 61 | |