Skip to content

Commit 43a2428

Browse files
committed
auto test filters.
1 parent e094c46 commit 43a2428

File tree

7 files changed

+246
-50
lines changed

7 files changed

+246
-50
lines changed

pkg/parser.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,9 @@ func parseiRulesFrom(hr *gatewayv1beta1.HTTPRoute, rlt map[string]interface{}) e
327327
}
328328

329329
// experimental .. definition is not clear yet.
330-
setPath := `set rpath "[HTTP::path]"`
330+
setUri := `set ruri "[HTTP::uri]"`
331331
if rr.Path != nil && rr.Path.ReplaceFullPath != nil {
332-
setPath = fmt.Sprintf(`set rpath "%s"`, *rr.Path.ReplaceFullPath)
332+
setUri = fmt.Sprintf(`set ruri "%s"`, *rr.Path.ReplaceFullPath)
333333
}
334334

335335
setPort := `set rport [TCP::local_port]`
@@ -347,10 +347,10 @@ func parseiRulesFrom(hr *gatewayv1beta1.HTTPRoute, rlt map[string]interface{}) e
347347
%s
348348
%s
349349
%s
350-
set uri $rscheme://$rhostname:$rport$rpath?[HTTP::query]
351-
log local0. "request redirect to $uri"
352-
HTTP::respond %d Location $uri
353-
`, setScheme, setHostName, setPath, setPort, *rr.StatusCode))
350+
set url $rscheme://$rhostname:$rport$ruri
351+
log local0. "request redirect to $url"
352+
HTTP::respond %d Location $url
353+
`, setScheme, setHostName, setUri, setPort, *rr.StatusCode))
354354
}
355355
// <gateway:experimental>
356356
case gatewayv1beta1.HTTPRouteFilterURLRewrite:
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
---
3+
4+
apiVersion: gateway.networking.k8s.io/v1beta1
5+
kind: HTTPRoute
6+
metadata:
7+
name: test-filter-extensionref
8+
spec:
9+
parentRefs:
10+
- name: gateway
11+
sectionName: http
12+
hostnames:
13+
- {{ hostname }}
14+
rules:
15+
- filters:
16+
- type: ExtensionRef
17+
extensionRef:
18+
group: v1
19+
kind: Service
20+
name: test-service
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
---
3+
4+
apiVersion: gateway.networking.k8s.io/v1beta1
5+
kind: HTTPRoute
6+
metadata:
7+
name: test-filter-header
8+
spec:
9+
parentRefs:
10+
- name: gateway
11+
sectionName: http
12+
hostnames:
13+
- {{ hostname }}
14+
rules:
15+
- filters:
16+
- type: RequestHeaderModifier
17+
requestHeaderModifier:
18+
add:
19+
- name: test
20+
value: automation
21+
set:
22+
- name: dev
23+
value: agile
24+
backendRefs:
25+
- name: test-service
26+
port: 80
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
---
3+
4+
apiVersion: gateway.networking.k8s.io/v1beta1
5+
kind: HTTPRoute
6+
metadata:
7+
name: test-filter-requestredirect
8+
spec:
9+
parentRefs:
10+
- name: gateway
11+
sectionName: http
12+
hostnames:
13+
- {{ hostname }}
14+
rules:
15+
- filters:
16+
- type: RequestRedirect
17+
requestRedirect:
18+
scheme: https
19+
hostname: www.example.com
20+
# path:
21+
# type: ReplaceFullPath
22+
# replaceFullPath: /fake
23+
port: 443
24+
statusCode: 301
25+
backendRefs:
26+
- name: test-service
27+
port: 80
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
# ---
3+
4+
# apiVersion: gateway.networking.k8s.io/v1beta1
5+
# kind: HTTPRoute
6+
# metadata:
7+
# name: test-filter-urlrewrite
8+
# spec:
9+
# parentRefs:
10+
# - name: gateway
11+
# sectionName: http
12+
# hostnames:
13+
# - "cafe.example.com"
14+
# rules:
15+
# - matches:
16+
# - path:
17+
# type: PathPrefix
18+
# value: /urlrewrite-test
19+
# - headers:
20+
# - name: test
21+
# value: zongzw
22+
# - name: dev
23+
# value: andrew
24+
# filters:
25+
# - type: URLRewrite
26+
# urlRewrite:
27+
# hostname: www.example.com
28+
# path:
29+
# type: ReplaceFullPath
30+
# replaceFullPath: /fake
31+
# backendRefs:
32+
# - name: tea
33+
# port: 80
34+

