def _publishDependentCell(self, block_id):
     low, high = self._biigraph._get_range_block(block_id)
     count = 0
     d = self.lbcn
     bcn = d[block_id]
     brl = self._getBlockBRL(bcn)
     for row in range(low, high + 1):
         r = SimpleCell(d[block_id])
         r.root = ID((0, bcn.block_name[-1:], 0))
         deps = DependencySet()
         dep_table = BlockVersionTable()
         for block in range(block_id):
             time = self._biigraph._get_dep_elem_offset(row, block)
             if time != -1:
                 deps.add_implicit(d[block])
                 dep_table[d[block].block_name] = \
                    BlockVersion(self._getBlockBRL(d[block]), time)
         r.dependencies = deps
         pack = PublishRequest(BlockVersion(brl, count - 1))
         pack.tag = STABLE
         count += 1
         pack.cells.append(r)
         pack.contents[r.name.cell_name] = Content(
             id_=None, load=Blob('hola {0}'.format(r.name.cell_name)))
         pack.deptable = dep_table
         self.service.publish(pack)
    def test_publish_dev_with_tag(self, enqueuer):
        brl = BRLBlock('owner/user/block/branch')
        store = Mock(MongoServerStore)
        store.read_block_permissions = Mock(
            return_value=ElementPermissions(brl, private=False))
        user = Mock()
        user.blocks = {}
        store.read_user = Mock(return_value=user)

        block = Mock(Block)
        block.add_publication.return_value = (['mock_id'], [], [], [])
        block.deltas = []
        ensure = Security('authUser', store)
        ensure.check_read_block = Mock(return_value=True)
        ensure.check_create_block = Mock(return_value=True)
        ensure.check_write_block = Mock(return_value=True)
        ensure.check_publish_block = Mock(return_value=True)

        store.read_block.return_value = block
        store.read_published_cells.return_value = {}
        p = PublishService(store, 'authUser')
        p.security = ensure
        pack = PublishRequest(BlockVersion(brl, -1))
        pack.cells.append(SimpleCell('user/block/r1.h'))
        pack.deptable = BlockVersionTable()
        p.publish(pack)
Example #3
0
 def _publishDependentCell(self, block_id):
     low, high = self._biigraph._get_range_block(block_id)
     count = 0
     d = self.lbcn
     bcn = d[block_id]
     brl = self._getBlockBRL(bcn)
     for row in range(low, high + 1):
         r = SimpleCell(d[block_id])
         r.root = ID((0, bcn.block_name[-1:], 0))
         deps = DependencySet()
         dep_table = BlockVersionTable()
         for block in range(block_id):
             time = self._biigraph._get_dep_elem_offset(row, block)
             if time != -1:
                 deps.add_implicit(d[block])
                 dep_table[d[block].block_name] = \
                    BlockVersion(self._getBlockBRL(d[block]), time)
         r.dependencies = deps
         pack = PublishRequest(BlockVersion(brl, count - 1))
         pack.tag = STABLE
         count += 1
         pack.cells.append(r)
         pack.contents[r.name.cell_name] = Content(id_=None, load=Blob('hola {0}'.
                                                             format(r.name.cell_name)))
         pack.deptable = dep_table
         self.service.publish(pack)
    def test_publish_dev_with_tag(self, enqueuer):
        brl = BRLBlock('owner/user/block/branch')
        store = Mock(MongoServerStore)
        store.read_block_permissions = Mock(return_value=ElementPermissions(brl, private=False))
        user = Mock()
        user.blocks = {}
        store.read_user = Mock(return_value=user)

        block = Mock(Block)
        block.add_publication.return_value = (['mock_id'], [], [], [])
        block.deltas = []
        ensure = Security('authUser', store)
        ensure.check_read_block = Mock(return_value=True)
        ensure.check_create_block = Mock(return_value=True)
        ensure.check_write_block = Mock(return_value=True)
        ensure.check_publish_block = Mock(return_value=True)

        store.read_block.return_value = block
        store.read_published_cells.return_value = {}
        p = PublishService(store, 'authUser')
        p.security = ensure
        pack = PublishRequest(BlockVersion(brl, -1))
        pack.cells.append(SimpleCell('user/block/r1.h'))
        pack.deptable = BlockVersionTable()
        p.publish(pack)
Example #5
0
    def publish(self, brl_block, resource_info, version_tag=STABLE, dep_versions=None):
        """ make a simple publication of a single file named block_cell_name to block
        brl_block. If block does not exist, it creates it. It mantains coherence of root Ids
        for the find checks
        param resource_info: {cell_name: (blob, explicits)}
        """
        try:
            block = self.store.read_block(brl_block)
            version = BlockVersion(brl_block, len(block.deltas) - 1)
        except NotInStoreException:
            version = BlockVersion(brl_block, -1)

        publish_request = PublishRequest(version)
        publish_request.tag = version_tag

        block_name = brl_block.block_name
        for cell_name, (blob, dependencies) in resource_info.iteritems():
            if dependencies is not None:
                cell = SimpleCell(block_name + cell_name)
                if isinstance(dependencies, DependencySet):
                    cell.dependencies = dependencies
                else:
                    cell.dependencies.explicit.update([BlockCellName(d) for d in dependencies])
                publish_request.cells.append(cell)
            if blob is not None:
                blob = Blob(blob) if isinstance(blob, str) else blob
                publish_request.contents[cell_name] = Content(id_=None, load=blob)

        if isinstance(dep_versions, BlockVersion):
            dep_versions = [dep_versions]
        publish_request.deptable = BlockVersionTable(dep_versions)
        self.service.publish(publish_request)
