Skip to content

Commit 935dc3f

Browse files
committed
*: updates to improve code coverage in ./pkg/reconciler
1 parent a847888 commit 935dc3f

File tree

16 files changed

+988
-500
lines changed

16 files changed

+988
-500
lines changed

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ github.com/smartystreets/goconvey v0.0.0-20190330032615-68dc04aab96a/go.mod h1:s
503503
github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4kGIyLM=
504504
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
505505
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
506+
github.com/spf13/afero v1.2.2 h1:5jhuqJyZCZf2JRofRvN/nIFgIWNzPa3/Vz8mYylgbWc=
506507
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
507508
github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8=
508509
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
@@ -778,6 +779,7 @@ k8s.io/apimachinery v0.18.0/go.mod h1:9SnR/e11v5IbyPCGbvJViimtJ0SwHG4nfZFjU77ftc
778779
k8s.io/apimachinery v0.18.2 h1:44CmtbmkzVDAhCpRVSiP2R5PPrC2RtlIv/MoB8xpdRA=
779780
k8s.io/apimachinery v0.18.2/go.mod h1:9SnR/e11v5IbyPCGbvJViimtJ0SwHG4nfZFjU77ftcA=
780781
k8s.io/apiserver v0.18.0/go.mod h1:3S2O6FeBBd6XTo0njUrLxiqk8GNy6wWOftjhJcXYnjw=
782+
k8s.io/apiserver v0.18.2 h1:fwKxdTWwwYhxvtjo0UUfX+/fsitsNtfErPNegH2x9ic=
781783
k8s.io/apiserver v0.18.2/go.mod h1:Xbh066NqrZO8cbsoenCwyDJ1OSi8Ag8I2lezeHxzwzw=
782784
k8s.io/cli-runtime v0.18.0/go.mod h1:1eXfmBsIJosjn9LjEBUd2WVPoPAY9XGTqTFcPMIBsUQ=
783785
k8s.io/cli-runtime v0.18.2 h1:JiTN5RgkFNTiMxHBRyrl6n26yKWAuNRlei1ZJALUmC8=
@@ -801,6 +803,7 @@ k8s.io/kube-openapi v0.0.0-20200121204235-bf4fb3bd569c/go.mod h1:GRQhZsXIAJ1xR0C
801803
k8s.io/kubectl v0.18.0/go.mod h1:LOkWx9Z5DXMEg5KtOjHhRiC1fqJPLyCr3KtQgEolCkU=
802804
k8s.io/kubectl v0.18.2 h1:9jnGSOC2DDVZmMUTMi0D1aed438mfQcgqa5TAzVjA1k=
803805
k8s.io/kubectl v0.18.2/go.mod h1:OdgFa3AlsPKRpFFYE7ICTwulXOcMGXHTc+UKhHKvrb4=
806+
k8s.io/kubernetes v1.13.0 h1:qTfB+u5M92k2fCCCVP2iuhgwwSOv1EkAkvQY1tQODD8=
804807
k8s.io/kubernetes v1.13.0/go.mod h1:ocZa8+6APFNC2tX1DZASIbocyYT5jHzqFVsY5aoB7Jk=
805808
k8s.io/metrics v0.18.0/go.mod h1:8aYTW18koXqjLVKL7Ds05RPMX9ipJZI3mywYvBOxXd4=
806809
k8s.io/metrics v0.18.2/go.mod h1:qga8E7QfYNR9Q89cSCAjinC9pTZ7yv1XSVGUB0vJypg=

pkg/annotation/annotation.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ const (
3636
DefaultUninstallDisableHooksName = DefaultDomain + "/uninstall-disable-hooks"
3737

3838
DefaultUpgradeForceName = DefaultDomain + "/upgrade-force"
39+
40+
DefaultInstallDescriptionName = DefaultDomain + "/install-description"
41+
DefaultUpgradeDescriptionName = DefaultDomain + "/upgrade-description"
42+
DefaultUninstallDescriptionName = DefaultDomain + "/uninstall-description"
3943
)
4044

