github.com/banzaicloud/operator-tools@v0.28.10/pkg/reconciler/suite_test.go (about) 1 // Copyright © 2020 Banzai Cloud 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 reconciler_test 16 17 import ( 18 "context" 19 "fmt" 20 "os" 21 "path/filepath" 22 "testing" 23 24 "github.com/banzaicloud/operator-tools/pkg/utils" 25 "github.com/go-logr/logr" 26 "github.com/pborman/uuid" 27 corev1 "k8s.io/api/core/v1" 28 v1 "k8s.io/apimachinery/pkg/apis/meta/v1" 29 clientgoscheme "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 ) 34 35 var cfg *rest.Config 36 var k8sClient client.Client 37 var testEnv *envtest.Environment 38 var testNamespace = "test-" + uuid.New()[:8] 39 var controlNamespace = "control" 40 var log logr.Logger 41 42 func TestMain(m *testing.M) { 43 log = utils.Log 44 utils.GlobalLogLevel = 2 45 err := beforeSuite() 46 if err != nil { 47 fmt.Printf("%+v", err) 48 os.Exit(1) 49 } 50 code := m.Run() 51 err = afterSuite() 52 if err != nil { 53 fmt.Printf("%+v", err) 54 os.Exit(1) 55 } 56 os.Exit(code) 57 } 58 59 func beforeSuite() error { 60 testEnv = &envtest.Environment{ 61 CRDDirectoryPaths: []string{filepath.Join("..", "config", "crd", "bases")}, 62 } 63 64 var err error 65 66 cfg, err = testEnv.Start() 67 if err != nil { 68 return err 69 } 70 if cfg == nil { 71 return fmt.Errorf("failed to start testenv, config is nil") 72 } 73 74 k8sClient, err = client.New(cfg, client.Options{Scheme: clientgoscheme.Scheme}) 75 if err != nil { 76 return err 77 } 78 if k8sClient == nil { 79 return fmt.Errorf("failed to create k8s config") 80 } 81 82 for _, ns := range []string{controlNamespace, testNamespace} { 83 err := k8sClient.Create(context.TODO(), &corev1.Namespace{ 84 ObjectMeta: v1.ObjectMeta{ 85 Name: ns, 86 }, 87 }) 88 if err != nil { 89 return err 90 } 91 } 92 return nil 93 } 94 95 func afterSuite() error { 96 return testEnv.Stop() 97 } 98 99 func assertConfigMapList(t *testing.T, a func(l *corev1.ConfigMapList)) { 100 l := &corev1.ConfigMapList{} 101 102 if err := k8sClient.List(context.TODO(), l, client.InNamespace(controlNamespace)); err != nil { 103 t.Fatalf("+%v", err) 104 } 105 106 a(l) 107 } 108 109 func assertSecretList(t *testing.T, a func(l *corev1.SecretList)) { 110 l := &corev1.SecretList{} 111 112 if err := k8sClient.List(context.TODO(), l, client.InNamespace(controlNamespace)); err != nil { 113 t.Fatalf("+%v", err) 114 } 115 116 a(l) 117 }