-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathconftest.py
More file actions
182 lines (154 loc) · 6 KB
/
conftest.py
File metadata and controls
182 lines (154 loc) · 6 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import os
import time
from pathlib import Path
import pytest
import requests
from kubernetes import client, config
from tests.integration.rest_api.support import (
applyFeastProject,
create_feast_project,
create_namespace,
create_route,
delete_namespace,
deploy_and_validate_pod,
execPodCommand,
get_pod_name_by_prefix,
run_kubectl_command,
validate_feature_store_cr_status,
)
class FeastRestClient:
def __init__(self, base_url):
self.base_url = base_url.rstrip("/")
self.api_prefix = "/api/v1"
def _build_url(self, endpoint):
if not endpoint.startswith("/"):
endpoint = "/" + endpoint
return f"{self.base_url}{self.api_prefix}{endpoint}"
def get(self, endpoint, params=None):
params = params or {}
params.setdefault("allow_cache", "false")
url = self._build_url(endpoint)
return requests.get(url, params=params, verify=False)
def _wait_for_http_ready(
route_url: str,
timeout: int = 300,
interval: int = 5,
initial_delay: int = 30,
) -> None:
"""
Poll the HTTP endpoint until it returns a non-502 response.
After Pod/CR readiness is confirmed, the backend behind the ingress may
still be initializing. This helper avoids the race condition where tests
start before the Feast server is ready, causing all requests to return 502.
"""
health_url = f"{route_url}/api/v1/projects"
last_status = None
if initial_delay > 0:
print(
f"\n Waiting {initial_delay}s for backend to start after apply/dataset creation..."
)
time.sleep(initial_delay)
deadline = time.time() + timeout
print(
f"\n Waiting for HTTP endpoint to become ready (timeout={timeout}s): {health_url}"
)
while time.time() < deadline:
try:
resp = requests.get(health_url, timeout=10, verify=False)
last_status = resp.status_code
if resp.status_code != 502:
print(f" HTTP endpoint is ready (status={resp.status_code})")
return
print(
f" HTTP endpoint returned {resp.status_code}, retrying in {interval}s..."
)
except requests.exceptions.RequestException as exc:
last_status = str(exc)
print(f" HTTP request failed ({exc}), retrying in {interval}s...")
time.sleep(interval)
raise RuntimeError(
f"HTTP endpoint {health_url} did not become ready within {timeout}s "
f"(last status: {last_status})"
)
@pytest.fixture(scope="session")
def feast_rest_client():
# Load kubeconfig and initialize Kubernetes client
config.load_kube_config()
api_instance = client.CoreV1Api()
# Get the directory containing this conftest.py file
test_dir = Path(__file__).parent
resource_dir = test_dir / "resource"
# Constants and environment values
namespace = "test-ns-feast-rest"
credit_scoring = "credit-scoring"
driver_ranking = "driver-ranking"
# Registry REST service name created by the operator for credit-scoring (kind and OpenShift)
registry_rest_service = "feast-credit-scoring-registry-rest"
run_on_openshift = os.getenv("RUN_ON_OPENSHIFT_CI", "false").lower() == "true"
# Create test namespace
create_namespace(api_instance, namespace)
try:
# Deploy dependencies (same for kind and OpenShift)
deploy_and_validate_pod(
namespace, str(resource_dir / "redis.yaml"), "app=redis"
)
deploy_and_validate_pod(
namespace, str(resource_dir / "postgres.yaml"), "app=postgres"
)
# Create and validate FeatureStore CRs (SQL registry, same as kind)
create_feast_project(
str(resource_dir / "feast_config_credit_scoring.yaml"),
namespace,
credit_scoring,
)
validate_feature_store_cr_status(namespace, credit_scoring)
create_feast_project(
str(resource_dir / "feast_config_driver_ranking.yaml"),
namespace,
driver_ranking,
)
validate_feature_store_cr_status(namespace, driver_ranking)
if run_on_openshift:
# OpenShift: expose registry REST via route (no nginx ingress)
route_url = create_route(namespace, credit_scoring, registry_rest_service)
else:
# Kind: deploy nginx ingress and get route URL
run_kubectl_command(
[
"apply",
"-f",
str(resource_dir / "feast-registry-nginx.yaml"),
"-n",
namespace,
]
)
ingress_host = run_kubectl_command(
[
"get",
"ingress",
"feast-registry-ingress",
"-n",
namespace,
"-o",
"jsonpath={.spec.rules[0].host}",
]
)
route_url = f"http://{ingress_host}"
# Apply feast projects
applyFeastProject(namespace, credit_scoring)
applyFeastProject(namespace, driver_ranking)
# Create Saved Datasets and Permissions
pod_name = get_pod_name_by_prefix(namespace, credit_scoring)
execPodCommand(namespace, pod_name, ["python", "create_ui_visible_datasets.py"])
execPodCommand(namespace, pod_name, ["python", "permissions_apply.py"])
if not route_url:
raise RuntimeError("Route URL could not be fetched.")
# Wait for the HTTP endpoint to become ready before running tests.
# Pod/CR readiness does not guarantee the backend is serving traffic;
# the ingress may return 502 while the Feast server is still starting.
_wait_for_http_ready(route_url)
print(f"\n Connected to Feast REST at: {route_url}")
yield FeastRestClient(route_url)
finally:
print(f"\n Deleting namespace: {namespace}")
delete_namespace(api_instance, namespace)