Skip to content

Commit

Permalink
Merge pull request kubernetes-sigs#1756 from estroz/feature/go-v3-alp…
Browse files Browse the repository at this point in the history
…ha-e2e

🌱 test: add `go/v3-alpha` e2e tests
  • Loading branch information
k8s-ci-robot authored Nov 10, 2020
2 parents 5cde5fa + c4f88e2 commit 0cebbd6
Show file tree
Hide file tree
Showing 14 changed files with 528 additions and 358 deletions.
5 changes: 1 addition & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,12 @@ install: ## Build and install the binary with the current source code. Use it to

.PHONY: generate
generate: ## Update/generate all mock data. You should run this commands to update the mock data after your changes.
# If exists, rremove the controller-gen installed locally. It ensures that the version which will be used
# is the version defined in the Makefile scaffolded.
- rm -rf $(CONTROLLER_GEN_BIN_PATH)
make generate-testdata
go mod tidy

.PHONY: generate-testdata
generate-testdata: ## Update/generate the testdata in $GOPATH/src/sigs.k8s.io/kubebuilder
GO111MODULE=on ./generate_testdata.sh
./generate_testdata.sh

.PHONY: lint
lint: golangci-lint ## Run golangci lint checks
Expand Down
1 change: 1 addition & 0 deletions common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ cd "$base_dir" || {
}

go_workspace=''
export GOPATH=${GOPATH:-$(go env GOPATH)}
for p in ${GOPATH//:/ }; do
if [[ $PWD/ = $p/* ]]; then
go_workspace=$p
Expand Down
16 changes: 9 additions & 7 deletions generate_testdata.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.

set -e

source common.sh

build_kb() {
go build -o ./bin/kubebuilder sigs.k8s.io/kubebuilder/v2/cmd
}


#
# This function scaffolds test projects given a project name and
# project-version.
Expand All @@ -32,15 +29,19 @@ scaffold_test_project() {
local version=$2
local plugin=${3:-""}

testdata_dir=$(pwd)/testdata
local testdata_dir=$(pwd)/testdata
mkdir -p ./testdata/$project
rm -rf ./testdata/$project/*
pushd .
cd testdata/$project
kb=$testdata_dir/../bin/kubebuilder
local kb=$testdata_dir/../bin/kubebuilder
# Set "--plugins $plugin" if $plugin is not null.
local plugin_flag="${plugin:+--plugins $plugin}"

# Remove tool binaries so the correct versions are used for each plugin version.
rm -f "$(which controller-gen)"
rm -f "$(which kustomize)"

if [ $version == "2" ] || [ $version == "3-alpha" ]; then
if [ $version == "2" ] && [ -n "$plugin_flag" ]; then
echo "--plugins flag may not be set for project versions less than 3"
Expand All @@ -50,8 +51,6 @@ scaffold_test_project() {
header_text "Starting to generate projects with version $version"
header_text "Generating $project"

export GO111MODULE=on
export PATH=$PATH:$(go env GOPATH)/bin
go mod init sigs.k8s.io/kubebuilder/testdata/$project # our repo autodetection will traverse up to the kb module if we don't do this

header_text "initializing $project ..."
Expand Down Expand Up @@ -104,6 +103,9 @@ scaffold_test_project() {

set -e

export GO111MODULE=on
export PATH="$PATH:$(go env GOPATH)/bin"

build_kb
scaffold_test_project project-v2 2
scaffold_test_project project-v2-multigroup 2
Expand Down
4 changes: 0 additions & 4 deletions scripts/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,3 @@ export GO111MODULE=on

# You can use --image flag to specify the cluster version you want, e.g --image=kindest/node:v1.13.6, the supported version are listed at https://hub.docker.com/r/kindest/node/tags
kind create cluster -v 4 --name $KIND_NAME --retain --wait=1m --config test/kind-config.yaml --image=kindest/node:$K8S_VERSION

# setup the go modules required
go get sigs.k8s.io/controller-tools/cmd/[email protected]
go get sigs.k8s.io/kustomize/kustomize/[email protected]
18 changes: 10 additions & 8 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ EOF


function test_project {
project_dir=$1
version=$2
local project_dir=$1
local version=$2
rm -f "$(which controller-gen)"
rm -f "$(which kustomize)"
header_text "performing tests in dir $project_dir for project version v$version"
cd testdata/$project_dir
make all test
Expand Down Expand Up @@ -143,13 +145,13 @@ export GO111MODULE=on
go test -race -v ./cmd/... ./pkg/... ./plugins/...

# test project v2
GO111MODULE=on test_project project-v2 2
GO111MODULE=on test_project project-v2-multigroup 2
GO111MODULE=on test_project project-v2-addon 2
test_project project-v2 2
test_project project-v2-multigroup 2
test_project project-v2-addon 2

# test project v3
GO111MODULE=on test_project project-v3 3-alpha
GO111MODULE=on test_project project-v3-multigroup 3-alpha
GO111MODULE=on test_project project-v3-addon 3-alpha
test_project project-v3 3-alpha
test_project project-v3-multigroup 3-alpha
test_project project-v3-addon 3-alpha

exit $rc
12 changes: 11 additions & 1 deletion test/e2e/utils/test_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,18 @@ func NewTestContext(binaryName string, env ...string) (*TestContext, error) {
}, nil
}

// Prepare prepare a work directory for testing
// Prepare prepares the test environment.
func (t *TestContext) Prepare() error {
// Remove tools used by projects in the environment so the correct version is downloaded for each test.
fmt.Fprintf(GinkgoWriter, "cleaning up tools")
for _, toolName := range []string{"controller-gen", "kustomize"} {
if toolPath, err := exec.LookPath(toolName); err == nil {
if err := os.RemoveAll(toolPath); err != nil {
return err
}
}
}

fmt.Fprintf(GinkgoWriter, "preparing testing directory: %s\n", t.Dir)
return os.MkdirAll(t.Dir, 0755)
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
)

var _ = Describe("kubebuilder", func() {
Context("with v2 scaffolding", func() {
Context("with project version 2 scaffolding", func() {
var kbc *utils.TestContext
BeforeEach(func() {
var err error
Expand Down
Loading

0 comments on commit 0cebbd6

Please sign in to comment.