tests/templates/testcases.yaml.j2

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
method: GET
1212
queries:
1313
response:
14-
status: 200
14+
status_code: 200
1515
body:
1616
uri: /
1717
queries: {}
@@ -30,7 +30,7 @@
3030
method: GET
3131
queries:
3232
response:
33-
status: 200
33+
status_code: 200
3434
body:
3535
uri: /path-test
3636
queries: {}
@@ -49,7 +49,7 @@
4949
method: OPTIONS
5050
queries:
5151
response:
52-
status: 200
52+
status_code: 200
5353
body:
5454
uri: /
5555
queries: {}
@@ -69,10 +69,62 @@
6969
queries:
7070
test: automation
7171
response:
72-
status: 200
72+
status_code: 200
7373
body:
7474
uri: /
7575
queries:
7676
test: automation
7777
headers:
78-
Content-Type: text/plain
78+
Content-Type: text/plain #}
79+
80+
- name: header filter test
81+
context:
82+
- gateway
83+
- hrs-filters-header
84+
- service
85+
request:
86+
url: http://{{ virtual.ipaddr }}
87+
headers:
88+
Host: {{ hostname }}
89+
method: GET
90+
response:
91+
status_code: 200
92+
body:
93+
uri: /
94+
headers:
95+
Host: {{ hostname }}
96+
test: automation
97+
dev: agile
98+
headers:
99+
Content-Type: text/plain
100+
101+
- name: request-redirect filter test
102+
context:
103+
- gateway
104+
- hrs-filters-request-redirect
105+
- service
106+
request:
107+
url: http://{{ virtual.ipaddr }}
108+
headers:
109+
Host: {{ hostname }}
110+
method: GET
111+
response:
112+
status_code: 301
113+
headers:
114+
Location: https://www.example.com:443/
115+
body: ""
116+
117+
- name: extension-ref filter test
118+
context:
119+
- gateway
120+
- hrs-filters-extensionref
121+
- service
122+
request:
123+
url: http://{{ virtual.ipaddr }}
124+
headers:
125+
Host: {{ hostname }}
126+
method: GET
127+
response:
128+
status_code: 200
129+
body:
130+
uri: /

tests/test.py

Lines changed: 76 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,35 @@ def delete_kube_yaml(name, yaml_file):
8888
else:
8989
ok(name, "%s" % str(cp.stdout, 'utf-8'))
9090

