conftest.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import os
  2. from enum import Enum, auto
  3. from time import sleep
  4. import pytest
  5. import yaml
  6. from config import NamedTunnelConfig, ProxyDnsConfig, QuickTunnelConfig
  7. from constants import BACKOFF_SECS, PROXY_DNS_PORT
  8. from util import LOGGER
  9. class CfdModes(Enum):
  10. NAMED = auto()
  11. QUICK = auto()
  12. PROXY_DNS = auto()
  13. @pytest.fixture(scope="session")
  14. def component_tests_config():
  15. config_file = os.getenv("COMPONENT_TESTS_CONFIG")
  16. if config_file is None:
  17. raise Exception(
  18. "Need to provide path to config file in COMPONENT_TESTS_CONFIG")
  19. with open(config_file, 'r') as stream:
  20. config = yaml.safe_load(stream)
  21. LOGGER.info(f"component tests base config {config}")
  22. def _component_tests_config(additional_config={}, cfd_mode=CfdModes.NAMED, run_proxy_dns=True, provide_ingress=True):
  23. if run_proxy_dns:
  24. # Regression test for TUN-4177, running with proxy-dns should not prevent tunnels from running.
  25. # So we run all tests with it.
  26. additional_config["proxy-dns"] = True
  27. additional_config["proxy-dns-port"] = PROXY_DNS_PORT
  28. else:
  29. additional_config.pop("proxy-dns", None)
  30. additional_config.pop("proxy-dns-port", None)
  31. # Allows the ingress rules to be omitted from the provided config
  32. ingress = []
  33. if provide_ingress:
  34. ingress = config['ingress']
  35. # Provide the hostname to allow routing to the tunnel even if the ingress rule isn't defined in the config
  36. hostname = config['ingress'][0]['hostname']
  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.PROXY_DNS:
  45. return ProxyDnsConfig(cloudflared_binary=config['cloudflared_binary'])
  46. elif cfd_mode is CfdModes.QUICK:
  47. return QuickTunnelConfig(additional_config=additional_config, cloudflared_binary=config['cloudflared_binary'])
  48. else:
  49. raise Exception(f"Unknown cloudflared mode {cfd_mode}")
  50. return _component_tests_config
  51. # This fixture is automatically called before each tests to make sure the previous cloudflared has been shutdown
  52. @pytest.fixture(autouse=True)
  53. def wait_previous_cloudflared():
  54. sleep(BACKOFF_SECS)