generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #57 from vladimirvivien/flags-fix
Fixing issues with flags parsing
- Loading branch information
Showing
15 changed files
with
300 additions
and
143 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
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
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,51 @@ | ||
|
||
# CLI flags | ||
|
||
The test harness framework supports several CLI flags that can be used to influence how tests are executed. This example shows how to create tests that are configured using the CLI flags. | ||
|
||
## Configure tests with CLI flags | ||
|
||
To drive your tests with CLI flags, you must initialize a test environment using the passed in CLI flags. This is done by calling `envconf.NewFromFlags` function to create configuration for the environment as shown below: | ||
|
||
```go | ||
var test env.Environment | ||
|
||
func TestMain(m *testing.M) { | ||
cfg, err := envconf.NewFromFlags() | ||
if err != nil { | ||
log.Fatalf("envconf failed: %s", err) | ||
} | ||
test = env.NewWithConfig(cfg) | ||
os.Exit(test.Run(m)) | ||
} | ||
``` | ||
|
||
### Supported flags | ||
|
||
There are several supported flags (for more accurate list, see package `pkg/flag`): | ||
|
||
* `assess` | ||
* `features` | ||
* `labels` | ||
* `kubeconfig` | ||
* `namespace` | ||
|
||
### Running tests with flags | ||
|
||
The tests can be executed using the normal `go test` tools steps. For instance, to pass the flags to your tests, do the followings: | ||
|
||
```shell | ||
go test -v . -args --assess en | ||
``` | ||
|
||
You can also build a test binary, then pass the CLI flags to the binary. First, compile the test binary: | ||
|
||
```shell | ||
go test -c -o flags.test . | ||
``` | ||
|
||
Then execute the test binary passing the CLI arguments: | ||
|
||
```shell | ||
./flags.test --assess en | ||
``` |
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,82 @@ | ||
/* | ||
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 flags | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"os" | ||
"testing" | ||
|
||
"sigs.k8s.io/e2e-framework/pkg/env" | ||
"sigs.k8s.io/e2e-framework/pkg/envconf" | ||
"sigs.k8s.io/e2e-framework/pkg/features" | ||
) | ||
|
||
func greet(key string) string { | ||
salut := map[string]string{ | ||
"en": "Hello", | ||
"fr": "Allo", | ||
"es": "Olá", | ||
} | ||
return salut[key] | ||
} | ||
|
||
var test env.Environment | ||
|
||
// TestMain sets up a test suite and configures the test | ||
// environment using CLI flags. Note that the test.* flags | ||
// along with the framework flags will be available. | ||
// | ||
// Pass flags in couple of ways: | ||
// | ||
// go test -v . -args --assess en | ||
// | ||
// Or, build a test binary first: | ||
// | ||
// go test -c -o flags.test . | ||
// | ||
// Then, execute the test: | ||
// | ||
// ./flags.test --assess en | ||
func TestMain(m *testing.M) { | ||
// 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 TestWithFlags(t *testing.T) { | ||
f := features.New("salutation").WithLabel("type", "lang") | ||
f.Assess("en", func(ctx context.Context, t *testing.T, _ *envconf.Config) context.Context { | ||
if greet("en") != "Hello" { | ||
t.Error("unexpected message: en") | ||
} | ||
return ctx | ||
}) | ||
f.Assess("es", func(ctx context.Context, t *testing.T, _ *envconf.Config) context.Context { | ||
if greet("es") != "Olá" { | ||
t.Error("unexpected message: es") | ||
} | ||
return ctx | ||
}) | ||
|
||
test.Test(t, f.Feature()) | ||
} |
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
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
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
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
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
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
Oops, something went wrong.