Skip to content
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

Fix for multiple WithRemote options #3982

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pkg/oci/remote/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,14 @@ func WithRemoteOptions(opts ...remote.Option) Option {
}
}

// WithMoreRemoteOptions is a functional option for adding to the default
// remote options already specified
func WithMoreRemoteOptions(opts ...remote.Option) Option {
return func(o *options) {
o.ROpt = append(o.ROpt, opts...)
}
}

// WithTargetRepository is a functional option for overriding the default
// target repository hosting the signature and attestation tags.
func WithTargetRepository(repo name.Repository) Option {
Expand Down
68 changes: 67 additions & 1 deletion pkg/oci/remote/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package remote

import (
"context"
"errors"
"os"
"reflect"
Expand All @@ -42,6 +43,10 @@ func TestOptions(t *testing.T) {
// TODO(mattmoor): Incorporate user agent.
}

moreROpt := []remote.Option{
remote.WithContext(context.Background()),
}

tests := []struct {
name string
opts []Option
Expand Down Expand Up @@ -105,14 +110,24 @@ func TestOptions(t *testing.T) {
TargetRepository: repo,
ROpt: otherROpt,
},
}, {
name: "more remote options option",
opts: []Option{WithRemoteOptions(otherROpt...), WithMoreRemoteOptions(moreROpt...)},
want: &options{
SignatureSuffix: SignatureTagSuffix,
AttestationSuffix: AttestationTagSuffix,
SBOMSuffix: SBOMTagSuffix,
TargetRepository: repo,
ROpt: append(append([]remote.Option{}, otherROpt...), moreROpt...),
},
}}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := makeOptions(repo, test.opts...)
test.want.OriginalOptions = test.opts

if !reflect.DeepEqual(got, test.want) {
if !optionsEqual(got, test.want) {
t.Errorf("makeOptions() = %#v, wanted %#v", got, test.want)
}
})
Expand Down Expand Up @@ -170,3 +185,54 @@ func TestGetEnvTargetRepository(t *testing.T) {
})
}
}

// this is required due to the fact that reflect.DeepEquals reports
// two different slices of function points, with identical length and
// contents at each position as being different
func optionsEqual(o1, o2 *options) bool {
if (o1 == nil) != (o2 == nil) {
return false
}
if o1 == nil {
return true
}

if o1.AttestationSuffix != o2.AttestationSuffix {
return false
}
if o1.SignatureSuffix != o2.SignatureSuffix {
return false
}
if o1.SBOMSuffix != o2.SBOMSuffix {
return false
}
if o1.TagPrefix != o2.TagPrefix {
return false
}
if !slicesEqual(o1.ROpt, o2.ROpt) {
return false
}
if !slicesEqual(o1.NameOpts, o2.NameOpts) {
return false
}
if !slicesEqual(o1.OriginalOptions, o2.OriginalOptions) {
return false
}
return true
}

func slicesEqual[T any](o1, o2 []T) bool {
if len(o1) != len(o2) {
return false
}

for i := range o1 {
v1 := reflect.ValueOf(o1[i])
v2 := reflect.ValueOf(o2[i])
if v1 != v2 {
return false
}
}

return true
}
Loading