Skip to content

Commit 90c3b1b

Browse files
🐛 fix scaffold to allow run the tests directly
1 parent 90a3b40 commit 90c3b1b

File tree

25 files changed

+243
-25
lines changed

25 files changed

+243
-25
lines changed

docs/book/src/cronjob-tutorial/testdata/project/api/v1/webhook_suite_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"net"
2424
"path/filepath"
25+
"runtime"
2526
"testing"
2627
"time"
2728

@@ -30,7 +31,7 @@ import (
3031

3132
admissionv1 "k8s.io/api/admission/v1"
3233
//+kubebuilder:scaffold:imports
33-
"k8s.io/apimachinery/pkg/runtime"
34+
apimachineryruntime "k8s.io/apimachinery/pkg/runtime"
3435
"k8s.io/client-go/rest"
3536
ctrl "sigs.k8s.io/controller-runtime"
3637
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -63,6 +64,15 @@ var _ = BeforeSuite(func() {
6364
testEnv = &envtest.Environment{
6465
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
6566
ErrorIfCRDPathMissing: false,
67+
68+
// The BinaryAssetsDirectory is only required if you want to run the tests directly
69+
// without call the makefile target test. If not informed it will look for the
70+
// default path defined in controller-runtime which is /usr/local/kubebuilder/.
71+
// Note that you must have the required binaries setup under the bin directory to perform
72+
// the tests directly. When we run make test it will be setup and used automatically.
73+
BinaryAssetsDirectory: filepath.Join("..", "..", "bin", "k8s",
74+
fmt.Sprintf("1.27.1-%s-%s", runtime.GOOS, runtime.GOARCH)),
75+
6676
WebhookInstallOptions: envtest.WebhookInstallOptions{
6777
Paths: []string{filepath.Join("..", "..", "config", "webhook")},
6878
},
@@ -74,7 +84,7 @@ var _ = BeforeSuite(func() {
7484
Expect(err).NotTo(HaveOccurred())
7585
Expect(cfg).NotTo(BeNil())
7686

77-
scheme := runtime.NewScheme()
87+
scheme := apimachineryruntime.NewScheme()
7888
err = AddToScheme(scheme)
7989
Expect(err).NotTo(HaveOccurred())
8090

docs/book/src/cronjob-tutorial/testdata/project/internal/controller/suite_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ package controller
2727

2828
import (
2929
"context"
30+
"fmt"
3031
"path/filepath"
32+
"runtime"
3133
"testing"
3234

3335
ctrl "sigs.k8s.io/controller-runtime"
@@ -81,6 +83,14 @@ var _ = BeforeSuite(func() {
8183
testEnv = &envtest.Environment{
8284
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
8385
ErrorIfCRDPathMissing: true,
86+
87+
// The BinaryAssetsDirectory is only required if you want to run the tests directly
88+
// without call the makefile target test. If not informed it will look for the
89+
// default path defined in controller-runtime which is /usr/local/kubebuilder/.
90+
// Note that you must have the required binaries setup under the bin directory to perform
91+
// the tests directly. When we run make test it will be setup and used automatically.
92+
BinaryAssetsDirectory: filepath.Join("..", "..", "bin", "k8s",
93+
fmt.Sprintf("1.27.1-%s-%s", runtime.GOOS, runtime.GOARCH)),
8494
}
8595

8696
/*

hack/docs/internal/cronjob-tutorial/generate_cronjob.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,6 @@ func updateSuiteTest(sp *Sample) {
477477
err = pluginutil.InsertCode(
478478
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
479479
`
480-
"path/filepath"
481480
"testing"
482481
`, `
483482
ctrl "sigs.k8s.io/controller-runtime"
@@ -502,11 +501,7 @@ var testEnv *envtest.Environment
502501

503502
err = pluginutil.InsertCode(
504503
filepath.Join(sp.ctx.Dir, "internal/controller/suite_test.go"),
505-
`
506-
By("bootstrapping test environment")
507-
testEnv = &envtest.Environment{
508-
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
509-
ErrorIfCRDPathMissing: true,
504+
`, runtime.GOOS, runtime.GOARCH)),
510505
}
511506
`, `
512507
/*

pkg/plugins/golang/v4/scaffolds/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func (s *apiScaffolder) Scaffold() error {
101101

102102
if doController {
103103
if err := scaffold.Execute(
104-
&controllers.SuiteTest{Force: s.force},
104+
&controllers.SuiteTest{Force: s.force, K8SVersion: EnvtestK8SVersion},
105105
&controllers.Controller{ControllerRuntimeVersion: ControllerRuntimeVersion, Force: s.force},
106106
); err != nil {
107107
return fmt.Errorf("error scaffolding controller: %v", err)

pkg/plugins/golang/v4/scaffolds/init.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ const (
3737
ControllerRuntimeVersion = "v0.15.0"
3838
// ControllerToolsVersion is the kubernetes-sigs/controller-tools version to be used in the project
3939
ControllerToolsVersion = "v0.12.0"
40+
// EnvtestK8SVersion is the k8s version used to do the scaffold
41+
EnvtestK8SVersion = "1.27.1"
4042

4143
imageName = "controller:latest"
4244
)

pkg/plugins/golang/v4/scaffolds/internal/templates/api/webhook_suitetest.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ type WebhookSuite struct { //nolint:maligned
3636
// todo: currently is not possible to know if an API was or not scaffolded. We can fix it when #1826 be addressed
3737
WireResource bool
3838

39+
// K8SVersion define the k8s version used to do the scaffold
40+
// so that is possible retrieve the binaries
41+
K8SVersion string
42+
3943
// BaseDirectoryRelativePath define the Path for the base directory when it is multigroup
4044
BaseDirectoryRelativePath string
4145
}
@@ -133,16 +137,20 @@ package {{ .Resource.Version }}
133137
134138
import (
135139
"context"
140+
"crypto/tls"
141+
"fmt"
142+
"net"
136143
"path/filepath"
137144
"testing"
138-
"fmt"
145+
"time"
146+
"runtime"
139147
140148
. "github.com/onsi/ginkgo/v2"
141149
. "github.com/onsi/gomega"
142150
%s
143151
"k8s.io/client-go/kubernetes/scheme"
144152
"k8s.io/client-go/rest"
145-
"k8s.io/apimachinery/pkg/runtime"
153+
apimachineryruntime "k8s.io/apimachinery/pkg/runtime"
146154
ctrl "sigs.k8s.io/controller-runtime"
147155
"sigs.k8s.io/controller-runtime/pkg/client"
148156
"sigs.k8s.io/controller-runtime/pkg/envtest"
@@ -174,6 +182,15 @@ var _ = BeforeSuite(func() {
174182
testEnv = &envtest.Environment{
175183
CRDDirectoryPaths: []string{filepath.Join({{ .BaseDirectoryRelativePath }}, "config", "crd", "bases")},
176184
ErrorIfCRDPathMissing: {{ .WireResource }},
185+
186+
// The BinaryAssetsDirectory is only required if you want to run the tests directly
187+
// without call the makefile target test. If not informed it will look for the
188+
// default path defined in controller-runtime which is /usr/local/kubebuilder/.
189+
// Note that you must have the required binaries setup under the bin directory to perform
190+
// the tests directly. When we run make test it will be setup and used automatically.
191+
BinaryAssetsDirectory: filepath.Join({{ .BaseDirectoryRelativePath }}, "bin", "k8s",
192+
fmt.Sprintf("{{ .K8SVersion }}-%%s-%%s", runtime.GOOS, runtime.GOARCH)),
193+
177194
WebhookInstallOptions: envtest.WebhookInstallOptions{
178195
Paths: []string{filepath.Join({{ .BaseDirectoryRelativePath }}, "config", "webhook")},
179196
},
@@ -185,7 +202,7 @@ var _ = BeforeSuite(func() {
185202
Expect(err).NotTo(HaveOccurred())
186203
Expect(cfg).NotTo(BeNil())
187204
188-
scheme := runtime.NewScheme()
205+
scheme := apimachineryruntime.NewScheme()
189206
err = AddToScheme(scheme)
190207
Expect(err).NotTo(HaveOccurred())
191208

pkg/plugins/golang/v4/scaffolds/internal/templates/controllers/controller_suitetest.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ type SuiteTest struct {
3434
machinery.BoilerplateMixin
3535
machinery.ResourceMixin
3636

37+
// K8SVersion define the k8s version used to do the scaffold
38+
// so that is possible retrieve the binaries
39+
K8SVersion string
40+
3741
// CRDDirectoryRelativePath define the Path for the CRD
3842
CRDDirectoryRelativePath string
3943

@@ -130,7 +134,9 @@ package controller
130134
{{end}}
131135
132136
import (
137+
"fmt"
133138
"path/filepath"
139+
"runtime"
134140
"testing"
135141
136142
. "github.com/onsi/ginkgo/v2"
@@ -165,6 +171,14 @@ var _ = BeforeSuite(func() {
165171
testEnv = &envtest.Environment{
166172
CRDDirectoryPaths: []string{filepath.Join({{ .CRDDirectoryRelativePath }}, "config", "crd", "bases")},
167173
ErrorIfCRDPathMissing: {{ .Resource.HasAPI }},
174+
175+
// The BinaryAssetsDirectory is only required if you want to run the tests directly
176+
// without call the makefile target test. If not informed it will look for the
177+
// default path defined in controller-runtime which is /usr/local/kubebuilder/.
178+
// Note that you must have the required binaries setup under the bin directory to perform
179+
// the tests directly. When we run make test it will be setup and used automatically.
180+
BinaryAssetsDirectory: filepath.Join({{ .CRDDirectoryRelativePath }}, "bin", "k8s",
181+
fmt.Sprintf("{{ .K8SVersion }}-%%s-%%s", runtime.GOOS, runtime.GOARCH)),
168182
}
169183
170184
var err error

pkg/plugins/golang/v4/scaffolds/webhook.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ You need to implement the conversion.Hub and conversion.Convertible interfaces f
9898
// TODO: Add test suite for conversion webhook after #1664 has been merged & conversion tests supported in envtest.
9999
if doDefaulting || doValidation {
100100
if err := scaffold.Execute(
101-
&api.WebhookSuite{},
101+
&api.WebhookSuite{K8SVersion: EnvtestK8SVersion},
102102
); err != nil {
103103
return err
104104
}

testdata/project-v4-declarative-v1/internal/controller/suite_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ limitations under the License.
1717
package controller
1818

1919
import (
20+
"fmt"
2021
"path/filepath"
22+
"runtime"
2123
"testing"
2224

2325
. "github.com/onsi/ginkgo/v2"
@@ -54,6 +56,14 @@ var _ = BeforeSuite(func() {
5456
testEnv = &envtest.Environment{
5557
CRDDirectoryPaths: []string{filepath.Join("..", "..", "config", "crd", "bases")},
5658
ErrorIfCRDPathMissing: true,
59+
60+
// The BinaryAssetsDirectory is only required if you want to run the tests directly
61+
// without call the makefile target test. If not informed it will look for the
62+
// default path defined in controller-runtime which is /usr/local/kubebuilder/.
63+
// Note that you must have the required binaries setup under the bin directory to perform
64+
// the tests directly. When we run make test it will be setup and used automatically.
65+
BinaryAssetsDirectory: filepath.Join("..", "..", "bin", "k8s",
66+
fmt.Sprintf("1.27.1-%s-%s", runtime.GOOS, runtime.GOARCH)),
5767
}
5868

5969
var err error

testdata/project-v4-multigroup/api/crew/v1/webhook_suite_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"net"
2424
"path/filepath"
25+
"runtime"
2526
"testing"
2627
"time"
2728

@@ -30,7 +31,7 @@ import (
3031

3132
admissionv1 "k8s.io/api/admission/v1"
3233
//+kubebuilder:scaffold:imports
33-
"k8s.io/apimachinery/pkg/runtime"
34+
apimachineryruntime "k8s.io/apimachinery/pkg/runtime"
3435
"k8s.io/client-go/rest"
3536
ctrl "sigs.k8s.io/controller-runtime"
3637
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -63,6 +64,15 @@ var _ = BeforeSuite(func() {
6364
testEnv = &envtest.Environment{
6465
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "config", "crd", "bases")},
6566
ErrorIfCRDPathMissing: false,
67+
68+
// The BinaryAssetsDirectory is only required if you want to run the tests directly
69+
// without call the makefile target test. If not informed it will look for the
70+
// default path defined in controller-runtime which is /usr/local/kubebuilder/.
71+
// Note that you must have the required binaries setup under the bin directory to perform
72+
// the tests directly. When we run make test it will be setup and used automatically.
73+
BinaryAssetsDirectory: filepath.Join("..", "..", "..", "bin", "k8s",
74+
fmt.Sprintf("1.27.1-%s-%s", runtime.GOOS, runtime.GOARCH)),
75+
6676
WebhookInstallOptions: envtest.WebhookInstallOptions{
6777
Paths: []string{filepath.Join("..", "..", "..", "config", "webhook")},
6878
},
@@ -74,7 +84,7 @@ var _ = BeforeSuite(func() {
7484
Expect(err).NotTo(HaveOccurred())
7585
Expect(cfg).NotTo(BeNil())
7686

77-
scheme := runtime.NewScheme()
87+
scheme := apimachineryruntime.NewScheme()
7888
err = AddToScheme(scheme)
7989
Expect(err).NotTo(HaveOccurred())
8090

0 commit comments

Comments
 (0)