Ejemplo n.º 1
0
    def test_tensorboard_creation_triggers_status_creation(self):
        assert TensorboardJobStatus.objects.count() == 0
        project = ProjectFactory()
        project.tensorboard = TensorboardJobFactory()
        project.save()

        assert TensorboardJobStatus.objects.count() == 1
        assert project.tensorboard.last_status == JobLifeCycle.CREATED
Ejemplo n.º 2
0
    def test_notebook_creation_triggers_status_creation(self):
        assert NotebookJobStatus.objects.count() == 0
        project = ProjectFactory()
        project.notebook = NotebookJobFactory()
        project.save()

        assert NotebookJobStatus.objects.count() == 1
        assert project.notebook.last_status == JobLifeCycle.CREATED
Ejemplo n.º 3
0
    def test_project_deletion_cascade_to_notebook_job(self):
        assert NotebookJob.objects.count() == 0
        project = ProjectFactory()
        project.notebook = NotebookJobFactory()
        project.save()
        assert NotebookJob.objects.count() == 1

        with patch('schedulers.tensorboard_scheduler.stop_tensorboard') as _:
            with patch('schedulers.notebook_scheduler.stop_notebook') as _:
                project.delete()
        assert NotebookJob.objects.count() == 0
Ejemplo n.º 4
0
class TestUploadFilesView(BaseViewTest):
    model_class = Repo
    factory_class = RepoFactory
    HAS_AUTH = True

    def setUp(self):
        super().setUp()
        self.project = ProjectFactory(user=self.auth_client.user)
        self.url = '/{}/{}/{}/repo/upload'.format(API_V1,
                                                  self.project.user.username,
                                                  self.project.name)

    @staticmethod
    def get_upload_file(filename='repo'):
        file = File(
            open('./tests/fixtures_static/{}.tar.gz'.format(filename), 'rb'))
        return SimpleUploadedFile(filename,
                                  file.read(),
                                  content_type='multipart/form-data')

    def test_upload_files_creates_repo(self):
        user = self.auth_client.user
        repo_name = self.project.name

        # No repo was created yet
        assert self.model_class.objects.count() == 0
        repo_path = '{}/{}/{}/{}'.format(settings.REPOS_ROOT, user.username,
                                         repo_name, repo_name)
        self.assertFalse(os.path.exists(repo_path))

        uploaded_file = self.get_upload_file()

        with patch('repos.tasks.handle_new_files.delay') as mock_task:
            self.auth_client.put(self.url,
                                 data={'repo': uploaded_file},
                                 content_type=MULTIPART_CONTENT)
        file_path = '{}/{}/{}.tar.gz'.format(settings.UPLOAD_ROOT,
                                             user.username, repo_name)
        self.assertTrue(os.path.exists(file_path))
        assert mock_task.call_count == 1
        assert self.model_class.objects.count() == 1
        self.assertTrue(os.path.exists(repo_path))

    def test_upload_files_uses_repo_if_exists(self):
        user = self.auth_client.user
        repo_name = self.project.name

        repo_path = '{}/{}/{}/{}'.format(settings.REPOS_ROOT, user.username,
                                         repo_name, repo_name)
        self.assertFalse(os.path.exists(repo_path))

        repo = self.factory_class(project=self.project)
        assert self.model_class.objects.count() == 1
        self.assertTrue(os.path.exists(repo_path))

        uploaded_file = self.get_upload_file()

        with patch('repos.tasks.handle_new_files.delay') as mock_task:
            self.auth_client.put(self.url,
                                 data={'repo': uploaded_file},
                                 content_type=MULTIPART_CONTENT)
        file_path = '{}/{}/{}.tar.gz'.format(settings.UPLOAD_ROOT,
                                             user.username, repo_name)
        self.assertTrue(os.path.exists(file_path))
        assert mock_task.call_count == 1
        # No new repo was created and still exists
        assert self.model_class.objects.count() == 1
        self.assertTrue(os.path.exists(repo_path))

        # Deleting repo deletes the dir
        repo.delete()
        self.assertFalse(os.path.exists(repo_path))

    def test_handle_new_files_task(self):
        assert self.model_class.objects.count() == 0
        user = self.auth_client.user
        repo_name = self.project.name
        uploaded_file = self.get_upload_file()

        self.auth_client.put(self.url,
                             data={'repo': uploaded_file},
                             content_type=MULTIPART_CONTENT)

        upload_file_path = '{}/{}/{}.tar.gz'.format(settings.UPLOAD_ROOT,
                                                    user.username, repo_name)
        # Assert the the task handler takes care of cleaning the upload root after
        # committing changes
        self.assertFalse(os.path.exists(upload_file_path))

        # Assert repo model was created
        assert self.model_class.objects.count() == 1
        repo = self.model_class.objects.first()

        # Assert new git repo was created in the repos root and that also the tar file was deleted
        code_file_path = '{}/{}/{}/{}'.format(settings.REPOS_ROOT,
                                              user.username, self.project.name,
                                              self.project.name)
        tar_code_file_path = repo.get_tmp_tar_path()
        self.assertFalse(os.path.exists(tar_code_file_path))
        self.assertTrue(os.path.exists(code_file_path))
        # Assert that the code_file_path is a git repo
        git_file_path = '{}/.git'.format(code_file_path)
        self.assertTrue(os.path.exists(git_file_path))
        # Get last commit
        commit_hash, commit = git.get_last_commit(code_file_path)
        assert commit.author.email == user.email
        assert commit.author.name == user.username

        # Make a new upload with repo_new.tar.gz containing 2 files
        new_uploaded_file = self.get_upload_file('updated_repo')
        self.auth_client.put(self.url,
                             data={'repo': new_uploaded_file},
                             content_type=MULTIPART_CONTENT)

        upload_file_path = '{}/{}/{}.tar.gz'.format(settings.UPLOAD_ROOT,
                                                    user.username, repo_name)
        # Assert the the task handler takes care of cleaning the upload root after
        # committing changes
        self.assertFalse(os.path.exists(upload_file_path))

        # Assert same git repo was used in the repos root and that also the tar file was deleted
        self.assertFalse(os.path.exists(tar_code_file_path))
        self.assertTrue(os.path.exists(code_file_path))
        # Assert that the code_file_path is a git repo
        self.assertTrue(os.path.exists(git_file_path))
        # Get last commit
        commit_hash, commit = git.get_last_commit(code_file_path)
        assert commit.author.email == user.email
        assert commit.author.name == user.username
        # Assert that we committed 3 files (2 files in new_repo.tar.gz one file was deleted)
        assert len(git.get_committed_files(code_file_path, commit_hash)) == 3

        # Make a new upload with repo_with_folder.tar.gz containing 1 file one dir with fil
        new_uploaded_file = self.get_upload_file('repo_with_folder')
        self.auth_client.put(self.url,
                             data={'repo': new_uploaded_file},
                             content_type=MULTIPART_CONTENT)

        upload_file_path = '{}/{}/{}.tar.gz'.format(settings.UPLOAD_ROOT,
                                                    user.username, repo_name)
        # Assert the the task handler takes care of cleaning the upload root after
        # committing changes
        self.assertFalse(os.path.exists(upload_file_path))

        # Assert same git repo was used in the repos root and that also the tar file was deleted
        self.assertFalse(os.path.exists(tar_code_file_path))
        self.assertTrue(os.path.exists(code_file_path))
        # Assert that the code_file_path is a git repo
        self.assertTrue(os.path.exists(git_file_path))
        # Get last commit
        commit_hash, commit = git.get_last_commit(code_file_path)
        assert commit.author.email == user.email
        assert commit.author.name == user.username
        # Assert that we committed 3 files
        # (1 file updated 1 deleted, and one folder with 1 file added)
        assert len(git.get_committed_files(code_file_path, commit_hash)) == 3

        # Check that other user cannot commit to this repo
        new_user = UserFactory()
        self.auth_client.login_user(new_user)
        new_uploaded_file = self.get_upload_file('updated_repo')
        response = self.auth_client.put(self.url,
                                        data={'repo': new_uploaded_file},
                                        content_type=MULTIPART_CONTENT)

        assert response.status_code in (status.HTTP_401_UNAUTHORIZED,
                                        status.HTTP_403_FORBIDDEN)
        upload_file_path = '{}/{}/{}.tar.gz'.format(settings.UPLOAD_ROOT,
                                                    new_user.username,
                                                    repo_name)
        # Assert the the task handler takes care of cleaning the upload root after
        # committing changes
        self.assertFalse(os.path.exists(upload_file_path))

        # Assert same git repo was used in the repos root and that also the tar file was deleted
        self.assertFalse(os.path.exists(tar_code_file_path))
        self.assertTrue(os.path.exists(code_file_path))
        # Assert that the code_file_path is a git repo
        self.assertTrue(os.path.exists(git_file_path))
        # Get last commit and check it did not change
        new_commit_hash, new_commit = git.get_last_commit(code_file_path)
        assert commit.author.email == new_commit.author.email
        assert commit.author.name == new_commit.author.name
        assert new_commit_hash == commit_hash

        # Log old user, otherwise other tests will crash
        self.auth_client.login_user(user)

    def test_cannot_upload_if_project_has_a_running_notebook(self):
        user = self.auth_client.user
        repo_name = self.project.name

        # Update project with has_notebook True
        self.project.has_notebook = True
        self.project.save()

        assert self.model_class.objects.count() == 0

        uploaded_file = self.get_upload_file()

        with patch('repos.tasks.handle_new_files.delay') as mock_task:
            response = self.auth_client.put(self.url,
                                            data={'repo': uploaded_file},
                                            content_type=MULTIPART_CONTENT)
        assert response.status_code == status.HTTP_403_FORBIDDEN
        file_path = '{}/{}/{}.tar.gz'.format(settings.UPLOAD_ROOT,
                                             user.username, repo_name)
        self.assertFalse(os.path.exists(file_path))
        assert mock_task.call_count == 0
        # No new repo was not created and still exists
        assert self.model_class.objects.count() == 0
        repo_path = '{}/{}/{}/{}'.format(settings.REPOS_ROOT, user.username,
                                         repo_name, repo_name)
        self.assertFalse(os.path.exists(repo_path))