|
| 1 | +#!/usr/bin/env python |
| 2 | +import requests |
| 3 | +from conftest import CfdModes |
| 4 | +from constants import METRICS_PORT, MAX_RETRIES, BACKOFF_SECS |
| 5 | +from retrying import retry |
| 6 | +from cli import CloudflaredCli |
| 7 | +from util import LOGGER, write_config, start_cloudflared, wait_tunnel_ready, send_requests |
| 8 | +import platform |
| 9 | + |
| 10 | +""" |
| 11 | +Each test in TestManagement will: |
| 12 | +1. Acquire a management token from Cloudflare public API |
| 13 | +2. Make a request against the management service for the running tunnel |
| 14 | +""" |
| 15 | +class TestManagement: |
| 16 | + """ |
| 17 | + test_get_host_details does the following: |
| 18 | + 1. It gets a management token from Tunnelstore using cloudflared tail token <tunnel_id> |
| 19 | + 2. It gets the connector_id after starting a cloudflare tunnel |
| 20 | + 3. It sends a request to the management host with the connector_id and management token |
| 21 | + 4. Asserts that the response has a hostname and ip. |
| 22 | + """ |
| 23 | + def test_get_host_details(self, tmp_path, component_tests_config): |
| 24 | + # TUN-7377 : wait_tunnel_ready does not work properly in windows. |
| 25 | + # Skipping this test for windows for now and will address it as part of tun-7377 |
| 26 | + if platform.system() == "Windows": |
| 27 | + return |
| 28 | + config = component_tests_config(cfd_mode=CfdModes.NAMED, run_proxy_dns=False, provide_ingress=False) |
| 29 | + LOGGER.debug(config) |
| 30 | + headers = {} |
| 31 | + headers["Content-Type"] = "application/json" |
| 32 | + config_path = write_config(tmp_path, config.full_config) |
| 33 | + with start_cloudflared(tmp_path, config, cfd_pre_args=["tunnel", "--ha-connections", "1", "--label" , "test"], cfd_args=["run", "--hello-world"], new_process=True): |
| 34 | + wait_tunnel_ready(tunnel_url=config.get_url(), |
| 35 | + require_min_connections=1) |
| 36 | + cfd_cli = CloudflaredCli(config, config_path, LOGGER) |
| 37 | + connector_id = cfd_cli.get_connector_id(config)[0] |
| 38 | + url = cfd_cli.get_management_url("host_details", config, config_path) |
| 39 | + resp = send_request(url, headers=headers) |
| 40 | + |
| 41 | + # Assert response json. |
| 42 | + assert resp.status_code == 200, "Expected cloudflared to return 200 for host details" |
| 43 | + assert resp.json()["hostname"] == "custom:test", "Expected cloudflared to return hostname" |
| 44 | + assert resp.json()["ip"] != "", "Expected cloudflared to return ip" |
| 45 | + assert resp.json()["connector_id"] == connector_id, "Expected cloudflared to return connector_id" |
| 46 | + |
| 47 | + """ |
| 48 | + test_get_metrics will verify that the /metrics endpoint returns the prometheus metrics dump |
| 49 | + """ |
| 50 | + def test_get_metrics(self, tmp_path, component_tests_config): |
| 51 | + # TUN-7377 : wait_tunnel_ready does not work properly in windows. |
| 52 | + # Skipping this test for windows for now and will address it as part of tun-7377 |
| 53 | + if platform.system() == "Windows": |
| 54 | + return |
| 55 | + config = component_tests_config(cfd_mode=CfdModes.NAMED, run_proxy_dns=False, provide_ingress=False) |
| 56 | + LOGGER.debug(config) |
| 57 | + config_path = write_config(tmp_path, config.full_config) |
| 58 | + with start_cloudflared(tmp_path, config, cfd_pre_args=["tunnel", "--ha-connections", "1"], new_process=True): |
| 59 | + wait_tunnel_ready(require_min_connections=1) |
| 60 | + cfd_cli = CloudflaredCli(config, config_path, LOGGER) |
| 61 | + url = cfd_cli.get_management_url("metrics", config, config_path) |
| 62 | + resp = send_request(url) |
| 63 | + |
| 64 | + # Assert response. |
| 65 | + assert resp.status_code == 200, "Expected cloudflared to return 200 for /metrics" |
| 66 | + assert "# HELP build_info Build and version information" in resp.text, "Expected /metrics to have with the build_info details" |
| 67 | + |
| 68 | + """ |
| 69 | + test_get_pprof_heap will verify that the /debug/pprof/heap endpoint returns a pprof/heap dump response |
| 70 | + """ |
| 71 | + def test_get_pprof_heap(self, tmp_path, component_tests_config): |
| 72 | + # TUN-7377 : wait_tunnel_ready does not work properly in windows. |
| 73 | + # Skipping this test for windows for now and will address it as part of tun-7377 |
| 74 | + if platform.system() == "Windows": |
| 75 | + return |
| 76 | + config = component_tests_config(cfd_mode=CfdModes.NAMED, run_proxy_dns=False, provide_ingress=False) |
| 77 | + LOGGER.debug(config) |
| 78 | + config_path = write_config(tmp_path, config.full_config) |
| 79 | + with start_cloudflared(tmp_path, config, cfd_pre_args=["tunnel", "--ha-connections", "1"], new_process=True): |
| 80 | + wait_tunnel_ready(require_min_connections=1) |
| 81 | + cfd_cli = CloudflaredCli(config, config_path, LOGGER) |
| 82 | + url = cfd_cli.get_management_url("debug/pprof/heap", config, config_path) |
| 83 | + resp = send_request(url) |
| 84 | + |
| 85 | + # Assert response. |
| 86 | + assert resp.status_code == 200, "Expected cloudflared to return 200 for /debug/pprof/heap" |
| 87 | + assert resp.headers["Content-Type"] == "application/octet-stream", "Expected /debug/pprof/heap to have return a binary response" |
| 88 | + |
| 89 | + |
| 90 | +@retry(stop_max_attempt_number=MAX_RETRIES, wait_fixed=BACKOFF_SECS * 1000) |
| 91 | +def send_request(url, headers={}): |
| 92 | + with requests.Session() as s: |
| 93 | + return s.get(url, timeout=BACKOFF_SECS, headers=headers) |
0 commit comments