Skip to content

Commit c162794

Browse files
authored
Merge pull request #1873 from everettraven/feature/fake-client-delete-dry-run
⚠️ make fake client delete operations honor dry run opt
2 parents da9d35c + 98d34c2 commit c162794

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

pkg/client/fake/client.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,12 @@ func (c *fakeClient) Delete(ctx context.Context, obj client.Object, opts ...clie
473473
delOptions := client.DeleteOptions{}
474474
delOptions.ApplyOptions(opts)
475475

476+
for _, dryRunOpt := range delOptions.DryRun {
477+
if dryRunOpt == metav1.DryRunAll {
478+
return nil
479+
}
480+
}
481+
476482
// Check the ResourceVersion if that Precondition was specified.
477483
if delOptions.Preconditions != nil && delOptions.Preconditions.ResourceVersion != nil {
478484
name := accessor.GetName()
@@ -507,6 +513,12 @@ func (c *fakeClient) DeleteAllOf(ctx context.Context, obj client.Object, opts ..
507513
dcOptions := client.DeleteAllOfOptions{}
508514
dcOptions.ApplyOptions(opts)
509515

516+
for _, dryRunOpt := range dcOptions.DryRun {
517+
if dryRunOpt == metav1.DryRunAll {
518+
return nil
519+
}
520+
}
521+
510522
gvr, _ := meta.UnsafeGuessKindToResource(gvk)
511523
o, err := c.tracker.List(gvr, gvk, dcOptions.Namespace)
512524
if err != nil {

pkg/client/fake/client_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,27 @@ var _ = Describe("Fake client", func() {
830830
Expect(obj).To(Equal(cm))
831831
Expect(obj.ObjectMeta.ResourceVersion).To(Equal(trackerAddResourceVersion))
832832
})
833+
834+
It("Should not Delete the object", func() {
835+
By("Deleting a configmap with DryRun with Delete()")
836+
err := cl.Delete(context.Background(), cm, client.DryRunAll)
837+
Expect(err).To(BeNil())
838+
839+
By("Deleting a configmap with DryRun with DeleteAllOf()")
840+
err = cl.DeleteAllOf(context.Background(), cm, client.DryRunAll)
841+
Expect(err).To(BeNil())
842+
843+
By("Getting the configmap")
844+
namespacedName := types.NamespacedName{
845+
Name: "test-cm",
846+
Namespace: "ns2",
847+
}
848+
obj := &corev1.ConfigMap{}
849+
err = cl.Get(context.Background(), namespacedName, obj)
850+
Expect(err).To(BeNil())
851+
Expect(obj).To(Equal(cm))
852+
Expect(obj.ObjectMeta.ResourceVersion).To(Equal(trackerAddResourceVersion))
853+
})
833854
})
834855

835856
It("should be able to Patch", func() {

0 commit comments

Comments
 (0)