4
4
5
5
import pytest
6
6
7
- from testsuite .gateway import TLSGatewayListener , GatewayRoute
8
- from testsuite .gateway . envoy . route import EnvoyVirtualRoute
7
+ from testsuite .backend . httpbin import Httpbin
8
+ from testsuite .gateway import GatewayRoute , GatewayListener , Hostname
9
9
from testsuite .gateway .gateway_api .gateway import KuadrantGateway
10
10
from testsuite .gateway .gateway_api .route import HTTPRoute
11
+ from testsuite .kuadrant .policy .dns import DNSPolicy
11
12
from testsuite .kuadrant .policy .rate_limit import Limit , RateLimitPolicy
12
13
14
+ pytestmark = [pytest .mark .kuadrant_only ]
15
+
13
16
14
17
@pytest .fixture (scope = "module" )
15
18
def wildcard_domain2 (base_domain ):
16
19
"""Wildcard domain for Gateway B"""
17
- return f"*.{ base_domain } -b"
20
+ return f"*.{ base_domain } "
21
+
22
+
23
+ @pytest .fixture (scope = "module" )
24
+ def gateway (request , kuadrant , cluster , blame , wildcard_domain , module_label ): # pylint: disable=unused-argument
25
+ """Create and configure Gateway A"""
26
+ # Added this gateway because I couldn't get the tests to pass or work as -
27
+ # expected using the existing conftest version
28
+ gw = KuadrantGateway .create_instance (cluster , blame ("gw" ), {"app" : module_label })
29
+ gw .add_listener (GatewayListener (hostname = wildcard_domain ))
30
+ request .addfinalizer (gw .delete )
31
+ gw .commit ()
32
+ gw .wait_for_ready ()
33
+ return gw
18
34
19
35
20
36
@pytest .fixture (scope = "module" )
21
- def gateway_b (request , cluster , blame , wildcard_domain2 , module_label ): # pylint: disable=unused-argument
37
+ def gateway_b (request , kuadrant , cluster , blame , wildcard_domain2 , module_label ): # pylint: disable=unused-argument
22
38
"""Create and configure Gateway B"""
23
- gateway_name = blame ("gw-b" )
24
- gw = KuadrantGateway .create_instance (cluster , gateway_name , {"app" : module_label })
25
- gw .add_listener (TLSGatewayListener (hostname = wildcard_domain2 , gateway_name = gateway_name ))
39
+ gw = KuadrantGateway .create_instance (cluster , blame ("gw-b" ), {"app" : module_label })
40
+ gw .add_listener (GatewayListener (hostname = wildcard_domain2 ))
26
41
request .addfinalizer (gw .delete )
27
42
gw .commit ()
28
43
gw .wait_for_ready ()
29
44
return gw
30
45
31
46
32
47
@pytest .fixture (scope = "module" )
33
- def route_b (request , kuadrant , wildcard_domain2 , gateway_b , blame , backend , module_label ) -> GatewayRoute :
48
+ def hostname_b (gateway_b , exposer , blame ) -> Hostname :
49
+ """Expose Hostname for Gateway B"""
50
+ hostname = exposer .expose_hostname (blame ("hostname-b" ), gateway_b )
51
+ return hostname
52
+
53
+
54
+ @pytest .fixture (scope = "session" )
55
+ def backend_b (request , cluster , blame , label , testconfig ): # pylint: disable=unused-argument
56
+ """Deploys Httpbin backend"""
57
+ # Added to resolve backend errors I was seeing in Gateway B / Route B YAML files
58
+ image = testconfig ["httpbin" ]["image" ]
59
+ httpbin = Httpbin (cluster , blame ("httpbin" ), label , image )
60
+ httpbin .commit ()
61
+ return httpbin
62
+
63
+
64
+ @pytest .fixture (scope = "module" )
65
+ def route_b (
66
+ request , gateway_b , blame , hostname_b , module_label , backend_b
67
+ ) -> GatewayRoute : # pylint: disable=unused-argument
34
68
"""Create and configure Route B"""
35
- if kuadrant :
36
- route = HTTPRoute .create_instance (gateway_b .cluster , blame ("route-b" ), gateway_b , {"app" : module_label })
37
- else :
38
- route = EnvoyVirtualRoute .create_instance (gateway_b .cluster , blame ("route-b" ), gateway_b )
39
- route .add_hostname (wildcard_domain2 )
40
- route .add_backend (backend )
69
+ route = HTTPRoute .create_instance (gateway_b .cluster , blame ("route-b" ), gateway_b , {"app" : module_label })
70
+ route .add_hostname (hostname_b .hostname )
71
+ route .add_backend (backend_b )
41
72
request .addfinalizer (route .delete )
42
73
route .commit ()
74
+ route .wait_for_ready ()
43
75
return route
44
76
45
77
46
78
@pytest .fixture (scope = "module" )
47
- def rate_limit_policy (request , cluster , blame , module_label , gateway ):
79
+ def client_b (route_b , hostname_b ): # pylint: disable=unused-argument
80
+ """Returns httpx client for Gateway B"""
81
+ client = hostname_b .client ()
82
+ yield client
83
+ client .close ()
84
+
85
+
86
+ @pytest .fixture (scope = "module" )
87
+ def rate_limit_policy (request , cluster , blame , module_label , gateway , route_b ): # pylint: disable=unused-argument
48
88
"""RateLimitPolicy for testing"""
49
89
policy = RateLimitPolicy .create_instance (cluster , blame ("limit" ), gateway , labels = {"testRun" : module_label })
50
90
policy .add_limit ("basic" , [Limit (5 , "10s" )])
@@ -53,65 +93,55 @@ def rate_limit_policy(request, cluster, blame, module_label, gateway):
53
93
return policy
54
94
55
95
96
+ @pytest .fixture (scope = "module" )
97
+ def dns_policy_b (blame , gateway_b , module_label , dns_provider_secret , request ):
98
+ """DNSPolicy fixture for Gateway B"""
99
+ # Added to resolve DNS errors I was seeing in Gateway B / Route B YAML files
100
+ policy = DNSPolicy .create_instance (
101
+ gateway_b .cluster , blame ("dns-b" ), gateway_b , dns_provider_secret , labels = {"app" : module_label }
102
+ )
103
+ request .addfinalizer (policy .delete )
104
+ policy .commit ()
105
+ policy .wait_for_ready ()
106
+ return policy
107
+
108
+
56
109
def test_update_ratelimit_policy_target_ref (
57
- gateway , gateway_b , rate_limit_policy , client , auth , route_b
110
+ gateway , gateway_b , rate_limit_policy , client , client_b , auth , blame , dns_policy_b
58
111
): # pylint: disable=unused-argument
59
112
"""Test updating the targetRef of a RateLimitPolicy from Gateway A to Gateway B"""
60
- initial_target_ref = rate_limit_policy .model ["spec" ]["targetRef" ]["name" ]
61
- assert (
62
- initial_target_ref == gateway .model .metadata .name
63
- ), f"Initial targetRef mismatch: expected { gateway .model .metadata .name } , got { initial_target_ref } "
64
-
65
- response = client .get ("/get" , auth = auth )
66
- assert response .status_code == 200
113
+ # Kept auth=auth as removing it results in an 'HTTP 401 Unauthorized' error
114
+ responses = client .get_many ("/get" , 5 , auth = auth )
115
+ responses .assert_all (status_code = 200 )
116
+ assert client .get ("/get" , auth = auth ).status_code == 429
67
117
68
- rate_limit_policy .wait_for_ready ()
69
- rate_limit_policy .refresh ()
70
-
71
- rate_limit_policy .model ["spec" ]["targetRef" ]["name" ] = gateway_b .model .metadata .name
118
+ rate_limit_policy .refresh ().model .spec .targetRef .name = gateway_b .model .metadata .name
72
119
res = rate_limit_policy .apply ()
73
120
assert res .status () == 0 , res .err ()
121
+ rate_limit_policy .wait_for_ready ()
74
122
75
- rate_limit_policy .refresh ()
76
- updated_target_ref = rate_limit_policy .model ["spec" ]["targetRef" ]["name" ]
77
- assert (
78
- updated_target_ref == gateway_b .model .metadata .name
79
- ), f"Updated targetRef mismatch: expected { gateway_b .model .metadata .name } , got { updated_target_ref } "
80
-
81
- response = client .get ("/get" , auth = auth )
82
- assert response .status_code == 200
123
+ responses = client_b .get_many ("/get" , 5 , auth = auth )
124
+ responses .assert_all (status_code = 200 )
125
+ assert client_b .get ("/get" , auth = auth ).status_code == 429
83
126
84
127
85
128
def test_update_auth_policy_target_ref (
86
- gateway , gateway_b , authorization , client , auth , route_b
129
+ gateway , gateway_b , authorization , client , client_b , auth , blame , dns_policy_b
87
130
): # pylint: disable=unused-argument
88
131
"""Test updating the targetRef of an AuthPolicy from Gateway A to Gateway B"""
89
- # Update targetRef of the AuthPolicy to point to gateway A
90
- authorization .model [ " spec" ][ " targetRef" ] = gateway .reference
132
+ # Overwriting this because the higher-level conftest sets a wrong targetRef for this tests purpose
133
+ authorization .model . spec . targetRef = gateway .reference
91
134
authorization .apply ()
92
135
authorization .wait_for_ready ()
93
136
authorization .refresh ()
94
137
95
- initial_target_ref = authorization .model ["spec" ]["targetRef" ]["name" ]
96
- assert (
97
- initial_target_ref == gateway .model .metadata .name
98
- ), f"Initial targetRef mismatch: expected { gateway .model .metadata .name } , got { initial_target_ref } "
99
-
100
138
response = client .get ("/get" , auth = auth )
101
139
assert response .status_code == 200
102
140
103
- authorization .wait_for_ready ()
104
- authorization .refresh ()
105
-
106
- authorization .model ["spec" ]["targetRef" ] = gateway_b .reference
141
+ authorization .refresh ().model .spec .targetRef = gateway_b .reference
107
142
res = authorization .apply ()
108
143
assert res .status () == 0 , res .err ()
144
+ authorization .wait_for_ready ()
109
145
110
- authorization .refresh ()
111
- updated_target_ref = authorization .model ["spec" ]["targetRef" ]["name" ]
112
- assert (
113
- updated_target_ref == gateway_b .model .metadata .name
114
- ), f"Updated targetRef mismatch: expected { gateway_b .model .metadata .name } , got { updated_target_ref } "
115
-
116
- response = client .get ("/get" , auth = auth )
146
+ response = client_b .get ("/get" , auth = auth )
117
147
assert response .status_code == 200
0 commit comments