generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add documentation about passing custom flags
- Loading branch information
Showing
2 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) | ||
} |