Make SetDefaults of Destination duck type nil safer (#2670)

Signed-off-by: Christoph Stäbler <cstabler@redhat.com>

Signed-off-by: Christoph Stäbler <cstabler@redhat.com>
This commit is contained in:
Christoph Stäbler 2023-01-25 09:36:39 +01:00 committed by GitHub
parent 247510c00e
commit 408ad0773f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 6 deletions

View File

@ -73,6 +73,10 @@ func (d *Destination) GetRef() *KReference {
}
func (d *Destination) SetDefaults(ctx context.Context) {
if d == nil {
return
}
if d.Ref != nil && d.Ref.Namespace == "" {
d.Ref.Namespace = apis.ParentMeta(ctx).Namespace
}

View File

@ -179,7 +179,10 @@ func TestDestinationSetDefaults(t *testing.T) {
d *Destination
ctx context.Context
want string
}{"uri set, nothing in ref, not modified ": {
}{"destination nil ": {
d: nil,
ctx: ctx,
}, "uri set, nothing in ref, not modified ": {
d: &Destination{URI: apis.HTTP("example.com")},
ctx: ctx,
}, "namespace set, nothing in context, not modified ": {
@ -206,11 +209,13 @@ func TestDestinationSetDefaults(t *testing.T) {
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
tc.d.SetDefaults(tc.ctx)
if tc.d.Ref != nil && tc.d.Ref.Namespace != tc.want {
t.Errorf("Got: %s wanted %s", tc.d.Ref.Namespace, tc.want)
}
if tc.d.Ref == nil && tc.want != "" {
t.Error("Got: nil Ref wanted", tc.want)
if tc.d != nil {
if tc.d.Ref != nil && tc.d.Ref.Namespace != tc.want {
t.Errorf("Got: %s wanted %s", tc.d.Ref.Namespace, tc.want)
}
if tc.d.Ref == nil && tc.want != "" {
t.Error("Got: nil Ref wanted", tc.want)
}
}
})
}