mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-14 21:53:52 +00:00
Merge pull request #125383 from p0lyn0mial/upstream-unstructured-testchecklistconsistency
client-go/consistencydetector: refactor TestCheckListFromCacheDataConsistencyIfRequestedInternalHappyPath to work with unstructured data
This commit is contained in:
@@ -24,6 +24,8 @@ import (
|
|||||||
|
|
||||||
v1 "k8s.io/api/core/v1"
|
v1 "k8s.io/api/core/v1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
"k8s.io/utils/ptr"
|
"k8s.io/utils/ptr"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -52,26 +54,80 @@ func TestCheckListFromCacheDataConsistencyIfRequestedInternalPanics(t *testing.T
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCheckListFromCacheDataConsistencyIfRequestedInternalHappyPath(t *testing.T) {
|
func TestCheckListFromCacheDataConsistencyIfRequestedInternalHappyPath(t *testing.T) {
|
||||||
ctx := context.TODO()
|
scenarios := []struct {
|
||||||
listOptions := metav1.ListOptions{TimeoutSeconds: ptr.To(int64(39))}
|
name string
|
||||||
expectedRequestOptions := metav1.ListOptions{
|
listResponse runtime.Object
|
||||||
|
retrievedList runtime.Object
|
||||||
|
retrievedListOptions metav1.ListOptions
|
||||||
|
|
||||||
|
expectedRequestOptions metav1.ListOptions
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "list detector works with a typed list",
|
||||||
|
listResponse: &v1.PodList{
|
||||||
|
ListMeta: metav1.ListMeta{ResourceVersion: "2"},
|
||||||
|
Items: []v1.Pod{*makePod("p1", "1"), *makePod("p2", "2")},
|
||||||
|
},
|
||||||
|
retrievedListOptions: metav1.ListOptions{TimeoutSeconds: ptr.To(int64(39))},
|
||||||
|
retrievedList: &v1.PodList{
|
||||||
|
ListMeta: metav1.ListMeta{ResourceVersion: "2"},
|
||||||
|
Items: []v1.Pod{*makePod("p1", "1"), *makePod("p2", "2")},
|
||||||
|
},
|
||||||
|
expectedRequestOptions: metav1.ListOptions{
|
||||||
ResourceVersion: "2",
|
ResourceVersion: "2",
|
||||||
ResourceVersionMatch: metav1.ResourceVersionMatchExact,
|
ResourceVersionMatch: metav1.ResourceVersionMatchExact,
|
||||||
TimeoutSeconds: ptr.To(int64(39)),
|
TimeoutSeconds: ptr.To(int64(39)),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "list detector works with a unstructured list",
|
||||||
|
listResponse: &unstructured.UnstructuredList{
|
||||||
|
Object: map[string]interface{}{
|
||||||
|
"apiVersion": "vTest",
|
||||||
|
"kind": "rTestList",
|
||||||
|
"metadata": map[string]interface{}{
|
||||||
|
"resourceVersion": "3",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Items: []unstructured.Unstructured{
|
||||||
|
*makeUnstructuredObject("vTest", "rTest", "item1"),
|
||||||
|
*makeUnstructuredObject("vTest", "rTest", "item2"),
|
||||||
|
*makeUnstructuredObject("vTest", "rTest", "item3"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
retrievedListOptions: metav1.ListOptions{TimeoutSeconds: ptr.To(int64(39))},
|
||||||
|
retrievedList: &unstructured.UnstructuredList{
|
||||||
|
Object: map[string]interface{}{
|
||||||
|
"apiVersion": "vTest",
|
||||||
|
"kind": "rTestList",
|
||||||
|
"metadata": map[string]interface{}{
|
||||||
|
"resourceVersion": "3",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Items: []unstructured.Unstructured{
|
||||||
|
*makeUnstructuredObject("vTest", "rTest", "item1"),
|
||||||
|
*makeUnstructuredObject("vTest", "rTest", "item2"),
|
||||||
|
*makeUnstructuredObject("vTest", "rTest", "item3"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expectedRequestOptions: metav1.ListOptions{
|
||||||
|
ResourceVersion: "3",
|
||||||
|
ResourceVersionMatch: metav1.ResourceVersionMatchExact,
|
||||||
|
TimeoutSeconds: ptr.To(int64(39)),
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
listResponse := &v1.PodList{
|
for _, scenario := range scenarios {
|
||||||
ListMeta: metav1.ListMeta{ResourceVersion: "2"},
|
t.Run(scenario.name, func(t *testing.T) {
|
||||||
Items: []v1.Pod{*makePod("p1", "1"), *makePod("p2", "2")},
|
ctx := context.TODO()
|
||||||
}
|
listOptions := metav1.ListOptions{TimeoutSeconds: ptr.To(int64(39))}
|
||||||
retrievedList := &v1.PodList{
|
fakeLister := &listWrapper{response: scenario.listResponse}
|
||||||
ListMeta: metav1.ListMeta{ResourceVersion: "2"},
|
|
||||||
Items: []v1.Pod{*makePod("p1", "1"), *makePod("p2", "2")},
|
|
||||||
}
|
|
||||||
fakeLister := &listWrapper{response: listResponse}
|
|
||||||
|
|
||||||
checkListFromCacheDataConsistencyIfRequestedInternal(ctx, "", fakeLister.List, listOptions, retrievedList)
|
checkListFromCacheDataConsistencyIfRequestedInternal(ctx, "", fakeLister.List, listOptions, scenario.retrievedList)
|
||||||
|
|
||||||
require.Equal(t, 1, fakeLister.counter)
|
require.Equal(t, 1, fakeLister.counter)
|
||||||
require.Equal(t, 1, len(fakeLister.requestOptions))
|
require.Equal(t, 1, len(fakeLister.requestOptions))
|
||||||
require.Equal(t, fakeLister.requestOptions[0], expectedRequestOptions)
|
require.Equal(t, fakeLister.requestOptions[0], scenario.expectedRequestOptions)
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user