-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrelease_test.go
More file actions
286 lines (271 loc) · 8.03 KB
/
Copy pathrelease_test.go
File metadata and controls
286 lines (271 loc) · 8.03 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package main
import (
"os"
"path/filepath"
"testing"
"time"
"gopkg.in/yaml.v2"
)
func TestUpdateVersionYAML(t *testing.T) {
tests := []struct {
name string
input YAMLVersionData
image string
releaseNotes string
wantImages []string
wantNotes string
wantTitle string
}{
{
name: "replaces single image and release notes",
input: YAMLVersionData{
Versiontitle: "old-version",
Releasenotes: "old notes",
Sources: []Sources{
{
Type: "DockerImages",
Images: []string{"old-ecr-uri:old-tag"},
},
},
Deliveryoptions: []Deliveryoptions{
{Title: "delivery1"},
},
},
image: "new-ecr-uri:new-tag",
releaseNotes: "New feature release",
wantImages: []string{"new-ecr-uri:new-tag"},
wantNotes: "New feature release",
wantTitle: "test-version",
},
{
name: "replaces multiple images across sources",
input: YAMLVersionData{
Versiontitle: "old-version",
Releasenotes: "old notes",
Sources: []Sources{
{
Type: "DockerImages",
Images: []string{"uri1:tag1", "uri2:tag2"},
},
{
Type: "DockerImages",
Images: []string{"uri3:tag3"},
},
},
},
image: "new-ecr:latest",
releaseNotes: "Multi-image update",
wantImages: []string{"new-ecr:latest"},
wantNotes: "Multi-image update",
wantTitle: "test-version",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Create temp directory structure
tmpDir := t.TempDir()
origDir, _ := os.Getwd()
os.Chdir(tmpDir)
defer os.Chdir(origDir)
productName := "TestProduct"
version := "test-version"
// Write the input YAML
dir := filepath.Join("data", productName, "versions")
os.MkdirAll(dir, 0o755)
inputBytes, err := yaml.Marshal(tc.input)
if err != nil {
t.Fatalf("failed to marshal input: %v", err)
}
filePath := filepath.Join(dir, version+".yaml")
if err := os.WriteFile(filePath, inputBytes, 0o644); err != nil {
t.Fatalf("failed to write input YAML: %v", err)
}
// Run updateVersionYAML
if err := updateVersionYAML(productName, version, tc.image, tc.releaseNotes); err != nil {
t.Fatalf("updateVersionYAML() error: %v", err)
}
// Read back and verify
result, err := getYAMLData(filePath)
if err != nil {
t.Fatalf("failed to read result: %v", err)
}
if result.Releasenotes != tc.wantNotes {
t.Errorf("release notes = %q, want %q", result.Releasenotes, tc.wantNotes)
}
if result.Versiontitle != tc.wantTitle {
t.Errorf("version title = %q, want %q", result.Versiontitle, tc.wantTitle)
}
// All images in all sources should be the new image
for i, src := range result.Sources {
for j, img := range src.Images {
if img != tc.image {
t.Errorf("source[%d].images[%d] = %q, want %q", i, j, img, tc.image)
}
}
}
})
}
}
func TestLatestVersion(t *testing.T) {
tests := []struct {
name string
details *EntityDetails
want string
wantErr bool
}{
{
name: "no versions returns error",
details: &EntityDetails{},
wantErr: true,
},
{
name: "single version",
details: &EntityDetails{
Versions: []struct {
ID string `json:"Id"`
ReleaseNotes string `json:"ReleaseNotes"`
UpgradeInstructions string `json:"UpgradeInstructions"`
VersionTitle string `json:"VersionTitle"`
CreationDate time.Time `json:"CreationDate"`
Sources []struct {
Type string `json:"Type"`
ID string `json:"Id"`
Images []string `json:"Images"`
Compatibility struct {
Platform string `json:"Platform"`
} `json:"Compatibility"`
} `json:"Sources"`
DeliveryOptions []struct {
ID string `json:"Id"`
Type string `json:"Type"`
SourceID string `json:"SourceId"`
Title string `json:"Title"`
ShortDescription string `json:"ShortDescription"`
IsRecommended bool `json:"isRecommended"`
Compatibility struct {
AWSServices []string `json:"AWSServices"`
} `json:"Compatibility"`
Instructions struct {
Usage string `json:"Usage"`
} `json:"Instructions"`
Recommendations struct {
DeploymentResources []struct {
Text string `json:"Text"`
URL string `json:"Url"`
} `json:"DeploymentResources"`
} `json:"Recommendations"`
Visibility string `json:"Visibility"`
} `json:"DeliveryOptions"`
}{
{VersionTitle: "v1.0", CreationDate: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)},
},
},
want: "v1.0",
},
{
name: "picks newest by creation date",
details: &EntityDetails{
Versions: []struct {
ID string `json:"Id"`
ReleaseNotes string `json:"ReleaseNotes"`
UpgradeInstructions string `json:"UpgradeInstructions"`
VersionTitle string `json:"VersionTitle"`
CreationDate time.Time `json:"CreationDate"`
Sources []struct {
Type string `json:"Type"`
ID string `json:"Id"`
Images []string `json:"Images"`
Compatibility struct {
Platform string `json:"Platform"`
} `json:"Compatibility"`
} `json:"Sources"`
DeliveryOptions []struct {
ID string `json:"Id"`
Type string `json:"Type"`
SourceID string `json:"SourceId"`
Title string `json:"Title"`
ShortDescription string `json:"ShortDescription"`
IsRecommended bool `json:"isRecommended"`
Compatibility struct {
AWSServices []string `json:"AWSServices"`
} `json:"Compatibility"`
Instructions struct {
Usage string `json:"Usage"`
} `json:"Instructions"`
Recommendations struct {
DeploymentResources []struct {
Text string `json:"Text"`
URL string `json:"Url"`
} `json:"DeploymentResources"`
} `json:"Recommendations"`
Visibility string `json:"Visibility"`
} `json:"DeliveryOptions"`
}{
{VersionTitle: "v1.0", CreationDate: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC)},
{VersionTitle: "v1.2", CreationDate: time.Date(2024, 6, 15, 0, 0, 0, 0, time.UTC)},
{VersionTitle: "v1.1", CreationDate: time.Date(2024, 3, 1, 0, 0, 0, 0, time.UTC)},
},
},
want: "v1.2",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
got, err := latestVersion(tc.details)
if tc.wantErr {
if err == nil {
t.Fatal("expected error, got nil")
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got != tc.want {
t.Errorf("latestVersion() = %q, want %q", got, tc.want)
}
})
}
}
func TestReleaseVersionValidation(t *testing.T) {
tests := []struct {
name string
product string
version string
image string
releaseNotes string
wantErr string
}{
{
name: "missing product",
product: "", version: "v1", image: "img:1", releaseNotes: "notes",
wantErr: "product, version, image, and release notes are all required",
},
{
name: "missing version",
product: "P", version: "", image: "img:1", releaseNotes: "notes",
wantErr: "product, version, image, and release notes are all required",
},
{
name: "missing image",
product: "P", version: "v1", image: "", releaseNotes: "notes",
wantErr: "product, version, image, and release notes are all required",
},
{
name: "missing release notes",
product: "P", version: "v1", image: "img:1", releaseNotes: "",
wantErr: "product, version, image, and release notes are all required",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
err := releaseVersion(tc.product, tc.version, tc.image, tc.releaseNotes, "", true)
if err == nil {
t.Fatal("expected error, got nil")
}
if err.Error() != tc.wantErr {
t.Errorf("error = %q, want %q", err.Error(), tc.wantErr)
}
})
}
}