-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers_test.py
108 lines (94 loc) · 2.45 KB
/
helpers_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from tiltfile_runner import run_tiltfile_func
import unittest
import pytest
def gen_service(name, ports=[("test", 1234)], namespace="dave"):
port_str = '\n'.join([f"""
- name: {port_name}
port: {port}
protocol: TCP
targetPort: test
""" for port_name, port in ports])
return f"""
apiVersion: apps/v1
kind: Service
metadata:
name: {name}
namespace: test
spec:
ports:
{port_str}
"""
class GetServiceUrlTest(unittest.TestCase):
def test_it_finds_single_service_and_port(self):
url = run_tiltfile_func("helpers/Tiltfile", "get_service_url",
yaml=f"""
{gen_service("dave")}
"""
)
assert url == "dave.test:1234"
def test_it_fails_if_multiple_services(self):
with pytest.raises(Exception, match=r".*Tiltfile failed.*"):
run_tiltfile_func("helpers/Tiltfile", "get_service_url",
yaml=f"""
{gen_service("dave")}
---
{gen_service("dave2")}
"""
)
def test_it_fails_if_no_services_found(self):
with pytest.raises(Exception, match=r".*Tiltfile failed.*"):
run_tiltfile_func("helpers/Tiltfile", "get_service_url",
yaml=f"""
apiVersion: apps/v1
kind: Deployment
metadata:
name: dave
namespace: test
"""
)
def test_it_fails_if_multiple_ports(self):
with pytest.raises(Exception, match=r".*Tiltfile failed.*"):
run_tiltfile_func("helpers/Tiltfile", "get_service_url",
yaml=f"""
{gen_service("dave", [("test", 1234), ("test2", 5678)])}
"""
)
def test_it_fails_if_no_ports(self):
with pytest.raises(Exception, match=r".*Tiltfile failed.*"):
run_tiltfile_func("helpers/Tiltfile", "get_service_url",
yaml=f"""
apiVersion: apps/v1
kind: Service
metadata:
name: "dave"
namespace: test
spec:
ports: []
"""
)
def test_it_can_match_service_on_name(self):
url = run_tiltfile_func("helpers/Tiltfile", "get_service_url",
service_name="dave",
yaml=f"""
{gen_service("dave")}
---
{gen_service("dave2")}
"""
)
assert url == "dave.test:1234"
def test_it_can_match_port_on_name(self):
url = run_tiltfile_func("helpers/Tiltfile", "get_service_url",
port="port1",
yaml=f"""
{gen_service("dave", [("port1", 1234), ("port2", 5678)])}
"""
)
assert url == "dave.test:1234"
def test_it_can_match_port_on_number(self):
url = run_tiltfile_func("helpers/Tiltfile", "get_service_url",
port=1234,
yaml=f"""
{gen_service("dave", [("port1", 1234), ("port2", 5678)])}
"""
)
assert url == "dave.test:1234"