示例#1
0
    def test_read_remaining_characters(self):
        the_file = utils.IterableToFileAdapter([
            'chunk1', 'chunk2'
        ])

        self.assertEqual('c', the_file.read(1))
        self.assertEqual('h', the_file.read(1))
示例#2
0
    def test_constructor(self):
        class FakeIterable(object):
            def __iter__(_self):
                return 'iterator'

        the_file = utils.IterableToFileAdapter(FakeIterable())

        self.assertEqual('iterator', the_file.iterator)
示例#3
0
    def test_read_reached_end_of_file(self):
        the_file = utils.IterableToFileAdapter([
            'chunk1', 'chunk2'
        ])

        self.assertEqual('chunk1', the_file.read(100))
        self.assertEqual('chunk2', the_file.read(100))
        self.assertEqual('', the_file.read(100))
示例#4
0
    def test_read_stores_remaining_characters(self):
        the_file = utils.IterableToFileAdapter([
            'chunk1', 'chunk2'
        ])

        the_file.read(1)

        self.assertEquals('hunk1', the_file.remaining_data)
 def download_image(self, context, session, instance, image_id):
     try:
         host_url = CONF.xenserver.connection_url
         image_data = IMAGE_API.download(context, image_id)
         image_stream = utils.IterableToFileAdapter(image_data)
         sr_ref = vm_utils.safe_find_sr(session)
         vdis = xenapi_image.stream_to_vdis(context, session, instance,
                                            host_url, sr_ref, image_stream)
     except xenapi_exception.OsXenApiException as e:
         LOG.error("Image download failed with exception: %s", e)
         raise exception.CouldNotFetchImage(image_id=image_id)
     return vdis
示例#6
0
    def test_as_file(self):
        self.mox.StubOutWithMock(utils, 'IterableToFileAdapter')
        glance_image = self.mox.CreateMock(utils.GlanceImage)
        image = utils.RawTGZImage(glance_image)
        glance_image.data().AndReturn('iterable-data')
        utils.IterableToFileAdapter('iterable-data').AndReturn('data-as-file')

        self.mox.ReplayAll()

        result = image._as_file()

        self.assertEqual('data-as-file', result)
 def upload_image(self, context, session, instance, image_id, vdi_uuids):
     try:
         host_url = CONF.xenserver.connection_url
         level = vm_utils.get_compression_level()
         metadata = self._get_metadata(context, instance, image_id)
         image_chunks = xenapi_image.stream_from_vdis(context,
                                                      session,
                                                      instance,
                                                      host_url,
                                                      vdi_uuids,
                                                      compresslevel=level)
         image_stream = utils.IterableToFileAdapter(image_chunks)
         IMAGE_API.update(context, image_id, metadata, data=image_stream)
     except xenapi_exception.OsXenApiException as e:
         LOG.error("Image upload failed with exception: %s", e)
         raise exception.CouldNotUploadImage(image_id=image_id)
示例#8
0
    def test_empty_chunks(self):
        the_file = utils.IterableToFileAdapter([
            '', '', 'chunk2'
        ])

        self.assertEqual('chunk2', the_file.read(100))
示例#9
0
    def test_read_one_character(self):
        the_file = utils.IterableToFileAdapter(['chunk1', 'chunk2'])

        self.assertEquals('c', the_file.read(1))