This is an automated email from the ASF dual-hosted git repository.
pbacsko pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/yunikorn-core.git
The following commit(s) were added to refs/heads/master by this push:
new de774081 [YUNIKORN-2649] Improve CalculateAbsUsedCapacity &
CompUsageRatio funtion's test coverage (#880)
de774081 is described below
commit de77408168efc0123d2c4b43465c042619f6a87b
Author: SP12893678 <[email protected]>
AuthorDate: Fri May 31 15:23:25 2024 +0200
[YUNIKORN-2649] Improve CalculateAbsUsedCapacity & CompUsageRatio funtion's
test coverage (#880)
Closes: #880
Signed-off-by: Peter Bacsko <[email protected]>
---
pkg/common/resources/resources_test.go | 109 +++++++++++++++++++++++++++++++++
1 file changed, 109 insertions(+)
diff --git a/pkg/common/resources/resources_test.go
b/pkg/common/resources/resources_test.go
index a24d0f8a..d134101f 100644
--- a/pkg/common/resources/resources_test.go
+++ b/pkg/common/resources/resources_test.go
@@ -1205,6 +1205,110 @@ func TestGetShares(t *testing.T) {
}
}
+func TestCompUsageRatio(t *testing.T) {
+ tests := []struct {
+ left *Resource
+ right *Resource
+ total *Resource
+ expected int
+ message string
+ }{
+ {
+ left: nil,
+ right: nil,
+ total: nil,
+ expected: 0,
+ message: "nil resources",
+ },
+ {
+ left: NewResource(),
+ right: NewResource(),
+ total: nil,
+ expected: 0,
+ message: "empty resource with total nil",
+ },
+ {
+ left: NewResource(),
+ right: NewResource(),
+ total: NewResource(),
+ expected: 0,
+ message: "empty resources",
+ },
+ {
+ left:
NewResourceFromMap(map[string]Quantity{"zero": 0}),
+ right:
NewResourceFromMap(map[string]Quantity{"zero": 0}),
+ total: nil,
+ expected: 0,
+ message: "zero valued resource",
+ },
+ {
+ left:
NewResourceFromMap(map[string]Quantity{"large": 5, "zero": 0, "small": -5}),
+ right:
NewResourceFromMap(map[string]Quantity{"large": 5, "zero": 0, "small": -5}),
+ total: nil,
+ expected: 0,
+ message: "negative valued resource",
+ },
+ {
+ left:
NewResourceFromMap(map[string]Quantity{"zero": 0}),
+ right:
NewResourceFromMap(map[string]Quantity{"zero": 0}),
+ total: NewResource(),
+ expected: 0,
+ message: "zero valued resource with total",
+ },
+ {
+ left:
NewResourceFromMap(map[string]Quantity{"large": 5, "zero": 0, "small": -5}),
+ right:
NewResourceFromMap(map[string]Quantity{"large": 5, "zero": 0, "small": -5}),
+ total:
NewResourceFromMap(map[string]Quantity{"large": 5, "zero": 0, "small": -5}),
+ expected: 0,
+ message: "same resource and total",
+ },
+ {
+ left:
NewResourceFromMap(map[string]Quantity{"large": 5, "zero": 0, "small": -5}),
+ right:
NewResourceFromMap(map[string]Quantity{"large": 5, "zero": 0}),
+ total:
NewResourceFromMap(map[string]Quantity{"large": 10, "zero": 10}),
+ expected: -1,
+ message: "left side has more one negative value",
+ },
+ {
+ left:
NewResourceFromMap(map[string]Quantity{"large": 5, "zero": 0}),
+ right:
NewResourceFromMap(map[string]Quantity{"large": 5, "zero": 0, "small": -5}),
+ total:
NewResourceFromMap(map[string]Quantity{"large": 10, "zero": 10}),
+ expected: 1,
+ message: "right side has more one negative value",
+ },
+ {
+ left:
NewResourceFromMap(map[string]Quantity{"first": 10, "second": 5}),
+ right:
NewResourceFromMap(map[string]Quantity{"first": 5, "second": 10}),
+ total:
NewResourceFromMap(map[string]Quantity{"first": 15}),
+ expected: -1,
+ message: "left side first one bigger, last one
smaller",
+ },
+ {
+ left:
NewResourceFromMap(map[string]Quantity{"first": 5, "second": 10}),
+ right:
NewResourceFromMap(map[string]Quantity{"first": 10, "second": 5}),
+ total:
NewResourceFromMap(map[string]Quantity{"first": 15}),
+ expected: 1,
+ message: "left side first one smaller, last one
bigger",
+ },
+ {
+ left:
NewResourceFromMap(map[string]Quantity{"second": 10, "first": 5}),
+ right:
NewResourceFromMap(map[string]Quantity{"first": 10, "second": 5}),
+ total: NewResourceFromMap(map[string]Quantity{}),
+ expected: 0,
+ message: "left side key order not same as right side",
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.message, func(t *testing.T) {
+ ratio := CompUsageRatio(tc.left, tc.right, tc.total)
+ if ratio != tc.expected {
+ t.Errorf("incorrect ratio for %s, expected %v
got: %v", tc.message, tc.expected, ratio)
+ }
+ })
+ }
+}
+
func TestCompareShares(t *testing.T) {
tests := []struct {
left []float64
@@ -1642,6 +1746,11 @@ func TestCalculateAbsUsedCapacity(t *testing.T) {
used:
NewResourceFromMap(map[string]Quantity{"memory": math.MaxInt64}),
expected:
NewResourceFromMap(map[string]Quantity{"memory": 100}),
},
+ "multiple resources not in used": {
+ capacity:
NewResourceFromMap(map[string]Quantity{"memory": 1024, "vcores": 2, "gpu": 1}),
+ used:
NewResourceFromMap(map[string]Quantity{"memory": 512}),
+ expected:
NewResourceFromMap(map[string]Quantity{"memory": 50}),
+ },
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]