cloudflare/cloudflared
Publicmirrored from https://github.com/cloudflare/cloudflaredAvailable
component-tests/conftest.py
67lines · 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 | | |
93f8f6b5Devin Carr3 years ago | 8 | from config import NamedTunnelConfig, ProxyDnsConfig, QuickTunnelConfig |
1e8dea91Nuno Diegues4 years ago | 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() | |
93f8f6b5Devin Carr3 years ago | 15 | QUICK = auto() |
1e8dea91Nuno Diegues4 years ago | 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 | | |
7b8b3f73Devin Carr3 years ago | 29 | def _component_tests_config(additional_config={}, cfd_mode=CfdModes.NAMED, run_proxy_dns=True, provide_ingress=True): |
1e8dea91Nuno Diegues4 years ago | 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 | | |
7b8b3f73Devin Carr3 years ago | 39 | # Allows the ingress rules to be omitted from the provided config |
| 40 | ingress = [] | |
| 41 | if provide_ingress: | |
| 42 | ingress = config['ingress'] | |
| 43 | | |
| 44 | # Provide the hostname to allow routing to the tunnel even if the ingress rule isn't defined in the config | |
| 45 | hostname = config['ingress'][0]['hostname'] | |
| 46 | | |
1e8dea91Nuno Diegues4 years ago | 47 | if cfd_mode is CfdModes.NAMED: |
25cfbec0cthuang5 years ago | 48 | return NamedTunnelConfig(additional_config=additional_config, |
b25d38ddNuno Diegues5 years ago | 49 | cloudflared_binary=config['cloudflared_binary'], |
| 50 | tunnel=config['tunnel'], | |
| 51 | credentials_file=config['credentials_file'], | |
7b8b3f73Devin Carr3 years ago | 52 | ingress=ingress, |
| 53 | hostname=hostname) | |
1e8dea91Nuno Diegues4 years ago | 54 | elif cfd_mode is CfdModes.PROXY_DNS: |
| 55 | return ProxyDnsConfig(cloudflared_binary=config['cloudflared_binary']) | |
93f8f6b5Devin Carr3 years ago | 56 | elif cfd_mode is CfdModes.QUICK: |
| 57 | return QuickTunnelConfig(additional_config=additional_config, cloudflared_binary=config['cloudflared_binary']) | |
1e8dea91Nuno Diegues4 years ago | 58 | else: |
| 59 | raise Exception(f"Unknown cloudflared mode {cfd_mode}") | |
a7344435cthuang5 years ago | 60 | |
f23e33c0cthuang5 years ago | 61 | return _component_tests_config |
| 62 | | |
| 63 | | |
| 64 | # This fixture is automatically called before each tests to make sure the previous cloudflared has been shutdown | |
| 65 | @pytest.fixture(autouse=True) | |
| 66 | def wait_previous_cloudflared(): | |
| 67 | sleep(BACKOFF_SECS) |