Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/dev/version-manifest/main_test.go
2492 views
1
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package main
6
7
import (
8
"bytes"
9
"io/fs"
10
"testing"
11
"testing/fstest"
12
13
"github.com/google/go-cmp/cmp"
14
)
15
16
func TestProduceManifest(t *testing.T) {
17
type Expectation struct {
18
MF string
19
Error string
20
}
21
tests := []struct {
22
Name string
23
FS fs.FS
24
Expectation Expectation
25
}{
26
{
27
Name: "happy path",
28
FS: fstest.MapFS{
29
"c1/metadata.yaml": &fstest.MapFile{Data: []byte("helm-component: c1")},
30
"c1/imgnames.txt": &fstest.MapFile{Data: []byte("failthis\nimgc1:v1")},
31
"c2/metadata.yaml": &fstest.MapFile{Data: []byte("helm-component: c2.nested")},
32
"c2/imgnames.txt": &fstest.MapFile{Data: []byte("failthis\nimgc2:v2\n\n")},
33
"c20/metadata.yaml": &fstest.MapFile{Data: []byte("helm-component: c2.other")},
34
"c20/imgnames.txt": &fstest.MapFile{Data: []byte("failthis\nimgc2:v2\n\n")},
35
},
36
Expectation: Expectation{
37
MF: `components:
38
c1:
39
version: v1
40
41
c2:
42
nested:
43
version: v2
44
45
other:
46
version: v2
47
48
`,
49
},
50
},
51
{
52
Name: "imgnames only",
53
FS: fstest.MapFS{
54
"c1/imgnames.txt": &fstest.MapFile{Data: []byte("imgc1")},
55
},
56
Expectation: Expectation{MF: "components:\n"},
57
},
58
{
59
Name: "incomplete metadata",
60
FS: fstest.MapFS{
61
"c1/metadata.yaml": &fstest.MapFile{Data: []byte("")},
62
"c1/imgnames.txt": &fstest.MapFile{Data: []byte("failthis\nimgc1")},
63
},
64
Expectation: Expectation{MF: "components:\n"},
65
},
66
{
67
Name: "missing imgnames",
68
FS: fstest.MapFS{
69
"c1/metadata.yaml": &fstest.MapFile{Data: []byte("helm-component: foobar")},
70
},
71
Expectation: Expectation{Error: "cannot read image names for c1/metadata.yaml: open c1/imgnames.txt: file does not exist"},
72
},
73
{
74
Name: "broken metadata",
75
FS: fstest.MapFS{
76
"c1/metadata.yaml": &fstest.MapFile{Data: []byte("invalid YAML")},
77
"c1/imgnames.txt": &fstest.MapFile{Data: []byte("failthis\nimgc1")},
78
},
79
Expectation: Expectation{Error: "cannot unmarshal c1/metadata.yaml: yaml: unmarshal errors:\n line 1: cannot unmarshal !!str `invalid...` into main.MD"},
80
},
81
{
82
Name: "invalid image name",
83
FS: fstest.MapFS{
84
"c1/metadata.yaml": &fstest.MapFile{Data: []byte("helm-component: foobar")},
85
"c1/imgnames.txt": &fstest.MapFile{Data: []byte("failthis\nimgc1")},
86
},
87
Expectation: Expectation{Error: "invalid image format: imgc1"},
88
},
89
}
90
91
for _, test := range tests {
92
t.Run(test.Name, func(t *testing.T) {
93
buf := bytes.NewBuffer(nil)
94
err := produceManifest(buf, test.FS)
95
var act Expectation
96
if err != nil {
97
act.Error = err.Error()
98
} else {
99
act.MF = buf.String()
100
}
101
102
if diff := cmp.Diff(test.Expectation, act); diff != "" {
103
t.Errorf("produceManifest() mismatch (-want +got):\n%s", diff)
104
}
105
})
106
}
107
}
108
109