def test_serialize_deserialize(): value = DeploymentDefinitions( nodes={ '111': DeploymentDefinition(value={ 'a': 1, 'b': '2' }, expiration_date=(datetime.datetime( 2018, 11, 12, 13, 14).replace( tzinfo=dateutil.tz.tzutc()))), '222': DeploymentDefinition(value={ 'c': 3, 'd': '4' }, expiration_date=(datetime.datetime( 2018, 11, 16, 17, 18).replace( tzinfo=dateutil.tz.tzutc()))) }) serialized = value.serialize() assert serialized == { 'type': 'SerializableTypes.DEPLOYMENT_DEFINITIONS', 'nodes': { '111': { 'type': 'SerializableTypes.DEPLOYMENT_DEFINITION', 'value': { 'a': 1, 'b': '2' }, 'expiration_date': '2018-11-12T13:14:00+00:00' }, '222': { 'type': 'SerializableTypes.DEPLOYMENT_DEFINITION', 'value': { 'c': 3, 'd': '4' }, 'expiration_date': '2018-11-16T17:18:00+00:00' } }, 'jupyter_deployments': {}, 'dask_deployments': {} } deserialized = DeploymentDefinitions.deserialize(serialized=serialized) assert deserialized == value
def test_serialize_deserialize(): value = DeploymentDefinition(value={ 'a': 1, 'b': '2' }, expiration_date=(datetime.datetime( 2018, 11, 12, 13, 14).replace(tzinfo=dateutil.tz.tzutc()))) serialized = value.serialize() assert serialized == { 'type': 'SerializableTypes.DEPLOYMENT_DEFINITION', 'value': { 'a': 1, 'b': '2' }, 'expiration_date': '2018-11-12T13:14:00+00:00' } deserialized = DeploymentDefinition.deserialize(serialized=serialized) assert deserialized == value
def get_nodes_deployment_definition( deployment: NodesImpl) -> DeploymentDefinition: # noqa """Obtains a definition from an allocation deployment. Expiration date is the minimum node allocation end date, or one day from now if the nodes were not yet allocated. :param deployment: Deployment to obtain the definition of. """ expiration_date = get_expiration_date_from_nodes(nodes=deployment) return DeploymentDefinition(value=deployment.serialize(), expiration_date=expiration_date)
def get_dask_deployment_definition( dask_deployment: DaskDeploymentImpl) -> DeploymentDefinition: # noqa """Obtains a definition from a Dask deployment. Expiration date is the minimum allocation end date of the node the scheduler is deployed on, or one day from now if the node is not allocated. :param dask_deployment: Deployment to obtain the definition of. """ expiration_date = get_expiration_date_from_nodes( nodes=[dask_deployment.scheduler.deployment.node]) return DeploymentDefinition(value=dask_deployment.serialize(), expiration_date=expiration_date)
def deserialize(serialized: dict) -> 'DeploymentDefinitions': try: assert serialized['type'] == str( SerializableTypes.DEPLOYMENT_DEFINITIONS) nodes = { uuid: DeploymentDefinition.deserialize(serialized=node) for uuid, node in serialized.get('nodes', {}).items() } jupyter_deployments = { uuid: DeploymentDefinition.deserialize(serialized=node) for uuid, node in serialized.get('jupyter_deployments', {}).items() } dask_deployments = { uuid: DeploymentDefinition.deserialize(serialized=node) for uuid, node in serialized.get('dask_deployments', {}).items() } return DeploymentDefinitions( nodes=nodes, jupyter_deployments=jupyter_deployments, dask_deployments=dask_deployments) except KeyError as e: raise RuntimeError("Unable to deserialize.") from e
def get_jupyter_deployment_definition( jupyter_deployment: JupyterDeploymentImpl ) -> DeploymentDefinition: # noqa """Obtains a definition from a Jupyter deployment. Expiration date is the allocation end date of the node the notebook is deployed on, or one day from now if the node is not allocated. :param jupyter_deployment: Deployment to obtain the definition of. """ expiration_date = get_expiration_date_from_nodes( nodes=[jupyter_deployment.deployment.node]) return DeploymentDefinition(value=jupyter_deployment.serialize(), expiration_date=expiration_date)
def test_missing_serialized_keys(): serialized = {'type': 'SerializableTypes.DEPLOYMENT_DEFINITION'} with pytest.raises(RuntimeError): DeploymentDefinition.deserialize(serialized=serialized)
def test_invalid_serialized_type(): serialized = {'type': 'SerializableTypes.DEPLOYMENT_DEFINITION2'} with pytest.raises(AssertionError): DeploymentDefinition.deserialize(serialized=serialized)
def test_discard_expired_deployments(): base = datetime.datetime(2017, 11, 12, 10, 00).replace( tzinfo=dateutil.tz.tzutc()) deployments = DeploymentDefinitions( nodes={ '30s in the past': DeploymentDefinition( value={}, expiration_date=base - datetime.timedelta(seconds=30)), '15s in the past': DeploymentDefinition( value={}, expiration_date=base - datetime.timedelta(seconds=15)), 'now': DeploymentDefinition( value={}, expiration_date=base), '15s in the future': DeploymentDefinition( value={}, expiration_date=base + datetime.timedelta(seconds=15)), '30s in the future': DeploymentDefinition( value={}, expiration_date=base + datetime.timedelta(seconds=30)), '45s in the future': DeploymentDefinition( value={}, expiration_date=base + datetime.timedelta(seconds=45)), '60s in the future': DeploymentDefinition( value={}, expiration_date=base + datetime.timedelta(seconds=60))}, jupyter_deployments={ '-30s Jupyter': DeploymentDefinition( value={}, expiration_date=base - datetime.timedelta(seconds=30)), '+60s Jupyter': DeploymentDefinition( value={}, expiration_date=base + datetime.timedelta(seconds=60))}) assert len(deployments.nodes) == 7 assert len(deployments.jupyter_deployments) == 2 old_utc_now = \ idact.detail.deployment_sync.discard_expired_deployments.utc_now def fake_utc_now(): return base try: idact.detail.deployment_sync.discard_expired_deployments.utc_now = \ fake_utc_now new_deployments = discard_expired_deployments(deployments=deployments) finally: idact.detail.deployment_sync.discard_expired_deployments.utc_now = \ old_utc_now assert len(deployments.nodes) == 7 assert len(deployments.jupyter_deployments) == 2 assert new_deployments.nodes == { '30s in the future': DeploymentDefinition( value={}, expiration_date=base + datetime.timedelta(seconds=30)), '45s in the future': DeploymentDefinition( value={}, expiration_date=base + datetime.timedelta(seconds=45)), '60s in the future': DeploymentDefinition( value={}, expiration_date=base + datetime.timedelta(seconds=60))} assert new_deployments.jupyter_deployments == { '+60s Jupyter': DeploymentDefinition( value={}, expiration_date=base + datetime.timedelta(seconds=60))}