@@ -4,8 +4,10 @@ import (
44 "testing"
55 "time"
66
7+ "github.com/openmcp-project/openmcp-operator/api/clusters/v1alpha1"
78 "github.com/stretchr/testify/assert"
89 ctrl "sigs.k8s.io/controller-runtime"
10+ "sigs.k8s.io/controller-runtime/pkg/client"
911)
1012
1113func Test_Entry (t * testing.T ) {
@@ -34,3 +36,100 @@ func Test_Entry(t *testing.T) {
3436func getRequeueAfter (res ctrl.Result , _ error ) time.Duration {
3537 return res .RequeueAfter .Round (time .Second )
3638}
39+
40+ func TestFor (t * testing.T ) {
41+ tests := []struct {
42+ name string
43+ firstObj client.Object
44+ secondObj client.Object
45+ expectSame bool
46+ description string
47+ }{
48+ {
49+ name : "same object returns same entry" ,
50+ firstObj : & v1alpha1.Cluster {
51+ ObjectMeta : ctrl.ObjectMeta {
52+ Name : "test" ,
53+ Namespace : "test" ,
54+ },
55+ },
56+ secondObj : & v1alpha1.Cluster {
57+ ObjectMeta : ctrl.ObjectMeta {
58+ Name : "test" ,
59+ Namespace : "test" ,
60+ },
61+ },
62+ expectSame : true ,
63+ description : "Expected to get the same entry back" ,
64+ },
65+ {
66+ name : "different namespace returns different entry" ,
67+ firstObj : & v1alpha1.Cluster {
68+ ObjectMeta : ctrl.ObjectMeta {
69+ Name : "test" ,
70+ Namespace : "test" ,
71+ },
72+ },
73+ secondObj : & v1alpha1.Cluster {
74+ ObjectMeta : ctrl.ObjectMeta {
75+ Name : "test" ,
76+ Namespace : "test2" ,
77+ },
78+ },
79+ expectSame : false ,
80+ description : "Expected to get a different entry back" ,
81+ },
82+ {
83+ name : "different name returns different entry" ,
84+ firstObj : & v1alpha1.Cluster {
85+ ObjectMeta : ctrl.ObjectMeta {
86+ Name : "test" ,
87+ Namespace : "test" ,
88+ },
89+ },
90+ secondObj : & v1alpha1.Cluster {
91+ ObjectMeta : ctrl.ObjectMeta {
92+ Name : "test2" ,
93+ Namespace : "test" ,
94+ },
95+ },
96+ expectSame : false ,
97+ description : "Expected to get a different entry back" ,
98+ },
99+ {
100+ name : "different kind returns different entry" ,
101+ firstObj : & v1alpha1.Cluster {
102+ ObjectMeta : ctrl.ObjectMeta {
103+ Name : "test" ,
104+ Namespace : "test" ,
105+ },
106+ },
107+ secondObj : & v1alpha1.AccessRequest {
108+ ObjectMeta : ctrl.ObjectMeta {
109+ Name : "test" ,
110+ Namespace : "test" ,
111+ },
112+ },
113+ expectSame : false ,
114+ description : "Expected to get a different entry back" ,
115+ },
116+ }
117+
118+ for _ , tt := range tests {
119+ t .Run (tt .name , func (t * testing.T ) {
120+ store := NewStore (time .Second , time .Minute , 2 )
121+ entry1 := store .For (tt .firstObj )
122+
123+ assert .NotNil (t , entry1 , "Expected entry to be created" )
124+ assert .Equal (t , 1 * time .Second , getRequeueAfter (entry1 .Stable ()))
125+
126+ entry2 := store .For (tt .secondObj )
127+
128+ if tt .expectSame {
129+ assert .Equal (t , entry1 , entry2 , tt .description )
130+ } else {
131+ assert .NotEqual (t , entry1 , entry2 , tt .description )
132+ }
133+ })
134+ }
135+ }
0 commit comments