|
| 1 | +/* |
| 2 | +Copyright 2016 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package framework |
| 18 | + |
| 19 | +import ( |
| 20 | + "flag" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + |
| 24 | + "k8s.io/client-go/tools/clientcmd" |
| 25 | +) |
| 26 | + |
| 27 | +const ( |
| 28 | + // TODO(gry): make sure the default port, do we need it? |
| 29 | + defaultHost = "https://127.0.0.1:6443" |
| 30 | + |
| 31 | + // DefaultNumNodes is the number of nodes. If not specified, then number of nodes is auto-detected |
| 32 | + DefaultNumNodes = -1 |
| 33 | +) |
| 34 | + |
| 35 | +// TestContextType contains test settings and global state. Due to |
| 36 | +// historic reasons, it is a mixture of items managed by the test |
| 37 | +// framework itself, cloud providers and individual tests. |
| 38 | +// The goal is to move anything not required by the framework |
| 39 | +// into the code which uses the settings. |
| 40 | +// |
| 41 | +// The recommendation for those settings is: |
| 42 | +// - They are stored in their own context structure or local |
| 43 | +// variables. |
| 44 | +// - The standard `flag` package is used to register them. |
| 45 | +// The flag name should follow the pattern <part1>.<part2>....<partn> |
| 46 | +// where the prefix is unlikely to conflict with other tests or |
| 47 | +// standard packages and each part is in lower camel case. For |
| 48 | +// example, test/e2e/storage/csi/context.go could define |
| 49 | +// storage.csi.numIterations. |
| 50 | +// - framework/config can be used to simplify the registration of |
| 51 | +// multiple options with a single function call: |
| 52 | +// var storageCSI { |
| 53 | +// NumIterations `default:"1" usage:"number of iterations"` |
| 54 | +// } |
| 55 | +// _ config.AddOptions(&storageCSI, "storage.csi") |
| 56 | +// - The direct use Viper in tests is possible, but discouraged because |
| 57 | +// it only works in test suites which use Viper (which is not |
| 58 | +// required) and the supported options cannot be |
| 59 | +// discovered by a test suite user. |
| 60 | +// |
| 61 | +// Test suite authors can use framework/viper to make all command line |
| 62 | +// parameters also configurable via a configuration file. |
| 63 | +type TestContextType struct { |
| 64 | + KubeConfig string |
| 65 | + KubeContext string |
| 66 | + Host string |
| 67 | + |
| 68 | + OutputDir string |
| 69 | + |
| 70 | + // If set to true test will dump data about the namespace in which test was running. |
| 71 | + DumpLogsOnFailure bool |
| 72 | + // Disables dumping cluster log from master and nodes after all tests. |
| 73 | + DisableLogDump bool |
| 74 | + TimeoutSeconds int |
| 75 | +} |
| 76 | + |
| 77 | +// TestContext should be used by all tests to access common context data. |
| 78 | +var TestContext TestContextType |
| 79 | + |
| 80 | +// RegisterCommonFlags registers flags common to all e2e test suites. |
| 81 | +// The flag set can be flag.CommandLine (if desired) or a custom |
| 82 | +// flag set that then gets passed to viperconfig.ViperizeFlags. |
| 83 | +// |
| 84 | +// The other Register*Flags methods below can be used to add more |
| 85 | +// test-specific flags. However, those settings then get added |
| 86 | +// regardless whether the test is actually in the test suite. |
| 87 | +// |
| 88 | +// For tests that have been converted to registering their |
| 89 | +// options themselves, copy flags from test/e2e/framework/config |
| 90 | +// as shown in HandleFlags. |
| 91 | +func RegisterCommonFlags(flags *flag.FlagSet) { |
| 92 | + flags.StringVar(&TestContext.KubeConfig, clientcmd.RecommendedConfigPathFlag, os.Getenv(clientcmd.RecommendedConfigPathEnvVar), "Path to kubeconfig containing embedded authinfo.") |
| 93 | + flags.StringVar(&TestContext.KubeContext, clientcmd.FlagContext, "", "kubeconfig context to use/override. If unset, will use value from 'current-context'") |
| 94 | + 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)) |
| 95 | + flags.StringVar(&TestContext.OutputDir, "e2e-output-dir", "/tmp", "Output directory for interesting/useful test data, like performance data, benchmarks, and other metrics.") |
| 96 | + 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.") |
| 97 | + flags.BoolVar(&TestContext.DisableLogDump, "disable-log-dump", false, "If set to true, logs from master and nodes won't be gathered after test run.") |
| 98 | + flag.IntVar(&TestContext.TimeoutSeconds, "pod-wait-timeout", 100, "Timeout to wait for a pod to be ready.") |
| 99 | +} |
0 commit comments