def test_commit_does_not_exist(self, mocker): application_template = factories.ApplicationTemplateFactory() factories.ApplicationInstanceFactory( application_template=application_template, commit_id='', spawner_application_template_options=json.dumps( {'CONTAINER_NAME': 'user-defined-container'}), spawner_application_instance_id=json.dumps( {'task_arn': 'arn:test:vis/task-id/999'}), ) mock_get_application_template = mocker.patch( 'dataworkspace.apps.applications.views._application_template') mock_get_application_template.return_value = application_template develop_visualisations_permission = Permission.objects.get( codename='develop_visualisations', content_type=ContentType.objects.get_for_model( ApplicationInstance), ) user = factories.UserFactory.create( username='******', is_staff=False, is_superuser=False, ) user.user_permissions.add(develop_visualisations_permission) client = Client(**get_http_sso_data(user)) client.post(reverse('admin:index'), follow=True) with _visualisation_ui_gitlab_mocks(): response = client.get( reverse('visualisations:logs', args=(1, 'xxx'))) assert response.status_code == 200 assert response.content == b'No logs were found for this visualisation.'
def test_no_events(self, mocker): application_template = factories.ApplicationTemplateFactory() factories.ApplicationInstanceFactory( application_template=application_template, commit_id="xxx", spawner_application_template_options=json.dumps( {"CONTAINER_NAME": "user-defined-container"} ), spawner_application_instance_id=json.dumps({"task_arn": "arn:test:vis/task-id/999"}), ) mock_get_application_template = mocker.patch( "dataworkspace.apps.applications.views._application_template" ) mock_get_application_template.return_value = application_template mock_boto = mocker.patch("dataworkspace.apps.core.boto3_client.boto3.client") mock_boto.return_value.get_log_events.side_effect = botocore.exceptions.ClientError( error_response={"Error": {"Code": "ResourceNotFoundException"}}, operation_name="get_log_events", ) develop_visualisations_permission = Permission.objects.get( codename="develop_visualisations", content_type=ContentType.objects.get_for_model(ApplicationInstance), ) user = factories.UserFactory.create( username="******", is_staff=False, is_superuser=False, ) user.user_permissions.add(develop_visualisations_permission) client = Client(**get_http_sso_data(user)) client.post(reverse("admin:index"), follow=True) with _visualisation_ui_gitlab_mocks(): response = client.get(reverse("visualisations:logs", args=(1, "xxx"))) assert response.status_code == 200 assert response.content == b"No logs were found for this visualisation."
def test_user_with_size_config_shows_correct_config(self): group_name = "Visualisation Tools" template = factories.ApplicationTemplateFactory() template.group_name = group_name template.save() user = get_user_model().objects.create() UserToolConfiguration.objects.create( user=user, tool_template=template, size=UserToolConfiguration.SIZE_EXTRA_LARGE, ) client = Client(**get_http_sso_data(user)) response = client.get(reverse("applications:tools"), follow=True) assert len(response.context["tools"][group_name]["tools"]) == 3 tool = None for item in response.context["tools"][group_name]["tools"]: if item.name == template.nice_name: tool = item break assert tool is not None assert tool.tool_configuration.size_config.name == "Extra Large" assert tool.tool_configuration.size_config.cpu == 4096 assert tool.tool_configuration.size_config.memory == 30720
def test_with_events(self, mocker): application_template = factories.ApplicationTemplateFactory() factories.ApplicationInstanceFactory( application_template=application_template, commit_id='xxx', spawner_application_template_options=json.dumps( {'CONTAINER_NAME': 'user-defined-container'}), spawner_application_instance_id=json.dumps( {'task_arn': 'arn:test:vis/task-id/999'}), ) mock_get_application_template = mocker.patch( 'dataworkspace.apps.applications.views._application_template') mock_get_application_template.return_value = application_template mock_boto = mocker.patch( 'dataworkspace.apps.applications.utils.boto3.client') mock_boto.return_value.get_log_events.side_effect = [ { 'nextForwardToken': '12345', 'events': [{ 'timestamp': 1605891793796, 'message': 'log message 1' }], }, { 'events': [{ 'timestamp': 1605891793797, 'message': 'log message 2' }] }, ] develop_visualisations_permission = Permission.objects.get( codename='develop_visualisations', content_type=ContentType.objects.get_for_model( ApplicationInstance), ) user = factories.UserFactory.create( username='******', is_staff=False, is_superuser=False, ) user.user_permissions.add(develop_visualisations_permission) client = Client(**get_http_sso_data(user)) client.post(reverse('admin:index'), follow=True) with _visualisation_ui_gitlab_mocks(): response = client.get( reverse('visualisations:logs', args=(1, 'xxx'))) assert response.status_code == 200 assert response.content == ( b'2020-11-20 17:03:13.796000 - log message 1\n' b'2020-11-20 17:03:13.797000 - log message 2\n')
def test_user_with_no_size_config_shows_default_config(self): factories.ApplicationTemplateFactory() user = get_user_model().objects.create() client = Client(**get_http_sso_data(user)) response = client.get(reverse('applications:tools'), follow=True) assert len(response.context['applications']) == 1 assert (response.context['applications'][0] ['tool_configuration'].size_config.name == 'Medium') assert (response.context['applications'][0] ['tool_configuration'].size_config.cpu == 1024) assert (response.context['applications'][0] ['tool_configuration'].size_config.memory == 8192)
def test_get_shows_all_size_choices(self): tool = factories.ApplicationTemplateFactory() user = get_user_model().objects.create() client = Client(**get_http_sso_data(user)) response = client.get( reverse( 'applications:configure_tool_size', kwargs={'tool_host_basename': tool.host_basename}, ), ) assert response.status_code == 200 assert b'Small' in response.content assert b'Medium (default)' in response.content assert b'Large' in response.content assert b'Extra Large' in response.content
def test_user_with_size_config_shows_correct_config(self): tool = factories.ApplicationTemplateFactory() user = get_user_model().objects.create() UserToolConfiguration.objects.create( user=user, tool_template=tool, size=UserToolConfiguration.SIZE_EXTRA_LARGE) client = Client(**get_http_sso_data(user)) response = client.get(reverse('applications:tools'), follow=True) assert len(response.context['applications']) == 1 assert (response.context['applications'][0] ['tool_configuration'].size_config.name == 'Extra Large') assert (response.context['applications'][0] ['tool_configuration'].size_config.cpu == 4096) assert (response.context['applications'][0] ['tool_configuration'].size_config.memory == 30720)
def test_with_events(self, mocker): application_template = factories.ApplicationTemplateFactory() factories.ApplicationInstanceFactory( application_template=application_template, commit_id="xxx", spawner_application_template_options=json.dumps( {"CONTAINER_NAME": "user-defined-container"} ), spawner_application_instance_id=json.dumps({"task_arn": "arn:test:vis/task-id/999"}), ) mock_get_application_template = mocker.patch( "dataworkspace.apps.applications.views._application_template" ) mock_get_application_template.return_value = application_template mock_boto = mocker.patch("dataworkspace.apps.core.boto3_client.boto3.client") mock_boto.return_value.get_log_events.side_effect = [ { "nextForwardToken": "12345", "events": [{"timestamp": 1605891793796, "message": "log message 1"}], }, {"events": [{"timestamp": 1605891793797, "message": "log message 2"}]}, ] develop_visualisations_permission = Permission.objects.get( codename="develop_visualisations", content_type=ContentType.objects.get_for_model(ApplicationInstance), ) user = factories.UserFactory.create( username="******", is_staff=False, is_superuser=False, ) user.user_permissions.add(develop_visualisations_permission) client = Client(**get_http_sso_data(user)) client.post(reverse("admin:index"), follow=True) with _visualisation_ui_gitlab_mocks(): response = client.get(reverse("visualisations:logs", args=(1, "xxx"))) assert response.status_code == 200 assert response.content == ( b"2020-11-20 17:03:13.796000 - log message 1\n" b"2020-11-20 17:03:13.797000 - log message 2\n" )
def test_post_creates_new_tool_configuration(self): tool = factories.ApplicationTemplateFactory(nice_name='RStudio') user = get_user_model().objects.create() assert not tool.user_tool_configuration.filter(user=user).first() client = Client(**get_http_sso_data(user)) response = client.post( reverse( 'applications:configure_tool_size', kwargs={'tool_host_basename': tool.host_basename}, ), {'size': UserToolConfiguration.SIZE_EXTRA_LARGE}, follow=True, ) assert response.status_code == 200 assert str(list( response.context['messages'])[0]) == 'Saved RStudio size' assert (tool.user_tool_configuration.filter( user=user).first().size == UserToolConfiguration.SIZE_EXTRA_LARGE)
def test_post_updates_existing_tool_configuration(self): tool = factories.ApplicationTemplateFactory(nice_name="RStudio") user = get_user_model().objects.create() UserToolConfiguration.objects.create( user=user, tool_template=tool, size=UserToolConfiguration.SIZE_EXTRA_LARGE ) client = Client(**get_http_sso_data(user)) response = client.post( reverse( "applications:configure_tool_size", kwargs={"tool_host_basename": tool.host_basename}, ), {"size": UserToolConfiguration.SIZE_SMALL}, follow=True, ) assert response.status_code == 200 assert str(list(response.context["messages"])[0]) == "Saved RStudio size" assert ( tool.user_tool_configuration.filter(user=user).first().size == UserToolConfiguration.SIZE_SMALL )
def test_user_with_no_size_config_shows_default_config(self): group_name = "Visualisation Tools" template = factories.ApplicationTemplateFactory() template.group_name = group_name template.save() user = get_user_model().objects.create() client = Client(**get_http_sso_data(user)) response = client.get(reverse("applications:tools"), follow=True) assert len(response.context["tools"][group_name]["tools"]) == 3 tool = None for item in response.context["tools"][group_name]["tools"]: if item.name == template.nice_name: tool = item break assert tool is not None assert tool.tool_configuration.size_config.name == "Medium" assert tool.tool_configuration.size_config.cpu == 1024 assert tool.tool_configuration.size_config.memory == 8192