From ecf9facdb27b381be367e188fb741789137eb9ad Mon Sep 17 00:00:00 2001 From: Camila Macedo Date: Sun, 19 Jul 2020 14:27:17 +0100 Subject: [PATCH] envtest setup without additional bins to envtest --- .gitignore | 9 ++ pkg/plugin/v3/scaffolds/init.go | 8 ++ .../controller/controller_suitetest.go | 19 ++++ .../scaffolds/internal/templates/gitignore.go | 1 + .../scaffolds/internal/templates/makefile.go | 7 +- .../scaffolds/internal/templates/testsetup.go | 102 ++++++++++++++++++ testdata/project-v3-addon/Makefile | 7 +- .../controllers/suite_test.go | 9 ++ testdata/project-v3-addon/test-setup.sh | 52 +++++++++ testdata/project-v3-multigroup/Makefile | 7 +- .../controllers/crew/suite_test.go | 9 ++ .../controllers/foo.policy/suite_test.go | 9 ++ .../controllers/sea-creatures/suite_test.go | 9 ++ .../controllers/ship/suite_test.go | 9 ++ testdata/project-v3-multigroup/test-setup.sh | 52 +++++++++ testdata/project-v3/Makefile | 7 +- testdata/project-v3/controllers/suite_test.go | 9 ++ testdata/project-v3/test-setup.sh | 52 +++++++++ 18 files changed, 373 insertions(+), 4 deletions(-) create mode 100644 pkg/plugin/v3/scaffolds/internal/templates/testsetup.go create mode 100755 testdata/project-v3-addon/test-setup.sh create mode 100755 testdata/project-v3-multigroup/test-setup.sh create mode 100755 testdata/project-v3/test-setup.sh diff --git a/.gitignore b/.gitignore index 69415a7d940..feeda20d4e8 100644 --- a/.gitignore +++ b/.gitignore @@ -29,3 +29,12 @@ bin/* /testdata/project-v3/go.sum /testdata/project-v3-multigroup/go.sum /testdata/project-v3-addon/go.sum + +# Binaries for programs and plugins +*.exe +*.exe~ +*.dll +*.so +*.dylib +bin +testbin diff --git a/pkg/plugin/v3/scaffolds/init.go b/pkg/plugin/v3/scaffolds/init.go index 7ea614e98c8..016b8ee0bb4 100644 --- a/pkg/plugin/v3/scaffolds/init.go +++ b/pkg/plugin/v3/scaffolds/init.go @@ -40,6 +40,10 @@ const ( ControllerToolsVersion = "v0.3.0" // KustomizeVersion is the kubernetes-sigs/kustomize version to be used in the project KustomizeVersion = "v3.5.4" + //KubernetesVersion is the K8S version which will be used in the script + KubernetesVersion = "v1.18.2" + //ETCDVersion is the ETCD version which will be used in the script + ETCDVersion = "v3.4.3" imageName = "controller:latest" ) @@ -111,6 +115,10 @@ func (s *initScaffolder) scaffold() error { ControllerToolsVersion: ControllerToolsVersion, KustomizeVersion: KustomizeVersion, }, + &templates.TestSetup{ + KubernetesVersion: KubernetesVersion, + ETCDVersion: ETCDVersion, + }, &templates.Dockerfile{}, &templates.DockerignoreFile{}, &templates.Kustomize{}, diff --git a/pkg/plugin/v3/scaffolds/internal/templates/controller/controller_suitetest.go b/pkg/plugin/v3/scaffolds/internal/templates/controller/controller_suitetest.go index 46ce6bc4691..38ea83425bb 100644 --- a/pkg/plugin/v3/scaffolds/internal/templates/controller/controller_suitetest.go +++ b/pkg/plugin/v3/scaffolds/internal/templates/controller/controller_suitetest.go @@ -33,6 +33,9 @@ type SuiteTest struct { file.MultiGroupMixin file.BoilerplateMixin file.ResourceMixin + + //TestBinPath is the path where the binaries required for to run the tests will b placed + TestBinPath string } // SetTemplateDefaults implements file.Template @@ -46,6 +49,14 @@ func (f *SuiteTest) SetTemplateDefaults() error { } f.Path = f.Resource.Replacer().Replace(f.Path) + if f.TestBinPath == "" { + if f.MultiGroup { + f.TestBinPath = "../../testbin" + } else { + f.TestBinPath = "../testbin" + } + } + f.TemplateBody = fmt.Sprintf(controllerSuiteTestTemplate, file.NewMarkerFor(f.Path, importMarker), file.NewMarkerFor(f.Path, addSchemeMarker), @@ -134,6 +145,10 @@ func TestAPIs(t *testing.T) { } var _ = BeforeSuite(func(done Done) { + Expect(os.Setenv("TEST_ASSET_KUBE_APISERVER", "{{ .TestBinPath }}/kube-apiserver")).To(Succeed()) + Expect(os.Setenv("TEST_ASSET_ETCD", "{{ .TestBinPath }}/etcd")).To(Succeed()) + Expect(os.Setenv("TEST_ASSET_KUBECTL", "{{ .TestBinPath }}/kubectl")).To(Succeed()) + logf.SetLogger(zap.LoggerTo(GinkgoWriter, true)) By("bootstrapping test environment") @@ -159,5 +174,9 @@ var _ = AfterSuite(func() { By("tearing down the test environment") err := testEnv.Stop() Expect(err).ToNot(HaveOccurred()) + + Expect(os.Unsetenv("TEST_ASSET_KUBE_APISERVER")).To(Succeed()) + Expect(os.Unsetenv("TEST_ASSET_ETCD")).To(Succeed()) + Expect(os.Unsetenv("TEST_ASSET_KUBECTL")).To(Succeed()) }) ` diff --git a/pkg/plugin/v3/scaffolds/internal/templates/gitignore.go b/pkg/plugin/v3/scaffolds/internal/templates/gitignore.go index 3910f8d3195..57029bb939f 100644 --- a/pkg/plugin/v3/scaffolds/internal/templates/gitignore.go +++ b/pkg/plugin/v3/scaffolds/internal/templates/gitignore.go @@ -46,6 +46,7 @@ const gitignoreTemplate = ` *.so *.dylib bin +testbin # Test binary, build with ` + "`go test -c`" + ` *.test diff --git a/pkg/plugin/v3/scaffolds/internal/templates/makefile.go b/pkg/plugin/v3/scaffolds/internal/templates/makefile.go index ae4e140ed00..620d7f94d46 100644 --- a/pkg/plugin/v3/scaffolds/internal/templates/makefile.go +++ b/pkg/plugin/v3/scaffolds/internal/templates/makefile.go @@ -70,7 +70,7 @@ endif all: manager # Run tests -test: generate fmt vet manifests +test: testsetup generate fmt vet manifests go test -race -coverprofile cover.out ./... # Build manager binary @@ -153,4 +153,9 @@ KUSTOMIZE=$(GOBIN)/kustomize else KUSTOMIZE=$(shell which kustomize) endif + +# Setup binaries required to run the tests +testsetup: + chmod +x test-setup.sh + ./test-setup.sh ` diff --git a/pkg/plugin/v3/scaffolds/internal/templates/testsetup.go b/pkg/plugin/v3/scaffolds/internal/templates/testsetup.go new file mode 100644 index 00000000000..aa03a33c74e --- /dev/null +++ b/pkg/plugin/v3/scaffolds/internal/templates/testsetup.go @@ -0,0 +1,102 @@ +/* +Copyright 2020 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package templates + +import ( + "sigs.k8s.io/kubebuilder/pkg/model/file" +) + +var _ file.Template = &Makefile{} + +// Makefile scaffolds the Makefile +type TestSetup struct { + file.TemplateMixin + + //KubernetesVersion is the K8S version which will be used in the script + KubernetesVersion string + + //ETCDVersion is the ETCD version which will be used in the script + ETCDVersion string +} + +// SetTemplateDefaults implements input.Template +func (f *TestSetup) SetTemplateDefaults() error { + if f.Path == "" { + f.Path = "test-setup.sh" + } + + f.TemplateBody = testsetupTemplate + + f.IfExistsAction = file.Error + + return nil +} + +//nolint:lll +const testsetupTemplate = `set -eu + +# */ +# To use envtest is required etcd, kube-apiserver and kubetcl binaries in the testbin directory. +# This script will perform this setup for linux or mac os x envs. +# */ + +K8S_VER={{ .KubernetesVersion }} +ETCD_VER={{ .ETCDVersion }} +OS=$(uname -s | tr '[:upper:]' '[:lower:]') +ARCH=$(uname -m | sed 's/x86_64/amd64/') +ETCD_EXT="tar.gz" + +rm -rf testbin +mkdir -p testbin + +# install etcd bin +# the extension for linux env is not equals for mac os x +if [ $OS == "darwin" ]; then + ETCD_EXT="zip" +fi +[[ -x testbin/etcd ]] || curl -L https://storage.googleapis.com/etcd/${ETCD_VER}/etcd-${ETCD_VER}-${OS}-${ARCH}.${ETCD_EXT} | tar zx -C testbin --strip-components=1 etcd-${ETCD_VER}-${OS}-${ARCH}/etcd + +# install kube-apiserver and kubetcl bin +if [ $OS == "darwin" ] +then + # kubernetes do not provide the kubernetes-server for darwin, so to have the kube-apiserver is rquired to build it locally + # if the project is already cloned locally do nothing + if [ ! -d $GOPATH/src/k8s.io/kubernetes ]; then + git clone https://github.com/kubernetes/kubernetes $GOPATH/src/k8s.io/kubernetes --depth=1 -b v1.18.2 + fi + + # if the kube-apiserve is alredy built just copy + if [ ! -f $GOPATH/src/k8s.io/kubernetes/_output/local/bin/darwin/amd64/kube-apiserver ]; then + DIR=$(pwd) + cd $GOPATH/src/k8s.io/kubernetes + # Build for linux first otherwise it won't work for darwin - :( + export KUBE_BUILD_PLATFORMS="linux/amd64" + make WHAT=cmd/kube-apiserver + export KUBE_BUILD_PLATFORMS="darwin/amd64" + make WHAT=cmd/kube-apiserver + cd ${DIR} + fi + cp $GOPATH/src/k8s.io/kubernetes/_output/local/bin/darwin/amd64/kube-apiserver testbin/ + + # now let's get the kubectl + curl -LO https://storage.googleapis.com/kubernetes-release/release/${K8S_VER}/bin/darwin/amd64/kubectl + chmod +x kubectl + mv kubectl testbin/ +else + [[ -x testbin/kube-apiserver && -x testbin/kubectl ]] || curl -L https://dl.k8s.io/${K8S_VER}/kubernetes-server-${OS}-${ARCH}.tar.gz | tar zx -C testbin --strip-components=3 kubernetes/server/bin/kube-apiserver kubernetes/server/bin/kubectl +fi +` diff --git a/testdata/project-v3-addon/Makefile b/testdata/project-v3-addon/Makefile index 514e029eedd..921f1ffa6c7 100644 --- a/testdata/project-v3-addon/Makefile +++ b/testdata/project-v3-addon/Makefile @@ -14,7 +14,7 @@ endif all: manager # Run tests -test: generate fmt vet manifests +test: testsetup generate fmt vet manifests go test -race -coverprofile cover.out ./... # Build manager binary @@ -97,3 +97,8 @@ KUSTOMIZE=$(GOBIN)/kustomize else KUSTOMIZE=$(shell which kustomize) endif + +# Setup binaries required to run the tests +testsetup: + chmod +x test-setup.sh + ./test-setup.sh diff --git a/testdata/project-v3-addon/controllers/suite_test.go b/testdata/project-v3-addon/controllers/suite_test.go index 9281118cbd9..d89740e4275 100644 --- a/testdata/project-v3-addon/controllers/suite_test.go +++ b/testdata/project-v3-addon/controllers/suite_test.go @@ -17,6 +17,7 @@ limitations under the License. package controllers import ( + "os" "path/filepath" "testing" @@ -50,6 +51,10 @@ func TestAPIs(t *testing.T) { } var _ = BeforeSuite(func(done Done) { + Expect(os.Setenv("TEST_ASSET_KUBE_APISERVER", "../testbin/kube-apiserver")).To(Succeed()) + Expect(os.Setenv("TEST_ASSET_ETCD", "../testbin/etcd")).To(Succeed()) + Expect(os.Setenv("TEST_ASSET_KUBECTL", "../testbin/kubectl")).To(Succeed()) + logf.SetLogger(zap.LoggerTo(GinkgoWriter, true)) By("bootstrapping test environment") @@ -84,4 +89,8 @@ var _ = AfterSuite(func() { By("tearing down the test environment") err := testEnv.Stop() Expect(err).ToNot(HaveOccurred()) + + Expect(os.Unsetenv("TEST_ASSET_KUBE_APISERVER")).To(Succeed()) + Expect(os.Unsetenv("TEST_ASSET_ETCD")).To(Succeed()) + Expect(os.Unsetenv("TEST_ASSET_KUBECTL")).To(Succeed()) }) diff --git a/testdata/project-v3-addon/test-setup.sh b/testdata/project-v3-addon/test-setup.sh new file mode 100755 index 00000000000..b8fda8fa9d3 --- /dev/null +++ b/testdata/project-v3-addon/test-setup.sh @@ -0,0 +1,52 @@ +set -eu + +# */ +# To use envtest is required etcd, kube-apiserver and kubetcl binaries in the testbin directory. +# This script will perform this setup for linux or mac os x envs. +# */ + +K8S_VER=v1.18.2 +ETCD_VER=v3.4.3 +OS=$(uname -s | tr '[:upper:]' '[:lower:]') +ARCH=$(uname -m | sed 's/x86_64/amd64/') +ETCD_EXT="tar.gz" + +rm -rf testbin +mkdir -p testbin + +# install etcd bin +# the extension for linux env is not equals for mac os x +if [ $OS == "darwin" ]; then + ETCD_EXT="zip" +fi +[[ -x testbin/etcd ]] || curl -L https://storage.googleapis.com/etcd/${ETCD_VER}/etcd-${ETCD_VER}-${OS}-${ARCH}.${ETCD_EXT} | tar zx -C testbin --strip-components=1 etcd-${ETCD_VER}-${OS}-${ARCH}/etcd + +# install kube-apiserver and kubetcl bin +if [ $OS == "darwin" ] +then + # kubernetes do not provide the kubernetes-server for darwin, so to have the kube-apiserver is rquired to build it locally + # if the project is already cloned locally do nothing + if [ ! -d $GOPATH/src/k8s.io/kubernetes ]; then + git clone https://github.com/kubernetes/kubernetes $GOPATH/src/k8s.io/kubernetes --depth=1 -b v1.18.2 + fi + + # if the kube-apiserve is alredy built just copy + if [ ! -f $GOPATH/src/k8s.io/kubernetes/_output/local/bin/darwin/amd64/kube-apiserver ]; then + DIR=$(pwd) + cd $GOPATH/src/k8s.io/kubernetes + # Build for linux first otherwise it won't work for darwin - :( + export KUBE_BUILD_PLATFORMS="linux/amd64" + make WHAT=cmd/kube-apiserver + export KUBE_BUILD_PLATFORMS="darwin/amd64" + make WHAT=cmd/kube-apiserver + cd ${DIR} + fi + cp $GOPATH/src/k8s.io/kubernetes/_output/local/bin/darwin/amd64/kube-apiserver testbin/ + + # now let's get the kubectl + curl -LO https://storage.googleapis.com/kubernetes-release/release/${K8S_VER}/bin/darwin/amd64/kubectl + chmod +x kubectl + mv kubectl testbin/ +else + [[ -x testbin/kube-apiserver && -x testbin/kubectl ]] || curl -L https://dl.k8s.io/${K8S_VER}/kubernetes-server-${OS}-${ARCH}.tar.gz | tar zx -C testbin --strip-components=3 kubernetes/server/bin/kube-apiserver kubernetes/server/bin/kubectl +fi diff --git a/testdata/project-v3-multigroup/Makefile b/testdata/project-v3-multigroup/Makefile index 514e029eedd..921f1ffa6c7 100644 --- a/testdata/project-v3-multigroup/Makefile +++ b/testdata/project-v3-multigroup/Makefile @@ -14,7 +14,7 @@ endif all: manager # Run tests -test: generate fmt vet manifests +test: testsetup generate fmt vet manifests go test -race -coverprofile cover.out ./... # Build manager binary @@ -97,3 +97,8 @@ KUSTOMIZE=$(GOBIN)/kustomize else KUSTOMIZE=$(shell which kustomize) endif + +# Setup binaries required to run the tests +testsetup: + chmod +x test-setup.sh + ./test-setup.sh diff --git a/testdata/project-v3-multigroup/controllers/crew/suite_test.go b/testdata/project-v3-multigroup/controllers/crew/suite_test.go index bc8be190119..a4cd38d6647 100644 --- a/testdata/project-v3-multigroup/controllers/crew/suite_test.go +++ b/testdata/project-v3-multigroup/controllers/crew/suite_test.go @@ -17,6 +17,7 @@ limitations under the License. package controllers import ( + "os" "path/filepath" "testing" @@ -50,6 +51,10 @@ func TestAPIs(t *testing.T) { } var _ = BeforeSuite(func(done Done) { + Expect(os.Setenv("TEST_ASSET_KUBE_APISERVER", "../../testbin/kube-apiserver")).To(Succeed()) + Expect(os.Setenv("TEST_ASSET_ETCD", "../../testbin/etcd")).To(Succeed()) + Expect(os.Setenv("TEST_ASSET_KUBECTL", "../../testbin/kubectl")).To(Succeed()) + logf.SetLogger(zap.LoggerTo(GinkgoWriter, true)) By("bootstrapping test environment") @@ -78,4 +83,8 @@ var _ = AfterSuite(func() { By("tearing down the test environment") err := testEnv.Stop() Expect(err).ToNot(HaveOccurred()) + + Expect(os.Unsetenv("TEST_ASSET_KUBE_APISERVER")).To(Succeed()) + Expect(os.Unsetenv("TEST_ASSET_ETCD")).To(Succeed()) + Expect(os.Unsetenv("TEST_ASSET_KUBECTL")).To(Succeed()) }) diff --git a/testdata/project-v3-multigroup/controllers/foo.policy/suite_test.go b/testdata/project-v3-multigroup/controllers/foo.policy/suite_test.go index a75aa322952..0eb3df8423d 100644 --- a/testdata/project-v3-multigroup/controllers/foo.policy/suite_test.go +++ b/testdata/project-v3-multigroup/controllers/foo.policy/suite_test.go @@ -17,6 +17,7 @@ limitations under the License. package controllers import ( + "os" "path/filepath" "testing" @@ -50,6 +51,10 @@ func TestAPIs(t *testing.T) { } var _ = BeforeSuite(func(done Done) { + Expect(os.Setenv("TEST_ASSET_KUBE_APISERVER", "../../testbin/kube-apiserver")).To(Succeed()) + Expect(os.Setenv("TEST_ASSET_ETCD", "../../testbin/etcd")).To(Succeed()) + Expect(os.Setenv("TEST_ASSET_KUBECTL", "../../testbin/kubectl")).To(Succeed()) + logf.SetLogger(zap.LoggerTo(GinkgoWriter, true)) By("bootstrapping test environment") @@ -78,4 +83,8 @@ var _ = AfterSuite(func() { By("tearing down the test environment") err := testEnv.Stop() Expect(err).ToNot(HaveOccurred()) + + Expect(os.Unsetenv("TEST_ASSET_KUBE_APISERVER")).To(Succeed()) + Expect(os.Unsetenv("TEST_ASSET_ETCD")).To(Succeed()) + Expect(os.Unsetenv("TEST_ASSET_KUBECTL")).To(Succeed()) }) diff --git a/testdata/project-v3-multigroup/controllers/sea-creatures/suite_test.go b/testdata/project-v3-multigroup/controllers/sea-creatures/suite_test.go index da5788a7de6..b1fe542b0bf 100644 --- a/testdata/project-v3-multigroup/controllers/sea-creatures/suite_test.go +++ b/testdata/project-v3-multigroup/controllers/sea-creatures/suite_test.go @@ -17,6 +17,7 @@ limitations under the License. package controllers import ( + "os" "path/filepath" "testing" @@ -51,6 +52,10 @@ func TestAPIs(t *testing.T) { } var _ = BeforeSuite(func(done Done) { + Expect(os.Setenv("TEST_ASSET_KUBE_APISERVER", "../../testbin/kube-apiserver")).To(Succeed()) + Expect(os.Setenv("TEST_ASSET_ETCD", "../../testbin/etcd")).To(Succeed()) + Expect(os.Setenv("TEST_ASSET_KUBECTL", "../../testbin/kubectl")).To(Succeed()) + logf.SetLogger(zap.LoggerTo(GinkgoWriter, true)) By("bootstrapping test environment") @@ -82,4 +87,8 @@ var _ = AfterSuite(func() { By("tearing down the test environment") err := testEnv.Stop() Expect(err).ToNot(HaveOccurred()) + + Expect(os.Unsetenv("TEST_ASSET_KUBE_APISERVER")).To(Succeed()) + Expect(os.Unsetenv("TEST_ASSET_ETCD")).To(Succeed()) + Expect(os.Unsetenv("TEST_ASSET_KUBECTL")).To(Succeed()) }) diff --git a/testdata/project-v3-multigroup/controllers/ship/suite_test.go b/testdata/project-v3-multigroup/controllers/ship/suite_test.go index 0fedb93aeaa..5d0fdc12bed 100644 --- a/testdata/project-v3-multigroup/controllers/ship/suite_test.go +++ b/testdata/project-v3-multigroup/controllers/ship/suite_test.go @@ -17,6 +17,7 @@ limitations under the License. package controllers import ( + "os" "path/filepath" "testing" @@ -52,6 +53,10 @@ func TestAPIs(t *testing.T) { } var _ = BeforeSuite(func(done Done) { + Expect(os.Setenv("TEST_ASSET_KUBE_APISERVER", "../../testbin/kube-apiserver")).To(Succeed()) + Expect(os.Setenv("TEST_ASSET_ETCD", "../../testbin/etcd")).To(Succeed()) + Expect(os.Setenv("TEST_ASSET_KUBECTL", "../../testbin/kubectl")).To(Succeed()) + logf.SetLogger(zap.LoggerTo(GinkgoWriter, true)) By("bootstrapping test environment") @@ -86,4 +91,8 @@ var _ = AfterSuite(func() { By("tearing down the test environment") err := testEnv.Stop() Expect(err).ToNot(HaveOccurred()) + + Expect(os.Unsetenv("TEST_ASSET_KUBE_APISERVER")).To(Succeed()) + Expect(os.Unsetenv("TEST_ASSET_ETCD")).To(Succeed()) + Expect(os.Unsetenv("TEST_ASSET_KUBECTL")).To(Succeed()) }) diff --git a/testdata/project-v3-multigroup/test-setup.sh b/testdata/project-v3-multigroup/test-setup.sh new file mode 100755 index 00000000000..b8fda8fa9d3 --- /dev/null +++ b/testdata/project-v3-multigroup/test-setup.sh @@ -0,0 +1,52 @@ +set -eu + +# */ +# To use envtest is required etcd, kube-apiserver and kubetcl binaries in the testbin directory. +# This script will perform this setup for linux or mac os x envs. +# */ + +K8S_VER=v1.18.2 +ETCD_VER=v3.4.3 +OS=$(uname -s | tr '[:upper:]' '[:lower:]') +ARCH=$(uname -m | sed 's/x86_64/amd64/') +ETCD_EXT="tar.gz" + +rm -rf testbin +mkdir -p testbin + +# install etcd bin +# the extension for linux env is not equals for mac os x +if [ $OS == "darwin" ]; then + ETCD_EXT="zip" +fi +[[ -x testbin/etcd ]] || curl -L https://storage.googleapis.com/etcd/${ETCD_VER}/etcd-${ETCD_VER}-${OS}-${ARCH}.${ETCD_EXT} | tar zx -C testbin --strip-components=1 etcd-${ETCD_VER}-${OS}-${ARCH}/etcd + +# install kube-apiserver and kubetcl bin +if [ $OS == "darwin" ] +then + # kubernetes do not provide the kubernetes-server for darwin, so to have the kube-apiserver is rquired to build it locally + # if the project is already cloned locally do nothing + if [ ! -d $GOPATH/src/k8s.io/kubernetes ]; then + git clone https://github.com/kubernetes/kubernetes $GOPATH/src/k8s.io/kubernetes --depth=1 -b v1.18.2 + fi + + # if the kube-apiserve is alredy built just copy + if [ ! -f $GOPATH/src/k8s.io/kubernetes/_output/local/bin/darwin/amd64/kube-apiserver ]; then + DIR=$(pwd) + cd $GOPATH/src/k8s.io/kubernetes + # Build for linux first otherwise it won't work for darwin - :( + export KUBE_BUILD_PLATFORMS="linux/amd64" + make WHAT=cmd/kube-apiserver + export KUBE_BUILD_PLATFORMS="darwin/amd64" + make WHAT=cmd/kube-apiserver + cd ${DIR} + fi + cp $GOPATH/src/k8s.io/kubernetes/_output/local/bin/darwin/amd64/kube-apiserver testbin/ + + # now let's get the kubectl + curl -LO https://storage.googleapis.com/kubernetes-release/release/${K8S_VER}/bin/darwin/amd64/kubectl + chmod +x kubectl + mv kubectl testbin/ +else + [[ -x testbin/kube-apiserver && -x testbin/kubectl ]] || curl -L https://dl.k8s.io/${K8S_VER}/kubernetes-server-${OS}-${ARCH}.tar.gz | tar zx -C testbin --strip-components=3 kubernetes/server/bin/kube-apiserver kubernetes/server/bin/kubectl +fi diff --git a/testdata/project-v3/Makefile b/testdata/project-v3/Makefile index 514e029eedd..921f1ffa6c7 100644 --- a/testdata/project-v3/Makefile +++ b/testdata/project-v3/Makefile @@ -14,7 +14,7 @@ endif all: manager # Run tests -test: generate fmt vet manifests +test: testsetup generate fmt vet manifests go test -race -coverprofile cover.out ./... # Build manager binary @@ -97,3 +97,8 @@ KUSTOMIZE=$(GOBIN)/kustomize else KUSTOMIZE=$(shell which kustomize) endif + +# Setup binaries required to run the tests +testsetup: + chmod +x test-setup.sh + ./test-setup.sh diff --git a/testdata/project-v3/controllers/suite_test.go b/testdata/project-v3/controllers/suite_test.go index 7b4e4598420..651eee9fcec 100644 --- a/testdata/project-v3/controllers/suite_test.go +++ b/testdata/project-v3/controllers/suite_test.go @@ -17,6 +17,7 @@ limitations under the License. package controllers import ( + "os" "path/filepath" "testing" @@ -50,6 +51,10 @@ func TestAPIs(t *testing.T) { } var _ = BeforeSuite(func(done Done) { + Expect(os.Setenv("TEST_ASSET_KUBE_APISERVER", "../testbin/kube-apiserver")).To(Succeed()) + Expect(os.Setenv("TEST_ASSET_ETCD", "../testbin/etcd")).To(Succeed()) + Expect(os.Setenv("TEST_ASSET_KUBECTL", "../testbin/kubectl")).To(Succeed()) + logf.SetLogger(zap.LoggerTo(GinkgoWriter, true)) By("bootstrapping test environment") @@ -84,4 +89,8 @@ var _ = AfterSuite(func() { By("tearing down the test environment") err := testEnv.Stop() Expect(err).ToNot(HaveOccurred()) + + Expect(os.Unsetenv("TEST_ASSET_KUBE_APISERVER")).To(Succeed()) + Expect(os.Unsetenv("TEST_ASSET_ETCD")).To(Succeed()) + Expect(os.Unsetenv("TEST_ASSET_KUBECTL")).To(Succeed()) }) diff --git a/testdata/project-v3/test-setup.sh b/testdata/project-v3/test-setup.sh new file mode 100755 index 00000000000..b8fda8fa9d3 --- /dev/null +++ b/testdata/project-v3/test-setup.sh @@ -0,0 +1,52 @@ +set -eu + +# */ +# To use envtest is required etcd, kube-apiserver and kubetcl binaries in the testbin directory. +# This script will perform this setup for linux or mac os x envs. +# */ + +K8S_VER=v1.18.2 +ETCD_VER=v3.4.3 +OS=$(uname -s | tr '[:upper:]' '[:lower:]') +ARCH=$(uname -m | sed 's/x86_64/amd64/') +ETCD_EXT="tar.gz" + +rm -rf testbin +mkdir -p testbin + +# install etcd bin +# the extension for linux env is not equals for mac os x +if [ $OS == "darwin" ]; then + ETCD_EXT="zip" +fi +[[ -x testbin/etcd ]] || curl -L https://storage.googleapis.com/etcd/${ETCD_VER}/etcd-${ETCD_VER}-${OS}-${ARCH}.${ETCD_EXT} | tar zx -C testbin --strip-components=1 etcd-${ETCD_VER}-${OS}-${ARCH}/etcd + +# install kube-apiserver and kubetcl bin +if [ $OS == "darwin" ] +then + # kubernetes do not provide the kubernetes-server for darwin, so to have the kube-apiserver is rquired to build it locally + # if the project is already cloned locally do nothing + if [ ! -d $GOPATH/src/k8s.io/kubernetes ]; then + git clone https://github.com/kubernetes/kubernetes $GOPATH/src/k8s.io/kubernetes --depth=1 -b v1.18.2 + fi + + # if the kube-apiserve is alredy built just copy + if [ ! -f $GOPATH/src/k8s.io/kubernetes/_output/local/bin/darwin/amd64/kube-apiserver ]; then + DIR=$(pwd) + cd $GOPATH/src/k8s.io/kubernetes + # Build for linux first otherwise it won't work for darwin - :( + export KUBE_BUILD_PLATFORMS="linux/amd64" + make WHAT=cmd/kube-apiserver + export KUBE_BUILD_PLATFORMS="darwin/amd64" + make WHAT=cmd/kube-apiserver + cd ${DIR} + fi + cp $GOPATH/src/k8s.io/kubernetes/_output/local/bin/darwin/amd64/kube-apiserver testbin/ + + # now let's get the kubectl + curl -LO https://storage.googleapis.com/kubernetes-release/release/${K8S_VER}/bin/darwin/amd64/kubectl + chmod +x kubectl + mv kubectl testbin/ +else + [[ -x testbin/kube-apiserver && -x testbin/kubectl ]] || curl -L https://dl.k8s.io/${K8S_VER}/kubernetes-server-${OS}-${ARCH}.tar.gz | tar zx -C testbin --strip-components=3 kubernetes/server/bin/kube-apiserver kubernetes/server/bin/kubectl +fi