Skip to content

Commit 9dbdbd2

Browse files
authored
Merge pull request #2382 from estroz/chore/clean-up-test-setup
🌱 chore(test): clean up e2e setup scripts, unit test utils
2 parents c02d98f + 040da5d commit 9dbdbd2

File tree

11 files changed

+253
-86
lines changed

11 files changed

+253
-86
lines changed

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- name: Run linter
1919
uses: golangci/golangci-lint-action@v2
2020
with:
21-
version: v1.37 # Always uses the latest patch version.
21+
version: v1.41 # Always uses the latest patch version.
2222
only-new-issues: true # Show only new issues if it's a pull request
2323
- name: Report failure
2424
uses: nashmaniac/[email protected]

CONTRIBUTING.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@ $ git clone [email protected]:<user>/kubebuilder.git $GOPATH/src/sigs.k8s.io/kubebu
3535
```
3636

3737
1. Ensure you activate module support before continue (`$ export GO111MODULE=on`)
38-
1. Run the command `make install` to create a bin with the source code
38+
1. Run the command `make install` to create a bin with the source code
3939

4040
**NOTE** In order to check the local environment run `make test-unit`.
4141

42-
## What to do before submitting a pull request
42+
## What to do before submitting a pull request
4343

44-
1. Run the script `make generate` to update/generate the mock data used in the e2e test in `$GOPATH/src/sigs.k8s.io/kubebuilder/testdata/`
44+
1. Run the script `make generate` to update/generate the mock data used in the e2e test in `$GOPATH/src/sigs.k8s.io/kubebuilder/testdata/`
45+
1. Run `make test-unit test-e2e-local`
46+
- e2e tests use [`kind`][kind] and [`setup-envtest`][setup-envtest]. If you want to bring your own binaries, place them in `$(go env GOPATH)/bin`.
4547

4648
**IMPORTANT:** The `make generate` is very helpful. By using it, you can check if good part of the commands still working successfully after the changes. Also, note that its usage is a pre-requirement to submit a PR.
4749

@@ -87,9 +89,9 @@ separately.
8789

8890
## Where the CI Tests are configured
8991

90-
1. See the [action files](.github/workflows) to check its tests, and the scripts used on it.
91-
1. Note that the prow tests used in the CI are configured in [kubernetes-sigs/kubebuilder/kubebuilder-presubmits.yaml](https://github.com/kubernetes/test-infra/blob/master/config/jobs/kubernetes-sigs/kubebuilder/kubebuilder-presubmits.yaml).
92-
1. Check that all scripts used by the CI are defined in the project.
92+
1. See the [action files](.github/workflows) to check its tests, and the scripts used on it.
93+
1. Note that the prow tests used in the CI are configured in [kubernetes-sigs/kubebuilder/kubebuilder-presubmits.yaml](https://github.com/kubernetes/test-infra/blob/master/config/jobs/kubernetes-sigs/kubebuilder/kubebuilder-presubmits.yaml).
94+
1. Check that all scripts used by the CI are defined in the project.
9395

9496
## How to contribute to docs
9597

@@ -102,7 +104,7 @@ The docs are published off of three branches:
102104
legacy docs
103105
- `master`:
104106
[master.book.kubebuilder.io](https://master.book.kubebuilder.io) --
105-
"nightly" docs
107+
"nightly" docs
106108

107109
See [VERSIONING.md](VERSIONING.md#book-releases) for more information.
108110

@@ -129,3 +131,5 @@ KubeBuilder and the related repositories. See
129131
Participation in the Kubernetes community is governed by the [Kubernetes Code of Conduct](code-of-conduct.md).
130132

131133
[golangci]:https://github.com/golangci/golangci-lint
134+
[kind]:https://kind.sigs.k8s.io/#installation-and-usage
135+
[setup-envtest]:https://book.kubebuilder.io/reference/envtest

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,14 @@ go-apidiff:
101101
test: test-unit test-integration test-testdata test-book ## Run the unit and integration tests (used in the CI)
102102

103103
.PHONY: test-unit
104+
TEST_PKGS := ./pkg/... ./test/e2e/utils/...
104105
test-unit: ## Run the unit tests
105-
go test -race -v ./pkg/...
106+
go test -race $(TEST_PKGS)
106107

107108
.PHONY: test-coverage
108109
test-coverage: ## Run unit tests creating the output to report coverage
109110
- rm -rf *.out # Remove all coverage files if exists
110-
go test -race -failfast -tags=integration -coverprofile=coverage-all.out -coverpkg="./pkg/cli/...,./pkg/config/...,./pkg/internal/...,./pkg/machinery/...,./pkg/model/...,./pkg/plugin/...,./pkg/plugins/golang" ./pkg/...
111+
go test -race -failfast -tags=integration -coverprofile=coverage-all.out -coverpkg="./pkg/cli/...,./pkg/config/...,./pkg/internal/...,./pkg/machinery/...,./pkg/model/...,./pkg/plugin/...,./pkg/plugins/golang" $(TEST_PKGS)
111112

112113
.PHONY: test-integration
113114
test-integration: ## Run the integration tests

test/common.sh

Lines changed: 27 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ if [ -n "$TRACE" ]; then
2727
set -x
2828
fi
2929

30-
k8s_version=1.19.2
30+
tools_k8s_version=1.19.2
3131
kind_version=0.11.1
3232
goarch=amd64
3333

@@ -59,8 +59,14 @@ function header_text {
5959
echo "$header$*$reset"
6060
}
6161

62+
# Certain tools are installed to GOBIN.
63+
export PATH="$(go env GOPATH)/bin:${PATH}"
64+
65+
# Kubebuilder's bin path should be the last added to PATH such that it is preferred.
6266
tmp_root=/tmp
6367
kb_root_dir=$tmp_root/kubebuilder
68+
mkdir -p "$kb_root_dir"
69+
export PATH="${kb_root_dir}/bin:${PATH}"
6470

6571
# Skip fetching and untaring the tools by setting the SKIP_FETCH_TOOLS variable
6672
# in your environment to any value:
@@ -71,66 +77,40 @@ kb_root_dir=$tmp_root/kubebuilder
7177
# machine, but rebuild the kubebuilder and kubebuilder-bin binaries.
7278
SKIP_FETCH_TOOLS=${SKIP_FETCH_TOOLS:-""}
7379

74-
# Remove previously built binary and fetched tools if they need to be fetched again
75-
function prepare_staging_dir {
76-
header_text "Preparing staging dir"
77-
78-
if [ -z "$SKIP_FETCH_TOOLS" ]; then
79-
rm -rf "$kb_root_dir"
80-
else
81-
rm -f "$kb_root_dir/bin/kubebuilder"
82-
fi
83-
}
84-
8580
# Build kubebuilder
8681
function build_kb {
8782
header_text "Building kubebuilder"
88-
go build -o $kb_root_dir/bin/kubebuilder ./cmd
89-
kb=$kb_root_dir/bin/kubebuilder
83+
84+
go build -o "${kb_root_dir}/bin/kubebuilder" ./cmd
85+
kb="${kb_root_dir}/bin/kubebuilder"
9086
}
9187

92-
# Fetch k8s API gen tools and make it available under kb_root_dir/bin.
88+
# Fetch k8s API tools and manage them globally with setup-envtest.
9389
function fetch_tools {
94-
if [ -z "$SKIP_FETCH_TOOLS" ]; then
95-
header_text "Fetching kb tools"
96-
kb_tools_archive_name="kubebuilder-tools-$k8s_version-$goos-$goarch.tar.gz"
97-
kb_tools_download_url="https://storage.googleapis.com/kubebuilder-tools/$kb_tools_archive_name"
98-
99-
kb_tools_archive_path="$tmp_root/$kb_tools_archive_name"
100-
if [ ! -f $kb_tools_archive_path ]; then
101-
curl -sL ${kb_tools_download_url} -o "$kb_tools_archive_path"
102-
fi
103-
tar -zvxf "$kb_tools_archive_path" -C "$tmp_root/"
90+
if ! is_installed setup-envtest; then
91+
header_text "Installing setup-envtest to $(go env GOPATH)/bin"
92+
93+
go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest
10494
fi
10595

106-
export KUBEBUILDER_ASSETS=$kb_root_dir/bin/
107-
}
96+
if [ -z "$SKIP_FETCH_TOOLS" ]; then
97+
header_text "Installing e2e tools with setup-envtest"
10898

109-
# Check if version of tool is less than or equal a required version
110-
function is_ver_lessthanequal {
111-
[ "$1" = "`echo -e \"$1\n$2\" | sort -V | head -n1`" ]
112-
}
99+
setup-envtest use $tools_k8s_version
100+
fi
113101

