github.com/kyma-project/kyma/components/asset-store-controller-manager@v0.0.0-20191203152857-3792b5df17c5/internal/controllers/suite_test.go (about)

     1  package controllers
     2  
     3  import (
     4  	"path/filepath"
     5  	"sync"
     6  	"testing"
     7  	"time"
     8  
     9  	assethook "github.com/kyma-project/kyma/components/asset-store-controller-manager/internal/assethook/automock"
    10  	loader "github.com/kyma-project/kyma/components/asset-store-controller-manager/internal/loader/automock"
    11  	store "github.com/kyma-project/kyma/components/asset-store-controller-manager/internal/store/automock"
    12  	assetstorev1alpha2 "github.com/kyma-project/kyma/components/asset-store-controller-manager/pkg/apis/assetstore/v1alpha2"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  	"k8s.io/client-go/kubernetes/scheme"
    16  	"k8s.io/client-go/rest"
    17  	"sigs.k8s.io/controller-runtime"
    18  	"sigs.k8s.io/controller-runtime/pkg/client"
    19  	"sigs.k8s.io/controller-runtime/pkg/envtest"
    20  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    21  	"sigs.k8s.io/controller-runtime/pkg/log/zap"
    22  	"sigs.k8s.io/controller-runtime/pkg/manager"
    23  	// +kubebuilder:scaffold:imports
    24  )
    25  
    26  // These tests use Ginkgo (BDD-style Go testing framework). Refer to
    27  // http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
    28  
    29  var cfg *rest.Config
    30  var k8sClient client.Client
    31  var testEnv *envtest.Environment
    32  
    33  // StartTestManager adds recFn
    34  func StartTestManager(mgr manager.Manager, g *GomegaWithT) (chan struct{}, *sync.WaitGroup) {
    35  	stop := make(chan struct{})
    36  	wg := &sync.WaitGroup{}
    37  	go func() {
    38  		wg.Add(1)
    39  		g.Expect(mgr.Start(stop)).NotTo(HaveOccurred())
    40  		wg.Done()
    41  	}()
    42  	return stop, wg
    43  }
    44  
    45  func TestAPIs(t *testing.T) {
    46  	RegisterFailHandler(Fail)
    47  
    48  	RunSpecsWithDefaultAndCustomReporters(t,
    49  		"Controller Suite",
    50  		[]Reporter{envtest.NewlineReporter{}})
    51  }
    52  
    53  var _ = BeforeSuite(func(done Done) {
    54  	logf.SetLogger(zap.LoggerTo(GinkgoWriter, true))
    55  
    56  	By("bootstrapping test environment")
    57  	testEnv = &envtest.Environment{
    58  		CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
    59  	}
    60  
    61  	var err error
    62  	cfg, err = testEnv.Start()
    63  	Expect(err).ToNot(HaveOccurred())
    64  	Expect(cfg).ToNot(BeNil())
    65  
    66  	err = assetstorev1alpha2.AddToScheme(scheme.Scheme)
    67  	Expect(err).NotTo(HaveOccurred())
    68  
    69  	err = assetstorev1alpha2.AddToScheme(scheme.Scheme)
    70  	Expect(err).NotTo(HaveOccurred())
    71  
    72  	err = assetstorev1alpha2.AddToScheme(scheme.Scheme)
    73  	Expect(err).NotTo(HaveOccurred())
    74  
    75  	err = assetstorev1alpha2.AddToScheme(scheme.Scheme)
    76  	Expect(err).NotTo(HaveOccurred())
    77  
    78  	// +kubebuilder:scaffold:scheme
    79  
    80  	k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
    81  	Expect(err).ToNot(HaveOccurred())
    82  	Expect(k8sClient).ToNot(BeNil())
    83  
    84  	close(done)
    85  }, 60)
    86  
    87  var _ = AfterSuite(func() {
    88  	By("tearing down the test environment")
    89  	err := testEnv.Stop()
    90  	Expect(err).ToNot(HaveOccurred())
    91  })
    92  
    93  type MockContainer struct {
    94  	Store     *store.Store
    95  	Extractor *assethook.MetadataExtractor
    96  	Mutator   *assethook.Mutator
    97  	Validator *assethook.Validator
    98  	Loader    *loader.Loader
    99  }
   100  
   101  func NewMockContainer() *MockContainer {
   102  	return &MockContainer{
   103  		Store:     new(store.Store),
   104  		Extractor: new(assethook.MetadataExtractor),
   105  		Mutator:   new(assethook.Mutator),
   106  		Validator: new(assethook.Validator),
   107  		Loader:    new(loader.Loader),
   108  	}
   109  }
   110  
   111  func (c *MockContainer) AssertExpetactions(t GinkgoTInterface) {
   112  	c.Store.AssertExpectations(t)
   113  	c.Extractor.AssertExpectations(t)
   114  	c.Mutator.AssertExpectations(t)
   115  	c.Validator.AssertExpectations(t)
   116  	c.Loader.AssertExpectations(t)
   117  }
   118  
   119  func validateReconcilation(err error, result controllerruntime.Result) {
   120  	Expect(err).ToNot(HaveOccurred())
   121  	Expect(result.Requeue).To(BeFalse())
   122  	Expect(result.RequeueAfter).To(Equal(60 * time.Hour))
   123  }
   124  
   125  func validateAsset(status assetstorev1alpha2.CommonAssetStatus, meta controllerruntime.ObjectMeta, expectedBaseURL string, files []string, expectedPhase assetstorev1alpha2.AssetPhase, expectedReason assetstorev1alpha2.AssetReason) {
   126  	Expect(status.Phase).To(Equal(expectedPhase))
   127  	Expect(status.Reason).To(Equal(expectedReason))
   128  	Expect(status.AssetRef.BaseURL).To(Equal(expectedBaseURL))
   129  	Expect(status.AssetRef.Files).To(HaveLen(len(files)))
   130  	Expect(meta.Finalizers).To(ContainElement("test"))
   131  }