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 error messages; add tests #3351

Merged
merged 1 commit into from
Jan 9, 2015
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions pkg/api/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,29 @@ limitations under the License.
package api

import (
"reflect"
"strings"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api/resource"
"github.com/GoogleCloudPlatform/kubernetes/pkg/conversion"

"github.com/davecgh/go-spew/spew"
)

// Conversion error conveniently packages up errors in conversions.
type ConversionError struct {
In, Out interface{}
Message string
}

// Return a helpful string about the error
func (c *ConversionError) Error() string {
return spew.Sprintf(
"Conversion error: %s. (in: %v(%+v) out: %v)",
c.Message, reflect.TypeOf(c.In), c.In, reflect.TypeOf(c.Out),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you just say %T rather than using reflect?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, didn't know that one--doesn't seem to work with spew, though.

)
}

// Semantic can do semantic deep equality checks for api objects.
// Example: api.Semantic.DeepEqual(aPod, aPodWithNonNilButEmptyMaps) == true
var Semantic = conversion.EqualitiesOrDie(
Expand All @@ -38,8 +55,12 @@ var Semantic = conversion.EqualitiesOrDie(
if b.Amount == nil && a.MilliValue() == 0 {
return true
}
if a.Amount == nil || b.Amount == nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can they both be nil?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It only takes one nil to cause a crash on the next line...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's subtle-- if both are nil, the first if will catch it; MilliValue() is safe to call and returns zero in that case.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it legit to call a method on a nil object?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that a & b can't be nil. I made quantities safe to use in an uninitialized state.

return false
}
return a.Amount.Cmp(b.Amount) == 0
},
pullPoliciesEqual,
)

// TODO: Address these per #1502
Expand Down
71 changes: 71 additions & 0 deletions pkg/api/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Copyright 2015 Google Inc. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package api

import (
"strings"
"testing"

"github.com/GoogleCloudPlatform/kubernetes/pkg/api/resource"

"speter.net/go/exp/math/dec/inf"
)

func TestConversionError(t *testing.T) {
var i int
var s string
i = 3
s = "foo"
c := ConversionError{
In: &i, Out: &s,
Message: "Can't make x into y, silly",
}
var e error
e = &c // ensure it implements error
msg := e.Error()
t.Logf("Message is %v", msg)
for _, part := range []string{"3", "int", "string", "Can't"} {
if !strings.Contains(msg, part) {
t.Errorf("didn't find %v", part)
}
}
}

func TestSemantic(t *testing.T) {
table := []struct {
a, b interface{}
shouldEqual bool
}{
{resource.MustParse("0"), resource.Quantity{}, true},
{resource.Quantity{}, resource.MustParse("0"), true},
{resource.Quantity{}, resource.MustParse("1m"), false},
{
resource.Quantity{inf.NewDec(5, 0), resource.BinarySI},
resource.Quantity{inf.NewDec(5, 0), resource.DecimalSI},
true,
},
{resource.MustParse("2m"), resource.MustParse("1m"), false},
{PullPolicy("NEVER"), PullPolicy("neveR"), true},
{PullPolicy("NEVER"), PullPolicy("neveRi"), false},
}

for index, item := range table {
if e, a := item.shouldEqual, Semantic.DeepEqual(item.a, item.b); e != a {
t.Errorf("expected %v, got %v.", index, e, a)
}
}
}
24 changes: 19 additions & 5 deletions pkg/api/v1beta1/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package v1beta1

import (
"errors"
"fmt"
"strconv"

Expand Down Expand Up @@ -234,7 +233,11 @@ func init() {
case newer.PodUnknown:
*out = PodUnknown
default:
return errors.New("The string provided is not a valid PodPhase constant value")
return &newer.ConversionError{
In: in,
Out: out,
Message: "The string provided is not a valid PodPhase constant value",
}
}

return nil
Expand All @@ -256,7 +259,11 @@ func init() {
case PodUnknown:
*out = newer.PodUnknown
default:
return errors.New("The string provided is not a valid PodPhase constant value")
return &newer.ConversionError{
In: in,
Out: out,
Message: "The string provided is not a valid PodPhase constant value",
}
}
return nil
},
Expand Down Expand Up @@ -349,7 +356,11 @@ func init() {
return err
}
if in.TemplateRef != nil && in.Template == nil {
return errors.New("objects with a template ref cannot be converted to older objects, must populate template")
return &newer.ConversionError{
In: in,
Out: out,
Message: "objects with a template ref cannot be converted to older objects, must populate template",
}
}
if in.Template != nil {
if err := s.Convert(in.Template, &out.PodTemplate, 0); err != nil {
Expand Down Expand Up @@ -616,7 +627,10 @@ func init() {
for k, v := range *in {
fv, err := strconv.ParseFloat(v.String(), 64)
if err != nil {
return fmt.Errorf("value '%v' of '%v': %v", v, k, err)
return &newer.ConversionError{
In: in, Out: out,
Message: fmt.Sprintf("value '%v' of '%v': %v", v, k, err),
}
}
if k == ResourceCPU {
(*out)[newer.ResourceCPU] = *resource.NewMilliQuantity(int64(fv*1000), resource.DecimalSI)
Expand Down
24 changes: 19 additions & 5 deletions pkg/api/v1beta2/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package v1beta2

import (
"errors"
"fmt"
"strconv"

Expand Down Expand Up @@ -122,7 +121,11 @@ func init() {
case newer.PodUnknown:
*out = PodUnknown
default:
return errors.New("The string provided is not a valid PodPhase constant value")
return &newer.ConversionError{
In: in,
Out: out,
Message: "The string provided is not a valid PodPhase constant value",
}
}

return nil
Expand All @@ -144,7 +147,11 @@ func init() {
case PodUnknown:
*out = newer.PodUnknown
default:
return errors.New("The string provided is not a valid PodPhase constant value")
return &newer.ConversionError{
In: in,
Out: out,
Message: "The string provided is not a valid PodPhase constant value",
}
}
return nil
},
Expand Down Expand Up @@ -237,7 +244,11 @@ func init() {
return err
}
if in.TemplateRef != nil && in.Template == nil {
return errors.New("objects with a template ref cannot be converted to older objects, must populate template")
return &newer.ConversionError{
In: in,
Out: out,
Message: "objects with a template ref cannot be converted to older objects, must populate template",
}
}
if in.Template != nil {
if err := s.Convert(in.Template, &out.PodTemplate, 0); err != nil {
Expand Down Expand Up @@ -532,7 +543,10 @@ func init() {
for k, v := range *in {
fv, err := strconv.ParseFloat(v.String(), 64)
if err != nil {
return fmt.Errorf("value '%v' of '%v': %v", v, k, err)
return &newer.ConversionError{
In: in, Out: out,
Message: fmt.Sprintf("value '%v' of '%v': %v", v, k, err),
}
}
if k == ResourceCPU {
(*out)[newer.ResourceCPU] = *resource.NewMilliQuantity(int64(fv*1000), resource.DecimalSI)
Expand Down