Skip to content

Commit 0726be1

Browse files
UPSTREAM: <carry>: [Default Catalog Tests] - Change logic to get ocp images from openshift/catalogd/manifests.yaml
1 parent 04ee415 commit 0726be1

File tree

3 files changed

+66
-33
lines changed

3 files changed

+66
-33
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: 63 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,101 @@
11
package utils
22

33
import (
4+
"bytes"
45
"fmt"
6+
"io"
57
"io/fs"
68
"os"
79
"path/filepath"
810
"regexp"
911
"strings"
1012

11-
"sigs.k8s.io/yaml"
13+
yamlutil "k8s.io/apimachinery/pkg/util/yaml"
1214

1315
apiv1 "github.com/operator-framework/operator-controller/api/v1"
1416
)
1517

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

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)
22+
info, err := os.Stat(catalogsPath)
23+
if err != nil {
24+
if os.IsNotExist(err) {
25+
return nil, fmt.Errorf("catalogs path %s does not exist", catalogsPath)
26+
}
27+
return nil, err
2528
}
2629

27-
err := filepath.WalkDir(catalogsPath, func(path string, d fs.DirEntry, err error) error {
30+
var images []string
31+
if info.IsDir() {
32+
err = filepath.WalkDir(catalogsPath, func(path string, d fs.DirEntry, err error) error {
33+
if err != nil {
34+
return err
35+
}
36+
37+
if d.IsDir() || (!strings.HasSuffix(d.Name(), ".yaml") &&
38+
!strings.HasSuffix(d.Name(), ".yml")) {
39+
return nil
40+
}
41+
42+
content, err := os.ReadFile(path)
43+
if err != nil {
44+
return nil
45+
}
46+
47+
refs, err := parseClusterCatalogs(content, re)
48+
if err != nil {
49+
return err
50+
}
51+
images = append(images, refs...)
52+
return nil
53+
})
2854
if err != nil {
29-
return err
55+
return nil, err
3056
}
31-
32-
if d.IsDir() || (!strings.HasSuffix(d.Name(), ".yaml") &&
33-
!strings.HasSuffix(d.Name(), ".yml")) {
34-
return nil
57+
} else {
58+
content, err := os.ReadFile(catalogsPath)
59+
if err != nil {
60+
return nil, err
3561
}
36-
37-
content, err := os.ReadFile(path)
62+
refs, err := parseClusterCatalogs(content, re)
3863
if err != nil {
39-
return nil
64+
return nil, err
4065
}
41-
// Replace any helm templating
42-
content = re.ReplaceAll(content, []byte{})
66+
images = append(images, refs...)
67+
}
68+
69+
if len(images) == 0 {
70+
return nil, fmt.Errorf("no images found under catalogs path %s", catalogsPath)
71+
}
4372

73+
return images, nil
74+
}
75+
76+
func parseClusterCatalogs(content []byte, re *regexp.Regexp) ([]string, error) {
77+
// Replace any helm templating
78+
content = re.ReplaceAll(content, []byte{})
79+
80+
decoder := yamlutil.NewYAMLOrJSONDecoder(bytes.NewReader(content), 4096)
81+
82+
var images []string
83+
for {
4484
var catalog apiv1.ClusterCatalog
45-
if err := yaml.Unmarshal(content, &catalog); err != nil {
46-
return nil
85+
if err := decoder.Decode(&catalog); err != nil {
86+
if err == io.EOF {
87+
break
88+
}
89+
return nil, err
4790
}
4891

4992
if catalog.Kind != "ClusterCatalog" {
50-
return nil
93+
continue
5194
}
5295

5396
if catalog.Spec.Source.Type == apiv1.SourceTypeImage && catalog.Spec.Source.Image != nil {
5497
images = append(images, catalog.Spec.Source.Image.Ref)
5598
}
56-
57-
return nil
58-
})
59-
60-
if err != nil {
61-
return nil, err
62-
}
63-
64-
if len(images) == 0 {
65-
return nil, fmt.Errorf("no images found under catalogs path %s", catalogsPath)
6699
}
67100

68101
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)