|
| 1 | +""" |
| 2 | +Conftest for changing targetRef field in policies |
| 3 | +""" |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from testsuite.gateway import GatewayRoute, GatewayListener, Hostname, Exposer |
| 8 | +from testsuite.gateway.gateway_api.gateway import KuadrantGateway |
| 9 | +from testsuite.gateway.gateway_api.hostname import DNSPolicyExposer |
| 10 | +from testsuite.gateway.gateway_api.route import HTTPRoute |
| 11 | +from testsuite.kuadrant.policy.dns import DNSPolicy |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture(scope="module") |
| 15 | +def exposer2(request, cluster) -> Exposer: |
| 16 | + """Second DNSPolicyExposer setup for Gateway 2""" |
| 17 | + exposer = DNSPolicyExposer(cluster) |
| 18 | + request.addfinalizer(exposer.delete) |
| 19 | + exposer.commit() |
| 20 | + return exposer |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture(scope="module") |
| 24 | +def base_domain2(exposer2): |
| 25 | + """Returns preconfigured base domain for the second Gateway""" |
| 26 | + return exposer2.base_domain |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture(scope="module") |
| 30 | +def wildcard_domain2(base_domain2): |
| 31 | + """Wildcard domain for Gateway 2""" |
| 32 | + return f"*.{base_domain2}" |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture(scope="module") |
| 36 | +def gateway(request, cluster, blame, wildcard_domain, module_label): |
| 37 | + """Create and configure Gateway 1""" |
| 38 | + gw = KuadrantGateway.create_instance(cluster, blame("gw"), {"app": module_label}) |
| 39 | + gw.add_listener(GatewayListener(hostname=wildcard_domain)) |
| 40 | + request.addfinalizer(gw.delete) |
| 41 | + gw.commit() |
| 42 | + gw.wait_for_ready() |
| 43 | + return gw |
| 44 | + |
| 45 | + |
| 46 | +@pytest.fixture(scope="module") |
| 47 | +def gateway2(request, cluster, blame, wildcard_domain2, module_label): |
| 48 | + """Create and configure Gateway 2""" |
| 49 | + gw = KuadrantGateway.create_instance(cluster, blame("gw2"), {"app": module_label}) |
| 50 | + gw.add_listener(GatewayListener(hostname=wildcard_domain2)) |
| 51 | + request.addfinalizer(gw.delete) |
| 52 | + gw.commit() |
| 53 | + gw.wait_for_ready() |
| 54 | + return gw |
| 55 | + |
| 56 | + |
| 57 | +@pytest.fixture(scope="module") |
| 58 | +def hostname2(gateway2, exposer2, blame) -> Hostname: |
| 59 | + """Expose Hostname for Gateway 2""" |
| 60 | + hostname = exposer2.expose_hostname(blame("hostname2"), gateway2) |
| 61 | + return hostname |
| 62 | + |
| 63 | + |
| 64 | +@pytest.fixture(scope="module") |
| 65 | +def route2(request, gateway2, blame, hostname2, module_label, backend) -> GatewayRoute: |
| 66 | + """Create and configure Route 2""" |
| 67 | + route = HTTPRoute.create_instance(gateway2.cluster, blame("route2"), gateway2, {"app": module_label}) |
| 68 | + route.add_hostname(hostname2.hostname) |
| 69 | + route.add_backend(backend) |
| 70 | + request.addfinalizer(route.delete) |
| 71 | + route.commit() |
| 72 | + route.wait_for_ready() |
| 73 | + return route |
| 74 | + |
| 75 | + |
| 76 | +@pytest.fixture(scope="module") |
| 77 | +def client2(route2, hostname2): # pylint: disable=unused-argument |
| 78 | + """Returns httpx client for Gateway 2""" |
| 79 | + client = hostname2.client() |
| 80 | + yield client |
| 81 | + client.close() |
| 82 | + |
| 83 | + |
| 84 | +@pytest.fixture(scope="module") |
| 85 | +def dns_policy2(blame, gateway2, module_label, dns_provider_secret, request): |
| 86 | + """DNSPolicy fixture for Gateway 2""" |
| 87 | + policy = DNSPolicy.create_instance( |
| 88 | + gateway2.cluster, blame("dns2"), gateway2, dns_provider_secret, labels={"app": module_label} |
| 89 | + ) |
| 90 | + request.addfinalizer(policy.delete) |
| 91 | + policy.commit() |
| 92 | + policy.wait_for_ready() |
| 93 | + return policy |
| 94 | + |
| 95 | + |
| 96 | +@pytest.fixture(scope="session") |
| 97 | +def change_target_ref(): |
| 98 | + """Function that changes targetRef of given policy""" |
| 99 | + |
| 100 | + def _change_targetref(policy, gateway): |
| 101 | + def _apply_target_ref(apiobj): |
| 102 | + apiobj.model.spec.targetRef = gateway.reference |
| 103 | + return True |
| 104 | + |
| 105 | + policy.modify_and_apply(_apply_target_ref) |
| 106 | + policy.wait_for_ready() |
| 107 | + |
| 108 | + return _change_targetref |
0 commit comments