Skip to content

Commit 19ee341

Browse files
camilamacedo86ci-robot
authored andcommitted
UPSTREAM: <carry>: [Default Catalog Tests]: Final cleanups and enhancements of initial implementation
1 parent 5f4dac7 commit 19ee341

File tree

7 files changed

+85
-17
lines changed

7 files changed

+85
-17
lines changed
Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,54 @@
1+
# Get the directory where this Makefile is, so we can use it below for including
2+
# Include the same Bingo variables used by the project
3+
DIR := $(strip $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))))
4+
include $(DIR)/../../.bingo/Variables.mk
5+
6+
#SECTION General
7+
8+
# The help target prints out all targets with their descriptions organized
9+
# beneath their categories. The categories are represented by '#SECTION' and the
10+
# target descriptions by '#HELP' or '#EXHELP'. The awk commands is responsible for reading the
11+
# entire set of makefiles included in this invocation, looking for lines of the
12+
# file as xyz: #HELP something, and then pretty-format the target and help. Then,
13+
# if there's a line with #SECTION something, that gets pretty-printed as a category.
14+
# More info on the usage of ANSI control characters for terminal formatting:
15+
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
16+
# More info on the awk command:
17+
# http://linuxcommand.org/lc3_adv_awk.php
18+
# The extended-help target uses '#EXHELP' as the delineator.
19+
20+
.PHONY: help
21+
help: #HELP Display essential help.
22+
@awk 'BEGIN {FS = ":[^#]*#HELP"; printf "\nUsage:\n make \033[36m<target>\033[0m\n\n"} /^[a-zA-Z_0-9-]+:.*#HELP / { printf " \033[36m%-17s\033[0m %s\n", $$1, $$2 } ' $(MAKEFILE_LIST)
23+
24+
#SECTION Tests
25+
126
.PHONY: test-catalog
2-
test-catalog: ## Run the set of tests to validate the quality of catalogs
3-
WHAT=catalog \
27+
test-catalog: #HELP Run the set of tests to validate the quality of catalogs
428
E2E_GINKGO_OPTS="$(if $(ARTIFACT_DIR),--output-dir='$(ARTIFACT_DIR)') --junit-report junit_e2e.xml" \
529
go test -count=1 -v ./test/validate/...;
30+
31+
#SECTION Development
32+
33+
.PHONY: verify #HELP To verify the code
34+
verify: tidy fmt vet lint
35+
36+
.PHONY: tidy #HELP Run go mod tidy.
37+
tidy:
38+
go mod tidy
39+
40+
.PHONY: fmt
41+
fmt: #HELP Run go fmt against code.
42+
go fmt ./...
43+
44+
.PHONY: vet
45+
vet: #HELP Run go vet against code.
46+
go vet ./...
47+
48+
.PHONY: lint
49+
lint: $(GOLANGCI_LINT) #HELP Run golangci linter.
50+
$(GOLANGCI_LINT) run
51+
52+
.PHONY: fix-lint
53+
fix-lint: $(GOLANGCI_LINT) #HELP Fix lint issues
54+
$(GOLANGCI_LINT) run --fix

openshift/default-catalog-consistency/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,13 @@ Those tests are used to check the consistency of the default catalogs in OpenShi
77

88
```bash
99
make test-catalog
10+
```
11+
12+
### Before Pushing Changes
13+
14+
Before pushing changes to the repository, you should verify that the tests pass.
15+
This can be done by running:
16+
17+
```bash
18+
make verify
1019
```

openshift/default-catalog-consistency/pkg/check/check.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"github/operator-framework-operator-controller/openshift/default-catalog-consistency/pkg/extract"
87
"io/fs"
98
"os"
109
"path/filepath"
1110

1211
specsgov1 "github.com/opencontainers/image-spec/specs-go/v1"
13-
"github.com/operator-framework/operator-registry/alpha/declcfg"
1412
"oras.land/oras-go/v2"
13+
14+
"github.com/operator-framework/operator-registry/alpha/declcfg"
15+
16+
"github/operator-framework-operator-controller/openshift/default-catalog-consistency/pkg/extract"
1517
)
1618

1719
// Checks is a collection of checks to be performed on the catalog image.

openshift/default-catalog-consistency/pkg/check/image.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"strings"
1010

1111
"github.com/containers/image/v5/manifest"
12-
1312
specsgov1 "github.com/opencontainers/image-spec/specs-go/v1"
1413
"oras.land/oras-go/v2"
1514
)
@@ -57,7 +56,7 @@ func ImageHasLabels(expectedLabels map[string]string) ImageCheck {
5756
}
5857
}
5958

60-
var pairs []string
59+
pairs := make([]string, 0, len(expectedLabels))
6160
for k, v := range expectedLabels {
6261
pairs = append(pairs, fmt.Sprintf("%s=%s", k, v))
6362
}

