Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example of how to use e2e framework with real cluster #149

Merged
merged 3 commits into from
Jun 29, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions examples/real_cluster/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Real cluster example (including cloud provider)

This package shows how the test framework can be used with a real self-managed or cloud vendor managed cluster (AKS/GKE/EKS), since one may want to launch some tests inside the cluster. This example creates a random namespace, deploys simple deployment and if done removes the namespace.

To properly connect to a cloud provider cluster it is required to import for side effects one of auth module. If testing on self-managed cluster there is no need to include these dependencies.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

module -> modules

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If testing on a self-managed...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

```go
_ "k8s.io/client-go/plugin/pkg/client/auth/azure" // auth for AKS clusters
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" // auth for GKE clusters
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc" // auth for OIDC (EKS)
```

ResolveKubeConfigFile() function is called to get kubeconfig loaded, it uses either `--kubeconfig` flag, `KUBECONFIG` env or by default ` $HOME/.kube/config` path.

```go
func TestMain(m *testing.M) {
testenv = env.New()
path := conf.ResolveKubeConfigFile()
cfg := envconf.NewWithKubeConfig(path)
testenv = env.NewWithConfig(cfg)
```

Later testing is the same as on Kind cluster.

```go
testenv.Setup(
envfuncs.CreateNamespace(namespace),
)
testenv.Finish(
envfuncs.DeleteNamespace(namespace),
)
os.Exit(testenv.Run(m))
```
vladimirvivien marked this conversation as resolved.
Show resolved Hide resolved
77 changes: 77 additions & 0 deletions examples/real_cluster/kind_with_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
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 e2e

import (
"context"
"testing"
"time"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/e2e-framework/pkg/envconf"
"sigs.k8s.io/e2e-framework/pkg/features"
)

func TestRealCluster(t *testing.T) {
deploymentFeature := features.New("appsv1/deployment").
Setup(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
// start a deployment
deployment := newDeployment(cfg.Namespace(), "test-deployment", 1)
if err := cfg.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 {
var dep appsv1.Deployment
if err := cfg.Client().Resources().Get(ctx, "test-deployment", cfg.Namespace(), &dep); err != nil {
t.Fatal(err)
}
if &dep != nil {
t.Logf("deployment found: %s", dep.Name)
}
return context.WithValue(ctx, "test-deployment", &dep)
}).
Teardown(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
dep := ctx.Value("test-deployment").(*appsv1.Deployment)
if err := cfg.Client().Resources().Delete(ctx, dep); err != nil {
t.Fatal(err)
}
return ctx
}).Feature()

testenv.Test(t, deploymentFeature)
}

func newDeployment(namespace string, name string, replicaCount int32) *appsv1.Deployment {
return &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{Name: name, Namespace: namespace, Labels: map[string]string{"app": "test-app"}},
Spec: appsv1.DeploymentSpec{
Replicas: &replicaCount,
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{"app": "test-app"},
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{Labels: map[string]string{"app": "test-app"}},
Spec: corev1.PodSpec{Containers: []corev1.Container{{Name: "nginx", Image: "nginx"}}},
},
},
}
}
47 changes: 47 additions & 0 deletions examples/real_cluster/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
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 e2e

import (
// If testing with a cloud vendor managed cluster uncomment one of the below dependencies to properly get authorised.
//_ "k8s.io/client-go/plugin/pkg/client/auth/azure" // auth for AKS clusters
//_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" // auth for GKE clusters
//_ "k8s.io/client-go/plugin/pkg/client/auth/oidc" // auth for OIDC
"os"
"sigs.k8s.io/e2e-framework/klient/conf"
"sigs.k8s.io/e2e-framework/pkg/env"
"sigs.k8s.io/e2e-framework/pkg/envconf"
"sigs.k8s.io/e2e-framework/pkg/envfuncs"
"testing"
)

var testenv env.Environment

func TestMain(m *testing.M) {
testenv = env.New()
path := conf.ResolveKubeConfigFile()
cfg := envconf.NewWithKubeConfig(path)
testenv = env.NewWithConfig(cfg)
namespace := envconf.RandomName("sample-ns", 16)
testenv.Setup(
envfuncs.CreateNamespace(namespace),
)
testenv.Finish(
envfuncs.DeleteNamespace(namespace),
)
os.Exit(testenv.Run(m))
}