def test_session_api_patch_permissions(client, two_workstation_sets): tests = ( (two_workstation_sets.ws1.editor, 200, True), (two_workstation_sets.ws1.user, 200, True), (two_workstation_sets.ws1.user1, 404, False), (two_workstation_sets.ws2.editor, 404, False), (two_workstation_sets.ws2.user, 404, False), (UserFactory(), 404, False), (UserFactory(is_staff=True), 404, False), (None, 401, False), ) for test in tests: s = SessionFactory( workstation_image=two_workstation_sets.ws1.image, creator=two_workstation_sets.ws1.user, ) response = get_view_for_user( viewname="api:session-keep-alive", client=client, method=client.patch, user=test[0], reverse_kwargs={"pk": s.pk}, content_type="application/json", ) assert response.status_code == test[1] # The maximum duration should have changed from the default s.refresh_from_db() assert s.status == s.QUEUED # Read only, always unchanged assert (s.maximum_duration == timedelta(minutes=10)) is not test[2]
def test_workstation_ready(http_image, docker_client, settings): path, sha256 = http_image wsi = WorkstationImageFactory( image__from_path=path, image_sha256=sha256, ready=False ) # Execute the celery in place settings.task_eager_propagates = (True,) settings.task_always_eager = (True,) s = SessionFactory(workstation_image=wsi) s.refresh_from_db() assert s.status == s.FAILED
def test_workstation_ready(http_image, docker_client, settings): path, sha256 = http_image wsi = WorkstationImageFactory(image__from_path=path, image_sha256=sha256, ready=False) # Execute the celery in place settings.task_eager_propagates = (True, ) settings.task_always_eager = (True, ) s = SessionFactory(workstation_image=wsi) s.refresh_from_db() assert s.status == s.FAILED
def test_session_update_read_only_fails(client): user = UserFactory() s = SessionFactory(creator=user) response = get_view_for_user( client=client, method=client.patch, viewname="api:session-keep-alive", reverse_kwargs={"pk": s.pk}, user=user, data={"status": "Stopped"}, content_type="application/json", ) assert response.status_code == 200 s.refresh_from_db() assert s.status == s.QUEUED
def test_session_start(http_image, docker_client, settings): path, sha256 = http_image wsi = WorkstationImageFactory(image__from_path=path, image_sha256=sha256, ready=True) # Execute the celery in place settings.task_eager_propagates = (True, ) settings.task_always_eager = (True, ) with capture_on_commit_callbacks(execute=True): s = SessionFactory(workstation_image=wsi) try: assert s.service.container s.refresh_from_db() assert s.status == s.STARTED container = s.service.container assert container.labels["traefik.enable"] == "true" assert container.labels[ f"traefik.http.services.{s.hostname}-http.loadbalancer.server.port"] == str( s.workstation_image.http_port) assert container.labels[ f"traefik.http.services.{s.hostname}-websocket.loadbalancer.server.port"] == str( s.workstation_image.websocket_port) networks = container.attrs.get("NetworkSettings")["Networks"] assert len(networks) == 1 assert settings.WORKSTATIONS_NETWORK_NAME in networks with capture_on_commit_callbacks(execute=True): s.user_finished = True s.save() with pytest.raises(NotFound): # noinspection PyStatementEffect s.service.container finally: stop_all_sessions()
def test_session_update_extends_timeout(client): user = UserFactory() s = SessionFactory(creator=user) assert s.maximum_duration == timedelta(minutes=10) response = get_view_for_user( client=client, method=client.patch, viewname="api:session-keep-alive", reverse_kwargs={"pk": s.pk}, user=user, content_type="application/json", ) assert response.status_code == 200 s.refresh_from_db() # Just check that it changed from the default assert s.maximum_duration != timedelta(minutes=10)
def test_session_start(http_image, docker_client, settings): path, sha256 = http_image wsi = WorkstationImageFactory( image__from_path=path, image_sha256=sha256, ready=True ) # Execute the celery in place settings.task_eager_propagates = (True,) settings.task_always_eager = (True,) s = SessionFactory(workstation_image=wsi) try: assert s.service.container s.refresh_from_db() assert s.status == s.STARTED container = s.service.container assert container.labels["traefik.enable"] == "true" assert container.labels["traefik.http.port"] == str( s.workstation_image.http_port ) assert container.labels["traefik.websocket.port"] == str( s.workstation_image.websocket_port ) networks = container.attrs.get("NetworkSettings")["Networks"] assert len(networks) == 1 assert settings.WORKSTATIONS_NETWORK_NAME in networks s.user_finished = True s.save() with pytest.raises(NotFound): # noinspection PyStatementEffect s.service.container finally: s.stop()
def test_session_limit(http_image, docker_client, settings): path, sha256 = http_image wsi = WorkstationImageFactory(image__from_path=path, image_sha256=sha256, ready=True) # Execute the celery in place settings.WORKSTATIONS_MAXIMUM_SESSIONS = 1 settings.task_eager_propagates = (True, ) settings.task_always_eager = (True, ) try: with capture_on_commit_callbacks(execute=True): s1 = SessionFactory(workstation_image=wsi) s1.refresh_from_db() assert s1.status == s1.STARTED with capture_on_commit_callbacks(execute=True): s2 = SessionFactory(workstation_image=wsi) s2.refresh_from_db() assert s2.status == s2.FAILED s1.stop() with capture_on_commit_callbacks(execute=True): s3 = SessionFactory(workstation_image=wsi) s3.refresh_from_db() assert s3.status == s3.STARTED finally: stop_all_sessions()
def test_session_limit(http_image, docker_client, settings): path, sha256 = http_image wsi = WorkstationImageFactory( image__from_path=path, image_sha256=sha256, ready=True ) # Execute the celery in place settings.task_eager_propagates = (True,) settings.task_always_eager = (True,) settings.WORKSTATIONS_MAXIMUM_SESSIONS = 1 try: s1 = SessionFactory(workstation_image=wsi) s1.refresh_from_db() assert s1.status == s1.STARTED s2 = SessionFactory(workstation_image=wsi) s2.refresh_from_db() assert s2.status == s2.FAILED s1.stop() s3 = SessionFactory(workstation_image=wsi) s3.refresh_from_db() assert s3.status == s3.STARTED finally: stop_all_sessions()
def test_session_keep_alive_limit(client, settings): user = UserFactory() s = SessionFactory(creator=user) assert s.maximum_duration == timedelta(minutes=10) s.created = now() - timedelta(days=1) s.save() response = get_view_for_user( client=client, method=client.patch, viewname="api:session-keep-alive", reverse_kwargs={"pk": s.pk}, user=user, content_type="application/json", ) assert response.status_code == 400 s.refresh_from_db() assert s.maximum_duration == timedelta( seconds=settings.WORKSTATIONS_SESSION_DURATION_LIMIT)
def test_session_update(client): session = SessionFactory() assert session.user_finished == False response = get_view_for_user( client=client, method=client.post, viewname="workstations:session-update", reverse_kwargs={ "slug": session.workstation_image.workstation.slug, "pk": session.pk, }, user=session.creator, data={"user_finished": True}, ) assert response.status_code == 302 assert response.url == session.get_absolute_url() session.refresh_from_db() assert session.user_finished == True
def test_session_update(client): user = UserFactory(is_staff=True) session = SessionFactory() assert session.user_finished == False response = get_view_for_user( client=client, method=client.post, viewname="workstations:session-update", reverse_kwargs={ "slug": session.workstation_image.workstation.slug, "pk": session.pk, }, user=user, data={"user_finished": True}, ) assert response.status_code == 302 assert response.url == session.get_absolute_url() session.refresh_from_db() assert session.user_finished == True