114-
# Check if version of tool is less than a required version
115-
function is_ver_lessthan {
116-
[ "$1" = "$2" ] && return 1 || is_ver_lessthanequal $1 $2
102+
# Export KUBEBUILDER_ASSETS.
103+
eval $(setup-envtest use -i -p env $tools_k8s_version)
104+
# Downloaded tools should be used instead of counterparts present in the environment.
105+
export PATH="${KUBEBUILDER_ASSETS}:${PATH}"
117106
}
118107

119-
# Installing kind in a temporal dir if no previously installed
108+
# Installing kind in a temporal dir if no previously installed to GOBIN.
120109
function install_kind {
121-
header_text "Checking if kind is installed"
122110
if ! is_installed kind ; then
123-
header_text "Kind not found, installing kind"
124-
pushd $(mktemp -d)
125-
GO111MODULE=on go get sigs.k8s.io/kind@v$kind_version
126-
popd
127-
else
128-
if is_ver_lessthan `kind version -q` $kind_version ; then
129-
header_text "Kind version less than v$kind_version, updating kind"
130-
pushd $(mktemp -d)
131-
GO111MODULE=on go get sigs.k8s.io/kind@v$kind_version
132-
popd
133-
fi
111+
header_text "Installing kind to $(go env GOPATH)/bin"
112+
113+
go install sigs.k8s.io/kind@v$kind_version
134114
fi
135115
}
136116

