|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | | -package watch |
| 15 | +package watch_test |
16 | 16 |
|
17 | 17 | import ( |
18 | 18 | "testing" |
19 | 19 |
|
20 | 20 | "github.com/stretchr/testify/assert" |
| 21 | + corev1 "k8s.io/api/core/v1" |
21 | 22 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
22 | 23 | "sigs.k8s.io/controller-runtime/pkg/event" |
23 | 24 |
|
| 25 | + "github.com/mongodb/mongodb-atlas-kubernetes/v2/api" |
24 | 26 | akov2 "github.com/mongodb/mongodb-atlas-kubernetes/v2/api/v1" |
| 27 | + "github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/controller/customresource" |
| 28 | + "github.com/mongodb/mongodb-atlas-kubernetes/v2/internal/controller/watch" |
25 | 29 | ) |
26 | 30 |
|
27 | 31 | func TestSelectNamespacesPredicate(t *testing.T) { |
@@ -61,11 +65,259 @@ func TestSelectNamespacesPredicate(t *testing.T) { |
61 | 65 |
|
62 | 66 | for name, tt := range tests { |
63 | 67 | t.Run(name, func(t *testing.T) { |
64 | | - f := SelectNamespacesPredicate(tt.namespaces) |
| 68 | + f := watch.SelectNamespacesPredicate(tt.namespaces) |
65 | 69 | assert.Equal(t, tt.expect, f.CreateFunc(tt.createEvent)) |
66 | 70 | assert.Equal(t, tt.expect, f.UpdateFunc(tt.updateEvent)) |
67 | 71 | assert.Equal(t, tt.expect, f.DeleteFunc(tt.deleteEvent)) |
68 | 72 | assert.Equal(t, tt.expect, f.GenericFunc(tt.genericEvent)) |
69 | 73 | }) |
70 | 74 | } |
71 | 75 | } |
| 76 | + |
| 77 | +func TestDeprecatedCommonPredicates(t *testing.T) { |
| 78 | + for _, tc := range []struct { |
| 79 | + title string |
| 80 | + old *akov2.AtlasProject |
| 81 | + new *akov2.AtlasProject |
| 82 | + want bool |
| 83 | + }{ |
| 84 | + { |
| 85 | + title: "no changes - resync", |
| 86 | + old: sampleObj(), |
| 87 | + new: sampleObj(), |
| 88 | + want: true, |
| 89 | + }, |
| 90 | + { |
| 91 | + title: "no gen change", |
| 92 | + old: sampleObj(resourceVersion("0")), |
| 93 | + new: sampleObj(resourceVersion("1")), |
| 94 | + want: false, |
| 95 | + }, |
| 96 | + { |
| 97 | + title: "finalizers changed", |
| 98 | + old: sampleObj(resourceVersion("0")), |
| 99 | + new: sampleObj(resourceVersion("1"), finalizers([]string{"finalize"})), |
| 100 | + want: true, |
| 101 | + }, |
| 102 | + { |
| 103 | + title: "skipped", |
| 104 | + old: sampleObj(resourceVersion("0")), |
| 105 | + new: sampleObj(resourceVersion("1"), skipAnnotation()), |
| 106 | + want: false, |
| 107 | + }, |
| 108 | + { |
| 109 | + title: "no longer skipped", |
| 110 | + old: sampleObj(resourceVersion("0"), skipAnnotation()), |
| 111 | + new: sampleObj( |
| 112 | + resourceVersion("1"), |
| 113 | + ), |
| 114 | + want: true, |
| 115 | + }, |
| 116 | + } { |
| 117 | + t.Run(tc.title, func(t *testing.T) { |
| 118 | + f := watch.DeprecatedCommonPredicates() |
| 119 | + assert.Equal(t, tc.want, f.UpdateFunc( |
| 120 | + event.UpdateEvent{ObjectOld: tc.old, ObjectNew: tc.new})) |
| 121 | + }) |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +func TestDefaultPredicates(t *testing.T) { |
| 126 | + for _, tc := range []struct { |
| 127 | + title string |
| 128 | + old *akov2.AtlasProject |
| 129 | + new *akov2.AtlasProject |
| 130 | + wantCreate bool |
| 131 | + wantUpdate bool |
| 132 | + wantDelete bool |
| 133 | + wantGeneric bool |
| 134 | + }{ |
| 135 | + { |
| 136 | + title: "no changes", |
| 137 | + old: sampleObj(), |
| 138 | + new: sampleObj(), |
| 139 | + wantCreate: true, |
| 140 | + wantUpdate: true, |
| 141 | + wantDelete: false, |
| 142 | + wantGeneric: true, |
| 143 | + }, |
| 144 | + { |
| 145 | + title: "no gen change", |
| 146 | + old: sampleObj(resourceVersion("0")), |
| 147 | + new: sampleObj(resourceVersion("1")), |
| 148 | + wantCreate: true, |
| 149 | + wantUpdate: false, |
| 150 | + wantDelete: false, |
| 151 | + wantGeneric: true, |
| 152 | + }, |
| 153 | + { |
| 154 | + title: "finalizers set", |
| 155 | + old: sampleObj(resourceVersion("0")), |
| 156 | + new: sampleObj(resourceVersion("1"), finalizers([]string{"finalize"})), |
| 157 | + wantCreate: true, |
| 158 | + wantUpdate: false, // finalizer changes do not trigger updates unlike with deprecated |
| 159 | + wantDelete: false, |
| 160 | + wantGeneric: true, |
| 161 | + }, |
| 162 | + { |
| 163 | + title: "skipped", |
| 164 | + old: sampleObj( |
| 165 | + resourceVersion("0"), |
| 166 | + ), |
| 167 | + new: sampleObj( |
| 168 | + resourceVersion("1"), |
| 169 | + skipAnnotation(), |
| 170 | + ), |
| 171 | + wantCreate: true, |
| 172 | + wantUpdate: false, |
| 173 | + wantDelete: false, |
| 174 | + wantGeneric: true, |
| 175 | + }, |
| 176 | + { |
| 177 | + title: "no longer skipped", |
| 178 | + old: sampleObj(resourceVersion("0"), skipAnnotation()), |
| 179 | + new: sampleObj(resourceVersion("1")), |
| 180 | + wantCreate: true, |
| 181 | + wantUpdate: true, |
| 182 | + wantDelete: false, |
| 183 | + wantGeneric: true, |
| 184 | + }, |
| 185 | + { |
| 186 | + title: "finalizers removed", |
| 187 | + old: sampleObj(resourceVersion("0"), finalizers([]string{"finalize"})), |
| 188 | + new: sampleObj(resourceVersion("1")), |
| 189 | + wantCreate: true, |
| 190 | + wantUpdate: false, |
| 191 | + wantDelete: false, |
| 192 | + wantGeneric: true, |
| 193 | + }, |
| 194 | + } { |
| 195 | + t.Run(tc.title, func(t *testing.T) { |
| 196 | + f := watch.DefaultPredicates[*akov2.AtlasProject]() |
| 197 | + assert.Equal(t, tc.wantCreate, |
| 198 | + f.Create(event.TypedCreateEvent[*akov2.AtlasProject]{Object: tc.new}), "on create") |
| 199 | + assert.Equal(t, tc.wantUpdate, |
| 200 | + f.Update(event.TypedUpdateEvent[*akov2.AtlasProject]{ |
| 201 | + ObjectOld: tc.old, ObjectNew: tc.new, |
| 202 | + }), "on update") |
| 203 | + assert.Equal(t, tc.wantDelete, |
| 204 | + f.Delete(event.TypedDeleteEvent[*akov2.AtlasProject]{Object: tc.new}), "on delete") |
| 205 | + assert.Equal(t, tc.wantGeneric, |
| 206 | + f.Generic(event.TypedGenericEvent[*akov2.AtlasProject]{Object: tc.new}), "on generic event") |
| 207 | + }) |
| 208 | + } |
| 209 | +} |
| 210 | + |
| 211 | +func TestReadyTransitionPredicate(t *testing.T) { |
| 212 | + for _, tc := range []struct { |
| 213 | + title string |
| 214 | + old *akov2.AtlasProject |
| 215 | + new *akov2.AtlasProject |
| 216 | + wantCreate bool |
| 217 | + wantUpdate bool |
| 218 | + wantDelete bool |
| 219 | + wantGeneric bool |
| 220 | + }{ |
| 221 | + { |
| 222 | + title: "no changes", |
| 223 | + old: sampleObj(), |
| 224 | + new: sampleObj(), |
| 225 | + wantCreate: false, |
| 226 | + wantUpdate: false, |
| 227 | + wantDelete: true, |
| 228 | + wantGeneric: false, |
| 229 | + }, |
| 230 | + { |
| 231 | + title: "ready now", |
| 232 | + old: sampleObj(ready(corev1.ConditionFalse)), |
| 233 | + new: sampleObj(ready(corev1.ConditionTrue)), |
| 234 | + wantCreate: false, |
| 235 | + wantUpdate: true, |
| 236 | + wantDelete: true, |
| 237 | + wantGeneric: false, |
| 238 | + }, |
| 239 | + { |
| 240 | + title: "no longer ready", |
| 241 | + old: sampleObj(ready(corev1.ConditionTrue)), |
| 242 | + new: sampleObj(ready(corev1.ConditionFalse)), |
| 243 | + wantCreate: false, |
| 244 | + wantUpdate: false, |
| 245 | + wantDelete: true, |
| 246 | + wantGeneric: false, |
| 247 | + }, |
| 248 | + } { |
| 249 | + t.Run(tc.title, func(t *testing.T) { |
| 250 | + f := watch.ReadyTransitionPredicate(projectReady) |
| 251 | + assert.Equal(t, tc.wantCreate, |
| 252 | + f.Create(event.CreateEvent{Object: tc.new}), "on create") |
| 253 | + assert.Equal(t, tc.wantUpdate, |
| 254 | + f.Update(event.UpdateEvent{ObjectOld: tc.old, ObjectNew: tc.new}), "on update") |
| 255 | + assert.Equal(t, tc.wantDelete, |
| 256 | + f.Delete(event.DeleteEvent{Object: tc.new}), "on delete") |
| 257 | + assert.Equal(t, tc.wantGeneric, |
| 258 | + f.Generic(event.GenericEvent{Object: tc.new}), "on generic event") |
| 259 | + }) |
| 260 | + } |
| 261 | +} |
| 262 | + |
| 263 | +func projectReady(p *akov2.AtlasProject) bool { |
| 264 | + for _, c := range p.Status.Conditions { |
| 265 | + if c.Type == api.ReadyType && c.Status == corev1.ConditionTrue { |
| 266 | + return true |
| 267 | + } |
| 268 | + } |
| 269 | + return false |
| 270 | +} |
| 271 | + |
| 272 | +type optionFunc func(*akov2.AtlasProject) *akov2.AtlasProject |
| 273 | + |
| 274 | +func sampleObj(opts ...optionFunc) *akov2.AtlasProject { |
| 275 | + p := &akov2.AtlasProject{ |
| 276 | + ObjectMeta: metav1.ObjectMeta{ |
| 277 | + Name: "project", |
| 278 | + Namespace: "ns", |
| 279 | + Annotations: map[string]string{}, |
| 280 | + }, |
| 281 | + Spec: akov2.AtlasProjectSpec{ |
| 282 | + Name: "atlas-project", |
| 283 | + }, |
| 284 | + } |
| 285 | + for _, opt := range opts { |
| 286 | + p = opt(p) |
| 287 | + } |
| 288 | + return p |
| 289 | +} |
| 290 | + |
| 291 | +func resourceVersion(rs string) optionFunc { |
| 292 | + return func(p *akov2.AtlasProject) *akov2.AtlasProject { |
| 293 | + p.ResourceVersion = rs |
| 294 | + return p |
| 295 | + } |
| 296 | +} |
| 297 | + |
| 298 | +func skipAnnotation() optionFunc { |
| 299 | + return func(p *akov2.AtlasProject) *akov2.AtlasProject { |
| 300 | + p.Annotations[customresource.ReconciliationPolicyAnnotation] = |
| 301 | + customresource.ReconciliationPolicySkip |
| 302 | + return p |
| 303 | + } |
| 304 | +} |
| 305 | + |
| 306 | +func finalizers(f []string) optionFunc { |
| 307 | + return func(p *akov2.AtlasProject) *akov2.AtlasProject { |
| 308 | + p.Finalizers = f |
| 309 | + return p |
| 310 | + } |
| 311 | +} |
| 312 | + |
| 313 | +func ready(status corev1.ConditionStatus) optionFunc { |
| 314 | + return func(p *akov2.AtlasProject) *akov2.AtlasProject { |
| 315 | + p.Status.Conditions = append(p.Status.Conditions, |
| 316 | + api.Condition{ |
| 317 | + Type: api.ReadyType, |
| 318 | + Status: status, |
| 319 | + }, |
| 320 | + ) |
| 321 | + return p |
| 322 | + } |
| 323 | +} |
0 commit comments