github.com/kyma-project/kyma/components/asset-store-controller-manager@v0.0.0-20191203152857-3792b5df17c5/internal/controllers/clusterbucket_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("ClusterBucket", func() {
    22  	var (
    23  		bucket     *assetstorev1alpha2.ClusterBucket
    24  		reconciler *ClusterBucketReconciler
    25  		mocks      *MockContainer
    26  		t          GinkgoTInterface
    27  		request    ctrl.Request
    28  	)
    29  
    30  	BeforeEach(func() {
    31  		bucket = newFixClusterBucket()
    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 = &ClusterBucketReconciler{
    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 ClusterBucket", func() {
    61  		By("creating the ClusterBucket")
    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 ClusterBucket")
    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  		err = k8sClient.Get(context.TODO(), request.NamespacedName, bucket)
   101  		// then
   102  		Expect(err).ToNot(HaveOccurred())
   103  		Expect(bucket.Status.Phase).To(Equal(assetstorev1alpha2.BucketReady))
   104  		Expect(bucket.Status.Reason).To(Equal(assetstorev1alpha2.BucketPolicyUpdated))
   105  
   106  		By("deleting the ClusterBucket")
   107  		// when
   108  		err = k8sClient.Delete(context.TODO(), bucket)
   109  		// then
   110  		Expect(err).ToNot(HaveOccurred())
   111  
   112  		// given
   113  		mocks.Store.On("DeleteBucket", mock.Anything, "test").Return(nil).Once()
   114  
   115  		// when
   116  		result, err = reconciler.Reconcile(request)
   117  		// then
   118  		Expect(err).ToNot(HaveOccurred())
   119  		Expect(result.Requeue).To(BeFalse())
   120  		Expect(result.RequeueAfter).To(Equal(60 * time.Hour))
   121  
   122  		// when
   123  		err = k8sClient.Get(context.TODO(), request.NamespacedName, bucket)
   124  		// then
   125  		Expect(err).To(HaveOccurred())
   126  		Expect(apiErrors.IsNotFound(err)).To(BeTrue())
   127  	})
   128  })
   129  
   130  func newFixClusterBucket() *assetstorev1alpha2.ClusterBucket {
   131  	return &assetstorev1alpha2.ClusterBucket{
   132  		ObjectMeta: ctrl.ObjectMeta{
   133  			Name: string(uuid.NewUUID()),
   134  		},
   135  		Spec: assetstorev1alpha2.ClusterBucketSpec{
   136  			CommonBucketSpec: assetstorev1alpha2.CommonBucketSpec{
   137  				Region: assetstorev1alpha2.BucketRegionAPNortheast1,
   138  				Policy: assetstorev1alpha2.BucketPolicyReadOnly,
   139  			},
   140  		},
   141  		Status: assetstorev1alpha2.ClusterBucketStatus{CommonBucketStatus: assetstorev1alpha2.CommonBucketStatus{
   142  			LastHeartbeatTime: v1.Now(),
   143  		}},
   144  	}
   145  }