mirror of https://github.com/knative/pkg.git
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:
parent
247510c00e
commit
408ad0773f
|
@ -73,6 +73,10 @@ func (d *Destination) GetRef() *KReference {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *Destination) SetDefaults(ctx context.Context) {
|
func (d *Destination) SetDefaults(ctx context.Context) {
|
||||||
|
if d == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if d.Ref != nil && d.Ref.Namespace == "" {
|
if d.Ref != nil && d.Ref.Namespace == "" {
|
||||||
d.Ref.Namespace = apis.ParentMeta(ctx).Namespace
|
d.Ref.Namespace = apis.ParentMeta(ctx).Namespace
|
||||||
}
|
}
|
||||||
|
|
|
@ -179,7 +179,10 @@ func TestDestinationSetDefaults(t *testing.T) {
|
||||||
d *Destination
|
d *Destination
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
want string
|
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")},
|
d: &Destination{URI: apis.HTTP("example.com")},
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
}, "namespace set, nothing in context, not modified ": {
|
}, "namespace set, nothing in context, not modified ": {
|
||||||
|
@ -206,11 +209,13 @@ func TestDestinationSetDefaults(t *testing.T) {
|
||||||
for name, tc := range tests {
|
for name, tc := range tests {
|
||||||
t.Run(name, func(t *testing.T) {
|
t.Run(name, func(t *testing.T) {
|
||||||
tc.d.SetDefaults(tc.ctx)
|
tc.d.SetDefaults(tc.ctx)
|
||||||
if tc.d.Ref != nil && tc.d.Ref.Namespace != tc.want {
|
if tc.d != nil {
|
||||||
t.Errorf("Got: %s wanted %s", tc.d.Ref.Namespace, tc.want)
|
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.Ref == nil && tc.want != "" {
|
||||||
|
t.Error("Got: nil Ref wanted", tc.want)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue