def test_import_task_status(self):
        self.start_server()

        # Generate 3 MiB of data for the image, enough to get a few
        # status messages
        limit = 3 * units.Mi
        image_id = self._create_and_stage(data_iter=test_utils.FakeData(limit))

        # This utility function will grab the current task status at
        # any time and stash it into a list of statuses if it finds a
        # new one
        statuses = []

        def grab_task_status():
            image = self.api_get('/v2/images/%s' % image_id).json
            task_id = image['os_glance_import_task']
            task = self.api_get('/v2/tasks/%s' % task_id).json
            msg = task['message']
            if msg not in statuses:
                statuses.append(msg)

        # This is the only real thing we have mocked out, which is the
        # "upload this to glance_store" part, which we override so we
        # can control the block size and check our task status
        # synchronously and not depend on timers. It just reads the
        # source data in 64KiB chunks and throws it away.
        def fake_upload(data, *a, **k):
            while True:
                grab_task_status()

                if not data.read(65536):
                    break
                time.sleep(0.1)

        with mock.patch('glance.location.ImageProxy._upload_to_store') as mu:
            mu.side_effect = fake_upload

            # Start the import...
            resp = self._import_direct(image_id, ['store2'])
            self.assertEqual(202, resp.status_code)

            # ...and wait until it finishes
            for i in range(0, 100):
                image = self.api_get('/v2/images/%s' % image_id).json
                if not image.get('os_glance_import_task'):
                    break
                time.sleep(0.1)

        # Image should be in active state and we should have gotten a
        # new message every 1MiB in the process. We mocked StopWatch
        # to always be expired so that we fire the callback every
        # time.
        self.assertEqual('active', image['status'])
        self.assertEqual(['', 'Copied 0 MiB', 'Copied 1 MiB', 'Copied 2 MiB',
                          'Copied 3 MiB'],
                         statuses)
示例#2
0
    def test_via_read(self):
        fd = test_utils.FakeData(1024)
        data = []
        for i in range(0, 1025, 256):
            chunk = fd.read(256)
            data.append(chunk)
            if not chunk:
                break

        self.assertEqual(5, len(data))
        # Make sure we got a zero-length final read
        self.assertEqual(b'', data[-1])
        # Make sure we only got 1024 bytes
        self.assertEqual(1024, len(b''.join(data)))
示例#3
0
 def test_via_iter(self):
     data = b''.join(list(test_utils.FakeData(1024)))
     self.assertEqual(1024, len(data))