Some nit cleanups (#1040)

This commit is contained in:
Victor Agababov 2020-02-03 16:56:30 -08:00 committed by GitHub
parent 41aec11a3c
commit 0264117c2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 108 additions and 135 deletions

View File

@ -33,7 +33,7 @@ const (
) )
func TestValidateDestination(t *testing.T) { func TestValidateDestination(t *testing.T) {
ctx := context.TODO() ctx := context.Background()
validRef := KReference{ validRef := KReference{
Kind: kind, Kind: kind,
@ -50,88 +50,74 @@ func TestValidateDestination(t *testing.T) {
tests := map[string]struct { tests := map[string]struct {
dest *Destination dest *Destination
want string want string
}{ }{"nil valid": {
"nil valid": { dest: nil,
dest: nil, }, "valid ref": {
want: "", dest: &Destination{
Ref: &validRef,
}, },
"valid ref": { }, "invalid ref, missing name": {
dest: &Destination{ dest: &Destination{
Ref: &validRef, Ref: &KReference{
}, Namespace: namespace,
want: "", Kind: kind,
}, APIVersion: apiVersion,
"invalid ref, missing name": {
dest: &Destination{
Ref: &KReference{
Namespace: namespace,
Kind: kind,
APIVersion: apiVersion,
},
},
want: "missing field(s): ref.name",
},
"invalid ref, missing api version": {
dest: &Destination{
Ref: &KReference{
Namespace: namespace,
Kind: kind,
Name: name,
},
},
want: "missing field(s): ref.apiVersion",
},
"invalid ref, missing kind": {
dest: &Destination{
Ref: &KReference{
Namespace: namespace,
APIVersion: apiVersion,
Name: name,
},
},
want: "missing field(s): ref.kind",
},
"valid uri": {
dest: &Destination{
URI: &validURL,
}, },
}, },
"invalid, uri has no host": { want: "missing field(s): ref.name",
dest: &Destination{ }, "invalid ref, missing api version": {
URI: &apis.URL{ dest: &Destination{
Scheme: "http", Ref: &KReference{
}, Namespace: namespace,
}, Kind: kind,
want: "invalid value: Relative URI is not allowed when Ref and [apiVersion, kind, name] is absent: uri", Name: name,
},
"invalid, uri is not absolute url": {
dest: &Destination{
URI: &apis.URL{
Host: "host",
},
},
want: "invalid value: Relative URI is not allowed when Ref and [apiVersion, kind, name] is absent: uri",
},
"invalid, both uri and ref, uri is absolute URL": {
dest: &Destination{
URI: &validURL,
Ref: &validRef,
},
want: "Absolute URI is not allowed when Ref or [apiVersion, kind, name] is present: [apiVersion, kind, name], ref, uri",
},
"invalid, both ref, [apiVersion, kind, name] and uri are nil": {
dest: &Destination{},
want: "expected at least one, got none: ref, uri",
},
"valid, both uri and ref, uri is not a absolute URL": {
dest: &Destination{
URI: &apis.URL{
Path: "/handler",
},
Ref: &validRef,
}, },
}, },
} want: "missing field(s): ref.apiVersion",
}, "invalid ref, missing kind": {
dest: &Destination{
Ref: &KReference{
Namespace: namespace,
APIVersion: apiVersion,
Name: name,
},
},
want: "missing field(s): ref.kind",
}, "valid uri": {
dest: &Destination{
URI: &validURL,
},
}, "invalid, uri has no host": {
dest: &Destination{
URI: &apis.URL{
Scheme: "http",
},
},
want: "invalid value: Relative URI is not allowed when Ref and [apiVersion, kind, name] is absent: uri",
}, "invalid, uri is not absolute url": {
dest: &Destination{
URI: &apis.URL{
Host: "host",
},
},
want: "invalid value: Relative URI is not allowed when Ref and [apiVersion, kind, name] is absent: uri",
}, "invalid, both uri and ref, uri is absolute URL": {
dest: &Destination{
URI: &validURL,
Ref: &validRef,
},
want: "Absolute URI is not allowed when Ref or [apiVersion, kind, name] is present: [apiVersion, kind, name], ref, uri",
}, "invalid, both ref, [apiVersion, kind, name] and uri are nil": {
dest: &Destination{},
want: "expected at least one, got none: ref, uri",
}, "valid, both uri and ref, uri is not a absolute URL": {
dest: &Destination{
URI: &apis.URL{
Path: "/handler",
},
Ref: &validRef,
},
}}
for name, tc := range tests { for name, tc := range tests {
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
@ -148,7 +134,7 @@ func TestValidateDestination(t *testing.T) {
} }
} }
func TestDestination_GetRef(t *testing.T) { func TestDestinationGetRef(t *testing.T) {
ref := &KReference{ ref := &KReference{
APIVersion: apiVersion, APIVersion: apiVersion,
Kind: kind, Kind: kind,
@ -157,26 +143,22 @@ func TestDestination_GetRef(t *testing.T) {
tests := map[string]struct { tests := map[string]struct {
dest *Destination dest *Destination
want *KReference want *KReference
}{ }{"nil destination": {
"nil destination": { dest: nil,
dest: nil, want: nil,
want: nil, }, "uri": {
}, dest: &Destination{
"uri": { URI: &apis.URL{
dest: &Destination{ Host: "foo",
URI: &apis.URL{
Host: "foo",
},
}, },
want: nil,
}, },
"ref": { want: nil,
dest: &Destination{ }, "ref": {
Ref: ref, dest: &Destination{
}, Ref: ref,
want: ref,
}, },
} want: ref,
}}
for n, tc := range tests { for n, tc := range tests {
t.Run(n, func(t *testing.T) { t.Run(n, func(t *testing.T) {
@ -191,44 +173,36 @@ func TestDestination_GetRef(t *testing.T) {
func TestDestinationSetDefaults(t *testing.T) { func TestDestinationSetDefaults(t *testing.T) {
ctx := context.Background() ctx := context.Background()
parentNamespace := "parentNamespace" const parentNamespace = "parentNamespace"
tests := map[string]struct { tests := map[string]struct {
d *Destination d *Destination
ctx context.Context ctx context.Context
want string want string
}{ }{"uri set, nothing in ref, not modified ": {
"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 ": {
want: "", d: &Destination{Ref: &KReference{Namespace: namespace}},
}, ctx: ctx,
"namespace set, nothing in context, not modified ": { want: namespace,
d: &Destination{Ref: &KReference{Namespace: namespace}}, }, "namespace set, context set, not modified ": {
ctx: ctx, d: &Destination{Ref: &KReference{Namespace: namespace}},
want: namespace, ctx: apis.WithinParent(ctx, metav1.ObjectMeta{Namespace: parentNamespace}),
}, want: namespace,
"namespace set, context set, not modified ": { }, "namespace set, uri set, context set, not modified ": {
d: &Destination{Ref: &KReference{Namespace: namespace}}, d: &Destination{Ref: &KReference{Namespace: namespace}, URI: apis.HTTP("example.com")},
ctx: apis.WithinParent(ctx, metav1.ObjectMeta{Namespace: parentNamespace}), ctx: apis.WithinParent(ctx, metav1.ObjectMeta{Namespace: parentNamespace}),
want: namespace, want: namespace,
}, }, "namespace not set, context set, defaulted": {
"namespace set, uri set, context set, not modified ": { d: &Destination{Ref: &KReference{}},
d: &Destination{Ref: &KReference{Namespace: namespace}, URI: apis.HTTP("example.com")}, ctx: apis.WithinParent(ctx, metav1.ObjectMeta{Namespace: parentNamespace}),
ctx: apis.WithinParent(ctx, metav1.ObjectMeta{Namespace: parentNamespace}), want: parentNamespace,
want: namespace, }, "namespace not set, uri set, context set, defaulted": {
}, d: &Destination{Ref: &KReference{}, URI: apis.HTTP("example.com")},
"namespace not set, context set, defaulted": { ctx: apis.WithinParent(ctx, metav1.ObjectMeta{Namespace: parentNamespace}),
d: &Destination{Ref: &KReference{}}, want: parentNamespace,
ctx: apis.WithinParent(ctx, metav1.ObjectMeta{Namespace: parentNamespace}), }}
want: parentNamespace,
},
"namespace not set, uri set, context set, defaulted": {
d: &Destination{Ref: &KReference{}, URI: apis.HTTP("example.com")},
ctx: apis.WithinParent(ctx, metav1.ObjectMeta{Namespace: parentNamespace}),
want: parentNamespace,
},
}
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)

View File

@ -46,10 +46,9 @@ type KReference struct {
func (kr *KReference) Validate(ctx context.Context) *apis.FieldError { func (kr *KReference) Validate(ctx context.Context) *apis.FieldError {
var errs *apis.FieldError var errs *apis.FieldError
if kr == nil { if kr == nil {
errs = errs.Also(apis.ErrMissingField("name")) return errs.Also(apis.ErrMissingField("name")).
errs = errs.Also(apis.ErrMissingField("apiVersion")) Also(apis.ErrMissingField("apiVersion")).
errs = errs.Also(apis.ErrMissingField("kind")) Also(apis.ErrMissingField("kind"))
return errs
} }
if kr.Name == "" { if kr.Name == "" {
errs = errs.Also(apis.ErrMissingField("name")) errs = errs.Also(apis.ErrMissingField("name"))

View File

@ -95,7 +95,7 @@ func TestValidate(t *testing.T) {
func TestKReferenceSetDefaults(t *testing.T) { func TestKReferenceSetDefaults(t *testing.T) {
ctx := context.Background() ctx := context.Background()
parentNamespace := "parentNamespace" const parentNamespace = "parentNamespace"
tests := map[string]struct { tests := map[string]struct {
ref *KReference ref *KReference
@ -122,7 +122,7 @@ func TestKReferenceSetDefaults(t *testing.T) {
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
tc.ref.SetDefaults(tc.ctx) tc.ref.SetDefaults(tc.ctx)
if tc.ref.Namespace != tc.want { if tc.ref.Namespace != tc.want {
t.Errorf("Got: %s wanted %s", tc.ref.Namespace, tc.want) t.Errorf("Namespace = %s; want: %s", tc.ref.Namespace, tc.want)
} }
}) })
} }