Пример #1
0
    def test_start(self):
        outf = six.StringIO()
        producer = vdi_through_dev.TarGzProducer('fpath', outf,
            '100', 'fname')

        tfile = self.mox.CreateMock(tarfile.TarFile)
        tinfo = self.mox.CreateMock(tarfile.TarInfo)

        inf = self.mox.CreateMock(open)

        self.mox.StubOutWithMock(vdi_through_dev, 'tarfile')
        self.mox.StubOutWithMock(producer, '_open_file')

        vdi_through_dev.tarfile.TarInfo(name='fname').AndReturn(tinfo)
        vdi_through_dev.tarfile.open(fileobj=outf, mode='w|gz').AndReturn(
            fake_context(tfile))
        producer._open_file('fpath', 'rb').AndReturn(fake_context(inf))
        tfile.addfile(tinfo, fileobj=inf)
        outf.close()

        self.mox.ReplayAll()

        producer.start()

        self.assertEqual(100, tinfo.size)
Пример #2
0
    def test__perform_upload(self):
        producer = self.mox.CreateMock(vdi_through_dev.TarGzProducer)
        consumer = self.mox.CreateMock(glance.UpdateGlanceImage)
        pool = self.mox.CreateMock(eventlet.GreenPool)
        store = vdi_through_dev.UploadToGlanceAsRawTgz('context', 'session',
                                                       'instance', 'id',
                                                       ['vdi0', 'vdi1'])
        self.mox.StubOutWithMock(store, '_create_pipe')
        self.mox.StubOutWithMock(store, '_get_virtual_size')
        self.mox.StubOutWithMock(producer, 'get_metadata')
        self.mox.StubOutWithMock(vdi_through_dev, 'TarGzProducer')
        self.mox.StubOutWithMock(glance, 'UpdateGlanceImage')
        self.mox.StubOutWithMock(vdi_through_dev, 'eventlet')

        producer.get_metadata().AndReturn('metadata')
        store._get_virtual_size().AndReturn('324')
        store._create_pipe().AndReturn(('readfile', 'writefile'))
        vdi_through_dev.TarGzProducer('devpath', 'writefile', '324',
                                      'disk.raw').AndReturn(producer)
        glance.UpdateGlanceImage('context', 'id', 'metadata',
                                 'readfile').AndReturn(consumer)
        vdi_through_dev.eventlet.GreenPool().AndReturn(pool)
        pool.spawn(producer.start)
        pool.spawn(consumer.start)
        pool.waitall()

        self.mox.ReplayAll()

        store._perform_upload('devpath')
Пример #3
0
    def test_get_metadata(self):
        producer = vdi_through_dev.TarGzProducer('devpath', 'writefile',
            '100', 'fname')

        self.assertEqual({
            'disk_format': 'raw',
            'container_format': 'tgz'},
            producer.get_metadata())
Пример #4
0
    def test_constructor(self):
        producer = vdi_through_dev.TarGzProducer('devpath', 'writefile',
            '100', 'fname')

        self.assertEqual('devpath', producer.fpath)
        self.assertEqual('writefile', producer.output)
        self.assertEqual('100', producer.size)
        self.assertEqual('writefile', producer.output)
Пример #5
0
    def test_start(self, mock_tar_open, mock_tar_TarInfo):
        outf = six.StringIO()
        producer = vdi_through_dev.TarGzProducer('fpath', outf, '100', 'fname')

        tfile = mock.create_autospec(tarfile.TarFile, spec_set=True)
        tinfo = mock.create_autospec(tarfile.TarInfo)

        inf = mock.create_autospec(open, spec_set=True)

        mock_tar_open.return_value = fake_context(tfile)
        mock_tar_TarInfo.return_value = tinfo

        with mock.patch.object(
                producer, '_open_file',
                return_value=fake_context(inf)) as mock_open_file:
            producer.start()

            self.assertEqual(100, tinfo.size)
            mock_tar_TarInfo.assert_called_once_with(name='fname')
            mock_tar_open.assert_called_once_with(fileobj=outf, mode='w|gz')
            mock_open_file.assert_called_once_with('fpath', 'rb')
            tfile.addfile.assert_called_once_with(tinfo, fileobj=inf)