Example #6
0
    def test_add_modify_delete_cell(self):
        publish_request = PublishRequest(BlockVersion(self.brl, -1))
        publish_request.cells.append(SimpleCell('user/block/r1.h'))
        publish_request.cells.append(SimpleCell('user/block/r2.h'))
        publish_request.cells.append(SimpleCell('user/block/r3.h'))
        publish_request.contents['r1.h'] = Content(id_=None, load=None)
        publish_request.tag = STABLE
        self.block.add_publication(publish_request)

        ids0 = set(self.block.cells.get_all_ids(0).values())
        names0 = set(self.block.cells.get_all_ids(0).keys())
        expected_ids0 = set([ID(self.block_id + 0), ID(self.block_id + 1), ID(self.block_id + 2)])
        expected_names0 = set(['r1.h', 'r2.h', 'r3.h'])
        self.assertEqual(expected_ids0, ids0)
        self.assertEqual(expected_names0, names0)

        publish_request = PublishRequest(BlockVersion(self.brl, 0))
        publish_request.deleted.append('r1.h')
        publish_request.cells.append(SimpleCell('user/block/r2.h'))
        publish_request.cells.append(SimpleCell('user/block/r4.h'))
        publish_request.tag = STABLE
        self.block.add_publication(publish_request)

        self.assertEqual(expected_ids0, ids0)
        self.assertEqual(expected_names0, names0)

        prev = self.block.cells.get_all_ids(0)
        id3 = prev['r3.h']
        expected = set([id3, ID(self.block_id + 3), ID(self.block_id + 4)])
        self.assertEqual(expected, set(self.block.cells.get_all_ids(1).values()))
        expected = set(['r4.h', 'r2.h', 'r3.h'])
        self.assertEqual(expected, set(self.block.cells.get_all_ids(1).keys()))
        self.assertEqual(expected, set(self.block.last_version_cells().keys()))
 def test_nonzero_one_file_content(self):
     changes = Changes()
     changes.modified['modified_cont'] = Modification(
                                                 Resource(SimpleCell('usr/block/pretty.js'),
                                                          Content(id_=None, load=Blob('mod_content'))),
                                                 Resource(SimpleCell('usr/block/pretty.js'),
                                                          Content(id_=None, load=Blob('mod_content2'))))
     p = PublishRequest(BlockVersion.loads('usr/block: 3'))
     p.msg = "Test Msg"
     p.changes = changes
     self.assertTrue(bool(p))
 def test_nonzero_one_file_content(self):
     changes = Changes()
     changes.modified['modified_cont'] = Modification(
         Resource(SimpleCell('usr/block/pretty.js'),
                  Content(id_=None, load=Blob('mod_content'))),
         Resource(SimpleCell('usr/block/pretty.js'),
                  Content(id_=None, load=Blob('mod_content2'))))
     p = PublishRequest(BlockVersion.loads('usr/block: 3'))
     p.msg = "Test Msg"
     p.changes = changes
     self.assertTrue(bool(p))
    def test_publish_no_contents(self, enqueuer):
        brl = BRLBlock('owner/user/block/branch')
        store = Mock(MongoServerStore)
        store.read_block_permissions = Mock(return_value=ElementPermissions(brl, private=False))

        p = PublishService(store, 'authUser')
        pack = PublishRequest(BlockVersion(brl, -1))
        pack.cells.append(SimpleCell('user/block/r1.h'))
        pack.contents['r1.h'] = Content(id_=None, load=Blob('hola'))
        pack.versiontag = 'mytag'
        with self.assertRaisesRegexp(PublishException, 'DEV'):
            p.publish(pack)
Example #10
0
 def _publishIndependentCell(self, block_cell_name, nVersions=10,
                             version_tag=None):
     '''publishes nVersions of each cell'''
     block_brl = self._getBlockBRL(block_cell_name)
     for v in range(0, nVersions):
         r = SimpleCell(block_cell_name)
         pack = PublishRequest(BlockVersion(block_brl, v - 1))
         pack.tag = version_tag if version_tag is not None else STABLE
         pack.cells.append(r)
         pack.deptable = BlockVersionTable()
         pack.contents[r.name.cell_name] = Content(id_=None, load=Blob('hola {0}'.format(v)))
         self.service.publish(pack)
    def test_publish_no_contents(self, enqueuer):
        brl = BRLBlock('owner/user/block/branch')
        store = Mock(MongoServerStore)
        store.read_block_permissions = Mock(
            return_value=ElementPermissions(brl, private=False))

        p = PublishService(store, 'authUser')
        pack = PublishRequest(BlockVersion(brl, -1))
        pack.cells.append(SimpleCell('user/block/r1.h'))
        pack.contents['r1.h'] = Content(id_=None, load=Blob('hola'))
        pack.versiontag = 'mytag'
        with self.assertRaisesRegexp(PublishException, 'DEV'):
            p.publish(pack)
