-
Notifications
You must be signed in to change notification settings - Fork 2
/
HelmChart_test.go
207 lines (192 loc) · 4.31 KB
/
HelmChart_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"context"
"encoding/json"
"fmt"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"testing"
)
const (
simpleChartPath = "testdata/charts/chart"
releaseName = "mocha"
releaseNamespace = "dog"
)
func TestHelmChart_simpleTemplate(t *testing.T) {
ctx := context.Background()
chrt, err := loader.Load(simpleChartPath)
if err != nil {
t.Errorf("Failed to load the sample chart: %v", err)
}
type fields struct {
Chart Chart
ValueFiles []string
Values map[string]interface{}
SopsValueFiles []string
}
var tv map[string]interface{}
err = json.Unmarshal([]byte(`{"image": {"tag": "latest"}}`), &tv)
tests := []struct {
name string
fields fields
want string
wantErr bool
}{
{
name: "Test default chart values",
fields: fields{
Chart: Chart{
Path: simpleChartPath,
},
},
want: fmt.Sprintf(`---
# Source: mocha/templates/pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: %v
namespace: %v
spec:
containers:
- name: container
image: rancher/rancher:stable
---
apiVersion: batch/v1
kind: Job
metadata:
name: %v
namespace: %v
annotations:
"helm.sh/hook": post-install
"helm.sh/hook-weight": "-5"
"helm.sh/hook-delete-policy": hook-succeeded
spec:
template:
spec:
containers:
- name: post-install-job
image: rancher/pause:stable`, releaseName, releaseNamespace, releaseName, releaseNamespace),
},
{
name: "Test values as map",
fields: fields{
Chart: Chart{
Path: simpleChartPath,
},
Values: tv,
},
want: fmt.Sprintf(`---
# Source: mocha/templates/pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: %v
namespace: %v
spec:
containers:
- name: container
image: rancher/rancher:latest
---
apiVersion: batch/v1
kind: Job
metadata:
name: %v
namespace: %v
annotations:
"helm.sh/hook": post-install
"helm.sh/hook-weight": "-5"
"helm.sh/hook-delete-policy": hook-succeeded
spec:
template:
spec:
containers:
- name: post-install-job
image: rancher/pause:stable`, releaseName, releaseNamespace, releaseName, releaseNamespace),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &HelmChart{
TypeMeta: TypeMeta{APIVersion: apiVersion, Kind: kind},
ObjectMeta: ObjectMeta{Name: releaseName, Namespace: releaseNamespace},
Chart: tt.fields.Chart,
ValueFiles: tt.fields.ValueFiles,
Values: tt.fields.Values,
SopsValueFiles: tt.fields.SopsValueFiles,
}
got, err := c.template(ctx, chrt)
if (err != nil) != tt.wantErr {
t.Errorf("template() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("template() got = %v, want %v", got, tt.want)
}
})
}
}
func TestHelmChart_template(t *testing.T) {
ctx := context.Background()
type fields struct {
Chart Chart
ValueFiles []string
Values map[string]interface{}
SopsValueFiles []string
}
type args struct {
ctx context.Context
ch *chart.Chart
}
tests := []struct {
name string
fields fields
args args
want string
wantErr bool
}{
{
name: "Test from local full tarball",
fields: fields{
Chart: Chart{
Path: "testdata/charts/chart-with-compressed-dependencies-2.1.8.tgz",
},
},
args: args{},
want: "",
},
{
name: "Test from local chart with missing dependencies",
fields: fields{
Chart: Chart{
Path: "testdata/charts/chart-missing-deps",
},
},
args: args{},
want: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &HelmChart{
TypeMeta: TypeMeta{APIVersion: apiVersion, Kind: kind},
ObjectMeta: ObjectMeta{Name: "mocha", Namespace: "dog"},
Chart: tt.fields.Chart,
ValueFiles: tt.fields.ValueFiles,
Values: tt.fields.Values,
SopsValueFiles: tt.fields.SopsValueFiles,
}
chrt, err := loadAndUpdate(ctx, tt.fields.Chart.Path)
if err != nil {
t.Errorf("failed to load chart: %v", err)
}
got, err := c.template(ctx, chrt)
if (err != nil) != tt.wantErr {
t.Errorf("template() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("template() got = %v, want %v", got, tt.want)
}
})
}
}