Skip to content

Commit

Permalink
Merge pull request #57 from vladimirvivien/flags-fix
Browse files Browse the repository at this point in the history
Fixing issues with flags parsing
  • Loading branch information
k8s-ci-robot authored Oct 8, 2021
2 parents 5ae5375 + 84baa29 commit cd133e2
Show file tree
Hide file tree
Showing 15 changed files with 300 additions and 143 deletions.
8 changes: 6 additions & 2 deletions examples/custom_env_funcs/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ import (
func TestListPods(t *testing.T) {
f := features.New("pod list").
Assess("pods from kube-system", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
client, err := cfg.Client()
if err != nil {
t.Fatal(err)
}
var pods corev1.PodList
err := cfg.Client().Resources("kube-system").List(context.TODO(), &pods)
err = client.Resources("kube-system").List(context.TODO(), &pods)
if err != nil {
t.Fatal(err)
}
Expand All @@ -41,4 +45,4 @@ func TestListPods(t *testing.T) {
})

testenv.Test(t, f.Feature())
}
}
13 changes: 4 additions & 9 deletions examples/custom_env_funcs/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ import (
"sigs.k8s.io/e2e-framework/support/kind"
)

var (
testenv env.Environment
)
var testenv env.Environment

func TestMain(m *testing.M) {
testenv = env.New()
Expand All @@ -46,23 +44,20 @@ func TestMain(m *testing.M) {
time.Sleep(time.Second * 10)

// update environment with kubecofig file
if _, err := cfg.WithKubeconfigFile(kubeconfig); err != nil {
return ctx, err
}

cfg.WithKubeconfigFile(kubeconfig)
// propagate cluster value
return context.WithValue(ctx, 1, cluster), nil
},
).Finish(
// Teardown func: delete kind cluster
func(ctx context.Context, cfg *envconf.Config) (context.Context, error) {
cluster := ctx.Value(1).(*kind.Cluster) // nil should be tested
if err := cluster.Destroy(); err != nil{
if err := cluster.Destroy(); err != nil {
return ctx, err
}
return ctx, nil
},
)

os.Exit(testenv.Run(m))
}
}
51 changes: 51 additions & 0 deletions examples/flags/README.md
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
```
82 changes: 82 additions & 0 deletions examples/flags/flags_test.go
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())
}
25 changes: 21 additions & 4 deletions examples/predefined_env_funcs/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,13 @@ import (
func TestKubernetes(t *testing.T) {
podFeature := features.New("pod list").
Assess("pods from kube-system", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
client, err := cfg.Client()
if err != nil {
t.Fatal(err)
}

var pods corev1.PodList
err := cfg.Client().Resources("kube-system").List(context.TODO(), &pods)
err = client.Resources("kube-system").List(context.TODO(), &pods)
if err != nil {
t.Fatal(err)
}
Expand All @@ -46,17 +51,25 @@ func TestKubernetes(t *testing.T) {
// feature uses pre-generated namespace (see TestMain)
depFeature := features.New("appsv1/deployment").
Setup(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
client, err := cfg.Client()
if err != nil {
t.Fatal(err)
}
// insert a deployment
deployment := newDeployment(cfg.Namespace(), "test-deployment", 1)
if err := cfg.Client().Resources().Create(ctx, deployment); err != nil {
if err := client.Resources().Create(ctx, deployment); err != nil {
t.Fatal(err)
}
time.Sleep(2 * time.Second)
return ctx
}).
Assess("deployment creation", func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
client, err := cfg.Client()
if err != nil {
t.Fatal(err)
}
var dep appsv1.Deployment
if err := cfg.Client().Resources().Get(ctx, "test-deployment", cfg.Namespace(), &dep); err != nil {
if err := client.Resources().Get(ctx, "test-deployment", cfg.Namespace(), &dep); err != nil {
t.Fatal(err)
}
if &dep != nil {
Expand All @@ -65,8 +78,12 @@ func TestKubernetes(t *testing.T) {
return context.WithValue(ctx, "test-deployment", &dep)
}).
Teardown(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
client, err := cfg.Client()
if err != nil {
t.Fatal(err)
}
dep := ctx.Value("test-deployment").(*appsv1.Deployment)
if err := cfg.Client().Resources().Delete(ctx, dep); err != nil {
if err := client.Resources().Delete(ctx, dep); err != nil {
t.Fatal(err)
}
return ctx
Expand Down
4 changes: 1 addition & 3 deletions examples/predefined_env_funcs/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ import (
"sigs.k8s.io/e2e-framework/pkg/envfuncs"
)

var (
testenv env.Environment
)
var testenv env.Environment

func TestMain(m *testing.M) {
testenv = env.New()
Expand Down
4 changes: 1 addition & 3 deletions examples/suites/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ import (
"sigs.k8s.io/e2e-framework/pkg/envconf"
)

var (
testenv env.Environment
)
var testenv env.Environment

func TestMain(m *testing.M) {
var err error
Expand Down
2 changes: 1 addition & 1 deletion klient/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func ResolveKubeConfigFile() string {
// If a flag --kubeconfig is specified with the config location, use that
if flag.Parsed() {
f := flag.Lookup("kubeconfig")
if f != nil {
if f != nil && f.Value.String() != "" {
return f.Value.String()
}
}
Expand Down
4 changes: 2 additions & 2 deletions klient/conf/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func setup() {
_, err := os.Stat(kubeconfigpath)
// create file if not exists
if os.IsNotExist(err) {
err = os.MkdirAll(kubeconfigdir, 0777)
err = os.MkdirAll(kubeconfigdir, 0o777)
if err != nil {
log.Println("failed to create .kube dir", err)
return
Expand Down Expand Up @@ -76,7 +76,7 @@ func setup() {
}

func createFile(path, data string) error {
return ioutil.WriteFile(path, []byte(data), 0644)
return ioutil.WriteFile(path, []byte(data), 0o644)
}

// genKubeconfig used to genearte kube config file
Expand Down
4 changes: 0 additions & 4 deletions pkg/env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ func TestEnv_New(t *testing.T) {
if e.cfg.Namespace() != "" {
t.Error("unexpected envconfig.Namespace value")
}

if e.cfg.Client() != nil {
t.Error("unexpected envconfig.Client")
}
}

func TestEnv_APIMethods(t *testing.T) {
Expand Down
Loading

0 comments on commit cd133e2

Please sign in to comment.