def test_workspace_deletion(app, session, default_user,
                            sample_yadage_workflow_in_db,
                            tmp_shared_volume_path, workspace, hard_delete):
    """Test workspace deletion."""
    workflow = sample_yadage_workflow_in_db
    create_workflow_workspace(sample_yadage_workflow_in_db.get_workspace())
    absolute_workflow_workspace = os.path.join(tmp_shared_volume_path,
                                               workflow.get_workspace())

    # create a job for the workflow
    workflow_job = Job(id_=uuid.uuid4(), workflow_uuid=workflow.id_)
    job_cache_entry = JobCache(job_id=workflow_job.id_)
    session.add(workflow_job)
    session.add(job_cache_entry)
    session.commit()

    # create cached workspace
    cache_dir_path = os.path.abspath(
        os.path.join(absolute_workflow_workspace, os.pardir, 'archive',
                     str(workflow_job.id_)))
    os.makedirs(cache_dir_path)

    # check that the workflow workspace exists
    assert os.path.exists(absolute_workflow_workspace)
    assert os.path.exists(cache_dir_path)
    _delete_workflow(workflow, hard_delete=hard_delete, workspace=workspace)
    if hard_delete or workspace:
        assert not os.path.exists(absolute_workflow_workspace)

    # check that all cache entries for jobs
    # of the deleted workflow are removed
    cache_entries_after_delete = JobCache.query.filter_by(
        job_id=workflow_job.id_).all()
    assert not cache_entries_after_delete
    assert not os.path.exists(cache_dir_path)
def test_delete_workflow(app, session, default_user,
                         sample_yadage_workflow_in_db, status, hard_delete):
    """Test deletion of a workflow in all possible statuses."""
    sample_yadage_workflow_in_db.status = status
    session.add(sample_yadage_workflow_in_db)
    session.commit()

    _delete_workflow(sample_yadage_workflow_in_db, hard_delete=hard_delete)
    if not hard_delete:
        assert sample_yadage_workflow_in_db.status == WorkflowStatus.deleted
    else:
        assert session.query(Workflow).filter_by(
            id_=sample_yadage_workflow_in_db.id_).all() == []
def test_workspace_permissions(app, session, default_user,
                               sample_yadage_workflow_in_db,
                               tmp_shared_volume_path):
    """Test workspace dir permissions."""
    create_workflow_workspace(sample_yadage_workflow_in_db.get_workspace())
    expeted_worspace_permissions = 'drwxrwxr-x'
    absolute_workflow_workspace = os.path.join(
        tmp_shared_volume_path, sample_yadage_workflow_in_db.get_workspace())
    workspace_permissions = \
        stat.filemode(os.stat(absolute_workflow_workspace).st_mode)
    assert os.path.exists(absolute_workflow_workspace)
    assert workspace_permissions == expeted_worspace_permissions
    _delete_workflow(sample_yadage_workflow_in_db,
                     hard_delete=True,
                     workspace=True)
def test_deletion_of_workspace_of_an_already_deleted_workflow(
        app,
        session,
        default_user,
        yadage_workflow_with_name,
        tmp_shared_volume_path):
    """Test workspace deletion of an already deleted workflow."""
    with app.test_client() as client:
        res = client.post(url_for('api.create_workflow'),
                          query_string={
                              "user": default_user.id_},
                          content_type='application/json',
                          data=json.dumps(yadage_workflow_with_name))
        assert res.status_code == 201
        response_data = json.loads(res.get_data(as_text=True))

        workflow = Workflow.query.filter(
            Workflow.id_ == response_data.get('workflow_id')).first()
        assert workflow

        absolute_workflow_workspace = os.path.join(
            tmp_shared_volume_path,
            workflow.get_workspace())

        # check that the workflow workspace exists
        assert os.path.exists(absolute_workflow_workspace)
        with app.test_client() as client:
            res = client.put(
                url_for('api.set_workflow_status',
                        workflow_id_or_name=workflow.id_),
                query_string={
                    'user': default_user.id_,
                    'status': 'deleted'
                },
                content_type='application/json',
                data=json.dumps({'hard_delete': False,
                                 'workspace': False}))
        assert os.path.exists(absolute_workflow_workspace)

        _delete_workflow(workflow,
                         hard_delete=False,
                         workspace=True)
        assert not os.path.exists(absolute_workflow_workspace)
def test_delete_all_workflow_runs(app, session, default_user,
                                  yadage_workflow_with_name, hard_delete):
    """Test deletion of all runs of a given workflow."""
    # add 5 workflows in the database with the same name
    for i in range(5):
        workflow = Workflow(
            id_=uuid.uuid4(),
            name=yadage_workflow_with_name['name'],
            owner_id=default_user.id_,
            reana_specification=yadage_workflow_with_name[
                'reana_specification'],
            operational_options={},
            type_=yadage_workflow_with_name['reana_specification']['workflow']
            ['type'],
            logs='')
        session.add(workflow)
        if i == 4:
            workflow.status = WorkflowStatus.running
            not_deleted_one = workflow.id_
        session.commit()

    first_workflow = session.query(Workflow).\
        filter_by(name=yadage_workflow_with_name['name']).first()
    _delete_workflow(first_workflow, all_runs=True, hard_delete=hard_delete)
    if not hard_delete:
        for workflow in session.query(Workflow).\
                filter_by(name=first_workflow.name).all():
            if not_deleted_one == workflow.id_:
                assert workflow.status == WorkflowStatus.running
            else:
                assert workflow.status == WorkflowStatus.deleted
    else:
        # the one running should not be deleted
        assert len(
            session.query(Workflow).filter_by(
                name=first_workflow.name).all()) == 1
def test_deletion_of_workspace_of_an_already_deleted_workflow(
        app, session, default_user, sample_yadage_workflow_in_db,
        tmp_shared_volume_path):
    """Test workspace deletion of an already deleted workflow."""
    create_workflow_workspace(sample_yadage_workflow_in_db.get_workspace())
    absolute_workflow_workspace = os.path.join(
        tmp_shared_volume_path, sample_yadage_workflow_in_db.get_workspace())

    # check that the workflow workspace exists
    assert os.path.exists(absolute_workflow_workspace)
    _delete_workflow(sample_yadage_workflow_in_db,
                     hard_delete=False,
                     workspace=False)
    assert os.path.exists(absolute_workflow_workspace)

    _delete_workflow(sample_yadage_workflow_in_db,
                     hard_delete=False,
                     workspace=True)
    assert not os.path.exists(absolute_workflow_workspace)

    _delete_workflow(sample_yadage_workflow_in_db,
                     hard_delete=True,
                     workspace=True)