4145
func (i InstallDisableHooks) Name() string {
@@ -127,3 +131,60 @@ func (u UninstallDisableHooks) UninstallOption(val string) helmclient.UninstallO
127131
return nil
128132
}
129133
}
134+
135+
var _ Install = &InstallDescription{}
136+
137+
type InstallDescription struct {
138+
CustomName string
139+
}
140+
141+
func (i InstallDescription) Name() string {
142+
if i.CustomName != "" {
143+
return i.CustomName
144+
}
145+
return DefaultInstallDescriptionName
146+
}
147+
func (i InstallDescription) InstallOption(v string) helmclient.InstallOption {
148+
return func(i *action.Install) error {
149+
i.Description = v
150+
return nil
151+
}
152+
}
153+
154+
var _ Upgrade = &UpgradeDescription{}
155+
156+
type UpgradeDescription struct {
157+
CustomName string
158+
}
159+
160+
func (u UpgradeDescription) Name() string {
161+
if u.CustomName != "" {
162+
return u.CustomName
163+
}
164+
return DefaultUpgradeDescriptionName
165+
}
166+
func (u UpgradeDescription) UpgradeOption(v string) helmclient.UpgradeOption {
167+
return func(upgrade *action.Upgrade) error {
168+
upgrade.Description = v
169+
return nil
170+
}
171+
}
172+
173+
var _ Uninstall = &UninstallDescription{}
174+
175+
type UninstallDescription struct {
176+
CustomName string
177+
}
178+
179+
func (u UninstallDescription) Name() string {
180+
if u.CustomName != "" {
181+
return u.CustomName
182+
}
183+
return DefaultUninstallDescriptionName
184+
}
185+
func (u UninstallDescription) UninstallOption(v string) helmclient.UninstallOption {
186+
return func(uninstall *action.Uninstall) error {
187+
uninstall.Description = v
188+
return nil
189+
}
190+
}

pkg/annotation/annotation_test.go

Lines changed: 81 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,45 @@ var _ = Describe("Annotation", func() {
3434
})
3535

3636
It("should disable hooks", func() {
37-
Expect(a.InstallOption("true")(&install)).NotTo(HaveOccurred())
37+
Expect(a.InstallOption("true")(&install)).To(Succeed())
3838
Expect(install.DisableHooks).To(BeTrue())
3939
})
4040

4141
It("should enable hooks", func() {
4242
install.DisableHooks = true
43-
Expect(a.InstallOption("false")(&install)).NotTo(HaveOccurred())
43+
Expect(a.InstallOption("false")(&install)).To(Succeed())
4444
Expect(install.DisableHooks).To(BeFalse())
4545
})
4646

4747
It("should default to false with invalid value", func() {
4848
install.DisableHooks = true
49-
Expect(a.InstallOption("invalid")(&install)).NotTo(HaveOccurred())
49+
Expect(a.InstallOption("invalid")(&install)).To(Succeed())
5050
Expect(install.DisableHooks).To(BeFalse())
5151
})
5252
})
53+
54+
Describe("Description", func() {
55+
var a annotation.InstallDescription
56+
57+
BeforeEach(func() {
58+
a = annotation.InstallDescription{}
59+
})
60+
61+
It("should return a default name", func() {
62+
Expect(a.Name()).To(Equal(annotation.DefaultInstallDescriptionName))
63+
})
64+
65+
It("should return a custom name", func() {
66+
const customName = "custom.domain/custom-name"
67+
a.CustomName = customName
68+
Expect(a.Name()).To(Equal(customName))
69+
})
70+
71+
It("should set a description", func() {
72+
Expect(a.InstallOption("test description")(&install)).To(Succeed())
73+
Expect(install.Description).To(Equal("test description"))
74+
})
75+
})
5376
})
5477

5578
Describe("Upgrade", func() {
@@ -77,19 +100,19 @@ var _ = Describe("Annotation", func() {
77100
})
78101

79102
It("should disable hooks", func() {
80-
Expect(a.UpgradeOption("true")(&upgrade)).NotTo(HaveOccurred())
103+
Expect(a.UpgradeOption("true")(&upgrade)).To(Succeed())
81104
Expect(upgrade.DisableHooks).To(BeTrue())
82105
})
83106

84107
It("should enable hooks", func() {
85108
upgrade.DisableHooks = true
86-
Expect(a.UpgradeOption("false")(&upgrade)).NotTo(HaveOccurred())
109+
Expect(a.UpgradeOption("false")(&upgrade)).To(Succeed())
87110
Expect(upgrade.DisableHooks).To(BeFalse())
88111
})
89112

90113
It("should default to false with invalid value", func() {
91114
upgrade.DisableHooks = true
92-
Expect(a.UpgradeOption("invalid")(&upgrade)).NotTo(HaveOccurred())
115+
Expect(a.UpgradeOption("invalid")(&upgrade)).To(Succeed())
93116
Expect(upgrade.DisableHooks).To(BeFalse())
94117
})
95118
})
@@ -112,22 +135,45 @@ var _ = Describe("Annotation", func() {
112135
})
113136

