Skip to content

Commit

Permalink
envtest setup without additional bins to envtest
Browse files Browse the repository at this point in the history
  • Loading branch information
camilamacedo86 committed Jul 19, 2020
1 parent 6678ae5 commit 3b7a8bc
Show file tree
Hide file tree
Showing 18 changed files with 401 additions and 4 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions pkg/plugin/v3/scaffolds/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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{},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand Down Expand Up @@ -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")
Expand All @@ -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())
})
`
1 change: 1 addition & 0 deletions pkg/plugin/v3/scaffolds/internal/templates/gitignore.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const gitignoreTemplate = `
*.so
*.dylib
bin
testbin
# Test binary, build with ` + "`go test -c`" + `
*.test
Expand Down
7 changes: 6 additions & 1 deletion pkg/plugin/v3/scaffolds/internal/templates/makefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
`
109 changes: 109 additions & 0 deletions pkg/plugin/v3/scaffolds/internal/templates/testsetup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
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"
TESTBIN_DIR=testbin
# Do nothing if the $TESTBIN_DIR directory exist already.
if [ ! -d $TESTBIN_DIR ]; then
mkdir -p $TESTBIN_DIR
# install etcd binary
# the extension for linux env is not equals for mac os x
if [ $OS == "darwin" ]; then
ETCD_EXT="zip"
fi
[[ -x ${TESTBIN_DIR}/etcd ]] || curl -L https://storage.googleapis.com/etcd/${ETCD_VER}/etcd-${ETCD_VER}-${OS}-${ARCH}.${ETCD_EXT} | tar zx -C ${TESTBIN_DIR} --strip-components=1 etcd-${ETCD_VER}-${OS}-${ARCH}/etcd
# install kube-apiserver and kubetcl binaries
if [ $OS == "darwin" ]
then
# kubernetes do not provide the kubernetes-server for darwin,
# In this way, to have the kube-apiserver is required to build it locally
# if the project is cloned locally already 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 built already then, just copy it
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_DIR/
# setup kubectl binary
curl -LO https://storage.googleapis.com/kubernetes-release/release/${K8S_VER}/bin/darwin/amd64/kubectl
chmod +x kubectl
mv kubectl $TESTBIN_DIR/
# allow run the tests without the Mac OS Firewall popup shows for each execution
codesign --deep --force --verbose --sign - ./${TESTBIN_DIR}/kube-apiserver
else
[[ -x testbin/kube-apiserver && -x ${TESTBIN_DIR}/kubectl ]] || curl -L https://dl.k8s.io/${K8S_VER}/kubernetes-server-${OS}-${ARCH}.tar.gz | tar zx -C ${TESTBIN_DIR} --strip-components=3 kubernetes/server/bin/kube-apiserver kubernetes/server/bin/kubectl
fi
fi
`
7 changes: 6 additions & 1 deletion testdata/project-v3-addon/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
9 changes: 9 additions & 0 deletions testdata/project-v3-addon/controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package controllers

import (
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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())
})
59 changes: 59 additions & 0 deletions testdata/project-v3-addon/test-setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
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"
TESTBIN_DIR=testbin

# Do nothing if the $TESTBIN_DIR directory exist already.
if [ ! -d $TESTBIN_DIR ]; then
mkdir -p $TESTBIN_DIR

# install etcd binary
# the extension for linux env is not equals for mac os x
if [ $OS == "darwin" ]; then
ETCD_EXT="zip"
fi
[[ -x ${TESTBIN_DIR}/etcd ]] || curl -L https://storage.googleapis.com/etcd/${ETCD_VER}/etcd-${ETCD_VER}-${OS}-${ARCH}.${ETCD_EXT} | tar zx -C ${TESTBIN_DIR} --strip-components=1 etcd-${ETCD_VER}-${OS}-${ARCH}/etcd

# install kube-apiserver and kubetcl binaries
if [ $OS == "darwin" ]
then
# kubernetes do not provide the kubernetes-server for darwin,
# In this way, to have the kube-apiserver is required to build it locally
# if the project is cloned locally already 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 built already then, just copy it
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_DIR/

# setup kubectl binary
curl -LO https://storage.googleapis.com/kubernetes-release/release/${K8S_VER}/bin/darwin/amd64/kubectl
chmod +x kubectl
mv kubectl $TESTBIN_DIR/

# allow run the tests without the Mac OS Firewall popup shows for each execution
codesign --deep --force --verbose --sign - ./${TESTBIN_DIR}/kube-apiserver
else
[[ -x testbin/kube-apiserver && -x ${TESTBIN_DIR}/kubectl ]] || curl -L https://dl.k8s.io/${K8S_VER}/kubernetes-server-${OS}-${ARCH}.tar.gz | tar zx -C ${TESTBIN_DIR} --strip-components=3 kubernetes/server/bin/kube-apiserver kubernetes/server/bin/kubectl
fi
fi
7 changes: 6 additions & 1 deletion testdata/project-v3-multigroup/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
9 changes: 9 additions & 0 deletions testdata/project-v3-multigroup/controllers/crew/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package controllers

import (
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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())
})
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package controllers

import (
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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())
})
Loading

0 comments on commit 3b7a8bc

Please sign in to comment.