cloudflare/cloudflared

Public

mirrored fromhttps://github.com/cloudflare/cloudflaredAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
cc1c6d9abc865533e1ebb5a6a387fd3973ff7bc6

Branches

Tags

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

Clone

HTTPS

Download ZIP

component-tests/test_proxy_dns.py

72lines · modecode

1#!/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:
12 def test_proxy_dns_with_named_tunnel(self, tmp_path, component_tests_config):
13 run_test_scenario(tmp_path, component_tests_config, CfdModes.NAMED, run_proxy_dns=True)
14
15 def test_proxy_dns_with_classic_tunnel(self, tmp_path, component_tests_config):
16 run_test_scenario(tmp_path, component_tests_config, CfdModes.CLASSIC, run_proxy_dns=True)
17
18 def test_proxy_dns_alone(self, tmp_path, component_tests_config):
19 run_test_scenario(tmp_path, component_tests_config, CfdModes.PROXY_DNS, run_proxy_dns=True)
20
21 def test_named_tunnel_alone(self, tmp_path, component_tests_config):
22 run_test_scenario(tmp_path, component_tests_config, CfdModes.NAMED, run_proxy_dns=False)
23
24 def test_classic_tunnel_alone(self, tmp_path, component_tests_config):
25 run_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):
29 expect_proxy_dns = run_proxy_dns
30 expect_tunnel = False
31
32 if cfd_mode == CfdModes.NAMED:
33 expect_tunnel = True
34 pre_args = ["tunnel"]
35 args = ["run"]
36 elif cfd_mode == CfdModes.CLASSIC:
37 expect_tunnel = True
38 pre_args = []
39 args = []
40 elif cfd_mode == CfdModes.PROXY_DNS:
41 expect_proxy_dns = True
42 pre_args = []
43 args = ["proxy-dns", "--port", str(constants.PROXY_DNS_PORT)]
44 else:
45 assert False, f"Unknown cfd_mode {cfd_mode}"
46
47 config = component_tests_config(cfd_mode=cfd_mode, run_proxy_dns=run_proxy_dns)
48 with start_cloudflared(tmp_path, config, cfd_pre_args=pre_args, cfd_args=args, new_process=True, capture_output=False):
49 if expect_tunnel:
50 wait_tunnel_ready()
51 else:
52 check_tunnel_not_connected()
53 verify_proxy_dns(expect_proxy_dns)
54
55
56def verify_proxy_dns(should_be_running):
57 # Wait for the Proxy DNS listener to come up.
58 sleep(constants.BACKOFF_SECS)
59 had_failure = False
60 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
61 try:
62 sock.connect(('localhost', constants.PROXY_DNS_PORT))
63 sock.send(b"anything")
64 except:
65 if should_be_running:
66 assert False, "Expected Proxy DNS to be running, but it was not."
67 had_failure = True
68 finally:
69 sock.close()
70
71 if not should_be_running and not had_failure:
72 assert False, "Proxy DNS should not have been running, but it was."
73