Example #12
0
    def test_check_publish_block(self):
        # 1. Onwer can write the block if its private
        brl = BRLBlock("goku/goku/block/master")
        self._add_block_to_user("goku", brl, True)

        ensure = Security("goku", self.store)
        self._subscribe("goku", "enterprise_275_50_x")

        pack = PublishRequest(BlockVersion(brl, -1))
        pack.cells.append(SimpleCell('user/block/r1.h'))
        pack.contents['r1.h'] = Content(id_=None, load=Blob('hola'))
        pack.versiontag = 'mytag'

        ensure.check_publish_block(brl, pack)

        # If the owner is not paying he can't write
        ensure = Security("goku", self.store)
        self._subscribe("goku", "free")
        self.assertRaises(ForbiddenException, ensure.check_publish_block, brl, pack)
        self._subscribe("goku", "enterprise_275_50_x")

        # 1b. But other not granted user can't write
        ensure = Security("freezer", self.store)
        self._subscribe("freezer", "enterprise_275_50_x")
        self.assertRaises(ForbiddenException, ensure.check_publish_block, brl, pack)

        # 2. If bulma is granted as administrator or write he can write
        ensure = Security("bulma", self.store)
        self._subscribe("bulma", "enterprise_275_50_x")
        goku = self.store.read_user("goku")
        goku.administrators.grant("bulma")
        self.store.update_user(goku)
        ensure.check_publish_block(brl, pack)
        goku.administrators.revoke("bulma")
        self.store.update_user(goku)
        self.assertRaises(ForbiddenException, ensure.check_publish_block, brl, pack)

        bper = ElementPermissions(brl, private=True)
        bper.write.grant("bulma")
        self.store.upsert_block_permissions(bper)
        ensure.check_publish_block(brl, pack)
        bper.write.remove("bulma")
        self.store.upsert_block_permissions(bper)
        self.assertRaises(ForbiddenException, ensure.check_publish_block, brl, pack)

        # 3. If we give read permissions only, user cant write
        bper = ElementPermissions(brl, private=True)
        bper.read.grant("bulma")
        self.store.upsert_block_permissions(bper)
        self.assertRaises(ForbiddenException, ensure.check_publish_block, brl, pack)
Example #13
0
    def test_renames(self):
        publish_request = PublishRequest(BlockVersion(self.brl, -1))
        publish_request.cells.append(SimpleCell('user/block/r1.h'))
        publish_request.contents['r1.h'] = Content(id_=None, load=None)
        publish_request.cells.append(SimpleCell('user/block/r2.h'))
        publish_request.contents['r2.h'] = Content(id_=None, load=None)
        publish_request.cells.append(SimpleCell('user/block/r3.h'))
        publish_request.contents['r3.h'] = Content(id_=None, load=None)
        publish_request.tag = STABLE
        self.block.add_publication(publish_request)

        publish_request = PublishRequest(BlockVersion(self.brl, 0))
        publish_request.cells.append(SimpleCell('user/block/r11.h'))
        publish_request.deleted.append('r1.h')
        publish_request.renames = Renames({CellName('r1.h'): CellName('r11.h')})
        publish_request.tag = STABLE
        self.block.add_publication(publish_request)

        publish_request = PublishRequest(BlockVersion(self.brl, 1))
        publish_request.cells.append(SimpleCell('user/block/r21.h'))
        publish_request.deleted.append('r2.h')
        publish_request.renames = Renames({CellName('r2.h'): CellName('r21.h')})
        publish_request.tag = STABLE
        self.block.add_publication(publish_request)

        self.assertEquals({}, self.block.get_renames(0, 0))
        self.assertEquals({'r1.h': 'r11.h'}, self.block.get_renames(0, 1))
        self.assertEquals({'r2.h': 'r21.h', 'r1.h': 'r11.h'}, self.block.get_renames(0, 2))
 def test_serialize(self):
     p = PublishRequest(BlockVersion.loads('usr/block: 3'))
     p.msg = "Test Msg"
     p.changes = self._changes()
     serial = p.serialize()
     p2 = PublishRequest.deserialize(serial)
     self.assertEqual(p.parent, p2.parent)
     self.assertEqual(p.tag, p2.tag)
     self.assertEqual(p.msg, p2.msg)
     self.assert_bii_equal(p.cells, p2.cells)
     self.assertEqual(p.contents, p2.contents)
     self.assertEqual(p.contents_ids, p2.contents_ids)
     self.assertEqual(p.deleted, p2.deleted)
     self.assertEqual(p.renames, p2.renames)
 def test_set_changes(self):
     p = PublishRequest()
     changes = self._changes()
     p.changes = changes
     self.assertTrue(bool(p))
     self.assertItemsEqual(['deleted', 'renamed'], p.deleted)
     self.assertEqual(changes.renames, p.renames)
     self.assertEqual({SimpleCell('usr/block/created'),
                       SimpleCell('usr/block/renamed2'),
                       SimpleCell('usr/block/modified_cell', CPP),
                       SimpleCell('usr/block/modified_both', CPP)}, set(p.cells))
     self.assertEqual({'created': Content(id_=None, load=Blob('created')),
                       'modified_cont': Content(id_=None, load=Blob('mod_content2')),
                       'modified_both': Content(id_=None, load=Blob('mod_both2'))}, p.contents)
     self.assertEqual({'renamed2': ID((1234,))}, p.contents_ids)
 def _publishIndependentCell(self,
                             block_cell_name,
                             nVersions=10,
                             version_tag=None):
     '''publishes nVersions of each cell'''
     block_brl = self._getBlockBRL(block_cell_name)
     for v in range(0, nVersions):
         r = SimpleCell(block_cell_name)
         pack = PublishRequest(BlockVersion(block_brl, v - 1))
         pack.tag = version_tag if version_tag is not None else STABLE
         pack.cells.append(r)
         pack.deptable = BlockVersionTable()
         pack.contents[r.name.cell_name] = Content(
             id_=None, load=Blob('hola {0}'.format(v)))
         self.service.publish(pack)
