def test_new_task_info(self, pod: Pod): task_info = job_info.TaskInfo.new(pod) assert task_info == job_info.TaskInfo.parse_obj({ "uid": "05196752-598e-4d10-bf50-a9226d14c514", "job": "default/demo-job", "name": "demo", "namespace": "default", "resource_requests": Resource.new({ "cpu": "30m", "memory": "300Mi" }), "init_resource_requests": Resource.new({ "cpu": "1", "memory": "300Mi" }), "node_name": "", "status": TaskStatus.Unknown, "priority": 1, "volume_ready": False, "pod": pod, })
def test_add_task_info(self): pod1 = build_pod("ns", "p1", "", "Pending", { "cpu": "1000m", "memory": "1G" }) task1 = TaskInfo.new(pod1) pod2 = build_pod("ns", "p2", "n1", "Running", { "cpu": "2000m", "memory": "2G" }) task2 = TaskInfo.new(pod2) pod3 = build_pod("ns", "p3", "n1", "Pending", { "cpu": "1000m", "memory": "1G" }) task3 = TaskInfo.new(pod3) pod4 = build_pod("ns", "p4", "n1", "Pending", { "cpu": "1000m", "memory": "1G" }) task4 = TaskInfo.new(pod4) job = job_info.JobInfo(uid="job1") for task in [task1, task2, task3, task4]: job.add_task_info(task) assert job == JobInfo( uid="job1", allocated=Resource.new({ "cpu": "4000m", "memory": "4G" }), total_request=Resource.new({ "cpu": "5000m", "memory": "5G" }), tasks={ task1.uid: task1, task2.uid: task2, task3.uid: task3, task4.uid: task4, }, task_status_index={ TaskStatus.Pending: { task1.uid: task1 }, TaskStatus.Running: { task2.uid: task2 }, TaskStatus.Bound: { task3.uid: task3, task4.uid: task4 }, }, )
def test_get(resource_name: str, quantity: ResourceQuantity): resource = Resource( milli_cpu=ResourceQuantity(100), memory=ResourceQuantity(100), scalar_resources={"nvidia.com/gpu": ResourceQuantity(100)}, ) result = resource.get(resource_name) assert result == quantity
def test_get_pod_resource_request(self, pod: Pod, empty_pod: Pod): assert job_info.get_pod_resource_request(pod) == Resource.new({ "cpu": "1", "memory": "300Mi" }) assert job_info.get_pod_resource_request(empty_pod) == Resource()
def test_get_pod_resource_without_init_container(self, pod: Pod, empty_pod: Pod): assert job_info.get_pod_resource_without_init_container( pod) == Resource.new({ "cpu": "30m", "memory": "300Mi" }) assert job_info.get_pod_resource_without_init_container( empty_pod) == Resource()
def test_set_node(): node = build_node("n1", {"cpu": "8000m", "memory": "10G"}) pod1 = build_pod( "c1", "p1", "n1", PodPhase.Running, { "cpu": "1000m", "memory": "1G" }, ) pod2 = build_pod( "c1", "p2", "n1", PodPhase.Running, { "cpu": "2000m", "memory": "2G" }, ) node_info = NodeInfo.new(node) node_info.add_task(TaskInfo.new(pod1)) node_info.add_task(TaskInfo.new(pod2)) new_node = build_node("n2", {"cpu": "5000m", "memory": "5G"}) node_info.set_node(new_node) assert node_info == NodeInfo( name="n2", node=new_node, allocatable=Resource.new({ "cpu": "5000m", "memory": "5G" }), capability=Resource.new({ "cpu": "5000m", "memory": "5G" }), idle=Resource.new({ "cpu": "2000m", "memory": "2G" }), used=Resource.new({ "cpu": "3000m", "memory": "3G" }), state={"phase": "Ready"}, tasks={ "c1/p1": TaskInfo.new(pod1), "c1/p2": TaskInfo.new(pod2) }, )
def test_delete_task_info(self): pod1 = build_pod("ns", "p1", "", "Pending", { "cpu": "1000m", "memory": "1G" }) task1 = TaskInfo.new(pod1) pod2 = build_pod("ns", "p2", "n1", "Running", { "cpu": "2000m", "memory": "2G" }) task2 = TaskInfo.new(pod2) pod3 = build_pod("ns", "p3", "n1", "Running", { "cpu": "3000m", "memory": "3G" }) task3 = TaskInfo.new(pod3) job = JobInfo(uid="job") for task in [task1, task2, task3]: job.add_task_info(task) job.delete_task_info(task2) assert job == JobInfo( uid="job", allocated=Resource.new({ "cpu": "3000m", "memory": "3G" }), total_request=Resource.new({ "cpu": "4000m", "memory": "4G" }), tasks={ task1.uid: task1, task3.uid: task3 }, task_status_index={ TaskStatus.Pending: { task1.uid: task1 }, TaskStatus.Running: { task3.uid: task3 }, }, ) with pytest.raises(job_info.FailedToFindTask): job.delete_task_info(task2)
def test_resource_names(): resource = Resource.new({ "cpu": "10m", "memory": "10Mi", "nvidia.com/gpu": "1Gi" }) assert resource.resource_names == ["cpu", "memory", "nvidia.com/gpu"]
def test_string(): resource = Resource.new({ "cpu": "200m", "memory": "20Mi", "nvidia.com/gpu": "1Gi" }) assert str( resource ) == "cpu 200.000, memory 20971520, nvidia.com/gpu 1073741824000"
def test_add_unknown_pod(): node = build_node("n1", {"cpu": "2000m", "memory": "1G"}) pod = build_pod("c1", "p1", "n1", PodPhase.Unknown, { "cpu": "1000m", "memory": "2G" }) node_info = NodeInfo.new(node) with pytest.raises(NodeNotReady): node_info.add_task(TaskInfo.new(pod)) assert node_info == NodeInfo( name="n1", node=node, idle=Resource.new({ "cpu": "2000m", "memory": "1G" }), used=Resource(), releasing=Resource(), pipelined=Resource(), allocatable=Resource.new({ "cpu": "2000m", "memory": "1G" }), capability=Resource.new({ "cpu": "2000m", "memory": "1G" }), state={"phase": "Ready"}, tasks={}, )
def test_add_task_index(self): job = JobInfo() task_info = TaskInfo( uid="6a4254f0-c299-401a-ab0a-20dd9f27506b", resource_requests=Resource.new({ "cpu": "30m", "memory": "300Mi" }), status=TaskStatus.Bound, ) job.add_task_index(task_info) assert (job.task_status_index[TaskStatus.Bound] ["6a4254f0-c299-401a-ab0a-20dd9f27506b"] == task_info)
def test_add_non_owner_pod(): node = build_node("n1", {"cpu": "8000m", "memory": "10G"}) pod1 = build_pod( "c1", "p1", "n1", PodPhase.Running, { "cpu": "1000m", "memory": "1G" }, ) pod2 = build_pod( "c1", "p2", "n1", PodPhase.Running, { "cpu": "2000m", "memory": "2G" }, ) node_info = NodeInfo.new(node) for pod in [pod1, pod2]: task = TaskInfo.new(pod) node_info.add_task(task) assert node_info == NodeInfo( name="n1", node=node, idle=Resource.new({ "cpu": "5000m", "memory": "7G" }), used=Resource.new({ "cpu": "3000m", "memory": "3G" }), releasing=Resource(), pipelined=Resource(), allocatable=Resource.new({ "cpu": "8000m", "memory": "10G" }), capability=Resource.new({ "cpu": "8000m", "memory": "10G" }), state={"phase": "Ready"}, tasks={ "c1/p1": TaskInfo.new(pod1), "c1/p2": TaskInfo.new(pod2) }, )
def test_add_releasing_pod(): node = build_node("n1", {"cpu": "5000m", "memory": "8G"}) pod = build_pod("c1", "p1", "n1", PodPhase.Running, { "cpu": "1000m", "memory": "2G" }) pod.metadata.deletionTimestamp = "2020-01-01" node_info = NodeInfo.new(node) node_info.add_task(TaskInfo.new(pod)) assert node_info == NodeInfo( name="n1", node=node, idle=Resource.new({ "cpu": "4000m", "memory": "6G" }), used=Resource.new({ "cpu": "1000m", "memory": "2G" }), releasing=Resource.new({ "cpu": "1000m", "memory": "2G" }), pipelined=Resource(), allocatable=Resource.new({ "cpu": "5000m", "memory": "8G" }), capability=Resource.new({ "cpu": "5000m", "memory": "8G" }), state={"phase": "Ready"}, tasks={"c1/p1": TaskInfo.new(pod)}, )
def test_sub(left_resource_list: dict, right_resource_list: dict, expected: dict): left_resource = Resource.new(left_resource_list) right_resource = Resource.new(right_resource_list) expected_resource = Resource.new(expected) assert left_resource - right_resource == expected_resource
def test_is_zero(resource_list: dict, resource_name: str, is_zero: bool): resource = Resource.new(resource_list) assert resource.is_zero(resource_name) == is_zero
"c1/p3": TaskInfo.new(pod3), }, ) @parametrize( "node_info,node,expected_state", [ ( NodeInfo(), None, NodeState(phase="NotReady", reason="UnInitialized"), ), ( NodeInfo(used=Resource.new({ "cpu": "1000m", "memory": "2G" }), ), Node(status={"allocatable": { "cpu": "500m", "memory": "1G" }}), NodeState(phase="NotReady", reason="OutOfSync"), ), ( NodeInfo(), Node(status={"conditions": [{ "type": "Ready", "status": "False" }]}), NodeState(phase="NotReady", reason="NotReady"), ),
def test_fit_deta(left_resource: Resource, right_resource: Resource, expected: Resource): assert left_resource.fit_delta(right_resource) == expected
def test_set_max_resource(left_resource_list: dict, right_resource_list: dict, expected: dict): left_resource = Resource.new(left_resource_list) right_resource = Resource.new(right_resource_list) expected_resource = Resource.new(expected) assert expected_resource == left_resource.set_max_resource(right_resource)
def test_greater_equal_strict(left_resource_list: dict, right_resource_list: dict, expected: bool): left_resource = Resource.new(left_resource_list) right_resource = Resource.new(right_resource_list) assert left_resource.greater_equal_strict(right_resource) == expected
def test_diff( left_resource: Resource, right_resource: Resource, expected: Tuple[Resource, Resource], ): assert left_resource.diff(right_resource) == expected
def test_new_resource(resource_list: dict, resource: Resource): assert Resource.new(resource_list) == resource
def test_set_scalar_resource(): empty_resource = Resource() empty_resource.set_scalar_resource("nvidia.com/gpu", 100) assert empty_resource == Resource( scalar_resources={"nvidia.com/gpu": ResourceQuantity(100)})
def test_less(left_resource_list: dict, right_resource_list: dict, expected: bool): left_resource = Resource.new(left_resource_list) right_resource = Resource.new(right_resource_list) assert (left_resource < right_resource) == expected
def test_is_empty(resource_list: dict, is_empty: bool): resource = Resource.new(resource_list) assert resource.is_empty() == is_empty
def test_add(left_resource_list: dict, right_resource_list: dict, expected: dict): left_resource = Resource.new(left_resource_list) right_resource = Resource.new(right_resource_list) excpeted_resource = Resource.new(expected) assert left_resource + right_resource == excpeted_resource
def test_multi(resource_list: dict, ratio: int, expected: dict): resource = Resource.new(resource_list) expected_resource = Resource.new(expected) assert resource * ratio == expected_resource
assert resource.resource_names == ["cpu", "memory", "nvidia.com/gpu"] @parametrize( "resource_list,resource", [ ( { "cpu": "2000m", "memory": "1G", "pods": 20, "nvidia.com/gpu": "1G" }, Resource( milli_cpu=ResourceQuantity(2000), memory=ResourceQuantity(1e9), max_task_num=20, scalar_resources={"nvidia.com/gpu": ResourceQuantity(1e12)}, ), ), ( { "cpu": "2000m", "memory": "1G", "pods": 20, "nvidia.com/gpu": "" }, Resource( milli_cpu=ResourceQuantity(2000), memory=ResourceQuantity(1e9), max_task_num=20, scalar_resources={},