Skip to content

Commit 1f66d6f

Browse files
Merge pull request #521 from camilamacedo86/change-location
OPRUN-4199: [Default Catalog Tests] - Change logic to get ocp images from openshift/catalogd/manifests.yaml
2 parents a15517d + 139ba75 commit 1f66d6f

File tree

3 files changed

+75
-28
lines changed

3 files changed

+75
-28
lines changed

openshift/default-catalog-consistency/go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ require (
1010
github.com/opencontainers/image-spec v1.1.1
1111
github.com/operator-framework/operator-controller v1.3.0
1212
github.com/operator-framework/operator-registry v1.55.0
13+
k8s.io/apimachinery v0.33.2
1314
oras.land/oras-go/v2 v2.6.0
14-
sigs.k8s.io/yaml v1.5.0
1515
)
1616

1717
require (
@@ -130,7 +130,6 @@ require (
130130
gopkg.in/warnings.v0 v0.1.2 // indirect
131131
gopkg.in/yaml.v3 v3.0.1 // indirect
132132
k8s.io/api v0.33.2 // indirect
133-
k8s.io/apimachinery v0.33.2 // indirect
134133
k8s.io/client-go v0.33.2 // indirect
135134
k8s.io/klog/v2 v2.130.1 // indirect
136135
k8s.io/kube-openapi v0.0.0-20250701173324-9bd5c66d9911 // indirect
@@ -139,4 +138,5 @@ require (
139138
sigs.k8s.io/json v0.0.0-20241014173422-cfa47c3a1cc8 // indirect
140139
sigs.k8s.io/randfill v1.0.0 // indirect
141140
sigs.k8s.io/structured-merge-diff/v4 v4.7.0 // indirect
141+
sigs.k8s.io/yaml v1.5.0 // indirect
142142
)

openshift/default-catalog-consistency/test/utils/utils.go

Lines changed: 72 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,115 @@
11
package utils
22

33
import (
4+
"bytes"
5+
"errors"
46
"fmt"
7+
"io"
58
"io/fs"
69
"os"
710
"path/filepath"
811
"regexp"
912
"strings"
1013

11-
"sigs.k8s.io/yaml"
14+
yamlutil "k8s.io/apimachinery/pkg/util/yaml"
1215

1316
apiv1 "github.com/operator-framework/operator-controller/api/v1"
1417
)
1518

1619
// ParseImageRefsFromCatalog reads the catalogs from the files used and returns a list of image references.
1720
func ParseImageRefsFromCatalog(catalogsPath string) ([]string, error) {
18-
var images []string
19-
2021
re := regexp.MustCompile(`{{.*}}`)
2122

22-
// Check if the directory exists first
23-
if _, err := os.Stat(catalogsPath); os.IsNotExist(err) {
24-
return nil, fmt.Errorf("catalogs path %s does not exist", catalogsPath)
23+
info, err := os.Stat(catalogsPath)
24+
if err != nil {
25+
if os.IsNotExist(err) {
26+
return nil, fmt.Errorf("catalogs path %s does not exist", catalogsPath)
27+
}
28+
return nil, err
2529
}
2630

27-
err := filepath.WalkDir(catalogsPath, func(path string, d fs.DirEntry, err error) error {
28-
if err != nil {
29-
return err
30-
}
31+
var (
32+
images []string
33+
)
3134

32-
if d.IsDir() || (!strings.HasSuffix(d.Name(), ".yaml") &&
33-
!strings.HasSuffix(d.Name(), ".yml")) {
34-
return nil
35-
}
35+
if info.IsDir() {
36+
images, err = parseCatalogDir(catalogsPath, re)
37+
} else {
38+
images, err = parseCatalogFile(catalogsPath, re)
39+
}
40+
if err != nil {
41+
return nil, err
42+
}
3643

37-
content, err := os.ReadFile(path)
44+
if len(images) == 0 {
45+
return nil, fmt.Errorf("no images found under catalogs path %s", catalogsPath)
46+
}
47+
48+
return images, nil
49+
}
50+
51+
func parseCatalogDir(root string, re *regexp.Regexp) ([]string, error) {
52+
var images []string
53+
54+
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
3855
if err != nil {
39-
return nil
56+
return err
4057
}
41-
// Replace any helm templating
42-
content = re.ReplaceAll(content, []byte{})
4358

44-
var catalog apiv1.ClusterCatalog
45-
if err := yaml.Unmarshal(content, &catalog); err != nil {
59+
if d.IsDir() {
4660
return nil
4761
}
4862

49-
if catalog.Kind != "ClusterCatalog" {
63+
if !strings.HasSuffix(d.Name(), ".yaml") && !strings.HasSuffix(d.Name(), ".yml") {
5064
return nil
5165
}
5266

53-
if catalog.Spec.Source.Type == apiv1.SourceTypeImage && catalog.Spec.Source.Image != nil {
54-
images = append(images, catalog.Spec.Source.Image.Ref)
67+
refs, err := parseCatalogFile(path, re)
68+
if err != nil {
69+
return err
5570
}
5671

72+
images = append(images, refs...)
5773
return nil
5874
})
75+
if err != nil {
76+
return nil, err
77+
}
78+
79+
return images, nil
80+
}
5981

82+
func parseCatalogFile(path string, re *regexp.Regexp) ([]string, error) {
83+
content, err := os.ReadFile(path)
6084
if err != nil {
6185
return nil, err
6286
}
87+
return parseClusterCatalogs(content, re)
88+
}
6389

64-
if len(images) == 0 {
65-
return nil, fmt.Errorf("no images found under catalogs path %s", catalogsPath)
90+
func parseClusterCatalogs(content []byte, re *regexp.Regexp) ([]string, error) {
91+
// Replace any helm templating
92+
content = re.ReplaceAll(content, []byte{})
93+
94+
decoder := yamlutil.NewYAMLOrJSONDecoder(bytes.NewReader(content), 4096)
95+
96+
var images []string
97+
for {
98+
var catalog apiv1.ClusterCatalog
99+
if err := decoder.Decode(&catalog); err != nil {
100+
if errors.Is(err, io.EOF) {
101+
break
102+
}
103+
return nil, err
104+
}
105+
106+
if catalog.Kind != "ClusterCatalog" {
107+
continue
108+
}
109+
110+
if catalog.Spec.Source.Type == apiv1.SourceTypeImage && catalog.Spec.Source.Image != nil {
111+
images = append(images, catalog.Spec.Source.Image.Ref)
112+
}
66113
}
67114

68115
return images, nil

openshift/default-catalog-consistency/test/validate/suite_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func TestSuite(t *testing.T) {
2323
}
2424

2525
var _ = Describe("OLM-Catalog-Validation", func() {
26-
catalogsPath := "../../../../helm/olmv1/templates/openshift-catalogs"
26+
catalogsPath := "../../../catalogd/manifests.yaml"
2727
images, err := utils.ParseImageRefsFromCatalog(catalogsPath)
2828
Expect(err).ToNot(HaveOccurred())
2929
Expect(images).ToNot(BeEmpty(), "no images found")

0 commit comments

Comments
 (0)