From 778b0c0057a19b51eb2ef1ad07c034b8c767ed31 Mon Sep 17 00:00:00 2001 From: Bowei Du Date: Tue, 5 Jun 2018 14:22:47 -0700 Subject: [PATCH] Add build for the e2e tests To run the e2e tests, RUN_INGRESS_E2E must be set. Otherwise, the tests will be skipped. --- Dockerfile.e2e-test | 18 ++++++++ Makefile | 2 +- build/build.sh | 12 ++++-- build/rules.mk | 8 ++-- cmd/e2e-test/main_test.go | 84 ++++++++++++++++++++++++++++++++++++ cmd/e2e-test/nothing_test.go | 24 +++++++++++ cmd/e2e-test/placeholder.go | 20 +++++++++ pkg/e2e/doc.go | 19 ++++++++ pkg/e2e/framework.go | 46 ++++++++++++++++++++ 9 files changed, 224 insertions(+), 9 deletions(-) create mode 100644 Dockerfile.e2e-test create mode 100644 cmd/e2e-test/main_test.go create mode 100644 cmd/e2e-test/nothing_test.go create mode 100644 cmd/e2e-test/placeholder.go create mode 100644 pkg/e2e/doc.go create mode 100644 pkg/e2e/framework.go diff --git a/Dockerfile.e2e-test b/Dockerfile.e2e-test new file mode 100644 index 0000000000..c407bb3013 --- /dev/null +++ b/Dockerfile.e2e-test @@ -0,0 +1,18 @@ +# Copyright 2018 The Kubernetes Authors. All rights reserved. +# +# 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. + +FROM debian:9 + +ADD bin/ARG_ARCH/ARG_BIN /ARG_BIN +ENTRYPOINT ["/ARG_BIN"] diff --git a/Makefile b/Makefile index 16ca47a04e..987a1eb140 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,7 @@ PKG := k8s.io/ingress-gce # List of binaries to build. You must have a matching Dockerfile.BINARY # for each BINARY. -CONTAINER_BINARIES := glbc +CONTAINER_BINARIES := glbc e2e-test # Latest commit hash for current branch. GIT_COMMIT := $(shell git rev-parse HEAD) diff --git a/build/build.sh b/build/build.sh index 89a3782aca..520dc3866a 100755 --- a/build/build.sh +++ b/build/build.sh @@ -42,7 +42,11 @@ if [ $GOARCH == "amd64" ]; then export GOBIN="$GOPATH/bin/linux_amd64" fi -go install \ - -installsuffix "static" \ - -ldflags "-X ${PKG}/pkg/version.Version=${VERSION} -X ${PKG}/pkg/version.GitCommit=${GIT_COMMIT}" \ - ./... +if echo "${TARGET}" | grep '.*-test$'; then + go test -c -o "${TARGET}" "$PKG/cmd/$(basename ${TARGET})" +else + go install \ + -installsuffix "static" \ + -ldflags "-X ${PKG}/pkg/version.Version=${VERSION} -X ${PKG}/pkg/version.GitCommit=${GIT_COMMIT}" \ + ./... +fi diff --git a/build/rules.mk b/build/rules.mk index a6ea7553e9..4a65916d18 100644 --- a/build/rules.mk +++ b/build/rules.mk @@ -74,7 +74,7 @@ ifeq ($(VERBOSE), 1) VERBOSE_OUTPUT := >&1 else DOCKER_BUILD_FLAGS := -q - VERBOSE_OUTPUT := >/dev/null + VERBOSE_OUTPUT := >/dev/null 2>/dev/null endif # This MUST appear as the first rule in a Makefile @@ -106,7 +106,7 @@ build: $(GO_BINARIES) images-build # Rule for all bin/$(ARCH)/bin/$(BINARY) $(GO_BINARIES): build-dirs @echo "building : $@" - @docker pull $(BUILD_IMAGE) + @docker pull $(BUILD_IMAGE) $(VERBOSE_OUTPUT) @docker run \ --rm \ --sig-proxy=true \ @@ -122,11 +122,11 @@ $(GO_BINARIES): build-dirs ARCH=$(ARCH) \ VERSION=$(VERSION) \ PKG=$(PKG) \ - GIT_COMMIT=$(GIT_COMMIT) \ + TARGET=$@ \ + GIT_COMMIT=$(GIT_COMMIT) \ ./build/build.sh \ " - # Rules for dockerfiles. define DOCKERFILE_RULE .$(BINARY)-$(ARCH)-dockerfile: Dockerfile.$(BINARY) diff --git a/cmd/e2e-test/main_test.go b/cmd/e2e-test/main_test.go new file mode 100644 index 0000000000..c2dadc743a --- /dev/null +++ b/cmd/e2e-test/main_test.go @@ -0,0 +1,84 @@ +/* +Copyright 2018 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 main + +import ( + "flag" + "fmt" + "os" + "path/filepath" + "testing" + + "github.com/golang/glog" + "k8s.io/client-go/rest" + "k8s.io/client-go/tools/clientcmd" + "k8s.io/ingress-gce/pkg/e2e" + // Pull in the auth library for GCP. + _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" +) + +var ( + flags struct { + inCluster bool + kubeconfig string + } + + Framework *e2e.Framework +) + +func init() { + home := os.Getenv("HOME") + if home != "" { + flag.StringVar(&flags.kubeconfig, "kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file") + } else { + flag.StringVar(&flags.kubeconfig, "kubeconfig", "", "absolute path to the kubeconfig file") + } + flag.BoolVar(&flags.inCluster, "inCluster", false, "set to true if running in the cluster") +} + +// TestMain is the entrypoint for the end-to-end test suite. This is where +// global resource setup should be done. +func TestMain(m *testing.M) { + flag.Parse() + + if os.Getenv("RUN_INGRESS_E2E") != "true" { + fmt.Fprintln(os.Stderr, "You must set the RUN_INGRESS_E2E environment variable to 'true' run the tests.") + return + } + + var err error + var kubeconfig *rest.Config + + if flags.inCluster { + kubeconfig, err = rest.InClusterConfig() + if err != nil { + glog.Fatalf("Error creating InClusterConfig(): %v", err) + } + } else { + kubeconfig, err = clientcmd.BuildConfigFromFlags("", flags.kubeconfig) + if err != nil { + glog.Fatalf("Error creating kubernetes clientset from %q: %v", flags.kubeconfig, err) + } + } + + Framework = e2e.NewFramework(kubeconfig) + if err := Framework.SanityCheck(); err != nil { + glog.Fatalf("Framework sanity check failed: %v", err) + } + + os.Exit(m.Run()) +} diff --git a/cmd/e2e-test/nothing_test.go b/cmd/e2e-test/nothing_test.go new file mode 100644 index 0000000000..f5af0a4a1f --- /dev/null +++ b/cmd/e2e-test/nothing_test.go @@ -0,0 +1,24 @@ +/* +Copyright 2018 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 main + +import ( + "testing" +) + +func TestNothing(t *testing.T) { +} diff --git a/cmd/e2e-test/placeholder.go b/cmd/e2e-test/placeholder.go new file mode 100644 index 0000000000..66d1132331 --- /dev/null +++ b/cmd/e2e-test/placeholder.go @@ -0,0 +1,20 @@ +/* +Copyright 2018 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 main + +// This is a placeholder file to keep the Golang build happy. +func main() {} diff --git a/pkg/e2e/doc.go b/pkg/e2e/doc.go new file mode 100644 index 0000000000..bbd6d6f375 --- /dev/null +++ b/pkg/e2e/doc.go @@ -0,0 +1,19 @@ +/* +Copyright 2018 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 e2e contains supporting infrastructure for end-to-end integration +// testing driven by the tests in cmd/e2e-test. +package e2e diff --git a/pkg/e2e/framework.go b/pkg/e2e/framework.go new file mode 100644 index 0000000000..80655b9cb9 --- /dev/null +++ b/pkg/e2e/framework.go @@ -0,0 +1,46 @@ +/* +Copyright 2018 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 e2e + +import ( + "github.com/golang/glog" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/kubernetes" + "k8s.io/client-go/rest" +) + +// NewFramework returns a new test framework to run. +func NewFramework(config *rest.Config) *Framework { + return &Framework{ + Clientset: kubernetes.NewForConfigOrDie(config), + } +} + +// Framework is the ent-to-end test framework. +type Framework struct { + Clientset *kubernetes.Clientset +} + +// SanityCheck the test environment before proceeding. +func (f *Framework) SanityCheck() error { + if _, err := f.Clientset.Core().Pods("default").List(metav1.ListOptions{}); err != nil { + glog.Errorf("Error talking to the Kubernetes master: %v", err) + return err + } + // TODO: check connectivity etc. + return nil +}