91+
def json_is_included_expectedly(expected, given):
92+
if type(expected) != type(given):
93+
return False
94+
95+
if type(expected) == type({}):
96+
for k, v in expected.items():
97+
actual = given.get(k, None)
98+
if type(v) != type(actual):
99+
return False
100+
if type(v) == type([]):
101+
for n in v:
102+
if not n in actual:
103+
return False
104+
elif type(v) == type({}):
105+
included = json_is_included_expectedly(v, actual)
106+
if not included:
107+
return False
108+
else:
109+
if actual != v:
110+
return False
111+
elif type(expected) == type([]):
112+
for n in expected:
113+
if not n in given:
114+
return False
115+
else:
116+
if expected != given:
117+
return False
118+
return True
119+
91120
def curl_verify(name, req, expected_resp):
92121
method = req.get('method', 'GET')
93122
queries = req.get('queries', {})
@@ -96,32 +125,44 @@ def curl_verify(name, req, expected_resp):
96125
body = req.get('body', {})
97126
expected_status = expected_resp.get('status_code', 200)
98127
expected_headers = expected_resp.get('headers', {})
99-
expected_body_json = expected_resp.get('body', {})
128+
expected_body = expected_resp.get('body', {})
100129
try:
101-
resp = requests.api.request(method=method, url="%s" % url, params=queries, headers=headers, json=body)
102-
if resp.status_code != expected_status:
103-
fail(name, "Status code unexpected: expected: %d, actually: %d" % (expected_status, resp.status_code))
104-
for k, v in expected_headers.items():
105-
if resp.headers[k] != v:
106-
fail(name, "Header unexpected: expected %s => %s, actually %s => %s" % (k, v, k, resp.headers[k]))
130+
warn(name, "requesting: %s" % req)
131+
resp = requests.request(method=method, url="%s" % url, params=queries, headers=headers, json=body, allow_redirects=False)
107132
try:
108-
resp_json = resp.json()
133+
resp_headers = dict(resp.headers)
134+
if type(expected_body) == type({}):
135+
resp_body = resp.json()
136+
else:
137+
resp_body = resp.text
109138
except Exception as e:
110-
fail(name, "Response format unexpected: not json-formated(%s)" % e)
111-
if type(expected_body_json) != type(resp_json):
112-
fail(name, "Response format unexpected: expected: %s, actually: %s" % (type(expected_body_json), type(resp_json)))
113-
if type(expected_body_json) == type([]):
114-
for n in expected_body_json:
115-
if not n in resp_json:
116-
fail(name, "Response body unexpected, missing %s" % n)
117-
elif type(expected_body_json) == type({}):
118-
for k, v in expected_body_json.items():
119-
if v != resp_json[k]:
120-
fail(name, "Response body unexpected: expected %s => %s, actually %s => %s" % (k, v, k, resp_json[k]))
139+
return False, "Response format unexpected: not json-formated(%s)" % e
140+
141+
if resp.status_code != expected_status:
142+
return False, "Status code unexpected: expected: %d, actually: %d" % (expected_status, resp.status_code)
143+
if not json_is_included_expectedly(expected_headers, resp_headers):
144+
return False, "Header unexpected: expected %s, actually %s " % (expected_headers, resp_headers)
145+
146+
if not json_is_included_expectedly(expected_body, resp_body):
147+
return False, "Response body unexpected: expected %s, actually %s" % (expected_body, resp_body)
148+
121149
except Exception as e:
122-
fail(name, "failed to %s to %s: %s" % (method, url, e))
150+
return False, "failed to %s to %s: %s" % (method, url, e)
123151
else:
124-
ok(name, "Successfully verified %s via %s %s" % (name, method, url))
152+
return True, "Successfully verified %s via %s %s" % (name, method, url)
153+
154+
155+
class test_context():
156+
def __init__(self, name, ctx_yamls) -> None:
157+
self.ctxs = ctx_yamls
158+
self.name = name
159+
def __enter__(self):
160+
for ctx in self.ctxs:
161+
gen_kube_yaml(self.name, ctx)
162+
apply_kube_yaml(self.name, '%s/deps/%s.yaml' % (homedir, ctx))
163+
def __exit__(self, exc_type, exc_val, exc_tb):
164+
for ctx in self.ctxs:
165+
delete_kube_yaml(self.name, '%s/deps/%s.yaml' % (homedir, ctx))
125166

126167
os.environ.setdefault('KUBE_CONFIG_FILEPATH', '~/.kube/config')
127168
load_config()
@@ -130,21 +171,17 @@ def curl_verify(name, req, expected_resp):
130171
for case in testcases:
131172
n = case['name']
132173
note(n, "Testing %s" % n)
133-
for ctx in case['context']:
134-
gen_kube_yaml(n, ctx)
135-
apply_kube_yaml(n, '%s/deps/%s.yaml' % (homedir, ctx))
136-
137-
retries = 50
138-
for t in range(retries):
139-
try:
140-
curl_verify(n, case['request'], case['response'])
141-
except Exception as e:
142-
time.sleep(2)
143-
warn(n, "Another retry: %d" % (retries-t))
144-
if t == retries-1:
145-
fail(n, "Timeout for testing... quit.")
146-
else:
147-
break
148-
149-
for ctx in case['context']:
150-
delete_kube_yaml(n, '%s/deps/%s.yaml' % (homedir, ctx))
174+
175+
with test_context(n, case['context']):
176+
retries = 50
177+
for t in range(retries):
178+
(passed, msg) = curl_verify(n, case['request'], case['response'])
179+
if not passed:
180+
time.sleep(2)
181+
warn(n, msg)
182+
warn(n, "Another retry: %d" % (retries-t))
183+
if t == retries-1:
184+
fail(n, "Timeout for testing... quit.")
185+
else:
186+
ok(n, msg)
187+
break

0 commit comments

Comments
 (0)