-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add komega package with helpers for writing tests
The helpers can be used to write tests using the gomega library in conjunction with the Kubernetes API, or a mock of it. Co-Authored-By: Joel Speed <joel.speed@hotmail.co.uk>
- Loading branch information
Showing
8 changed files
with
569 additions
and
5 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,14 @@ | ||
approvers: | ||
- controller-runtime-admins | ||
- controller-runtime-maintainers | ||
- controller-runtime-approvers | ||
- schrej | ||
- JoelSpeed | ||
- sbueringer | ||
reviewers: | ||
- controller-runtime-admins | ||
- controller-runtime-reviewers | ||
- controller-runtime-approvers | ||
- schrej | ||
- JoelSpeed | ||
- sbueringer |
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,92 @@ | ||
package komega | ||
|
||
import ( | ||
"context" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// defaultK is the Komega used by the package global functions. | ||
var defaultK = &komega{ctx: context.Background()} | ||
|
||
// SetClient sets the client used by the package global functions. | ||
func SetClient(c client.Client) { | ||
defaultK.client = c | ||
} | ||
|
||
// SetContext sets the context used by the package global functions. | ||
func SetContext(c context.Context) { | ||
defaultK.ctx = c | ||
} | ||
|
||
func checkDefaultClient() { | ||
if defaultK.client == nil { | ||
panic("Default Komega's client is not set. Use SetClient to set it.") | ||
} | ||
} | ||
|
||
// Get returns a function that fetches a resource and returns the occurring error. | ||
// It can be used with gomega.Eventually() like this | ||
// deployment := appsv1.Deployment{ ... } | ||
// gomega.Eventually(komega.Get(&deployment)).To(gomega.Succeed()) | ||
// By calling the returned function directly it can also be used with gomega.Expect(komega.Get(...)()).To(...) | ||
func Get(obj client.Object) func() error { | ||
checkDefaultClient() | ||
return defaultK.Get(obj) | ||
} | ||
|
||
// List returns a function that lists resources and returns the occurring error. | ||
// It can be used with gomega.Eventually() like this | ||
// deployments := v1.DeploymentList{ ... } | ||
// gomega.Eventually(k.List(&deployments)).To(gomega.Succeed()) | ||
// By calling the returned function directly it can also be used as gomega.Expect(k.List(...)()).To(...) | ||
func List(list client.ObjectList, opts ...client.ListOption) func() error { | ||
checkDefaultClient() | ||
return defaultK.List(list, opts...) | ||
} | ||
|
||
// Update returns a function that fetches a resource, applies the provided update function and then updates the resource. | ||
// It can be used with gomega.Eventually() like this: | ||
// deployment := appsv1.Deployment{ ... } | ||
// gomega.Eventually(k.Update(&deployment, func (o client.Object) { | ||
// deployment.Spec.Replicas = 3 | ||
// return &deployment | ||
// })).To(gomega.Scucceed()) | ||
// By calling the returned function directly it can also be used as gomega.Expect(k.Update(...)()).To(...) | ||
func Update(obj client.Object, f func(), opts ...client.UpdateOption) func() error { | ||
checkDefaultClient() | ||
return defaultK.Update(obj, f, opts...) | ||
} | ||
|
||
// UpdateStatus returns a function that fetches a resource, applies the provided update function and then updates the resource's status. | ||
// It can be used with gomega.Eventually() like this: | ||
// deployment := appsv1.Deployment{ ... } | ||
// gomega.Eventually(k.Update(&deployment, func (o client.Object) { | ||
// deployment.Status.AvailableReplicas = 1 | ||
// return &deployment | ||
// })).To(gomega.Scucceed()) | ||
// By calling the returned function directly it can also be used as gomega.Expect(k.UpdateStatus(...)()).To(...) | ||
func UpdateStatus(obj client.Object, f func(), opts ...client.UpdateOption) func() error { | ||
checkDefaultClient() | ||
return defaultK.UpdateStatus(obj, f, opts...) | ||
} | ||
|
||
// Object returns a function that fetches a resource and returns the object. | ||
// It can be used with gomega.Eventually() like this: | ||
// deployment := appsv1.Deployment{ ... } | ||
// gomega.Eventually(k.Object(&deployment)).To(HaveField("Spec.Replicas", gomega.Equal(pointer.Int32(3)))) | ||
// By calling the returned function directly it can also be used as gomega.Expect(k.Object(...)()).To(...) | ||
func Object(obj client.Object) func() (client.Object, error) { | ||
checkDefaultClient() | ||
return defaultK.Object(obj) | ||
} | ||
|
||
// ObjectList returns a function that fetches a resource and returns the object. | ||
// It can be used with gomega.Eventually() like this: | ||
// deployments := appsv1.DeploymentList{ ... } | ||
// gomega.Eventually(k.ObjectList(&deployments)).To(HaveField("Items", HaveLen(1))) | ||
// By calling the returned function directly it can also be used as gomega.Expect(k.ObjectList(...)()).To(...) | ||
func ObjectList(list client.ObjectList, opts ...client.ListOption) func() (client.ObjectList, error) { | ||
checkDefaultClient() | ||
return defaultK.ObjectList(list, opts...) | ||
} |
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,116 @@ | ||
package komega | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/gomega" | ||
appsv1 "k8s.io/api/apps/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/utils/pointer" | ||
) | ||
|
||
func TestDefaultGet(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
fc := createFakeClient() | ||
SetClient(fc) | ||
|
||
fetched := appsv1.Deployment{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "default", | ||
Name: "test", | ||
}, | ||
} | ||
g.Eventually(Get(&fetched)).Should(Succeed()) | ||
|
||
g.Expect(*fetched.Spec.Replicas).To(BeEquivalentTo(5)) | ||
} | ||
|
||
func TestDefaultList(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
fc := createFakeClient() | ||
SetClient(fc) | ||
|
||
list := appsv1.DeploymentList{} | ||
g.Eventually(List(&list)).Should(Succeed()) | ||
|
||
g.Expect(list.Items).To(HaveLen(1)) | ||
depl := exampleDeployment() | ||
g.Expect(list.Items[0]).To(And( | ||
HaveField("ObjectMeta.Name", Equal(depl.ObjectMeta.Name)), | ||
HaveField("ObjectMeta.Namespace", Equal(depl.ObjectMeta.Namespace)), | ||
)) | ||
} | ||
|
||
func TestDefaultUpdate(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
fc := createFakeClient() | ||
SetClient(fc) | ||
|
||
updateDeployment := appsv1.Deployment{ | ||
ObjectMeta: exampleDeployment().ObjectMeta, | ||
} | ||
g.Eventually(Update(&updateDeployment, func() { | ||
updateDeployment.Annotations = map[string]string{"updated": "true"} | ||
})).Should(Succeed()) | ||
|
||
fetched := appsv1.Deployment{ | ||
ObjectMeta: exampleDeployment().ObjectMeta, | ||
} | ||
g.Expect(Object(&fetched)()).To(HaveField("ObjectMeta.Annotations", HaveKeyWithValue("updated", "true"))) | ||
} | ||
|
||
func TestDefaultUpdateStatus(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
fc := createFakeClient() | ||
SetClient(fc) | ||
|
||
updateDeployment := appsv1.Deployment{ | ||
ObjectMeta: exampleDeployment().ObjectMeta, | ||
} | ||
g.Eventually(UpdateStatus(&updateDeployment, func() { | ||
updateDeployment.Status.AvailableReplicas = 1 | ||
})).Should(Succeed()) | ||
|
||
fetched := appsv1.Deployment{ | ||
ObjectMeta: exampleDeployment().ObjectMeta, | ||
} | ||
g.Expect(Object(&fetched)()).To(HaveField("Status.AvailableReplicas", BeEquivalentTo(1))) | ||
} | ||
|
||
func TestDefaultObject(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
fc := createFakeClient() | ||
SetClient(fc) | ||
|
||
fetched := appsv1.Deployment{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "default", | ||
Name: "test", | ||
}, | ||
} | ||
g.Eventually(Object(&fetched)).Should(And( | ||
Not(BeNil()), | ||
HaveField("Spec.Replicas", Equal(pointer.Int32(5))), | ||
)) | ||
} | ||
|
||
func TestDefaultObjectList(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
fc := createFakeClient() | ||
SetClient(fc) | ||
|
||
list := appsv1.DeploymentList{} | ||
g.Eventually(ObjectList(&list)).Should(And( | ||
Not(BeNil()), | ||
HaveField("Items", And( | ||
HaveLen(1), | ||
ContainElement(HaveField("Spec.Replicas", Equal(pointer.Int32(5)))), | ||
)), | ||
)) | ||
} |
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,78 @@ | ||
/* | ||
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 komega | ||
|
||
import ( | ||
"context" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
// Komega is a collection of utilites for writing tests involving a mocked | ||
// Kubernetes API. | ||
type Komega interface { | ||
// Get returns a function that fetches a resource and returns the occurring error. | ||
// It can be used with gomega.Eventually() like this | ||
// deployment := appsv1.Deployment{ ... } | ||
// gomega.Eventually(k.Get(&deployment)).To(gomega.Succeed()) | ||
// By calling the returned function directly it can also be used with gomega.Expect(k.Get(...)()).To(...) | ||
Get(client.Object) func() error | ||
|
||
// List returns a function that lists resources and returns the occurring error. | ||
// It can be used with gomega.Eventually() like this | ||
// deployments := v1.DeploymentList{ ... } | ||
// gomega.Eventually(k.List(&deployments)).To(gomega.Succeed()) | ||
// By calling the returned function directly it can also be used as gomega.Expect(k.List(...)()).To(...) | ||
List(client.ObjectList, ...client.ListOption) func() error | ||
|
||
// Update returns a function that fetches a resource, applies the provided update function and then updates the resource. | ||
// It can be used with gomega.Eventually() like this: | ||
// deployment := appsv1.Deployment{ ... } | ||
// gomega.Eventually(k.Update(&deployment, func (o client.Object) { | ||
// deployment.Spec.Replicas = 3 | ||
// return &deployment | ||
// })).To(gomega.Scucceed()) | ||
// By calling the returned function directly it can also be used as gomega.Expect(k.Update(...)()).To(...) | ||
Update(client.Object, func(), ...client.UpdateOption) func() error | ||
|
||
// UpdateStatus returns a function that fetches a resource, applies the provided update function and then updates the resource's status. | ||
// It can be used with gomega.Eventually() like this: | ||
// deployment := appsv1.Deployment{ ... } | ||
// gomega.Eventually(k.Update(&deployment, func (o client.Object) { | ||
// deployment.Status.AvailableReplicas = 1 | ||
// return &deployment | ||
// })).To(gomega.Scucceed()) | ||
// By calling the returned function directly it can also be used as gomega.Expect(k.UpdateStatus(...)()).To(...) | ||
UpdateStatus(client.Object, func(), ...client.UpdateOption) func() error | ||
|
||
// Object returns a function that fetches a resource and returns the object. | ||
// It can be used with gomega.Eventually() like this: | ||
// deployment := appsv1.Deployment{ ... } | ||
// gomega.Eventually(k.Object(&deployment)).To(HaveField("Spec.Replicas", gomega.Equal(pointer.Int32(3)))) | ||
// By calling the returned function directly it can also be used as gomega.Expect(k.Object(...)()).To(...) | ||
Object(client.Object) func() (client.Object, error) | ||
|
||
// ObjectList returns a function that fetches a resource and returns the object. | ||
// It can be used with gomega.Eventually() like this: | ||
// deployments := appsv1.DeploymentList{ ... } | ||
// gomega.Eventually(k.ObjectList(&deployments)).To(HaveField("Items", HaveLen(1))) | ||
// By calling the returned function directly it can also be used as gomega.Expect(k.ObjectList(...)()).To(...) | ||
ObjectList(client.ObjectList, ...client.ListOption) func() (client.ObjectList, error) | ||
|
||
// WithContext returns a copy that uses the given context. | ||
WithContext(context.Context) Komega | ||
} |
Oops, something went wrong.