mirror of
https://github.com/cloudflare/cloudflared.git
synced 2025-07-27 18:19:57 +00:00
TUN-4049: Add component tests to assert logging behavior when running from terminal
This commit is contained in:

committed by
Chung Ting Huang

parent
d22b374208
commit
f23e33c082
@@ -4,11 +4,15 @@ import copy
|
||||
import os
|
||||
import yaml
|
||||
|
||||
from dataclasses import dataclass
|
||||
from dataclasses import dataclass, InitVar
|
||||
|
||||
from constants import METRICS_PORT
|
||||
|
||||
from util import LOGGER
|
||||
|
||||
# frozen=True raises exception when assigning to fields. This emulates immutability
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class TunnelBaseConfig:
|
||||
no_autoupdate: bool = True
|
||||
@@ -27,20 +31,41 @@ class NamedTunnelBaseConfig(TunnelBaseConfig):
|
||||
# so we have to use default values here and check if they are set in __post_init__
|
||||
tunnel: str = None
|
||||
credentials_file: str = None
|
||||
ingress: list = None
|
||||
|
||||
def __post_init__(self):
|
||||
if self.tunnel is None:
|
||||
raise TypeError("Field tunnel is not set")
|
||||
if self.credentials_file is None:
|
||||
raise TypeError("Field credentials_file is not set")
|
||||
if self.ingress is None:
|
||||
raise TypeError("Field ingress is not set")
|
||||
|
||||
def merge_config(self, additional):
|
||||
config = super(NamedTunnelBaseConfig, self).merge_config(additional)
|
||||
config['tunnel'] = self.tunnel
|
||||
config['credentials-file'] = self.credentials_file
|
||||
# In some cases we want to override default ingress, such as in config tests
|
||||
if 'ingress' not in config:
|
||||
config['ingress'] = self.ingress
|
||||
return config
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class NamedTunnelConfig(NamedTunnelBaseConfig):
|
||||
full_config: dict = None
|
||||
additional_config: InitVar[dict] = {}
|
||||
|
||||
def __post_init__(self, additional_config):
|
||||
# Cannot call set self.full_config because the class is frozen, instead, we can use __setattr__
|
||||
# https://docs.python.org/3/library/dataclasses.html#frozen-instances
|
||||
object.__setattr__(self, 'full_config',
|
||||
self.merge_config(additional_config))
|
||||
|
||||
def get_url(self):
|
||||
return "https://" + self.ingress[0]['hostname']
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ClassicTunnelBaseConfig(TunnelBaseConfig):
|
||||
hostname: str = None
|
||||
@@ -54,28 +79,44 @@ class ClassicTunnelBaseConfig(TunnelBaseConfig):
|
||||
|
||||
def merge_config(self, additional):
|
||||
config = super(ClassicTunnelBaseConfig, self).merge_config(additional)
|
||||
config['hostnamel'] = self.hostname
|
||||
config['hostname'] = self.hostname
|
||||
config['origincert'] = self.origincert
|
||||
return config
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ClassicTunnelConfig(ClassicTunnelBaseConfig):
|
||||
full_config: dict = None
|
||||
additional_config: InitVar[dict] = {}
|
||||
|
||||
def __post_init__(self, additional_config):
|
||||
# Cannot call set self.full_config because the class is frozen, instead, we can use __setattr__
|
||||
# https://docs.python.org/3/library/dataclasses.html#frozen-instances
|
||||
object.__setattr__(self, 'full_config',
|
||||
self.merge_config(additional_config))
|
||||
|
||||
def get_url(self):
|
||||
return "https://" + self.hostname
|
||||
|
||||
|
||||
@dataclass
|
||||
class ComponentTestConfig:
|
||||
cloudflared_binary: str
|
||||
named_tunnel_config: dict
|
||||
classic_tunnel_config: dict
|
||||
named_tunnel_config: NamedTunnelConfig
|
||||
classic_tunnel_config: ClassicTunnelConfig
|
||||
|
||||
|
||||
def build_config_from_env():
|
||||
config_path = get_env("COMPONENT_TESTS_CONFIG")
|
||||
config_content = base64.b64decode(get_env("COMPONENT_TESTS_CONFIG_CONTENT")).decode('utf-8')
|
||||
config_content = base64.b64decode(
|
||||
get_env("COMPONENT_TESTS_CONFIG_CONTENT")).decode('utf-8')
|
||||
config_yaml = yaml.safe_load(config_content)
|
||||
|
||||
credentials_file = get_env("COMPONENT_TESTS_CREDENTIALS_FILE")
|
||||
write_file(credentials_file, config_yaml["credentials_file"])
|
||||
|
||||
origincert = get_env("COMPONENT_TESTS_ORIGINCERT")
|
||||
write_file(origincert,config_yaml["origincert"])
|
||||
write_file(origincert, config_yaml["origincert"])
|
||||
|
||||
write_file(config_content, config_path)
|
||||
|
||||
@@ -94,4 +135,4 @@ def get_env(env_name):
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
build_config_from_env()
|
||||
build_config_from_env()
|
||||
|
Reference in New Issue
Block a user