Example #17
0
    def test_add_modify_cell(self):
        publish_request = PublishRequest(BlockVersion(self.brl, -1))
        publish_request.cells.append(SimpleCell('user/block/r1.h'))
        publish_request.tag = STABLE
        self.block.add_publication(publish_request)

        self.assertEqual({'r1.h': ID(self.block_id + 0)}, self.block.cells.get_all_ids(0))

        publish_request = PublishRequest(BlockVersion(self.brl, 0))
        publish_request.cells.append(SimpleCell('user/block/r1.h'))
        publish_request.tag = STABLE
        self.block.add_publication(publish_request)

        self.assertEqual({'r1.h': ID(self.block_id + 0)}, self.block.cells.get_all_ids(0))
        self.assertEqual({'r1.h': ID(self.block_id + 1)}, self.block.cells.get_all_ids(1))
        self.assertEqual({'r1.h': ID(self.block_id + 1)}, self.block.last_version_cells())
    def test_publish_rejected(self):
        store = Mock(MongoServerStore)
        user = Mock()
        user.blocks = {}
        store.read_user = Mock(return_value=user)

        block = Mock(Block)
        block.add_publication.return_value = (Mock(), Mock())
        store.read_block.return_value = block
        brl = BRLBlock('user/user/block/branch')

        p = PublishService(store, 'authUser')
        pack = PublishRequest(BlockVersion(brl, -1))
        pack.cells.append(SimpleCell('user/block/r1.h'))
        pack.contents['r1.h'] = Content(id_=None, load=Blob('hola'))
        pack.deptable = BlockVersionTable()
        with self.assertRaises(ForbiddenException):
            p.publish(pack)
    def test_publish_rejected(self):
        store = Mock(MongoServerStore)
        user = Mock()
        user.blocks = {}
        store.read_user = Mock(return_value=user)

        block = Mock(Block)
        block.add_publication.return_value = (Mock(), Mock())
        store.read_block.return_value = block
        brl = BRLBlock('user/user/block/branch')

        p = PublishService(store, 'authUser')
        pack = PublishRequest(BlockVersion(brl, -1))
        pack.cells.append(SimpleCell('user/block/r1.h'))
        pack.contents['r1.h'] = Content(id_=None, load=Blob('hola'))
        pack.deptable = BlockVersionTable()
        with self.assertRaises(ForbiddenException):
            p.publish(pack)
Example #20
0
 def test_new_cell(self):
     publish_request = PublishRequest(BlockVersion(self.brl, -1))
     publish_request.cells.append(SimpleCell('user/block/r1.h'))
     self.block.add_publication(publish_request)
     self.assertEqual({'r1.h': ID(self.block_id + 0)},
                      self.block.cells.get_all_ids(0))
     self.assertEqual({'r1.h': ID(self.block_id + 0)},
                      self.block.cells.get_all_ids(100))
     self.assertEquals({'r1.h': ID(self.block_id + 0)},
                       self.block.last_version_cells())
Example #21
0
    def test_get_version_by_tag(self):
        brl_block = BRLBlock('%s/%s/TestBlock/master' % (self.testUser.ID, self.testUser.ID))
        publish_request = PublishRequest(BlockVersion(brl_block, -1))
        publish_request.tag = STABLE
        publish_request.versiontag = 'mytag'
        publish_request.cells.append(SimpleCell(brl_block.block_name + 'r1.h'))
        publish_request.contents['r1.h'] = Content(id_=None, load=Blob('hola'))
        publish_request.deptable = BlockVersionTable()
        self.service.publish(publish_request)

        publish_request = PublishRequest(BlockVersion(brl_block, 0, 'mytag'))
        publish_request.tag = STABLE
        publish_request.versiontag = 'mytag'
        publish_request.cells.append(SimpleCell(brl_block.block_name + 'r12.h'))
        publish_request.contents['r2.h'] = Content(id_=None, load=Blob('hola'))
        publish_request.deptable = BlockVersionTable()
        self.service.publish(publish_request)

        block_version = self.service.get_version_by_tag(brl_block, 'mytag')
        self.assertEquals(1, block_version.time)
