Skip to content

Commit

Permalink
WIP - e2e: distinct help messages with and without extended options
Browse files Browse the repository at this point in the history
Flags defined via the framework/config package are considered less
important and only shown when explicitly requested via --full-help.

WIP! This commit merely shows how this could be done. For it to be
useful, all non-essential flags would have to be defined via
framework/config.

While at it, this commit also fixes a small usability issue:
previously the e2e.test silently ignored additional parameters.
  • Loading branch information
pohly committed Sep 12, 2018
1 parent 88c2208 commit d994c26
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 5 deletions.
1 change: 1 addition & 0 deletions test/e2e/framework/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ go_library(
"//staging/src/k8s.io/client-go/util/retry:go_default_library",
"//staging/src/k8s.io/csi-api/pkg/client/clientset/versioned:go_default_library",
"//staging/src/k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset:go_default_library",
"//test/e2e/framework/config:go_default_library",
"//test/e2e/framework/ginkgowrapper:go_default_library",
"//test/e2e/framework/metrics:go_default_library",
"//test/e2e/manifest:go_default_library",
Expand Down
9 changes: 5 additions & 4 deletions test/e2e/framework/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ import (
"unicode/utf8"
)

// CommandLine is the flag set that AddOptions adds to. Usually this
// is the same as the default in the flag package, but can also be
// something else (for example during testing).
var CommandLine = flag.CommandLine
// CommandLine is the flag set that AddOptions adds to. This
// is distinct from the global default in the flag package, so
// these additional flags can be treated separately from other
// flags if desired.
var CommandLine = flag.NewFlagSet("config", flag.ContinueOnError)

// AddOptions analyzes the options value and creates the necessary
// flags to populate it.
Expand Down
41 changes: 40 additions & 1 deletion test/e2e/framework/test_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
kubeletconfig "k8s.io/kubernetes/pkg/kubelet/apis/config"
e2econfig "k8s.io/kubernetes/test/e2e/framework/config"
)

const defaultHost = "http://127.0.0.1:8080"
Expand Down Expand Up @@ -295,11 +296,49 @@ func RegisterNodeFlags() {
flag.StringVar(&TestContext.SystemSpecName, "system-spec-name", "", "The name of the system spec (e.g., gke) that's used in the node e2e test. The system specs are in test/e2e_node/system/specs/. This is used by the test framework to determine which tests to run for validating the system requirements.")
}

func CopyFlags(from, to *flag.FlagSet) {
from.VisitAll(func(f *flag.Flag) {
to.Var(f.Value, f.Name, f.Usage)
})
}

// HandleFlags sets up all flags and parses the command line.
func HandleFlags() {
RegisterCommonFlags()
RegisterClusterFlags()
flag.Parse()

help := flag.Bool("help", false, "show common flags")
fullHelp := flag.Bool("full-help", false, "show all flags, including the less commonly used ones")
// We copy all flags (normal and config) into a single flag
// set and then parse in one go. The default usage
// instructions only dump the normal flags. If users want to see
// all available options, they can use --full-help or dump
// a sample config file.
allFlags := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
CopyFlags(flag.CommandLine, allFlags)
CopyFlags(e2econfig.CommandLine, allFlags)
err := allFlags.Parse(os.Args[1:])
if err == flag.ErrHelp || *help {
fmt.Fprintf(flag.CommandLine.Output(), "Common flags for %s:\n", os.Args[0])
flag.CommandLine.PrintDefaults()
os.Exit(1)
}
if err != nil {
os.Exit(1)
}
if *fullHelp {
fmt.Fprintf(flag.CommandLine.Output(), "All flags for %s:\n", os.Args[0])
flag.CommandLine.PrintDefaults()
os.Exit(1)
}
if allFlags.NArg() > 0 {
// We have no support for additional parameters. We
// intentionally do not dump valid parameters here
// because the list would be too long, making it
// harder to spot the error message.
fmt.Fprintf(os.Stderr, "Unrecognized parameters for %s, see --full-help: %v\n", os.Args[0], allFlags.Args())
os.Exit(1)
}
}

func createKubeConfig(clientCfg *restclient.Config) *clientcmdapi.Config {
Expand Down

0 comments on commit d994c26

Please sign in to comment.