Skip to content

Commit

Permalink
🐛 fix scaffold to allow run the tests directly
Browse files Browse the repository at this point in the history
  • Loading branch information
camilamacedo86 committed May 7, 2023
1 parent 8217db9 commit c39ad11
Show file tree
Hide file tree
Showing 27 changed files with 265 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"net"
"path/filepath"
"runtime"
"testing"
"time"

Expand All @@ -30,7 +31,7 @@ import (

admissionv1 "k8s.io/api/admission/v1"
//+kubebuilder:scaffold:imports
"k8s.io/apimachinery/pkg/runtime"
apimachineryruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -63,6 +64,15 @@ var _ = BeforeSuite(func() {
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
ErrorIfCRDPathMissing: false,

// The BinaryAssetsDirectory is only required if you want to run the tests directly
// without call the makefile target test. If not informed it will look for the
// default path defined in controller-runtime which is /usr/local/kubebuilder/.
// Note that you must have the required binaries setup under the bin directory to perform
// the tests directly. When we run make test it will be setup and used automatically.
BinaryAssetsDirectory: filepath.Join("..", "..", "bin", "k8s",
fmt.Sprintf("1.27.1-%s-%s", runtime.GOOS, runtime.GOARCH)),

WebhookInstallOptions: envtest.WebhookInstallOptions{
Paths: []string{filepath.Join("..", "..", "config", "webhook")},
},
Expand All @@ -74,7 +84,7 @@ var _ = BeforeSuite(func() {
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

scheme := runtime.NewScheme()
scheme := apimachineryruntime.NewScheme()
err = AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ package controller

import (
"context"
"fmt"
"path/filepath"
"runtime"
"testing"

ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -81,6 +83,14 @@ var _ = BeforeSuite(func() {
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
ErrorIfCRDPathMissing: true,

// The BinaryAssetsDirectory is only required if you want to run the tests directly
// without call the makefile target test. If not informed it will look for the
// default path defined in controller-runtime which is /usr/local/kubebuilder/.
// Note that you must have the required binaries setup under the bin directory to perform
// the tests directly. When we run make test it will be setup and used automatically.
BinaryAssetsDirectory: filepath.Join("..", "..", "bin", "k8s",
fmt.Sprintf("1.27.1-%s-%s", runtime.GOOS, runtime.GOARCH)),
}

/*
Expand Down
7 changes: 1 addition & 6 deletions hack/docs/internal/cronjob-tutorial/generate_cronjob.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,6 @@ func updateSuiteTest(sp *Sample) {
err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
`
"path/filepath"
"testing"
`, `
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -513,11 +512,7 @@ var testEnv *envtest.Environment

err = pluginutil.InsertCode(
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
`
By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
ErrorIfCRDPathMissing: true,
`, runtime.GOOS, runtime.GOARCH)),
}
`, `
/*
Expand Down
2 changes: 1 addition & 1 deletion pkg/plugins/golang/v4/scaffolds/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (s *apiScaffolder) Scaffold() error {

if doController {
if err := scaffold.Execute(
&controllers.SuiteTest{Force: s.force},
&controllers.SuiteTest{Force: s.force, K8SVersion: EnvtestK8SVersion},
&controllers.Controller{ControllerRuntimeVersion: ControllerRuntimeVersion, Force: s.force},
); err != nil {
return fmt.Errorf("error scaffolding controller: %v", err)
Expand Down
2 changes: 2 additions & 0 deletions pkg/plugins/golang/v4/scaffolds/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ const (
ControllerRuntimeVersion = "v0.14.4"
// ControllerToolsVersion is the kubernetes-sigs/controller-tools version to be used in the project
ControllerToolsVersion = "v0.12.0"
// EnvtestK8SVersion is the k8s version used to do the scaffold
EnvtestK8SVersion = "1.27.1"

imageName = "controller:latest"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ type WebhookSuite struct { //nolint:maligned
// todo: currently is not possible to know if an API was or not scaffolded. We can fix it when #1826 be addressed
WireResource bool

// K8SVersion define the k8s version used to do the scaffold
// so that is possible retrieve the binaries
K8SVersion string

// BaseDirectoryRelativePath define the Path for the base directory when it is multigroup
BaseDirectoryRelativePath string
}
Expand Down Expand Up @@ -133,16 +137,20 @@ package {{ .Resource.Version }}
import (
"context"
"crypto/tls"
"fmt"
"net"
"path/filepath"
"testing"
"fmt"
"time"
"runtime"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
%s
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/apimachinery/pkg/runtime"
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"
Expand Down Expand Up @@ -174,6 +182,15 @@ var _ = BeforeSuite(func() {
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join({{ .BaseDirectoryRelativePath }}, "config", "crd", "bases")},
ErrorIfCRDPathMissing: {{ .WireResource }},
// The BinaryAssetsDirectory is only required if you want to run the tests directly
// without call the makefile target test. If not informed it will look for the
// default path defined in controller-runtime which is /usr/local/kubebuilder/.
// Note that you must have the required binaries setup under the bin directory to perform
// the tests directly. When we run make test it will be setup and used automatically.
BinaryAssetsDirectory: filepath.Join({{ .BaseDirectoryRelativePath }}, "bin", "k8s",
fmt.Sprintf("{{ .K8SVersion }}-%%s-%%s", runtime.GOOS, runtime.GOARCH)),
WebhookInstallOptions: envtest.WebhookInstallOptions{
Paths: []string{filepath.Join({{ .BaseDirectoryRelativePath }}, "config", "webhook")},
},
Expand All @@ -185,7 +202,7 @@ var _ = BeforeSuite(func() {
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())
scheme := runtime.NewScheme()
scheme := apimachineryruntime.NewScheme()
err = AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ type SuiteTest struct {
machinery.BoilerplateMixin
machinery.ResourceMixin

// K8SVersion define the k8s version used to do the scaffold
// so that is possible retrieve the binaries
K8SVersion string

// CRDDirectoryRelativePath define the Path for the CRD
CRDDirectoryRelativePath string

Expand Down Expand Up @@ -130,7 +134,9 @@ package controller
{{end}}
import (
"fmt"
"path/filepath"
"runtime"
"testing"
. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -165,6 +171,14 @@ var _ = BeforeSuite(func() {
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join({{ .CRDDirectoryRelativePath }}, "config", "crd", "bases")},
ErrorIfCRDPathMissing: {{ .Resource.HasAPI }},
// The BinaryAssetsDirectory is only required if you want to run the tests directly
// without call the makefile target test. If not informed it will look for the
// default path defined in controller-runtime which is /usr/local/kubebuilder/.
// Note that you must have the required binaries setup under the bin directory to perform
// the tests directly. When we run make test it will be setup and used automatically.
BinaryAssetsDirectory: filepath.Join({{ .CRDDirectoryRelativePath }}, "bin", "k8s",
fmt.Sprintf("{{ .K8SVersion }}-%%s-%%s", runtime.GOOS, runtime.GOARCH)),
}
var err error
Expand Down
2 changes: 1 addition & 1 deletion pkg/plugins/golang/v4/scaffolds/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ You need to implement the conversion.Hub and conversion.Convertible interfaces f
// TODO: Add test suite for conversion webhook after #1664 has been merged & conversion tests supported in envtest.
if doDefaulting || doValidation {
if err := scaffold.Execute(
&api.WebhookSuite{},
&api.WebhookSuite{K8SVersion: EnvtestK8SVersion},
); err != nil {
return err
}
Expand Down
14 changes: 12 additions & 2 deletions testdata/project-v4-config/api/v1/webhook_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"net"
"path/filepath"
"runtime"
"testing"
"time"

Expand All @@ -30,7 +31,7 @@ import (

admissionv1 "k8s.io/api/admission/v1"
//+kubebuilder:scaffold:imports
"k8s.io/apimachinery/pkg/runtime"
apimachineryruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -63,6 +64,15 @@ var _ = BeforeSuite(func() {
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
ErrorIfCRDPathMissing: false,

// The BinaryAssetsDirectory is only required if you want to run the tests directly
// without call the makefile target test. If not informed it will look for the
// default path defined in controller-runtime which is /usr/local/kubebuilder/.
// Note that you must have the required binaries setup under the bin directory to perform
// the tests directly. When we run make test it will be setup and used automatically.
BinaryAssetsDirectory: filepath.Join("..", "..", "bin", "k8s",
fmt.Sprintf("1.27.1-%s-%s", runtime.GOOS, runtime.GOARCH)),

WebhookInstallOptions: envtest.WebhookInstallOptions{
Paths: []string{filepath.Join("..", "..", "config", "webhook")},
},
Expand All @@ -74,7 +84,7 @@ var _ = BeforeSuite(func() {
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

scheme := runtime.NewScheme()
scheme := apimachineryruntime.NewScheme()
err = AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

Expand Down
10 changes: 10 additions & 0 deletions testdata/project-v4-config/internal/controller/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ limitations under the License.
package controller

import (
"fmt"
"path/filepath"
"runtime"
"testing"

. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -54,6 +56,14 @@ var _ = BeforeSuite(func() {
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
ErrorIfCRDPathMissing: true,

// The BinaryAssetsDirectory is only required if you want to run the tests directly
// without call the makefile target test. If not informed it will look for the
// default path defined in controller-runtime which is /usr/local/kubebuilder/.
// Note that you must have the required binaries setup under the bin directory to perform
// the tests directly. When we run make test it will be setup and used automatically.
BinaryAssetsDirectory: filepath.Join("..", "..", "bin", "k8s",
fmt.Sprintf("1.27.1-%s-%s", runtime.GOOS, runtime.GOARCH)),
}

var err error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ limitations under the License.
package controller

import (
"fmt"
"path/filepath"
"runtime"
"testing"

. "github.com/onsi/ginkgo/v2"
Expand Down Expand Up @@ -54,6 +56,14 @@ var _ = BeforeSuite(func() {
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
ErrorIfCRDPathMissing: true,

// The BinaryAssetsDirectory is only required if you want to run the tests directly
// without call the makefile target test. If not informed it will look for the
// default path defined in controller-runtime which is /usr/local/kubebuilder/.
// Note that you must have the required binaries setup under the bin directory to perform
// the tests directly. When we run make test it will be setup and used automatically.
BinaryAssetsDirectory: filepath.Join("..", "..", "bin", "k8s",
fmt.Sprintf("1.27.1-%s-%s", runtime.GOOS, runtime.GOARCH)),
}

var err error
Expand Down
14 changes: 12 additions & 2 deletions testdata/project-v4-multigroup/api/crew/v1/webhook_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"net"
"path/filepath"
"runtime"
"testing"
"time"

Expand All @@ -30,7 +31,7 @@ import (

admissionv1 "k8s.io/api/admission/v1"
//+kubebuilder:scaffold:imports
"k8s.io/apimachinery/pkg/runtime"
apimachineryruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -63,6 +64,15 @@ var _ = BeforeSuite(func() {
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crd", "bases")},
ErrorIfCRDPathMissing: false,

// The BinaryAssetsDirectory is only required if you want to run the tests directly
// without call the makefile target test. If not informed it will look for the
// default path defined in controller-runtime which is /usr/local/kubebuilder/.
// Note that you must have the required binaries setup under the bin directory to perform
// the tests directly. When we run make test it will be setup and used automatically.
BinaryAssetsDirectory: filepath.Join("..", "..", "..", "bin", "k8s",
fmt.Sprintf("1.27.1-%s-%s", runtime.GOOS, runtime.GOARCH)),

WebhookInstallOptions: envtest.WebhookInstallOptions{
Paths: []string{filepath.Join("..", "..", "..", "config", "webhook")},
},
Expand All @@ -74,7 +84,7 @@ var _ = BeforeSuite(func() {
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

scheme := runtime.NewScheme()
scheme := apimachineryruntime.NewScheme()
err = AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

Expand Down
14 changes: 12 additions & 2 deletions testdata/project-v4-multigroup/api/ship/v1/webhook_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"fmt"
"net"
"path/filepath"
"runtime"
"testing"
"time"

Expand All @@ -30,7 +31,7 @@ import (

admissionv1 "k8s.io/api/admission/v1"
//+kubebuilder:scaffold:imports
"k8s.io/apimachinery/pkg/runtime"
apimachineryruntime "k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -63,6 +64,15 @@ var _ = BeforeSuite(func() {
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crd", "bases")},
ErrorIfCRDPathMissing: false,

// The BinaryAssetsDirectory is only required if you want to run the tests directly
// without call the makefile target test. If not informed it will look for the
// default path defined in controller-runtime which is /usr/local/kubebuilder/.
// Note that you must have the required binaries setup under the bin directory to perform
// the tests directly. When we run make test it will be setup and used automatically.
BinaryAssetsDirectory: filepath.Join("..", "..", "..", "bin", "k8s",
fmt.Sprintf("1.27.1-%s-%s", runtime.GOOS, runtime.GOARCH)),

WebhookInstallOptions: envtest.WebhookInstallOptions{
Paths: []string{filepath.Join("..", "..", "..", "config", "webhook")},
},
Expand All @@ -74,7 +84,7 @@ var _ = BeforeSuite(func() {
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

scheme := runtime.NewScheme()
scheme := apimachineryruntime.NewScheme()
err = AddToScheme(scheme)
Expect(err).NotTo(HaveOccurred())

Expand Down
Loading

0 comments on commit c39ad11

Please sign in to comment.