Skip to content

Commit

Permalink
Added DeleteCollection to the fake.Client
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron committed Feb 12, 2019
1 parent 49a6424 commit 3e60985
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
42 changes: 42 additions & 0 deletions pkg/client/fake/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,48 @@ func (c *fakeClient) Delete(ctx context.Context, obj runtime.Object, opts ...cli
return c.tracker.Delete(gvr, accessor.GetNamespace(), accessor.GetName())
}

func (c *fakeClient) DeleteCollection(ctx context.Context, obj runtime.Object, opts ...client.DeleteCollectionOptionFunc) error {
gvk, err := apiutil.GVKForObject(obj, scheme.Scheme)
if err != nil {
return err
}

if !strings.HasSuffix(gvk.Kind, "List") {
return fmt.Errorf("non-list type %T (kind %q) passed as input", obj, gvk)
}
// we need the non-list GVK, so chop off the "List" from the end of the kind
gvk.Kind = gvk.Kind[:len(gvk.Kind)-4]

dcOptions := client.DeleteCollectionOptions{}
dcOptions.ApplyOptions(opts)

gvr, _ := meta.UnsafeGuessKindToResource(gvk)
o, err := c.tracker.List(gvr, gvk, dcOptions.Namespace)
if err != nil {
return err
}

objs, err := meta.ExtractList(o)
if err != nil {
return err
}
filteredObjs, err := objectutil.FilterWithLabels(objs, dcOptions.LabelSelector)
if err != nil {
return err
}
for _, o := range filteredObjs {
accessor, err := meta.Accessor(o)
if err != nil {
return err
}
err = c.tracker.Delete(gvr, accessor.GetNamespace(), accessor.GetName())
if err != nil {
return err
}
}
return nil
}

func (c *fakeClient) Update(ctx context.Context, obj runtime.Object) error {
gvr, err := getGVRFromObject(obj, c.scheme)
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions pkg/client/fake/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,18 @@ var _ = Describe("Fake client", func() {
Expect(list.Items).To(HaveLen(1))
Expect(list.Items).To(ConsistOf(*dep2))
})

It("should be able to Delete a Collection", func() {
By("Deleting a deploymentList")
err := cl.DeleteCollection(nil, &appsv1.DeploymentList{})
Expect(err).To(BeNil())

By("Listing all deployments in the namespace")
list := &appsv1.DeploymentList{}
err = cl.List(nil, list, client.InNamespace("ns1"))
Expect(err).To(BeNil())
Expect(list.Items).To(BeEmpty())
})
}

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

0 comments on commit 3e60985

Please sign in to comment.