Example #22
0
def build_publish_request(biiapi, hive_holder, block_name, tag, msg,
                          versiontag, origin, biiout):
    block_name, dep_block_names = _check_input(hive_holder, block_name)
    _check_dep_blocks(biiapi, hive_holder, dep_block_names, biiout)
    block_holder = hive_holder[block_name]
    if not changevalidator.check_block_size(block_holder, biiout):
        raise PublishException("Block is too large to be published")

    parent = block_holder.parent
    _check_possible(parent, biiapi, biiout)

    if parent.time != -1:  # Update
        remote_block_holder = biiapi.get_block_holder(parent)
        base_resources = remote_block_holder.resources
        parent_delta_info = biiapi.get_version_delta_info(parent)
    else:  # New block
        base_resources = None
        parent_delta_info = None
        remote_block_holder = None

    changes = compare(base_resources, block_holder.resources)
    if not block_changed(changes, block_holder, remote_block_holder):
        if parent_delta_info and tag > parent_delta_info.tag:
            biiout.info('No changes, promoting tag %s -> %s' % (parent_delta_info.tag, tag))
            changes.modified.pop(BIICODE_FILE, None)
        else:
            raise UpToDatePublishException("Up to date, nothing to publish")
    changes.deduce_renames()

    request = PublishRequest()
    request.parent = parent
    request.changes = changes
    if parent_delta_info:
        request.parent_time = parent_delta_info.date
    assert all(bv.time is not None for bv in block_holder.requirements.itervalues())
    request.deptable = block_holder.requirements
    request.tag = tag
    request.msg = msg
    request.versiontag = versiontag
    request.origin = origin
    return request
    def setUp(self):
        self.conn.drop_database(self._testMethodName)
        self.store = MongoServerStore(self.conn, self._testMethodName)
        self.mother = ModelCreator(self.store)
        self.user = self.mother.make_test_user()
        self.p = PublishService(self.store, self.user.ID)
        self.brl = BRLBlock('%s/%s/block/master' % (self.user.ID, self.user.ID))

        pack = PublishRequest(BlockVersion(self.brl, -1))
        pack.cells.append(SimpleCell('%s/block/r1.h' % self.user.ID))
        pack.cells.append(SimpleCell('%s/block/r2.h' % self.user.ID))
        pack.cells.append(SimpleCell('%s/block/r3.h' % self.user.ID))
        pack.contents['r1.h'] = Content(id_=None, load=Blob('r1'))
        pack.contents['r2.h'] = Content(id_=None, load=Blob('r2'))
        pack.contents['r3.h'] = Content(id_=None, load=Blob('r3'))
        pack.deptable = BlockVersionTable()
        self.pack = pack

        self.cell_collection = self.store.db[GenericServerStore.PUBLISHED_CELL_ST]
        self.content_collection = self.store.db[GenericServerStore.PUBLISHED_CONTENT_ST]
        self.block_collection = self.store.db[GenericServerStore.BLOCK_ST]
Example #24
0
    def test_add_delete_cell(self):
        publish_request = PublishRequest(BlockVersion(self.brl, -1))
        publish_request.tag = STABLE
        publish_request.cells.append(SimpleCell('user/block/r1.h'))
        publish_request.contents['r1.h'] = Content(id_=None, load=None)
        self.block.add_publication(publish_request)

        self.assertEqual({'r1.h': ID(self.block_id + 0)}, self.block.cells.get_all_ids(0))
        self.assertEqual({'r1.h': ID(self.block_id + 0)}, self.block.contents.get_all_ids(0))

        publish_request = PublishRequest(BlockVersion(self.brl, 0))
        publish_request.tag = STABLE
        publish_request.deleted.append('r1.h')
        self.block.add_publication(publish_request)

        self.assertEqual({'r1.h': self.block_id + 0}, self.block.cells.get_all_ids(0))
        self.assertEqual({'r1.h': self.block_id + 0}, self.block.contents.get_all_ids(0))

        self.assertEqual({}, self.block.cells.get_all_ids(1))
        self.assertEqual({}, self.block.contents.get_all_ids(1))
        self.assertEqual({}, self.block.last_version_cells())
Example #25
0
 def test_set_changes(self):
     p = PublishRequest()
     changes = self._changes()
     p.changes = changes
     self.assertTrue(bool(p))
     self.assertItemsEqual(['deleted', 'renamed'], p.deleted)
     self.assertEqual(changes.renames, p.renames)
     self.assertEqual(
         {
             SimpleCell('usr/block/created'),
             SimpleCell('usr/block/renamed2'),
             SimpleCell('usr/block/modified_cell', CPP),
             SimpleCell('usr/block/modified_both', CPP)
         }, set(p.cells))
     self.assertEqual(
         {
             'created': Content(id_=None, load=Blob('created')),
             'modified_cont': Content(id_=None, load=Blob('mod_content2')),
             'modified_both': Content(id_=None, load=Blob('mod_both2'))
         }, p.contents)
     self.assertEqual({'renamed2': ID((1234, ))}, p.contents_ids)
Example #26
0
    def test_add_delete_cell(self):
        publish_request = PublishRequest(BlockVersion(self.brl, -1))
        publish_request.tag = STABLE
        publish_request.cells.append(SimpleCell('user/block/r1.h'))
        publish_request.contents['r1.h'] = Content(id_=None, load=None)
        self.block.add_publication(publish_request)

        self.assertEqual({'r1.h': ID(self.block_id + 0)},
                         self.block.cells.get_all_ids(0))
        self.assertEqual({'r1.h': ID(self.block_id + 0)},
                         self.block.contents.get_all_ids(0))

        publish_request = PublishRequest(BlockVersion(self.brl, 0))
        publish_request.tag = STABLE
        publish_request.deleted.append('r1.h')
        self.block.add_publication(publish_request)

        self.assertEqual({'r1.h': self.block_id + 0},
                         self.block.cells.get_all_ids(0))
        self.assertEqual({'r1.h': self.block_id + 0},
                         self.block.contents.get_all_ids(0))

        self.assertEqual({}, self.block.cells.get_all_ids(1))
        self.assertEqual({}, self.block.contents.get_all_ids(1))
        self.assertEqual({}, self.block.last_version_cells())
    def test_publish_concurrent_modification(self):
        self.store = TestingMemServerStore()
        brl = 'user'
        self.store.create_user(User(brl))
        self.service = BiiService(self.store, brl)

        self.brl_block = BRLBlock('user/user/block/master')
        request = PublishRequest(BlockVersion(self.brl_block, -1))
        request.cells.append(SimpleCell('user/block/r1.h'))
        request.contents['r1.h'] = Content(id_=None, load=Blob('hola'))
        request.deptable = BlockVersionTable()
        request.tag = DEV
        self.service.publish(request)

        '''Branch 1 (from master) creates new resource'''
        self.brl_block1 = BRLBlock('user/user/block/branch1')
        request = PublishRequest(BlockVersion(self.brl_block, 0))
        request.cells.append(SimpleCell('user/block/r2.h'))
        request.contents['r2.h'] = Content(id_=None, load=Blob('adios'))
        request.deptable = BlockVersionTable()
        with self.assertRaisesRegexp(BiiException, 'Concurrent modification'):
            self.service.publish(request)
