cloudflare/cloudflared

Public

mirrored from https://github.com/cloudflare/cloudflaredAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2021.12.4

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

component-tests/test_proxy_dns.py

72lines · modeblame

1e8dea91Nuno Diegues4 years ago1#!/usr/bin/env python
2import socket
3from time import sleep
4
5import constants
6from conftest import CfdModes
7from util import start_cloudflared, wait_tunnel_ready, check_tunnel_not_connected
8
9
10# Sanity checks that test that we only run Proxy DNS and Tunnel when we really expect them to be there.
11class TestProxyDns:
12def test_proxy_dns_with_named_tunnel(self, tmp_path, component_tests_config):
13run_test_scenario(tmp_path, component_tests_config, CfdModes.NAMED, run_proxy_dns=True)
14
15def test_proxy_dns_with_classic_tunnel(self, tmp_path, component_tests_config):
16run_test_scenario(tmp_path, component_tests_config, CfdModes.CLASSIC, run_proxy_dns=True)
17
18def test_proxy_dns_alone(self, tmp_path, component_tests_config):
19run_test_scenario(tmp_path, component_tests_config, CfdModes.PROXY_DNS, run_proxy_dns=True)
20
21def test_named_tunnel_alone(self, tmp_path, component_tests_config):
22run_test_scenario(tmp_path, component_tests_config, CfdModes.NAMED, run_proxy_dns=False)
23
24def test_classic_tunnel_alone(self, tmp_path, component_tests_config):
25run_test_scenario(tmp_path, component_tests_config, CfdModes.CLASSIC, run_proxy_dns=False)
26
27
28def run_test_scenario(tmp_path, component_tests_config, cfd_mode, run_proxy_dns):
29expect_proxy_dns = run_proxy_dns
30expect_tunnel = False
31
32if cfd_mode == CfdModes.NAMED:
33expect_tunnel = True
34pre_args = ["tunnel"]
35args = ["run"]
36elif cfd_mode == CfdModes.CLASSIC:
37expect_tunnel = True
38pre_args = []
39args = []
40elif cfd_mode == CfdModes.PROXY_DNS:
41expect_proxy_dns = True
42pre_args = []
43args = ["proxy-dns", "--port", str(constants.PROXY_DNS_PORT)]
44else:
45assert False, f"Unknown cfd_mode {cfd_mode}"
46
47config = component_tests_config(cfd_mode=cfd_mode, run_proxy_dns=run_proxy_dns)
48with start_cloudflared(tmp_path, config, cfd_pre_args=pre_args, cfd_args=args, new_process=True, capture_output=False):
49if expect_tunnel:
50wait_tunnel_ready()
51else:
52check_tunnel_not_connected()
53verify_proxy_dns(expect_proxy_dns)
54
55
56def verify_proxy_dns(should_be_running):
57# Wait for the Proxy DNS listener to come up.
58sleep(constants.BACKOFF_SECS)
59had_failure = False
60sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
61try:
62sock.connect(('localhost', constants.PROXY_DNS_PORT))
63sock.send(b"anything")
64except:
65if should_be_running:
66assert False, "Expected Proxy DNS to be running, but it was not."
67had_failure = True
68finally:
69sock.close()
70
71if not should_be_running and not had_failure:
72assert False, "Proxy DNS should not have been running, but it was."