2021-03-05 22:50:11 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
from util import start_cloudflared
|
|
|
|
|
2021-03-08 15:42:49 +00:00
|
|
|
|
2021-03-05 22:50:11 +00:00
|
|
|
class TestConfig:
|
|
|
|
# tmp_path is a fixture provides a temporary directory unique to the test invocation
|
2021-03-08 14:09:10 +00:00
|
|
|
def test_validate_ingress_rules(self, tmp_path, component_tests_config):
|
|
|
|
extra_config = {
|
2021-03-05 22:50:11 +00:00
|
|
|
'ingress': [
|
|
|
|
{
|
|
|
|
"hostname": "example.com",
|
|
|
|
"service": "https://localhost:8000",
|
|
|
|
"originRequest": {
|
|
|
|
"originServerName": "test.example.com",
|
|
|
|
"caPool": "/etc/certs/ca.pem"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"hostname": "api.example.com",
|
|
|
|
"path": "login",
|
|
|
|
"service": "https://localhost:9000",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"hostname": "wss.example.com",
|
|
|
|
"service": "wss://localhost:8000",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"hostname": "ssh.example.com",
|
|
|
|
"service": "ssh://localhost:8000",
|
|
|
|
},
|
|
|
|
{"service": "http_status:404"}
|
|
|
|
],
|
|
|
|
}
|
2021-03-11 13:49:09 +00:00
|
|
|
config = component_tests_config(extra_config)
|
2021-03-05 22:50:11 +00:00
|
|
|
validate_args = ["ingress", "validate"]
|
2021-03-11 13:49:09 +00:00
|
|
|
_ = start_cloudflared(tmp_path, config, validate_args)
|
2021-03-08 14:09:10 +00:00
|
|
|
|
2021-03-11 13:49:09 +00:00
|
|
|
self.match_rule(tmp_path, config,
|
2021-03-08 15:42:49 +00:00
|
|
|
"http://example.com/index.html", 1)
|
2021-03-11 13:49:09 +00:00
|
|
|
self.match_rule(tmp_path, config,
|
2021-03-08 15:42:49 +00:00
|
|
|
"https://example.com/index.html", 1)
|
2021-03-11 13:49:09 +00:00
|
|
|
self.match_rule(tmp_path, config,
|
2021-03-08 15:42:49 +00:00
|
|
|
"https://api.example.com/login", 2)
|
2021-03-11 13:49:09 +00:00
|
|
|
self.match_rule(tmp_path, config,
|
2021-03-08 15:42:49 +00:00
|
|
|
"https://wss.example.com", 3)
|
2021-03-11 13:49:09 +00:00
|
|
|
self.match_rule(tmp_path, config,
|
2021-03-08 15:42:49 +00:00
|
|
|
"https://ssh.example.com", 4)
|
2021-03-11 13:49:09 +00:00
|
|
|
self.match_rule(tmp_path, config,
|
2021-03-08 15:42:49 +00:00
|
|
|
"https://api.example.com", 5)
|
2021-03-05 22:50:11 +00:00
|
|
|
|
|
|
|
# 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
|
2021-03-08 15:42:49 +00:00
|
|
|
|
2021-03-11 13:49:09 +00:00
|
|
|
def match_rule(self, tmp_path, config, url, rule_num):
|
2021-03-05 22:50:11 +00:00
|
|
|
args = ["ingress", "rule", url]
|
2021-03-11 13:49:09 +00:00
|
|
|
match_rule = start_cloudflared(tmp_path, config, args)
|
2021-03-05 22:50:11 +00:00
|
|
|
|
2021-03-08 15:42:49 +00:00
|
|
|
assert f"Matched rule #{rule_num}" .encode() in match_rule.stdout
|