Skip to content

Commit c36f1b6

Browse files
change makefile test target for no longer be required manual steps to run the commands (#3983)
**Description** Customize the makefile target tests to download the binaries. Closes: #3692
1 parent 63a080f commit c36f1b6

File tree

8 files changed

+104
-32
lines changed

8 files changed

+104
-32
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,9 @@ website/resources/
111111
website/node_modules/
112112
website/tech-doc-hugo
113113

114+
# samples bin
115+
testdata/go/memcached-operator/bin/*
116+
117+
114118
*\.DS_Store
119+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# entries is a list of entries to include in
2+
# release notes and/or the migration guide
3+
entries:
4+
- description: >
5+
In Go projects, resolved an issue that caused failing tests by changing the Makefile's `test` target to
6+
automatically download and configure the necessary `envtest` binaries.
7+
8+
# kind is one of:
9+
# - addition
10+
# - change
11+
# - deprecation
12+
# - removal
13+
# - bugfix
14+
kind: "bugfix"
15+
16+
# Is this a breaking change?
17+
breaking: false

internal/plugins/envtest/init.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2020 The Operator-SDK Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// TODO: This implementation is already done for v3+. Also, it might be
16+
// addressed on v2 as well. More info: https://github.com/kubernetes-sigs/kubebuilder/pull/1711
17+
package envtest
18+
19+
import (
20+
"fmt"
21+
"io/ioutil"
22+
"os"
23+
"strings"
24+
25+
"sigs.k8s.io/kubebuilder/pkg/model/config"
26+
)
27+
28+
// controllerRuntimeVersion version to be used to download the envtest setup script
29+
const controllerRuntimeVersion = "v0.6.3"
30+
31+
// RunInit modifies the project scaffolded by kubebuilder's Init plugin.
32+
func RunInit(cfg *config.Config) error {
33+
// Only run these if project version is v3.
34+
if !cfg.IsV3() {
35+
return nil
36+
}
37+
38+
// Update the scaffolded Makefile with operator-sdk recipes.
39+
if err := initUpdateMakefile("Makefile"); err != nil {
40+
return fmt.Errorf("error updating Makefile: %v", err)
41+
}
42+
return nil
43+
}
44+
45+
// initUpdateMakefile updates a vanilla kubebuilder Makefile with operator-sdk recipes.
46+
func initUpdateMakefile(filePath string) error {
47+
makefileBytes, err := ioutil.ReadFile(filePath)
48+
if err != nil {
49+
return err
50+
}
51+
52+
makefileBytes = []byte(strings.Replace(string(makefileBytes),
53+
"# Run tests\ntest: generate fmt vet manifests\n\tgo test ./... -coverprofile cover.out",
54+
fmt.Sprintf(makefileTestTarget, controllerRuntimeVersion), 1))
55+
56+
var mode os.FileMode = 0644
57+
if info, err := os.Stat(filePath); err != nil {
58+
mode = info.Mode()
59+
}
60+
return ioutil.WriteFile(filePath, makefileBytes, mode)
61+
}
62+
63+
const makefileTestTarget = `# Run tests
64+
ENVTEST_ASSETS_DIR = $(shell pwd)/testbin
65+
test: generate fmt vet manifests
66+
mkdir -p $(ENVTEST_ASSETS_DIR)
67+
test -f $(ENVTEST_ASSETS_DIR)/setup-envtest.sh || curl -sSLo $(ENVTEST_ASSETS_DIR)/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/%s/hack/setup-envtest.sh
68+
source $(ENVTEST_ASSETS_DIR)/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out`

internal/plugins/golang/v2/init.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"sigs.k8s.io/kubebuilder/pkg/model/config"
2222
"sigs.k8s.io/kubebuilder/pkg/plugin"
2323

24+
"github.com/operator-framework/operator-sdk/internal/plugins/envtest"
2425
"github.com/operator-framework/operator-sdk/internal/plugins/manifests"
2526
"github.com/operator-framework/operator-sdk/internal/plugins/scorecard"
2627
)
@@ -64,6 +65,9 @@ func (p *initPlugin) Run() error {
6465

6566
// SDK phase 2 plugins.
6667
func (p *initPlugin) runPhase2() error {
68+
if err := envtest.RunInit(p.config); err != nil {
69+
return err
70+
}
6771
if err := manifests.RunInit(p.config); err != nil {
6872
return err
6973
}

internal/plugins/manifests/init.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package manifests
1818
import (
1919
"fmt"
2020
"io/ioutil"
21+
"os"
2122

2223
"sigs.k8s.io/kubebuilder/pkg/model/config"
2324

@@ -61,7 +62,11 @@ func initUpdateMakefile(cfg *config.Config, filePath string) error {
6162

6263
makefileBytes = append(makefileBytes, []byte(makefileBundleBuildFragment)...)
6364

64-
return ioutil.WriteFile(filePath, makefileBytes, 0644)
65+
var mode os.FileMode = 0644
66+
if info, err := os.Stat(filePath); err != nil {
67+
mode = info.Mode()
68+
}
69+
return ioutil.WriteFile(filePath, makefileBytes, mode)
6570
}
6671

6772
// Makefile fragments to add to the base Makefile.

testdata/go/memcached-operator/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ endif
2626
all: manager
2727

2828
# Run tests
29+
ENVTEST_ASSETS_DIR = $(shell pwd)/testbin
2930
test: generate fmt vet manifests
30-
go test ./... -coverprofile cover.out
31+
mkdir -p $(ENVTEST_ASSETS_DIR)
32+
test -f $(ENVTEST_ASSETS_DIR)/setup-envtest.sh || curl -sSLo $(ENVTEST_ASSETS_DIR)/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.6.3/hack/setup-envtest.sh
33+
source $(ENVTEST_ASSETS_DIR)/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out
3134

3235
# Build manager binary
3336
manager: generate fmt vet

website/content/en/docs/building-operators/golang/migration.md

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -214,20 +214,6 @@ func main() {
214214
215215
- Ensure that you copy all customizations made in `cmd/manager/main.go` to `main.go`. You’ll also need to ensure that all needed schemes have been registered, if you have been using third-party API's (i.e Route Api from OpenShift).
216216
217-
### Configuring your test environment
218-
219-
[Setup the `envtest` binaries and environment][envtest-setup] for your project.
220-
Update your `test` Makefile target to the following:
221-
222-
```sh
223-
# Run tests
224-
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
225-
test: generate fmt vet manifests
226-
mkdir -p ${ENVTEST_ASSETS_DIR}
227-
test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/master/hack/setup-envtest.sh
228-
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out
229-
```
230-
231217
## Migrate your tests
232218
233219
For the new layout, you will see that `controllers/suite_test.go` is created when a controller is scaffolded by the tool. This file contains boilerplate for executing integration tests using [envtest][envtest] with [ginkgo](https://onsi.github.io/ginkgo/) and [gomega][gomega].
@@ -355,7 +341,6 @@ E.g `kubectl logs deployment.apps/memcached-operator-controller-manager -n memca
355341
[metrics]: https://book.kubebuilder.io/reference/metrics.html?highlight=metr#metrics
356342
[memcached_controller]: https://github.com/operator-framework/operator-sdk/blob/master/testdata/go/memcached-operator/controllers/memcached_controller.go
357343
[rbac_markers]: https://book.kubebuilder.io/reference/markers/rbac.html
358-
[envtest-setup]: /docs/building-operators/golang/references/envtest-setup
359344
[kube-auth-proxy]: https://github.com/brancz/kube-rbac-proxy
360345
[markers]: https://book.kubebuilder.io/reference/markers.html?highlight=markers#marker-syntax
361346
[operator-scope]: /docs/building-operators/golang/operator-scope

website/content/en/docs/building-operators/golang/quickstart.md

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,6 @@ Create a simple Memcached API:
3434
operator-sdk create api --group cache --version v1 --kind Memcached --resource=true --controller=true
3535
```
3636

37-
### Configuring your test environment
38-
39-
[Setup the `envtest` binaries and environment][envtest-setup] for your project.
40-
Update your `test` Makefile target to the following:
41-
42-
```sh
43-
# Run tests
44-
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
45-
test: generate fmt vet manifests
46-
mkdir -p ${ENVTEST_ASSETS_DIR}
47-
test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/master/hack/setup-envtest.sh
48-
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out
49-
```
50-
5137
### Build and push the operator image
5238

5339
Use the built-in Makefile targets to build and push your operator. Make
@@ -102,6 +88,5 @@ Read the [tutorial][tutorial] for an in-depth walkthough of building a Go operat
10288
[docker_tool]:https://docs.docker.com/install/
10389
[kubectl_tool]:https://kubernetes.io/docs/tasks/tools/install-kubectl/
10490
[operator_install]: /docs/installation/install-operator-sdk
105-
[envtest-setup]: /docs/building-operators/golang/references/envtest-setup
10691
[tutorial]: /docs/building-operators/golang/tutorial/
10792

0 commit comments

Comments
 (0)