@@ -14,12 +14,83 @@ See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
16
17
- package cache
17
+ package cache_test
18
18
19
19
import (
20
+ "context"
21
+
20
22
. "github.com/onsi/ginkgo"
23
+ . "github.com/onsi/gomega"
24
+ kcorev1 "k8s.io/api/core/v1"
25
+
26
+ "sigs.k8s.io/controller-runtime/pkg/cache"
27
+ "sigs.k8s.io/controller-runtime/pkg/client"
21
28
)
22
29
30
+ var _ = Describe ("Informer Cache" , func () {
31
+ var stop chan struct {}
32
+
33
+ BeforeEach (func () {
34
+ stop = make (chan struct {})
35
+ Expect (cfg ).NotTo (BeNil ())
36
+ })
37
+ AfterEach (func () {
38
+ close (stop )
39
+ })
40
+
41
+ Describe ("as a Reader" , func () {
42
+ It ("should be able to list objects that haven't been watched previously" , func () {
43
+ By ("Creating the cache" )
44
+ reader , err := cache .New (cfg , cache.Options {})
45
+ Expect (err ).NotTo (HaveOccurred ())
46
+
47
+ By ("running the cache and waiting for it to sync" )
48
+ go func () {
49
+ defer GinkgoRecover ()
50
+ Expect (reader .Start (stop )).ToNot (HaveOccurred ())
51
+ }()
52
+ Expect (reader .WaitForCacheSync (stop )).NotTo (BeFalse ())
53
+
54
+ By ("Listing all services in the cluster" )
55
+ listObj := & kcorev1.ServiceList {}
56
+ Expect (reader .List (context .Background (), nil , listObj )).NotTo (HaveOccurred ())
57
+
58
+ By ("Verifying that the returned list contains the Kubernetes service" )
59
+ // NB: there has to be at least the kubernetes service in the cluster
60
+ Expect (listObj .Items ).NotTo (BeEmpty ())
61
+ hasKubeService := false
62
+ for _ , svc := range listObj .Items {
63
+ if svc .Namespace == "default" && svc .Name == "kubernetes" {
64
+ hasKubeService = true
65
+ break
66
+ }
67
+ }
68
+ Expect (hasKubeService ).To (BeTrue ())
69
+ })
70
+
71
+ It ("should be able to get objects that haven't been watched previously" , func () {
72
+ By ("Creating the cache" )
73
+ reader , err := cache .New (cfg , cache.Options {})
74
+ Expect (err ).NotTo (HaveOccurred ())
75
+
76
+ By ("running the cache and waiting for it to sync" )
77
+ go func () {
78
+ defer GinkgoRecover ()
79
+ Expect (reader .Start (stop )).ToNot (HaveOccurred ())
80
+ }()
81
+ Expect (reader .WaitForCacheSync (stop )).NotTo (BeFalse ())
82
+
83
+ By ("Getting the Kubernetes service" )
84
+ svc := & kcorev1.Service {}
85
+ Expect (reader .Get (context .Background (), client.ObjectKey {Namespace : "default" , Name : "kubernetes" }, svc )).NotTo (HaveOccurred ())
86
+
87
+ By ("Verifying that the returned service looks reasonable" )
88
+ Expect (svc .Name ).To (Equal ("kubernetes" ))
89
+ Expect (svc .Namespace ).To (Equal ("default" ))
90
+ })
91
+ })
92
+ })
93
+
23
94
var _ = Describe ("Indexers" , func () {
24
95
//three := int64(3)
25
96
//knownPodKey := client.ObjectKey{Name: "some-pod", Namespace: "some-ns"}
0 commit comments