Improve quota describe output

This commit is contained in:
derekwaynecarr 2015-06-01 23:29:23 -04:00
parent 1845ca88fc
commit 6968a4e0df
3 changed files with 60 additions and 1 deletions

View File

@ -190,7 +190,9 @@ func ParseQuantity(str string) (*Quantity, error) {
// of an amount.
// Arguably, this should be inf.RoundHalfUp (normal rounding), but
// that would have the side effect of rounding values < .5m to zero.
amount.Round(amount, 3, inf.RoundUp)
if v, ok := amount.Unscaled(); v != int64(0) || !ok {
amount.Round(amount, 3, inf.RoundUp)
}
// The max is just a simple cap.
if amount.Cmp(maxAllowed) > 0 {
@ -237,6 +239,11 @@ func (q *Quantity) Canonicalize() (string, suffix) {
return "0", ""
}
// zero is zero always
if q.Amount.Cmp(&inf.Dec{}) == 0 {
return "0", ""
}
format := q.Format
switch format {
case DecimalExponent, DecimalSI:

View File

@ -57,6 +57,26 @@ func TestDec(t *testing.T) {
}
}
// TestQuantityParseZero ensures that when a 0 quantity is passed, its string value is 0
func TestQuantityParseZero(t *testing.T) {
zero := MustParse("0")
if expected, actual := "0", zero.String(); expected != actual {
t.Errorf("Expected %v, actual %v", expected, actual)
}
}
// Verifies that you get 0 as canonical value if internal value is 0, and not 0<suffix>
func TestQuantityCanocicalizeZero(t *testing.T) {
val := MustParse("1000m")
x := val.Amount
y := dec(1, 0)
z := val.Amount.Sub(x, y)
zero := Quantity{z, DecimalSI}
if expected, actual := "0", zero.String(); expected != actual {
t.Errorf("Expected %v, actual %v", expected, actual)
}
}
func TestQuantityParse(t *testing.T) {
table := []struct {
input string

View File

@ -20,9 +20,41 @@ import (
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/resource"
versioned "github.com/GoogleCloudPlatform/kubernetes/pkg/api/v1beta3"
)
func TestResourceQuotaStatusConversion(t *testing.T) {
// should serialize as "0"
expected := resource.NewQuantity(int64(0), resource.DecimalSI)
if "0" != expected.String() {
t.Errorf("Expected: 0, Actual: %v, do not require units", expected.String())
}
parsed := resource.MustParse("0")
if "0" != parsed.String() {
t.Errorf("Expected: 0, Actual: %v, do not require units", parsed.String())
}
quota := &api.ResourceQuota{}
quota.Status = api.ResourceQuotaStatus{}
quota.Status.Hard = api.ResourceList{}
quota.Status.Used = api.ResourceList{}
quota.Status.Hard[api.ResourcePods] = *expected
// round-trip the object
data, _ := versioned.Codec.Encode(quota)
object, _ := versioned.Codec.Decode(data)
after := object.(*api.ResourceQuota)
actualQuantity := after.Status.Hard[api.ResourcePods]
actual := &actualQuantity
// should be "0", but was "0m"
if expected.String() != actual.String() {
t.Errorf("Expected %v, Actual %v", expected.String(), actual.String())
}
}
func TestNodeConversion(t *testing.T) {
obj, err := versioned.Codec.Decode([]byte(`{"kind":"Minion","apiVersion":"v1beta3"}`))
if err != nil {