openshift/default-catalog-consistency/pkg/extract/extract.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ func (r *ExtractedImage) Cleanup() {
4141
}
4242

4343
// UnpackImage pulls the image, extracts it to disk, and opens it as an OCI store.
44-
func UnpackImage(ctx context.Context, imageRef, name string, sysCtx *types.SystemContext) (res *ExtractedImage, err error) {
44+
func UnpackImage(ctx context.Context, imageRef, name string, sysCtx *types.SystemContext) (*ExtractedImage, error) {
4545
tmpDir, err := os.MkdirTemp("", fmt.Sprintf("oci-%s-", name))
4646
if err != nil {
4747
return nil, fmt.Errorf("create temp dir: %w", err)
4848
}
4949

5050
var digestTag string
5151

52-
res, err = func() (*ExtractedImage, error) {
52+
extracted, err := func() (*ExtractedImage, error) {
5353
srcRef, err := docker.ParseReference("//" + imageRef)
5454
if err != nil {
5555
return nil, fmt.Errorf("parse image ref: %w", err)
@@ -59,7 +59,12 @@ func UnpackImage(ctx context.Context, imageRef, name string, sysCtx *types.Syste
5959
if err != nil {
6060
return nil, fmt.Errorf("create policy context: %w", err)
6161
}
62-
defer policyCtx.Destroy()
62+
// Ensure policy context is cleaned up properly
63+
defer func() {
64+
if err := policyCtx.Destroy(); err != nil {
65+
fmt.Printf("unable to destroy policy context: %s", err)
66+
}
67+
}()
6368

6469
canonicalRef, err := resolveCanonicalRef(ctx, srcRef, sysCtx)
6570
if err != nil {
@@ -115,11 +120,13 @@ func UnpackImage(ctx context.Context, imageRef, name string, sysCtx *types.Syste
115120
}()
116121

117122
if err != nil {
118-
os.RemoveAll(tmpDir)
123+
if err := os.RemoveAll(tmpDir); err != nil {
124+
fmt.Printf("failed to remove temp dir: %v\n", err)
125+
}
119126
return nil, err
120127
}
121128

122-
return res, nil
129+
return extracted, nil
123130
}
124131

125132
// extractLayers extracts the filesystem layers from the OCI image layout under the given digest tag.
@@ -166,8 +173,9 @@ func extractLayers(ctx context.Context, layoutPath, fsPath, tag string) error {
166173

167174
_, err := archive.Apply(ctx, fsPath, decompress, archive.WithFilter(func(hdr *tar.Header) (bool, error) {
168175
// Clean up extended headers and enforce safe permissions
176+
// This configuration allow to extract the image layers
177+
// without the need of root permissions in CI environments
169178
hdr.PAXRecords = nil
170-
hdr.Xattrs = nil
171179
hdr.Uid = os.Getuid()
172180
hdr.Gid = os.Getgid()
173181
if hdr.FileInfo().IsDir() {
@@ -183,10 +191,9 @@ func extractLayers(ctx context.Context, layoutPath, fsPath, tag string) error {
183191
return nil
184192
}()
185193
if err != nil {
186-
return fmt.Errorf("decompress layer %d: %w", i, err)
194+
return err
187195
}
188196
}
189-
190197
return nil
191198
}
192199

@@ -229,7 +236,7 @@ func loadPolicyContext(sourceContext *types.SystemContext, imageRef string) (*si
229236
// if we need to validate the image signature then we will need to
230237
// change it.
231238
if err != nil {
232-
fmt.Println(fmt.Sprintf("no default policy found for (%s), using insecure policy", imageRef))
239+
fmt.Printf("no default policy found for (%s), using insecure policy \n", imageRef)
233240
insecurePolicy := []byte(`{
234241
"default": [{"type": "insecureAcceptAnything"}]
235242
}`)

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import (
55
"io/fs"
66
"os"
77
"path/filepath"
8-
"sigs.k8s.io/yaml"
98
"strings"
109

10+
"sigs.k8s.io/yaml"
11+
1112
apiv1 "github.com/operator-framework/operator-controller/api/v1"
1213
)
1314

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package validate
33
import (
44
"context"
55
"fmt"
6-
"github.com/containers/image/v5/types"
76
"os"
87
"testing"
98

109
. "github.com/onsi/ginkgo/v2"
1110
. "github.com/onsi/gomega"
1211

12+
"github.com/containers/image/v5/types"
13+
1314
"github/operator-framework-operator-controller/openshift/default-catalog-consistency/pkg/check"
1415
"github/operator-framework-operator-controller/openshift/default-catalog-consistency/pkg/extract"
1516
"github/operator-framework-operator-controller/openshift/default-catalog-consistency/test/utils"

0 commit comments

Comments
 (0)