114137
It("should force upgrades", func() {
115-
Expect(a.UpgradeOption("true")(&upgrade)).NotTo(HaveOccurred())
138+
Expect(a.UpgradeOption("true")(&upgrade)).To(Succeed())
116139
Expect(upgrade.Force).To(BeTrue())
117140
})
118141

119142
It("should not force upgrades", func() {
120143
upgrade.Force = true
121-
Expect(a.UpgradeOption("false")(&upgrade)).NotTo(HaveOccurred())
144+
Expect(a.UpgradeOption("false")(&upgrade)).To(Succeed())
122145
Expect(upgrade.Force).To(BeFalse())
123146
})
124147

125148
It("should default to not forcing upgrades", func() {
126149
upgrade.Force = true
127-
Expect(a.UpgradeOption("invalid")(&upgrade)).NotTo(HaveOccurred())
150+
Expect(a.UpgradeOption("invalid")(&upgrade)).To(Succeed())
128151
Expect(upgrade.Force).To(BeFalse())
129152
})
130153
})
154+
155+
Describe("Description", func() {
156+
var a annotation.UpgradeDescription
157+
158+
BeforeEach(func() {
159+
a = annotation.UpgradeDescription{}
160+
})
161+
162+
It("should return a default name", func() {
163+
Expect(a.Name()).To(Equal(annotation.DefaultUpgradeDescriptionName))
164+
})
165+
166+
It("should return a custom name", func() {
167+
const customName = "custom.domain/custom-name"
168+
a.CustomName = customName
169+
Expect(a.Name()).To(Equal(customName))
170+
})
171+
172+
It("should set a description", func() {
173+
Expect(a.UpgradeOption("test description")(&upgrade)).To(Succeed())
174+
Expect(upgrade.Description).To(Equal("test description"))
175+
})
176+
})
131177
})
132178

133179
Describe("Uninstall", func() {
@@ -155,21 +201,44 @@ var _ = Describe("Annotation", func() {
155201
})
156202

157203
It("should disable hooks", func() {
158-
Expect(a.UninstallOption("true")(&uninstall)).NotTo(HaveOccurred())
204+
Expect(a.UninstallOption("true")(&uninstall)).To(Succeed())
159205
Expect(uninstall.DisableHooks).To(BeTrue())
160206
})
161207

162208
It("should enable hooks", func() {
163209
uninstall.DisableHooks = true
164-
Expect(a.UninstallOption("false")(&uninstall)).NotTo(HaveOccurred())
210+
Expect(a.UninstallOption("false")(&uninstall)).To(Succeed())
165211
Expect(uninstall.DisableHooks).To(BeFalse())
166212
})
167213

168214
It("should default to false with invalid value", func() {
169215
uninstall.DisableHooks = true
170-
Expect(a.UninstallOption("invalid")(&uninstall)).NotTo(HaveOccurred())
216+
Expect(a.UninstallOption("invalid")(&uninstall)).To(Succeed())
171217
Expect(uninstall.DisableHooks).To(BeFalse())
172218
})
173219
})
220+
221+
Describe("Description", func() {
222+
var a annotation.UninstallDescription
223+
224+
BeforeEach(func() {
225+
a = annotation.UninstallDescription{}
226+
})
227+
228+
It("should return a default name", func() {
229+
Expect(a.Name()).To(Equal(annotation.DefaultUninstallDescriptionName))
230+
})
231+
232+
It("should return a custom name", func() {
233+
const customName = "custom.domain/custom-name"
234+
a.CustomName = customName
235+
Expect(a.Name()).To(Equal(customName))
236+
})
237+
238+
It("should set a description", func() {
239+
Expect(a.UninstallOption("test description")(&uninstall)).To(Succeed())
240+
Expect(uninstall.Description).To(Equal("test description"))
241+
})
242+
})
174243
})
175244
})

pkg/client/actionclient.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ type ActionClientGetter interface {
4848
ActionClientFor(obj Object) (ActionInterface, error)
4949
}
5050

