cloudflare/cloudflared
Publicmirrored from https://github.com/cloudflare/cloudflaredAvailable
component-tests/conftest.py
60lines · modeblame
a7344435cthuang5 years ago | 1 | import os |
1e8dea91Nuno Diegues4 years ago | 2 | from enum import Enum, auto |
| 3 | from time import sleep | |
| 4 | | |
a7344435cthuang5 years ago | 5 | import pytest |
| 6 | import yaml | |
| 7 | | |
1e8dea91Nuno Diegues4 years ago | 8 | from config import NamedTunnelConfig, ClassicTunnelConfig, ProxyDnsConfig |
| 9 | from constants import BACKOFF_SECS, PROXY_DNS_PORT | |
a7344435cthuang5 years ago | 10 | from util import LOGGER |
| 11 | | |
f23e33c0cthuang5 years ago | 12 | |
1e8dea91Nuno Diegues4 years ago | 13 | class CfdModes(Enum): |
| 14 | NAMED = auto() | |
| 15 | CLASSIC = auto() | |
| 16 | PROXY_DNS = auto() | |
| 17 | | |
| 18 | | |
a7344435cthuang5 years ago | 19 | @pytest.fixture(scope="session") |
| 20 | def component_tests_config(): | |
| 21 | config_file = os.getenv("COMPONENT_TESTS_CONFIG") | |
| 22 | if config_file is None: | |
f23e33c0cthuang5 years ago | 23 | raise Exception( |
| 24 | "Need to provide path to config file in COMPONENT_TESTS_CONFIG") | |
a7344435cthuang5 years ago | 25 | with open(config_file, 'r') as stream: |
| 26 | config = yaml.safe_load(stream) | |
| 27 | LOGGER.info(f"component tests base config {config}") | |
| 28 | | |
1e8dea91Nuno Diegues4 years ago | 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: | |
25cfbec0cthuang5 years ago | 40 | return NamedTunnelConfig(additional_config=additional_config, |
b25d38ddNuno Diegues5 years ago | 41 | cloudflared_binary=config['cloudflared_binary'], |
| 42 | tunnel=config['tunnel'], | |
| 43 | credentials_file=config['credentials_file'], | |
| 44 | ingress=config['ingress']) | |
1e8dea91Nuno Diegues4 years ago | 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}") | |
a7344435cthuang5 years ago | 53 | |
f23e33c0cthuang5 years ago | 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) |