From 2d86c18c71591f2b20610c1347289474c83f25e5 Mon Sep 17 00:00:00 2001 From: Matteo Ruina Date: Mon, 13 Feb 2023 15:25:27 +0100 Subject: [PATCH] Add documentation about passing custom flags --- examples/custom_flags/README.md | 29 +++++++++++ examples/custom_flags/custom_flags_test.go | 56 ++++++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 examples/custom_flags/README.md create mode 100644 examples/custom_flags/custom_flags_test.go diff --git a/examples/custom_flags/README.md b/examples/custom_flags/README.md new file mode 100644 index 00000000..692319ba --- /dev/null +++ b/examples/custom_flags/README.md @@ -0,0 +1,29 @@ +# Using custom flags + +You can pass additional custom flags to the CLI +using the `flag` package and defining the custom flags before calling `envconf.NewFromFlags()`. + +For example: + +```console +❯ go test -v ./... -args --my-custom-flag hello +=== RUN TestWithCustomFlag +=== RUN TestWithCustomFlag/feature +=== RUN TestWithCustomFlag/feature/custom_flag + custom_flags_test.go:52: Custom flag my-custom-flag: hello +--- PASS: TestWithCustomFlag (0.00s) + --- PASS: TestWithCustomFlag/feature (0.00s) + --- PASS: TestWithCustomFlag/feature/custom_flag (0.00s) +PASS +ok sigs.k8s.io/e2e-framework/examples/custom_flags 0.491s +``` + +or by compiling the test code + +```console +❯ go test -c -o custom_flags.test . +❯ ./custom_flags.test --help 2>&1 | grep -A2 my-custom + -my-custom-flag string + my custom flag for my tests + -namespace string +``` diff --git a/examples/custom_flags/custom_flags_test.go b/examples/custom_flags/custom_flags_test.go new file mode 100644 index 00000000..4fd62d20 --- /dev/null +++ b/examples/custom_flags/custom_flags_test.go @@ -0,0 +1,56 @@ +/* +Copyright 2021 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 custom_flags + +import ( + "context" + "flag" + "os" + "testing" + + log "k8s.io/klog/v2" + + "sigs.k8s.io/e2e-framework/pkg/env" + "sigs.k8s.io/e2e-framework/pkg/envconf" + "sigs.k8s.io/e2e-framework/pkg/features" +) + +var ( + test env.Environment + myCustomFlag string +) + +func TestMain(m *testing.M) { + flag.StringVar(&myCustomFlag, "my-custom-flag", "", "my custom flag for my tests") + // create config from flags (always in TestMain because it calls + // flag.Parse()) + cfg, err := envconf.NewFromFlags() + if err != nil { + log.Fatalf("failed to build envconf from flags: %s", err) + } + test = env.NewWithConfig(cfg) + os.Exit(test.Run(m)) +} + +func TestWithCustomFlag(t *testing.T) { + f := features.New("feature") + f.Assess("custom flag", func(ctx context.Context, t *testing.T, _ *envconf.Config) context.Context { + t.Logf("Custom flag my-custom-flag: %v", myCustomFlag) + return ctx + }) + test.Test(t, f.Feature()) +}