cloudflare/cloudflared
Publicmirrored fromhttps://github.com/cloudflare/cloudflaredAvailable
component-tests/test_config.py
57lines · modecode
| 1 | #!/usr/bin/env python |
| 2 | from util import start_cloudflared |
| 3 | |
| 4 | |
| 5 | class TestConfig: |
| 6 | # tmp_path is a fixture provides a temporary directory unique to the test invocation |
| 7 | def test_validate_ingress_rules(self, tmp_path, component_tests_config): |
| 8 | extra_config = { |
| 9 | 'ingress': [ |
| 10 | { |
| 11 | "hostname": "example.com", |
| 12 | "service": "https://localhost:8000", |
| 13 | "originRequest": { |
| 14 | "originServerName": "test.example.com", |
| 15 | "caPool": "/etc/certs/ca.pem" |
| 16 | }, |
| 17 | }, |
| 18 | { |
| 19 | "hostname": "api.example.com", |
| 20 | "path": "login", |
| 21 | "service": "https://localhost:9000", |
| 22 | }, |
| 23 | { |
| 24 | "hostname": "wss.example.com", |
| 25 | "service": "wss://localhost:8000", |
| 26 | }, |
| 27 | { |
| 28 | "hostname": "ssh.example.com", |
| 29 | "service": "ssh://localhost:8000", |
| 30 | }, |
| 31 | {"service": "http_status:404"} |
| 32 | ], |
| 33 | } |
| 34 | config = component_tests_config(extra_config) |
| 35 | validate_args = ["ingress", "validate"] |
| 36 | _ = start_cloudflared(tmp_path, config, validate_args) |
| 37 | |
| 38 | self.match_rule(tmp_path, config, |
| 39 | "http://example.com/index.html", 1) |
| 40 | self.match_rule(tmp_path, config, |
| 41 | "https://example.com/index.html", 1) |
| 42 | self.match_rule(tmp_path, config, |
| 43 | "https://api.example.com/login", 2) |
| 44 | self.match_rule(tmp_path, config, |
| 45 | "https://wss.example.com", 3) |
| 46 | self.match_rule(tmp_path, config, |
| 47 | "https://ssh.example.com", 4) |
| 48 | self.match_rule(tmp_path, config, |
| 49 | "https://api.example.com", 5) |
| 50 | |
| 51 | # This is used to check that the command tunnel ingress url <url> matches rule number <rule_num>. Note that rule number uses 1-based indexing |
| 52 | |
| 53 | def match_rule(self, tmp_path, config, url, rule_num): |
| 54 | args = ["ingress", "rule", url] |
| 55 | match_rule = start_cloudflared(tmp_path, config, args) |
| 56 | |
| 57 | assert f"Matched rule #{rule_num}" .encode() in match_rule.stdout |
| 58 | |