51+
type ActionClientGetterFunc func(obj Object) (ActionInterface, error)
52+
53+
func (acgf ActionClientGetterFunc) ActionClientFor(obj Object) (ActionInterface, error) {
54+
return acgf(obj)
55+
}
56+
5157
type ActionInterface interface {
5258
Get(name string, opts ...GetOption) (*release.Release, error)
5359
Install(name, namespace string, chrt *chart.Chart, vals map[string]interface{}, opts ...InstallOption) (*release.Release, error)

pkg/client/actionclient_test.go

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@ import (
1515
v1 "k8s.io/api/core/v1"
1616
apierrors "k8s.io/apimachinery/pkg/api/errors"
1717
"k8s.io/apimachinery/pkg/api/meta"
18-
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1918
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
2019
"k8s.io/apimachinery/pkg/runtime"
21-
"k8s.io/apimachinery/pkg/types"
2220
"sigs.k8s.io/controller-runtime/pkg/client"
2321
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"
2422
"sigs.k8s.io/yaml"
@@ -64,7 +62,6 @@ var _ = Describe("ActionClient", func() {
6462
)
6563
BeforeEach(func() {
6664
obj = testutil.BuildTestCR(gvk)
67-
obj.SetUID(types.UID("test-uid"))
6865

6966
var err error
7067
actionConfigGetter := NewActionConfigGetter(cfg, rm, nil)
@@ -84,7 +81,13 @@ var _ = Describe("ActionClient", func() {
8481

8582
When("release is not installed", func() {
8683
AfterEach(func() {
87-
ensureUninstalled(ac, obj)
84+
if _, err := ac.Get(obj.GetName()); err == driver.ErrReleaseNotFound {
85+
return
86+
}
87+
_, err := ac.Uninstall(obj.GetName())
88+
if err != nil {
89+
panic(err)
90+
}
8891
})
8992
var _ = Describe("Install", func() {
9093
It("should succeed", func() {
@@ -147,9 +150,14 @@ var _ = Describe("ActionClient", func() {
147150
Expect(installedRelease).NotTo(BeNil())
148151
})
149152
AfterEach(func() {
150-
ensureUninstalled(ac, obj)
153+
if _, err := ac.Get(obj.GetName()); err == driver.ErrReleaseNotFound {
154+
return
155+
}
156+
_, err := ac.Uninstall(obj.GetName())
157+
if err != nil {
158+
panic(err)
159+
}
151160
})
152-
153161
var _ = Describe("Get", func() {
154162
var (
155163
rel *release.Release
@@ -240,7 +248,7 @@ var _ = Describe("ActionClient", func() {
240248
Expect(err).To(BeNil())
241249
Expect(resp).NotTo(BeNil())
242250
})
243-
verifyNoRelease(cl, obj.GetNamespace(), obj.GetName(), resp)
251+
verifyNoRelease(cl, obj.GetNamespace(), obj.GetName(), resp.Release)
244252
})
245253
When("using an option function that returns an error", func() {
246254
It("should fail", func() {
@@ -315,15 +323,6 @@ func manifestToObjects(manifest string) []runtime.Object {
315323
return objs
316324
}
317325

318-
func ensureUninstalled(ac ActionInterface, obj metav1.Object) {
319-
if _, err := ac.Get(obj.GetName()); err == driver.ErrReleaseNotFound {
320-
return
321-
}
322-
resp, err := ac.Uninstall(obj.GetName())
323-
Expect(err).To(BeNil())
324-
Expect(resp).NotTo(BeNil())
325-
}
326-
327326
func verifyRelease(cl client.Client, ns string, rel *release.Release) {
328327
By("verifying release secret exists at release version", func() {
329328
releaseSecrets := &v1.SecretList{}
@@ -351,21 +350,21 @@ func verifyRelease(cl client.Client, ns string, rel *release.Release) {
351350
})
352351
}
353352

354-
func verifyNoRelease(cl client.Client, ns string, name string, resp *release.UninstallReleaseResponse) {
353+
func verifyNoRelease(cl client.Client, ns string, name string, rel *release.Release) {
355354
By("verifying all release secrets are removed", func() {
356355
releaseSecrets := &v1.SecretList{}
357356
err := cl.List(context.TODO(), releaseSecrets, client.InNamespace(ns), client.MatchingLabels{"owner": "helm", "name": name})
358357
Expect(err).To(BeNil())
359358
Expect(releaseSecrets.Items).To(HaveLen(0))
360359
})
361360
By("verifying the uninstall description option was honored", func() {
362-
if resp != nil {
363-
Expect(resp.Release.Info.Description).To(Equal("Test Description"))
361+
if rel != nil {
362+
Expect(rel.Info.Description).To(Equal("Test Description"))
364363
}
365364
})
366365
By("verifying all release resources are removed", func() {
367-
if resp != nil {
368-
for _, r := range releaseutil.SplitManifests(resp.Release.Manifest) {
366+
if rel != nil {
367+
for _, r := range releaseutil.SplitManifests(rel.Manifest) {
369368
u := &unstructured.Unstructured{}
370369
err := yaml.Unmarshal([]byte(r), u)
371370
Expect(err).To(BeNil())

0 commit comments

Comments
 (0)