test/e2e/setup.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
# limitations under the License.
1616

1717
build_kb
18-
export PATH=$kb_root_dir/bin:$PATH
1918
fetch_tools
2019
install_kind
2120

test/e2e/utils/kubectl.go

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,15 @@ func (vi VersionInfo) GetMajorInt() uint64 { return vi.major }
112112
func (vi VersionInfo) GetMinorInt() uint64 { return vi.minor }
113113

114114
func (vi *VersionInfo) parseVersionInts() (err error) {
115-
if vi.major, err = strconv.ParseUint(vi.Major, 10, 64); err != nil {
116-
return err
115+
if vi.Major != "" {
116+
if vi.major, err = strconv.ParseUint(vi.Major, 10, 64); err != nil {
117+
return err
118+
}
117119
}
118-
if vi.minor, err = strconv.ParseUint(vi.Minor, 10, 64); err != nil {
119-
return err
120+
if vi.Minor != "" {
121+
if vi.minor, err = strconv.ParseUint(vi.Minor, 10, 64); err != nil {
122+
return err
123+
}
120124
}
121125
return nil
122126
}
@@ -127,24 +131,35 @@ type KubernetesVersion struct {
127131
ServerVersion VersionInfo `json:"serverVersion,omitempty"`
128132
}
129133

130-
func (v *KubernetesVersion) prepare() (err error) {
131-
if err = v.ClientVersion.parseVersionInts(); err != nil {
134+
func (v *KubernetesVersion) prepare() error {
135+
if err := v.ClientVersion.parseVersionInts(); err != nil {
136+
return err
137+
}
138+
if err := v.ServerVersion.parseVersionInts(); err != nil {
132139
return err
133140
}
134-
return v.ServerVersion.parseVersionInts()
141+
return nil
135142
}
136143

137144
// Version is a func to run kubectl version command
138145
func (k *Kubectl) Version() (ver KubernetesVersion, err error) {
139-
var out string
140-
if out, err = k.Command("version", "-o", "json"); err != nil {
141-
return KubernetesVersion{}, err
142-
}
143-
if err = json.Unmarshal([]byte(out), &ver); err != nil {
146+
out, err := k.Command("version", "-o", "json")
147+
if err != nil {
144148
return KubernetesVersion{}, err
145149
}
146-
if err = ver.prepare(); err != nil {
150+
if err := ver.decode(out); err != nil {
147151
return KubernetesVersion{}, err
148152
}
149153
return ver, nil
150154
}
155+
156+
func (v *KubernetesVersion) decode(out string) (err error) {
157+
dec := json.NewDecoder(strings.NewReader(out))
158+
if err := dec.Decode(&v); err != nil {
159+
return err
160+
}
161+
if err := v.prepare(); err != nil {
162+
return err
163+
}
164+
return nil
165+
}

test/e2e/utils/kubectl_test.go

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package utils
18+
19+
import (
20+
. "github.com/onsi/ginkgo"
21+
. "github.com/onsi/gomega"
22+
)
23+
24+
var _ = Describe("Kubectl", func() {
25+
var ver KubernetesVersion
26+
AfterEach(func() {
27+
ver = KubernetesVersion{}
28+
})
29+
Context("successful 'kubectl version' output", func() {
30+
It("decodes both client and server versions", func() {
31+
Expect(ver.decode(clientServerOutput)).To(Succeed())
32+
Expect(ver.ClientVersion.major).To(BeNumerically("==", 1))
33+
Expect(ver.ClientVersion.minor).To(BeNumerically("==", 21))
34+
Expect(ver.ServerVersion.major).To(BeNumerically("==", 1))
35+
Expect(ver.ServerVersion.minor).To(BeNumerically("==", 21))
36+
})
37+
It("decodes only client version", func() {
38+
Expect(ver.decode(clientOnlyOutput)).To(Succeed())
39+
Expect(ver.ClientVersion.major).To(BeNumerically("==", 1))
40+
Expect(ver.ClientVersion.minor).To(BeNumerically("==", 21))
41+
Expect(ver.ServerVersion.major).To(BeNumerically("==", 0))
42+
Expect(ver.ServerVersion.minor).To(BeNumerically("==", 0))
43+
})
44+
})
45+
Context("'kubectl version' output with non-JSON text", func() {
46+
It("handles warning logs", func() {
47+
Expect(ver.decode(clientServerWithWarningOutput)).To(Succeed())
48+
Expect(ver.ClientVersion.major).To(BeNumerically("==", 1))
49+
Expect(ver.ClientVersion.minor).To(BeNumerically("==", 21))
50+
Expect(ver.ServerVersion.major).To(BeNumerically("==", 1))
51+
Expect(ver.ServerVersion.minor).To(BeNumerically("==", 21))
52+
})
53+
})
54+
Context("with error text", func() {
55+
It("returns an error", func() {
56+
Expect(ver.decode(errorOutput)).NotTo(Succeed())
57+
})
58+
})
59+
})
60+
61+
const clientServerOutput = `
62+
{
63+
"clientVersion": {
64+
"major": "1",
65+
"minor": "21",
66+
"gitVersion": "v0.21.0-beta.1",
67+
"gitCommit": "0d10c3f72592addce965b9bb34992eb6fc283a3b",
68+
"gitTreeState": "clean",
69+
"buildDate": "2021-08-31T22:03:33Z",
70+
"goVersion": "go1.16.6",
71+
"compiler": "gc",
72+
"platform": "linux/amd64"
73+
},
74+
"serverVersion": {
75+
"major": "1",
76+
"minor": "21",
77+
"gitVersion": "v1.21.1",
78+
"gitCommit": "5e58841cce77d4bc13713ad2b91fa0d961e69192",
79+
"gitTreeState": "clean",
80+
"buildDate": "2021-05-18T01:10:20Z",
81+
"goVersion": "go1.16.4",
82+
"compiler": "gc",
83+
"platform": "linux/amd64"
84+
}
85+
}
86+
`
87+
88+
const clientOnlyOutput = `
89+
{
90+
"clientVersion": {
91+
"major": "1",
92+
"minor": "21",
93+
"gitVersion": "v0.21.0-beta.1",
94+
"gitCommit": "0d10c3f72592addce965b9bb34992eb6fc283a3b",
95+
"gitTreeState": "clean",
96+
"buildDate": "2021-08-31T22:03:33Z",
97+
"goVersion": "go1.16.6",
98+
"compiler": "gc",
99+
"platform": "linux/amd64"
100+
}
101+
}
102+
`
103+
104+
const clientServerWithWarningOutput = `
105+
{
106+
"clientVersion": {
107+
"major": "1",
108+
"minor": "21",
109+
"gitVersion": "v0.21.0-beta.1",
110+
"gitCommit": "0d10c3f72592addce965b9bb34992eb6fc283a3b",
111+
"gitTreeState": "clean",
112+
"buildDate": "2021-08-31T22:03:33Z",
113+
"goVersion": "go1.16.6",
114+
"compiler": "gc",
115+
"platform": "linux/amd64"
116+
},
117+
"serverVersion": {
118+
"major": "1",
119+
"minor": "21",
120+
"gitVersion": "v1.21.1",
121+
"gitCommit": "5e58841cce77d4bc13713ad2b91fa0d961e69192",
122+
"gitTreeState": "clean",
123+
"buildDate": "2021-05-18T01:10:20Z",
124+
"goVersion": "go1.16.4",
125+
"compiler": "gc",
126+
"platform": "linux/amd64"
127+
}
128+
}
129+
WARNING: version difference between client (0.21) and server (1.21) exceeds the supported minor version skew of +/-1
130+
`
131+
132+
const errorOutput = `
133+
ERROR: reason blah blah
134+
`

0 commit comments

Comments
 (0)