-
Notifications
You must be signed in to change notification settings - Fork 38.7k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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), | ||
) | ||
} | ||
|
||
// Semantic can do semantic deep equality checks for api objects. | ||
// Example: api.Semantic.DeepEqual(aPod, aPodWithNonNilButEmptyMaps) == true | ||
var Semantic = conversion.EqualitiesOrDie( | ||
|
@@ -38,8 +55,12 @@ var Semantic = conversion.EqualitiesOrDie( | |
if b.Amount == nil && a.MilliValue() == 0 { | ||
return true | ||
} | ||
if a.Amount == nil || b.Amount == nil { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can they both be nil? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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... There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it legit to call a method on a nil object? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
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) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.