def test_get_by_runs_not_created_yet( mocker: MockFixture, tensorboard_manager_mocked: TensorboardManager): fake_tensorboard_id = '72a5cabc-548c-4a66-8ea9-645736569dfd' fake_tensorboard_path = '/tb/' + fake_tensorboard_id + '/' runs = [ Run(name='run-name-1', owner='sigfrid'), Run(name='run-name-2', owner='schreck'), Run(name='run-name-3', owner='jacek') ] k8s_tensorboard = K8STensorboardInstance.from_runs(id=fake_tensorboard_id, runs=runs) mocker.patch.object(tensorboard_manager_mocked.client, 'list_deployments').return_value = \ [k8s_tensorboard.deployment] mocker.patch.object( tensorboard_manager_mocked.client, 'list_ingresses').return_value = [k8s_tensorboard.ingress] mocker.patch.object(tensorboard_manager_mocked.client, 'get_pod').return_value = None tensorboard = tensorboard_manager_mocked.get_by_runs(runs) assert tensorboard.id == fake_tensorboard_id assert tensorboard.status == TensorboardStatus.CREATING assert tensorboard.url == fake_tensorboard_path
def test_get_by_run_names_not_found( mocker: MockFixture, tensorboard_manager_mocked: TensorboardManager): mocker.patch.object(tensorboard_manager_mocked.client, 'get_deployment').return_value = None get_runs = [ Run(name='run-name-2', owner='schreck'), Run(name='run-name-3', owner='jacek'), Run(name='run-name-1', owner='sigfrid') ] tensorboard = tensorboard_manager_mocked.get_by_runs(runs=get_runs) assert tensorboard is None
def test_create(tensorboard_manager_mocked: TensorboardManager): fake_runs = [ Run(name="some-run-3", owner='alice'), Run(name="some-run-2", owner='alice'), Run(name="some-run-1", owner='bob') ] tensorboard = tensorboard_manager_mocked.create(fake_runs) tensorboard_manager_mocked.client.create_deployment.assert_called_once_with( namespace=FAKE_NAMESPACE, body=mock.ANY) tensorboard_manager_mocked.client.create_service.assert_called_once_with( namespace=FAKE_NAMESPACE, body=mock.ANY) tensorboard_manager_mocked.client.create_ingress.assert_called_once_with( namespace=FAKE_NAMESPACE, body=mock.ANY) assert tensorboard
def test_generate_tensorboard_deployment(mocker): mocker.patch('k8s.models.NautaPlatformConfig') fake_runs = [ Run(name="some-run-3", owner='alice'), Run(name="some-run-2", owner='alice'), Run(name="some-run-1", owner='bob'), ] model_instance = K8STensorboardInstance.from_runs( id='a7db5449-6168-4010-9ce6-cbaefbbfa4a1', runs=fake_runs) assert model_instance.deployment.metadata.name == "tensorboard-a7db5449-6168-4010-9ce6-cbaefbbfa4a1" volume_mounts: List[kube_models.V1VolumeMount] = \ model_instance.deployment.spec.template.spec.containers[0].volume_mounts assert len(volume_mounts) == len(fake_runs)
def test_validate_runs(mocker, tensorboard_manager_mocked: TensorboardManager): def fake_isdir(path: str): return path in (TensorboardManager.OUTPUT_PUBLIC_MOUNT_PATH + '/jeanne/training1', TensorboardManager.OUTPUT_PUBLIC_MOUNT_PATH + '/abbie/training2') mocker.patch('os.path.isdir', new=fake_isdir) fake_runs = [ Run(name='training1', owner='jeanne'), Run(name='training2', owner='abbie'), Run(name='training3', owner='harold') ] valid, invalid = tensorboard_manager_mocked.validate_runs(fake_runs) assert valid == [fake_runs[0], fake_runs[1]] assert invalid == [fake_runs[2]]
def from_dict(cls, body): runs_from_json = body['runNames'] runs = [] for json_run in runs_from_json: name = json_run['name'] owner = json_run['owner'] new_run = Run(name=name, owner=owner) runs.append(new_run) return cls(run_names=runs)
def test_generate_run_names_hash(): fake_runs = [ Run(name="some-run-3", owner='alice'), Run(name="some-run-2", owner='alice'), Run(name="some-run-1", owner='bob'), ] expected_hash = '72c9324ef4a28d65dc5d134bb0edc77fb700bd79' run_names_hash_original = K8STensorboardInstance.generate_run_names_hash( fake_runs) random.shuffle(fake_runs) run_names_hash_suffled = K8STensorboardInstance.generate_run_names_hash( fake_runs) assert run_names_hash_original == expected_hash assert run_names_hash_suffled == expected_hash
def test_get_by_runs(mocker: MockFixture, tensorboard_manager_mocked: TensorboardManager): fake_tensorboard_id = '5c0b46de-4017-4062-9ac8-94698cc0c513' fake_tensorboard_path = '/tb/' + fake_tensorboard_id + '/' fake_tensorboard_pod_phase = 'RUNNING' expected_tensorboard_status = TensorboardStatus.RUNNING runs = [ Run(name='run-name-1', owner='sigfrid'), Run(name='run-name-2', owner='schreck'), Run(name='run-name-3', owner='jacek') ] k8s_tensorboard = K8STensorboardInstance.from_runs(id=fake_tensorboard_id, runs=runs) # mocking manually this method is done because we want to mock Kubernetes behaviour to check, if our code # requests Kubernetes API server for deployment with proper label_selector. If label_selector matches # created deployment, this mocked method will return it. Our code should pass the same label_selector regardless of # order of run_names in get_by_run_names parameter. That's why we create below get_run_names with other order to # check if returned deployment would still be the same. # noinspection PyUnusedLocal def _fake_list_deployments(namespace: str, label_selector: str): deployments = [k8s_tensorboard.deployment] splitted_label_selector = label_selector.split('=') label_selector_key = splitted_label_selector[0] label_selector_value = splitted_label_selector[1] if deployments[0].metadata.labels[ label_selector_key] == label_selector_value: return deployments fake_pod = V1Pod(status=V1PodStatus( phase=fake_tensorboard_pod_phase, container_statuses=[ V1ContainerStatus( ready=True, image='', image_id='', name='', restart_count=0), V1ContainerStatus( ready=True, image='', image_id='', name='', restart_count=0) ])) mocker.patch.object(tensorboard_manager_mocked.client, 'list_deployments', new=_fake_list_deployments) mocker.patch.object( tensorboard_manager_mocked.client, 'list_ingresses').return_value = [k8s_tensorboard.ingress] mocker.patch.object(tensorboard_manager_mocked.client, 'get_pod').return_value = fake_pod mocker.patch( 'tensorboard.tensorboard.TensorboardManager._check_tensorboard_nginx_reachable' ).return_value = True get_runs = [ Run(name='run-name-2', owner='schreck'), Run(name='run-name-3', owner='jacek'), Run(name='run-name-1', owner='sigfrid') ] tensorboard = tensorboard_manager_mocked.get_by_runs(get_runs) assert tensorboard.id == fake_tensorboard_id assert tensorboard.status == expected_tensorboard_status assert tensorboard.url == fake_tensorboard_path