1 /* 2 Copyright 2021 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package komega 18 19 import ( 20 "context" 21 22 "sigs.k8s.io/controller-runtime/pkg/client" 23 ) 24 25 // Komega is a collection of utilites for writing tests involving a mocked 26 // Kubernetes API. 27 type Komega interface { 28 // Get returns a function that fetches a resource and returns the occurring error. 29 // It can be used with gomega.Eventually() like this 30 // deployment := appsv1.Deployment{ ... } 31 // gomega.Eventually(k.Get(&deployment)).To(gomega.Succeed()) 32 // By calling the returned function directly it can also be used with gomega.Expect(k.Get(...)()).To(...) 33 Get(client.Object) func() error 34 35 // List returns a function that lists resources and returns the occurring error. 36 // It can be used with gomega.Eventually() like this 37 // deployments := v1.DeploymentList{ ... } 38 // gomega.Eventually(k.List(&deployments)).To(gomega.Succeed()) 39 // By calling the returned function directly it can also be used as gomega.Expect(k.List(...)()).To(...) 40 List(client.ObjectList, ...client.ListOption) func() error 41 42 // Update returns a function that fetches a resource, applies the provided update function and then updates the resource. 43 // It can be used with gomega.Eventually() like this: 44 // deployment := appsv1.Deployment{ ... } 45 // gomega.Eventually(k.Update(&deployment, func() { 46 // deployment.Spec.Replicas = 3 47 // })).To(gomega.Succeed()) 48 // By calling the returned function directly it can also be used as gomega.Expect(k.Update(...)()).To(...) 49 Update(client.Object, func(), ...client.UpdateOption) func() error 50 51 // UpdateStatus returns a function that fetches a resource, applies the provided update function and then updates the resource's status. 52 // It can be used with gomega.Eventually() like this: 53 // deployment := appsv1.Deployment{ ... } 54 // gomega.Eventually(k.Update(&deployment, func() { 55 // deployment.Status.AvailableReplicas = 1 56 // })).To(gomega.Succeed()) 57 // By calling the returned function directly it can also be used as gomega.Expect(k.UpdateStatus(...)()).To(...) 58 UpdateStatus(client.Object, func(), ...client.SubResourceUpdateOption) func() error 59 60 // Object returns a function that fetches a resource and returns the object. 61 // It can be used with gomega.Eventually() like this: 62 // deployment := appsv1.Deployment{ ... } 63 // gomega.Eventually(k.Object(&deployment)).To(HaveField("Spec.Replicas", gomega.Equal(ptr.To(int32(3))))) 64 // By calling the returned function directly it can also be used as gomega.Expect(k.Object(...)()).To(...) 65 Object(client.Object) func() (client.Object, error) 66 67 // ObjectList returns a function that fetches a resource and returns the object. 68 // It can be used with gomega.Eventually() like this: 69 // deployments := appsv1.DeploymentList{ ... } 70 // gomega.Eventually(k.ObjectList(&deployments)).To(HaveField("Items", HaveLen(1))) 71 // By calling the returned function directly it can also be used as gomega.Expect(k.ObjectList(...)()).To(...) 72 ObjectList(client.ObjectList, ...client.ListOption) func() (client.ObjectList, error) 73 74 // WithContext returns a copy that uses the given context. 75 WithContext(context.Context) Komega 76 } 77