github.com/kubevela/workflow@v0.6.0/controllers/suite_test.go (about)

     1  /*
     2  Copyright 2022 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package controllers
    18  
    19  import (
    20  	"path/filepath"
    21  	"testing"
    22  
    23  	"github.com/crossplane/crossplane-runtime/pkg/event"
    24  	. "github.com/onsi/ginkgo"
    25  	. "github.com/onsi/gomega"
    26  	"github.com/pkg/errors"
    27  	"k8s.io/apimachinery/pkg/api/meta"
    28  	"k8s.io/apimachinery/pkg/runtime"
    29  	"k8s.io/client-go/kubernetes/scheme"
    30  	"k8s.io/client-go/rest"
    31  	"sigs.k8s.io/controller-runtime/pkg/client"
    32  	"sigs.k8s.io/controller-runtime/pkg/envtest"
    33  	logf "sigs.k8s.io/controller-runtime/pkg/log"
    34  	"sigs.k8s.io/controller-runtime/pkg/log/zap"
    35  
    36  	"github.com/kubevela/workflow/api/v1alpha1"
    37  	"github.com/kubevela/workflow/pkg/cue/packages"
    38  	//+kubebuilder:scaffold:imports
    39  )
    40  
    41  // These tests use Ginkgo (BDD-style Go testing framework). Refer to
    42  // http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
    43  
    44  var cfg *rest.Config
    45  var k8sClient client.Client
    46  var testEnv *envtest.Environment
    47  var reconciler *WorkflowRunReconciler
    48  var testScheme *runtime.Scheme
    49  var recorder = NewFakeRecorder(10000)
    50  
    51  func TestAPIs(t *testing.T) {
    52  	RegisterFailHandler(Fail)
    53  
    54  	RunSpecs(t, "Controller Suite")
    55  }
    56  
    57  var _ = BeforeSuite(func() {
    58  	logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
    59  
    60  	By("bootstrapping test environment")
    61  	testEnv = &envtest.Environment{
    62  		CRDDirectoryPaths:     []string{filepath.Join("..", "charts", "vela-workflow", "crds")},
    63  		ErrorIfCRDPathMissing: true,
    64  	}
    65  
    66  	var err error
    67  	// cfg is defined in this file globally.
    68  	cfg, err = testEnv.Start()
    69  	Expect(err).NotTo(HaveOccurred())
    70  	Expect(cfg).NotTo(BeNil())
    71  
    72  	pd, err := packages.NewPackageDiscover(cfg)
    73  	Expect(err).To(BeNil())
    74  
    75  	testScheme = scheme.Scheme
    76  	err = v1alpha1.AddToScheme(testScheme)
    77  	Expect(err).NotTo(HaveOccurred())
    78  
    79  	//+kubebuilder:scaffold:scheme
    80  
    81  	k8sClient, err = client.New(cfg, client.Options{Scheme: testScheme})
    82  	Expect(err).NotTo(HaveOccurred())
    83  	Expect(k8sClient).NotTo(BeNil())
    84  
    85  	reconciler = &WorkflowRunReconciler{
    86  		Client:   k8sClient,
    87  		Scheme:   testScheme,
    88  		Recorder: event.NewAPIRecorder(recorder),
    89  		Args: Args{
    90  			PackageDiscover: pd,
    91  		},
    92  	}
    93  
    94  }, 60)
    95  
    96  var _ = AfterSuite(func() {
    97  	By("tearing down the test environment")
    98  	err := testEnv.Stop()
    99  	Expect(err).NotTo(HaveOccurred())
   100  })
   101  
   102  type FakeRecorder struct {
   103  	Events  chan string
   104  	Message map[string][]*Events
   105  }
   106  
   107  type Events struct {
   108  	Name      string
   109  	Namespace string
   110  	EventType string
   111  	Reason    string
   112  	Message   string
   113  }
   114  
   115  func (f *FakeRecorder) Event(object runtime.Object, eventtype, reason, message string) {
   116  	if f.Events != nil {
   117  		objectMeta, err := meta.Accessor(object)
   118  		if err != nil {
   119  			return
   120  		}
   121  
   122  		event := &Events{
   123  			Name:      objectMeta.GetName(),
   124  			Namespace: objectMeta.GetNamespace(),
   125  			EventType: eventtype,
   126  			Reason:    reason,
   127  			Message:   message,
   128  		}
   129  
   130  		records, ok := f.Message[objectMeta.GetName()]
   131  		if !ok {
   132  			f.Message[objectMeta.GetName()] = []*Events{event}
   133  			return
   134  		}
   135  
   136  		records = append(records, event)
   137  		f.Message[objectMeta.GetName()] = records
   138  
   139  	}
   140  }
   141  
   142  func (f *FakeRecorder) Eventf(object runtime.Object, eventtype, reason, messageFmt string, args ...interface{}) {
   143  	f.Event(object, eventtype, reason, messageFmt)
   144  }
   145  
   146  func (f *FakeRecorder) AnnotatedEventf(object runtime.Object, annotations map[string]string, eventtype, reason, messageFmt string, args ...interface{}) {
   147  	f.Eventf(object, eventtype, reason, messageFmt, args...)
   148  }
   149  
   150  func (f *FakeRecorder) GetEventsWithName(name string) ([]*Events, error) {
   151  	records, ok := f.Message[name]
   152  	if !ok {
   153  		return nil, errors.New("not found events")
   154  	}
   155  
   156  	return records, nil
   157  }
   158  
   159  // NewFakeRecorder creates new fake event recorder with event channel with
   160  // buffer of given size.
   161  func NewFakeRecorder(bufferSize int) *FakeRecorder {
   162  	return &FakeRecorder{
   163  		Events:  make(chan string, bufferSize),
   164  		Message: make(map[string][]*Events),
   165  	}
   166  }