github.com/oam-dev/cluster-gateway@v1.9.0/e2e/framework/framework.go (about) 1 package framework 2 3 import ( 4 . "github.com/onsi/ginkgo/v2" 5 . "github.com/onsi/gomega" 6 "k8s.io/apimachinery/pkg/util/rand" 7 "k8s.io/client-go/kubernetes" 8 "k8s.io/client-go/rest" 9 "k8s.io/client-go/tools/clientcmd" 10 "sigs.k8s.io/controller-runtime/pkg/client" 11 12 "github.com/oam-dev/cluster-gateway/pkg/generated/clientset/versioned" 13 ) 14 15 // unique identifier of the e2e run 16 var RunID = rand.String(6) 17 18 type Framework interface { 19 HubRESTConfig() *rest.Config 20 TestClusterName() string 21 IsOCMInstalled() bool 22 23 HubNativeClient() kubernetes.Interface 24 HubRuntimeClient() client.Client 25 HubGatewayClient() versioned.Interface 26 } 27 28 var _ Framework = &framework{} 29 30 type framework struct { 31 basename string 32 ctx *E2EContext 33 } 34 35 func NewE2EFramework(basename string) Framework { 36 f := &framework{ 37 basename: basename, 38 ctx: context, 39 } 40 AfterEach(f.AfterEach) 41 BeforeEach(f.BeforeEach) 42 return f 43 } 44 45 func (f *framework) HubRESTConfig() *rest.Config { 46 restConfig, err := clientcmd.BuildConfigFromFlags("", f.ctx.HubKubeConfig) 47 Expect(err).NotTo(HaveOccurred()) 48 return restConfig 49 } 50 51 func (f *framework) HubNativeClient() kubernetes.Interface { 52 cfg := f.HubRESTConfig() 53 nativeClient, err := kubernetes.NewForConfig(cfg) 54 Expect(err).NotTo(HaveOccurred()) 55 return nativeClient 56 } 57 58 func (f *framework) HubRuntimeClient() client.Client { 59 cfg := f.HubRESTConfig() 60 runtimeClient, err := client.New(cfg, client.Options{ 61 Scheme: scheme, 62 }) 63 Expect(err).NotTo(HaveOccurred()) 64 return runtimeClient 65 } 66 67 func (f *framework) HubGatewayClient() versioned.Interface { 68 cfg := f.HubRESTConfig() 69 gatewayClient, err := versioned.NewForConfig(cfg) 70 Expect(err).NotTo(HaveOccurred()) 71 return gatewayClient 72 } 73 74 func (f *framework) IsOCMInstalled() bool { 75 return f.ctx.IsOCMInstalled 76 } 77 78 func (f *framework) TestClusterName() string { 79 return f.ctx.TestCluster 80 } 81 82 func (f *framework) BeforeEach() { 83 84 } 85 86 func (f *framework) AfterEach() { 87 88 }