Monkeypatch Fake Clients for tests
We currently branch based on the names of providers, images, etc in order to test with our fakes. We can get a lot more coverage by monkeypatching our fakes over the clients they are faking for. Change-Id: If80eb1f4d914f8b2fbd914311ecda2cf66e7a803
This commit is contained in:
parent
7dd935105c
commit
3caaf8ac18
|
@ -31,6 +31,26 @@ class Dummy(object):
|
|||
def delete(self):
|
||||
self.manager.delete(self)
|
||||
|
||||
def update(self, data):
|
||||
try:
|
||||
if self.should_fail:
|
||||
raise RuntimeError('This image has SHOULD_FAIL set to True.')
|
||||
except AttributeError:
|
||||
pass
|
||||
|
||||
|
||||
fake_images_list = None
|
||||
|
||||
|
||||
def get_fake_images_list():
|
||||
global fake_images_list
|
||||
if fake_images_list is None:
|
||||
fake_images_list = FakeList([Dummy(id='fake-image-id',
|
||||
status='READY',
|
||||
name='Fake Precise',
|
||||
metadata={})])
|
||||
return fake_images_list
|
||||
|
||||
|
||||
class FakeList(object):
|
||||
def __init__(self, l):
|
||||
|
@ -67,6 +87,7 @@ class FakeList(object):
|
|||
self._list.remove(self.get(obj))
|
||||
|
||||
def create(self, **kw):
|
||||
should_fail = kw.get('SHOULD_FAIL', '').lower() == 'true'
|
||||
s = Dummy(id=uuid.uuid4().hex,
|
||||
name=kw['name'],
|
||||
status='BUILD',
|
||||
|
@ -76,7 +97,8 @@ class FakeList(object):
|
|||
private=[dict(version=4, addr='fake')]
|
||||
),
|
||||
metadata={},
|
||||
manager=self)
|
||||
manager=self,
|
||||
should_fail=should_fail)
|
||||
self._list.append(s)
|
||||
t = threading.Thread(target=self._finish,
|
||||
name='FakeProvider create',
|
||||
|
@ -97,27 +119,32 @@ class FakeHTTPClient(object):
|
|||
|
||||
|
||||
class FakeClient(object):
|
||||
def __init__(self):
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.flavors = FakeList([
|
||||
Dummy(id='f1', ram=8192, name='Fake Flavor'),
|
||||
Dummy(id='f2', ram=8192, name='Unreal Flavor'),
|
||||
])
|
||||
self.images = FakeList([Dummy(id='i1', name='Fake Precise')])
|
||||
self.images = get_fake_images_list()
|
||||
self.client = FakeHTTPClient()
|
||||
self.servers = FakeList([])
|
||||
self.servers.api = self
|
||||
|
||||
|
||||
class FakeGlanceClient(object):
|
||||
def __init__(self, **kwargs):
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.id = 'fake-glance-id'
|
||||
self.should_fail = kwargs.get('SHOULD_FAIL', '').lower() == 'true'
|
||||
self.images = get_fake_images_list()
|
||||
|
||||
def update(self, **kwargs):
|
||||
if self.should_fail:
|
||||
raise RuntimeError('This image has SHOULD_FAIL set to True.')
|
||||
else:
|
||||
return True
|
||||
|
||||
class FakeServiceCatalog(object):
|
||||
def url_for(self, **kwargs):
|
||||
return 'fake-url'
|
||||
|
||||
|
||||
class FakeKeystoneClient(object):
|
||||
def __init__(self, **kwargs):
|
||||
self.service_catalog = FakeServiceCatalog()
|
||||
self.auth_token = 'fake-auth-token'
|
||||
|
||||
|
||||
class FakeFile(StringIO.StringIO):
|
||||
|
|
|
@ -28,7 +28,6 @@ import glanceclient.client
|
|||
import keystoneclient.v2_0.client as ksclient
|
||||
import time
|
||||
|
||||
import fakeprovider
|
||||
from nodeutils import iterate_timeout
|
||||
from task_manager import Task, TaskManager, ManagerStoppedException
|
||||
|
||||
|
@ -296,8 +295,6 @@ class ProviderManager(TaskManager):
|
|||
kwargs['region_name'] = self.provider.region_name
|
||||
if self.provider.api_timeout:
|
||||
kwargs['timeout'] = self.provider.api_timeout
|
||||
if self.provider.auth_url == 'fake':
|
||||
return fakeprovider.FAKE_CLIENT
|
||||
return novaclient.client.Client(*args, **kwargs)
|
||||
|
||||
def _getFlavors(self):
|
||||
|
@ -503,22 +500,18 @@ class ProviderManager(TaskManager):
|
|||
|
||||
def uploadImage(self, image_name, filename, disk_format, container_format,
|
||||
meta):
|
||||
if image_name.startswith('fake-'):
|
||||
image = fakeprovider.FakeGlanceClient(**meta)
|
||||
image.update(data='fake')
|
||||
else:
|
||||
# configure glance and upload image. Note the meta flags
|
||||
# are provided as custom glance properties
|
||||
glanceclient = self.get_glance_client(self.provider)
|
||||
image = glanceclient.images.create(
|
||||
name=image_name,
|
||||
is_public=False,
|
||||
disk_format=disk_format,
|
||||
container_format=container_format,
|
||||
**meta)
|
||||
filename = '%s.%s' % (filename, disk_format)
|
||||
image.update(data=open(filename, 'rb'))
|
||||
glanceclient = None
|
||||
# configure glance and upload image. Note the meta flags
|
||||
# are provided as custom glance properties
|
||||
glanceclient = self.get_glance_client(self.provider)
|
||||
image = glanceclient.images.create(
|
||||
name=image_name,
|
||||
is_public=False,
|
||||
disk_format=disk_format,
|
||||
container_format=container_format,
|
||||
**meta)
|
||||
filename = '%s.%s' % (filename, disk_format)
|
||||
image.update(data=open(filename, 'rb'))
|
||||
glanceclient = None
|
||||
return image.id
|
||||
|
||||
def listExtensions(self):
|
||||
|
|
|
@ -29,7 +29,7 @@ import fixtures
|
|||
import testresources
|
||||
import testtools
|
||||
|
||||
from nodepool import nodepool, allocation
|
||||
from nodepool import allocation, fakeprovider, nodepool
|
||||
|
||||
TRUE_VALUES = ('true', '1', 'yes')
|
||||
|
||||
|
@ -76,6 +76,16 @@ class BaseTestCase(testtools.TestCase, testresources.ResourcedTestCase):
|
|||
|
||||
self.useFixture(fixtures.MonkeyPatch('subprocess.Popen',
|
||||
LoggingPopenFactory))
|
||||
self.setUpFakes()
|
||||
|
||||
def setUpFakes(self):
|
||||
self.useFixture(fixtures.MonkeyPatch('keystoneclient.v2_0.client.'
|
||||
'Client',
|
||||
fakeprovider.FakeKeystoneClient))
|
||||
self.useFixture(fixtures.MonkeyPatch('glanceclient.client.Client',
|
||||
fakeprovider.FakeGlanceClient))
|
||||
self.useFixture(fixtures.MonkeyPatch('novaclient.client.Client',
|
||||
fakeprovider.FakeClient))
|
||||
|
||||
def wait_for_threads(self):
|
||||
whitelist = ['APScheduler',
|
||||
|
@ -176,11 +186,14 @@ class DBTestCase(BaseTestCase):
|
|||
self.dburi = f.dburi
|
||||
|
||||
def setup_config(self, filename):
|
||||
images_dir = fixtures.TempDir()
|
||||
self.useFixture(images_dir)
|
||||
configfile = os.path.join(os.path.dirname(__file__),
|
||||
'fixtures', filename)
|
||||
config = open(configfile).read()
|
||||
(fd, path) = tempfile.mkstemp()
|
||||
os.write(fd, config.format(dburi=self.dburi))
|
||||
os.write(fd, config.format(dburi=self.dburi,
|
||||
images_dir=images_dir.path))
|
||||
os.close(fd)
|
||||
return path
|
||||
|
||||
|
|
|
@ -38,4 +38,22 @@ if [[ "${BASE_IMAGE_FILE}" != "Fedora-Cloud-Base-20141029-21_Beta.x86_64.qcow2"
|
|||
exit 1
|
||||
fi
|
||||
|
||||
outfile=
|
||||
|
||||
TEMP=$(getopt -o o: -- "$@")
|
||||
eval set -- "$TEMP"
|
||||
while true ; do
|
||||
case "$1" in
|
||||
-o) outfile=$2; shift 2;;
|
||||
--) shift ; break ;;
|
||||
esac
|
||||
done
|
||||
|
||||
if [ -z "$outfile" ]; then
|
||||
echo "No output file specified."
|
||||
exit 1
|
||||
else
|
||||
echo "fake-data" > $outfile
|
||||
fi
|
||||
|
||||
echo "*** fake-image-create: done"
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
script-dir: .
|
||||
dburi: '{dburi}'
|
||||
images-dir: '{images_dir}'
|
||||
|
||||
cron:
|
||||
check: '*/15 * * * *'
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
script-dir: .
|
||||
dburi: '{dburi}'
|
||||
images-dir: '{images_dir}'
|
||||
|
||||
cron:
|
||||
check: '*/15 * * * *'
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
script-dir: .
|
||||
dburi: '{dburi}'
|
||||
images-dir: '{images_dir}'
|
||||
|
||||
cron:
|
||||
check: '*/15 * * * *'
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
script-dir: .
|
||||
elements-dir: .
|
||||
images-dir: .
|
||||
images-dir: '{images_dir}'
|
||||
|
||||
dburi: '{dburi}'
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
script-dir: .
|
||||
elements-dir: .
|
||||
images-dir: .
|
||||
images-dir: '{images_dir}'
|
||||
|
||||
dburi: '{dburi}'
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
script-dir: .
|
||||
elements-dir: .
|
||||
images-dir: .
|
||||
images-dir: '{images_dir}'
|
||||
|
||||
dburi: '{dburi}'
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
script-dir: .
|
||||
elements-dir: .
|
||||
images-dir: .
|
||||
images-dir: '{images_dir}'
|
||||
|
||||
dburi: '{dburi}'
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
script-dir: .
|
||||
dburi: '{dburi}'
|
||||
images-dir: '{images_dir}'
|
||||
|
||||
cron:
|
||||
check: '*/15 * * * *'
|
||||
|
|
Loading…
Reference in New Issue