Exemple #1
0
    def test_archive_triggers_stopping_of_jobs(self):
        assert self.queryset.count() == 1
        for _ in range(2):
            job = JobFactory(project=self.object)
            job.set_status(JobLifeCycle.SCHEDULED)
        assert Job.objects.count() == 2

        with patch('scheduler.tasks.jobs.jobs_stop.apply_async') as job_mock_stop:
            resp = self.auth_client.post(self.url + 'archive/')
        assert job_mock_stop.call_count == 2
        assert resp.status_code == status.HTTP_200_OK
        assert self.queryset.count() == 0
        assert Job.objects.count() == 0
        assert Job.all.count() == 2
Exemple #2
0
    def test_delete_triggers_stopping_of_jobs(self):
        assert self.queryset.count() == 1
        for _ in range(2):
            job = JobFactory(project=self.object)
            job.set_status(JobLifeCycle.SCHEDULED)
        assert Job.objects.count() == 2

        with patch(
                'scheduler.tasks.jobs.jobs_stop.apply_async') as job_mock_stop:
            resp = self.auth_client.delete(self.url)
        assert job_mock_stop.call_count == 2
        assert resp.status_code == status.HTTP_204_NO_CONTENT
        assert self.queryset.count() == 0
        assert ExperimentGroup.objects.count() == 0
        assert Experiment.objects.count() == 0

        # Delete does not work for other project public and private
        resp = self.auth_client.delete(self.url_other)
        assert resp.status_code in (status.HTTP_401_UNAUTHORIZED,
                                    status.HTTP_403_FORBIDDEN)
        resp = self.auth_client.delete(self.url_private)
        assert resp.status_code in (status.HTTP_401_UNAUTHORIZED,
                                    status.HTTP_403_FORBIDDEN)
Exemple #3
0
class TestJobLogsViewV1(BaseViewTest):
    num_log_lines = 10
    HAS_AUTH = True

    def setUp(self):
        super().setUp()
        project = ProjectFactory(user=self.auth_client.user)
        self.job = JobFactory(project=project)
        self.logs = []
        self.url = '/{}/{}/{}/jobs/{}/logs'.format(API_V1,
                                                   project.user.username,
                                                   project.name, self.job.id)
        self.stream_url = '/{}/{}/{}/jobs/{}/logs/stream'.format(
            API_V1, project.user.username, project.name, self.job.id)
        self.ws_url = '/{}/{}/{}/jobs/{}/logs'.format(WS_V1,
                                                      project.user.username,
                                                      project.name,
                                                      self.job.id)

    def create_logs(self, temp):
        log_path = stores.get_job_logs_path(job_name=self.job.unique_name,
                                            temp=temp)
        stores.create_job_logs_path(job_name=self.job.unique_name, temp=temp)
        fake = Faker()
        self.logs = []
        for _ in range(self.num_log_lines):
            self.logs.append(fake.sentence())
        with open(log_path, 'w') as file:
            for line in self.logs:
                file.write(line)
                file.write('\n')

    def test_get_done_job(self):
        self.job.set_status(JobLifeCycle.SUCCEEDED)
        self.assertTrue(self.job.is_done)
        # No logs
        resp = self.auth_client.get(self.url)
        assert resp.status_code == status.HTTP_404_NOT_FOUND
        # Check the it does not return temp file
        self.create_logs(temp=True)
        resp = self.auth_client.get(self.url)
        assert resp.status_code == status.HTTP_404_NOT_FOUND
        # Check returns the correct file
        self.create_logs(temp=False)
        resp = self.auth_client.get(self.url)
        assert resp.status_code == status.HTTP_200_OK

        data = [i for i in resp._iterator]  # pylint:disable=protected-access
        data = [d for d in data[0].decode('utf-8').split('\n') if d]
        assert len(data) == len(self.logs)
        assert data == self.logs

    @patch('api.jobs.views.process_logs')
    def test_get_non_done_job(self, _):
        self.assertFalse(self.job.is_done)
        # No logs
        resp = self.auth_client.get(self.url)
        assert resp.status_code == status.HTTP_404_NOT_FOUND
        # Check the it does not return non temp file
        self.create_logs(temp=False)
        resp = self.auth_client.get(self.url)
        assert resp.status_code == status.HTTP_404_NOT_FOUND
        # Check returns the correct file
        self.create_logs(temp=True)
        resp = self.auth_client.get(self.url)
        assert resp.status_code == status.HTTP_200_OK

        data = [i for i in resp._iterator]  # pylint:disable=protected-access
        data = [d for d in data[0].decode('utf-8').split('\n') if d]
        assert len(data) == len(self.logs)
        assert data == self.logs

    def test_stream_redirects_to_internal_service(self):
        response = self.auth_client.get(self.stream_url)
        self.assertEqual(response.status_code, 200)
        self.assertTrue(ProtectedView.NGINX_REDIRECT_HEADER in response)
        self.assertEqual(response[ProtectedView.NGINX_REDIRECT_HEADER],
                         self.ws_url)