From 5d4729346b272b2cae25a9a4a24c4e5e520e845d 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 | 1 + test/e2e/e2e.go | 25 ++++++++ test/e2e/e2e_test.go | 56 +++++++++++++++++ test/e2e/framework/test_context.go | 99 ++++++++++++++++++++++++++++++ 5 files changed, 186 insertions(+), 1 deletion(-) 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 007e869e..ef07a0f0 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 ed8b78d2..36347042 100644 --- a/go.mod +++ b/go.mod @@ -17,6 +17,7 @@ require ( k8s.io/api v0.21.2 k8s.io/apimachinery v0.21.2 k8s.io/client-go v0.21.2 + k8s.io/component-base v0.21.2 k8s.io/klog/v2 v2.8.0 sigs.k8s.io/controller-runtime v0.9.2 ) diff --git a/test/e2e/e2e.go b/test/e2e/e2e.go new file mode 100644 index 00000000..49956126 --- /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 00000000..46c39523 --- /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 00000000..8fcec266 --- /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.") +}