Skip to content

Commit 134e3d3

Browse files
committed
Add Create/Update options to fake client
1 parent 164ec47 commit 134e3d3

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

pkg/client/fake/client.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"strings"
2525

2626
"k8s.io/apimachinery/pkg/api/meta"
27+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2728
"k8s.io/apimachinery/pkg/runtime"
2829
"k8s.io/apimachinery/pkg/runtime/schema"
2930
"k8s.io/client-go/kubernetes/scheme"
@@ -138,7 +139,16 @@ func (c *fakeClient) List(ctx context.Context, obj runtime.Object, opts ...clien
138139
return nil
139140
}
140141

141-
func (c *fakeClient) Create(ctx context.Context, obj runtime.Object) error {
142+
func (c *fakeClient) Create(ctx context.Context, obj runtime.Object, opts ...client.CreateOptionFunc) error {
143+
createOptions := &client.CreateOptions{}
144+
createOptions.ApplyOptions(opts)
145+
146+
for _, dryRunOpt := range createOptions.DryRun {
147+
if dryRunOpt == metav1.DryRunAll {
148+
return nil
149+
}
150+
}
151+
142152
gvr, err := getGVRFromObject(obj, c.scheme)
143153
if err != nil {
144154
return err
@@ -163,7 +173,16 @@ func (c *fakeClient) Delete(ctx context.Context, obj runtime.Object, opts ...cli
163173
return c.tracker.Delete(gvr, accessor.GetNamespace(), accessor.GetName())
164174
}
165175

166-
func (c *fakeClient) Update(ctx context.Context, obj runtime.Object) error {
176+
func (c *fakeClient) Update(ctx context.Context, obj runtime.Object, opts ...client.UpdateOptionFunc) error {
177+
updateOptions := &client.UpdateOptions{}
178+
updateOptions.ApplyOptions(opts)
179+
180+
for _, dryRunOpt := range updateOptions.DryRun {
181+
if dryRunOpt == metav1.DryRunAll {
182+
return nil
183+
}
184+
}
185+
167186
gvr, err := getGVRFromObject(obj, c.scheme)
168187
if err != nil {
169188
return err

pkg/client/fake/client_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
appsv1 "k8s.io/api/apps/v1"
2424
corev1 "k8s.io/api/core/v1"
25+
"k8s.io/apimachinery/pkg/api/errors"
2526
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2627
"k8s.io/apimachinery/pkg/runtime"
2728
"k8s.io/apimachinery/pkg/types"
@@ -154,6 +155,56 @@ var _ = Describe("Fake client", func() {
154155
Expect(list.Items).To(HaveLen(1))
155156
Expect(list.Items).To(ConsistOf(*dep2))
156157
})
158+
159+
Context("with the DryRun option", func() {
160+
It("should not create a new object", func() {
161+
By("Creating a new configmap with DryRun")
162+
newcm := &corev1.ConfigMap{
163+
ObjectMeta: metav1.ObjectMeta{
164+
Name: "new-test-cm",
165+
Namespace: "ns2",
166+
},
167+
}
168+
err := cl.Create(nil, newcm, client.CreateDryRunAll())
169+
Expect(err).To(BeNil())
170+
171+
By("Getting the new configmap")
172+
namespacedName := types.NamespacedName{
173+
Name: "new-test-cm",
174+
Namespace: "ns2",
175+
}
176+
obj := &corev1.ConfigMap{}
177+
err = cl.Get(nil, namespacedName, obj)
178+
Expect(err).To(HaveOccurred())
179+
Expect(errors.IsNotFound(err)).To(BeTrue())
180+
Expect(obj).NotTo(Equal(newcm))
181+
})
182+
183+
It("should not Update the object", func() {
184+
By("Updating a new configmap with DryRun")
185+
newcm := &corev1.ConfigMap{
186+
ObjectMeta: metav1.ObjectMeta{
187+
Name: "test-cm",
188+
Namespace: "ns2",
189+
},
190+
Data: map[string]string{
191+
"test-key": "new-value",
192+
},
193+
}
194+
err := cl.Update(nil, newcm, client.UpdateDryRunAll())
195+
Expect(err).To(BeNil())
196+
197+
By("Getting the new configmap")
198+
namespacedName := types.NamespacedName{
199+
Name: "test-cm",
200+
Namespace: "ns2",
201+
}
202+
obj := &corev1.ConfigMap{}
203+
err = cl.Get(nil, namespacedName, obj)
204+
Expect(err).To(BeNil())
205+
Expect(obj).To(Equal(cm))
206+
})
207+
})
157208
}
158209

159210
Context("with default scheme.Scheme", func() {

0 commit comments

Comments
 (0)