From e6dd46513dc51c1adcd4c6224f73208e6e7c7c0b Mon Sep 17 00:00:00 2001 From: hustjieke Date: Thu, 23 Sep 2021 10:36:56 +0800 Subject: [PATCH] test: e2e prototype framework stage 1 #199 [summary] 1. "make e2e-local" without any other env. 2. here we should never import kubernetes directly, otherwise we'll meet "unknown revision v0.0.0" when use go mod tidy. see: https://github.com/kubernetes/kubernetes/issues/79384 3. the prototype framework is extracted from kubernetes, for more details, see: https://github.com/kubernetes/kubernetes/tree/master/test/e2e 4. new file: test/e2e/e2e.go test/e2e/framework/test_context.go [test] test/e2e/e2e_test.go --- Makefile | 6 +- go.mod | 10 ++- go.sum | 5 -- test/e2e/e2e.go | 25 ++++++++ test/e2e/e2e_test.go | 56 +++++++++++++++++ test/e2e/framework/test_context.go | 99 ++++++++++++++++++++++++++++++ 6 files changed, 193 insertions(+), 8 deletions(-) create mode 100644 test/e2e/e2e.go create mode 100644 test/e2e/e2e_test.go create mode 100644 test/e2e/framework/test_context.go diff --git a/Makefile b/Makefile index 007e869ee..ef07a0f02 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,3 @@ - # Image URL to use all building/pushing image targets IMG ?= controller:latest SIDECAR_IMG ?= sidecar:latest @@ -113,3 +112,8 @@ GOBIN=$(PROJECT_DIR)/bin go get $(2) ;\ rm -rf $$TMP_DIR ;\ } endef + +# E2E tests +########### +e2e-local: + go test -v ./test/e2e $(G_ARGS) -timeout 20m diff --git a/go.mod b/go.mod index ed8b78d2b..a33d0849a 100644 --- a/go.mod +++ b/go.mod @@ -12,11 +12,17 @@ require ( github.com/onsi/ginkgo v1.16.4 github.com/onsi/gomega v1.13.0 github.com/presslabs/controller-util v0.3.0 - github.com/spf13/cobra v1.1.1 + github.com/spf13/cobra v1.1.3 github.com/stretchr/testify v1.7.0 k8s.io/api v0.21.2 k8s.io/apimachinery v0.21.2 k8s.io/client-go v0.21.2 - k8s.io/klog/v2 v2.8.0 + k8s.io/component-base v0.21.2 + k8s.io/klog/v2 v2.9.0 sigs.k8s.io/controller-runtime v0.9.2 ) + +replace ( + github.com/spf13/cobra => github.com/spf13/cobra v1.1.1 + k8s.io/klog/v2 => k8s.io/klog/v2 v2.8.0 +) diff --git a/go.sum b/go.sum index 981ec73e9..052aad6d4 100644 --- a/go.sum +++ b/go.sum @@ -127,8 +127,6 @@ github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vb github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= -github.com/go-logr/logr v0.1.0/go.mod h1:ixOQHD9gLJUVQQ2ZOR7zLEifBX6tGkNJF4QyIY7sIas= -github.com/go-logr/logr v0.2.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU= github.com/go-logr/logr v0.4.0 h1:K7/B1jt6fIBQVd4Owv2MqGQClcgf0R266+7C/QjRcLc= github.com/go-logr/logr v0.4.0/go.mod h1:z6/tIYblkpsD+a4lm/fGIIU9mZ+XfAiaFtq7xTgseGU= github.com/go-logr/zapr v0.4.0 h1:uc1uML3hRYL9/ZZPdgHS/n8Nzo+eaYL/Efxkkamf7OM= @@ -387,7 +385,6 @@ github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasO github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ= github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk= github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE= -github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ= github.com/spf13/cobra v1.1.1 h1:KfztREH0tPxJJ+geloSLaAkaPkr4ki2Er5quFV1TDo4= github.com/spf13/cobra v1.1.1/go.mod h1:WnodtKOvamDL/PwE2M4iKs8aMDBZ5Q5klgD3qfVJQMI= github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo= @@ -757,8 +754,6 @@ k8s.io/component-base v0.21.2 h1:EsnmFFoJ86cEywC0DoIkAUiEV6fjgauNugiw1lmIjs4= k8s.io/component-base v0.21.2/go.mod h1:9lvmIThzdlrJj5Hp8Z/TOgIkdfsNARQ1pT+3PByuiuc= k8s.io/gengo v0.0.0-20200413195148-3a45101e95ac/go.mod h1:ezvh/TsK7cY6rbqRK0oQQ8IAqLxYwwyPxAX1Pzy0ii0= k8s.io/gengo v0.0.0-20201214224949-b6c5ce23f027/go.mod h1:FiNAH4ZV3gBg2Kwh89tzAEV2be7d5xI0vBa/VySYy3E= -k8s.io/klog/v2 v2.0.0/go.mod h1:PBfzABfn139FHAV07az/IF9Wp1bkk3vpT2XSJ76fSDE= -k8s.io/klog/v2 v2.2.0/go.mod h1:Od+F08eJP+W3HUb4pSrPpgp9DGU4GzlpG/TmITuYh/Y= k8s.io/klog/v2 v2.8.0 h1:Q3gmuM9hKEjefWFFYF0Mat+YyFJvsUyYuwyNNJ5C9Ts= k8s.io/klog/v2 v2.8.0/go.mod h1:hy9LJ/NvuK+iVyP4Ehqva4HxZG/oXyIS3n3Jmire4Ec= k8s.io/kube-openapi v0.0.0-20210305001622-591a79e4bda7 h1:vEx13qjvaZ4yfObSSXW7BrMc/KQBBT/Jyee8XtLf4x0= diff --git a/test/e2e/e2e.go b/test/e2e/e2e.go new file mode 100644 index 000000000..499561265 --- /dev/null +++ b/test/e2e/e2e.go @@ -0,0 +1,25 @@ +/* +Copyright 2015 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 ( + "testing" +) + +func RunE2ETests(t *testing.T) { + // empty now +} diff --git a/test/e2e/e2e_test.go b/test/e2e/e2e_test.go new file mode 100644 index 000000000..46c395234 --- /dev/null +++ b/test/e2e/e2e_test.go @@ -0,0 +1,56 @@ +/* +Copyright 2015 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 ( + "flag" + "fmt" + "math/rand" + "os" + "testing" + "time" + + "k8s.io/component-base/version" + + "github.com/radondb/radondb-mysql-kubernetes/test/e2e/framework" +) + +// handleFlags sets up all flags and parses the command line. +func handleFlags() { + framework.RegisterCommonFlags(flag.CommandLine) + flag.Parse() +} + +func TestMain(m *testing.M) { + var versionFlag bool + flag.CommandLine.BoolVar(&versionFlag, "version", false, "Displays version information.") + + // Register test flags, then parse flags. + handleFlags() + + if versionFlag { + fmt.Printf("%s\n", version.Get()) + os.Exit(0) + } + + rand.Seed(time.Now().UnixNano()) + os.Exit(m.Run()) +} + +func TestE2E(t *testing.T) { + RunE2ETests(t) +} diff --git a/test/e2e/framework/test_context.go b/test/e2e/framework/test_context.go new file mode 100644 index 000000000..8fcec266b --- /dev/null +++ b/test/e2e/framework/test_context.go @@ -0,0 +1,99 @@ +/* +Copyright 2016 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 framework + +import ( + "flag" + "fmt" + "os" + + "k8s.io/client-go/tools/clientcmd" +) + +const ( + // TODO(gry): make sure the default port, do we need it? + defaultHost = "https://127.0.0.1:6443" + + // DefaultNumNodes is the number of nodes. If not specified, then number of nodes is auto-detected + DefaultNumNodes = -1 +) + +// TestContextType contains test settings and global state. Due to +// historic reasons, it is a mixture of items managed by the test +// framework itself, cloud providers and individual tests. +// The goal is to move anything not required by the framework +// into the code which uses the settings. +// +// The recommendation for those settings is: +// - They are stored in their own context structure or local +// variables. +// - The standard `flag` package is used to register them. +// The flag name should follow the pattern ..... +// where the prefix is unlikely to conflict with other tests or +// standard packages and each part is in lower camel case. For +// example, test/e2e/storage/csi/context.go could define +// storage.csi.numIterations. +// - framework/config can be used to simplify the registration of +// multiple options with a single function call: +// var storageCSI { +// NumIterations `default:"1" usage:"number of iterations"` +// } +// _ config.AddOptions(&storageCSI, "storage.csi") +// - The direct use Viper in tests is possible, but discouraged because +// it only works in test suites which use Viper (which is not +// required) and the supported options cannot be +// discovered by a test suite user. +// +// Test suite authors can use framework/viper to make all command line +// parameters also configurable via a configuration file. +type TestContextType struct { + KubeConfig string + KubeContext string + Host string + + OutputDir string + + // If set to true test will dump data about the namespace in which test was running. + DumpLogsOnFailure bool + // Disables dumping cluster log from master and nodes after all tests. + DisableLogDump bool + TimeoutSeconds int +} + +// TestContext should be used by all tests to access common context data. +var TestContext TestContextType + +// RegisterCommonFlags registers flags common to all e2e test suites. +// The flag set can be flag.CommandLine (if desired) or a custom +// flag set that then gets passed to viperconfig.ViperizeFlags. +// +// The other Register*Flags methods below can be used to add more +// test-specific flags. However, those settings then get added +// regardless whether the test is actually in the test suite. +// +// For tests that have been converted to registering their +// options themselves, copy flags from test/e2e/framework/config +// as shown in HandleFlags. +func RegisterCommonFlags(flags *flag.FlagSet) { + flags.StringVar(&TestContext.KubeConfig, clientcmd.RecommendedConfigPathFlag, os.Getenv(clientcmd.RecommendedConfigPathEnvVar), "Path to kubeconfig containing embedded authinfo.") + flags.StringVar(&TestContext.KubeContext, clientcmd.FlagContext, "", "kubeconfig context to use/override. If unset, will use value from 'current-context'") + flags.StringVar(&TestContext.Host, "host", "", fmt.Sprintf("The host, or apiserver, to connect to. Will default to %s if this argument and --kubeconfig are not set.", defaultHost)) + flags.StringVar(&TestContext.OutputDir, "e2e-output-dir", "/tmp", "Output directory for interesting/useful test data, like performance data, benchmarks, and other metrics.") + flags.BoolVar(&TestContext.DumpLogsOnFailure, "dump-logs-on-failure", true, "If set to true test will dump data about the namespace in which test was running.") + flags.BoolVar(&TestContext.DisableLogDump, "disable-log-dump", false, "If set to true, logs from master and nodes won't be gathered after test run.") + flag.IntVar(&TestContext.TimeoutSeconds, "pod-wait-timeout", 100, "Timeout to wait for a pod to be ready.") +}