refactor to use Schema(contentType)

Kubernetes-commit: e6e6dd826d76a2b19515cd907062f1f74f9e52c2
This commit is contained in:
Alexander Zielenski
2022-10-14 12:46:36 -07:00
committed by Kubernetes Publisher
parent 8b6ceae557
commit 12cafe2b1b
6 changed files with 171 additions and 128 deletions

View File

@@ -29,6 +29,7 @@ import (
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/version"
"k8s.io/client-go/discovery"
@@ -169,38 +170,52 @@ func TestOpenAPIDiskCache(t *testing.T) {
paths, err := openapiClient.Paths()
require.NoError(t, err)
assert.Equal(t, 1, fakeServer.RequestCounters["/openapi/v3"])
require.Greater(t, len(paths), 0)
i := 0
for k, v := range paths {
i++
_, err = v.SchemaJSON()
assert.NoError(t, err)
path := "/openapi/v3/" + strings.TrimPrefix(k, "/")
assert.Equal(t, 1, fakeServer.RequestCounters[path])
// Ensure schema call is served from memory
_, err = v.SchemaJSON()
assert.NoError(t, err)
assert.Equal(t, 1, fakeServer.RequestCounters[path])
client.Invalidate()
// Refetch the schema from a new openapi client to try to force a new
// http request
newPaths, err := client.OpenAPIV3().Paths()
if !assert.NoError(t, err) {
continue
}
// Ensure schema call is still served from disk
_, err = newPaths[k].SchemaJSON()
assert.NoError(t, err)
assert.Equal(t, 1+i, fakeServer.RequestCounters["/openapi/v3"])
assert.Equal(t, 1, fakeServer.RequestCounters[path])
contentTypes := []string{
runtime.ContentTypeJSON, openapi.ContentTypeOpenAPIV3PB,
}
for _, contentType := range contentTypes {
t.Run(contentType, func(t *testing.T) {
// Reset all counters (cant just reset to nil since reference is shared)
for k := range fakeServer.RequestCounters {
delete(fakeServer.RequestCounters, k)
}
i := 0
for k, v := range paths {
i++
_, err = v.Schema(contentType)
assert.NoError(t, err)
path := "/openapi/v3/" + strings.TrimPrefix(k, "/")
assert.Equal(t, 1, fakeServer.RequestCounters[path])
// Ensure schema call is served from memory
_, err = v.Schema(contentType)
assert.NoError(t, err)
assert.Equal(t, 1, fakeServer.RequestCounters[path])
client.Invalidate()
// Refetch the schema from a new openapi client to try to force a new
// http request
newPaths, err := client.OpenAPIV3().Paths()
if !assert.NoError(t, err) {
continue
}
// Ensure schema call is still served from disk
_, err = newPaths[k].Schema(contentType)
assert.NoError(t, err)
assert.Equal(t, i, fakeServer.RequestCounters["/openapi/v3"])
assert.Equal(t, 1, fakeServer.RequestCounters[path])
}
})
}
}
type fakeDiscoveryClient struct {

View File

@@ -27,8 +27,10 @@ import (
"github.com/stretchr/testify/require"
errorsutil "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/discovery"
"k8s.io/client-go/discovery/fake"
"k8s.io/client-go/openapi"
"k8s.io/client-go/rest"
testutil "k8s.io/client-go/util/testing"
)
@@ -415,40 +417,48 @@ func TestOpenAPIMemCache(t *testing.T) {
paths, err := openapiClient.Paths()
require.NoError(t, err)
for k, v := range paths {
original, err := v.SchemaPB()
if !assert.NoError(t, err) {
continue
}
contentTypes := []string{
runtime.ContentTypeJSON, openapi.ContentTypeOpenAPIV3PB,
}
pathsAgain, err := openapiClient.Paths()
if !assert.NoError(t, err) {
continue
}
for _, contentType := range contentTypes {
t.Run(contentType, func(t *testing.T) {
for k, v := range paths {
original, err := v.Schema(contentType)
if !assert.NoError(t, err) {
continue
}
schemaAgain, err := pathsAgain[k].SchemaPB()
if !assert.NoError(t, err) {
continue
}
pathsAgain, err := openapiClient.Paths()
if !assert.NoError(t, err) {
continue
}
assert.True(t, reflect.ValueOf(paths).Pointer() == reflect.ValueOf(pathsAgain).Pointer())
assert.True(t, reflect.ValueOf(original).Pointer() == reflect.ValueOf(schemaAgain).Pointer())
schemaAgain, err := pathsAgain[k].Schema(contentType)
if !assert.NoError(t, err) {
continue
}
// Invalidate and try again. This time pointers should not be equal
client.Invalidate()
assert.True(t, reflect.ValueOf(paths).Pointer() == reflect.ValueOf(pathsAgain).Pointer())
assert.True(t, reflect.ValueOf(original).Pointer() == reflect.ValueOf(schemaAgain).Pointer())
pathsAgain, err = client.OpenAPIV3().Paths()
if !assert.NoError(t, err) {
continue
}
// Invalidate and try again. This time pointers should not be equal
client.Invalidate()
schemaAgain, err = pathsAgain[k].SchemaPB()
if !assert.NoError(t, err) {
continue
}
pathsAgain, err = client.OpenAPIV3().Paths()
if !assert.NoError(t, err) {
continue
}
assert.True(t, reflect.ValueOf(paths).Pointer() != reflect.ValueOf(pathsAgain).Pointer())
assert.True(t, reflect.ValueOf(original).Pointer() != reflect.ValueOf(schemaAgain).Pointer())
assert.Equal(t, original, schemaAgain)
schemaAgain, err = pathsAgain[k].Schema(contentType)
if !assert.NoError(t, err) {
continue
}
assert.True(t, reflect.ValueOf(paths).Pointer() != reflect.ValueOf(pathsAgain).Pointer())
assert.True(t, reflect.ValueOf(original).Pointer() != reflect.ValueOf(schemaAgain).Pointer())
assert.Equal(t, original, schemaAgain)
}
})
}
}