test/apimachinery/watchlist: move common functionality to separate functions

This commit is contained in:
Lukasz Szaszkiewicz 2024-07-15 11:21:00 +02:00
parent d778356bc6
commit 951d325111

View File

@ -35,6 +35,7 @@ import (
utilfeature "k8s.io/apiserver/pkg/util/feature" utilfeature "k8s.io/apiserver/pkg/util/feature"
clientfeatures "k8s.io/client-go/features" clientfeatures "k8s.io/client-go/features"
"k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache" "k8s.io/client-go/tools/cache"
"k8s.io/client-go/util/consistencydetector" "k8s.io/client-go/util/consistencydetector"
"k8s.io/component-base/featuregate" "k8s.io/component-base/featuregate"
@ -103,14 +104,7 @@ var _ = SIGDescribe("API Streaming (aka. WatchList)", framework.WithSerial(), fu
expectedSecrets = append(expectedSecrets, *secret) expectedSecrets = append(expectedSecrets, *secret)
} }
var actualRequestsMadeByKubeClient []string rt, clientConfig := clientConfigWithRoundTripper(f)
clientConfig := f.ClientConfig()
clientConfig.Wrap(func(rt http.RoundTripper) http.RoundTripper {
return roundTripFunc(func(req *http.Request) (*http.Response, error) {
actualRequestsMadeByKubeClient = append(actualRequestsMadeByKubeClient, req.URL.RawQuery)
return rt.RoundTrip(req)
})
})
wrappedKubeClient, err := kubernetes.NewForConfig(clientConfig) wrappedKubeClient, err := kubernetes.NewForConfig(clientConfig)
framework.ExpectNoError(err) framework.ExpectNoError(err)
@ -120,26 +114,35 @@ var _ = SIGDescribe("API Streaming (aka. WatchList)", framework.WithSerial(), fu
ginkgo.By("Verifying if the secret list was properly streamed") ginkgo.By("Verifying if the secret list was properly streamed")
streamedSecrets := secretList.Items streamedSecrets := secretList.Items
sort.Sort(byName(expectedSecrets))
gomega.Expect(cmp.Equal(expectedSecrets, streamedSecrets)).To(gomega.BeTrueBecause("data received via watchlist must match the added data")) gomega.Expect(cmp.Equal(expectedSecrets, streamedSecrets)).To(gomega.BeTrueBecause("data received via watchlist must match the added data"))
ginkgo.By("Verifying if expected requests were sent to the server") ginkgo.By("Verifying if expected requests were sent to the server")
expectedRequestMadeByKubeClient := []string{ expectedRequestMadeByKubeClient := getExpectedRequestMadeByClientFor(secretList.ResourceVersion)
// corresponds to a streaming request made by the kube client to stream the secrets gomega.Expect(rt.actualRequests).To(gomega.Equal(expectedRequestMadeByKubeClient))
"allowWatchBookmarks=true&resourceVersionMatch=NotOlderThan&sendInitialEvents=true&watch=true",
}
if consistencydetector.IsDataConsistencyDetectionForWatchListEnabled() {
// corresponds to a standard list request made by the consistency detector build in into the kube client
expectedRequestMadeByKubeClient = append(expectedRequestMadeByKubeClient, fmt.Sprintf("resourceVersion=%s&resourceVersionMatch=Exact", secretList.ResourceVersion))
}
gomega.Expect(actualRequestsMadeByKubeClient).To(gomega.Equal(expectedRequestMadeByKubeClient))
}) })
}) })
type roundTripFunc func(req *http.Request) (*http.Response, error) type roundTripper struct {
actualRequests []string
delegate http.RoundTripper
}
func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) { func (r *roundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req) r.actualRequests = append(r.actualRequests, req.URL.RawQuery)
return r.delegate.RoundTrip(req)
}
func (r *roundTripper) Wrap(delegate http.RoundTripper) http.RoundTripper {
r.delegate = delegate
return r
}
func clientConfigWithRoundTripper(f *framework.Framework) (*roundTripper, *rest.Config) {
clientConfig := f.ClientConfig()
rt := &roundTripper{}
clientConfig.Wrap(rt.Wrap)
return rt, clientConfig
} }
func verifyStore(ctx context.Context, expectedSecrets []v1.Secret, store cache.Store) { func verifyStore(ctx context.Context, expectedSecrets []v1.Secret, store cache.Store) {
@ -157,6 +160,18 @@ func verifyStore(ctx context.Context, expectedSecrets []v1.Secret, store cache.S
framework.ExpectNoError(err) framework.ExpectNoError(err)
} }
func getExpectedRequestMadeByClientFor(rv string) []string {
expectedRequestMadeByClient := []string{
// corresponds to a streaming request made by the client to stream the secrets
"allowWatchBookmarks=true&resourceVersionMatch=NotOlderThan&sendInitialEvents=true&watch=true",
}
if consistencydetector.IsDataConsistencyDetectionForWatchListEnabled() {
// corresponds to a standard list request made by the consistency detector build in into the client
expectedRequestMadeByClient = append(expectedRequestMadeByClient, fmt.Sprintf("resourceVersion=%s&resourceVersionMatch=Exact", rv))
}
return expectedRequestMadeByClient
}
type byName []v1.Secret type byName []v1.Secret
func (a byName) Len() int { return len(a) } func (a byName) Len() int { return len(a) }