generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 101
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
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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. | ||
```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
|
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,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"}}}, | ||
}, | ||
}, | ||
} | ||
} |
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,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)) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
module
->modules
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed