def test_to_dict(self): pod = Pod(name='this-is-a-pod', state=PodState.RUNNING, pod_type=PodType.WORKER, container_states=[ ContainerState(state='h1', message='test message') ], pod_conditions=[ PodCondition(cond_type='h2', reason='test reason') ]) self.assertEqual( pod.to_dict(include_private_info=True), { 'name': 'this-is-a-pod', 'pod_type': 'WORKER', 'state': 'RUNNING', 'message': 'h1:test message, h2:test reason' })
def test_from_json(self): json = { 'metadata': { 'name': 'test-pod', 'labels': { 'app-name': 'u244777dac51949c5b2b-data-join-job', 'fl-replica-type': 'master' }, }, 'status': { 'pod_ip': '172.10.0.20', 'phase': 'Running', 'conditions': [{ 'type': 'Failed', 'reason': 'Test reason' }], 'containerStatuses': [{ 'containerID': 'docker://034eaf58d4e24581232832661636da9949b6e2fb056398939fc2c0f2809d4c64', 'image': 'artifact.bytedance.com/fedlearner/fedlearner:438d603', 'state': { 'running': { 'message': 'Test message' } } }] } } expected_pod = Pod(name='test-pod', state=PodState.RUNNING, pod_type=PodType.MASTER, pod_ip='172.10.0.20', container_states=[ ContainerState(state='running', message='Test message') ], pod_conditions=[ PodCondition(cond_type='Failed', reason='Test reason') ]) self.assertEqual(Pod.from_json(json), expected_pod)
def test_from_json(self): json = { 'status': { 'appState': 'FLStateComplete', 'completionTime': '2021-04-26T08:33:45Z', 'flReplicaStatus': { 'Master': { 'failed': { 'test-pod1': {} } }, 'Worker': { 'succeeded': { 'test-pod2': {}, 'test-pod3': {} } } } } } completed_at = int( datetime(2021, 4, 26, 8, 33, 45, tzinfo=timezone.utc).timestamp()) expected_flapp = FlApp(state=FlAppState.COMPLETED, completed_at=completed_at, pods=[ Pod(name='test-pod1', state=PodState.FAILED_AND_FREED, pod_type=PodType.MASTER), Pod(name='test-pod2', state=PodState.SUCCEEDED_AND_FREED, pod_type=PodType.WORKER), Pod(name='test-pod3', state=PodState.SUCCEEDED_AND_FREED, pod_type=PodType.WORKER) ]) self.assertEqual(FlApp.from_json(json), expected_flapp)
def get_pods_for_frontend(self, include_private_info=True): flapp_details = self.get_flapp_details() flapp = FlApp.from_json(flapp_details.get('flapp', None)) pods_json = None if 'pods' in flapp_details: pods_json = flapp_details['pods'].get('items', None) pods = [] if pods_json is not None: pods = [Pod.from_json(p) for p in pods_json] # deduplication pods both in pods and flapp result = {} for pod in flapp.pods: result[pod.name] = pod for pod in pods: result[pod.name] = pod return [pod.to_dict(include_private_info) for pod in result.values()]