mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 12:15:52 +00:00
Additional resource quantity testing
Fractional binary SI quantities that cannot be represented as decimal internally were incorrectly calculated.
This commit is contained in:
parent
3d1076ebf3
commit
2d7a9160a6
@ -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
|
||||
|
@ -459,6 +459,7 @@ func (q *Quantity) AsApproximateFloat64() float64 {
|
||||
if exponent == 0 {
|
||||
return base
|
||||
}
|
||||
|
||||
return base * math.Pow10(exponent)
|
||||
}
|
||||
|
||||
|
@ -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),
|
||||
|
Loading…
Reference in New Issue
Block a user