-
Notifications
You must be signed in to change notification settings - Fork 90
Add release validation tests for image-references #2165
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
openshift-merge-bot
merged 7 commits into
openshift:oadp-dev
from
Joeavaikath:release-image-match-tests
Apr 23, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c48061c
Add release validation tests for image-references
Joeavaikath 59ec707
Add entry in manager.yml
Joeavaikath cdfdd78
Make bundle fix
Joeavaikath c3e99ef
Fix tag
Joeavaikath 3655e8a
Improve tests
Joeavaikath 9f5a6b5
Fix image paths
Joeavaikath 99443bb
gofmt
Joeavaikath File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| package release | ||
|
|
||
| import ( | ||
| "errors" | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "runtime" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func repoRoot(t *testing.T) string { | ||
| t.Helper() | ||
| _, filename, _, ok := runtime.Caller(0) | ||
| if !ok { | ||
| t.Fatal("unable to determine test file path") | ||
| } | ||
| return filepath.Dir(filepath.Dir(filepath.Dir(filename))) | ||
| } | ||
|
|
||
| // branchVersion returns the oadp-X.Y version from the current branch. | ||
| // Checks PULL_BASE_REF first (set by Prow to the PR target branch), | ||
| // then falls back to the local git branch name. | ||
| func branchVersion(t *testing.T) string { | ||
| t.Helper() | ||
| if ref := os.Getenv("PULL_BASE_REF"); strings.HasPrefix(ref, oadpBranchPrefix) { | ||
| return ref | ||
| } | ||
| out, err := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD").Output() | ||
| if err != nil { | ||
| t.Fatalf("failed to get current branch: %v", err) | ||
| } | ||
| branch := strings.TrimSpace(string(out)) | ||
| if !strings.HasPrefix(branch, oadpBranchPrefix) { | ||
| return "" | ||
| } | ||
| return branch | ||
| } | ||
|
|
||
| func readBundleFile(t *testing.T, root, relPath string) []byte { | ||
| t.Helper() | ||
| path := filepath.Join(root, relPath) | ||
| data, err := os.ReadFile(path) | ||
| if err != nil { | ||
| if errors.Is(err, os.ErrNotExist) { | ||
| t.Skipf("%s does not exist (only present on release branches)", relPath) | ||
| } | ||
| t.Fatalf("failed to read %s: %v", relPath, err) | ||
| } | ||
| return data | ||
| } | ||
|
|
||
| func reportErrors(t *testing.T, errs []error) { | ||
| t.Helper() | ||
| for _, err := range errs { | ||
| t.Error(err) | ||
| } | ||
| } | ||
|
|
||
| func assertErrors(t *testing.T, errs []error, wantErrs int, wantMsg string) { | ||
| t.Helper() | ||
| if len(errs) != wantErrs { | ||
| t.Fatalf("got %d errors, want %d: %v", len(errs), wantErrs, errs) | ||
| } | ||
| if wantMsg != "" { | ||
| for _, err := range errs { | ||
| if strings.Contains(err.Error(), wantMsg) { | ||
| return | ||
| } | ||
| } | ||
| t.Errorf("expected error containing %q, got %v", wantMsg, errs) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| package release | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "gopkg.in/yaml.v3" | ||
| ) | ||
|
|
||
| func parseImageReferences(data []byte) (*imageReferencesFile, error) { | ||
| var ir imageReferencesFile | ||
| if err := yaml.Unmarshal(data, &ir); err != nil { | ||
| return nil, fmt.Errorf("failed to parse image-references: %w", err) | ||
| } | ||
| if len(ir.Spec.Tags) == 0 { | ||
| return nil, fmt.Errorf("image-references has no tags") | ||
| } | ||
| return &ir, nil | ||
| } | ||
|
|
||
| func parseCSV(data []byte) (*csv, error) { | ||
| var c csv | ||
| if err := yaml.Unmarshal(data, &c); err != nil { | ||
| return nil, fmt.Errorf("failed to parse CSV: %w", err) | ||
| } | ||
| return &c, nil | ||
| } | ||
|
|
||
| // csvImages extracts RELATED_IMAGE_* env var values and container images | ||
| // from the CSV's deployment specs. | ||
| func csvImages(c *csv) (relatedImages, containerImages map[string]bool) { | ||
| relatedImages = make(map[string]bool) | ||
| containerImages = make(map[string]bool) | ||
| for _, dep := range c.Spec.Install.Spec.Deployments { | ||
| for _, container := range dep.Spec.Template.Spec.Containers { | ||
| containerImages[container.Image] = true | ||
| for _, env := range container.Env { | ||
| if strings.HasPrefix(env.Name, relatedImagePrefix) { | ||
| relatedImages[env.Value] = true | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return | ||
| } | ||
|
|
||
| // irImages extracts the set of image references from image-references tags. | ||
| func irImages(ir *imageReferencesFile) map[string]bool { | ||
| images := make(map[string]bool, len(ir.Spec.Tags)) | ||
| for _, tag := range ir.Spec.Tags { | ||
| images[tag.From.Name] = true | ||
| } | ||
| return images | ||
| } | ||
|
|
||
| // ValidateImageReferencesTagVersion checks that every image tag uses the | ||
| // expected release version (e.g. ":oadp-1.6"), preventing accidental use | ||
| // of tags from a different release stream. | ||
| func ValidateImageReferencesTagVersion(imageRefsData []byte, expectedVersion string, exceptions []string) []error { | ||
| ir, err := parseImageReferences(imageRefsData) | ||
| if err != nil { | ||
| return []error{err} | ||
| } | ||
|
|
||
| skip := make(map[string]bool, len(exceptions)) | ||
| for _, name := range exceptions { | ||
| skip[name] = true | ||
| } | ||
|
|
||
| expectedSuffix := ":" + expectedVersion | ||
| var errs []error | ||
| for _, tag := range ir.Spec.Tags { | ||
| if skip[tag.Name] { | ||
| continue | ||
| } | ||
| if !strings.HasSuffix(tag.From.Name, expectedSuffix) { | ||
| errs = append(errs, fmt.Errorf("image-references tag %q has image %q which does not end with %q", tag.Name, tag.From.Name, expectedSuffix)) | ||
| } | ||
| } | ||
| return errs | ||
| } | ||
|
|
||
| // ValidateImageReferencesMatchCSV ensures every image in image-references has | ||
| // a corresponding RELATED_IMAGE_* env var (or is a container image) in the CSV. | ||
| // This catches images added to image-references but not wired into the operator. | ||
| func ValidateImageReferencesMatchCSV(imageRefsData, csvData []byte) []error { | ||
| ir, err := parseImageReferences(imageRefsData) | ||
| if err != nil { | ||
| return []error{err} | ||
| } | ||
|
|
||
| c, err := parseCSV(csvData) | ||
| if err != nil { | ||
| return []error{err} | ||
| } | ||
|
|
||
| relatedImages, containerImages := csvImages(c) | ||
|
|
||
| var errs []error | ||
| for _, tag := range ir.Spec.Tags { | ||
| dockerImage := tag.From.Name | ||
| if containerImages[dockerImage] { | ||
| continue | ||
| } | ||
| if !relatedImages[dockerImage] { | ||
| errs = append(errs, fmt.Errorf("image-references tag %q has image %q which is not in CSV RELATED_IMAGE_* env vars", tag.Name, dockerImage)) | ||
| } | ||
| } | ||
| return errs | ||
| } | ||
|
|
||
| // ValidateCSVMatchImageReferences ensures every RELATED_IMAGE_* env var in the | ||
| // CSV has a corresponding entry in image-references. This catches orphaned env | ||
| // vars that reference images no longer tracked in image-references. | ||
| func ValidateCSVMatchImageReferences(imageRefsData, csvData []byte) []error { | ||
| ir, err := parseImageReferences(imageRefsData) | ||
| if err != nil { | ||
| return []error{err} | ||
| } | ||
|
|
||
| c, err := parseCSV(csvData) | ||
| if err != nil { | ||
| return []error{err} | ||
| } | ||
|
|
||
| known := irImages(ir) | ||
| relatedImages, _ := csvImages(c) | ||
|
|
||
| var errs []error | ||
| for image := range relatedImages { | ||
| if !known[image] { | ||
| errs = append(errs, fmt.Errorf("CSV RELATED_IMAGE_* has image %q which is not in image-references", image)) | ||
| } | ||
| } | ||
| return errs | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.