@@ -22,6 +22,7 @@ import (
22
22
"time"
23
23
24
24
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25
+ "k8s.io/apimachinery/pkg/runtime"
25
26
26
27
lru "github.com/hashicorp/golang-lru"
27
28
corev1 "k8s.io/api/core/v1"
@@ -30,18 +31,6 @@ import (
30
31
)
31
32
32
33
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
-
45
34
namespace := "foo"
46
35
resourceQuota := & corev1.ResourceQuota {
47
36
ObjectMeta : metav1.ObjectMeta {
@@ -50,20 +39,90 @@ func TestLRUCacheLookup(t *testing.T) {
50
39
},
51
40
}
52
41
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
+ },
60
85
}
61
86
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
+ }
65
105
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
+ })
68
126
}
127
+
69
128
}
0 commit comments