github.com/kyma-project/kyma/components/asset-store-controller-manager@v0.0.0-20191203152857-3792b5df17c5/internal/handler/bucket/bucket_test.go (about) 1 package bucket_test 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 "time" 8 9 "github.com/kyma-project/kyma/components/asset-store-controller-manager/internal/handler/bucket" 10 "github.com/kyma-project/kyma/components/asset-store-controller-manager/internal/store/automock" 11 "github.com/kyma-project/kyma/components/asset-store-controller-manager/pkg/apis/assetstore/v1alpha2" 12 . "github.com/onsi/gomega" 13 "github.com/pkg/errors" 14 "k8s.io/apimachinery/pkg/apis/meta/v1" 15 "k8s.io/client-go/tools/record" 16 logf "sigs.k8s.io/controller-runtime/pkg/runtime/log" 17 ) 18 19 var log = logf.Log.WithName("asset-test") 20 21 func TestBucketHandler_Handle_Default(t *testing.T) { 22 // Given 23 g := NewGomegaWithT(t) 24 ctx := context.TODO() 25 relistInterval := time.Minute 26 now := time.Now() 27 data := testData("test-bucket", v1alpha2.BucketPolicyReadOnly) 28 data.ObjectMeta.Generation = int64(1) 29 data.Status.ObservedGeneration = int64(1) 30 31 store := new(automock.Store) 32 defer store.AssertExpectations(t) 33 34 handler := bucket.New(log, fakeRecorder(), store, "https://localhost", relistInterval) 35 36 // When 37 status, err := handler.Do(ctx, now, data, data.Spec.CommonBucketSpec, data.Status.CommonBucketStatus) 38 39 // Then 40 g.Expect(err).ToNot(HaveOccurred()) 41 g.Expect(status).To(BeZero()) 42 } 43 44 func TestBucketHandler_Handle_OnAddOrUpdate(t *testing.T) { 45 t.Run("PolicyModified", func(t *testing.T) { 46 // Given 47 g := NewGomegaWithT(t) 48 ctx := context.TODO() 49 relistInterval := time.Minute 50 now := time.Now() 51 data := testData("test-bucket", v1alpha2.BucketPolicyReadOnly) 52 data.ObjectMeta.Generation = int64(1) 53 data.Status.ObservedGeneration = int64(2) 54 data.Status.Phase = v1alpha2.BucketReady 55 data.Status.RemoteName = fmt.Sprintf("%s-123", data.Name) 56 57 store := new(automock.Store) 58 defer store.AssertExpectations(t) 59 60 store.On("BucketExists", data.Status.RemoteName).Return(true, nil).Once() 61 store.On("CompareBucketPolicy", data.Status.RemoteName, data.Spec.Policy).Return(false, nil).Once() 62 store.On("SetBucketPolicy", data.Status.RemoteName, data.Spec.Policy).Return(nil).Once() 63 64 handler := bucket.New(log, fakeRecorder(), store, "https://localhost", relistInterval) 65 66 // When 67 status, err := handler.Do(ctx, now, data, data.Spec.CommonBucketSpec, data.Status.CommonBucketStatus) 68 69 // Then 70 g.Expect(err).ToNot(HaveOccurred()) 71 g.Expect(status).ToNot(BeZero()) 72 g.Expect(status.Phase).To(Equal(v1alpha2.BucketReady)) 73 g.Expect(status.Reason).To(Equal(v1alpha2.BucketPolicyUpdated)) 74 }) 75 76 t.Run("NoBucket", func(t *testing.T) { 77 // Given 78 g := NewGomegaWithT(t) 79 ctx := context.TODO() 80 relistInterval := time.Minute 81 now := time.Now() 82 data := testData("test-bucket", v1alpha2.BucketPolicyReadOnly) 83 data.ObjectMeta.Generation = int64(1) 84 data.Status.ObservedGeneration = int64(2) 85 remoteName := fmt.Sprintf("%s-123", data.Name) 86 url := "http://localhost" 87 88 store := new(automock.Store) 89 defer store.AssertExpectations(t) 90 91 store.On("CreateBucket", data.Namespace, data.Name, string(data.Spec.Region)).Return(remoteName, nil).Once() 92 store.On("SetBucketPolicy", remoteName, data.Spec.Policy).Return(nil).Once() 93 94 handler := bucket.New(log, fakeRecorder(), store, url, relistInterval) 95 96 // When 97 status, err := handler.Do(ctx, now, data, data.Spec.CommonBucketSpec, data.Status.CommonBucketStatus) 98 99 // Then 100 g.Expect(err).ToNot(HaveOccurred()) 101 g.Expect(status).ToNot(BeZero()) 102 g.Expect(status.Phase).To(Equal(v1alpha2.BucketReady)) 103 g.Expect(status.Reason).To(Equal(v1alpha2.BucketPolicyUpdated)) 104 g.Expect(status.RemoteName).To(Equal(remoteName)) 105 g.Expect(status.URL).To(Equal(fmt.Sprintf("%s/%s", url, remoteName))) 106 }) 107 108 t.Run("BucketCreationFailure", func(t *testing.T) { 109 // Given 110 g := NewGomegaWithT(t) 111 ctx := context.TODO() 112 relistInterval := time.Minute 113 now := time.Now() 114 data := testData("test-bucket", v1alpha2.BucketPolicyReadOnly) 115 data.ObjectMeta.Generation = int64(1) 116 data.Status.ObservedGeneration = int64(2) 117 url := "http://localhost" 118 119 store := new(automock.Store) 120 defer store.AssertExpectations(t) 121 122 store.On("CreateBucket", data.Namespace, data.Name, string(data.Spec.Region)).Return("", errors.New("nope")).Once() 123 124 handler := bucket.New(log, fakeRecorder(), store, url, relistInterval) 125 126 // When 127 status, err := handler.Do(ctx, now, data, data.Spec.CommonBucketSpec, data.Status.CommonBucketStatus) 128 129 // Then 130 g.Expect(err).To(HaveOccurred()) 131 g.Expect(status).ToNot(BeZero()) 132 g.Expect(status.Phase).To(Equal(v1alpha2.BucketFailed)) 133 g.Expect(status.Reason).To(Equal(v1alpha2.BucketCreationFailure)) 134 }) 135 136 t.Run("BucketPolicyUpdateFailed", func(t *testing.T) { 137 // Given 138 g := NewGomegaWithT(t) 139 ctx := context.TODO() 140 relistInterval := time.Minute 141 now := time.Now() 142 data := testData("test-bucket", v1alpha2.BucketPolicyReadOnly) 143 data.ObjectMeta.Generation = int64(1) 144 data.Status.ObservedGeneration = int64(2) 145 remoteName := fmt.Sprintf("%s-123", data.Name) 146 url := "http://localhost" 147 148 store := new(automock.Store) 149 defer store.AssertExpectations(t) 150 151 store.On("CreateBucket", data.Namespace, data.Name, string(data.Spec.Region)).Return(remoteName, nil).Once() 152 store.On("SetBucketPolicy", remoteName, data.Spec.Policy).Return(errors.New("nope")).Once() 153 154 handler := bucket.New(log, fakeRecorder(), store, url, relistInterval) 155 156 // When 157 status, err := handler.Do(ctx, now, data, data.Spec.CommonBucketSpec, data.Status.CommonBucketStatus) 158 159 // Then 160 g.Expect(err).To(HaveOccurred()) 161 g.Expect(status).ToNot(BeZero()) 162 g.Expect(status.Phase).To(Equal(v1alpha2.BucketFailed)) 163 g.Expect(status.Reason).To(Equal(v1alpha2.BucketPolicyUpdateFailed)) 164 g.Expect(status.RemoteName).To(Equal(remoteName)) 165 g.Expect(status.URL).To(Equal(fmt.Sprintf("%s/%s", url, remoteName))) 166 }) 167 } 168 169 func TestBucketHandler_Handle_OnReady(t *testing.T) { 170 t.Run("NotTaken", func(t *testing.T) { 171 // Given 172 g := NewGomegaWithT(t) 173 ctx := context.TODO() 174 relistInterval := time.Minute 175 now := time.Now() 176 data := testData("test-bucket", v1alpha2.BucketPolicyReadOnly) 177 data.ObjectMeta.Generation = int64(1) 178 data.Status.ObservedGeneration = int64(1) 179 data.Status.Phase = v1alpha2.BucketReady 180 data.Status.LastHeartbeatTime = v1.Now() 181 182 store := new(automock.Store) 183 defer store.AssertExpectations(t) 184 185 handler := bucket.New(log, fakeRecorder(), store, "https://localhost", relistInterval) 186 187 // When 188 status, err := handler.Do(ctx, now, data, data.Spec.CommonBucketSpec, data.Status.CommonBucketStatus) 189 190 // Then 191 g.Expect(err).ToNot(HaveOccurred()) 192 g.Expect(status).To(BeZero()) 193 }) 194 195 t.Run("NotChanged", func(t *testing.T) { 196 // Given 197 g := NewGomegaWithT(t) 198 ctx := context.TODO() 199 relistInterval := time.Minute 200 now := time.Now() 201 data := testData("test-bucket", v1alpha2.BucketPolicyReadOnly) 202 data.ObjectMeta.Generation = int64(1) 203 data.Status.ObservedGeneration = int64(1) 204 data.Status.Phase = v1alpha2.BucketReady 205 data.Status.LastHeartbeatTime = v1.NewTime(now.Add(-2 * relistInterval)) 206 data.Status.RemoteName = fmt.Sprintf("%s-123", data.Name) 207 208 store := new(automock.Store) 209 defer store.AssertExpectations(t) 210 211 store.On("BucketExists", data.Status.RemoteName).Return(true, nil).Once() 212 store.On("CompareBucketPolicy", data.Status.RemoteName, data.Spec.Policy).Return(true, nil).Once() 213 214 handler := bucket.New(log, fakeRecorder(), store, "https://localhost", relistInterval) 215 216 // When 217 status, err := handler.Do(ctx, now, data, data.Spec.CommonBucketSpec, data.Status.CommonBucketStatus) 218 219 // Then 220 g.Expect(err).ToNot(HaveOccurred()) 221 g.Expect(status).ToNot(BeZero()) 222 g.Expect(status.Phase).To(Equal(v1alpha2.BucketReady)) 223 g.Expect(status.Reason).To(Equal(v1alpha2.BucketPolicyUpdated)) 224 }) 225 226 t.Run("MissingBucket", func(t *testing.T) { 227 // Given 228 g := NewGomegaWithT(t) 229 ctx := context.TODO() 230 relistInterval := time.Minute 231 now := time.Now() 232 data := testData("test-bucket", v1alpha2.BucketPolicyReadOnly) 233 data.ObjectMeta.Generation = int64(1) 234 data.Status.ObservedGeneration = int64(1) 235 data.Status.Phase = v1alpha2.BucketReady 236 data.Status.LastHeartbeatTime = v1.NewTime(now.Add(-2 * relistInterval)) 237 data.Status.RemoteName = fmt.Sprintf("%s-123", data.Name) 238 239 store := new(automock.Store) 240 defer store.AssertExpectations(t) 241 242 store.On("BucketExists", data.Status.RemoteName).Return(false, nil).Once() 243 244 handler := bucket.New(log, fakeRecorder(), store, "https://localhost", relistInterval) 245 246 // When 247 status, err := handler.Do(ctx, now, data, data.Spec.CommonBucketSpec, data.Status.CommonBucketStatus) 248 249 // Then 250 g.Expect(err).To(HaveOccurred()) 251 g.Expect(status).ToNot(BeZero()) 252 g.Expect(status.Phase).To(Equal(v1alpha2.BucketFailed)) 253 g.Expect(status.Reason).To(Equal(v1alpha2.BucketNotFound)) 254 }) 255 256 t.Run("BucketVerificationFailure", func(t *testing.T) { 257 // Given 258 g := NewGomegaWithT(t) 259 ctx := context.TODO() 260 relistInterval := time.Minute 261 now := time.Now() 262 data := testData("test-bucket", v1alpha2.BucketPolicyReadOnly) 263 data.ObjectMeta.Generation = int64(1) 264 data.Status.ObservedGeneration = int64(1) 265 data.Status.Phase = v1alpha2.BucketReady 266 data.Status.LastHeartbeatTime = v1.NewTime(now.Add(-2 * relistInterval)) 267 data.Status.RemoteName = fmt.Sprintf("%s-123", data.Name) 268 269 store := new(automock.Store) 270 defer store.AssertExpectations(t) 271 272 store.On("BucketExists", data.Status.RemoteName).Return(false, errors.New("nope")).Once() 273 274 handler := bucket.New(log, fakeRecorder(), store, "https://localhost", relistInterval) 275 276 // When 277 status, err := handler.Do(ctx, now, data, data.Spec.CommonBucketSpec, data.Status.CommonBucketStatus) 278 279 // Then 280 g.Expect(err).To(HaveOccurred()) 281 g.Expect(status).ToNot(BeZero()) 282 g.Expect(status.Phase).To(Equal(v1alpha2.BucketFailed)) 283 g.Expect(status.Reason).To(Equal(v1alpha2.BucketVerificationFailure)) 284 }) 285 286 t.Run("PolicyModified", func(t *testing.T) { 287 // Given 288 g := NewGomegaWithT(t) 289 ctx := context.TODO() 290 relistInterval := time.Minute 291 now := time.Now() 292 data := testData("test-bucket", v1alpha2.BucketPolicyReadOnly) 293 data.ObjectMeta.Generation = int64(1) 294 data.Status.ObservedGeneration = int64(1) 295 data.Status.Phase = v1alpha2.BucketReady 296 data.Status.LastHeartbeatTime = v1.NewTime(now.Add(-2 * relistInterval)) 297 data.Status.RemoteName = fmt.Sprintf("%s-123", data.Name) 298 299 store := new(automock.Store) 300 defer store.AssertExpectations(t) 301 302 store.On("BucketExists", data.Status.RemoteName).Return(true, nil).Once() 303 store.On("CompareBucketPolicy", data.Status.RemoteName, data.Spec.Policy).Return(false, nil).Once() 304 store.On("SetBucketPolicy", data.Status.RemoteName, data.Spec.Policy).Return(nil).Once() 305 306 handler := bucket.New(log, fakeRecorder(), store, "https://localhost", relistInterval) 307 308 // When 309 status, err := handler.Do(ctx, now, data, data.Spec.CommonBucketSpec, data.Status.CommonBucketStatus) 310 311 // Then 312 g.Expect(err).ToNot(HaveOccurred()) 313 g.Expect(status).ToNot(BeZero()) 314 g.Expect(status.Phase).To(Equal(v1alpha2.BucketReady)) 315 g.Expect(status.Reason).To(Equal(v1alpha2.BucketPolicyUpdated)) 316 }) 317 318 t.Run("PolicyModificationError", func(t *testing.T) { 319 // Given 320 g := NewGomegaWithT(t) 321 ctx := context.TODO() 322 relistInterval := time.Minute 323 now := time.Now() 324 data := testData("test-bucket", v1alpha2.BucketPolicyReadOnly) 325 data.ObjectMeta.Generation = int64(1) 326 data.Status.ObservedGeneration = int64(1) 327 data.Status.Phase = v1alpha2.BucketReady 328 data.Status.LastHeartbeatTime = v1.NewTime(now.Add(-2 * relistInterval)) 329 data.Status.RemoteName = fmt.Sprintf("%s-123", data.Name) 330 331 store := new(automock.Store) 332 defer store.AssertExpectations(t) 333 334 store.On("BucketExists", data.Status.RemoteName).Return(true, nil).Once() 335 store.On("CompareBucketPolicy", data.Status.RemoteName, data.Spec.Policy).Return(false, nil).Once() 336 store.On("SetBucketPolicy", data.Status.RemoteName, data.Spec.Policy).Return(errors.New("nope")).Once() 337 338 handler := bucket.New(log, fakeRecorder(), store, "https://localhost", relistInterval) 339 340 // When 341 status, err := handler.Do(ctx, now, data, data.Spec.CommonBucketSpec, data.Status.CommonBucketStatus) 342 343 // Then 344 g.Expect(err).To(HaveOccurred()) 345 g.Expect(status).ToNot(BeZero()) 346 g.Expect(status.Phase).To(Equal(v1alpha2.BucketFailed)) 347 g.Expect(status.Reason).To(Equal(v1alpha2.BucketPolicyUpdateFailed)) 348 }) 349 350 t.Run("PolicyCompareError", func(t *testing.T) { 351 // Given 352 g := NewGomegaWithT(t) 353 ctx := context.TODO() 354 relistInterval := time.Minute 355 now := time.Now() 356 data := testData("test-bucket", v1alpha2.BucketPolicyReadOnly) 357 data.ObjectMeta.Generation = int64(1) 358 data.Status.ObservedGeneration = int64(1) 359 data.Status.Phase = v1alpha2.BucketReady 360 data.Status.LastHeartbeatTime = v1.NewTime(now.Add(-2 * relistInterval)) 361 data.Status.RemoteName = fmt.Sprintf("%s-123", data.Name) 362 363 store := new(automock.Store) 364 defer store.AssertExpectations(t) 365 366 store.On("BucketExists", data.Status.RemoteName).Return(true, nil).Once() 367 store.On("CompareBucketPolicy", data.Status.RemoteName, data.Spec.Policy).Return(false, errors.New("nope")).Once() 368 369 handler := bucket.New(log, fakeRecorder(), store, "https://localhost", relistInterval) 370 371 // When 372 status, err := handler.Do(ctx, now, data, data.Spec.CommonBucketSpec, data.Status.CommonBucketStatus) 373 374 // Then 375 g.Expect(err).To(HaveOccurred()) 376 g.Expect(status).ToNot(BeZero()) 377 g.Expect(status.Phase).To(Equal(v1alpha2.BucketFailed)) 378 g.Expect(status.Reason).To(Equal(v1alpha2.BucketPolicyVerificationFailed)) 379 }) 380 } 381 382 func TestBucketHandler_Handle_OnFailed(t *testing.T) { 383 t.Run("BucketCreationFailure", func(t *testing.T) { 384 // Given 385 g := NewGomegaWithT(t) 386 ctx := context.TODO() 387 relistInterval := time.Minute 388 now := time.Now() 389 data := testData("test-bucket", v1alpha2.BucketPolicyReadOnly) 390 data.ObjectMeta.Generation = int64(1) 391 data.Status.ObservedGeneration = int64(1) 392 data.Status.Phase = v1alpha2.BucketFailed 393 data.Status.Reason = v1alpha2.BucketCreationFailure 394 remoteName := fmt.Sprintf("%s-123", data.Name) 395 url := "http://localhost" 396 397 store := new(automock.Store) 398 defer store.AssertExpectations(t) 399 400 store.On("CreateBucket", data.Namespace, data.Name, string(data.Spec.Region)).Return(remoteName, nil).Once() 401 store.On("SetBucketPolicy", remoteName, data.Spec.Policy).Return(nil).Once() 402 403 handler := bucket.New(log, fakeRecorder(), store, url, relistInterval) 404 405 // When 406 status, err := handler.Do(ctx, now, data, data.Spec.CommonBucketSpec, data.Status.CommonBucketStatus) 407 408 // Then 409 g.Expect(err).ToNot(HaveOccurred()) 410 g.Expect(status).ToNot(BeZero()) 411 g.Expect(status.Phase).To(Equal(v1alpha2.BucketReady)) 412 g.Expect(status.Reason).To(Equal(v1alpha2.BucketPolicyUpdated)) 413 g.Expect(status.RemoteName).To(Equal(remoteName)) 414 g.Expect(status.URL).To(Equal(fmt.Sprintf("%s/%s", url, remoteName))) 415 }) 416 417 t.Run("BucketVerificationFailure", func(t *testing.T) { 418 // Given 419 g := NewGomegaWithT(t) 420 ctx := context.TODO() 421 relistInterval := time.Minute 422 now := time.Now() 423 data := testData("test-bucket", v1alpha2.BucketPolicyReadOnly) 424 data.ObjectMeta.Generation = int64(1) 425 data.Status.ObservedGeneration = int64(1) 426 data.Status.Phase = v1alpha2.BucketFailed 427 data.Status.Reason = v1alpha2.BucketVerificationFailure 428 data.Status.RemoteName = fmt.Sprintf("%s-123", data.Name) 429 430 store := new(automock.Store) 431 defer store.AssertExpectations(t) 432 433 store.On("BucketExists", data.Status.RemoteName).Return(true, nil).Once() 434 store.On("CompareBucketPolicy", data.Status.RemoteName, data.Spec.Policy).Return(true, nil).Once() 435 436 handler := bucket.New(log, fakeRecorder(), store, "https://localhost", relistInterval) 437 438 // When 439 status, err := handler.Do(ctx, now, data, data.Spec.CommonBucketSpec, data.Status.CommonBucketStatus) 440 441 // Then 442 g.Expect(err).ToNot(HaveOccurred()) 443 g.Expect(status).ToNot(BeZero()) 444 g.Expect(status.Phase).To(Equal(v1alpha2.BucketReady)) 445 g.Expect(status.Reason).To(Equal(v1alpha2.BucketPolicyUpdated)) 446 }) 447 448 t.Run("BucketPolicyUpdateFailed", func(t *testing.T) { 449 // Given 450 g := NewGomegaWithT(t) 451 ctx := context.TODO() 452 relistInterval := time.Minute 453 now := time.Now() 454 data := testData("test-bucket", v1alpha2.BucketPolicyReadOnly) 455 data.ObjectMeta.Generation = int64(1) 456 data.Status.ObservedGeneration = int64(1) 457 data.Status.Phase = v1alpha2.BucketFailed 458 data.Status.Reason = v1alpha2.BucketPolicyUpdateFailed 459 data.Status.RemoteName = fmt.Sprintf("%s-123", data.Name) 460 461 store := new(automock.Store) 462 defer store.AssertExpectations(t) 463 464 store.On("BucketExists", data.Status.RemoteName).Return(true, nil).Once() 465 store.On("CompareBucketPolicy", data.Status.RemoteName, data.Spec.Policy).Return(true, nil).Once() 466 467 handler := bucket.New(log, fakeRecorder(), store, "https://localhost", relistInterval) 468 469 // When 470 status, err := handler.Do(ctx, now, data, data.Spec.CommonBucketSpec, data.Status.CommonBucketStatus) 471 472 // Then 473 g.Expect(err).ToNot(HaveOccurred()) 474 g.Expect(status).ToNot(BeZero()) 475 g.Expect(status.Phase).To(Equal(v1alpha2.BucketReady)) 476 g.Expect(status.Reason).To(Equal(v1alpha2.BucketPolicyUpdated)) 477 }) 478 479 t.Run("BucketNotFound", func(t *testing.T) { 480 // Given 481 g := NewGomegaWithT(t) 482 ctx := context.TODO() 483 relistInterval := time.Minute 484 now := time.Now() 485 data := testData("test-bucket", v1alpha2.BucketPolicyReadOnly) 486 data.ObjectMeta.Generation = int64(1) 487 data.Status.ObservedGeneration = int64(1) 488 data.Status.Phase = v1alpha2.BucketFailed 489 data.Status.Reason = v1alpha2.BucketNotFound 490 remoteName := fmt.Sprintf("%s-123", data.Name) 491 url := "http://localhost" 492 493 store := new(automock.Store) 494 defer store.AssertExpectations(t) 495 496 store.On("CreateBucket", data.Namespace, data.Name, string(data.Spec.Region)).Return(remoteName, nil).Once() 497 store.On("SetBucketPolicy", remoteName, data.Spec.Policy).Return(nil).Once() 498 499 handler := bucket.New(log, fakeRecorder(), store, url, relistInterval) 500 501 // When 502 status, err := handler.Do(ctx, now, data, data.Spec.CommonBucketSpec, data.Status.CommonBucketStatus) 503 504 // Then 505 g.Expect(err).ToNot(HaveOccurred()) 506 g.Expect(status).ToNot(BeZero()) 507 g.Expect(status.Phase).To(Equal(v1alpha2.BucketReady)) 508 g.Expect(status.Reason).To(Equal(v1alpha2.BucketPolicyUpdated)) 509 g.Expect(status.RemoteName).To(Equal(remoteName)) 510 g.Expect(status.URL).To(Equal(fmt.Sprintf("%s/%s", url, remoteName))) 511 }) 512 } 513 514 func TestBucketHandler_Handle_OnDelete(t *testing.T) { 515 t.Run("WithRemoteName", func(t *testing.T) { 516 // Given 517 g := NewGomegaWithT(t) 518 ctx := context.TODO() 519 relistInterval := time.Minute 520 now := time.Now() 521 data := testData("test-bucket", v1alpha2.BucketPolicyReadOnly) 522 deletionTimestamp := v1.Now() 523 data.ObjectMeta.DeletionTimestamp = &deletionTimestamp 524 data.Status.RemoteName = fmt.Sprintf("%s-123", data.Name) 525 526 store := new(automock.Store) 527 defer store.AssertExpectations(t) 528 529 store.On("DeleteBucket", ctx, data.Status.RemoteName).Return(nil).Once() 530 531 handler := bucket.New(log, fakeRecorder(), store, "https://localhost", relistInterval) 532 533 // When 534 status, err := handler.Do(ctx, now, data, data.Spec.CommonBucketSpec, data.Status.CommonBucketStatus) 535 536 // Then 537 g.Expect(err).ToNot(HaveOccurred()) 538 g.Expect(status).To(BeZero()) 539 }) 540 541 t.Run("NoRemoteName", func(t *testing.T) { 542 // Given 543 g := NewGomegaWithT(t) 544 ctx := context.TODO() 545 relistInterval := time.Minute 546 now := time.Now() 547 data := testData("test-bucket", v1alpha2.BucketPolicyReadOnly) 548 deletionTimestamp := v1.Now() 549 data.ObjectMeta.DeletionTimestamp = &deletionTimestamp 550 551 store := new(automock.Store) 552 defer store.AssertExpectations(t) 553 554 handler := bucket.New(log, fakeRecorder(), store, "https://localhost", relistInterval) 555 556 // When 557 status, err := handler.Do(ctx, now, data, data.Spec.CommonBucketSpec, data.Status.CommonBucketStatus) 558 559 // Then 560 g.Expect(err).ToNot(HaveOccurred()) 561 g.Expect(status).To(BeZero()) 562 }) 563 564 t.Run("BucketNotFound", func(t *testing.T) { 565 // Given 566 g := NewGomegaWithT(t) 567 ctx := context.TODO() 568 relistInterval := time.Minute 569 now := time.Now() 570 data := testData("test-bucket", v1alpha2.BucketPolicyReadOnly) 571 deletionTimestamp := v1.Now() 572 data.ObjectMeta.DeletionTimestamp = &deletionTimestamp 573 data.Status.Reason = v1alpha2.BucketNotFound 574 data.Status.RemoteName = fmt.Sprintf("%s-123", data.Name) 575 576 store := new(automock.Store) 577 defer store.AssertExpectations(t) 578 579 handler := bucket.New(log, fakeRecorder(), store, "https://localhost", relistInterval) 580 581 // When 582 status, err := handler.Do(ctx, now, data, data.Spec.CommonBucketSpec, data.Status.CommonBucketStatus) 583 584 // Then 585 g.Expect(err).ToNot(HaveOccurred()) 586 g.Expect(status).To(BeZero()) 587 }) 588 589 t.Run("Error", func(t *testing.T) { 590 // Given 591 g := NewGomegaWithT(t) 592 ctx := context.TODO() 593 relistInterval := time.Minute 594 now := time.Now() 595 data := testData("test-bucket", v1alpha2.BucketPolicyReadOnly) 596 deletionTimestamp := v1.Now() 597 data.ObjectMeta.DeletionTimestamp = &deletionTimestamp 598 data.Status.RemoteName = fmt.Sprintf("%s-123", data.Name) 599 600 store := new(automock.Store) 601 defer store.AssertExpectations(t) 602 603 store.On("DeleteBucket", ctx, data.Status.RemoteName).Return(errors.New("nope")).Once() 604 605 handler := bucket.New(log, fakeRecorder(), store, "https://localhost", relistInterval) 606 607 // When 608 status, err := handler.Do(ctx, now, data, data.Spec.CommonBucketSpec, data.Status.CommonBucketStatus) 609 610 // Then 611 g.Expect(err).To(HaveOccurred()) 612 g.Expect(status).To(BeZero()) 613 }) 614 } 615 616 func fakeRecorder() record.EventRecorder { 617 return record.NewFakeRecorder(20) 618 } 619 620 func testData(name string, policy v1alpha2.BucketPolicy) *v1alpha2.Bucket { 621 return &v1alpha2.Bucket{ 622 ObjectMeta: v1.ObjectMeta{ 623 Name: name, 624 Namespace: fmt.Sprintf("%s-ns", name), 625 Generation: int64(1), 626 }, 627 Spec: v1alpha2.BucketSpec{ 628 CommonBucketSpec: v1alpha2.CommonBucketSpec{ 629 Policy: policy, 630 Region: "asia", 631 }, 632 }, 633 } 634 }