15
15
package release
16
16
17
17
import (
18
- "bytes"
19
- "encoding/json"
18
+ "io/ioutil"
20
19
"testing"
21
20
22
21
"github.com/stretchr/testify/assert"
22
+ "helm.sh/helm/v3/pkg/action"
23
23
cpb "helm.sh/helm/v3/pkg/chart"
24
24
lpb "helm.sh/helm/v3/pkg/chart/loader"
25
- rpb "helm.sh/helm/v3/pkg/release"
25
+ "helm.sh/helm/v3/pkg/chartutil"
26
+ kubefake "helm.sh/helm/v3/pkg/kube/fake"
27
+ "helm.sh/helm/v3/pkg/storage"
28
+ "helm.sh/helm/v3/pkg/storage/driver"
26
29
appsv1 "k8s.io/api/apps/v1"
27
30
v1 "k8s.io/api/core/v1"
28
31
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -219,49 +222,54 @@ func TestManagerGenerateStrategicMergePatch(t *testing.T) {
219
222
220
223
func TestManagerisUpgrade (t * testing.T ) {
221
224
tests := []struct {
222
- name string
223
- releaseName string
224
- releaseNs string
225
- values map [string ]interface {}
226
- chart * cpb.Chart
227
- deployedRelease * rpb.Release
228
- want bool
225
+ name string
226
+ releaseName string
227
+ releaseNs string
228
+ deployValues map [string ]interface {}
229
+ values map [string ]interface {}
230
+ deployChart * cpb.Chart
231
+ chart * cpb.Chart
232
+ want bool
229
233
}{
230
234
{
231
- name : "ok" ,
232
- releaseName : "deployed" ,
233
- releaseNs : "deployed-ns" ,
234
- values : map [string ]interface {}{"key" : "value" },
235
- chart : newTestChart (t , "./testdata/simple" ),
236
- deployedRelease : newTestRelease (newTestChart (t , "./testdata/simple" ), map [string ]interface {}{"key" : "value" }, "deployed" , "deployed-ns" ),
237
- want : false ,
235
+ name : "ok" ,
236
+ releaseName : "deployed" ,
237
+ releaseNs : "deployed-ns" ,
238
+ values : map [string ]interface {}{"key" : "value" },
239
+ deployValues : map [string ]interface {}{"key" : "value" },
240
+ chart : newTestChart (t , "./testdata/simple" ),
241
+ deployChart : newTestChart (t , "./testdata/simple" ),
242
+ want : false ,
238
243
},
239
244
{
240
- name : "different chart" ,
241
- releaseName : "deployed" ,
242
- releaseNs : "deployed-ns" ,
243
- values : map [string ]interface {}{"key" : "value" },
244
- chart : newTestChart (t , "./testdata/simple" ),
245
- deployedRelease : newTestRelease (newTestChart (t , "./testdata/simpledf" ), map [string ]interface {}{"key" : "value" }, "deployed" , "deployed-ns" ),
246
- want : true ,
245
+ name : "different chart" ,
246
+ releaseName : "deployed" ,
247
+ releaseNs : "deployed-ns" ,
248
+ values : map [string ]interface {}{"key" : "value" },
249
+ deployValues : map [string ]interface {}{"key" : "value" },
250
+ chart : newTestChart (t , "./testdata/simple" ),
251
+ deployChart : newTestChart (t , "./testdata/simpledf" ),
252
+ want : true ,
247
253
},
248
254
{
249
- name : "different values" ,
250
- releaseName : "deployed" ,
251
- releaseNs : "deployed-ns" ,
252
- values : map [string ]interface {}{"key" : "1" , "int" : int32 (1 )},
253
- chart : newTestChart (t , "./testdata/simple" ),
254
- deployedRelease : newTestRelease (newTestChart (t , "./testdata/simple" ), map [string ]interface {}{"key" : "" , "int" : int64 (1 )}, "deployed" , "deployed-ns" ),
255
- want : true ,
255
+ name : "different values" ,
256
+ releaseName : "deployed" ,
257
+ releaseNs : "deployed-ns" ,
258
+ deployValues : map [string ]interface {}{"key" : "1" },
259
+ values : map [string ]interface {}{"key" : "1" , "int" : int32 (1 )},
260
+ chart : newTestChart (t , "./testdata/simple" ),
261
+ deployChart : newTestChart (t , "./testdata/simple" ),
262
+ want : true ,
256
263
},
257
264
{
258
- name : "nil values" ,
259
- releaseName : "deployed" ,
260
- releaseNs : "deployed-ns" ,
261
- values : nil ,
262
- chart : newTestChart (t , "./testdata/simple" ),
263
- deployedRelease : newTestRelease (newTestChart (t , "./testdata/simple" ), map [string ]interface {}{}, "deployed" , "deployed-ns" ),
264
- want : false ,
265
+ name : "nil values" ,
266
+ releaseName : "deployed" ,
267
+ releaseNs : "deployed-ns" ,
268
+ deployValues : map [string ]interface {}{},
269
+ values : nil ,
270
+ chart : newTestChart (t , "./testdata/simple" ),
271
+ deployChart : newTestChart (t , "./testdata/simple" ),
272
+ want : false ,
265
273
},
266
274
}
267
275
for _ , test := range tests {
@@ -271,10 +279,21 @@ func TestManagerisUpgrade(t *testing.T) {
271
279
namespace : test .releaseNs ,
272
280
values : test .values ,
273
281
chart : test .chart ,
282
+ actionConfig : & action.Configuration {
283
+ Releases : storage .Init (driver .NewMemory ()),
284
+ KubeClient : & kubefake.FailingKubeClient {PrintingKubeClient : kubefake.PrintingKubeClient {Out : ioutil .Discard }},
285
+ Capabilities : chartutil .DefaultCapabilities ,
286
+ Log : t .Logf ,
287
+ },
274
288
}
275
- isUpgrade , err := m .isUpgrade (test .deployedRelease )
289
+ install := action .NewInstall (m .actionConfig )
290
+ install .Namespace = test .releaseNs
291
+ install .ReleaseName = test .releaseName
292
+ deployedRelease , err := install .Run (test .deployChart , test .deployValues )
293
+ assert .Nil (t , err )
294
+ isUpgrade , err := m .isUpgrade (deployedRelease )
276
295
assert .Equal (t , test .want , isUpgrade )
277
- assert .Equal ( t , nil , err )
296
+ assert .Nil ( t , err )
278
297
})
279
298
}
280
299
}
@@ -284,17 +303,3 @@ func newTestChart(t *testing.T, path string) *cpb.Chart {
284
303
assert .Nil (t , err )
285
304
return chart
286
305
}
287
-
288
- func newTestRelease (chart * cpb.Chart , values map [string ]interface {}, name , namespace string ) * rpb.Release { // nolint: unparam
289
- release := rpb .Mock (& rpb.MockReleaseOptions {
290
- Name : name ,
291
- Namespace : namespace ,
292
- Version : 1 ,
293
- })
294
-
295
- buffer := & bytes.Buffer {}
296
- _ = json .NewEncoder (buffer ).Encode (chart )
297
- _ = json .NewDecoder (buffer ).Decode (release .Chart )
298
- release .Config = values
299
- return release
300
- }
0 commit comments