forked from operator-framework/operator-registry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_test.go
277 lines (248 loc) · 7.85 KB
/
generate_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
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
package bundle
import (
"fmt"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
)
func TestGetMediaType(t *testing.T) {
tests := []struct {
directory string
mediaType string
errorMsg string
}{
{
"./testdata/get_mediatype/registry_v1_bundle",
RegistryV1Type,
"",
},
{
"./testdata/get_mediatype/helm_bundle",
HelmType,
"",
},
{
"./testdata/get_mediatype/plain_bundle",
PlainType,
"",
},
{
"./testdata/get_mediatype/empty_bundle",
"",
"The directory contains no files",
},
}
for _, item := range tests {
manifestType, err := GetMediaType(item.directory)
if item.errorMsg == "" {
require.Equal(t, item.mediaType, manifestType)
} else {
require.Error(t, err)
}
}
}
func TestValidateAnnotations(t *testing.T) {
tests := []struct {
existing []byte
expected []byte
err error
}{
{
buildTestAnnotations("annotations",
map[string]string{
"test1": "stable",
"test2": "stable,beta",
}),
buildTestAnnotations("annotations",
map[string]string{
"test1": "stable",
"test2": "stable,beta",
}),
nil,
},
{
buildTestAnnotations("annotations",
map[string]string{
"test1": "stable",
"test2": "stable,beta",
"test3": "beta",
}),
buildTestAnnotations("annotations",
map[string]string{
"test1": "stable",
"test2": "stable,beta",
}),
nil,
},
{
buildTestAnnotations("annotations",
map[string]string{
"test1": "stable",
"test2": "stable",
}),
buildTestAnnotations("annotations",
map[string]string{
"test1": "stable",
"test2": "stable,beta",
}),
fmt.Errorf(`Expect field "test2" to have value "stable,beta" instead of "stable"`),
},
{
buildTestAnnotations("annotations",
map[string]string{
"test1": "stable",
"test3": "stable",
}),
buildTestAnnotations("annotations",
map[string]string{
"test1": "stable",
"test2": "stable,beta",
}),
fmt.Errorf("Missing field: test2"),
},
{
[]byte("\t"),
buildTestAnnotations("annotations",
map[string]string{
"test1": "stable",
"test2": "stable,beta",
}),
fmt.Errorf("yaml: found character that cannot start any token"),
},
{
buildTestAnnotations("annotations",
map[string]string{
"test1": "stable",
"test2": "stable,beta",
}),
[]byte("\t"),
fmt.Errorf("yaml: found character that cannot start any token"),
},
}
for _, item := range tests {
err := ValidateAnnotations(item.existing, item.expected)
if item.err != nil {
require.Equal(t, item.err.Error(), err.Error())
} else {
require.Nil(t, err)
}
}
}
func TestGenerateAnnotationsFunc(t *testing.T) {
// Create test annotations struct
testAnnotations := &AnnotationMetadata{
Annotations: map[string]string{
MediatypeLabel: "test1",
ManifestsLabel: "test2",
MetadataLabel: "test3",
PackageLabel: "test4",
ChannelsLabel: "test5",
ChannelDefaultLabel: "test5",
},
}
// Create result annotations struct
resultAnnotations := AnnotationMetadata{}
data, err := GenerateAnnotations("test1", "test2", "test3", "test4", "test5", "test5")
require.NoError(t, err)
err = yaml.Unmarshal(data, &resultAnnotations)
require.NoError(t, err)
for key, value := range testAnnotations.Annotations {
require.Equal(t, value, resultAnnotations.Annotations[key])
}
}
func TestGenerateDockerfile(t *testing.T) {
expected := `FROM %s
LABEL operators.operatorframework.io.bundle.mediatype.v1=test1
LABEL operators.operatorframework.io.bundle.manifests.v1=test2
LABEL operators.operatorframework.io.bundle.metadata.v1=metadata/
LABEL operators.operatorframework.io.bundle.package.v1=test4
LABEL operators.operatorframework.io.bundle.channels.v1=test5
COPY a/b/c /manifests/
COPY x/y/z /metadata/
`
type DockerfileTest struct {
baseImage string
}
tests := []DockerfileTest{
{baseImage: "scratch"},
{baseImage: "registry/group/image@tag"},
}
for _, tt := range tests {
tt_expected := fmt.Sprintf(expected, tt.baseImage)
actual, err := GenerateDockerfile("test1", "test2", "metadata/", filepath.Join("a", "b", "c"), filepath.Join("x", "y", "z"), "./", "test4", "test5", "", tt.baseImage)
require.NoError(t, err)
require.Equal(t, tt_expected, string(actual))
}
}
func TestCopyYamlOutput(t *testing.T) {
testOutputDir := t.TempDir()
testContent := []byte{0, 1, 0, 0}
testManifestDir := "./testdata/generate/manifests"
testWorkingDir := "./"
testOverwrite := true
resultManifestDir, resultMetadataDir, err := CopyYamlOutput(testContent, testManifestDir, testOutputDir, testWorkingDir, testOverwrite)
require.NoError(t, err)
require.Equal(t, filepath.Join(testOutputDir, "manifests/"), resultManifestDir)
require.Equal(t, filepath.Join(testOutputDir, "metadata/"), resultMetadataDir)
outputAnnotationsFile := filepath.Join(testOutputDir, "metadata/", "annotations.yaml")
annotationsBlob, err := os.ReadFile(outputAnnotationsFile)
require.NoError(t, err)
require.Equal(t, testContent, annotationsBlob)
csvFile := filepath.Join(testOutputDir, "manifests/", "prometheusoperator.0.14.0.clusterserviceversion.yaml")
_, err = os.ReadFile(csvFile)
require.NoError(t, err)
}
func TestCopyYamlOutput_NoOutputDir(t *testing.T) {
testContent := []byte{0, 1, 0, 0}
testManifestDir := "./testdata/generate/manifests"
testWorkingDir := "./"
testOverwrite := true
resultManifestDir, resultMetadataDir, err := CopyYamlOutput(testContent, testManifestDir, "", testWorkingDir, testOverwrite)
require.NoError(t, err)
require.Equal(t, testManifestDir, resultManifestDir)
require.Equal(t, filepath.Join(filepath.Dir(testManifestDir), "metadata/"), resultMetadataDir)
outputAnnotationsFile := filepath.Join(resultMetadataDir, "annotations.yaml")
annotationsBlob, err := os.ReadFile(outputAnnotationsFile)
require.NoError(t, err)
require.Equal(t, testContent, annotationsBlob)
os.RemoveAll(filepath.Dir(outputAnnotationsFile))
}
func TestCopyYamlOutput_NestedCopy(t *testing.T) {
testOutputDir := t.TempDir()
testContent := []byte{0, 1, 0, 0}
testManifestDir := "./testdata/generate/nested_manifests"
testWorkingDir := "./"
testOverwrite := true
resultManifestDir, resultMetadataDir, err := CopyYamlOutput(testContent, testManifestDir, testOutputDir, testWorkingDir, testOverwrite)
require.NoError(t, err)
require.Equal(t, filepath.Join(testOutputDir, "manifests/"), resultManifestDir)
require.Equal(t, filepath.Join(testOutputDir, "metadata/"), resultMetadataDir)
outputAnnotationsFile := filepath.Join(testOutputDir, "metadata/", "annotations.yaml")
annotationsBlob, err := os.ReadFile(outputAnnotationsFile)
require.NoError(t, err)
require.Equal(t, testContent, annotationsBlob)
csvFile := filepath.Join(testOutputDir, "manifests/nested_manifests/", "prometheusoperator.0.14.0.clusterserviceversion.yaml")
_, err = os.ReadFile(csvFile)
require.NoError(t, err)
}
func TestGenerateFunc(t *testing.T) {
etcdPkgPath := "./testdata/etcd"
outputPath := "./testdata/tmp_output"
defer os.RemoveAll(outputPath)
err := GenerateFunc(filepath.Join(etcdPkgPath, "0.6.1"), outputPath, "", "", "", true, "scratch")
require.NoError(t, err)
os.Remove(filepath.Join("./", DockerFile))
output := fmt.Sprintf("annotations:\n" +
" operators.operatorframework.io.bundle.channel.default.v1: alpha\n" +
" operators.operatorframework.io.bundle.channels.v1: beta\n" +
" operators.operatorframework.io.bundle.manifests.v1: manifests/\n" +
" operators.operatorframework.io.bundle.mediatype.v1: registry+v1\n" +
" operators.operatorframework.io.bundle.metadata.v1: metadata/\n" +
" operators.operatorframework.io.bundle.package.v1: etcd\n")
outputAnnotationsFile := filepath.Join(outputPath, "metadata/", "annotations.yaml")
annotationsBlob, err := os.ReadFile(outputAnnotationsFile)
require.NoError(t, err)
require.EqualValues(t, output, string(annotationsBlob))
}