Example #28
0
    def publish(self,
                brl_block,
                resource_info,
                version_tag=STABLE,
                dep_versions=None):
        """ make a simple publication of a single file named block_cell_name to block
        brl_block. If block does not exist, it creates it. It mantains coherence of root Ids
        for the find checks
        param resource_info: {cell_name: (blob, explicits)}
        """
        try:
            block = self.store.read_block(brl_block)
            version = BlockVersion(brl_block, len(block.deltas) - 1)
        except NotInStoreException:
            version = BlockVersion(brl_block, -1)

        publish_request = PublishRequest(version)
        publish_request.tag = version_tag

        block_name = brl_block.block_name
        for cell_name, (blob, dependencies) in resource_info.iteritems():
            if dependencies is not None:
                cell = SimpleCell(block_name + cell_name)
                if isinstance(dependencies, DependencySet):
                    cell.dependencies = dependencies
                else:
                    cell.dependencies.explicit.update(
                        [BlockCellName(d) for d in dependencies])
                publish_request.cells.append(cell)
            if blob is not None:
                blob = Blob(blob) if isinstance(blob, str) else blob
                publish_request.contents[cell_name] = Content(id_=None,
                                                              load=blob)

        if isinstance(dep_versions, BlockVersion):
            dep_versions = [dep_versions]
        publish_request.deptable = BlockVersionTable(dep_versions)
        self.service.publish(publish_request)
    def test_publish(self, enqueuer):
        brl = BRLBlock('owner/user/block/branch')
        # moduleID=BlockID(UserID(123),456)
        store = Mock(MongoServerStore)
        store.read_block_permissions = Mock(return_value=ElementPermissions(brl, private=False))
        user = User("owner")
        user.numeric_id = 1
        user.blocks = {}
        store.read_user = Mock(return_value=user)
        block = Mock(Block)
        block.last_version.return_value = Mock(BlockVersion)
        block.add_publication.return_value = (Mock(list), Mock(list), Mock(list), Mock(list))
        block.deltas = []

        ensure = Security('authUser', store)
        ensure.check_create_block = Mock(return_value=True)
        ensure.check_write_block = Mock(return_value=True)
        ensure.check_read_block = Mock(return_value=True)

        store.read_block.return_value = block
        store.read_published_cells.return_value = {}
        p = PublishService(store, 'authUser')
        p.security = ensure

        pack = PublishRequest(BlockVersion(brl, -1))
        pack.cells.append(SimpleCell('user/block/r1.h'))
        pack.contents['r1.h'] = Content(id_=None, load=Blob('hola'))
        pack.cells.append(SimpleCell('user/block/r2.h'))
        pack.contents['r2.h'] = Content(id_=None, load=Blob('hola'))
        pack.cells.append(SimpleCell('user/block/r3.h'))
        pack.contents['r3.h'] = Content(id_=None, load=Blob('hola'))
        pack.deptable = BlockVersionTable()
        p.publish(pack)

        block.add_publication.assert_called_once_with(pack, p.auth_user)
        store.update_block.assert_called_once_with(block)
        self.assertEqual(1, store.create_published_cells.call_count)
        self.assertEqual(1, store.create_published_contents.call_count)

        # Check sizes
        self.assertEquals(user.blocks_bytes, 12)  # 12 bytes "hola" * 3

        # Publish again, see the size incremented
        pack._bytes = None  # Lazy computed
        p.publish(pack)
        self.assertEquals(user.blocks_bytes, 24)  # 24 bytes: "hola" * 3 * 2 publications

        # Max size exceeded for user
        user.max_workspace_size = 25
        self.assertRaises(ForbiddenException, p.publish, pack)

        # Try to publish only 1 byte
        pack._bytes = None  # Lazy computed
        pack.cells = []
        pack.contents = {}
        pack.cells.append(SimpleCell('user/block/r1.h'))
        pack.contents['r1.h'] = Content(id_=None, load=Blob('h'))
        p.publish(pack)
    def setUp(self):
        self.conn.drop_database(self._testMethodName)
        self.store = MongoServerStore(self.conn, self._testMethodName)
        self.mother = ModelCreator(self.store)
        self.user = self.mother.make_test_user()
        self.p = PublishService(self.store, self.user.ID)
        self.brl = BRLBlock('%s/%s/block/master' %
                            (self.user.ID, self.user.ID))

        pack = PublishRequest(BlockVersion(self.brl, -1))
        pack.cells.append(SimpleCell('%s/block/r1.h' % self.user.ID))
        pack.cells.append(SimpleCell('%s/block/r2.h' % self.user.ID))
        pack.cells.append(SimpleCell('%s/block/r3.h' % self.user.ID))
        pack.contents['r1.h'] = Content(id_=None, load=Blob('r1'))
        pack.contents['r2.h'] = Content(id_=None, load=Blob('r2'))
        pack.contents['r3.h'] = Content(id_=None, load=Blob('r3'))
        pack.deptable = BlockVersionTable()
        self.pack = pack

        self.cell_collection = self.store.db[
            GenericServerStore.PUBLISHED_CELL_ST]
        self.content_collection = self.store.db[
            GenericServerStore.PUBLISHED_CONTENT_ST]
        self.block_collection = self.store.db[GenericServerStore.BLOCK_ST]
