github.com/IBM-Blockchain/fabric-operator@v1.0.4/integration/migration/migration_suite_test.go (about) 1 /* 2 * Copyright contributors to the Hyperledger Fabric Operator project 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at: 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 package migration_test 20 21 import ( 22 "context" 23 "fmt" 24 "os" 25 "testing" 26 27 apis "github.com/IBM-Blockchain/fabric-operator/api" 28 "github.com/IBM-Blockchain/fabric-operator/pkg/global" 29 "github.com/IBM-Blockchain/fabric-operator/pkg/k8s/controllerclient" 30 . "github.com/onsi/ginkgo/v2" 31 . "github.com/onsi/gomega" 32 corev1 "k8s.io/api/core/v1" 33 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 34 "k8s.io/apimachinery/pkg/runtime" 35 "k8s.io/apimachinery/pkg/watch" 36 "k8s.io/client-go/kubernetes" 37 "sigs.k8s.io/controller-runtime/pkg/client/config" 38 "sigs.k8s.io/controller-runtime/pkg/manager" 39 ) 40 41 func TestMigration(t *testing.T) { 42 RegisterFailHandler(Fail) 43 RunSpecs(t, "Migration Suite") 44 } 45 46 var ( 47 kclient *kubernetes.Clientset 48 client controllerclient.Client 49 scheme *runtime.Scheme 50 namespace string 51 mgr manager.Manager 52 killchan context.Context 53 ) 54 55 var _ = BeforeSuite(func() { 56 var err error 57 cfg, err := config.GetConfig() 58 Expect(err).NotTo(HaveOccurred()) 59 60 namespace = os.Getenv("OPERATOR_NAMESPACE") 61 if namespace == "" { 62 namespace = "operator-test" 63 } 64 namespace = fmt.Sprintf("%s-migration", namespace) 65 66 mgr, err = manager.New(cfg, manager.Options{ 67 Namespace: namespace, 68 MetricsBindAddress: "0", 69 }) 70 Expect(err).NotTo(HaveOccurred()) 71 72 err = apis.AddToScheme(mgr.GetScheme()) 73 Expect(err).NotTo(HaveOccurred()) 74 75 killchan = context.TODO() 76 go mgr.Start(killchan) 77 78 client = controllerclient.New(mgr.GetClient(), &global.ConfigSetter{}) 79 scheme = mgr.GetScheme() 80 81 kclient, err = kubernetes.NewForConfig(cfg) 82 Expect(err).NotTo(HaveOccurred()) 83 84 cleanup() 85 86 ns := &corev1.Namespace{} 87 ns.Name = namespace 88 err = client.Create(context.TODO(), ns) 89 Expect(err).NotTo(HaveOccurred()) 90 }) 91 92 var _ = AfterSuite(func() { 93 err := cleanup() 94 Expect(err).NotTo(HaveOccurred()) 95 96 killchan.Done() 97 }) 98 99 func cleanup() error { 100 ns := &corev1.Namespace{} 101 ns.Name = namespace 102 103 err := client.Delete(context.TODO(), ns) 104 if err != nil { 105 return err 106 } 107 108 opts := metav1.ListOptions{} 109 watchNamespace, err := kclient.CoreV1().Namespaces().Watch(context.TODO(), opts) 110 if err != nil { 111 return err 112 } 113 114 for { 115 resultChan := <-watchNamespace.ResultChan() 116 if resultChan.Type == watch.Deleted { 117 ns := resultChan.Object.(*corev1.Namespace) 118 if ns.Name == namespace { 119 break 120 } 121 } 122 } 123 124 return nil 125 }