Skip to content

Commit

Permalink
Merge pull request #1873 from everettraven/feature/fake-client-delete…
Browse files Browse the repository at this point in the history
…-dry-run

⚠️  make fake client delete operations honor dry run opt
  • Loading branch information
k8s-ci-robot authored Apr 21, 2022
2 parents da9d35c + 98d34c2 commit c162794
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/client/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,12 @@ func (c *fakeClient) Delete(ctx context.Context, obj client.Object, opts ...clie
delOptions := client.DeleteOptions{}
delOptions.ApplyOptions(opts)

for _, dryRunOpt := range delOptions.DryRun {
if dryRunOpt == metav1.DryRunAll {
return nil
}
}

// Check the ResourceVersion if that Precondition was specified.
if delOptions.Preconditions != nil && delOptions.Preconditions.ResourceVersion != nil {
name := accessor.GetName()
Expand Down Expand Up @@ -507,6 +513,12 @@ func (c *fakeClient) DeleteAllOf(ctx context.Context, obj client.Object, opts ..
dcOptions := client.DeleteAllOfOptions{}
dcOptions.ApplyOptions(opts)

for _, dryRunOpt := range dcOptions.DryRun {
if dryRunOpt == metav1.DryRunAll {
return nil
}
}

gvr, _ := meta.UnsafeGuessKindToResource(gvk)
o, err := c.tracker.List(gvr, gvk, dcOptions.Namespace)
if err != nil {
Expand Down
21 changes: 21 additions & 0 deletions pkg/client/fake/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,27 @@ var _ = Describe("Fake client", func() {
Expect(obj).To(Equal(cm))
Expect(obj.ObjectMeta.ResourceVersion).To(Equal(trackerAddResourceVersion))
})

It("Should not Delete the object", func() {
By("Deleting a configmap with DryRun with Delete()")
err := cl.Delete(context.Background(), cm, client.DryRunAll)
Expect(err).To(BeNil())

By("Deleting a configmap with DryRun with DeleteAllOf()")
err = cl.DeleteAllOf(context.Background(), cm, client.DryRunAll)
Expect(err).To(BeNil())

By("Getting the configmap")
namespacedName := types.NamespacedName{
Name: "test-cm",
Namespace: "ns2",
}
obj := &corev1.ConfigMap{}
err = cl.Get(context.Background(), namespacedName, obj)
Expect(err).To(BeNil())
Expect(obj).To(Equal(cm))
Expect(obj.ObjectMeta.ResourceVersion).To(Equal(trackerAddResourceVersion))
})
})

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

0 comments on commit c162794

Please sign in to comment.