cloudflare/cloudflared
Publicmirrored fromhttps://github.com/cloudflare/cloudflaredAvailable
component-tests/conftest.py
55lines · 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, QuickTunnelConfig |
| 9 | from constants import BACKOFF_SECS |
| 10 | from util import LOGGER |
| 11 | |
| 12 | |
| 13 | class CfdModes(Enum): |
| 14 | NAMED = auto() |
| 15 | QUICK = auto() |
| 16 | |
| 17 | |
| 18 | @pytest.fixture(scope="session") |
| 19 | def component_tests_config(): |
| 20 | config_file = os.getenv("COMPONENT_TESTS_CONFIG") |
| 21 | if config_file is None: |
| 22 | raise Exception( |
| 23 | "Need to provide path to config file in COMPONENT_TESTS_CONFIG") |
| 24 | with open(config_file, 'r') as stream: |
| 25 | config = yaml.safe_load(stream) |
| 26 | LOGGER.info(f"component tests base config {config}") |
| 27 | |
| 28 | def _component_tests_config(additional_config={}, cfd_mode=CfdModes.NAMED, provide_ingress=True): |
| 29 | # Allows the ingress rules to be omitted from the provided config |
| 30 | ingress = [] |
| 31 | if provide_ingress: |
| 32 | ingress = config['ingress'] |
| 33 | |
| 34 | # Provide the hostname to allow routing to the tunnel even if the ingress rule isn't defined in the config |
| 35 | hostname = config['ingress'][0]['hostname'] |
| 36 | |
| 37 | if cfd_mode is CfdModes.NAMED: |
| 38 | return NamedTunnelConfig(additional_config=additional_config, |
| 39 | cloudflared_binary=config['cloudflared_binary'], |
| 40 | tunnel=config['tunnel'], |
| 41 | credentials_file=config['credentials_file'], |
| 42 | ingress=ingress, |
| 43 | hostname=hostname) |
| 44 | elif cfd_mode is CfdModes.QUICK: |
| 45 | return QuickTunnelConfig(additional_config=additional_config, cloudflared_binary=config['cloudflared_binary']) |
| 46 | else: |
| 47 | raise Exception(f"Unknown cloudflared mode {cfd_mode}") |
| 48 | |
| 49 | return _component_tests_config |
| 50 | |
| 51 | |
| 52 | # This fixture is automatically called before each tests to make sure the previous cloudflared has been shutdown |
| 53 | @pytest.fixture(autouse=True) |
| 54 | def wait_previous_cloudflared(): |
| 55 | sleep(BACKOFF_SECS) |
| 56 | |