This is an automated email from the ASF dual-hosted git repository.
mani pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/yunikorn-k8shim.git
The following commit(s) were added to refs/heads/master by this push:
new d7c6ffd9 [YUNIKORN-592] test: move the testing function into the
relevant test code file (#662)
d7c6ffd9 is described below
commit d7c6ffd9700fe093c3817c8adb2ce1412994be32
Author: Cliff Su <[email protected]>
AuthorDate: Mon Aug 28 13:21:20 2023 +0530
[YUNIKORN-592] test: move the testing function into the relevant test code
file (#662)
Closes: #662
Signed-off-by: Manikandan R <[email protected]>
---
pkg/cache/context_recovery_test.go | 32 ++++++++++++-----
pkg/cache/node_coordinator_test.go | 71 +++++++++++++++++++++++++++++++-------
pkg/common/utils/utils.go | 62 ---------------------------------
3 files changed, 82 insertions(+), 83 deletions(-)
diff --git a/pkg/cache/context_recovery_test.go
b/pkg/cache/context_recovery_test.go
index 99abb29f..6b638d7a 100644
--- a/pkg/cache/context_recovery_test.go
+++ b/pkg/cache/context_recovery_test.go
@@ -26,6 +26,7 @@ import (
"gotest.tools/v3/assert"
v1 "k8s.io/api/core/v1"
+ "k8s.io/apimachinery/pkg/api/resource"
apis "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
@@ -36,6 +37,19 @@ import (
"github.com/apache/yunikorn-k8shim/pkg/dispatcher"
)
+type K8sResource struct {
+ ResourceName v1.ResourceName
+ Value int64
+}
+
+func NewK8sResourceList(resources ...K8sResource)
map[v1.ResourceName]resource.Quantity {
+ resourceList := make(map[v1.ResourceName]resource.Quantity)
+ for _, r := range resources {
+ resourceList[r.ResourceName] = *resource.NewQuantity(r.Value,
resource.DecimalSI)
+ }
+ return resourceList
+}
+
func TestNodeRecoveringState(t *testing.T) {
apiProvider4test := client.NewMockedAPIProvider(false)
context := NewContext(apiProvider4test)
@@ -50,11 +64,11 @@ func TestNodeRecoveringState(t *testing.T) {
UID: "uid_0001",
},
Status: v1.NodeStatus{
- Capacity: utils.NewK8sResourceList(
- utils.K8sResource{
+ Capacity: NewK8sResourceList(
+ K8sResource{
ResourceName: v1.ResourceMemory,
Value: 1024,
- }, utils.K8sResource{
+ }, K8sResource{
ResourceName: v1.ResourceCPU,
Value: 10,
}),
@@ -68,11 +82,11 @@ func TestNodeRecoveringState(t *testing.T) {
UID: "uid_0002",
},
Status: v1.NodeStatus{
- Capacity: utils.NewK8sResourceList(
- utils.K8sResource{
+ Capacity: NewK8sResourceList(
+ K8sResource{
ResourceName: v1.ResourceMemory,
Value: 1024,
- }, utils.K8sResource{
+ }, K8sResource{
ResourceName: v1.ResourceCPU,
Value: 10,
}),
@@ -119,11 +133,11 @@ func TestNodesRecovery(t *testing.T) {
UID: types.UID("uid_000" +
strconv.Itoa(i)),
},
Status: v1.NodeStatus{
- Capacity: utils.NewK8sResourceList(
- utils.K8sResource{
+ Capacity: NewK8sResourceList(
+ K8sResource{
ResourceName: v1.ResourceMemory,
Value: 1024,
- }, utils.K8sResource{
+ }, K8sResource{
ResourceName: v1.ResourceCPU,
Value: 10,
}),
diff --git a/pkg/cache/node_coordinator_test.go
b/pkg/cache/node_coordinator_test.go
index 0246fe43..68a03f7a 100644
--- a/pkg/cache/node_coordinator_test.go
+++ b/pkg/cache/node_coordinator_test.go
@@ -23,9 +23,9 @@ import (
"gotest.tools/v3/assert"
v1 "k8s.io/api/core/v1"
+ "k8s.io/apimachinery/pkg/api/resource"
apis "k8s.io/apimachinery/pkg/apis/meta/v1"
- "github.com/apache/yunikorn-k8shim/pkg/common/utils"
siCommon "github.com/apache/yunikorn-scheduler-interface/lib/go/common"
"github.com/apache/yunikorn-scheduler-interface/lib/go/si"
)
@@ -36,19 +36,66 @@ const (
HostEmpty = ""
)
+func PodForTest(podName, memory, cpu string) *v1.Pod {
+ containers := make([]v1.Container, 0)
+ c1Resources := make(map[v1.ResourceName]resource.Quantity)
+ c1Resources[v1.ResourceMemory] = resource.MustParse(memory)
+ c1Resources[v1.ResourceCPU] = resource.MustParse(cpu)
+ containers = append(containers, v1.Container{
+ Name: "container-01",
+ Resources: v1.ResourceRequirements{
+ Requests: c1Resources,
+ },
+ })
+
+ return &v1.Pod{
+ TypeMeta: apis.TypeMeta{
+ Kind: "Pod",
+ APIVersion: "v1",
+ },
+ ObjectMeta: apis.ObjectMeta{
+ Name: podName,
+ },
+ Spec: v1.PodSpec{
+ Containers: containers,
+ },
+ }
+}
+
+func NodeForTest(nodeID, memory, cpu string) *v1.Node {
+ resourceList := make(map[v1.ResourceName]resource.Quantity)
+ resourceList[v1.ResourceName("memory")] = resource.MustParse(memory)
+ resourceList[v1.ResourceName("cpu")] = resource.MustParse(cpu)
+ return &v1.Node{
+ TypeMeta: apis.TypeMeta{
+ Kind: "Node",
+ APIVersion: "v1",
+ },
+ ObjectMeta: apis.ObjectMeta{
+ Name: nodeID,
+ Namespace: "default",
+ UID: "uid_0001",
+ },
+ Spec: v1.NodeSpec{},
+ Status: v1.NodeStatus{
+ Allocatable: resourceList,
+ },
+ }
+}
+
func TestUpdatePod(t *testing.T) {
mockedSchedulerApi := newMockSchedulerAPI()
nodes := newSchedulerNodes(mockedSchedulerApi, NewTestSchedulerCache())
- host1 := utils.NodeForTest(Host1, "10G", "10")
- host2 := utils.NodeForTest(Host2, "10G", "10")
+ host1 := NodeForTest(Host1, "10G", "10")
+ host2 := NodeForTest(Host2, "10G", "10")
nodes.addNode(host1)
nodes.addNode(host2)
coordinator := newNodeResourceCoordinator(nodes)
// pod is not assigned to any node
// this won't trigger an update
- pod1 := utils.PodForTest("pod1", "1G", "500m")
- pod2 := utils.PodForTest("pod1", "1G", "500m")
+ pod1 := PodForTest("pod1", "1G", "500m")
+ pod2 := PodForTest("pod1", "1G", "500m")
pod1.Status.Phase = v1.PodPending
pod1.Status.Phase = v1.PodPending
pod1.Spec.NodeName = ""
@@ -188,14 +235,14 @@ func TestUpdatePod(t *testing.T) {
func TestDeletePod(t *testing.T) {
mockedSchedulerApi := newMockSchedulerAPI()
nodes := newSchedulerNodes(mockedSchedulerApi, NewTestSchedulerCache())
- host1 := utils.NodeForTest(Host1, "10G", "10")
+ host1 := NodeForTest(Host1, "10G", "10")
nodes.addNode(host1)
coordinator := newNodeResourceCoordinator(nodes)
// pod from pending to running
// occupied resources should be added to the node
- pod1 := utils.PodForTest("pod1", "1G", "500m")
- pod2 := utils.PodForTest("pod1", "1G", "500m")
+ pod1 := PodForTest("pod1", "1G", "500m")
+ pod2 := PodForTest("pod1", "1G", "500m")
pod1.Status.Phase = v1.PodPending
pod2.Status.Phase = v1.PodRunning
pod1.Spec.NodeName = HostEmpty
@@ -236,14 +283,14 @@ func TestDeletePod(t *testing.T) {
func TestDeleteTerminatedPod(t *testing.T) {
mockedSchedulerApi := newMockSchedulerAPI()
nodes := newSchedulerNodes(mockedSchedulerApi, NewTestSchedulerCache())
- host1 := utils.NodeForTest(Host1, "10G", "10")
+ host1 := NodeForTest(Host1, "10G", "10")
nodes.addNode(host1)
coordinator := newNodeResourceCoordinator(nodes)
// pod from pending to running
// occupied resources should be added to the node
- pod1 := utils.PodForTest("pod1", "1G", "500m")
- pod2 := utils.PodForTest("pod1", "1G", "500m")
+ pod1 := PodForTest("pod1", "1G", "500m")
+ pod2 := PodForTest("pod1", "1G", "500m")
pod1.Status.Phase = v1.PodPending
pod2.Status.Phase = v1.PodRunning
pod1.Spec.NodeName = HostEmpty
@@ -300,7 +347,7 @@ func TestDeleteTerminatedPod(t *testing.T) {
func TestNodeCoordinatorFilterPods(t *testing.T) {
mockedSchedulerAPI := newMockSchedulerAPI()
nodes := newSchedulerNodes(mockedSchedulerAPI, NewTestSchedulerCache())
- host1 := utils.NodeForTest(Host1, "10G", "10")
+ host1 := NodeForTest(Host1, "10G", "10")
nodes.addNode(host1)
coordinator := newNodeResourceCoordinator(nodes)
diff --git a/pkg/common/utils/utils.go b/pkg/common/utils/utils.go
index e31dbf5a..df69b9d0 100644
--- a/pkg/common/utils/utils.go
+++ b/pkg/common/utils/utils.go
@@ -30,8 +30,6 @@ import (
v1 "k8s.io/api/core/v1"
schedulingv1 "k8s.io/api/scheduling/v1"
- "k8s.io/apimachinery/pkg/api/resource"
- apis "k8s.io/apimachinery/pkg/apis/meta/v1"
podv1 "k8s.io/kubernetes/pkg/api/v1/pod"
"github.com/apache/yunikorn-k8shim/pkg/appmgmt/interfaces"
@@ -190,19 +188,6 @@ func GetNamespaceQuotaFromAnnotation(namespaceObj
*v1.Namespace) *si.Resource {
}
}
-type K8sResource struct {
- ResourceName v1.ResourceName
- Value int64
-}
-
-func NewK8sResourceList(resources ...K8sResource)
map[v1.ResourceName]resource.Quantity {
- resourceList := make(map[v1.ResourceName]resource.Quantity)
- for _, r := range resources {
- resourceList[r.ResourceName] = *resource.NewQuantity(r.Value,
resource.DecimalSI)
- }
- return resourceList
-}
-
func WaitForCondition(eval func() bool, interval time.Duration, timeout
time.Duration) error {
deadline := time.Now().Add(timeout)
for {
@@ -218,53 +203,6 @@ func WaitForCondition(eval func() bool, interval
time.Duration, timeout time.Dur
}
}
-func PodForTest(podName, memory, cpu string) *v1.Pod {
- containers := make([]v1.Container, 0)
- c1Resources := make(map[v1.ResourceName]resource.Quantity)
- c1Resources[v1.ResourceMemory] = resource.MustParse(memory)
- c1Resources[v1.ResourceCPU] = resource.MustParse(cpu)
- containers = append(containers, v1.Container{
- Name: "container-01",
- Resources: v1.ResourceRequirements{
- Requests: c1Resources,
- },
- })
-
- return &v1.Pod{
- TypeMeta: apis.TypeMeta{
- Kind: "Pod",
- APIVersion: "v1",
- },
- ObjectMeta: apis.ObjectMeta{
- Name: podName,
- },
- Spec: v1.PodSpec{
- Containers: containers,
- },
- }
-}
-
-func NodeForTest(nodeID, memory, cpu string) *v1.Node {
- resourceList := make(map[v1.ResourceName]resource.Quantity)
- resourceList[v1.ResourceName("memory")] = resource.MustParse(memory)
- resourceList[v1.ResourceName("cpu")] = resource.MustParse(cpu)
- return &v1.Node{
- TypeMeta: apis.TypeMeta{
- Kind: "Node",
- APIVersion: "v1",
- },
- ObjectMeta: apis.ObjectMeta{
- Name: nodeID,
- Namespace: "default",
- UID: "uid_0001",
- },
- Spec: v1.NodeSpec{},
- Status: v1.NodeStatus{
- Allocatable: resourceList,
- },
- }
-}
-
// merge two string maps
// if the same key defined in the first and second maps
// the value will be set by the second map
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]