How to use init method of ginkgo Package

Best Ginkgo code snippet using ginkgo.init

Run Ginkgo automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

e2e_test.go

Source: e2e_test.go Github

copy
1/*
2Copyright 2018 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7   http://www.apache.org/licenses/LICENSE-2.0
8Unless required by applicable law or agreed to in writing, software
9distributed under the License is distributed on an "AS IS" BASIS,
10WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11See the License for the specific language governing permissions and
12limitations under the License.
13*/
14
15package e2e
16
17import (
18	"flag"
19	"fmt"
20	"log"
21	"math/rand"
22	"os"
23	"path"
24	"path/filepath"
25	"testing"
26	"time"
27
28	"github.com/onsi/ginkgo"
29	"github.com/onsi/ginkgo/config"
30	"github.com/onsi/ginkgo/reporters"
31	"github.com/onsi/gomega"
32	"k8s.io/kubernetes/test/e2e/framework"
33	"k8s.io/kubernetes/test/e2e/storage/drivers"
34	"k8s.io/kubernetes/test/e2e/storage/testsuites"
35
36	// ensure that cloud provider is loaded
37	_ "k8s.io/kubernetes/test/e2e/framework/providers/aws"
38)
39
40const kubeconfigEnvVar = "KUBECONFIG"
41
42func init() {
43	rand.Seed(time.Now().UTC().UnixNano())
44	testing.Init()
45	// k8s.io/kubernetes/test/e2e/framework requires env KUBECONFIG to be set
46	// it does not fall back to defaults
47	if os.Getenv(kubeconfigEnvVar) == "" {
48		kubeconfig := filepath.Join(os.Getenv("HOME"), ".kube", "config")
49		os.Setenv(kubeconfigEnvVar, kubeconfig)
50	}
51	framework.RegisterCommonFlags(flag.CommandLine)
52	framework.RegisterClusterFlags(flag.CommandLine)
53	_ = flag.Set("storage.migratedPlugins", "kubernetes.io/aws-ebs")
54	_ = flag.Set("provider", "aws")
55	flag.Parse()
56	framework.AfterReadingAllFlags(&framework.TestContext)
57}
58
59func TestEBSCSI(t *testing.T) {
60	gomega.RegisterFailHandler(ginkgo.Fail)
61
62	// Run tests through the Ginkgo runner with output to console + JUnit for Jenkins
63	var r []ginkgo.Reporter
64	if framework.TestContext.ReportDir != "" {
65		if err := os.MkdirAll(framework.TestContext.ReportDir, 0755); err != nil {
66			log.Fatalf("Failed creating report directory: %v", err)
67		} else {
68			r = append(r, reporters.NewJUnitReporter(path.Join(framework.TestContext.ReportDir, fmt.Sprintf("junit_%v%02d.xml", framework.TestContext.ReportPrefix, config.GinkgoConfig.ParallelNode))))
69		}
70	}
71	log.Printf("Starting e2e run %q on Ginkgo node %d", framework.RunID, config.GinkgoConfig.ParallelNode)
72
73	ginkgo.RunSpecsWithDefaultAndCustomReporters(t, "EBS CSI Migration Suite", r)
74}
75
76// List of testSuites to be executed in below loop
77var csiTestSuites = []func() testsuites.TestSuite{
78	testsuites.InitVolumesTestSuite,
79	testsuites.InitVolumeIOTestSuite,
80	testsuites.InitVolumeModeTestSuite,
81	testsuites.InitSubPathTestSuite,
82	testsuites.InitProvisioningTestSuite,
83	testsuites.InitSnapshottableTestSuite,
84	testsuites.InitVolumeExpandTestSuite,
85	testsuites.InitMultiVolumeTestSuite,
86}
87
88var _ = ginkgo.Describe("[ebs-csi-migration] EBS CSI Migration", func() {
89	// Init the *in-tree* driver.
90	// The CSIMigration & CSIMigrationAWS feature gates must be enabled on the cluster.
91	// The storage.migratedPlugins flag must be set to "kubernetes.io/aws-ebs". Then the tests will
92	// validate that CSI, not in-tree, operations are happening.
93	driver := drivers.InitAwsDriver()
94	ginkgo.Context(testsuites.GetDriverNameWithFeatureTags(driver), func() {
95		testsuites.DefineTestSuite(driver, csiTestSuites)
96	})
97})
98
Full Screen

joinhubscenario_timeout_test.go

Source: joinhubscenario_timeout_test.go Github

