Skip to content

Commit 164ec47

Browse files
committed
Add tests for DryRun on Create/Update
1 parent b1c2663 commit 164ec47

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

pkg/client/client_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,25 @@ var _ = Describe("Client", func() {
255255
// TODO(seans3): implement these
256256
// Example: ListOptions
257257
})
258+
259+
Context("with the DryRun option", func() {
260+
It("should not create a new object", func(done Done) {
261+
cl, err := client.New(cfg, client.Options{})
262+
Expect(err).NotTo(HaveOccurred())
263+
Expect(cl).NotTo(BeNil())
264+
265+
By("creating the object (with DryRun)")
266+
err = cl.Create(context.TODO(), dep, client.CreateDryRunAll())
267+
Expect(err).NotTo(HaveOccurred())
268+
269+
actual, err := clientset.AppsV1().Deployments(ns).Get(dep.Name, metav1.GetOptions{})
270+
Expect(err).To(HaveOccurred())
271+
Expect(errors.IsNotFound(err)).To(BeTrue())
272+
Expect(actual).To(Equal(&appsv1.Deployment{}))
273+
274+
close(done)
275+
})
276+
})
258277
})
259278

260279
Context("with unstructured objects", func() {
@@ -367,6 +386,33 @@ var _ = Describe("Client", func() {
367386

368387
})
369388

389+
Context("with the DryRun option", func() {
390+
It("should not create a new object from a go struct", func(done Done) {
391+
cl, err := client.New(cfg, client.Options{})
392+
Expect(err).NotTo(HaveOccurred())
393+
Expect(cl).NotTo(BeNil())
394+
395+
By("encoding the deployment as unstructured")
396+
u := &unstructured.Unstructured{}
397+
scheme.Convert(dep, u, nil)
398+
u.SetGroupVersionKind(schema.GroupVersionKind{
399+
Group: "apps",
400+
Kind: "Deployment",
401+
Version: "v1",
402+
})
403+
404+
By("creating the object")
405+
err = cl.Create(context.TODO(), u, client.CreateDryRunAll())
406+
Expect(err).NotTo(HaveOccurred())
407+
408+
actual, err := clientset.AppsV1().Deployments(ns).Get(dep.Name, metav1.GetOptions{})
409+
Expect(err).To(HaveOccurred())
410+
Expect(errors.IsNotFound(err)).To(BeTrue())
411+
Expect(actual).To(Equal(&appsv1.Deployment{}))
412+
413+
close(done)
414+
})
415+
})
370416
})
371417

372418
Describe("Update", func() {
@@ -1722,6 +1768,22 @@ var _ = Describe("Client", func() {
17221768
})
17231769
})
17241770

1771+
Describe("CreateOptions", func() {
1772+
It("should allow setting DryRun to 'all'", func() {
1773+
co := &client.CreateOptions{}
1774+
client.CreateDryRunAll()(co)
1775+
all := []string{metav1.DryRunAll}
1776+
Expect(co.AsCreateOptions().DryRun).To(Equal(all))
1777+
})
1778+
1779+
It("should produce empty metav1.CreateOptions if nil", func() {
1780+
var co *client.CreateOptions
1781+
Expect(co.AsCreateOptions()).To(Equal(&metav1.CreateOptions{}))
1782+
co = &client.CreateOptions{}
1783+
Expect(co.AsCreateOptions()).To(Equal(&metav1.CreateOptions{}))
1784+
})
1785+
})
1786+
17251787
Describe("DeleteOptions", func() {
17261788
It("should allow setting GracePeriodSeconds", func() {
17271789
do := &client.DeleteOptions{}
@@ -1846,6 +1908,22 @@ var _ = Describe("Client", func() {
18461908
Expect(lo.Namespace).To(Equal("test"))
18471909
})
18481910
})
1911+
1912+
Describe("UpdateOptions", func() {
1913+
It("should allow setting DryRun to 'all'", func() {
1914+
uo := &client.UpdateOptions{}
1915+
client.UpdateDryRunAll()(uo)
1916+
all := []string{metav1.DryRunAll}
1917+
Expect(uo.AsUpdateOptions().DryRun).To(Equal(all))
1918+
})
1919+
1920+
It("should produce empty metav1.UpdateOptions if nil", func() {
1921+
var co *client.UpdateOptions
1922+
Expect(co.AsUpdateOptions()).To(Equal(&metav1.UpdateOptions{}))
1923+
co = &client.UpdateOptions{}
1924+
Expect(co.AsUpdateOptions()).To(Equal(&metav1.UpdateOptions{}))
1925+
})
1926+
})
18491927
})
18501928

18511929
var _ = Describe("DelegatingReader", func() {

0 commit comments

Comments
 (0)