def build_publish_request(biiapi, hive_holder, block_name, tag, msg,
                          versiontag, origin, biiout):
    block_name, dep_block_names = _check_input(hive_holder, block_name)
    _check_dep_blocks(biiapi, hive_holder, dep_block_names, biiout)
    block_holder = hive_holder[block_name]
    if not changevalidator.check_block_size(block_holder, biiout):
        raise PublishException("Block is too large to be published")

    parent = block_holder.parent
    _check_possible(parent, biiapi, biiout)

    if parent.time != -1:  # Update
        remote_block_holder = biiapi.get_block_holder(parent)
        base_resources = remote_block_holder.resources
        parent_delta_info = biiapi.get_version_delta_info(parent)
    else:  # New block
        base_resources = None
        parent_delta_info = None
        remote_block_holder = None

    changes = compare(base_resources, block_holder.resources)
    if not block_changed(changes, block_holder, remote_block_holder):
        if parent_delta_info and tag > parent_delta_info.tag:
            biiout.info('No changes, promoting tag %s -> %s' % (parent_delta_info.tag, tag))
            changes.modified.pop(BIICODE_FILE, None)
        else:
            raise UpToDatePublishException("Up to date, nothing to publish")
    changes.deduce_renames()

    request = PublishRequest()
    request.parent = parent
    request.changes = changes
    if parent_delta_info:
        request.parent_time = parent_delta_info.date
    assert all(bv.time is not None for bv in block_holder.requirements.itervalues())
    request.deptable = block_holder.requirements
    request.tag = tag
    request.msg = msg
    request.versiontag = versiontag
    request.origin = origin
    return request
    def test_read_published_blocks_info(self, store_cls):
        """Insert a block and read all published blocks info (brl, lastpubdate)"""
        if store_cls == MongoServerStore:
            store = MongoServerStore(self.conn, self.__class__.__name__)
        else:
            store = TestingMemServerStore()
        block = Block(ID((23, 23)), BRLBlock("bonjovi/bonjovi/itsmylife/master"))
        ppack = PublishRequest(block.last_version())
        r1 = SimpleCell('user/block/r1.h')
        ppack.cells.append(r1)
        ppack.contents['r1.h'] = Content(id_=None, load=Blob('hola'))

        block.add_publication(ppack)
        store.create_block(block, False)
        ret = store.read_published_blocks_info()
        first = ret.next()

        self.assertEquals(first[0], "bonjovi/bonjovi/itsmylife/master")
        self.assertEquals(first[1].__class__, datetime.datetime)
Example #33
0
 def test_serialize(self):
     p = PublishRequest(BlockVersion.loads('usr/block: 3'))
     p.msg = "Test Msg"
     p.changes = self._changes()
     serial = p.serialize()
     p2 = PublishRequest.deserialize(serial)
     self.assertEqual(p.parent, p2.parent)
     self.assertEqual(p.tag, p2.tag)
     self.assertEqual(p.msg, p2.msg)
     self.assert_bii_equal(p.cells, p2.cells)
     self.assertEqual(p.contents, p2.contents)
     self.assertEqual(p.contents_ids, p2.contents_ids)
     self.assertEqual(p.deleted, p2.deleted)
     self.assertEqual(p.renames, p2.renames)
    def test_publish_concurrent_modification(self):
        self.store = TestingMemServerStore()
        brl = 'user'
        self.store.create_user(User(brl))
        self.service = BiiService(self.store, brl)

        self.brl_block = BRLBlock('user/user/block/master')
        request = PublishRequest(BlockVersion(self.brl_block, -1))
        request.cells.append(SimpleCell('user/block/r1.h'))
        request.contents['r1.h'] = Content(id_=None, load=Blob('hola'))
        request.deptable = BlockVersionTable()
        request.tag = DEV
        self.service.publish(request)
        '''Branch 1 (from master) creates new resource'''
        self.brl_block1 = BRLBlock('user/user/block/branch1')
        request = PublishRequest(BlockVersion(self.brl_block, 0))
        request.cells.append(SimpleCell('user/block/r2.h'))
        request.contents['r2.h'] = Content(id_=None, load=Blob('adios'))
        request.deptable = BlockVersionTable()
        with self.assertRaisesRegexp(BiiException, 'Concurrent modification'):
            self.service.publish(request)
