Skip to content

Commit cc0b86f

Browse files
committed
Add more tests for LRU cache lookup
1 parent 70d440b commit cc0b86f

File tree

4 files changed

+86
-28
lines changed

4 files changed

+86
-28
lines changed

pkg/controller/resourcequota/resource_quota_controller_test.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"testing"
2626
"time"
2727

28-
v1 "k8s.io/api/core/v1"
28+
"k8s.io/api/core/v1"
2929
"k8s.io/apimachinery/pkg/api/resource"
3030
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3131
"k8s.io/apimachinery/pkg/labels"
@@ -41,8 +41,6 @@ import (
4141
core "k8s.io/client-go/testing"
4242
"k8s.io/client-go/tools/cache"
4343
"k8s.io/kubernetes/pkg/controller"
44-
"k8s.io/kubernetes/pkg/quota/v1"
45-
"k8s.io/kubernetes/pkg/quota/v1/generic"
4644
"k8s.io/kubernetes/pkg/quota/v1/install"
4745
)
4846

pkg/controller/resourcequota/resource_quota_monitor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323

2424
"k8s.io/klog/v2"
2525

26-
v1 "k8s.io/api/core/v1"
26+
"k8s.io/api/core/v1"
2727
"k8s.io/apimachinery/pkg/api/meta"
2828
"k8s.io/apimachinery/pkg/runtime/schema"
2929
"k8s.io/apimachinery/pkg/util/clock"

staging/src/k8s.io/apiserver/pkg/admission/plugin/resourcequota/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ go_test(
5959
"//staging/src/k8s.io/api/core/v1:go_default_library",
6060
"//staging/src/k8s.io/apimachinery/pkg/api/resource:go_default_library",
6161
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
62+
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
6263
"//staging/src/k8s.io/apiserver/pkg/admission/plugin/resourcequota/apis/resourcequota:go_default_library",
6364
"//staging/src/k8s.io/client-go/informers:go_default_library",
6465
"//staging/src/k8s.io/client-go/kubernetes/fake:go_default_library",

staging/src/k8s.io/apiserver/pkg/admission/plugin/resourcequota/resource_access_test.go

Lines changed: 83 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"time"
2323

2424
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
"k8s.io/apimachinery/pkg/runtime"
2526

2627
lru "github.com/hashicorp/golang-lru"
2728
corev1 "k8s.io/api/core/v1"
@@ -30,18 +31,6 @@ import (
3031
)
3132

3233
func TestLRUCacheLookup(t *testing.T) {
33-
liveLookupCache, err := lru.New(100)
34-
if err != nil {
35-
t.Fatal(err)
36-
}
37-
kubeClient := fake.NewSimpleClientset()
38-
informerFactory := informers.NewSharedInformerFactory(kubeClient, 0)
39-
40-
accessor, _ := newQuotaAccessor()
41-
accessor.client = kubeClient
42-
accessor.lister = informerFactory.Core().V1().ResourceQuotas().Lister()
43-
accessor.liveLookupCache = liveLookupCache
44-
4534
namespace := "foo"
4635
resourceQuota := &corev1.ResourceQuota{
4736
ObjectMeta: metav1.ObjectMeta{
@@ -50,20 +39,90 @@ func TestLRUCacheLookup(t *testing.T) {
5039
},
5140
}
5241

53-
liveLookupCache.Add(resourceQuota.Namespace, liveLookupEntry{expiry: time.Now().Add(30 * time.Second), items: []*corev1.ResourceQuota{
54-
resourceQuota,
55-
}})
56-
57-
quotas, err := accessor.GetQuotas(resourceQuota.Namespace)
58-
if err != nil {
59-
t.Errorf("Unexpected error: %v", err)
42+
testcases := []struct {
43+
description string
44+
cacheInput []*corev1.ResourceQuota
45+
clientInput []runtime.Object
46+
ttl time.Duration
47+
namespace string
48+
expectedQuota *corev1.ResourceQuota
49+
}{
50+
{
51+
description: "object is found via cache",
52+
cacheInput: []*corev1.ResourceQuota{resourceQuota},
53+
ttl: 30 * time.Second,
54+
namespace: namespace,
55+
expectedQuota: resourceQuota,
56+
},
57+
{
58+
description: "object is outdated and not found with client",
59+
cacheInput: []*corev1.ResourceQuota{resourceQuota},
60+
ttl: -30 * time.Second,
61+
namespace: namespace,
62+
expectedQuota: nil,
63+
},
64+
{
65+
description: "object is outdated but is found with client",
66+
cacheInput: []*corev1.ResourceQuota{resourceQuota},
67+
clientInput: []runtime.Object{resourceQuota},
68+
ttl: -30 * time.Second,
69+
namespace: namespace,
70+
expectedQuota: resourceQuota,
71+
},
72+
{
73+
description: "object does not exist in cache and is not found with client",
74+
cacheInput: []*corev1.ResourceQuota{resourceQuota},
75+
ttl: 30 * time.Second,
76+
expectedQuota: nil,
77+
},
78+
{
79+
description: "object does not exist in cache and is found with client",
80+
cacheInput: []*corev1.ResourceQuota{},
81+
clientInput: []runtime.Object{resourceQuota},
82+
namespace: namespace,
83+
expectedQuota: resourceQuota,
84+
},
6085
}
6186

62-
if count := len(quotas); count != 1 {
63-
t.Errorf("Expected 1 object but got %d", count)
64-
}
87+
for _, tc := range testcases {
88+
t.Run(tc.description, func(t *testing.T) {
89+
liveLookupCache, err := lru.New(1)
90+
if err != nil {
91+
t.Fatal(err)
92+
}
93+
kubeClient := fake.NewSimpleClientset(tc.clientInput...)
94+
informerFactory := informers.NewSharedInformerFactory(kubeClient, 0)
95+
96+
accessor, _ := newQuotaAccessor()
97+
accessor.client = kubeClient
98+
accessor.lister = informerFactory.Core().V1().ResourceQuotas().Lister()
99+
accessor.liveLookupCache = liveLookupCache
100+
101+
for _, q := range tc.cacheInput {
102+
quota := q
103+
liveLookupCache.Add(quota.Namespace, liveLookupEntry{expiry: time.Now().Add(tc.ttl), items: []*corev1.ResourceQuota{quota}})
104+
}
65105

66-
if !reflect.DeepEqual(quotas[0], *resourceQuota) {
67-
t.Errorf("Retrieved object does not match")
106+
quotas, err := accessor.GetQuotas(tc.namespace)
107+
if err != nil {
108+
t.Errorf("Unexpected error: %v", err)
109+
}
110+
111+
if tc.expectedQuota != nil {
112+
if count := len(quotas); count != 1 {
113+
t.Fatalf("Expected 1 object but got %d", count)
114+
}
115+
116+
if !reflect.DeepEqual(quotas[0], *tc.expectedQuota) {
117+
t.Errorf("Retrieved object does not match")
118+
}
119+
return
120+
}
121+
122+
if count := len(quotas); count > 0 {
123+
t.Errorf("Expected 0 objects but got %d", count)
124+
}
125+
})
68126
}
127+
69128
}

0 commit comments

Comments
 (0)