Skip to content

Commit

Permalink
refactor komega package
Browse files Browse the repository at this point in the history
rename Matcher to komega since it's not really a matcher
komega.With... methods now return copies
allow to specify a Gomega instance to use
  • Loading branch information
schrej committed Jan 27, 2022
1 parent c472f24 commit ec47819
Show file tree
Hide file tree
Showing 6 changed files with 379 additions and 266 deletions.
55 changes: 55 additions & 0 deletions pkg/envtest/komega/default.go
@@ -0,0 +1,55 @@
package komega

import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

// defaultK is the Komega used by the package global functions.
var defaultK = &komega{}

// SetDefaultClient sets the client used by the package global functions.
func SetDefaultClient(c client.Client) {
defaultK = &komega{client: c}
}

func checkDefaultClient() {
if defaultK.client == nil {
panic("Default Komega's client is not set. Use WithDefaultClient to set it.")
}
}

// Get fetches an object until the forwarded error matches.
func Get(obj client.Object) func() error {
checkDefaultClient()
return defaultK.Get(obj)
}

// List fetches a list until the forwarded error matches.
func List(obj client.ObjectList, opts ...client.ListOption) func() error {
checkDefaultClient()
return defaultK.List(obj, opts...)
}

// Update tries to update an object by applying the updateFunc until the forwarded error matches.
func Update(obj client.Object, f UpdateFunc, opts ...client.UpdateOption) func() error {
checkDefaultClient()
return defaultK.Update(obj, f, opts...)
}

// UpdateStatus tries to update an object's status by applying the updateFunc until the forwarded error matches.
func UpdateStatus(obj client.Object, f UpdateFunc, opts ...client.UpdateOption) func() error {
checkDefaultClient()
return defaultK.UpdateStatus(obj, f, opts...)
}

// Object
func Object(obj client.Object) func() (client.Object, error) {
checkDefaultClient()
return defaultK.Object(obj)
}

// ObjectList
func ObjectList(obj client.ObjectList, opts ...client.ListOption) func() (client.ObjectList, error) {
checkDefaultClient()
return defaultK.ObjectList(obj, opts...)
}
75 changes: 50 additions & 25 deletions pkg/envtest/komega/interfaces.go
Expand Up @@ -18,39 +18,64 @@ package komega

import (
"context"
"time"

"github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// Komega is the root interface that the Matcher implements.
type Komega interface {
KomegaAsync
KomegaSync
WithContext(context.Context) Komega
}
// Get returns a function that fetches a resource and returns the occuring 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

// KomegaSync is the interface for any sync assertions that
// the matcher implements.
type KomegaSync interface {
Create(client.Object, ...client.CreateOption) gomega.GomegaAssertion
Delete(client.Object, ...client.DeleteOption) gomega.GomegaAssertion
WithExtras(...interface{}) KomegaSync
}
// List returns a function that lists resources and returns the occuring 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, UpdateFunc, ...client.UpdateOption) func() error

// KomegaAsync is the interface for any async assertions that
// the matcher implements.
type KomegaAsync interface {
Consistently(runtime.Object, ...client.ListOption) gomega.AsyncAssertion
Eventually(runtime.Object, ...client.ListOption) gomega.AsyncAssertion
Get(client.Object) gomega.AsyncAssertion
List(client.ObjectList, ...client.ListOption) gomega.AsyncAssertion
Update(client.Object, UpdateFunc, ...client.UpdateOption) gomega.AsyncAssertion
UpdateStatus(client.Object, UpdateFunc, ...client.UpdateOption) gomega.AsyncAssertion
WithTimeout(time.Duration) KomegaAsync
WithPollInterval(time.Duration) KomegaAsync
// 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, UpdateFunc, ...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)

// WithClient returns a copy that uses the given client.
WithClient(client.Client) Komega
// WithContext returns a copy that uses the given context.
WithContext(context.Context) Komega
}

// UpdateFunc modifies the object fetched from the API server before sending
Expand Down
127 changes: 127 additions & 0 deletions pkg/envtest/komega/komega.go
@@ -0,0 +1,127 @@
/*
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"

"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// komega is a collection of
type komega struct {
ctx context.Context
client client.Client
}

var _ Komega = &komega{}

// New creates a new Komega instance with the given client.
func New(c client.Client) Komega {
return &komega{
client: c,
}
}

// WithContext returns a copy that uses the given context.
func (k komega) WithContext(ctx context.Context) Komega {
k.ctx = ctx
return &k
}

// WithClient returns a copy that uses the given client.
func (k komega) WithClient(c client.Client) Komega {
k.client = c
return &k
}

// context returns the matcher context if one has been set or context.Background() otherwise.
func (k *komega) context() context.Context {
if k.ctx == nil {
return context.Background()
}
return k.ctx
}

// Get returns a function that fetches a resource and returns the occuring error.
func (k *komega) Get(obj client.Object) func() error {
key := types.NamespacedName{
Name: obj.GetName(),
Namespace: obj.GetNamespace(),
}
return func() error {
return k.client.Get(k.context(), key, obj)
}
}

// List returns a function that lists resources and returns the occuring error.
func (k *komega) List(obj client.ObjectList, opts ...client.ListOption) func() error {
return func() error {
return k.client.List(k.context(), obj, opts...)
}
}

// Update returns a function that fetches a resource, applies the provided update function and then updates the resource.
func (k *komega) Update(obj client.Object, updateFunc UpdateFunc, opts ...client.UpdateOption) func() error {
key := types.NamespacedName{
Name: obj.GetName(),
Namespace: obj.GetNamespace(),
}
return func() error {
err := k.client.Get(k.context(), key, obj)
if err != nil {
return err
}
return k.client.Update(k.context(), updateFunc(obj), opts...)
}
}

// UpdateStatus returns a function that fetches a resource, applies the provided update function and then updates the resource's status.
func (k *komega) UpdateStatus(obj client.Object, updateFunc UpdateFunc, opts ...client.UpdateOption) func() error {
key := types.NamespacedName{
Name: obj.GetName(),
Namespace: obj.GetNamespace(),
}
return func() error {
err := k.client.Get(k.context(), key, obj)
if err != nil {
return err
}
return k.client.Status().Update(k.context(), updateFunc(obj), opts...)
}
}

// Object returns a function that fetches a resource and returns the object.
func (k *komega) Object(obj client.Object) func() (client.Object, error) {
key := types.NamespacedName{
Name: obj.GetName(),
Namespace: obj.GetNamespace(),
}
return func() (client.Object, error) {
err := k.client.Get(k.context(), key, obj)
return obj, err
}
}

// ObjectList returns a function that fetches a resource and returns the object.
func (k *komega) ObjectList(obj client.ObjectList, opts ...client.ListOption) func() (client.ObjectList, error) {
return func() (client.ObjectList, error) {
err := k.client.List(k.context(), obj, opts...)
return obj, err
}
}

0 comments on commit ec47819

Please sign in to comment.