Skip to content

Commit

Permalink
Extend rewrite URI tests for VS and VSRs
Browse files Browse the repository at this point in the history
- Combine the VS and VSR tests into a single one.
- Extend the tests to cover the bug fix.
  • Loading branch information
pleshakov committed Dec 15, 2021
1 parent 25baf36 commit 4666d46
Show file tree
Hide file tree
Showing 14 changed files with 268 additions and 325 deletions.
32 changes: 32 additions & 0 deletions tests/data/rewrites/hello.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello
spec:
replicas: 1
selector:
matchLabels:
app: hello
template:
metadata:
labels:
app: hello
spec:
containers:
- name: hello
image: nginxdemos/nginx-hello:plain-text
ports:
- containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: hello-svc
spec:
ports:
- port: 80
targetPort: 8080
protocol: TCP
name: http
selector:
app: hello
17 changes: 17 additions & 0 deletions tests/data/rewrites/virtual-server-parent.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
apiVersion: k8s.nginx.org/v1
kind: VirtualServer
metadata:
name: rewrite-parent
spec:
host: vsr.example.com
upstreams:
- name: hello
service: hello-svc
port: 80
routes:
- path: /
route: prefixes
- path: ~ /regex1/?(.*)
route: regex-1
- path: ~ /regex2/?(.*)
route: regex-2
44 changes: 44 additions & 0 deletions tests/data/rewrites/virtual-server-rewrite.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
apiVersion: k8s.nginx.org/v1
kind: VirtualServer
metadata:
name: rewrite
spec:
host: vs.example.com
upstreams:
- name: hello
service: hello-svc
port: 80
routes:
- path: /backend1/
action:
proxy:
upstream: hello
rewritePath: /
- path: /backend2
action:
proxy:
upstream: hello
rewritePath: /backend2_1
- path: /match/
matches:
- conditions:
- cookie: user
value: john
action:
proxy:
upstream: hello
rewritePath: /user/john/
action:
proxy:
upstream: hello
rewritePath: /
- path: ~ /regex1/?(.*)
action:
proxy:
upstream: hello
rewritePath: /$1
- path: ~ /regex2/?(.*)
action:
proxy:
upstream: hello
rewritePath: /$1
34 changes: 34 additions & 0 deletions tests/data/rewrites/virtual-server-route-prefixes.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
apiVersion: k8s.nginx.org/v1
kind: VirtualServerRoute
metadata:
name: prefixes
spec:
host: vsr.example.com
upstreams:
- name: hello
service: hello-svc
port: 80
subroutes:
- path: /backend1/
action:
proxy:
upstream: hello
rewritePath: /
- path: /backend2
action:
proxy:
upstream: hello
rewritePath: /backend2_1
- path: /match/
matches:
- conditions:
- cookie: user
value: john
action:
proxy:
upstream: hello
rewritePath: /user/john/
action:
proxy:
upstream: hello
rewritePath: /
16 changes: 16 additions & 0 deletions tests/data/rewrites/virtual-server-route-regex1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: k8s.nginx.org/v1
kind: VirtualServerRoute
metadata:
name: regex-1
spec:
host: vsr.example.com
upstreams:
- name: hello
service: hello-svc
port: 80
subroutes:
- path: ~ /regex1/?(.*)
action:
proxy:
upstream: hello
rewritePath: /$1
16 changes: 16 additions & 0 deletions tests/data/rewrites/virtual-server-route-regex2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: k8s.nginx.org/v1
kind: VirtualServerRoute
metadata:
name: regex-2
spec:
host: vsr.example.com
upstreams:
- name: hello
service: hello-svc
port: 80
subroutes:
- path: ~ /regex2/?(.*)
action:
proxy:
upstream: hello
rewritePath: /$1
20 changes: 0 additions & 20 deletions tests/data/virtual-server-rewrites/standard/virtual-server.yaml

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

16 changes: 0 additions & 16 deletions tests/data/virtual-server-route-rewrites/route-single-prefix.yaml

This file was deleted.

109 changes: 109 additions & 0 deletions tests/suite/test_rewrites.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import pytest
import requests

from settings import TEST_DATA
from suite.resources_utils import create_items_from_yaml, wait_until_all_pods_are_ready, \
delete_items_from_yaml, wait_before_test
from suite.vs_vsr_resources_utils import (
create_virtual_server_from_yaml, delete_virtual_server, create_v_s_route_from_yaml, delete_v_s_route,
)

hello_app_yaml = f"{TEST_DATA}/rewrites/hello.yaml"


@pytest.fixture(scope="class")
def hello_app(request, kube_apis, test_namespace):
create_items_from_yaml(kube_apis, hello_app_yaml, test_namespace)
wait_until_all_pods_are_ready(kube_apis.v1, test_namespace)

