Browse Source

Add FakeData generator test utility

This adds a FakeData file-like generator that is able to generate
arbitrary amounts of data when we need to simulate reading from a
file or request pipeline, without having to store all of that in

Change-Id: Iff1fbe2b55f4be12e69c9fd3dec7e3b3e2593e53
(cherry picked from commit dc08127f05)
Dan Smith 1 month ago
2 changed files with 79 additions and 0 deletions
  1. +37
  2. +42

+ 37
- 0
glance/tests/unit/ View File

@@ -0,0 +1,37 @@
# Copyright 2020 Red Hat, Inc
# All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

from glance.tests import utils as test_utils

class TestFakeData(test_utils.BaseTestCase):
def test_via_read(self):
fd = test_utils.FakeData(1024)
data = []
for i in range(0, 1025, 256):
chunk =
if not chunk:

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)))

def test_via_iter(self):
data = b''.join(list(test_utils.FakeData(1024)))
self.assertEqual(1024, len(data))

+ 42
- 0
glance/tests/ View File

@@ -32,6 +32,7 @@ from oslo_config import cfg
from oslo_config import fixture as cfg_fixture
from oslo_log.fixture import logging_error as log_fixture
from oslo_log import log
from oslo_utils import units
import six
from six.moves import BaseHTTPServer
from six.moves import http_client as http
@@ -690,3 +691,44 @@ def start_standalone_http_server():

return thread, httpd, port

class FakeData(object):
"""Generate a bunch of data without storing it in memory.

This acts like a read-only file object which generates fake data
in chunks when read() is called or it is used as a generator. It
can generate an arbitrary amount of data without storing it in

:param length: The number of bytes to generate
:param chunk_size: The chunk size to return in iteration mode, or when
read() is called unbounded

def __init__(self, length, chunk_size=64 * units.Ki):
self._max = length
self._chunk_size = chunk_size
self._len = 0

def read(self, length=None):
if length is None:
length = self._chunk_size

length = min(length, self._max - self._len)

self._len += length
if length == 0:
return b''
return b'0' * length

def __iter__(self):
return self

def __next__(self):
r =
if len(r) == 0:
raise StopIteration()
return r