15
15
package olmcatalog
16
16
17
17
import (
18
- "bytes"
19
- "fmt"
20
18
"io/ioutil"
21
19
"os"
22
20
"os/exec"
23
21
"path/filepath"
24
22
"testing"
25
23
26
- "github.com/blang/semver"
27
- "github.com/operator-framework/api/pkg/operators/v1alpha1"
28
- olmapiv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
29
- log "github.com/sirupsen/logrus"
30
- "github.com/stretchr/testify/assert"
31
- "sigs.k8s.io/yaml"
32
-
33
24
"github.com/operator-framework/operator-sdk/internal/generate/clusterserviceversion/bases"
34
25
"github.com/operator-framework/operator-sdk/internal/util/fileutil"
35
26
"github.com/operator-framework/operator-sdk/internal/util/k8sutil"
36
27
"github.com/operator-framework/operator-sdk/internal/util/projutil"
28
+
29
+ "github.com/blang/semver"
30
+ "github.com/operator-framework/api/pkg/operators/v1alpha1"
31
+ "github.com/stretchr/testify/assert"
32
+ "sigs.k8s.io/yaml"
37
33
)
38
34
39
35
const (
@@ -81,6 +77,14 @@ func mkTempDirWithCleanup(t *testing.T, prefix string) (dir string, f func()) {
81
77
return
82
78
}
83
79
80
+ func readFile (t * testing.T , path string ) []byte {
81
+ b , err := ioutil .ReadFile (path )
82
+ if err != nil {
83
+ t .Fatalf ("Failed to read testdata file: %v" , err )
84
+ }
85
+ return b
86
+ }
87
+
84
88
// TODO: Change to table driven subtests to test out different Inputs/Output for the generator
85
89
func TestGoCSVNewWithInputsToOutput (t * testing.T ) {
86
90
// Change directory to project root so the test cases can form the correct pkg imports
@@ -115,15 +119,13 @@ func TestGoCSVNewWithInputsToOutput(t *testing.T) {
115
119
116
120
// Read expected CSV
117
121
expBundleDir := filepath .Join ("expected-catalog" , OLMCatalogChildDir , testProjectName , csvVersion )
122
+ csvExp := string (readFile (t , filepath .Join (expBundleDir , csvFileName )))
118
123
119
124
// Read generated CSV from outputDir path
120
125
outputBundleDir := filepath .Join (outputDir , OLMCatalogChildDir , testProjectName , csvVersion )
126
+ csvOutput := string (readFile (t , filepath .Join (outputBundleDir , csvFileName )))
121
127
122
- outputCSV := getCSVFromFile (t , filepath .Join (outputBundleDir , csvFileName ))
123
- removeSDKLabelsFromCSVHelper (outputCSV )
124
-
125
- expCSV := getCSVFromFile (t , filepath .Join (expBundleDir , csvFileName ))
126
- assert .Equal (t , expCSV , outputCSV )
128
+ assert .Equal (t , csvExp , csvOutput )
127
129
}
128
130
129
131
func TestGoCSVUpgradeWithInputsToOutput (t * testing.T ) {
@@ -172,14 +174,13 @@ func TestGoCSVUpgradeWithInputsToOutput(t *testing.T) {
172
174
173
175
// Read expected CSV
174
176
expCsvFile := filepath .Join (expCatalogDir , testProjectName , csvVersion , csvFileName )
177
+ csvExp := string (readFile (t , expCsvFile ))
175
178
176
179
// Read generated CSV from outputDir path
177
180
csvOutputFile := filepath .Join (outputFromCSVDir , csvVersion , csvFileName )
178
- outputCSV := getCSVFromFile (t , csvOutputFile )
179
- removeSDKLabelsFromCSVHelper (outputCSV )
180
- expCSV := getCSVFromFile (t , expCsvFile )
181
+ csvOutput := string (readFile (t , csvOutputFile ))
181
182
182
- assert .Equal (t , expCSV , outputCSV )
183
+ assert .Equal (t , csvExp , csvOutput )
183
184
}
184
185
185
186
func TestGoCSVNew (t * testing.T ) {
@@ -206,17 +207,11 @@ func TestGoCSVNew(t *testing.T) {
206
207
}
207
208
208
209
csvExpFile := getCSVFileNameLegacy (testProjectName , csvVersion )
210
+ csvExpBytes := readFile (t , filepath .Join (OLMCatalogDir , testProjectName , noUpdateDir , csvExpFile ))
209
211
if b , ok := fileMap [csvExpFile ]; ! ok {
210
212
t .Errorf ("Failed to generate CSV for version %s" , csvVersion )
211
213
} else {
212
- expCSV := getCSVFromFile (t , filepath .Join (OLMCatalogDir , testProjectName , noUpdateDir , csvExpFile ))
213
-
214
- outputCSV , err := generateCSV (b )
215
- if err != nil {
216
- t .Errorf ("Error occurred while generating CSV %v" , err )
217
- }
218
- removeSDKLabelsFromCSVHelper (outputCSV )
219
- assert .Equal (t , expCSV , outputCSV )
214
+ assert .Equal (t , string (csvExpBytes ), string (b ))
220
215
}
221
216
}
222
217
@@ -243,17 +238,11 @@ func TestGoCSVUpdate(t *testing.T) {
243
238
}
244
239
245
240
csvExpFile := getCSVFileNameLegacy (testProjectName , csvVersion )
241
+ csvExpBytes := readFile (t , filepath .Join (OLMCatalogDir , testProjectName , csvVersion , csvExpFile ))
246
242
if b , ok := fileMap [csvExpFile ]; ! ok {
247
243
t .Errorf ("Failed to generate CSV for version %s" , csvVersion )
248
244
} else {
249
- expCSV := getCSVFromFile (t , filepath .Join (OLMCatalogDir , testProjectName , csvVersion , csvExpFile ))
250
-
251
- outputCSV , err := generateCSV (b )
252
- if err != nil {
253
- t .Errorf ("Error occurred while generating CSV, %v" , err )
254
- }
255
- removeSDKLabelsFromCSVHelper (outputCSV )
256
- assert .Equal (t , expCSV , outputCSV )
245
+ assert .Equal (t , string (csvExpBytes ), string (b ))
257
246
}
258
247
}
259
248
@@ -280,17 +269,11 @@ func TestGoCSVUpgrade(t *testing.T) {
280
269
}
281
270
282
271
csvExpFile := getCSVFileNameLegacy (testProjectName , csvVersion )
272
+ csvExpBytes := readFile (t , filepath .Join (OLMCatalogDir , testProjectName , csvVersion , csvExpFile ))
283
273
if b , ok := fileMap [csvExpFile ]; ! ok {
284
274
t .Errorf ("Failed to generate CSV for version %s" , csvVersion )
285
275
} else {
286
- expCSV := getCSVFromFile (t , filepath .Join (OLMCatalogDir , testProjectName , csvVersion , csvExpFile ))
287
-
288
- outputCSV , err := generateCSV (b )
289
- if err != nil {
290
- t .Errorf ("Error occurred while generating CSV, %v" , err )
291
- }
292
- removeSDKLabelsFromCSVHelper (outputCSV )
293
- assert .Equal (t , expCSV , outputCSV )
276
+ assert .Equal (t , string (csvExpBytes ), string (b ))
294
277
}
295
278
}
296
279
@@ -318,16 +301,11 @@ func TestGoCSVNewManifests(t *testing.T) {
318
301
}
319
302
320
303
csvExpFile := getCSVFileNameLegacy (testProjectName , csvVersion )
304
+ csvExpBytes := readFile (t , filepath .Join (OLMCatalogDir , testProjectName , noUpdateDir , csvExpFile ))
321
305
if b , ok := fileMap [getCSVFileName (testProjectName )]; ! ok {
322
306
t .Errorf ("Failed to generate CSV for version %s" , csvVersion )
323
307
} else {
324
- expCSV := getCSVFromFile (t , filepath .Join (OLMCatalogDir , testProjectName , noUpdateDir , csvExpFile ))
325
- outputCSV , err := generateCSV (b )
326
- if err != nil {
327
- t .Errorf ("Error occurred while generating CSV, %v" , err )
328
- }
329
- removeSDKLabelsFromCSVHelper (outputCSV )
330
- assert .Equal (t , expCSV , outputCSV )
308
+ assert .Equal (t , string (csvExpBytes ), string (b ))
331
309
}
332
310
}
333
311
@@ -354,20 +332,11 @@ func TestGoCSVUpdateManifests(t *testing.T) {
354
332
}
355
333
356
334
csvExpFile := getCSVFileNameLegacy (testProjectName , csvVersion )
335
+ csvExpBytes := readFile (t , filepath .Join (OLMCatalogDir , testProjectName , csvVersion , csvExpFile ))
357
336
if b , ok := fileMap [getCSVFileName (testProjectName )]; ! ok {
358
337
t .Errorf ("Failed to generate CSV for version %s" , csvVersion )
359
338
} else {
360
- expCSV := getCSVFromFile (t , filepath .Join (OLMCatalogDir , testProjectName , csvVersion , csvExpFile ))
361
- if err != nil {
362
- t .Errorf ("Error occurred while generating CSV, %v" , err )
363
- }
364
-
365
- outputCSV , err := generateCSV (b )
366
- if err != nil {
367
- t .Errorf ("Error occurred while generating CSV, %v" , err )
368
- }
369
- removeSDKLabelsFromCSVHelper (outputCSV )
370
- assert .Equal (t , expCSV , outputCSV )
339
+ assert .Equal (t , string (csvExpBytes ), string (b ))
371
340
}
372
341
}
373
342
@@ -446,31 +415,6 @@ func TestGoCSVNewWithEmptyDeployDir(t *testing.T) {
446
415
}
447
416
}
448
417
449
- func TestSDKLabels (t * testing.T ) {
450
- cleanupFunc := chDirWithCleanup (t , testGoDataDir )
451
- defer cleanupFunc ()
452
-
453
- b := bases.ClusterServiceVersion {
454
- OperatorName : testProjectName ,
455
- OperatorType : projutil .OperatorTypeGo ,
456
- }
457
-
458
- // check if base CSV has the required stamp values
459
- csv , err := b .GetBase ()
460
- if err != nil {
461
- t .Fatal (err )
462
- }
463
-
464
- annotations := csv .ObjectMeta .Annotations
465
- if _ , ok := annotations [projutil .OperatorBuilder ]; ! ok {
466
- t .Errorf ("CSV base does not contain SDK stamp %s" , projutil .OperatorBuilder )
467
- }
468
-
469
- if _ , ok := annotations [projutil .OperatorLayout ]; ! ok {
470
- t .Errorf ("CSV base does not contain SDK stamp %s" , projutil .OperatorLayout )
471
- }
472
- }
473
-
474
418
func TestUpdateCSVVersion (t * testing.T ) {
475
419
cleanupFunc := chDirWithCleanup (t , testGoDataDir )
476
420
defer cleanupFunc ()
@@ -528,51 +472,3 @@ func TestUpdateCSVVersion(t *testing.T) {
528
472
t .Errorf ("Wanted csv replaces %s, got %s" , wantedReplaces , csv .Spec .Replaces )
529
473
}
530
474
}
531
-
532
- func getCSVFromFile (t * testing.T , path string ) * olmapiv1alpha1.ClusterServiceVersion {
533
- csvpath := filepath .Join (path )
534
- b , err := ioutil .ReadFile (csvpath )
535
- if err != nil {
536
- t .Errorf ("Error reading manifest %s: %v" , path , err )
537
- }
538
-
539
- csv , err := generateCSV (b )
540
- if err != nil {
541
- t .Errorf ("Error generating csv %s: %v" , path , err )
542
- }
543
- return csv
544
- }
545
-
546
- func generateCSV (input []byte ) (* olmapiv1alpha1.ClusterServiceVersion , error ) {
547
- scanner := k8sutil .NewYAMLScanner (bytes .NewBuffer (input ))
548
- for scanner .Scan () {
549
- manifest := scanner .Bytes ()
550
- typeMeta , err := k8sutil .GetTypeMetaFromBytes (manifest )
551
- if err != nil {
552
- log .Debugf ("Skipping non-Object manifest : %v" , err )
553
- continue
554
- }
555
- if typeMeta .Kind == olmapiv1alpha1 .ClusterServiceVersionKind {
556
- csv := & olmapiv1alpha1.ClusterServiceVersion {}
557
- if err := yaml .Unmarshal (manifest , csv ); err != nil {
558
- return nil , fmt .Errorf ("error unmarshalling ClusterServiceVersion from manifest: %v" , err )
559
- }
560
- return csv , nil
561
- }
562
- }
563
- if err := scanner .Err (); err != nil {
564
- return nil , fmt .Errorf ("error scanning manifest %v" , err )
565
- }
566
- return nil , fmt .Errorf ("error getting manifest" )
567
-
568
- }
569
-
570
- // removeSDKLabelsFromCSVHelper removes the sdk labels from CSV struct. Used for test cases where
571
- // we need to test the generated CSV with expected predefined CSV file on disk.
572
- func removeSDKLabelsFromCSVHelper (csv * v1alpha1.ClusterServiceVersion ) {
573
- if _ , exist := csv .ObjectMeta .Annotations [projutil .OperatorBuilder ]; exist {
574
- for label := range projutil .MakeOperatorMetricLabels () {
575
- delete (csv .ObjectMeta .Annotations , label )
576
- }
577
- }
578
- }
0 commit comments