github.com/kyma-project/kyma/components/asset-store-controller-manager@v0.0.0-20191203152857-3792b5df17c5/internal/controllers/bucket_controller_test.go (about) 1 package controllers 2 3 import ( 4 "context" 5 "time" 6 7 "github.com/kyma-project/kyma/components/asset-store-controller-manager/internal/finalizer" 8 assetstorev1alpha2 "github.com/kyma-project/kyma/components/asset-store-controller-manager/pkg/apis/assetstore/v1alpha2" 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 "github.com/stretchr/testify/mock" 12 apiErrors "k8s.io/apimachinery/pkg/api/errors" 13 "k8s.io/apimachinery/pkg/apis/meta/v1" 14 "k8s.io/apimachinery/pkg/types" 15 "k8s.io/apimachinery/pkg/util/uuid" 16 "k8s.io/client-go/tools/record" 17 ctrl "sigs.k8s.io/controller-runtime" 18 "sigs.k8s.io/controller-runtime/pkg/runtime/log" 19 ) 20 21 var _ = Describe("Bucket", func() { 22 var ( 23 bucket *assetstorev1alpha2.Bucket 24 reconciler *BucketReconciler 25 mocks *MockContainer 26 t GinkgoTInterface 27 request ctrl.Request 28 ) 29 30 BeforeEach(func() { 31 bucket = newFixBucket() 32 Expect(k8sClient.Create(context.TODO(), bucket)).To(Succeed()) 33 t = GinkgoT() 34 mocks = NewMockContainer() 35 36 request = ctrl.Request{ 37 NamespacedName: types.NamespacedName{ 38 Name: bucket.Name, 39 Namespace: bucket.Namespace, 40 }, 41 } 42 43 reconciler = &BucketReconciler{ 44 Client: k8sClient, 45 cacheSynchronizer: func(stop <-chan struct{}) bool { return true }, 46 Log: log.Log, 47 recorder: record.NewFakeRecorder(100), 48 relistInterval: 60 * time.Hour, 49 store: mocks.Store, 50 finalizer: finalizer.New("test"), 51 externalEndpoint: "https://minio.test.local", 52 maxConcurrentReconciles: 1, 53 } 54 }) 55 56 AfterEach(func() { 57 mocks.AssertExpetactions(t) 58 }) 59 60 It("should successfully create, update and delete Bucket", func() { 61 By("creating the Bucket") 62 // given 63 mocks.Store.On("CreateBucket", bucket.Namespace, bucket.Name, string(bucket.Spec.Region)).Return("test", nil).Once() 64 mocks.Store.On("SetBucketPolicy", "test", bucket.Spec.Policy).Return(nil).Once() 65 66 // when 67 result, err := reconciler.Reconcile(request) 68 // then 69 Expect(err).ToNot(HaveOccurred()) 70 Expect(result.Requeue).To(BeFalse()) 71 Expect(result.RequeueAfter).To(Equal(60 * time.Hour)) 72 73 // when 74 err = k8sClient.Get(context.TODO(), request.NamespacedName, bucket) 75 // then 76 Expect(err).ToNot(HaveOccurred()) 77 Expect(bucket.Status.Phase).To(Equal(assetstorev1alpha2.BucketReady)) 78 Expect(bucket.Status.Reason).To(Equal(assetstorev1alpha2.BucketPolicyUpdated)) 79 80 By("updating the Bucket") 81 // when 82 bucket.Spec.Policy = assetstorev1alpha2.BucketPolicyNone 83 err = k8sClient.Update(context.TODO(), bucket) 84 // then 85 Expect(err).ToNot(HaveOccurred()) 86 87 // given 88 mocks.Store.On("BucketExists", "test").Return(true, nil).Once() 89 mocks.Store.On("CompareBucketPolicy", "test", bucket.Spec.Policy).Return(false, nil).Once() 90 mocks.Store.On("SetBucketPolicy", "test", bucket.Spec.Policy).Return(nil).Once() 91 92 // when 93 result, err = reconciler.Reconcile(request) 94 // then 95 Expect(err).ToNot(HaveOccurred()) 96 Expect(result.Requeue).To(BeFalse()) 97 Expect(result.RequeueAfter).To(Equal(60 * time.Hour)) 98 99 // when 100 Expect(k8sClient.Get(context.TODO(), request.NamespacedName, bucket)).To(Succeed()) 101 // then 102 Expect(bucket.Status.Phase).To(Equal(assetstorev1alpha2.BucketReady)) 103 Expect(bucket.Status.Reason).To(Equal(assetstorev1alpha2.BucketPolicyUpdated)) 104 105 By("deleting the Bucket") 106 // when 107 err = k8sClient.Delete(context.TODO(), bucket) 108 // then 109 Expect(err).ToNot(HaveOccurred()) 110 111 // given 112 mocks.Store.On("DeleteBucket", mock.Anything, "test").Return(nil).Once() 113 114 // when 115 result, err = reconciler.Reconcile(request) 116 // then 117 Expect(err).ToNot(HaveOccurred()) 118 Expect(result.Requeue).To(BeFalse()) 119 Expect(result.RequeueAfter).To(Equal(60 * time.Hour)) 120 121 // when 122 err = k8sClient.Get(context.TODO(), request.NamespacedName, bucket) 123 // then 124 Expect(err).To(HaveOccurred()) 125 Expect(apiErrors.IsNotFound(err)).To(BeTrue()) 126 }) 127 }) 128 129 func newFixBucket() *assetstorev1alpha2.Bucket { 130 return &assetstorev1alpha2.Bucket{ 131 ObjectMeta: ctrl.ObjectMeta{ 132 Name: string(uuid.NewUUID()), 133 Namespace: "default", 134 }, 135 Spec: assetstorev1alpha2.BucketSpec{ 136 CommonBucketSpec: assetstorev1alpha2.CommonBucketSpec{ 137 Region: assetstorev1alpha2.BucketRegionAPNortheast1, 138 Policy: assetstorev1alpha2.BucketPolicyReadOnly, 139 }, 140 }, 141 Status: assetstorev1alpha2.BucketStatus{CommonBucketStatus: assetstorev1alpha2.CommonBucketStatus{ 142 LastHeartbeatTime: v1.Now(), 143 }}, 144 } 145 }