Skip to content

Commit 83260cc

Browse files
authored
refactor(conformance) merge similar utility functions. (#1055)
* merge similar utility functions. * update HTTPRouteMultipleRulesDifferentPools. * fix util function.
1 parent b7c839c commit 83260cc

7 files changed

+99
-197
lines changed

conformance/tests/basic/epp_unavailable_fail_open.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,14 @@ var EppUnAvailableFailOpen = suite.ConformanceTest{
6767
k8sutils.InferencePoolMustBeAcceptedByParent(t, s.Client, poolNN)
6868
gwAddr := k8sutils.GetGatewayEndpoint(t, s.Client, s.TimeoutConfig, gatewayNN)
6969

70-
pods, err := k8sutils.GetPodsWithLabel(t, s.Client, appBackendNamespace, backendPodLabels)
70+
pods, err := k8sutils.GetPodsWithLabel(t, s.Client, appBackendNamespace, backendPodLabels, s.TimeoutConfig)
7171
require.NoError(t, err, "Failed to get backend pods")
7272
require.Len(t, pods, expectedPodReplicas, "Expected to find %d backend pod, but found %d.", expectedPodReplicas, len(pods))
7373

7474
targetPodIP := pods[0].Status.PodIP
7575
t.Run("Phase 1: Verify baseline connectivity with EPP available", func(t *testing.T) {
7676
t.Log("Sending request to ensure the Gateway and EPP are working correctly...")
77-
trafficutils.MakeRequestWithRequestParamAndExpectSuccess(
77+
trafficutils.MakeRequestAndExpectSuccess(
7878
t,
7979
s.RoundTripper,
8080
s.TimeoutConfig,
@@ -97,7 +97,7 @@ var EppUnAvailableFailOpen = suite.ConformanceTest{
9797
require.NoError(t, deleteErr, "Failed to delete the EPP deployment")
9898

9999
t.Log("Sending request again, expecting success to verify fail-open...")
100-
trafficutils.MakeRequestWithRequestParamAndExpectSuccess(
100+
trafficutils.MakeRequestAndExpectSuccess(
101101
t,
102102
s.RoundTripper,
103103
s.TimeoutConfig,

conformance/tests/basic/gateway_following_epp_routing.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ var GatewayFollowingEPPRouting = suite.ConformanceTest{
7575
gwAddr := k8sutils.GetGatewayEndpoint(t, s.Client, s.TimeoutConfig, gatewayNN)
7676

7777
t.Logf("Fetching backend pods with labels: %v", backendPodLabels)
78-
pods, err := k8sutils.GetPodsWithLabel(t, s.Client, appBackendNamespace, backendPodLabels)
78+
pods, err := k8sutils.GetPodsWithLabel(t, s.Client, appBackendNamespace, backendPodLabels, s.TimeoutConfig)
7979
require.NoError(t, err, "Failed to get backend pods")
8080
require.Len(t, pods, expectedPodReplicas, "Expected to find %d backend pods, but found %d.", expectedPodReplicas, len(pods))
8181

@@ -94,7 +94,7 @@ var GatewayFollowingEPPRouting = suite.ConformanceTest{
9494
for i := 0; i < len(pods); i++ {
9595
// Send an initial request targeting a single pod and wait for it to be successful to ensure the Gateway and EPP
9696
// are functioning correctly before running the main test cases.
97-
trafficutils.MakeRequestWithRequestParamAndExpectSuccess(
97+
trafficutils.MakeRequestAndExpectSuccess(
9898
t,
9999
s.RoundTripper,
100100
s.TimeoutConfig,

conformance/tests/basic/inferencepool_httproute_port_validation.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,12 @@ var InferencePoolHTTPRoutePortValidation = suite.ConformanceTest{
6868
s.RoundTripper,
6969
s.TimeoutConfig,
7070
gatewayAddr,
71-
hostname,
72-
path,
73-
backendDeploymentName,
74-
appBackendNamespace,
71+
trafficutils.Request{
72+
Host: hostname,
73+
Path: path,
74+
Backend: backendDeploymentName,
75+
Namespace: appBackendNamespace,
76+
},
7577
)
7678
})
7779

@@ -88,10 +90,12 @@ var InferencePoolHTTPRoutePortValidation = suite.ConformanceTest{
8890
s.RoundTripper,
8991
s.TimeoutConfig,
9092
gatewayAddr,
91-
hostname,
92-
path,
93-
backendDeploymentName,
94-
appBackendNamespace,
93+
trafficutils.Request{
94+
Host: hostname,
95+
Path: path,
96+
Backend: backendDeploymentName,
97+
Namespace: appBackendNamespace,
98+
},
9599
)
96100
})
97101

@@ -109,10 +113,12 @@ var InferencePoolHTTPRoutePortValidation = suite.ConformanceTest{
109113
s.RoundTripper,
110114
s.TimeoutConfig,
111115
gatewayAddr,
112-
hostname,
113-
path,
114-
backendDeploymentName,
115-
appBackendNamespace,
116+
trafficutils.Request{
117+
Host: hostname,
118+
Path: path,
119+
Backend: backendDeploymentName,
120+
Namespace: appBackendNamespace,
121+
},
116122
)
117123
})
118124
},

conformance/tests/basic/inferencepool_multiple_rules_different_pools.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@ package basic
1919
import (
2020
"testing"
2121

22-
"k8s.io/apimachinery/pkg/labels"
2322
"k8s.io/apimachinery/pkg/types"
23+
gwhttp "sigs.k8s.io/gateway-api/conformance/utils/http"
2424
"sigs.k8s.io/gateway-api/conformance/utils/suite"
2525
"sigs.k8s.io/gateway-api/pkg/features"
2626

2727
"sigs.k8s.io/gateway-api-inference-extension/conformance/tests"
2828
k8sutils "sigs.k8s.io/gateway-api-inference-extension/conformance/utils/kubernetes"
29-
trafficutils "sigs.k8s.io/gateway-api-inference-extension/conformance/utils/traffic"
3029
)
3130

3231
func init() {
@@ -52,12 +51,11 @@ var HTTPRouteMultipleRulesDifferentPools = suite.ConformanceTest{
5251
routeName = "httproute-multiple-rules-different-pools"
5352
gatewayName = "conformance-primary-gateway"
5453

55-
backendPrimaryLabelValue = "primary-inference-model-server"
56-
backendSecondaryLabelValue = "secondary-inference-model-server"
57-
backendAppLabelKey = "app"
58-
5954
primaryPath = "/primary"
6055
secondaryPath = "/secondary"
56+
57+
primaryPodBackendPrefix = "primary-inference-model-server"
58+
secondaryPodBackendPrefix = "secondary-inference-model-server"
6159
)
6260

6361
primaryPoolNN := types.NamespacedName{Name: poolPrimaryName, Namespace: appBackendNamespace}
@@ -71,20 +69,28 @@ var HTTPRouteMultipleRulesDifferentPools = suite.ConformanceTest{
7169
})
7270

7371
t.Run("Traffic should be routed to the correct pool based on path", func(t *testing.T) {
74-
primarySelector := labels.SelectorFromSet(labels.Set{backendAppLabelKey: backendPrimaryLabelValue})
75-
secondarySelector := labels.SelectorFromSet(labels.Set{backendAppLabelKey: backendSecondaryLabelValue})
76-
77-
primaryPod := k8sutils.GetPod(t, s.Client, appBackendNamespace, primarySelector, s.TimeoutConfig.RequestTimeout)
78-
secondaryPod := k8sutils.GetPod(t, s.Client, appBackendNamespace, secondarySelector, s.TimeoutConfig.RequestTimeout)
79-
8072
gwAddr := k8sutils.GetGatewayEndpoint(t, s.Client, s.TimeoutConfig, gatewayNN)
8173

8274
t.Run("request to primary pool", func(t *testing.T) {
83-
trafficutils.MakeRequestAndExpectResponseFromPod(t, s.RoundTripper, s.TimeoutConfig, gwAddr, primaryPath, primaryPod)
75+
gwhttp.MakeRequestAndExpectEventuallyConsistentResponse(t, s.RoundTripper,
76+
s.TimeoutConfig, gwAddr, gwhttp.ExpectedResponse{
77+
Request: gwhttp.Request{
78+
Path: primaryPath,
79+
},
80+
Backend: primaryPodBackendPrefix, // Make sure the request is reaching the primary backend.
81+
Namespace: appBackendNamespace,
82+
})
8483
})
8584

8685
t.Run("request to secondary pool", func(t *testing.T) {
87-
trafficutils.MakeRequestAndExpectResponseFromPod(t, s.RoundTripper, s.TimeoutConfig, gwAddr, secondaryPath, secondaryPod)
86+
gwhttp.MakeRequestAndExpectEventuallyConsistentResponse(t, s.RoundTripper,
87+
s.TimeoutConfig, gwAddr, gwhttp.ExpectedResponse{
88+
Request: gwhttp.Request{
89+
Path: secondaryPath,
90+
},
91+
Backend: secondaryPodBackendPrefix, // Make sure the request is reaching the secondary backend.
92+
Namespace: appBackendNamespace,
93+
})
8894
})
8995
})
9096
},

conformance/tests/basic/inferencepool_resolvedrefs_condition.go

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package basic
1818

1919
import (
2020
"context"
21+
"net/http"
2122
"testing"
2223
"time"
2324

@@ -85,21 +86,25 @@ var InferencePoolParentStatus = suite.ConformanceTest{
8586
s.RoundTripper,
8687
s.TimeoutConfig,
8788
gwPrimaryAddr,
88-
hostnamePrimaryGw,
89-
pathPrimaryGw,
90-
backendServicePodName,
91-
appBackendNamespace,
89+
trafficutils.Request{
90+
Host: hostnamePrimaryGw,
91+
Path: pathPrimaryGw,
92+
Backend: backendServicePodName,
93+
Namespace: appBackendNamespace,
94+
},
9295
)
9396

9497
trafficutils.MakeRequestAndExpectSuccess(
9598
t,
9699
s.RoundTripper,
97100
s.TimeoutConfig,
98101
gwSecondaryAddr,
99-
hostnameSecondaryGw,
100-
pathSecondaryGw,
101-
backendServicePodName,
102-
appBackendNamespace,
102+
trafficutils.Request{
103+
Host: hostnameSecondaryGw,
104+
Path: pathSecondaryGw,
105+
Backend: backendServicePodName,
106+
Namespace: appBackendNamespace,
107+
},
103108
)
104109
})
105110

@@ -121,19 +126,24 @@ var InferencePoolParentStatus = suite.ConformanceTest{
121126
s.RoundTripper,
122127
s.TimeoutConfig,
123128
gwSecondaryAddr,
124-
hostnameSecondaryGw,
125-
pathSecondaryGw,
126-
backendServicePodName,
127-
appBackendNamespace,
129+
trafficutils.Request{
130+
Host: hostnameSecondaryGw,
131+
Path: pathSecondaryGw,
132+
Backend: backendServicePodName,
133+
Namespace: appBackendNamespace,
134+
},
128135
)
129136

130-
trafficutils.MakeRequestAndExpectNotFound(
137+
trafficutils.MakeRequestAndExpectEventuallyConsistentResponse(
131138
t,
132139
s.RoundTripper,
133140
s.TimeoutConfig,
134141
gwPrimaryAddr,
135-
hostnamePrimaryGw,
136-
pathPrimaryGw,
142+
trafficutils.Request{
143+
Host: hostnamePrimaryGw,
144+
Path: pathPrimaryGw,
145+
ExpectedStatusCode: http.StatusNotFound,
146+
},
137147
)
138148
})
139149

@@ -147,13 +157,16 @@ var InferencePoolParentStatus = suite.ConformanceTest{
147157
k8sutils.InferencePoolMustHaveNoParents(t, s.Client, poolNN)
148158
t.Logf("InferencePool %s correctly shows no parent statuses, indicating it's no longer referenced.", poolNN.String())
149159

150-
trafficutils.MakeRequestAndExpectNotFound(
160+
trafficutils.MakeRequestAndExpectEventuallyConsistentResponse(
151161
t,
152162
s.RoundTripper,
153163
s.TimeoutConfig,
154164
gwSecondaryAddr,
155-
hostnameSecondaryGw,
156-
pathSecondaryGw,
165+
trafficutils.Request{
166+
Host: hostnameSecondaryGw,
167+
Path: pathSecondaryGw,
168+
ExpectedStatusCode: http.StatusNotFound,
169+
},
157170
)
158171
})
159172

conformance/utils/kubernetes/helpers.go

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
corev1 "k8s.io/api/core/v1"
3131
apierrors "k8s.io/apimachinery/pkg/api/errors"
3232
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33-
"k8s.io/apimachinery/pkg/labels"
3433
"k8s.io/apimachinery/pkg/types"
3534
"k8s.io/apimachinery/pkg/util/wait"
3635
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -311,63 +310,37 @@ func GetGatewayEndpoint(t *testing.T, k8sClient client.Client, timeoutConfig gat
311310

312311
// GetPodsWithLabel retrieves a list of Pods.
313312
// It finds pods matching the given labels in a specific namespace.
314-
func GetPodsWithLabel(t *testing.T, c client.Client, namespace string, labels map[string]string) ([]corev1.Pod, error) {
313+
func GetPodsWithLabel(t *testing.T, c client.Client, namespace string, labels map[string]string, timeConfig gatewayapiconfig.TimeoutConfig) ([]corev1.Pod, error) {
315314
t.Helper()
316315

317-
podList := &corev1.PodList{}
316+
pods := &corev1.PodList{}
317+
timeout := timeConfig.RequestTimeout
318+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
319+
defer cancel()
320+
318321
listOptions := []client.ListOption{
319322
client.InNamespace(namespace),
320323
client.MatchingLabels(labels),
321324
}
322325

323326
t.Logf("Searching for Pods with labels %v in namespace %s", labels, namespace)
324-
if err := c.List(context.Background(), podList, listOptions...); err != nil {
325-
return nil, fmt.Errorf("failed to list pods with labels '%v' in namespace '%s': %w", labels, namespace, err)
326-
}
327-
328-
if len(podList.Items) == 0 {
329-
return nil, fmt.Errorf("no pods found with labels '%v' in namespace '%s'", labels, namespace)
330-
}
331-
return podList.Items, nil
332-
}
333-
334-
// GetPod waits for a Pod matching the specified labels to exist in the given
335-
// namespace and have an IP address assigned. This function returns the first
336-
// matching Pod found if there are multiple matches. It fails the on timeout or error.
337-
// TODO(#1003) combline with GetPodsWithLabel that is being introduced in PR #961
338-
func GetPod(t *testing.T, c client.Client, namespace string, selector labels.Selector, timeout time.Duration) *corev1.Pod {
339-
t.Helper()
340-
341-
var pods corev1.PodList
342-
ctx, cancel := context.WithTimeout(context.Background(), timeout)
343-
defer cancel()
344-
345327
waitErr := wait.PollUntilContextTimeout(ctx, 1*time.Second, timeout, true, func(ctx context.Context) (bool, error) {
346-
if err := c.List(ctx, &pods, &client.ListOptions{
347-
LabelSelector: selector,
348-
Namespace: namespace,
349-
}); err != nil {
350-
t.Logf("Error listing pods with selector %s: %v. Retrying.", selector.String(), err)
351-
return false, nil
328+
if err := c.List(context.Background(), pods, listOptions...); err != nil {
329+
return false, fmt.Errorf("failed to list pods with labels '%v' in namespace '%s': %w", labels, namespace, err)
352330
}
353-
354331
if len(pods.Items) > 0 {
355-
pod := pods.Items[0]
356-
if pod.Status.PodIP != "" && pod.Status.Phase == corev1.PodRunning {
357-
return true, nil
332+
for _, pod := range pods.Items {
333+
if pod.Status.PodIP == "" || pod.Status.Phase != corev1.PodRunning {
334+
t.Logf("Pod %s found, but not yet running or has no IP. Current phase: %s, IP: '%s'. Retrying.", pod.Name, pod.Status.Phase, pod.Status.PodIP)
335+
return false, nil
336+
}
358337
}
359-
t.Logf("Pod %s found, but not yet running or has no IP. Current phase: %s, IP: '%s'. Retrying.", pod.Name, pod.Status.Phase, pod.Status.PodIP)
360-
} else {
361-
t.Logf("No pods found with selector %s yet. Retrying.", selector.String())
338+
return true, nil
362339
}
340+
t.Logf("No pods found with selector %v yet. Retrying.", labels)
363341
return false, nil
364342
})
365-
require.NoErrorf(t, waitErr, "timed out waiting for Pod with selector %s in namespace %s to be ready", selector.String(), namespace)
366-
require.NotEmpty(t, pods.Items, "expected at least one pod for selector %s in namespace %s, but found none", selector.String(), namespace)
367-
368-
pod := &pods.Items[0]
369-
t.Logf("Successfully found ready Pod %s with IP %s for selector %s", pod.Name, pod.Status.PodIP, selector.String())
370-
return pod
343+
return pods.Items, waitErr
371344
}
372345

373346
// DeleteDeployment deletes the specified Deployment and waits until it is no longer

0 commit comments

Comments
 (0)