Example #35
0
    def test_add_modify_delete_cell(self):
        publish_request = PublishRequest(BlockVersion(self.brl, -1))
        publish_request.cells.append(SimpleCell('user/block/r1.h'))
        publish_request.cells.append(SimpleCell('user/block/r2.h'))
        publish_request.cells.append(SimpleCell('user/block/r3.h'))
        publish_request.contents['r1.h'] = Content(id_=None, load=None)
        publish_request.tag = STABLE
        self.block.add_publication(publish_request)

        ids0 = set(self.block.cells.get_all_ids(0).values())
        names0 = set(self.block.cells.get_all_ids(0).keys())
        expected_ids0 = set([
            ID(self.block_id + 0),
            ID(self.block_id + 1),
            ID(self.block_id + 2)
        ])
        expected_names0 = set(['r1.h', 'r2.h', 'r3.h'])
        self.assertEqual(expected_ids0, ids0)
        self.assertEqual(expected_names0, names0)

        publish_request = PublishRequest(BlockVersion(self.brl, 0))
        publish_request.deleted.append('r1.h')
        publish_request.cells.append(SimpleCell('user/block/r2.h'))
        publish_request.cells.append(SimpleCell('user/block/r4.h'))
        publish_request.tag = STABLE
        self.block.add_publication(publish_request)

        self.assertEqual(expected_ids0, ids0)
        self.assertEqual(expected_names0, names0)

        prev = self.block.cells.get_all_ids(0)
        id3 = prev['r3.h']
        expected = set([id3, ID(self.block_id + 3), ID(self.block_id + 4)])
        self.assertEqual(expected,
                         set(self.block.cells.get_all_ids(1).values()))
        expected = set(['r4.h', 'r2.h', 'r3.h'])
        self.assertEqual(expected, set(self.block.cells.get_all_ids(1).keys()))
        self.assertEqual(expected, set(self.block.last_version_cells().keys()))
Example #36
0
 def test_empty_changes(self):
     p = PublishRequest()
     self.assertFalse(bool(p))
Example #37
0
def publish(auth_user, bson_data):
    service = BiiService(app.store, auth_user)
    publish_request = PublishRequest.deserialize(bson_data["data"])
    return service.publish(publish_request)
    def test_publish(self, enqueuer):
        brl = BRLBlock('owner/user/block/branch')
        # moduleID=BlockID(UserID(123),456)
        store = Mock(MongoServerStore)
        store.read_block_permissions = Mock(
            return_value=ElementPermissions(brl, private=False))
        user = User("owner")
        user.numeric_id = 1
        user.blocks = {}
        store.read_user = Mock(return_value=user)
        block = Mock(Block)
        block.last_version.return_value = Mock(BlockVersion)
        block.add_publication.return_value = (Mock(list), Mock(list),
                                              Mock(list), Mock(list))
        block.deltas = []

        ensure = Security('authUser', store)
        ensure.check_create_block = Mock(return_value=True)
        ensure.check_write_block = Mock(return_value=True)
        ensure.check_read_block = Mock(return_value=True)

        store.read_block.return_value = block
        store.read_published_cells.return_value = {}
        p = PublishService(store, 'authUser')
        p.security = ensure

        pack = PublishRequest(BlockVersion(brl, -1))
        pack.cells.append(SimpleCell('user/block/r1.h'))
        pack.contents['r1.h'] = Content(id_=None, load=Blob('hola'))
        pack.cells.append(SimpleCell('user/block/r2.h'))
        pack.contents['r2.h'] = Content(id_=None, load=Blob('hola'))
        pack.cells.append(SimpleCell('user/block/r3.h'))
        pack.contents['r3.h'] = Content(id_=None, load=Blob('hola'))
        pack.deptable = BlockVersionTable()
        p.publish(pack)

        block.add_publication.assert_called_once_with(pack, p.auth_user)
        store.update_block.assert_called_once_with(block)
        self.assertEqual(1, store.create_published_cells.call_count)
        self.assertEqual(1, store.create_published_contents.call_count)

        # Check sizes
        self.assertEquals(user.blocks_bytes, 12)  # 12 bytes "hola" * 3

        # Publish again, see the size incremented
        pack._bytes = None  # Lazy computed
        p.publish(pack)
        self.assertEquals(user.blocks_bytes,
                          24)  # 24 bytes: "hola" * 3 * 2 publications

        # Max size exceeded for user
        user.max_workspace_size = 25
        self.assertRaises(ForbiddenException, p.publish, pack)

        # Try to publish only 1 byte
        pack._bytes = None  # Lazy computed
        pack.cells = []
        pack.contents = {}
        pack.cells.append(SimpleCell('user/block/r1.h'))
        pack.contents['r1.h'] = Content(id_=None, load=Blob('h'))
        p.publish(pack)
Example #39
0
    def test_renames(self):
        publish_request = PublishRequest(BlockVersion(self.brl, -1))
        publish_request.cells.append(SimpleCell('user/block/r1.h'))
        publish_request.contents['r1.h'] = Content(id_=None, load=None)
        publish_request.cells.append(SimpleCell('user/block/r2.h'))
        publish_request.contents['r2.h'] = Content(id_=None, load=None)
        publish_request.cells.append(SimpleCell('user/block/r3.h'))
        publish_request.contents['r3.h'] = Content(id_=None, load=None)
        publish_request.tag = STABLE
        self.block.add_publication(publish_request)

        publish_request = PublishRequest(BlockVersion(self.brl, 0))
        publish_request.cells.append(SimpleCell('user/block/r11.h'))
        publish_request.deleted.append('r1.h')
        publish_request.renames = Renames(
            {CellName('r1.h'): CellName('r11.h')})
        publish_request.tag = STABLE
        self.block.add_publication(publish_request)

        publish_request = PublishRequest(BlockVersion(self.brl, 1))
        publish_request.cells.append(SimpleCell('user/block/r21.h'))
        publish_request.deleted.append('r2.h')
        publish_request.renames = Renames(
            {CellName('r2.h'): CellName('r21.h')})
        publish_request.tag = STABLE
        self.block.add_publication(publish_request)

        self.assertEquals({}, self.block.get_renames(0, 0))
        self.assertEquals({'r1.h': 'r11.h'}, self.block.get_renames(0, 1))
        self.assertEquals({
            'r2.h': 'r21.h',
            'r1.h': 'r11.h'
        }, self.block.get_renames(0, 2))