copy
1// Copyright Contributors to the Open Cluster Management project
2package clusteradme2e
3
4import (
5	"github.com/onsi/ginkgo/v2"
6	"github.com/onsi/gomega"
7)
8
9var _ = ginkgo.Describe("test clusteradm with timeout", func() {
10	ginkgo.BeforeEach(func() {
11		e2e.ClearEnv()
12	})
13
14	ginkgo.AfterEach(func() {
15		ginkgo.By("reset e2e environment...")
16		e2e.ResetEnv()
17	})
18
19	ginkgo.Context("join hub scenario with timeout", func() {
20		var err error
21
22		ginkgo.It("should managedcluster join and accepted successfully", func() {
23			ginkgo.By("init hub with timeout")
24			err = e2e.Clusteradm().Init(
25				"--timeout", "400",
26				"--context", e2e.Cluster().Hub().Context(),
27			)
28			gomega.Expect(err).NotTo(gomega.HaveOccurred(), "clusteradm init error")
29
30			ginkgo.By("managedcluster1 join hub")
31			err = e2e.Clusteradm().Join(
32				"--context", e2e.Cluster().ManagedCluster1().Context(),
33				"--hub-token", e2e.CommandResult().Token(), "--hub-apiserver", e2e.CommandResult().Host(),
34				"--cluster-name", e2e.Cluster().ManagedCluster1().Name(),
35				"--wait",
36				"--force-internal-endpoint-lookup",
37			)
38			gomega.Expect(err).NotTo(gomega.HaveOccurred(), "managedcluster1 join error")
39
40			ginkgo.By("hub accept managedcluster1")
41			err = e2e.Clusteradm().Accept(
42				"--clusters", e2e.Cluster().ManagedCluster1().Name(),
43				"--wait", "30",
44				"--context", e2e.Cluster().Hub().Context(),
45			)
46			gomega.Expect(err).NotTo(gomega.HaveOccurred(), "clusteradm accept error")
47		})
48
49	})
50})
51
Full Screen

joinhubscenario_sa_test.go

Source: joinhubscenario_sa_test.go Github

copy
1// Copyright Contributors to the Open Cluster Management project
2package clusteradme2e
3
4import (
5	"github.com/onsi/ginkgo/v2"
6	"github.com/onsi/gomega"
7)
8
9var _ = ginkgo.Describe("test clusteradm with service account", func() {
10	ginkgo.BeforeEach(func() {
11		e2e.ClearEnv()
12	})
13
14	ginkgo.AfterEach(func() {
15		ginkgo.By("reset e2e environment...")
16		e2e.ResetEnv()
17	})
18
19	ginkgo.Context("join hub scenario with service account", func() {
20		var originalToken string
21		var err error
22
23		ginkgo.It("should managedclusters join and accepted successfully", func() {
24			ginkgo.By("init hub with service account")
25			err = e2e.Clusteradm().Init(
26				"--context", e2e.Cluster().Hub().Context(),
27			)
28			gomega.Expect(err).NotTo(gomega.HaveOccurred(), "clusteradm init error")
29
30			ginkgo.By("managedcluster1 join hub")
31			err = e2e.Clusteradm().Join(
32				"--context", e2e.Cluster().ManagedCluster1().Context(),
33				"--hub-token", e2e.CommandResult().Token(), "--hub-apiserver", e2e.CommandResult().Host(),
34				"--cluster-name", e2e.Cluster().ManagedCluster1().Name(),
35				"--wait",
36				"--force-internal-endpoint-lookup",
37			)
38			gomega.Expect(err).NotTo(gomega.HaveOccurred(), "managedcluster1 join error")
39
40			ginkgo.By("hub accept managedcluster1")
41			err = e2e.Clusteradm().Accept(
42				"--clusters", e2e.Cluster().ManagedCluster1().Name(),
43				"--wait", "30",
44				"--context", e2e.Cluster().Hub().Context(),
45			)
46			gomega.Expect(err).NotTo(gomega.HaveOccurred(), "clusteradm accept error")
47
48			ginkgo.By("get token from hub")
49			err = e2e.Clusteradm().Get("token")
50			gomega.Expect(err).NotTo(gomega.HaveOccurred(), "clusteradm get token error")
51
52			originalToken = e2e.CommandResult().RawCommand()
53
54			ginkgo.By("managedcluster2 join hub")
55			err = e2e.Clusteradm().Join(
56				"--context", e2e.Cluster().ManagedCluster2().Context(),
57				"--hub-token", e2e.CommandResult().Token(), "--hub-apiserver", e2e.CommandResult().Host(),
58				"--cluster-name", e2e.Cluster().ManagedCluster2().Name(),
59				"--wait",
60				"--force-internal-endpoint-lookup",
61			)
62			gomega.Expect(err).NotTo(gomega.HaveOccurred(), "managedcluster2 join error")
63
64			ginkgo.By("hub accept managedcluster2")
65			err = e2e.Clusteradm().Accept(
66				"--clusters", e2e.Cluster().ManagedCluster2().Name(),
67				"--wait", "30",
68				"--context", e2e.Cluster().Hub().Context(),
69			)
70			gomega.Expect(err).NotTo(gomega.HaveOccurred(), "clusteradm accept error")
71
72			ginkgo.By("delete token")
73			err = e2e.Clusteradm().Delete(
74				"token",
75			)
76			gomega.Expect(err).NotTo(gomega.HaveOccurred(), "clusteradm delete token error")
77
78			ginkgo.By("get token from hub")
79			err = e2e.Clusteradm().Get(
80				"token",
81			)
82			gomega.Expect(err).NotTo(gomega.HaveOccurred(), "clusteradm get token error")
83
84			gomega.Expect(e2e.CommandResult().RawCommand()).NotTo(gomega.Equal(originalToken), "new token identical as previous token after delete")
85		})
86
87	})
88
89})
90
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)