Additional resource quantity testing

Fractional binary SI quantities that cannot be represented as decimal
internally were incorrectly calculated.
This commit is contained in:
Clayton Coleman 2021-09-03 12:22:01 -04:00 committed by TAGAMI Yukihiro
parent 3d1076ebf3
commit 2d7a9160a6
3 changed files with 37 additions and 2 deletions

View File

@ -71,7 +71,7 @@ func Test_podResourceCollector_Handler(t *testing.T) {
"custom": resource.MustParse("0"),
},
Limits: v1.ResourceList{
"memory": resource.MustParse("2G"),
"memory": resource.MustParse("2.5Gi"),
"custom": resource.MustParse("6"),
},
}},
@ -95,7 +95,7 @@ func Test_podResourceCollector_Handler(t *testing.T) {
expected := `# HELP kube_pod_resource_limit [ALPHA] Resources limit for workloads on the cluster, broken down by pod. This shows the resource usage the scheduler and kubelet expect per pod for resources along with the unit for the resource if any.
# TYPE kube_pod_resource_limit gauge
kube_pod_resource_limit{namespace="test",node="node-one",pod="foo",priority="",resource="custom",scheduler="",unit=""} 6
kube_pod_resource_limit{namespace="test",node="node-one",pod="foo",priority="",resource="memory",scheduler="",unit="bytes"} 2e+09
kube_pod_resource_limit{namespace="test",node="node-one",pod="foo",priority="",resource="memory",scheduler="",unit="bytes"} 2.68435456e+09
# HELP kube_pod_resource_request [ALPHA] Resources requested by workloads on the cluster, broken down by pod. This shows the resource usage the scheduler and kubelet expect per pod for resources along with the unit for the resource if any.
# TYPE kube_pod_resource_request gauge
kube_pod_resource_request{namespace="test",node="node-one",pod="foo",priority="",resource="cpu",scheduler="",unit="cores"} 2

View File

@ -459,6 +459,7 @@ func (q *Quantity) AsApproximateFloat64() float64 {
if exponent == 0 {
return base
}
return base * math.Pow10(exponent)
}

View File

@ -1260,6 +1260,40 @@ func TestQuantityAsApproximateFloat64(t *testing.T) {
}
}
func TestStringQuantityAsApproximateFloat64(t *testing.T) {
table := []struct {
in string
out float64
}{
{"2Ki", 2048},
{"1.1Ki", 1126.4e+0},
{"1Mi", 1.048576e+06},
{"2Gi", 2.147483648e+09},
}
for _, item := range table {
t.Run(item.in, func(t *testing.T) {
in, err := ParseQuantity(item.in)
if err != nil {
t.Fatal(err)
}
out := in.AsApproximateFloat64()
if out != item.out {
t.Fatalf("expected %v, got %v", item.out, out)
}
if in.d.Dec != nil {
if i, ok := in.AsInt64(); ok {
q := intQuantity(i, 0, in.Format)
out := q.AsApproximateFloat64()
if out != item.out {
t.Fatalf("as int quantity: expected %v, got %v", item.out, out)
}
}
}
})
}
}
func benchmarkQuantities() []Quantity {
return []Quantity{
intQuantity(1024*1024*1024, 0, BinarySI),