Skip to content

Commit

Permalink
Allow separate additional repository validations
Browse files Browse the repository at this point in the history
Signed-off-by: Atanas Dinov <[email protected]>
  • Loading branch information
atanasdinov committed Apr 16, 2024
1 parent 6a1dad0 commit 55b44a6
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 36 deletions.
43 changes: 19 additions & 24 deletions pkg/image/validation/os.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,16 +182,6 @@ func validateSuma(os *image.OperatingSystem) []FailedValidation {
func validatePackages(os *image.OperatingSystem) []FailedValidation {
var failures []FailedValidation

if len(os.Packages.PKGList) == 0 {
if len(os.Packages.AdditionalRepos) > 0 {
failures = append(failures, FailedValidation{
UserMessage: "The 'packageList' field is required if any entries are specified under 'additionalRepos'.",
})
}

return failures
}

if slices.Contains(os.Packages.PKGList, "") {
failures = append(failures, FailedValidation{
UserMessage: "The 'packageList' field cannot contain empty values.",
Expand All @@ -206,24 +196,29 @@ func validatePackages(os *image.OperatingSystem) []FailedValidation {
})
}

var repoURLs []string
// It is possible to only provide `additionalRepos` without listing any packages
// under `packageList` in the cases where RPMs are side-loaded under the `/rpms` directory.
if len(os.Packages.AdditionalRepos) > 0 {
var repoURLs []string

for _, repo := range os.Packages.AdditionalRepos {
if repo.URL == "" {
msg := "The 'url' field is required for all entries under 'additionalRepos'."
failures = append(failures, FailedValidation{
UserMessage: msg,
})
}

repoURLs = append(repoURLs, repo.URL)
}

for _, repo := range os.Packages.AdditionalRepos {
if repo.URL == "" {
if duplicates := findDuplicates(repoURLs); len(duplicates) > 0 {
duplicateValues := strings.Join(duplicates, ", ")
msg := fmt.Sprintf("The 'additionalRepos' field contains duplicate repos: %s", duplicateValues)
failures = append(failures, FailedValidation{
UserMessage: "The 'url' field is required for all entries under 'additionalRepos'.",
UserMessage: msg,
})
}

repoURLs = append(repoURLs, repo.URL)
}

if duplicates := findDuplicates(repoURLs); len(duplicates) > 0 {
duplicateValues := strings.Join(duplicates, ", ")
msg := fmt.Sprintf("The 'additionalRepos' field contains duplicate repos: %s", duplicateValues)
failures = append(failures, FailedValidation{
UserMessage: msg,
})
}

return failures
Expand Down
12 changes: 0 additions & 12 deletions pkg/image/validation/os_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -486,18 +486,6 @@ func TestPackages(t *testing.T) {
"The 'packageList' field cannot contain empty values.",
},
},
`missing packages`: {
Packages: image.Packages{
AdditionalRepos: []image.AddRepo{
{
URL: "foo",
},
},
},
ExpectedFailedMessages: []string{
"The 'packageList' field is required if any entries are specified under 'additionalRepos'.",
},
},
`duplicate packages`: {
Packages: image.Packages{
PKGList: []string{"foo", "bar", "foo", "bar", "baz"},
Expand Down

0 comments on commit 55b44a6

Please sign in to comment.