@@ -20,6 +20,7 @@ import (
20
20
"context"
21
21
"errors"
22
22
"strconv"
23
+ "time"
23
24
24
25
"github.com/go-logr/logr"
25
26
. "github.com/onsi/ginkgo/v2"
@@ -37,6 +38,7 @@ import (
37
38
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
38
39
"k8s.io/apimachinery/pkg/runtime/schema"
39
40
apitypes "k8s.io/apimachinery/pkg/types"
41
+ "k8s.io/apimachinery/pkg/util/rand"
40
42
"k8s.io/cli-runtime/pkg/resource"
41
43
"k8s.io/utils/pointer"
42
44
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -61,7 +63,178 @@ var _ = Describe("ActionClient", func() {
61
63
It ("should return a valid ActionConfigGetter" , func () {
62
64
actionConfigGetter , err := NewActionConfigGetter (cfg , rm , logr .Discard ())
63
65
Expect (err ).ShouldNot (HaveOccurred ())
64
- Expect (NewActionClientGetter (actionConfigGetter )).NotTo (BeNil ())
66
+ acg , err := NewActionClientGetter (actionConfigGetter )
67
+ Expect (err ).To (BeNil ())
68
+ Expect (acg ).NotTo (BeNil ())
69
+ })
70
+
71
+ When ("options are specified" , func () {
72
+ expectErr := errors .New ("expect this error" )
73
+
74
+ var (
75
+ actionConfigGetter ActionConfigGetter
76
+ obj client.Object
77
+ )
78
+ BeforeEach (func () {
79
+ var err error
80
+ actionConfigGetter , err = NewActionConfigGetter (cfg , rm , logr .Discard ())
81
+ Expect (err ).ShouldNot (HaveOccurred ())
82
+ obj = testutil .BuildTestCR (gvk )
83
+ })
84
+
85
+ It ("should get clients with custom get options" , func () {
86
+ expectVersion := rand .Int ()
87
+ acg , err := NewActionClientGetter (actionConfigGetter , AppendGetOptions (
88
+ func (get * action.Get ) error {
89
+ get .Version = expectVersion
90
+ return nil
91
+ },
92
+ func (get * action.Get ) error {
93
+ Expect (get .Version ).To (Equal (expectVersion ))
94
+ return expectErr
95
+ },
96
+ ))
97
+ Expect (err ).To (BeNil ())
98
+ Expect (acg ).NotTo (BeNil ())
99
+
100
+ ac , err := acg .ActionClientFor (obj )
101
+ Expect (err ).To (BeNil ())
102
+ Expect (ac ).NotTo (BeNil ())
103
+
104
+ _ , err = ac .Get (obj .GetName ())
105
+ Expect (err ).To (MatchError (expectErr ))
106
+ })
107
+ It ("should get clients with custom install options" , func () {
108
+ acg , err := NewActionClientGetter (actionConfigGetter , AppendInstallOptions (
109
+ func (install * action.Install ) error {
110
+ install .Description = mockTestDesc
111
+ return nil
112
+ },
113
+ func (install * action.Install ) error {
114
+ Expect (install .Description ).To (Equal (mockTestDesc ))
115
+ return expectErr
116
+ },
117
+ ))
118
+ Expect (err ).To (BeNil ())
119
+ Expect (acg ).NotTo (BeNil ())
120
+
121
+ ac , err := acg .ActionClientFor (obj )
122
+ Expect (err ).To (BeNil ())
123
+ Expect (ac ).NotTo (BeNil ())
124
+
125
+ _ , err = ac .Install (obj .GetName (), obj .GetNamespace (), & chrt , chartutil.Values {})
126
+ Expect (err ).To (MatchError (expectErr ))
127
+ })
128
+ It ("should get clients with custom upgrade options" , func () {
129
+ acg , err := NewActionClientGetter (actionConfigGetter , AppendUpgradeOptions (
130
+ func (upgrade * action.Upgrade ) error {
131
+ upgrade .Description = mockTestDesc
132
+ return nil
133
+ },
134
+ func (upgrade * action.Upgrade ) error {
135
+ Expect (upgrade .Description ).To (Equal (mockTestDesc ))
136
+ return expectErr
137
+ },
138
+ ))
139
+ Expect (err ).To (BeNil ())
140
+ Expect (acg ).NotTo (BeNil ())
141
+
142
+ ac , err := acg .ActionClientFor (obj )
143
+ Expect (err ).To (BeNil ())
144
+ Expect (ac ).NotTo (BeNil ())
145
+
146
+ _ , err = ac .Upgrade (obj .GetName (), obj .GetNamespace (), & chrt , chartutil.Values {})
147
+ Expect (err ).To (MatchError (expectErr ))
148
+ })
149
+ It ("should get clients with custom uninstall options" , func () {
150
+ acg , err := NewActionClientGetter (actionConfigGetter , AppendUninstallOptions (
151
+ func (uninstall * action.Uninstall ) error {
152
+ uninstall .Description = mockTestDesc
153
+ return nil
154
+ },
155
+ func (uninstall * action.Uninstall ) error {
156
+ Expect (uninstall .Description ).To (Equal (mockTestDesc ))
157
+ return expectErr
158
+ },
159
+ ))
160
+ Expect (err ).To (BeNil ())
161
+ Expect (acg ).NotTo (BeNil ())
162
+
163
+ ac , err := acg .ActionClientFor (obj )
164
+ Expect (err ).To (BeNil ())
165
+ Expect (ac ).NotTo (BeNil ())
166
+
167
+ _ , err = ac .Uninstall (obj .GetName ())
168
+ Expect (err ).To (MatchError (expectErr ))
169
+ })
170
+ It ("should get clients with custom install failure uninstall options" , func () {
171
+ acg , err := NewActionClientGetter (actionConfigGetter , AppendInstallFailureUninstallOptions (
172
+ func (uninstall * action.Uninstall ) error {
173
+ uninstall .Description = mockTestDesc
174
+ return nil
175
+ },
176
+ func (uninstall * action.Uninstall ) error {
177
+ Expect (uninstall .Description ).To (Equal (mockTestDesc ))
178
+ return expectErr
179
+ },
180
+ ))
181
+ Expect (err ).To (BeNil ())
182
+ Expect (acg ).NotTo (BeNil ())
183
+
184
+ ac , err := acg .ActionClientFor (obj )
185
+ Expect (err ).To (BeNil ())
186
+ Expect (ac ).NotTo (BeNil ())
187
+
188
+ _ , err = ac .Install (obj .GetName (), obj .GetNamespace (), & chrt , chartutil.Values {}, func (install * action.Install ) error {
189
+ // Force the installatiom to fail by using an impossibly short wait.
190
+ // When the installation fails, the failure uninstall logic is attempted.
191
+ install .Wait = true
192
+ install .Timeout = time .Nanosecond * 1
193
+ return nil
194
+ })
195
+ Expect (err ).To (MatchError (ContainSubstring (expectErr .Error ())))
196
+
197
+ // Uninstall the chart to cleanup for other tests.
198
+ _ , err = ac .Uninstall (obj .GetName ())
199
+ Expect (err ).To (BeNil ())
200
+ })
201
+ It ("should get clients with custom upgrade failure rollback options" , func () {
202
+ expectMaxHistory := rand .Int ()
203
+ acg , err := NewActionClientGetter (actionConfigGetter , AppendUpgradeFailureRollbackOptions (
204
+ func (rollback * action.Rollback ) error {
205
+ rollback .MaxHistory = expectMaxHistory
206
+ return nil
207
+ },
208
+ func (rollback * action.Rollback ) error {
209
+ Expect (rollback .MaxHistory ).To (Equal (expectMaxHistory ))
210
+ return expectErr
211
+ },
212
+ ))
213
+ Expect (err ).To (BeNil ())
214
+ Expect (acg ).NotTo (BeNil ())
215
+
216
+ ac , err := acg .ActionClientFor (obj )
217
+ Expect (err ).To (BeNil ())
218
+ Expect (ac ).NotTo (BeNil ())
219
+
220
+ // Install the chart so that we can try an upgrade.
221
+ rel , err := ac .Install (obj .GetName (), obj .GetNamespace (), & chrt , chartutil.Values {})
222
+ Expect (err ).To (BeNil ())
223
+ Expect (rel ).NotTo (BeNil ())
224
+
225
+ _ , err = ac .Upgrade (obj .GetName (), obj .GetNamespace (), & chrt , chartutil.Values {}, func (upgrade * action.Upgrade ) error {
226
+ // Force the upgrade to fail by using an impossibly short wait.
227
+ // When the upgrade fails, the rollback logic is attempted.
228
+ upgrade .Wait = true
229
+ upgrade .Timeout = time .Nanosecond * 1
230
+ return nil
231
+ })
232
+ Expect (err ).To (MatchError (ContainSubstring (expectErr .Error ())))
233
+
234
+ // Uninstall the chart to cleanup for other tests.
235
+ _ , err = ac .Uninstall (obj .GetName ())
236
+ Expect (err ).To (BeNil ())
237
+ })
65
238
})
66
239
})
67
240
@@ -88,7 +261,8 @@ var _ = Describe("ActionClient", func() {
88
261
It ("should return a valid ActionClient" , func () {
89
262
actionConfGetter , err := NewActionConfigGetter (cfg , rm , logr .Discard ())
90
263
Expect (err ).ShouldNot (HaveOccurred ())
91
- acg := NewActionClientGetter (actionConfGetter )
264
+ acg , err := NewActionClientGetter (actionConfGetter )
265
+ Expect (err ).To (BeNil ())
92
266
ac , err := acg .ActionClientFor (obj )
93
267
Expect (err ).To (BeNil ())
94
268
Expect (ac ).NotTo (BeNil ())
@@ -107,7 +281,8 @@ var _ = Describe("ActionClient", func() {
107
281
108
282
actionConfigGetter , err := NewActionConfigGetter (cfg , rm , logr .Discard ())
109
283
Expect (err ).ShouldNot (HaveOccurred ())
110
- acg := NewActionClientGetter (actionConfigGetter )
284
+ acg , err := NewActionClientGetter (actionConfigGetter )
285
+ Expect (err ).To (BeNil ())
111
286
ac , err = acg .ActionClientFor (obj )
112
287
Expect (err ).To (BeNil ())
113
288
0 commit comments