def fin():
delete_items_from_yaml(kube_apis, hello_app_yaml, test_namespace)

request.addfinalizer(fin)


class RewritesSetup:
def __init__(self, public_endpoint):
self.url_base = f"http://{public_endpoint.public_ip}:{public_endpoint.port}"


vs_yaml = f"{TEST_DATA}/rewrites/virtual-server-rewrite.yaml"


@pytest.fixture(scope="class")
def vs_rewrites_setup(request, kube_apis, test_namespace, hello_app, ingress_controller_endpoint,
crd_ingress_controller):
vs = create_virtual_server_from_yaml(kube_apis.custom_objects, vs_yaml, test_namespace)
wait_before_test()

def fin():
delete_virtual_server(kube_apis.custom_objects, vs, test_namespace)

request.addfinalizer(fin)

return RewritesSetup(ingress_controller_endpoint)


vs_parent_yaml = f"{TEST_DATA}/rewrites/virtual-server-parent.yaml"
vsr_prefixes_yaml = f"{TEST_DATA}/rewrites/virtual-server-route-prefixes.yaml"
vsr_regex1_yaml = f"{TEST_DATA}/rewrites/virtual-server-route-regex1.yaml"
vsr_regex2_yaml = f"{TEST_DATA}/rewrites/virtual-server-route-regex2.yaml"


@pytest.fixture(scope="class")
def vsr_rewrites_setup(request, kube_apis, test_namespace, hello_app, ingress_controller_endpoint,
crd_ingress_controller):
vs_parent = create_virtual_server_from_yaml(kube_apis.custom_objects, vs_parent_yaml, test_namespace)
vsr_prefixes = create_v_s_route_from_yaml(kube_apis.custom_objects, vsr_prefixes_yaml, test_namespace)
vsr_regex1 = create_v_s_route_from_yaml(kube_apis.custom_objects, vsr_regex1_yaml, test_namespace)
vsr_regex2 = create_v_s_route_from_yaml(kube_apis.custom_objects, vsr_regex2_yaml, test_namespace)
wait_before_test()

def fin():
delete_virtual_server(kube_apis.custom_objects, vs_parent, test_namespace)
delete_v_s_route(kube_apis.custom_objects, vsr_prefixes, test_namespace)
delete_v_s_route(kube_apis.custom_objects, vsr_regex1, test_namespace)
delete_v_s_route(kube_apis.custom_objects, vsr_regex2, test_namespace)

request.addfinalizer(fin)

return RewritesSetup(ingress_controller_endpoint)


test_data = [("/backend1/", {"arg": "value"}, {}, "/?arg=value"),
("/backend1/abc", {"arg": "value"}, {}, "/abc?arg=value"),
("/backend2", {"arg": "value"}, {}, "/backend2_1?arg=value"),
("/backend2/", {"arg": "value"}, {}, "/backend2_1/?arg=value"),
("/backend2/abc", {"arg": "value"}, {}, "/backend2_1/abc?arg=value"),
("/match/", {"arg": "value"}, {}, "/?arg=value"),
("/match/abc", {"arg": "value"}, {}, "/abc?arg=value"),
("/match/", {"arg": "value"}, {"user": "john"}, "/user/john/?arg=value"),
("/match/abc", {"arg": "value"}, {"user": "john"}, "/user/john/abc?arg=value"),
("/regex1/", {"arg": "value"}, {}, "/?arg=value"),
("/regex1//", {"arg": "value"}, {}, "/?arg=value"),
("/regex2/abc", {"arg": "value"}, {}, "/abc?arg=value")]


@pytest.mark.parametrize('crd_ingress_controller', [({'type': 'complete'})], indirect=True)
class TestRewrites:
@pytest.mark.vs
@pytest.mark.parametrize("path,args,cookies,expected", test_data)
def test_vs_rewrite(self, vs_rewrites_setup, path, args, cookies, expected):
"""
Test VirtualServer URI rewrite
"""
url = vs_rewrites_setup.url_base + path
resp = requests.get(url, headers={"host": "vs.example.com"}, params=args, cookies=cookies)

assert f"URI: {expected}\nRequest" in resp.text

@pytest.mark.vsr
@pytest.mark.parametrize("path,args,cookies,expected", test_data)
def test_vsr_rewrite(self, vsr_rewrites_setup, path, args, cookies, expected):
"""
Test VirtualServerRoute URI rewrite
"""
url = vsr_rewrites_setup.url_base + path
resp = requests.get(url, headers={"host": "vsr.example.com"}, params=args, cookies=cookies)

assert f"URI: {expected}\nRequest" in resp.text
Loading

0 comments on commit 4666d46

Please sign in to comment.