github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/controllers/suite_test.go (about) 1 // Copyright 2019-2021 The Inspektor Gadget authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package controllers 16 17 import ( 18 "context" 19 "flag" 20 "fmt" 21 "math/rand" 22 "path/filepath" 23 "testing" 24 "time" 25 26 . "github.com/onsi/ginkgo" 27 . "github.com/onsi/gomega" 28 core "k8s.io/api/core/v1" 29 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 30 clientgoscheme "k8s.io/client-go/kubernetes/scheme" 31 "k8s.io/client-go/rest" 32 ctrl "sigs.k8s.io/controller-runtime" 33 "sigs.k8s.io/controller-runtime/pkg/client" 34 "sigs.k8s.io/controller-runtime/pkg/envtest" 35 logf "sigs.k8s.io/controller-runtime/pkg/log" 36 "sigs.k8s.io/controller-runtime/pkg/log/zap" 37 38 gadgetv1alpha1 "github.com/inspektor-gadget/inspektor-gadget/pkg/apis/gadget/v1alpha1" 39 "github.com/inspektor-gadget/inspektor-gadget/pkg/gadget-collection/gadgets" 40 //+kubebuilder:scaffold:imports 41 ) 42 43 var r *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano())) 44 45 // These tests use Ginkgo (BDD-style Go testing framework). Refer to 46 // http://onsi.github.io/ginkgo/ to learn more about Ginkgo. 47 48 var ( 49 cfg *rest.Config 50 k8sClient client.Client 51 testEnv *envtest.Environment 52 ) 53 54 var controllerTest = flag.Bool("controller-test", false, "run controller tests") 55 56 func TestAPIs(t *testing.T) { 57 if !*controllerTest { 58 t.Skip("skipping controller test.") 59 } 60 61 RegisterFailHandler(Fail) 62 63 RunSpecs(t, "Controller Suite") 64 } 65 66 var _ = BeforeSuite(func() { 67 logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true))) 68 69 By("bootstrapping test environment") 70 testEnv = &envtest.Environment{ 71 CRDDirectoryPaths: []string{filepath.Join("..", "resources", "crd", "bases")}, 72 ErrorIfCRDPathMissing: true, 73 } 74 75 var err error 76 cfg, err = testEnv.Start() 77 Expect(err).NotTo(HaveOccurred()) 78 Expect(cfg).NotTo(BeNil()) 79 80 err = gadgetv1alpha1.AddToScheme(clientgoscheme.Scheme) 81 Expect(err).NotTo(HaveOccurred()) 82 83 //+kubebuilder:scaffold:scheme 84 85 k8sClient, err = client.New(cfg, client.Options{Scheme: clientgoscheme.Scheme}) 86 Expect(err).NotTo(HaveOccurred()) 87 Expect(k8sClient).NotTo(BeNil()) 88 }, 60) 89 90 var _ = AfterSuite(func() { 91 By("tearing down the test environment") 92 err := testEnv.Stop() 93 Expect(err).NotTo(HaveOccurred()) 94 }) 95 96 // SetupTest creates a testing environment for the controller with the specific 97 // gadgets. It creates a temporary test namespace and automatically start and 98 // stop the TraceReconciler before and after tests. 99 func SetupTest(ctx context.Context, traceFactories map[string]gadgets.TraceFactory) *core.Namespace { 100 var managerCtx context.Context 101 var managerCancel context.CancelFunc 102 103 ns := &core.Namespace{} 104 105 BeforeEach(func() { 106 managerCtx, managerCancel = context.WithCancel(context.Background()) 107 108 *ns = core.Namespace{ 109 ObjectMeta: metav1.ObjectMeta{ 110 Name: fmt.Sprintf("test-gadget-%d", r.Intn(1000000)), 111 }, 112 } 113 err := k8sClient.Create(ctx, ns) 114 Expect(err).NotTo(HaveOccurred(), "failed to create test namespace") 115 116 mgr, err := ctrl.NewManager(cfg, ctrl.Options{}) 117 Expect(err).NotTo(HaveOccurred(), "failed to create manager") 118 119 // The node does not need to exist. It just needs to match the 120 // name in Trace resources. 121 node := "fake-node" 122 controller := &TraceReconciler{ 123 Client: mgr.GetClient(), 124 Scheme: mgr.GetScheme(), 125 Node: node, 126 TraceFactories: traceFactories, 127 TracerManager: nil, 128 } 129 err = controller.SetupWithManager(mgr) 130 Expect(err).NotTo(HaveOccurred(), "failed to setup controller") 131 132 go func() { 133 err := mgr.Start(managerCtx) 134 Expect(err).NotTo(HaveOccurred(), "failed to start manager") 135 }() 136 }) 137 138 AfterEach(func() { 139 managerCancel() 140 141 err := k8sClient.Delete(ctx, ns) 142 Expect(err).NotTo(HaveOccurred(), "failed to delete test namespace") 143 }) 144 145 return ns 146 }