Skip to content

Commit

Permalink
✨ (go/v4): Standardize the webhook and controller test suites (#4447)
Browse files Browse the repository at this point in the history
fix: standardize scaffold test suites

Signed-off-by: Mateus Oliveira <[email protected]>
  • Loading branch information
mateusoliveira43 authored Dec 27, 2024
1 parent 88f606f commit 6fb374f
Show file tree
Hide file tree
Showing 30 changed files with 168 additions and 162 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ Now, let's go through the code generated.
*/

var (
ctx context.Context
cancel context.CancelFunc
testEnv *envtest.Environment
cfg *rest.Config
k8sClient client.Client // You'll be using this client in your tests.
testEnv *envtest.Environment
)
var ctx context.Context
var cancel context.CancelFunc

func TestControllers(t *testing.T) {
RegisterFailHandler(Fail)
Expand Down Expand Up @@ -156,7 +156,6 @@ var _ = BeforeSuite(func() {
err = k8sManager.Start(ctx)
Expect(err).ToNot(HaveOccurred(), "failed to run manager")
}()

})

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ import (
. "github.com/onsi/gomega"

admissionv1 "k8s.io/api/admission/v1"

batchv1 "tutorial.kubebuilder.io/project/api/v1"

// +kubebuilder:scaffold:imports
apimachineryruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -43,16 +39,19 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log/zap"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"

batchv1 "tutorial.kubebuilder.io/project/api/v1"
// +kubebuilder:scaffold:imports
)

// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.

var (
cancel context.CancelFunc
cfg *rest.Config
ctx context.Context
cancel context.CancelFunc
k8sClient client.Client
cfg *rest.Config
testEnv *envtest.Environment
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ import (
// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.

var cfg *rest.Config
var k8sClient client.Client
var testEnv *envtest.Environment
var ctx context.Context
var cancel context.CancelFunc
var (
ctx context.Context
cancel context.CancelFunc
testEnv *envtest.Environment
cfg *rest.Config
k8sClient client.Client
)

func TestControllers(t *testing.T) {
RegisterFailHandler(Fail)
Expand Down Expand Up @@ -81,7 +83,6 @@ var _ = BeforeSuite(func() {
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())

})

var _ = AfterSuite(func() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ Now, let's go through the code generated.
*/

var (
ctx context.Context
cancel context.CancelFunc
testEnv *envtest.Environment
cfg *rest.Config
k8sClient client.Client // You'll be using this client in your tests.
testEnv *envtest.Environment
)
var ctx context.Context
var cancel context.CancelFunc

func TestControllers(t *testing.T) {
RegisterFailHandler(Fail)
Expand Down Expand Up @@ -156,7 +156,6 @@ var _ = BeforeSuite(func() {
err = k8sManager.Start(ctx)
Expect(err).ToNot(HaveOccurred(), "failed to run manager")
}()

})

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ import (
. "github.com/onsi/gomega"

admissionv1 "k8s.io/api/admission/v1"

batchv1 "tutorial.kubebuilder.io/project/api/v1"

// +kubebuilder:scaffold:imports
apimachineryruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -43,16 +39,19 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log/zap"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"

batchv1 "tutorial.kubebuilder.io/project/api/v1"
// +kubebuilder:scaffold:imports
)

// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.

var (
cancel context.CancelFunc
cfg *rest.Config
ctx context.Context
cancel context.CancelFunc
k8sClient client.Client
cfg *rest.Config
testEnv *envtest.Environment
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ import (
. "github.com/onsi/gomega"

admissionv1 "k8s.io/api/admission/v1"

batchv2 "tutorial.kubebuilder.io/project/api/v2"

// +kubebuilder:scaffold:imports
apimachineryruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -43,16 +39,19 @@ import (
"sigs.k8s.io/controller-runtime/pkg/log/zap"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"

batchv2 "tutorial.kubebuilder.io/project/api/v2"
// +kubebuilder:scaffold:imports
)

// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.

var (
cancel context.CancelFunc
cfg *rest.Config
ctx context.Context
cancel context.CancelFunc
k8sClient client.Client
cfg *rest.Config
testEnv *envtest.Environment
)

Expand Down
10 changes: 7 additions & 3 deletions hack/docs/internal/cronjob-tutorial/generate_cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,13 @@ func (sp *Sample) updateSuiteTest() {
err = pluginutil.ReplaceInFile(
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
`
var cfg *rest.Config
var k8sClient client.Client
var testEnv *envtest.Environment
var (
ctx context.Context
cancel context.CancelFunc
testEnv *envtest.Environment
cfg *rest.Config
k8sClient client.Client
)
`, SuiteTestEnv)
hackutils.CheckError("updating suite_test.go to add more variables", err)

Expand Down
4 changes: 3 additions & 1 deletion hack/docs/internal/cronjob-tutorial/writing_tests_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ Now, let's go through the code generated.
*/
var (
ctx context.Context
cancel context.CancelFunc
testEnv *envtest.Environment
cfg *rest.Config
k8sClient client.Client // You'll be using this client in your tests.
testEnv *envtest.Environment
)
`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,13 @@ import (
// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
var cfg *rest.Config
var k8sClient client.Client
var testEnv *envtest.Environment
var ctx context.Context
var cancel context.CancelFunc
var (
ctx context.Context
cancel context.CancelFunc
testEnv *envtest.Environment
cfg *rest.Config
k8sClient client.Client
)
func TestControllers(t *testing.T) {
RegisterFailHandler(Fail)
Expand Down Expand Up @@ -191,7 +193,6 @@ var _ = BeforeSuite(func() {
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())
})
var _ = AfterSuite(func() {
Expand All @@ -207,7 +208,7 @@ var _ = AfterSuite(func() {
// Makefile targets, the 'BinaryAssetsDirectory' must be explicitly configured.
//
// This function streamlines the process by finding the required binaries, similar to
// setting the 'KUBEBUILDER_ASSETS' environment variable. To ensure the binaries are
// setting the 'KUBEBUILDER_ASSETS' environment variable. To ensure the binaries are
// properly set up, run 'make setup-envtest' beforehand.
func getFirstFoundEnvTestBinaryDir() string {
basePath := filepath.Join({{ .CRDDirectoryRelativePath }}, "bin", "k8s")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,26 +195,27 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
%s
apimachineryruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
apimachineryruntime "k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
%s
)
// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
var (
cancel context.CancelFunc
cfg *rest.Config
ctx context.Context
cancel context.CancelFunc
k8sClient client.Client
cfg *rest.Config
testEnv *envtest.Environment
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ import (
// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.

var cfg *rest.Config
var k8sClient client.Client
var testEnv *envtest.Environment
var ctx context.Context
var cancel context.CancelFunc
var (
ctx context.Context
cancel context.CancelFunc
testEnv *envtest.Environment
cfg *rest.Config
k8sClient client.Client
)

func TestControllers(t *testing.T) {
RegisterFailHandler(Fail)
Expand Down Expand Up @@ -80,7 +82,6 @@ var _ = BeforeSuite(func() {
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())

})

var _ = AfterSuite(func() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ import (
// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.

var cfg *rest.Config
var k8sClient client.Client
var testEnv *envtest.Environment
var ctx context.Context
var cancel context.CancelFunc
var (
ctx context.Context
cancel context.CancelFunc
testEnv *envtest.Environment
cfg *rest.Config
k8sClient client.Client
)

func TestControllers(t *testing.T) {
RegisterFailHandler(Fail)
Expand Down Expand Up @@ -80,7 +82,6 @@ var _ = BeforeSuite(func() {
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())

})

var _ = AfterSuite(func() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ import (
// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.

var cfg *rest.Config
var k8sClient client.Client
var testEnv *envtest.Environment
var ctx context.Context
var cancel context.CancelFunc
var (
ctx context.Context
cancel context.CancelFunc
testEnv *envtest.Environment
cfg *rest.Config
k8sClient client.Client
)

func TestControllers(t *testing.T) {
RegisterFailHandler(Fail)
Expand Down Expand Up @@ -81,7 +83,6 @@ var _ = BeforeSuite(func() {
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())

})

var _ = AfterSuite(func() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ import (
// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.

var cfg *rest.Config
var k8sClient client.Client
var testEnv *envtest.Environment
var ctx context.Context
var cancel context.CancelFunc
var (
ctx context.Context
cancel context.CancelFunc
testEnv *envtest.Environment
cfg *rest.Config
k8sClient client.Client
)

func TestControllers(t *testing.T) {
RegisterFailHandler(Fail)
Expand Down Expand Up @@ -85,7 +87,6 @@ var _ = BeforeSuite(func() {
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())

})

var _ = AfterSuite(func() {
Expand Down
Loading

0 comments on commit 6fb374f

Please sign in to comment.